mirror of
https://github.com/irmen/prog8.git
synced 2024-12-25 08:29:25 +00:00
* structure TestProg8Parser with @Nested
This commit is contained in:
parent
ef0c4797bb
commit
c2986eaf47
@ -4,6 +4,7 @@ import prog8tests.helpers.*
|
||||
import org.junit.jupiter.api.TestInstance
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.Disabled
|
||||
import org.junit.jupiter.api.Nested
|
||||
import kotlin.test.*
|
||||
import kotlin.io.path.*
|
||||
|
||||
@ -19,6 +20,12 @@ import prog8.ast.expressions.*
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
class TestProg8Parser {
|
||||
|
||||
@Nested
|
||||
inner class Newline {
|
||||
|
||||
@Nested
|
||||
inner class AtEnd {
|
||||
|
||||
@Test
|
||||
fun testModuleSourceNeedNotEndWithNewline() {
|
||||
val nl = "\n" // say, Unix-style (different flavours tested elsewhere)
|
||||
@ -36,6 +43,7 @@ class TestProg8Parser {
|
||||
val module = parseModule(SourceCode.of(srcText))
|
||||
assertEquals(1, module.statements.size)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAllBlocksButLastMustEndWithNewline() {
|
||||
@ -52,81 +60,6 @@ class TestProg8Parser {
|
||||
assertEquals(2, module.statements.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testWindowsAndMacNewlinesAreAlsoFine() {
|
||||
val nlWin = "\r\n"
|
||||
val nlUnix = "\n"
|
||||
val nlMac = "\r"
|
||||
|
||||
//parseModule(Paths.get("test", "fixtures", "mac_newlines.p8").toAbsolutePath())
|
||||
|
||||
// a good mix of all kinds of newlines:
|
||||
val srcText =
|
||||
"foo {" +
|
||||
nlMac +
|
||||
nlWin +
|
||||
"}" +
|
||||
nlMac + // <-- do test a single \r (!) where an EOL is expected
|
||||
"bar {" +
|
||||
nlUnix +
|
||||
"}" +
|
||||
nlUnix + nlMac // both should be "eaten up" by just one EOL token
|
||||
"combi {" +
|
||||
nlMac + nlWin + nlUnix // all three should be "eaten up" by just one EOL token
|
||||
"}" +
|
||||
nlUnix // end with newline (see testModuleSourceNeedNotEndWithNewline)
|
||||
|
||||
val module = parseModule(SourceCode.of(srcText))
|
||||
assertEquals(2, module.statements.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInterleavedEolAndCommentBeforeFirstBlock() {
|
||||
// issue: #47
|
||||
val srcText = """
|
||||
; comment
|
||||
|
||||
; comment
|
||||
|
||||
blockA {
|
||||
}
|
||||
"""
|
||||
val module = parseModule(SourceCode.of(srcText))
|
||||
assertEquals(1, module.statements.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInterleavedEolAndCommentBetweenBlocks() {
|
||||
// issue: #47
|
||||
val srcText = """
|
||||
blockA {
|
||||
}
|
||||
; comment
|
||||
|
||||
; comment
|
||||
|
||||
blockB {
|
||||
}
|
||||
"""
|
||||
val module = parseModule(SourceCode.of(srcText))
|
||||
assertEquals(2, module.statements.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInterleavedEolAndCommentAfterLastBlock() {
|
||||
// issue: #47
|
||||
val srcText = """
|
||||
blockA {
|
||||
}
|
||||
; comment
|
||||
|
||||
; comment
|
||||
|
||||
"""
|
||||
val module = parseModule(SourceCode.of(srcText))
|
||||
assertEquals(1, module.statements.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testNewlineBetweenTwoBlocksOrDirectivesStillRequired() {
|
||||
// issue: #47
|
||||
@ -158,6 +91,89 @@ class TestProg8Parser {
|
||||
""")) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testWindowsAndMacNewlinesAreAlsoFine() {
|
||||
val nlWin = "\r\n"
|
||||
val nlUnix = "\n"
|
||||
val nlMac = "\r"
|
||||
|
||||
//parseModule(Paths.get("test", "fixtures", "mac_newlines.p8").toAbsolutePath())
|
||||
|
||||
// a good mix of all kinds of newlines:
|
||||
val srcText =
|
||||
"foo {" +
|
||||
nlMac +
|
||||
nlWin +
|
||||
"}" +
|
||||
nlMac + // <-- do test a single \r (!) where an EOL is expected
|
||||
"bar {" +
|
||||
nlUnix +
|
||||
"}" +
|
||||
nlUnix + nlMac // both should be "eaten up" by just one EOL token
|
||||
"combi {" +
|
||||
nlMac + nlWin + nlUnix // all three should be "eaten up" by just one EOL token
|
||||
"}" +
|
||||
nlUnix // end with newline (see testModuleSourceNeedNotEndWithNewline)
|
||||
|
||||
val module = parseModule(SourceCode.of(srcText))
|
||||
assertEquals(2, module.statements.size)
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
inner class EOLsInterleavedWithComments {
|
||||
|
||||
@Test
|
||||
fun testInterleavedEolAndCommentBeforeFirstBlock() {
|
||||
// issue: #47
|
||||
val srcText = """
|
||||
; comment
|
||||
|
||||
; comment
|
||||
|
||||
blockA {
|
||||
}
|
||||
"""
|
||||
val module = parseModule(SourceCode.of(srcText))
|
||||
assertEquals(1, module.statements.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInterleavedEolAndCommentBetweenBlocks() {
|
||||
// issue: #47
|
||||
val srcText = """
|
||||
blockA {
|
||||
}
|
||||
; comment
|
||||
|
||||
; comment
|
||||
|
||||
blockB {
|
||||
}
|
||||
"""
|
||||
val module = parseModule(SourceCode.of(srcText))
|
||||
assertEquals(2, module.statements.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInterleavedEolAndCommentAfterLastBlock() {
|
||||
// issue: #47
|
||||
val srcText = """
|
||||
blockA {
|
||||
}
|
||||
; comment
|
||||
|
||||
; comment
|
||||
|
||||
"""
|
||||
val module = parseModule(SourceCode.of(srcText))
|
||||
assertEquals(1, module.statements.size)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Nested
|
||||
inner class ImportDirectives {
|
||||
@Test
|
||||
fun parseModuleShouldNotLookAtImports() {
|
||||
val importedNoExt = assumeNotExists(fixturesDir, "i_do_not_exist")
|
||||
@ -167,8 +183,11 @@ class TestProg8Parser {
|
||||
|
||||
assertEquals(1, module.statements.size)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Nested
|
||||
inner class EmptySourcecode {
|
||||
@Test
|
||||
fun testParseModuleWithEmptyString() {
|
||||
val module = parseModule(SourceCode.of(""))
|
||||
@ -177,11 +196,14 @@ class TestProg8Parser {
|
||||
|
||||
@Test
|
||||
fun testParseModuleWithEmptyFile() {
|
||||
val path = assumeReadableFile(fixturesDir,"empty.p8")
|
||||
val path = assumeReadableFile(fixturesDir, "empty.p8")
|
||||
val module = parseModule(SourceCode.fromPath(path))
|
||||
assertEquals(0, module.statements.size)
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
inner class NameOfModule {
|
||||
@Test
|
||||
fun testModuleNameForSourceFromString() {
|
||||
val srcText = """
|
||||
@ -196,33 +218,61 @@ class TestProg8Parser {
|
||||
|
||||
@Test
|
||||
fun testModuleNameForSourceFromPath() {
|
||||
val path = assumeReadableFile(fixturesDir,"simple_main.p8")
|
||||
val path = assumeReadableFile(fixturesDir, "simple_main.p8")
|
||||
val module = parseModule(SourceCode.fromPath(path))
|
||||
assertEquals(path.nameWithoutExtension, module.name)
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
inner class PositionOfAstNodesAndParseErrors {
|
||||
|
||||
|
||||
fun assertPosition(actual: Position, expFile: String? = null, expLine: Int? = null, expStartCol: Int? = null, expEndCol: Int? = null) {
|
||||
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 (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? = null, expLine: Int? = null, expStartCol: Int? = null, expEndCol: Int? = null) {
|
||||
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 (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
|
||||
if (expFile != null) assertContains(actual.file, expFile, ".position.file")
|
||||
}
|
||||
|
||||
fun assertPositionOf(actual: Node, expFile: String? = null, expLine: Int? = null, expStartCol: Int? = null, expEndCol: Int? = null) =
|
||||
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? = null, expLine: Int? = null, expStartCol: Int? = null, expEndCol: Int? = null) =
|
||||
fun assertPositionOf(
|
||||
actual: Node,
|
||||
expFile: Regex? = null,
|
||||
expLine: Int? = null,
|
||||
expStartCol: Int? = null,
|
||||
expEndCol: Int? = null
|
||||
) =
|
||||
assertPosition(actual.position, expFile, expLine, expStartCol, expEndCol)
|
||||
|
||||
|
||||
@ -262,7 +312,7 @@ class TestProg8Parser {
|
||||
|
||||
@Test
|
||||
fun testModulePositionForSourceFromPath() {
|
||||
val path = assumeReadableFile(fixturesDir,"simple_main.p8")
|
||||
val path = assumeReadableFile(fixturesDir, "simple_main.p8")
|
||||
|
||||
val module = parseModule(SourceCode.fromPath(path))
|
||||
assertPositionOf(module, path.absolutePathString(), 1, 0) // TODO: endCol wrong
|
||||
@ -270,7 +320,7 @@ class TestProg8Parser {
|
||||
|
||||
@Test
|
||||
fun testInnerNodePositionsForSourceFromPath() {
|
||||
val path = assumeReadableFile(fixturesDir,"simple_main.p8")
|
||||
val path = assumeReadableFile(fixturesDir, "simple_main.p8")
|
||||
|
||||
val module = parseModule(SourceCode.fromPath(path))
|
||||
val mpf = module.position.file
|
||||
@ -282,6 +332,7 @@ class TestProg8Parser {
|
||||
assertPositionOf(startSub, mpf, 2, 4) // TODO: endCol wrong!
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* TODO: this test is testing way too much at once
|
||||
*/
|
||||
@ -323,6 +374,10 @@ class TestProg8Parser {
|
||||
assertPositionOf(whenStmt.choices[1], mpf, 8, 12) // TODO: endCol wrong!
|
||||
assertPositionOf(whenStmt.choices[2], mpf, 9, 12) // TODO: endCol wrong!
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
inner class CharLiterals {
|
||||
|
||||
@Test
|
||||
fun testCharLitAsArg() {
|
||||
@ -418,7 +473,10 @@ class TestProg8Parser {
|
||||
assertEquals('x', rhs.value, "char literal's .value")
|
||||
assertEquals(true, rhs.altEncoding, "char literal's .altEncoding")
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
inner class Ranges {
|
||||
|
||||
@Test
|
||||
fun testForloop() {
|
||||
@ -467,4 +525,6 @@ class TestProg8Parser {
|
||||
assertIs<NumericLiteralValue>(it4.from, "parser should leave it as is")
|
||||
assertIs<NumericLiteralValue>(it4.to, "parser should leave it as is")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user