+ add tests - 4 failing in TestModuleImporter

This commit is contained in:
meisl 2021-06-19 20:27:04 +02:00
parent ce554f7718
commit b071a58ca7
2 changed files with 99 additions and 3 deletions

View File

@ -13,9 +13,12 @@ import prog8.parser.ModuleImporter
import prog8.parser.ParseError import prog8.parser.ParseError
import java.nio.file.Path import java.nio.file.Path
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import kotlin.io.path.isRegularFile
import kotlin.test.* import kotlin.test.*
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class TestModuleImporter { class TestModuleImporter {
object DummyEncoding: IStringEncoding { object DummyEncoding: IStringEncoding {
@ -39,6 +42,7 @@ class TestModuleImporter {
override fun memorySize(dt: DataType): Int = 0 override fun memorySize(dt: DataType): Int = 0
} }
@Test @Test
fun testImportModuleWithSyntaxError() { fun testImportModuleWithSyntaxError() {
val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer) val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer)
@ -59,7 +63,53 @@ class TestModuleImporter {
} }
@Test @Test
fun testImportLibraryModuleImportingBadModule() { fun testImportModuleWithImportingModuleWithSyntaxError() {
val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer)
val importer = ModuleImporter(program, DummyEncoding, "blah", listOf("./test/fixtures"))
val importing = Path.of("test", "fixtures", "import_file_with_syntax_error.p8")
val imported = Path.of("test", "fixtures", "file_with_syntax_error.p8")
val act = { importer.importModule(importing) }
assertTrue(importing.isRegularFile(), "sanity check: should be regular file")
assertFailsWith<ParseError> { act() }
try {
act()
} catch (e: ParseError) {
assertEquals(imported.fileName.toString(), e.position.file, "provenance; should be the importED file's filename, incl. extension '.p8'")
assertEquals(2, e.position.line, "line; should be 1-based")
assertEquals(6, e.position.startCol, "startCol; should be 0-based" )
assertEquals(6, e.position.endCol, "endCol; should be 0-based")
}
}
@Test
fun testImportLibraryModuleWithSyntaxError() {
val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer)
val importer = ModuleImporter(program, DummyEncoding, "blah", listOf("./test/fixtures"))
val filename = "file_with_syntax_error"
val act = { importer.importLibraryModule(filename) }
assertFailsWith<ParseError> { act() }
try {
act()
} catch (e: ParseError) {
assertEquals(
filename + ".p8",
e.position.file,
"provenance; should be the path's filename, incl. extension '.p8'"
)
assertEquals(2, e.position.line, "line; should be 1-based")
assertEquals(6, e.position.startCol, "startCol; should be 0-based")
assertEquals(6, e.position.endCol, "endCol; should be 0-based")
}
}
@Test
fun testImportLibraryModuleWithImportingBadModule() {
val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer) val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer)
val importer = ModuleImporter(program, DummyEncoding, "blah", listOf("./test/fixtures")) val importer = ModuleImporter(program, DummyEncoding, "blah", listOf("./test/fixtures"))
@ -71,9 +121,13 @@ class TestModuleImporter {
try { try {
act() act()
} catch (e: ParseError) { } catch (e: ParseError) {
assertEquals(imported + ".p8", e.position.file, "provenance; should be the importED file's name, incl. extension '.p8'") assertEquals(
imported + ".p8",
e.position.file,
"provenance; should be the importED file's name, incl. extension '.p8'"
)
assertEquals(2, e.position.line, "line; should be 1-based") assertEquals(2, e.position.line, "line; should be 1-based")
assertEquals(6, e.position.startCol, "startCol; should be 0-based" ) assertEquals(6, e.position.startCol, "startCol; should be 0-based")
assertEquals(6, e.position.endCol, "endCol; should be 0-based") assertEquals(6, e.position.endCol, "endCol; should be 0-based")
} }
} }

View File

@ -3,8 +3,13 @@ package prog8tests
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import prog8.ast.statements.Block import prog8.ast.statements.Block
import prog8.parser.ParseError import prog8.parser.ParseError
import prog8.parser.Prog8Parser
import prog8.parser.Prog8Parser.parseModule import prog8.parser.Prog8Parser.parseModule
import java.nio.file.Path import java.nio.file.Path
import kotlin.io.path.exists
import kotlin.io.path.isDirectory
import kotlin.io.path.isReadable
import kotlin.io.path.isRegularFile
import kotlin.test.* import kotlin.test.*
class TestProg8Parser { class TestProg8Parser {
@ -148,6 +153,43 @@ class TestProg8Parser {
""") } """) }
} }
@Test
fun testParseModuleWithDirectoryPath() {
val srcPath = Path.of("test", "fixtures")
assertTrue(srcPath.isDirectory(), "sanity check: should be a directory")
assertFailsWith<java.nio.file.AccessDeniedException> { Prog8Parser.parseModule(srcPath) }
}
@Test
fun testParseModuleWithNonExistingPath() {
val srcPath = Path.of("test", "fixtures", "i_do_not_exist")
assertFalse(srcPath.exists(), "sanity check: file should not exist")
assertFailsWith<java.nio.file.NoSuchFileException> { Prog8Parser.parseModule(srcPath) }
}
@Test
fun testParseModuleWithPathMissingExtension_p8() {
val srcPathWithoutExt = Path.of("test", "fixtures", "file_with_syntax_error")
val srcPathWithExt = Path.of(srcPathWithoutExt.toString() + ".p8")
assertTrue(srcPathWithExt.isRegularFile(), "sanity check: should be normal file")
assertTrue(srcPathWithExt.isReadable(), "sanity check: should be readable")
assertFailsWith<java.nio.file.NoSuchFileException> { Prog8Parser.parseModule(srcPathWithoutExt) }
}
@Test
fun testParseModuleWithStringShouldNotLookAtImports() {
val srcText = "%import i_do_not_exist"
val module = Prog8Parser.parseModule(srcText)
assertEquals(1, module.statements.size)
}
@Test
fun testParseModuleWithPathShouldNotLookAtImports() {
val srcPath = Path.of("test", "fixtures", "import_nonexisting.p8")
val module = Prog8Parser.parseModule(srcPath)
assertEquals(1, module.statements.size)
}
@Test @Test
fun testErrorLocationForSourceFromString() { fun testErrorLocationForSourceFromString() {
val srcText = "bad * { }\n" val srcText = "bad * { }\n"