diff --git a/compiler/src/prog8/compiler/ModuleImporter.kt b/compiler/src/prog8/compiler/ModuleImporter.kt index c4eaa0317..9a60e2d1f 100644 --- a/compiler/src/prog8/compiler/ModuleImporter.kt +++ b/compiler/src/prog8/compiler/ModuleImporter.kt @@ -51,7 +51,6 @@ class ModuleImporter(private val program: Program, return executeImportDirective(import, null) } - //private fun importModule(stream: CharStream, modulePath: Path, isLibrary: Boolean): Module { private fun importModule(src: SourceCode) : Module { val moduleAst = Prog8Parser.parseModule(src) program.addModule(moduleAst) diff --git a/compilerAst/test/TestProg8Parser.kt b/compilerAst/test/TestProg8Parser.kt index 4910fc7e6..da566e58a 100644 --- a/compilerAst/test/TestProg8Parser.kt +++ b/compilerAst/test/TestProg8Parser.kt @@ -4,6 +4,7 @@ import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test import org.junit.jupiter.api.TestInstance import prog8.ast.IFunctionCall +import prog8.ast.Module import prog8.ast.Node import prog8.ast.base.Position import prog8.ast.expressions.CharLiteral @@ -17,12 +18,11 @@ import prog8.parser.SourceCode import prog8tests.helpers.assumeNotExists import prog8tests.helpers.assumeReadableFile import prog8tests.helpers.fixturesDir +import kotlin.io.path.Path +import kotlin.io.path.isRegularFile import kotlin.io.path.name import kotlin.io.path.nameWithoutExtension -import kotlin.test.assertContains -import kotlin.test.assertEquals -import kotlin.test.assertFailsWith -import kotlin.test.assertIs +import kotlin.test.* @TestInstance(TestInstance.Lifecycle.PER_CLASS) @@ -330,9 +330,9 @@ class TestProg8Parser { val mpf = module.position.file assertPositionOf(module, SourceCode.relative(path).toString(), 1, 0) // TODO: endCol wrong val mainBlock = module.statements.filterIsInstance()[0] - assertPositionOf(mainBlock, mpf, 1, 0) // TODO: endCol wrong! + assertPositionOf(mainBlock, mpf, 2, 0) // TODO: endCol wrong! val startSub = mainBlock.statements.filterIsInstance()[0] - assertPositionOf(startSub, mpf, 2, 4) // TODO: endCol wrong! + assertPositionOf(startSub, mpf, 3, 4) // TODO: endCol wrong! } @@ -378,6 +378,59 @@ class TestProg8Parser { } } + @Nested + inner class PositionFile { + @Test + fun `isn't absolute for filesystem paths`() { + val path = assumeReadableFile(fixturesDir, "simple_main.p8") + val module = parseModule(SourceCode.File(path)) + assertSomethingForAllNodes(module) { + assertFalse(Path(it.position.file).isAbsolute) + assertTrue(Path(it.position.file).isRegularFile()) + } + } + + @Test + fun `is mangled string id for string sources`() + { + val srcText=""" + %zeropage basicsafe + main { + sub start() { + ubyte aa=99 + aa++ + } + } + """.trimIndent() + val module = parseModule(SourceCode.Text(srcText)) + assertSomethingForAllNodes(module) { + assertTrue(it.position.file.startsWith(SourceCode.stringSourcePrefix)) + } + } + + @Test + fun `is library prefixed path for resources`() + { + val resource = SourceCode.Resource("prog8lib/math.p8") + val module = parseModule(resource) + assertSomethingForAllNodes(module) { + assertTrue(it.position.file.startsWith(SourceCode.libraryFilePrefix)) + } + } + + private fun assertSomethingForAllNodes(module: Module, asserter: (Node) -> Unit) { + asserter(module) + module.statements.forEach(asserter) + module.statements.filterIsInstance().forEach { b -> + asserter(b) + b.statements.forEach(asserter) + b.statements.filterIsInstance().forEach { s -> + asserter(s) + s.statements.forEach(asserter) + } + } + } } + @Nested inner class CharLiterals { diff --git a/compilerAst/test/TestSourceCode.kt b/compilerAst/test/TestSourceCode.kt index 04d8e5d99..5f9027814 100644 --- a/compilerAst/test/TestSourceCode.kt +++ b/compilerAst/test/TestSourceCode.kt @@ -1,30 +1,32 @@ package prog8tests +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.core.StringStartsWith import org.junit.jupiter.api.Test import org.junit.jupiter.api.TestInstance import prog8.parser.SourceCode import prog8.parser.SourceCode.Companion.libraryFilePrefix import prog8tests.helpers.* import kotlin.io.path.Path -import kotlin.test.assertContains -import kotlin.test.assertEquals -import kotlin.test.assertFailsWith -import kotlin.test.assertTrue +import kotlin.test.* @TestInstance(TestInstance.Lifecycle.PER_CLASS) class TestSourceCode { @Test - fun testFactoryMethod_Of() { + fun testFromString() { val text = """ main { } - """.trimIndent() + """ val src = SourceCode.Text(text) val actualText = src.getCharStream().toString() assertContains(src.origin, Regex("^$")) assertEquals(text, actualText) + assertFalse(src.isFromResources) + assertFalse(src.isFromFilesystem) + assertThat(src.toString(), StringStartsWith("prog8.parser.SourceCode")) } @Test @@ -54,6 +56,8 @@ class TestSourceCode { val expectedOrigin = SourceCode.relative(path).toString() assertEquals(expectedOrigin, src.origin) assertEquals(path.toFile().readText(), src.asString()) + assertFalse(src.isFromResources) + assertTrue(src.isFromFilesystem) } @Test @@ -75,6 +79,8 @@ class TestSourceCode { assertEquals("$libraryFilePrefix/$pathString", src.origin) assertEquals(srcFile.readText(), src.asString()) + assertTrue(src.isFromResources) + assertFalse(src.isFromFilesystem) } @Test diff --git a/compilerAst/test/fixtures/simple_main.p8 b/compilerAst/test/fixtures/simple_main.p8 index fb81add50..e2795ef11 100644 --- a/compilerAst/test/fixtures/simple_main.p8 +++ b/compilerAst/test/fixtures/simple_main.p8 @@ -1,4 +1,7 @@ +%zeropage basicsafe main { sub start() { + ubyte aa=99 + aa++ } }