mirror of
https://github.com/irmen/prog8.git
synced 2025-01-12 19:29:50 +00:00
added a few more tests for the file element of Position
This commit is contained in:
parent
cf0e395921
commit
4011dce31b
@ -51,7 +51,6 @@ class ModuleImporter(private val program: Program,
|
|||||||
return executeImportDirective(import, null)
|
return executeImportDirective(import, null)
|
||||||
}
|
}
|
||||||
|
|
||||||
//private fun importModule(stream: CharStream, modulePath: Path, isLibrary: Boolean): Module {
|
|
||||||
private fun importModule(src: SourceCode) : Module {
|
private fun importModule(src: SourceCode) : Module {
|
||||||
val moduleAst = Prog8Parser.parseModule(src)
|
val moduleAst = Prog8Parser.parseModule(src)
|
||||||
program.addModule(moduleAst)
|
program.addModule(moduleAst)
|
||||||
|
@ -4,6 +4,7 @@ import org.junit.jupiter.api.Nested
|
|||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import org.junit.jupiter.api.TestInstance
|
import org.junit.jupiter.api.TestInstance
|
||||||
import prog8.ast.IFunctionCall
|
import prog8.ast.IFunctionCall
|
||||||
|
import prog8.ast.Module
|
||||||
import prog8.ast.Node
|
import prog8.ast.Node
|
||||||
import prog8.ast.base.Position
|
import prog8.ast.base.Position
|
||||||
import prog8.ast.expressions.CharLiteral
|
import prog8.ast.expressions.CharLiteral
|
||||||
@ -17,12 +18,11 @@ import prog8.parser.SourceCode
|
|||||||
import prog8tests.helpers.assumeNotExists
|
import prog8tests.helpers.assumeNotExists
|
||||||
import prog8tests.helpers.assumeReadableFile
|
import prog8tests.helpers.assumeReadableFile
|
||||||
import prog8tests.helpers.fixturesDir
|
import prog8tests.helpers.fixturesDir
|
||||||
|
import kotlin.io.path.Path
|
||||||
|
import kotlin.io.path.isRegularFile
|
||||||
import kotlin.io.path.name
|
import kotlin.io.path.name
|
||||||
import kotlin.io.path.nameWithoutExtension
|
import kotlin.io.path.nameWithoutExtension
|
||||||
import kotlin.test.assertContains
|
import kotlin.test.*
|
||||||
import kotlin.test.assertEquals
|
|
||||||
import kotlin.test.assertFailsWith
|
|
||||||
import kotlin.test.assertIs
|
|
||||||
|
|
||||||
|
|
||||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||||
@ -330,9 +330,9 @@ class TestProg8Parser {
|
|||||||
val mpf = module.position.file
|
val mpf = module.position.file
|
||||||
assertPositionOf(module, SourceCode.relative(path).toString(), 1, 0) // TODO: endCol wrong
|
assertPositionOf(module, SourceCode.relative(path).toString(), 1, 0) // TODO: endCol wrong
|
||||||
val mainBlock = module.statements.filterIsInstance<Block>()[0]
|
val mainBlock = module.statements.filterIsInstance<Block>()[0]
|
||||||
assertPositionOf(mainBlock, mpf, 1, 0) // TODO: endCol wrong!
|
assertPositionOf(mainBlock, mpf, 2, 0) // TODO: endCol wrong!
|
||||||
val startSub = mainBlock.statements.filterIsInstance<Subroutine>()[0]
|
val startSub = mainBlock.statements.filterIsInstance<Subroutine>()[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<Block>().forEach { b ->
|
||||||
|
asserter(b)
|
||||||
|
b.statements.forEach(asserter)
|
||||||
|
b.statements.filterIsInstance<Subroutine>().forEach { s ->
|
||||||
|
asserter(s)
|
||||||
|
s.statements.forEach(asserter)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} }
|
||||||
|
|
||||||
@Nested
|
@Nested
|
||||||
inner class CharLiterals {
|
inner class CharLiterals {
|
||||||
|
|
||||||
|
@ -1,30 +1,32 @@
|
|||||||
package prog8tests
|
package prog8tests
|
||||||
|
|
||||||
|
import org.hamcrest.MatcherAssert.assertThat
|
||||||
|
import org.hamcrest.core.StringStartsWith
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import org.junit.jupiter.api.TestInstance
|
import org.junit.jupiter.api.TestInstance
|
||||||
import prog8.parser.SourceCode
|
import prog8.parser.SourceCode
|
||||||
import prog8.parser.SourceCode.Companion.libraryFilePrefix
|
import prog8.parser.SourceCode.Companion.libraryFilePrefix
|
||||||
import prog8tests.helpers.*
|
import prog8tests.helpers.*
|
||||||
import kotlin.io.path.Path
|
import kotlin.io.path.Path
|
||||||
import kotlin.test.assertContains
|
import kotlin.test.*
|
||||||
import kotlin.test.assertEquals
|
|
||||||
import kotlin.test.assertFailsWith
|
|
||||||
import kotlin.test.assertTrue
|
|
||||||
|
|
||||||
|
|
||||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||||
class TestSourceCode {
|
class TestSourceCode {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testFactoryMethod_Of() {
|
fun testFromString() {
|
||||||
val text = """
|
val text = """
|
||||||
main { }
|
main { }
|
||||||
""".trimIndent()
|
"""
|
||||||
val src = SourceCode.Text(text)
|
val src = SourceCode.Text(text)
|
||||||
val actualText = src.getCharStream().toString()
|
val actualText = src.getCharStream().toString()
|
||||||
|
|
||||||
assertContains(src.origin, Regex("^<String@[0-9a-f]+>$"))
|
assertContains(src.origin, Regex("^<String@[0-9a-f]+>$"))
|
||||||
assertEquals(text, actualText)
|
assertEquals(text, actualText)
|
||||||
|
assertFalse(src.isFromResources)
|
||||||
|
assertFalse(src.isFromFilesystem)
|
||||||
|
assertThat(src.toString(), StringStartsWith("prog8.parser.SourceCode"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -54,6 +56,8 @@ class TestSourceCode {
|
|||||||
val expectedOrigin = SourceCode.relative(path).toString()
|
val expectedOrigin = SourceCode.relative(path).toString()
|
||||||
assertEquals(expectedOrigin, src.origin)
|
assertEquals(expectedOrigin, src.origin)
|
||||||
assertEquals(path.toFile().readText(), src.asString())
|
assertEquals(path.toFile().readText(), src.asString())
|
||||||
|
assertFalse(src.isFromResources)
|
||||||
|
assertTrue(src.isFromFilesystem)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -75,6 +79,8 @@ class TestSourceCode {
|
|||||||
|
|
||||||
assertEquals("$libraryFilePrefix/$pathString", src.origin)
|
assertEquals("$libraryFilePrefix/$pathString", src.origin)
|
||||||
assertEquals(srcFile.readText(), src.asString())
|
assertEquals(srcFile.readText(), src.asString())
|
||||||
|
assertTrue(src.isFromResources)
|
||||||
|
assertFalse(src.isFromFilesystem)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
3
compilerAst/test/fixtures/simple_main.p8
vendored
3
compilerAst/test/fixtures/simple_main.p8
vendored
@ -1,4 +1,7 @@
|
|||||||
|
%zeropage basicsafe
|
||||||
main {
|
main {
|
||||||
sub start() {
|
sub start() {
|
||||||
|
ubyte aa=99
|
||||||
|
aa++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user