2024-09-08 14:03:57 +00:00
|
|
|
package prog8tests.compiler
|
2021-06-19 16:10:26 +00:00
|
|
|
|
2021-10-12 21:54:48 +00:00
|
|
|
import com.github.michaelbull.result.getErrorOrElse
|
|
|
|
import com.github.michaelbull.result.getOrElse
|
2021-11-07 23:16:58 +00:00
|
|
|
import io.kotest.assertions.fail
|
|
|
|
import io.kotest.assertions.throwables.shouldThrow
|
|
|
|
import io.kotest.assertions.withClue
|
|
|
|
import io.kotest.core.spec.style.FunSpec
|
|
|
|
import io.kotest.matchers.collections.shouldBeIn
|
|
|
|
import io.kotest.matchers.shouldBe
|
|
|
|
import io.kotest.matchers.string.shouldContain
|
2022-02-05 02:50:54 +00:00
|
|
|
import prog8.ast.Program
|
2022-03-10 22:08:41 +00:00
|
|
|
import prog8.code.core.IErrorReporter
|
2022-03-21 00:01:21 +00:00
|
|
|
import prog8.code.core.SourceCode
|
|
|
|
import prog8.code.core.internedStringsModuleName
|
2022-05-22 21:11:22 +00:00
|
|
|
import prog8.compiler.ModuleImporter
|
|
|
|
import prog8.parser.ParseError
|
|
|
|
import prog8tests.helpers.*
|
2022-02-05 02:50:54 +00:00
|
|
|
import kotlin.io.path.*
|
2021-07-18 17:02:47 +00:00
|
|
|
|
2021-06-19 16:10:26 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
class TestModuleImporter: FunSpec({
|
|
|
|
val count = listOf("1st", "2nd", "3rd", "4th", "5th")
|
2021-07-18 17:02:47 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
lateinit var program: Program
|
|
|
|
|
|
|
|
beforeTest {
|
2021-10-29 22:05:55 +00:00
|
|
|
program = Program("foo", DummyFunctions, DummyMemsizer, DummyStringEncoder)
|
2021-08-01 15:24:12 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
fun makeImporter(errors: IErrorReporter? = null, searchIn: Iterable<String>) =
|
|
|
|
ModuleImporter(program, "blah", errors ?: ErrorReporterForTests(false), searchIn.toList())
|
|
|
|
|
|
|
|
fun makeImporter(errors: IErrorReporter?, vararg searchIn: String): ModuleImporter {
|
2021-10-11 19:20:57 +00:00
|
|
|
return makeImporter(errors, searchIn.asList())
|
|
|
|
}
|
2021-08-01 15:24:12 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
context("ImportModule") {
|
2021-06-19 18:45:37 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
context("WithInvalidPath") {
|
|
|
|
test("testNonexisting") {
|
2022-06-26 16:51:03 +00:00
|
|
|
val dirRel = assumeDirectory(".", workingDir.relativize(fixturesDir))
|
2021-10-11 19:20:57 +00:00
|
|
|
val importer = makeImporter(null, dirRel.invariantSeparatorsPathString)
|
2022-06-26 16:51:03 +00:00
|
|
|
val srcPathRel = assumeNotExists(dirRel, "i_do_not_exist")
|
2021-08-01 13:15:54 +00:00
|
|
|
val srcPathAbs = srcPathRel.absolute()
|
2023-02-28 19:08:11 +00:00
|
|
|
val error1 = importer.importMainModule(srcPathRel).getErrorOrElse { fail("should have import error") }
|
2021-11-08 14:50:29 +00:00
|
|
|
withClue(".file should be normalized") {
|
|
|
|
"${error1.file}" shouldBe "${error1.file.normalize()}"
|
|
|
|
}
|
|
|
|
withClue(".file should point to specified path") {
|
|
|
|
error1.file.absolutePath shouldBe "${srcPathAbs.normalize()}"
|
|
|
|
}
|
|
|
|
program.modules.size shouldBe 1
|
2023-02-28 19:08:11 +00:00
|
|
|
val error2 = importer.importMainModule(srcPathAbs).getErrorOrElse { fail("should have import error") }
|
2021-11-08 14:50:29 +00:00
|
|
|
withClue(".file should be normalized") {
|
|
|
|
"${error2.file}" shouldBe "${error2.file.normalize()}"
|
|
|
|
}
|
|
|
|
withClue(".file should point to specified path") {
|
|
|
|
error2.file.absolutePath shouldBe "${srcPathAbs.normalize()}"
|
|
|
|
}
|
|
|
|
program.modules.size shouldBe 1
|
2021-07-18 17:02:47 +00:00
|
|
|
}
|
2021-06-19 18:45:37 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
test("testDirectory") {
|
2022-06-26 16:51:03 +00:00
|
|
|
val srcPathRel = assumeDirectory(workingDir.relativize(fixturesDir))
|
2021-08-01 13:15:54 +00:00
|
|
|
val srcPathAbs = srcPathRel.absolute()
|
2021-08-01 20:47:11 +00:00
|
|
|
val searchIn = Path(".", "$srcPathRel").invariantSeparatorsPathString
|
2021-10-11 19:20:57 +00:00
|
|
|
val importer = makeImporter(null, searchIn)
|
2021-08-01 13:15:54 +00:00
|
|
|
|
2023-02-28 19:08:11 +00:00
|
|
|
shouldThrow<FileSystemException> { importer.importMainModule(srcPathRel) }
|
2021-08-01 13:15:54 +00:00
|
|
|
.let {
|
2021-11-08 14:50:29 +00:00
|
|
|
withClue(".file should be normalized") {
|
|
|
|
"${it.file}" shouldBe "${it.file.normalize()}"
|
|
|
|
}
|
|
|
|
withClue(".file should point to specified path") {
|
|
|
|
it.file.absolutePath shouldBe "${srcPathAbs.normalize()}"
|
|
|
|
}
|
2021-08-01 13:15:54 +00:00
|
|
|
}
|
2021-11-08 14:50:29 +00:00
|
|
|
program.modules.size shouldBe 1
|
2021-08-01 13:15:54 +00:00
|
|
|
|
2023-02-28 19:08:11 +00:00
|
|
|
shouldThrow<FileSystemException> { importer.importMainModule(srcPathAbs) }
|
2021-08-01 13:15:54 +00:00
|
|
|
.let {
|
2021-11-08 14:50:29 +00:00
|
|
|
withClue(".file should be normalized") {
|
|
|
|
"${it.file}" shouldBe "${it.file.normalize()}"
|
|
|
|
}
|
|
|
|
withClue(".file should point to specified path") {
|
|
|
|
it.file.absolutePath shouldBe "${srcPathAbs.normalize()}"
|
|
|
|
}
|
2021-08-01 13:15:54 +00:00
|
|
|
}
|
2021-11-08 14:50:29 +00:00
|
|
|
program.modules.size shouldBe 1
|
2021-07-18 17:02:47 +00:00
|
|
|
}
|
2021-06-19 16:10:26 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
context("WithValidPath") {
|
2021-08-01 13:15:54 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
test("testAbsolute") {
|
2021-08-01 13:15:54 +00:00
|
|
|
val searchIn = listOf(
|
2022-06-26 16:51:03 +00:00
|
|
|
Path(".").div(workingDir.relativize(fixturesDir)), // we do want a dot "." in front
|
2021-08-01 13:15:54 +00:00
|
|
|
).map { it.invariantSeparatorsPathString }
|
2021-10-11 19:20:57 +00:00
|
|
|
val importer = makeImporter(null, searchIn)
|
2021-12-04 17:20:22 +00:00
|
|
|
val fileName = "ast_simple_main.p8"
|
2022-06-26 16:51:03 +00:00
|
|
|
val path = assumeReadableFile(searchIn[0], fileName)
|
2021-08-01 13:15:54 +00:00
|
|
|
|
2023-02-28 19:08:11 +00:00
|
|
|
val module = importer.importMainModule(path.absolute()).getOrElse { throw it }
|
2021-11-08 14:50:29 +00:00
|
|
|
program.modules.size shouldBe 2
|
2021-11-07 23:16:58 +00:00
|
|
|
module shouldBeIn program.modules
|
2021-11-08 14:50:29 +00:00
|
|
|
module.program shouldBe program
|
2021-08-01 13:15:54 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
test("testRelativeToWorkingDir") {
|
2021-08-01 13:15:54 +00:00
|
|
|
val searchIn = listOf(
|
2022-06-26 16:51:03 +00:00
|
|
|
Path(".").div(workingDir.relativize(fixturesDir)), // we do want a dot "." in front
|
2021-08-01 13:15:54 +00:00
|
|
|
).map { it.invariantSeparatorsPathString }
|
2021-10-11 19:20:57 +00:00
|
|
|
val importer = makeImporter(null, searchIn)
|
2021-12-04 17:20:22 +00:00
|
|
|
val fileName = "ast_simple_main.p8"
|
2022-06-26 16:51:03 +00:00
|
|
|
val path = assumeReadableFile(searchIn[0], fileName)
|
2021-11-08 14:50:29 +00:00
|
|
|
withClue("sanity check: path should NOT be absolute") {
|
|
|
|
path.isAbsolute shouldBe false
|
|
|
|
}
|
2021-08-01 13:15:54 +00:00
|
|
|
|
2023-02-28 19:08:11 +00:00
|
|
|
val module = importer.importMainModule(path).getOrElse { throw it }
|
2021-11-08 14:50:29 +00:00
|
|
|
program.modules.size shouldBe 2
|
2021-11-07 23:16:58 +00:00
|
|
|
module shouldBeIn program.modules
|
2021-11-08 14:50:29 +00:00
|
|
|
module.program shouldBe program
|
2021-08-01 13:15:54 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
test("testRelativeTo1stDirInSearchList") {
|
2021-08-01 15:24:12 +00:00
|
|
|
val searchIn = Path(".")
|
2022-06-26 16:51:03 +00:00
|
|
|
.div(workingDir.relativize(fixturesDir))
|
2021-08-01 15:24:12 +00:00
|
|
|
.invariantSeparatorsPathString
|
2021-10-11 19:20:57 +00:00
|
|
|
val importer = makeImporter(null, searchIn)
|
2021-12-04 17:20:22 +00:00
|
|
|
val fileName = "ast_simple_main.p8"
|
2021-08-01 13:15:54 +00:00
|
|
|
val path = Path(".", fileName)
|
2022-06-26 16:51:03 +00:00
|
|
|
assumeReadableFile(searchIn, path)
|
2021-08-01 13:15:54 +00:00
|
|
|
|
2023-02-28 19:08:11 +00:00
|
|
|
val module = importer.importMainModule(path).getOrElse { throw it }
|
2021-11-08 14:50:29 +00:00
|
|
|
program.modules.size shouldBe 2
|
2021-11-07 23:16:58 +00:00
|
|
|
module shouldBeIn program.modules
|
2021-11-08 14:50:29 +00:00
|
|
|
module.program shouldBe program
|
2021-07-18 17:02:47 +00:00
|
|
|
}
|
2021-06-19 18:27:04 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
context("WithBadFile") {
|
|
|
|
test("testWithSyntaxError") {
|
2022-06-26 16:51:03 +00:00
|
|
|
val searchIn = assumeDirectory("./", workingDir.relativize(fixturesDir))
|
2021-10-11 19:20:57 +00:00
|
|
|
val importer = makeImporter(null, searchIn.invariantSeparatorsPathString)
|
2022-06-26 16:51:03 +00:00
|
|
|
val srcPath = assumeReadableFile(fixturesDir, "ast_file_with_syntax_error.p8")
|
2021-08-01 13:15:54 +00:00
|
|
|
|
2023-02-28 19:08:11 +00:00
|
|
|
val act = { importer.importMainModule(srcPath) }
|
2021-08-01 13:15:54 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
repeat(2) { n -> withClue(count[n] + " call") {
|
2022-04-15 20:38:32 +00:00
|
|
|
shouldThrow<ParseError> { act() }.let {
|
2021-11-08 14:50:29 +00:00
|
|
|
it.position.file shouldBe SourceCode.relative(srcPath).toString()
|
|
|
|
withClue("line; should be 1-based") { it.position.line shouldBe 2 }
|
2023-06-26 23:59:22 +00:00
|
|
|
withClue("startCol; should be 0-based") { it.position.startCol shouldBe 4 }
|
2021-11-08 14:50:29 +00:00
|
|
|
withClue("endCol; should be 0-based") { it.position.endCol shouldBe 6 }
|
2021-11-07 23:16:58 +00:00
|
|
|
}
|
2021-08-01 13:15:54 +00:00
|
|
|
}
|
2021-11-08 14:50:29 +00:00
|
|
|
program.modules.size shouldBe 1
|
2021-08-01 13:15:54 +00:00
|
|
|
}
|
2021-07-18 17:02:47 +00:00
|
|
|
}
|
2021-08-01 13:15:54 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
fun doTestImportingFileWithSyntaxError(repetitions: Int) {
|
2022-06-26 16:51:03 +00:00
|
|
|
val searchIn = assumeDirectory("./", workingDir.relativize(fixturesDir))
|
2021-10-11 19:20:57 +00:00
|
|
|
val importer = makeImporter(null, searchIn.invariantSeparatorsPathString)
|
2022-06-26 16:51:03 +00:00
|
|
|
val importing = assumeReadableFile(fixturesDir, "import_file_with_syntax_error.p8")
|
|
|
|
val imported = assumeReadableFile(fixturesDir, "file_with_syntax_error.p8")
|
2021-08-01 13:15:54 +00:00
|
|
|
|
2023-02-28 19:08:11 +00:00
|
|
|
val act = { importer.importMainModule(importing) }
|
2021-08-01 13:15:54 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
repeat(repetitions) { n -> withClue(count[n] + " call") {
|
2021-12-04 00:03:51 +00:00
|
|
|
shouldThrow<ParseError> { act() }.let {
|
2021-11-08 14:50:29 +00:00
|
|
|
it.position.file shouldBe SourceCode.relative(imported).toString()
|
|
|
|
withClue("line; should be 1-based") { it.position.line shouldBe 2 }
|
2023-06-26 23:59:22 +00:00
|
|
|
withClue("startCol; should be 0-based") { it.position.startCol shouldBe 4 }
|
2021-11-08 14:50:29 +00:00
|
|
|
withClue("endCol; should be 0-based") { it.position.endCol shouldBe 6 }
|
2021-08-01 13:15:54 +00:00
|
|
|
}
|
2021-11-07 23:16:58 +00:00
|
|
|
}
|
2021-12-04 00:03:51 +00:00
|
|
|
withClue("imported module with error in it should not be present") { program.modules.size shouldBe 1 }
|
2021-11-08 14:50:29 +00:00
|
|
|
program.modules[0].name shouldBe internedStringsModuleName
|
2021-08-01 13:15:54 +00:00
|
|
|
}
|
2021-07-18 17:02:47 +00:00
|
|
|
}
|
2021-11-07 23:16:58 +00:00
|
|
|
|
|
|
|
test("testImportingFileWithSyntaxError_once") {
|
|
|
|
doTestImportingFileWithSyntaxError(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
test("testImportingFileWithSyntaxError_twice") {
|
|
|
|
doTestImportingFileWithSyntaxError(2)
|
|
|
|
}
|
2021-08-01 13:15:54 +00:00
|
|
|
}
|
2021-07-18 17:02:47 +00:00
|
|
|
}
|
2021-06-21 10:02:36 +00:00
|
|
|
}
|
2021-06-19 18:27:04 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
context("ImportLibraryModule") {
|
|
|
|
context("WithInvalidName") {
|
|
|
|
test("testWithNonExistingName") {
|
2022-06-26 16:51:03 +00:00
|
|
|
val searchIn = assumeDirectory("./", workingDir.relativize(fixturesDir))
|
2021-10-21 23:25:26 +00:00
|
|
|
val errors = ErrorReporterForTests(false)
|
2021-10-11 19:20:57 +00:00
|
|
|
val importer = makeImporter(errors, searchIn.invariantSeparatorsPathString)
|
2022-06-26 16:51:03 +00:00
|
|
|
val filenameNoExt = assumeNotExists(fixturesDir, "i_do_not_exist").name
|
|
|
|
val filenameWithExt = assumeNotExists(fixturesDir, "i_do_not_exist.p8").name
|
2021-08-01 13:15:54 +00:00
|
|
|
|
|
|
|
repeat(2) { n ->
|
2023-02-28 19:08:11 +00:00
|
|
|
val result = importer.importImplicitLibraryModule(filenameNoExt)
|
2021-11-08 14:50:29 +00:00
|
|
|
withClue(count[n] + " call / NO .p8 extension") { result shouldBe null }
|
|
|
|
withClue(count[n] + " call / NO .p8 extension") { errors.noErrors() shouldBe false }
|
2022-02-27 15:27:02 +00:00
|
|
|
errors.errors.single() shouldContain "0:0: no module found with name i_do_not_exist"
|
2021-10-11 19:20:57 +00:00
|
|
|
errors.report()
|
2021-11-08 14:50:29 +00:00
|
|
|
program.modules.size shouldBe 1
|
2021-08-01 15:24:12 +00:00
|
|
|
|
2023-02-28 19:08:11 +00:00
|
|
|
val result2 = importer.importImplicitLibraryModule(filenameWithExt)
|
2021-11-08 14:50:29 +00:00
|
|
|
withClue(count[n] + " call / with .p8 extension") { result2 shouldBe null }
|
|
|
|
withClue(count[n] + " call / with .p8 extension") { importer.errors.noErrors() shouldBe false }
|
2022-02-27 15:27:02 +00:00
|
|
|
errors.errors.single() shouldContain "0:0: no module found with name i_do_not_exist.p8"
|
2021-10-11 19:20:57 +00:00
|
|
|
errors.report()
|
2021-11-08 14:50:29 +00:00
|
|
|
program.modules.size shouldBe 1
|
2021-07-18 17:02:47 +00:00
|
|
|
}
|
2021-08-01 13:15:54 +00:00
|
|
|
}
|
2021-06-19 18:27:04 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
context("WithValidName") {
|
|
|
|
context("WithBadFile") {
|
|
|
|
test("testWithSyntaxError") {
|
2022-06-26 16:51:03 +00:00
|
|
|
val searchIn = assumeDirectory("./", workingDir.relativize(fixturesDir))
|
2021-10-11 19:20:57 +00:00
|
|
|
val importer = makeImporter(null, searchIn.invariantSeparatorsPathString)
|
2022-06-26 16:51:03 +00:00
|
|
|
val srcPath = assumeReadableFile(fixturesDir, "ast_file_with_syntax_error.p8")
|
2021-08-01 13:15:54 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
repeat(2) { n -> withClue(count[n] + " call") {
|
|
|
|
shouldThrow<ParseError>()
|
|
|
|
{
|
2023-02-28 19:08:11 +00:00
|
|
|
importer.importImplicitLibraryModule(srcPath.nameWithoutExtension) }.let {
|
2021-11-08 14:50:29 +00:00
|
|
|
it.position.file shouldBe SourceCode.relative(srcPath).toString()
|
|
|
|
withClue("line; should be 1-based") { it.position.line shouldBe 2 }
|
2023-06-26 23:59:22 +00:00
|
|
|
withClue("startCol; should be 0-based") { it.position.startCol shouldBe 4 }
|
2021-11-08 14:50:29 +00:00
|
|
|
withClue("endCol; should be 0-based") { it.position.endCol shouldBe 6 }
|
2021-08-01 15:24:12 +00:00
|
|
|
}
|
2021-11-07 23:16:58 +00:00
|
|
|
}
|
2021-11-08 14:50:29 +00:00
|
|
|
program.modules.size shouldBe 1
|
2021-08-01 13:15:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-01 14:26:27 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
fun doTestImportingFileWithSyntaxError(repetitions: Int) {
|
2022-06-26 16:51:03 +00:00
|
|
|
val searchIn = assumeDirectory("./", workingDir.relativize(fixturesDir))
|
2021-10-11 19:20:57 +00:00
|
|
|
val importer = makeImporter(null, searchIn.invariantSeparatorsPathString)
|
2022-06-26 16:51:03 +00:00
|
|
|
val importing = assumeReadableFile(fixturesDir, "import_file_with_syntax_error.p8")
|
|
|
|
val imported = assumeReadableFile(fixturesDir, "file_with_syntax_error.p8")
|
2021-08-01 13:15:54 +00:00
|
|
|
|
2023-02-28 19:08:11 +00:00
|
|
|
val act = { importer.importImplicitLibraryModule(importing.nameWithoutExtension) }
|
2021-08-01 13:15:54 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
repeat(repetitions) { n -> withClue(count[n] + " call") {
|
2022-04-15 20:38:32 +00:00
|
|
|
shouldThrow<ParseError> {
|
2021-11-07 23:16:58 +00:00
|
|
|
act() }.let {
|
2021-11-08 14:50:29 +00:00
|
|
|
it.position.file shouldBe SourceCode.relative(imported).toString()
|
|
|
|
withClue("line; should be 1-based") { it.position.line shouldBe 2 }
|
2023-06-26 23:59:22 +00:00
|
|
|
withClue("startCol; should be 0-based") { it.position.startCol shouldBe 4 }
|
2021-11-08 14:50:29 +00:00
|
|
|
withClue("endCol; should be 0-based") { it.position.endCol shouldBe 6 }
|
2021-11-07 23:16:58 +00:00
|
|
|
}
|
2021-08-01 13:15:54 +00:00
|
|
|
}
|
2021-11-08 14:50:29 +00:00
|
|
|
withClue("imported module with error in it should not be present") { program.modules.size shouldBe 1 }
|
|
|
|
program.modules[0].name shouldBe internedStringsModuleName
|
2021-10-13 18:26:35 +00:00
|
|
|
importer.errors.report()
|
2021-08-01 13:15:54 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-01 14:26:27 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
test("testImportingFileWithSyntaxError_once") {
|
2021-08-01 14:26:27 +00:00
|
|
|
doTestImportingFileWithSyntaxError(1)
|
|
|
|
}
|
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
test("testImportingFileWithSyntaxError_twice") {
|
2021-08-01 14:26:27 +00:00
|
|
|
doTestImportingFileWithSyntaxError(2)
|
|
|
|
}
|
2021-07-18 17:02:47 +00:00
|
|
|
}
|
2021-06-19 16:10:26 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-07 23:16:58 +00:00
|
|
|
})
|