From 7530fb67c89280d7054fc695060e9f8dac3e3e78 Mon Sep 17 00:00:00 2001 From: meisl Date: Sun, 4 Jul 2021 18:09:16 +0200 Subject: [PATCH] + add tests for inner nodes' positions; refactor tests --- compilerAst/test/TestProg8Parser.kt | 96 +++++++++++++++++++----- compilerAst/test/fixtures/simple_main.p8 | 4 +- 2 files changed, 78 insertions(+), 22 deletions(-) diff --git a/compilerAst/test/TestProg8Parser.kt b/compilerAst/test/TestProg8Parser.kt index a780643cc..761d66461 100644 --- a/compilerAst/test/TestProg8Parser.kt +++ b/compilerAst/test/TestProg8Parser.kt @@ -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("^$"), 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("^$"), 1, 0, 0) + assertPositionOf(module, Regex("^$"), 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()[0] + assertPositionOf(mainBlock, mpf, 1, 0) // TODO: endCol wrong! + val startSub = mainBlock.statements.filterIsInstance()[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()[0] + assertPositionOf(targetDirective, mpf, 1, 0) // TODO: endCol wrong! + val mainBlock = module.statements.filterIsInstance()[0] + assertPositionOf(mainBlock, mpf, 2, 0) // TODO: endCol wrong! + val startSub = mainBlock.statements.filterIsInstance()[0] + assertPositionOf(startSub, mpf, 3, 4) // TODO: endCol wrong! + val declFoo = startSub.statements.filterIsInstance()[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()[1] + assertPositionOf(declBar, mpf, 5, 8) // TODO: endCol wrong! + val whenStmt = startSub.statements.filterIsInstance()[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(module.statements.first()) - assertEquals((module.statements.first() as Block).name, "main") + assertEquals("main", (module.statements.first() as Block).name) } } diff --git a/compilerAst/test/fixtures/simple_main.p8 b/compilerAst/test/fixtures/simple_main.p8 index afaa79f93..fb81add50 100644 --- a/compilerAst/test/fixtures/simple_main.p8 +++ b/compilerAst/test/fixtures/simple_main.p8 @@ -1,4 +1,4 @@ main { - sub start() { - } + sub start() { + } }