*/+ refactor tests of ModuleImporter, add some tests related to libdirs issue

This commit is contained in:
meisl 2021-07-18 19:02:47 +02:00
parent c914f7bbcf
commit 0d06e3ff22

View File

@ -1,20 +1,68 @@
package prog8tests package prog8tests
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.Test
import kotlin.test.*
import prog8tests.helpers.* 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 kotlin.io.path.*
import prog8.ast.Program import prog8.ast.Program
import prog8.parser.ModuleImporter
import prog8.parser.ParseError import prog8.parser.ParseError
import prog8.parser.ModuleImporter
@TestInstance(TestInstance.Lifecycle.PER_CLASS) @TestInstance(TestInstance.Lifecycle.PER_CLASS)
class TestModuleImporter { 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 @Test
fun testImportModuleWithNonExistingPath() { fun testImportModuleWithNonExistingPath() {
val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer) val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer)
@ -22,7 +70,7 @@ class TestModuleImporter {
val importer = ModuleImporter(program, "blah", listOf(searchIn)) val importer = ModuleImporter(program, "blah", listOf(searchIn))
val srcPath = assumeNotExists(fixturesDir, "i_do_not_exist") val srcPath = assumeNotExists(fixturesDir, "i_do_not_exist")
assertFailsWith<NoSuchFileException> { importer.importModule(srcPath) } assertThrows<NoSuchFileException> { importer.importModule(srcPath) }
} }
@Test @Test
@ -30,13 +78,13 @@ class TestModuleImporter {
val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer) val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer)
val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/") val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/")
val importer = ModuleImporter(program, "blah", listOf(searchIn)) val importer = ModuleImporter(program, "blah", listOf(searchIn))
val srcPath = assumeDirectory(fixturesDir) val srcPath = assumeDirectory(fixturesDir)
// fn importModule(Path) used to check *.isReadable()*, but NOT .isRegularFile(): assertThrows<AccessDeniedException> { importer.importModule(srcPath) }
assumeReadable(srcPath) .let {
assertThat(it.message!!, containsString("$srcPath"))
assertFailsWith<AccessDeniedException> { importer.importModule(srcPath) } assertThat(it.file, `is`(srcPath.toFile()))
}
} }
@Test @Test
@ -44,19 +92,17 @@ class TestModuleImporter {
val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer) val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer)
val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/") val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/")
val importer = ModuleImporter(program, "blah", listOf(searchIn)) val importer = ModuleImporter(program, "blah", listOf(searchIn))
val srcPath = assumeReadableFile(fixturesDir, "file_with_syntax_error.p8")
val filename = "file_with_syntax_error.p8" val act = { importer.importModule(srcPath) }
val path = assumeReadableFile(fixturesDir, filename)
val act = { importer.importModule(path) }
assertFailsWith<ParseError> { act() } repeat (2) { n ->
try { assertThrows<ParseError>(count[n] + " call") { act() }.let {
act() assertThat(it.position.file, `is`(srcPath.absolutePathString()))
} catch (e: ParseError) { assertThat("line; should be 1-based", it.position.line, `is`(2))
assertEquals(path.absolutePathString(), e.position.file) assertThat("startCol; should be 0-based", it.position.startCol, `is`(6))
assertEquals(2, e.position.line, "line; should be 1-based") assertThat("endCol; should be 0-based", it.position.endCol, `is`(6))
assertEquals(6, e.position.startCol, "startCol; should be 0-based" ) }
assertEquals(6, e.position.endCol, "endCol; should be 0-based")
} }
} }
@ -65,21 +111,18 @@ class TestModuleImporter {
val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer) val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer)
val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/") val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/")
val importer = ModuleImporter(program, "blah", listOf(searchIn)) val importer = ModuleImporter(program, "blah", listOf(searchIn))
val importing = assumeReadableFile(fixturesDir, "import_file_with_syntax_error.p8") val importing = assumeReadableFile(fixturesDir, "import_file_with_syntax_error.p8")
val imported = assumeReadableFile(fixturesDir, "file_with_syntax_error.p8") val imported = assumeReadableFile(fixturesDir, "file_with_syntax_error.p8")
val act = { importer.importModule(importing) } val act = { importer.importModule(importing) }
assertFailsWith<ParseError> { act() } repeat (2) { n ->
try { assertThrows<ParseError>(count[n] + " call") { act() }.let {
act() assertThat(it.position.file, `is`(imported.absolutePathString()))
} catch (e: ParseError) { assertThat("line; should be 1-based", it.position.line, `is`(2))
val expectedProvenance = imported.absolutePathString() assertThat("startCol; should be 0-based", it.position.startCol, `is`(6))
assertEquals(expectedProvenance, e.position.file) assertThat("endCol; should be 0-based", it.position.endCol, `is`(6))
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")
} }
} }
@ -91,8 +134,16 @@ class TestModuleImporter {
val filenameNoExt = assumeNotExists(fixturesDir, "i_do_not_exist").name val filenameNoExt = assumeNotExists(fixturesDir, "i_do_not_exist").name
val filenameWithExt = assumeNotExists(fixturesDir, "i_do_not_exist.p8").name val filenameWithExt = assumeNotExists(fixturesDir, "i_do_not_exist.p8").name
assertFailsWith<NoSuchFileException> { importer.importLibraryModule(filenameNoExt) } repeat (2) { n ->
assertFailsWith<NoSuchFileException> { importer.importLibraryModule(filenameWithExt) } assertThrows<NoSuchFileException>(count[n] + " call / NO .p8 extension")
{ importer.importLibraryModule(filenameNoExt) }.let {
assertThat(it.message!!, containsString(filenameWithExt))
}
assertThrows<NoSuchFileException>(count[n] + " call / with .p8 extension")
{ importer.importLibraryModule(filenameWithExt) }.let {
assertThat(it.message!!, containsString(filenameWithExt))
}
}
} }
@Test @Test
@ -102,17 +153,14 @@ class TestModuleImporter {
val importer = ModuleImporter(program, "blah", listOf(searchIn)) val importer = ModuleImporter(program, "blah", listOf(searchIn))
val srcPath = assumeReadableFile(fixturesDir,"file_with_syntax_error.p8") val srcPath = assumeReadableFile(fixturesDir,"file_with_syntax_error.p8")
val act = { importer.importLibraryModule(srcPath.nameWithoutExtension) } repeat (2) { n ->
assertThrows<ParseError> (count[n] + " call")
assertFailsWith<ParseError> { act() } { importer.importLibraryModule(srcPath.nameWithoutExtension) } .let {
try { assertThat(it.position.file, `is`(srcPath.absolutePathString()))
act() assertThat("line; should be 1-based", it.position.line, `is`(2))
} catch (e: ParseError) { assertThat("startCol; should be 0-based", it.position.startCol, `is`(6))
val expectedProvenance = srcPath.absolutePathString() assertThat("endCol; should be 0-based", it.position.endCol, `is`(6))
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")
} }
} }
@ -121,21 +169,18 @@ class TestModuleImporter {
val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer) val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer)
val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/") val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/")
val importer = ModuleImporter(program, "blah", listOf(searchIn)) val importer = ModuleImporter(program, "blah", listOf(searchIn))
val importing = assumeReadableFile(fixturesDir, "import_file_with_syntax_error.p8") val importing = assumeReadableFile(fixturesDir, "import_file_with_syntax_error.p8")
val imported = assumeReadableFile(fixturesDir,"file_with_syntax_error.p8") val imported = assumeReadableFile(fixturesDir,"file_with_syntax_error.p8")
val act = { importer.importLibraryModule(importing.nameWithoutExtension) } val act = { importer.importLibraryModule(importing.nameWithoutExtension) }
assertFailsWith<ParseError> { act() } repeat(2) { n ->
try { assertThrows<ParseError>(count[n] + " call") { act() }.let {
act() assertThat(it.position.file, `is`(imported.normalize().absolutePathString()))
} catch (e: ParseError) { assertThat("line; should be 1-based", it.position.line, `is`(2))
val expectedProvenance = imported.normalize().absolutePathString() assertThat("startCol; should be 0-based", it.position.startCol, `is`(6))
assertEquals(expectedProvenance, e.position.file) assertThat("endCol; should be 0-based", it.position.endCol, `is`(6))
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")
} }
} }