diff --git a/compilerAst/test/TestModuleImporter.kt b/compilerAst/test/TestModuleImporter.kt index 3b18d5cae..04e296d62 100644 --- a/compilerAst/test/TestModuleImporter.kt +++ b/compilerAst/test/TestModuleImporter.kt @@ -13,9 +13,12 @@ import prog8.parser.ModuleImporter import prog8.parser.ParseError import java.nio.file.Path import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInstance +import kotlin.io.path.isRegularFile import kotlin.test.* +@TestInstance(TestInstance.Lifecycle.PER_CLASS) class TestModuleImporter { object DummyEncoding: IStringEncoding { @@ -39,6 +42,7 @@ class TestModuleImporter { override fun memorySize(dt: DataType): Int = 0 } + @Test fun testImportModuleWithSyntaxError() { val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer) @@ -59,7 +63,53 @@ class TestModuleImporter { } @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 { 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 { 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 importer = ModuleImporter(program, DummyEncoding, "blah", listOf("./test/fixtures")) @@ -71,9 +121,13 @@ class TestModuleImporter { try { act() } 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(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") } } diff --git a/compilerAst/test/TestProg8Parser.kt b/compilerAst/test/TestProg8Parser.kt index d30e15bdd..749c1c7d1 100644 --- a/compilerAst/test/TestProg8Parser.kt +++ b/compilerAst/test/TestProg8Parser.kt @@ -3,8 +3,13 @@ package prog8tests import org.junit.jupiter.api.Test import prog8.ast.statements.Block import prog8.parser.ParseError +import prog8.parser.Prog8Parser import prog8.parser.Prog8Parser.parseModule 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.* 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 { 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 { 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 { 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 fun testErrorLocationForSourceFromString() { val srcText = "bad * { }\n"