mirror of
https://github.com/irmen/prog8.git
synced 2024-12-25 08:29:25 +00:00
*/+ refactor tests of ModuleImporter, add some tests related to libdirs issue
This commit is contained in:
parent
c914f7bbcf
commit
0d06e3ff22
@ -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<NoSuchFileException> { importer.importModule(srcPath) }
|
||||
assertThrows<NoSuchFileException> { 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<AccessDeniedException> { importer.importModule(srcPath) }
|
||||
assertThrows<AccessDeniedException> { 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<ParseError> { 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<ParseError>(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<ParseError> { 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<ParseError>(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<NoSuchFileException> { importer.importLibraryModule(filenameNoExt) }
|
||||
assertFailsWith<NoSuchFileException> { importer.importLibraryModule(filenameWithExt) }
|
||||
repeat (2) { n ->
|
||||
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
|
||||
@ -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<ParseError> { 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<ParseError> (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<ParseError> { 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<ParseError>(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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user