From 0d06e3ff22537a676208f7dd3d3f47cf12d210d2 Mon Sep 17 00:00:00 2001 From: meisl Date: Sun, 18 Jul 2021 19:02:47 +0200 Subject: [PATCH] */+ refactor tests of ModuleImporter, add some tests related to libdirs issue --- compilerAst/test/TestModuleImporter.kt | 155 ++++++++++++++++--------- 1 file changed, 100 insertions(+), 55 deletions(-) diff --git a/compilerAst/test/TestModuleImporter.kt b/compilerAst/test/TestModuleImporter.kt index 0466e8123..e3c1105ba 100644 --- a/compilerAst/test/TestModuleImporter.kt +++ b/compilerAst/test/TestModuleImporter.kt @@ -1,20 +1,68 @@ package prog8tests -import org.junit.jupiter.api.TestInstance -import org.junit.jupiter.api.Test -import kotlin.test.* import prog8tests.helpers.* - +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInstance +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.* +import org.junit.jupiter.api.assertThrows import kotlin.io.path.* import prog8.ast.Program -import prog8.parser.ModuleImporter import prog8.parser.ParseError +import prog8.parser.ModuleImporter + @TestInstance(TestInstance.Lifecycle.PER_CLASS) class TestModuleImporter { + private val count = listOf("1st", "2nd", "3rd", "4th", "5th") + + @Test + fun testImportModuleWithExistingPath_absolute() { + val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer) + val searchIn = listOf( + Path(".").div(workingDir.relativize(fixturesDir)), // we do want a dot "." in front + ).map { it.invariantSeparatorsPathString } + val importer = ModuleImporter(program, "blah", searchIn) + val fileName = "simple_main.p8" + val path = assumeReadableFile(searchIn[0], fileName) + + val module = importer.importModule(path.absolute()) + assertThat(module.program, `is`(program)) + } + + @Test + fun testImportModuleWithExistingPath_relativeToWorkingDir() { + val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer) + val searchIn = listOf( + Path(".").div(workingDir.relativize(fixturesDir)), // we do want a dot "." in front + ).map { it.invariantSeparatorsPathString } + val importer = ModuleImporter(program, "blah", searchIn) + val fileName = "simple_main.p8" + val path = assumeReadableFile(searchIn[0], fileName) + assertThat("sanity check: path should NOT be absolute", path.isAbsolute, `is`(false)) + + val module = importer.importModule(path) + assertThat(module.program, `is`(program)) + } + + @Test + fun testImportModuleWithExistingPath_relativeTo1stDirInSearchList() { + val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer) + val searchIn = listOf( + Path(".").div(workingDir.relativize(fixturesDir)), // we do want a dot "." in front + ).map { it.invariantSeparatorsPathString } + val importer = ModuleImporter(program, "blah", searchIn) + val fileName = "simple_main.p8" + val path = Path(".", fileName) + assumeReadableFile(searchIn[0], path) + + val module = importer.importModule(path) + assertThat(module.program, `is`(program)) + } + @Test fun testImportModuleWithNonExistingPath() { val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer) @@ -22,7 +70,7 @@ class TestModuleImporter { val importer = ModuleImporter(program, "blah", listOf(searchIn)) val srcPath = assumeNotExists(fixturesDir, "i_do_not_exist") - assertFailsWith { importer.importModule(srcPath) } + assertThrows { importer.importModule(srcPath) } } @Test @@ -30,13 +78,13 @@ class TestModuleImporter { val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer) val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/") val importer = ModuleImporter(program, "blah", listOf(searchIn)) - val srcPath = assumeDirectory(fixturesDir) - // fn importModule(Path) used to check *.isReadable()*, but NOT .isRegularFile(): - assumeReadable(srcPath) - - assertFailsWith { importer.importModule(srcPath) } + assertThrows { importer.importModule(srcPath) } + .let { + assertThat(it.message!!, containsString("$srcPath")) + assertThat(it.file, `is`(srcPath.toFile())) + } } @Test @@ -44,19 +92,17 @@ class TestModuleImporter { val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer) val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/") val importer = ModuleImporter(program, "blah", listOf(searchIn)) + val srcPath = assumeReadableFile(fixturesDir, "file_with_syntax_error.p8") - val filename = "file_with_syntax_error.p8" - val path = assumeReadableFile(fixturesDir, filename) - val act = { importer.importModule(path) } + val act = { importer.importModule(srcPath) } - assertFailsWith { act() } - try { - act() - } catch (e: ParseError) { - assertEquals(path.absolutePathString(), e.position.file) - 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") + repeat (2) { n -> + assertThrows(count[n] + " call") { act() }.let { + assertThat(it.position.file, `is`(srcPath.absolutePathString())) + assertThat("line; should be 1-based", it.position.line, `is`(2)) + assertThat("startCol; should be 0-based", it.position.startCol, `is`(6)) + assertThat("endCol; should be 0-based", it.position.endCol, `is`(6)) + } } } @@ -65,21 +111,18 @@ class TestModuleImporter { val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer) val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/") val importer = ModuleImporter(program, "blah", listOf(searchIn)) - val importing = assumeReadableFile(fixturesDir, "import_file_with_syntax_error.p8") val imported = assumeReadableFile(fixturesDir, "file_with_syntax_error.p8") val act = { importer.importModule(importing) } - assertFailsWith { act() } - try { - act() - } catch (e: ParseError) { - val expectedProvenance = imported.absolutePathString() - assertEquals(expectedProvenance, e.position.file) - 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") + repeat (2) { n -> + assertThrows(count[n] + " call") { act() }.let { + assertThat(it.position.file, `is`(imported.absolutePathString())) + assertThat("line; should be 1-based", it.position.line, `is`(2)) + assertThat("startCol; should be 0-based", it.position.startCol, `is`(6)) + assertThat("endCol; should be 0-based", it.position.endCol, `is`(6)) + } } } @@ -91,8 +134,16 @@ class TestModuleImporter { val filenameNoExt = assumeNotExists(fixturesDir, "i_do_not_exist").name val filenameWithExt = assumeNotExists(fixturesDir, "i_do_not_exist.p8").name - assertFailsWith { importer.importLibraryModule(filenameNoExt) } - assertFailsWith { importer.importLibraryModule(filenameWithExt) } + repeat (2) { n -> + assertThrows(count[n] + " call / NO .p8 extension") + { importer.importLibraryModule(filenameNoExt) }.let { + assertThat(it.message!!, containsString(filenameWithExt)) + } + assertThrows(count[n] + " call / with .p8 extension") + { importer.importLibraryModule(filenameWithExt) }.let { + assertThat(it.message!!, containsString(filenameWithExt)) + } + } } @Test @@ -102,17 +153,14 @@ class TestModuleImporter { val importer = ModuleImporter(program, "blah", listOf(searchIn)) val srcPath = assumeReadableFile(fixturesDir,"file_with_syntax_error.p8") - val act = { importer.importLibraryModule(srcPath.nameWithoutExtension) } - - assertFailsWith { act() } - try { - act() - } catch (e: ParseError) { - val expectedProvenance = srcPath.absolutePathString() - assertEquals(expectedProvenance, e.position.file) - 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") + repeat (2) { n -> + assertThrows (count[n] + " call") + { importer.importLibraryModule(srcPath.nameWithoutExtension) } .let { + assertThat(it.position.file, `is`(srcPath.absolutePathString())) + assertThat("line; should be 1-based", it.position.line, `is`(2)) + assertThat("startCol; should be 0-based", it.position.startCol, `is`(6)) + assertThat("endCol; should be 0-based", it.position.endCol, `is`(6)) + } } } @@ -121,21 +169,18 @@ class TestModuleImporter { val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer) val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/") val importer = ModuleImporter(program, "blah", listOf(searchIn)) - val importing = assumeReadableFile(fixturesDir, "import_file_with_syntax_error.p8") val imported = assumeReadableFile(fixturesDir,"file_with_syntax_error.p8") val act = { importer.importLibraryModule(importing.nameWithoutExtension) } - assertFailsWith { act() } - try { - act() - } catch (e: ParseError) { - val expectedProvenance = imported.normalize().absolutePathString() - assertEquals(expectedProvenance, e.position.file) - 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") + repeat(2) { n -> + assertThrows(count[n] + " call") { act() }.let { + assertThat(it.position.file, `is`(imported.normalize().absolutePathString())) + assertThat("line; should be 1-based", it.position.line, `is`(2)) + assertThat("startCol; should be 0-based", it.position.startCol, `is`(6)) + assertThat("endCol; should be 0-based", it.position.endCol, `is`(6)) + } } }