diff --git a/src/test/interpreter/VisitorIntegrationTest.java b/src/test/interpreter/VisitorIntegrationTest.java index 835fcc5..43d3953 100644 --- a/src/test/interpreter/VisitorIntegrationTest.java +++ b/src/test/interpreter/VisitorIntegrationTest.java @@ -1,8 +1,14 @@ package interpreter; +import interpreter.antlr.MineScriptLexer; import interpreter.antlr.MineScriptParser; import interpreter.types.MSBool; import interpreter.types.MSNumber; +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.CharStreams; +import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.misc.ParseCancellationException; +import org.antlr.v4.runtime.tree.ParseTree; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -558,4 +564,82 @@ void visitIfLocalScopeOuterAccessThrowsRuntimeException() { Assertions.assertThrows(RuntimeException.class, () -> visitor.visit(getProgTreeFromString(input))); } + + @Test + void integrationTestParserDoesNotThrowErrors(){ + String program = """ + a = 0 + x = 0 + if (true) do + x = 123 + endif + a = x + """; + + CharStream input = CharStreams.fromString(program + System.lineSeparator()); + MineScriptLexer lexer = new MineScriptLexer(input); + lexer.removeErrorListeners(); + lexer.addErrorListener(InterpreterErrorListener.INSTANCE); + CommonTokenStream tokens = new CommonTokenStream(lexer); + MineScriptParser parser = new MineScriptParser(tokens); + parser.removeErrorListeners(); + parser.addErrorListener(InterpreterErrorListener.INSTANCE); + ParseTree tree = parser.program(); + + String expectedOutput = "(program (statement a = (expression 0) \\n) (statement x = (expression 0) \\n) (statement if ( (expression true) ) (statements do \\n (statement x = (expression 123) \\n)) endif \\n) (statement a = (expression x) \\n\\n) )"; + + Assertions.assertEquals(expectedOutput, tree.toStringTree(parser)); + Assertions.assertDoesNotThrow(() -> visitor.visit(tree)); + } + + @Test + void integrationTestVisitorThrowsTreeError(){ + String program = """ + a = 0 + if (true) do + x = 123 + endif + a = x + """; + + CharStream input = CharStreams.fromString(program + System.lineSeparator()); + MineScriptLexer lexer = new MineScriptLexer(input); + lexer.removeErrorListeners(); + lexer.addErrorListener(InterpreterErrorListener.INSTANCE); + CommonTokenStream tokens = new CommonTokenStream(lexer); + MineScriptParser parser = new MineScriptParser(tokens); + parser.removeErrorListeners(); + parser.addErrorListener(InterpreterErrorListener.INSTANCE); + ParseTree tree = parser.program(); + + String expectedOutput = "(program (statement a = (expression 0) \\n) (statement if ( (expression true) ) (statements do \\n (statement x = (expression 123) \\n)) endif \\n) (statement a = (expression x) \\n\\n) )"; + + Assertions.assertEquals(expectedOutput, tree.toStringTree(parser)); + Assertions.assertThrows(RuntimeException.class, () -> visitor.visit(tree)); + } + + @Test + void integrationTestParserThrowsTreeError(){ + String program = """ + a = 0 + x = 0 + if (true) + x = 123 + endif + a = x + Print(1) + """; + + CharStream input = CharStreams.fromString(program + System.lineSeparator()); + MineScriptLexer lexer = new MineScriptLexer(input); + lexer.removeErrorListeners(); + lexer.addErrorListener(InterpreterErrorListener.INSTANCE); + CommonTokenStream tokens = new CommonTokenStream(lexer); + MineScriptParser parser = new MineScriptParser(tokens); + parser.removeErrorListeners(); + parser.addErrorListener(InterpreterErrorListener.INSTANCE); + Assertions.assertThrows(ParseCancellationException.class, () -> parser.program()); + + } + } \ No newline at end of file