From a2588a178cf54dd7d957f77d53073060d910fb27 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 13 Jun 2021 14:59:57 +0200 Subject: [PATCH] added some simple unit tests to the ast parser --- compiler/compiler.iml | 2 +- compilerAst/build.gradle | 18 ++++++ compilerAst/compilerAst.iml | 2 + compilerAst/test/TestAntlrParser.kt | 72 +++++++++++++++++++++ dbusCompilerService/dbusCompilerService.iml | 6 +- httpCompilerService/httpCompilerService.iml | 2 +- 6 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 compilerAst/test/TestAntlrParser.kt diff --git a/compiler/compiler.iml b/compiler/compiler.iml index 3ed342866..daef22628 100644 --- a/compiler/compiler.iml +++ b/compiler/compiler.iml @@ -13,7 +13,7 @@ - + diff --git a/compilerAst/build.gradle b/compilerAst/build.gradle index 9b993cea7..258986ce1 100644 --- a/compilerAst/build.gradle +++ b/compilerAst/build.gradle @@ -25,6 +25,11 @@ dependencies { implementation 'org.antlr:antlr4-runtime:4.9' implementation project(':parser') + testImplementation "org.jetbrains.kotlin:kotlin-test-junit5" + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.2' + testImplementation 'org.hamcrest:hamcrest-junit:2.0.0.0' + testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.2' + // antlr('org.antlr:antlr4:4.9') { // exclude group: 'com.ibm.icu', module: 'icu4j' // } @@ -62,6 +67,19 @@ sourceSets { } } +test { + // Enable JUnit 5 (Gradle 4.6+). + useJUnitPlatform() + + // Always run tests, even when nothing changed. + dependsOn 'cleanTest' + + // Show test results. + testLogging { + events "skipped", "failed" + } +} + task wrapper(type: Wrapper) { gradleVersion = '6.7' } diff --git a/compilerAst/compilerAst.iml b/compilerAst/compilerAst.iml index c121944e7..240433064 100644 --- a/compilerAst/compilerAst.iml +++ b/compilerAst/compilerAst.iml @@ -4,11 +4,13 @@ + + \ No newline at end of file diff --git a/compilerAst/test/TestAntlrParser.kt b/compilerAst/test/TestAntlrParser.kt new file mode 100644 index 000000000..45fc55679 --- /dev/null +++ b/compilerAst/test/TestAntlrParser.kt @@ -0,0 +1,72 @@ +package prog8tests + +import org.antlr.v4.runtime.* +import org.junit.jupiter.api.Test +import prog8.ast.IStringEncoding +import prog8.ast.antlr.toAst +import prog8.ast.statements.Block +import prog8.parser.* +import java.nio.file.Path +import kotlin.test.* + +class TestAntlrParser { + + class MyErrorListener: ConsoleErrorListener() { + override fun syntaxError(recognizer: Recognizer<*, *>?, offendingSymbol: Any?, line: Int, charPositionInLine: Int, msg: String, e: RecognitionException?) { + throw ParsingFailedError(msg) + } + } + + object TestStringEncoding: IStringEncoding { + override fun encodeString(str: String, altEncoding: Boolean): List { + TODO("Not yet implemented") + } + + override fun decodeString(bytes: List, altEncoding: Boolean): String { + TODO("Not yet implemented") + } + } + + @Test + fun testAntlrTree() { + // can create charstreams from many other sources as well; + val charstream = CharStreams.fromString(""" +main { + sub start() { + return + } +} +""") + val lexer = prog8Lexer(charstream) + val tokens = CommonTokenStream(lexer) + val parser = prog8Parser(tokens) + parser.errorHandler = BailErrorStrategy() +// parser.removeErrorListeners() +// parser.addErrorListener(MyErrorListener()) + val nodes = parser.module() + val blockName = nodes.block(0).identifier().NAME().text + assertEquals(blockName, "main") + } + + @Test + fun testProg8Ast() { + // can create charstreams from many other sources as well; + val charstream = CharStreams.fromString(""" +main { + sub start() { + return + } +} +""") + val lexer = prog8Lexer(charstream) + val tokens = CommonTokenStream(lexer) + val parser = prog8Parser(tokens) + parser.errorHandler = BailErrorStrategy() +// parser.removeErrorListeners() +// parser.addErrorListener(MyErrorListener()) + + val ast = parser.module().toAst("test", false, Path.of(""), TestStringEncoding) + assertIs(ast.statements.first()) + assertEquals((ast.statements.first() as Block).name, "main") + } +} diff --git a/dbusCompilerService/dbusCompilerService.iml b/dbusCompilerService/dbusCompilerService.iml index 9cc8bc298..68e671f9a 100644 --- a/dbusCompilerService/dbusCompilerService.iml +++ b/dbusCompilerService/dbusCompilerService.iml @@ -2,10 +2,10 @@ - - + + - + diff --git a/httpCompilerService/httpCompilerService.iml b/httpCompilerService/httpCompilerService.iml index a65181582..cc1763d0c 100644 --- a/httpCompilerService/httpCompilerService.iml +++ b/httpCompilerService/httpCompilerService.iml @@ -5,7 +5,7 @@ - +