Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/test/interpreter/VisitorIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) <EOF>)";

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) <EOF>)";

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());

}

}