mirror of
https://github.com/irmen/prog8.git
synced 2025-01-26 19:30:59 +00:00
+ add tests for inner nodes' positions; refactor tests
This commit is contained in:
parent
19bb56df47
commit
7530fb67c8
@ -3,11 +3,10 @@ package prog8tests
|
||||
import org.junit.jupiter.api.Test
|
||||
import prog8.ast.Node
|
||||
import prog8.ast.base.Position
|
||||
import prog8.ast.statements.*
|
||||
import kotlin.test.*
|
||||
import java.nio.file.Path // TODO: use kotlin.io.path.Path instead
|
||||
import kotlin.io.path.*
|
||||
import prog8.ast.statements.Block
|
||||
import prog8.ast.statements.Directive
|
||||
import prog8.parser.ParseError
|
||||
import prog8.parser.Prog8Parser.parseModule
|
||||
import prog8.parser.SourceCode
|
||||
@ -199,25 +198,27 @@ class TestProg8Parser {
|
||||
}
|
||||
|
||||
|
||||
fun assertPosition(actual: Position, expFile: String, expLine: Int, expStartCol: Int, expEndCol: Int) {
|
||||
assertEquals(expLine, actual.line, ".position.line (1-based)")
|
||||
assertEquals(expStartCol, actual.startCol, ".position.startCol (0-based)" )
|
||||
assertEquals(expEndCol, actual.endCol, ".position.endCol (0-based)")
|
||||
assertEquals(expFile, actual.file, ".position.file")
|
||||
fun assertPosition(actual: Position, expFile: String? = null, expLine: Int? = null, expStartCol: Int? = null, expEndCol: Int? = null) {
|
||||
require(!listOf(expLine, expStartCol, expEndCol).all { it == null })
|
||||
if (expLine != null) assertEquals(expLine, actual.line, ".position.line (1-based)")
|
||||
if (expStartCol != null) assertEquals(expStartCol, actual.startCol, ".position.startCol (0-based)" )
|
||||
if (expEndCol != null) assertEquals(expEndCol, actual.endCol, ".position.endCol (0-based)")
|
||||
if (expFile != null) assertEquals(expFile, actual.file, ".position.file")
|
||||
}
|
||||
|
||||
fun assertPosition(actual: Position, expFile: Regex, expLine: Int, expStartCol: Int, expEndCol: Int) {
|
||||
assertEquals(expLine, actual.line, ".position.line (1-based)")
|
||||
assertEquals(expStartCol, actual.startCol, ".position.startCol (0-based)" )
|
||||
assertEquals(expEndCol, actual.endCol, ".position.endCol (0-based)")
|
||||
fun assertPosition(actual: Position, expFile: Regex? = null, expLine: Int? = null, expStartCol: Int? = null, expEndCol: Int? = null) {
|
||||
require(!listOf(expLine, expStartCol, expEndCol).all { it == null })
|
||||
if (expLine != null) assertEquals(expLine, actual.line, ".position.line (1-based)")
|
||||
if (expStartCol != null) assertEquals(expStartCol, actual.startCol, ".position.startCol (0-based)" )
|
||||
if (expEndCol != null) assertEquals(expEndCol, actual.endCol, ".position.endCol (0-based)")
|
||||
// Note: assertContains expects *actual* value first
|
||||
assertContains(actual.file, expFile, ".position.file")
|
||||
if (expFile != null) assertContains(actual.file, expFile, ".position.file")
|
||||
}
|
||||
|
||||
fun assertPositionOf(actual: Node, expFile: String, expLine: Int, expStartCol: Int, expEndCol: Int) =
|
||||
fun assertPositionOf(actual: Node, expFile: String? = null, expLine: Int? = null, expStartCol: Int? = null, expEndCol: Int? = null) =
|
||||
assertPosition(actual.position, expFile, expLine, expStartCol, expEndCol)
|
||||
|
||||
fun assertPositionOf(actual: Node, expFile: Regex, expLine: Int, expStartCol: Int, expEndCol: Int) =
|
||||
fun assertPositionOf(actual: Node, expFile: Regex? = null, expLine: Int? = null, expStartCol: Int? = null, expEndCol: Int? = null) =
|
||||
assertPosition(actual.position, expFile, expLine, expStartCol, expEndCol)
|
||||
|
||||
|
||||
@ -230,7 +231,7 @@ class TestProg8Parser {
|
||||
parseModule(SourceCode.of(srcText))
|
||||
} catch (e: ParseError) {
|
||||
assertPosition(e.position, Regex("^<String@[0-9a-f]+>$"), 1, 4, 4)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -241,7 +242,7 @@ class TestProg8Parser {
|
||||
try {
|
||||
parseModule(SourceCode.fromPath(path))
|
||||
} catch (e: ParseError) {
|
||||
assertPosition(e.position, path.absolutePathString(), 2, 6, 6)
|
||||
assertPosition(e.position, path.absolutePathString(), 2, 6) // TODO: endCol wrong
|
||||
}
|
||||
}
|
||||
|
||||
@ -252,7 +253,7 @@ class TestProg8Parser {
|
||||
}
|
||||
""".trimIndent()
|
||||
val module = parseModule(SourceCode.of(srcText))
|
||||
assertPositionOf(module, Regex("^<String@[0-9a-f]+>$"), 1, 0, 0)
|
||||
assertPositionOf(module, Regex("^<String@[0-9a-f]+>$"), 1, 0) // TODO: endCol wrong
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -260,10 +261,65 @@ class TestProg8Parser {
|
||||
val path = fixturesDir.resolve("simple_main.p8")
|
||||
|
||||
val module = parseModule(SourceCode.fromPath(path))
|
||||
assertPositionOf(module, path.absolutePathString(), 1, 0, 0)
|
||||
assertPositionOf(module, path.absolutePathString(), 1, 0) // TODO: endCol wrong
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun testInnerNodePositionsForSourceFromPath() {
|
||||
val path = fixturesDir.resolve("simple_main.p8")
|
||||
|
||||
val module = parseModule(SourceCode.fromPath(path))
|
||||
val mpf = module.position.file
|
||||
|
||||
assertPositionOf(module, path.absolutePathString(), 1, 0) // TODO: endCol wrong
|
||||
val mainBlock = module.statements.filterIsInstance<Block>()[0]
|
||||
assertPositionOf(mainBlock, mpf, 1, 0) // TODO: endCol wrong!
|
||||
val startSub = mainBlock.statements.filterIsInstance<Subroutine>()[0]
|
||||
assertPositionOf(startSub, mpf, 2, 4) // TODO: endCol wrong!
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: this test is testing way too much at once
|
||||
*/
|
||||
@Test
|
||||
fun testInnerNodePositionsForSourceFromString() {
|
||||
val srcText = """
|
||||
%target 16, "abc" ; DirectiveArg directly inherits from Node - neither an Expression nor a Statement..?
|
||||
main {
|
||||
sub start() {
|
||||
ubyte foo = 42
|
||||
ubyte bar
|
||||
when (foo) {
|
||||
23 -> bar = 'x' ; WhenChoice, also directly inheriting Node
|
||||
42 -> bar = 'y'
|
||||
else -> bar = 'z'
|
||||
}
|
||||
}
|
||||
}
|
||||
""".trimIndent()
|
||||
val module = parseModule(SourceCode.of(srcText))
|
||||
val mpf = module.position.file
|
||||
|
||||
val targetDirective = module.statements.filterIsInstance<Directive>()[0]
|
||||
assertPositionOf(targetDirective, mpf, 1, 0) // TODO: endCol wrong!
|
||||
val mainBlock = module.statements.filterIsInstance<Block>()[0]
|
||||
assertPositionOf(mainBlock, mpf, 2, 0) // TODO: endCol wrong!
|
||||
val startSub = mainBlock.statements.filterIsInstance<Subroutine>()[0]
|
||||
assertPositionOf(startSub, mpf, 3, 4) // TODO: endCol wrong!
|
||||
val declFoo = startSub.statements.filterIsInstance<VarDecl>()[0]
|
||||
assertPositionOf(declFoo, mpf, 4, 8) // TODO: endCol wrong!
|
||||
val rhsFoo = declFoo.value!!
|
||||
assertPositionOf(rhsFoo, mpf, 4, 20) // TODO: endCol wrong!
|
||||
val declBar = startSub.statements.filterIsInstance<VarDecl>()[1]
|
||||
assertPositionOf(declBar, mpf, 5, 8) // TODO: endCol wrong!
|
||||
val whenStmt = startSub.statements.filterIsInstance<WhenStatement>()[0]
|
||||
assertPositionOf(whenStmt, mpf, 6, 8) // TODO: endCol wrong!
|
||||
assertPositionOf(whenStmt.choices[0], mpf, 7, 12) // TODO: endCol wrong!
|
||||
assertPositionOf(whenStmt.choices[1], mpf, 8, 12) // TODO: endCol wrong!
|
||||
assertPositionOf(whenStmt.choices[2], mpf, 9, 12) // TODO: endCol wrong!
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun testProg8Ast() {
|
||||
val module = parseModule(SourceCode.of("""
|
||||
@ -274,6 +330,6 @@ class TestProg8Parser {
|
||||
}
|
||||
"""))
|
||||
assertIs<Block>(module.statements.first())
|
||||
assertEquals((module.statements.first() as Block).name, "main")
|
||||
assertEquals("main", (module.statements.first() as Block).name)
|
||||
}
|
||||
}
|
||||
|
4
compilerAst/test/fixtures/simple_main.p8
vendored
4
compilerAst/test/fixtures/simple_main.p8
vendored
@ -1,4 +1,4 @@
|
||||
main {
|
||||
sub start() {
|
||||
}
|
||||
sub start() {
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user