refactor tests
This commit is contained in:
57
src/test/java/parser/LexerGrammarParserTest.java
Normal file
57
src/test/java/parser/LexerGrammarParserTest.java
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
package parser;
|
||||||
|
|
||||||
|
import lexer.StupsLexer;
|
||||||
|
import org.antlr.v4.runtime.CharStreams;
|
||||||
|
import org.antlr.v4.runtime.Lexer;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
|
import parser.grammar.Grammar;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
class LexerGrammarParserTest {
|
||||||
|
|
||||||
|
private static StupsParser parser;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void init() throws IOException, URISyntaxException {
|
||||||
|
final Path path = Paths.get(LexerGrammarParserTest.class.getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||||
|
final Grammar grammar = Grammar.fromFile(path);
|
||||||
|
parser = StupsParser.fromGrammar(grammar);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static StupsLexer getLexer(String program) {
|
||||||
|
try {
|
||||||
|
final Path path = Paths.get(LexerGrammarParserTest.class.getClassLoader().getResource("examplePrograms/" + program).toURI());
|
||||||
|
final String programCode = Files.readString(path, StandardCharsets.US_ASCII);
|
||||||
|
return new StupsLexer(CharStreams.fromString(programCode));
|
||||||
|
} catch (Exception ignore) {
|
||||||
|
ignore.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = {"EmptyFile.stups",
|
||||||
|
"EmptyMain.stups",
|
||||||
|
"GeneralComment.stups",
|
||||||
|
"MultipleDeclarations.stups",
|
||||||
|
"DeclarationAssignment.stups",
|
||||||
|
"Expr.stups",
|
||||||
|
"GeneralWhile.stups",
|
||||||
|
"GeneralIfElse.stups"})
|
||||||
|
void testVariousPrograms(String prog) {
|
||||||
|
final Lexer lex = getLexer(prog);
|
||||||
|
|
||||||
|
assertThat(parser.parse(lex.getAllTokens(), lex.getVocabulary())).isNotNull();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,110 +0,0 @@
|
|||||||
package parser;
|
|
||||||
|
|
||||||
import lexer.StupsLexer;
|
|
||||||
import org.antlr.v4.runtime.CharStreams;
|
|
||||||
import org.antlr.v4.runtime.Lexer;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.URISyntaxException;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
|
|
||||||
class LexerStupsParserGrammarTest {
|
|
||||||
|
|
||||||
private Lexer initLexer(String program) {
|
|
||||||
try {
|
|
||||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("examplePrograms/" + program).toURI());
|
|
||||||
final String programCode = Files.readString(path, StandardCharsets.US_ASCII);
|
|
||||||
return new StupsLexer(CharStreams.fromString(programCode));
|
|
||||||
} catch (Exception ignore) {
|
|
||||||
ignore.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testEmptyFile() throws URISyntaxException, IOException {
|
|
||||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
|
||||||
final StupsParser stupsParser = StupsParser.fromGrammar(path);
|
|
||||||
|
|
||||||
final Lexer lex = this.initLexer("EmptyFile.stups");
|
|
||||||
|
|
||||||
assertThat(stupsParser.parse(lex.getAllTokens(), lex.getVocabulary())).isNotNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testEmptyMain() throws URISyntaxException, IOException {
|
|
||||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
|
||||||
final StupsParser stupsParser = StupsParser.fromGrammar(path);
|
|
||||||
|
|
||||||
final Lexer lex = this.initLexer("EmptyMain.stups");
|
|
||||||
|
|
||||||
assertThat(stupsParser.parse(lex.getAllTokens(), lex.getVocabulary())).isNotNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testGeneralComment() throws URISyntaxException, IOException {
|
|
||||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
|
||||||
final StupsParser stupsParser = StupsParser.fromGrammar(path);
|
|
||||||
|
|
||||||
final Lexer lex = this.initLexer("GeneralComment.stups");
|
|
||||||
|
|
||||||
assertThat(stupsParser.parse(lex.getAllTokens(), lex.getVocabulary())).isNotNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void tesMultiDecl() throws URISyntaxException, IOException {
|
|
||||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
|
||||||
final StupsParser stupsParser = StupsParser.fromGrammar(path);
|
|
||||||
|
|
||||||
final Lexer lex = this.initLexer("MultipleDeclarations.stups");
|
|
||||||
|
|
||||||
assertThat(stupsParser.parse(lex.getAllTokens(), lex.getVocabulary())).isNotNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testDeclarationAssignment() throws URISyntaxException, IOException {
|
|
||||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
|
||||||
final StupsParser stupsParser = StupsParser.fromGrammar(path);
|
|
||||||
|
|
||||||
final Lexer lex = this.initLexer("DeclarationAssignment.stups");
|
|
||||||
|
|
||||||
assertThat(stupsParser.parse(lex.getAllTokens(), lex.getVocabulary())).isNotNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testExpr() throws URISyntaxException, IOException {
|
|
||||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
|
||||||
final StupsParser stupsParser = StupsParser.fromGrammar(path);
|
|
||||||
|
|
||||||
final Lexer lex = this.initLexer("Expr.stups");
|
|
||||||
|
|
||||||
assertThat(stupsParser.parse(lex.getAllTokens(), lex.getVocabulary())).isNotNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testGeneralWhile() throws URISyntaxException, IOException {
|
|
||||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
|
||||||
final StupsParser stupsParser = StupsParser.fromGrammar(path);
|
|
||||||
|
|
||||||
final Lexer lex = this.initLexer("GeneralWhile.stups");
|
|
||||||
|
|
||||||
assertThat(stupsParser.parse(lex.getAllTokens(), lex.getVocabulary())).isNotNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testGeneralIfElse() throws URISyntaxException, IOException {
|
|
||||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
|
||||||
final StupsParser stupsParser = StupsParser.fromGrammar(path);
|
|
||||||
|
|
||||||
final Lexer lex = this.initLexer("GeneralIfElse.stups");
|
|
||||||
|
|
||||||
assertThat(stupsParser.parse(lex.getAllTokens(), lex.getVocabulary())).isNotNull();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -83,10 +83,8 @@ class ASTBalancerTest {
|
|||||||
@Test
|
@Test
|
||||||
void testTree1Flip() {
|
void testTree1Flip() {
|
||||||
final AST tree = tree1();
|
final AST tree = tree1();
|
||||||
System.out.println("Before:\n" + tree);
|
|
||||||
|
|
||||||
ASTBalancer.flip(tree);
|
ASTBalancer.flip(tree);
|
||||||
System.out.println("After:\n" + tree);
|
|
||||||
|
|
||||||
assertThat(tree.getRoot().getChildren().get(0).getName()).isEqualTo("INTEGER_LIT");
|
assertThat(tree.getRoot().getChildren().get(0).getName()).isEqualTo("INTEGER_LIT");
|
||||||
assertThat(tree.getRoot().getChildren().get(1).getName()).isEqualTo("expr");
|
assertThat(tree.getRoot().getChildren().get(1).getName()).isEqualTo("expr");
|
||||||
@ -95,11 +93,9 @@ class ASTBalancerTest {
|
|||||||
@Test
|
@Test
|
||||||
void testTree1Flip2x() {
|
void testTree1Flip2x() {
|
||||||
final AST tree = tree1();
|
final AST tree = tree1();
|
||||||
System.out.println("Before:\n" + tree);
|
|
||||||
|
|
||||||
ASTBalancer.flip(tree);
|
ASTBalancer.flip(tree);
|
||||||
ASTBalancer.flip(tree);
|
ASTBalancer.flip(tree);
|
||||||
System.out.println("After:\n" + tree);
|
|
||||||
|
|
||||||
assertThat(tree).isEqualTo(tree1());
|
assertThat(tree).isEqualTo(tree1());
|
||||||
}
|
}
|
||||||
@ -107,10 +103,8 @@ class ASTBalancerTest {
|
|||||||
@Test
|
@Test
|
||||||
void testTree2Flip() {
|
void testTree2Flip() {
|
||||||
final AST tree = tree2();
|
final AST tree = tree2();
|
||||||
System.out.println("Before:\n" + tree);
|
|
||||||
|
|
||||||
ASTBalancer.flip(tree);
|
ASTBalancer.flip(tree);
|
||||||
System.out.println("After:\n" + tree);
|
|
||||||
|
|
||||||
assertThat(tree.getRoot().getChildren().get(0).getName()).isEqualTo("INTEGER_LIT");
|
assertThat(tree.getRoot().getChildren().get(0).getName()).isEqualTo("INTEGER_LIT");
|
||||||
assertThat(tree.getRoot().getChildren().get(1).getName()).isEqualTo("expr");
|
assertThat(tree.getRoot().getChildren().get(1).getName()).isEqualTo("expr");
|
||||||
@ -121,11 +115,9 @@ class ASTBalancerTest {
|
|||||||
@Test
|
@Test
|
||||||
void testTree2Flip2x() {
|
void testTree2Flip2x() {
|
||||||
final AST tree = tree2();
|
final AST tree = tree2();
|
||||||
System.out.println("Before:\n" + tree);
|
|
||||||
|
|
||||||
ASTBalancer.flip(tree);
|
ASTBalancer.flip(tree);
|
||||||
ASTBalancer.flip(tree);
|
ASTBalancer.flip(tree);
|
||||||
System.out.println("After:\n" + tree);
|
|
||||||
|
|
||||||
assertThat(tree).isEqualTo(tree2());
|
assertThat(tree).isEqualTo(tree2());
|
||||||
}
|
}
|
||||||
@ -134,10 +126,8 @@ class ASTBalancerTest {
|
|||||||
void testTree1LeftPrecedence() {
|
void testTree1LeftPrecedence() {
|
||||||
final AST tree = tree1();
|
final AST tree = tree1();
|
||||||
ASTBalancer.flip(tree);
|
ASTBalancer.flip(tree);
|
||||||
System.out.println("Before:\n" + tree);
|
|
||||||
|
|
||||||
ASTBalancer.leftPrecedence(tree);
|
ASTBalancer.leftPrecedence(tree);
|
||||||
System.out.println("After:\n" + tree);
|
|
||||||
|
|
||||||
assertThat(tree.size()).isEqualTo(3);
|
assertThat(tree.size()).isEqualTo(3);
|
||||||
assertThat(tree.getRoot().getValue()).isEqualTo("SUB");
|
assertThat(tree.getRoot().getValue()).isEqualTo("SUB");
|
||||||
@ -147,10 +137,8 @@ class ASTBalancerTest {
|
|||||||
void testTree2LeftPrecedence() {
|
void testTree2LeftPrecedence() {
|
||||||
final AST tree = tree2();
|
final AST tree = tree2();
|
||||||
ASTBalancer.flip(tree);
|
ASTBalancer.flip(tree);
|
||||||
System.out.println("Before:\n" + tree);
|
|
||||||
|
|
||||||
ASTBalancer.leftPrecedence(tree);
|
ASTBalancer.leftPrecedence(tree);
|
||||||
System.out.println("After:\n" + tree);
|
|
||||||
|
|
||||||
assertThat(tree.size()).isEqualTo(5);
|
assertThat(tree.size()).isEqualTo(5);
|
||||||
assertThat(tree.getRoot().getValue()).isEqualTo("SUB");
|
assertThat(tree.getRoot().getValue()).isEqualTo("SUB");
|
||||||
@ -161,14 +149,12 @@ class ASTBalancerTest {
|
|||||||
final AST tree = tree2();
|
final AST tree = tree2();
|
||||||
ASTBalancer.flip(tree);
|
ASTBalancer.flip(tree);
|
||||||
ASTBalancer.leftPrecedence(tree);
|
ASTBalancer.leftPrecedence(tree);
|
||||||
System.out.println("Before:\n" + tree);
|
|
||||||
|
|
||||||
final AST tree1 = tree2();
|
final AST tree1 = tree2();
|
||||||
ASTBalancer.flip(tree1);
|
ASTBalancer.flip(tree1);
|
||||||
ASTBalancer.leftPrecedence(tree1);
|
ASTBalancer.leftPrecedence(tree1);
|
||||||
|
|
||||||
ASTBalancer.operatorPrecedence(tree);
|
ASTBalancer.operatorPrecedence(tree);
|
||||||
System.out.println("After:\n" + tree);
|
|
||||||
|
|
||||||
assertThat(tree).isEqualTo(tree1);
|
assertThat(tree).isEqualTo(tree1);
|
||||||
}
|
}
|
||||||
@ -178,12 +164,10 @@ class ASTBalancerTest {
|
|||||||
final AST tree = tree3();
|
final AST tree = tree3();
|
||||||
ASTBalancer.flip(tree);
|
ASTBalancer.flip(tree);
|
||||||
ASTBalancer.leftPrecedence(tree);
|
ASTBalancer.leftPrecedence(tree);
|
||||||
System.out.println("Before:\n" + tree);
|
|
||||||
|
|
||||||
assertThat(tree.getRoot().getValue()).isEqualTo("MUL");
|
assertThat(tree.getRoot().getValue()).isEqualTo("MUL");
|
||||||
|
|
||||||
ASTBalancer.operatorPrecedence(tree);
|
ASTBalancer.operatorPrecedence(tree);
|
||||||
System.out.println("After:\n" + tree);
|
|
||||||
|
|
||||||
assertThat(tree.size()).isEqualTo(5);
|
assertThat(tree.size()).isEqualTo(5);
|
||||||
assertThat(tree.getRoot().getValue()).isEqualTo("SUB");
|
assertThat(tree.getRoot().getValue()).isEqualTo("SUB");
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package parser.ast;
|
|||||||
import lexer.StupsLexer;
|
import lexer.StupsLexer;
|
||||||
import org.antlr.v4.runtime.CharStreams;
|
import org.antlr.v4.runtime.CharStreams;
|
||||||
import org.antlr.v4.runtime.Lexer;
|
import org.antlr.v4.runtime.Lexer;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import parser.StupsParser;
|
import parser.StupsParser;
|
||||||
import parser.grammar.Grammar;
|
import parser.grammar.Grammar;
|
||||||
@ -18,11 +19,22 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||||||
|
|
||||||
class ASTCompacterTest {
|
class ASTCompacterTest {
|
||||||
|
|
||||||
private Lexer initLexer(String program) {
|
private static Grammar grammar;
|
||||||
|
private static StupsParser parser;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void init() throws IOException, URISyntaxException {
|
||||||
|
final Path path = Paths.get(ASTCompacterTest.class.getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||||
|
grammar = Grammar.fromFile(path);
|
||||||
|
parser = StupsParser.fromGrammar(grammar);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static AST getTree(String program) {
|
||||||
try {
|
try {
|
||||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("examplePrograms/" + program).toURI());
|
final Path path = Paths.get(ASTCompacterTest.class.getClassLoader().getResource("examplePrograms/" + program).toURI());
|
||||||
final String programCode = Files.readString(path, StandardCharsets.US_ASCII);
|
final String programCode = Files.readString(path, StandardCharsets.US_ASCII);
|
||||||
return new StupsLexer(CharStreams.fromString(programCode));
|
final Lexer lex = new StupsLexer(CharStreams.fromString(programCode));
|
||||||
|
return parser.parse(lex.getAllTokens(), lex.getVocabulary());
|
||||||
} catch (Exception ignore) {
|
} catch (Exception ignore) {
|
||||||
ignore.printStackTrace();
|
ignore.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -31,65 +43,39 @@ class ASTCompacterTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testDeleteChildren() throws URISyntaxException, IOException {
|
void testDeleteChildren() {
|
||||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
final AST tree = getTree("GeneralOperator.stups");
|
||||||
final Grammar grammar = Grammar.fromFile(path);
|
|
||||||
final StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
|
||||||
|
|
||||||
final Lexer lex = this.initLexer("GeneralOperator.stups");
|
|
||||||
final AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
|
||||||
|
|
||||||
final long before = tree.size();
|
final long before = tree.size();
|
||||||
|
|
||||||
ASTCompacter.deleteChildren(tree, grammar);
|
ASTCompacter.deleteChildren(tree, grammar);
|
||||||
|
|
||||||
assertThat(before - tree.size()).isEqualTo(3);
|
assertThat(before - tree.size()).isEqualTo(3);
|
||||||
System.out.println(tree);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testPromote() throws URISyntaxException, IOException {
|
void testPromote() {
|
||||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
final AST tree = getTree("GeneralOperator.stups");
|
||||||
final Grammar grammar = Grammar.fromFile(path);
|
|
||||||
final StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
|
||||||
|
|
||||||
final Lexer lex = this.initLexer("GeneralOperator.stups");
|
|
||||||
final AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
|
||||||
|
|
||||||
final long before = tree.size();
|
final long before = tree.size();
|
||||||
|
|
||||||
ASTCompacter.promote(tree, grammar);
|
ASTCompacter.promote(tree, grammar);
|
||||||
|
|
||||||
assertThat(before - tree.size()).isEqualTo(14);
|
assertThat(before - tree.size()).isEqualTo(14);
|
||||||
System.out.println(tree);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testDeleteEmpty() throws URISyntaxException, IOException {
|
void testDeleteEmpty() {
|
||||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
final AST tree = getTree("GeneralOperator.stups");
|
||||||
final Grammar grammar = Grammar.fromFile(path);
|
|
||||||
final StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
|
||||||
|
|
||||||
final Lexer lex = this.initLexer("GeneralOperator.stups");
|
|
||||||
final AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
|
||||||
ASTCompacter.deleteChildren(tree, grammar);
|
ASTCompacter.deleteChildren(tree, grammar);
|
||||||
|
|
||||||
final long before = tree.size();
|
final long before = tree.size();
|
||||||
|
|
||||||
ASTCompacter.deleteIfEmpty(tree, grammar);
|
ASTCompacter.deleteIfEmpty(tree, grammar);
|
||||||
|
|
||||||
assertThat(before - tree.size()).isEqualTo(2);
|
assertThat(before - tree.size()).isEqualTo(2);
|
||||||
System.out.println(tree);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testClean() throws URISyntaxException, IOException {
|
void testClean() {
|
||||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
final AST tree = getTree("GeneralOperator.stups");
|
||||||
final Grammar grammar = Grammar.fromFile(path);
|
|
||||||
final StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
|
||||||
|
|
||||||
final Lexer lex = this.initLexer("GeneralOperator.stups");
|
|
||||||
final AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
|
||||||
|
|
||||||
ASTCompacter.clean(tree, grammar);
|
ASTCompacter.clean(tree, grammar);
|
||||||
|
|
||||||
|
|||||||
@ -11,7 +11,6 @@ class ASTTest {
|
|||||||
final ASTNode root = new ASTNode("Wurzel", 1);
|
final ASTNode root = new ASTNode("Wurzel", 1);
|
||||||
|
|
||||||
final AST tree = new AST(root);
|
final AST tree = new AST(root);
|
||||||
System.out.println(tree);
|
|
||||||
|
|
||||||
assertThat(tree).hasToString("Wurzel\n");
|
assertThat(tree).hasToString("Wurzel\n");
|
||||||
}
|
}
|
||||||
@ -26,7 +25,6 @@ class ASTTest {
|
|||||||
root.addChild(childB);
|
root.addChild(childB);
|
||||||
|
|
||||||
final AST tree = new AST(root);
|
final AST tree = new AST(root);
|
||||||
System.out.println(tree);
|
|
||||||
|
|
||||||
assertThat(tree).hasToString("Wurzel\n├── A\n└── B\n");
|
assertThat(tree).hasToString("Wurzel\n├── A\n└── B\n");
|
||||||
}
|
}
|
||||||
@ -41,7 +39,6 @@ class ASTTest {
|
|||||||
childA.addChild(childB);
|
childA.addChild(childB);
|
||||||
|
|
||||||
final AST tree = new AST(root);
|
final AST tree = new AST(root);
|
||||||
System.out.println(tree);
|
|
||||||
|
|
||||||
assertThat(tree).hasToString("Wurzel\n└── A\n └── B\n");
|
assertThat(tree).hasToString("Wurzel\n└── A\n └── B\n");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,9 +11,9 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||||||
|
|
||||||
class GrammarTest {
|
class GrammarTest {
|
||||||
|
|
||||||
private Path getPath(String name) {
|
private static Path getPath(String name) {
|
||||||
try {
|
try {
|
||||||
return Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/" + name).toURI());
|
return Paths.get(GrammarTest.class.getClass().getClassLoader().getResource("exampleGrammars/" + name).toURI());
|
||||||
} catch (URISyntaxException e) {
|
} catch (URISyntaxException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -23,7 +23,7 @@ class GrammarTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testSimpleGrammar0() throws IOException {
|
void testSimpleGrammar0() throws IOException {
|
||||||
final Path path = this.getPath("SimpleGrammar0.grammar");
|
final Path path = getPath("SimpleGrammar0.grammar");
|
||||||
|
|
||||||
final Grammar grammar = Grammar.fromFile(path);
|
final Grammar grammar = Grammar.fromFile(path);
|
||||||
assert grammar != null;
|
assert grammar != null;
|
||||||
@ -39,13 +39,11 @@ class GrammarTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testSimpleGrammar1() throws IOException {
|
void testSimpleGrammar1() throws IOException {
|
||||||
final Path path = this.getPath("SimpleGrammar1.grammar");
|
final Path path = getPath("SimpleGrammar1.grammar");
|
||||||
|
|
||||||
final Grammar grammar = Grammar.fromFile(path);
|
final Grammar grammar = Grammar.fromFile(path);
|
||||||
assert grammar != null;
|
assert grammar != null;
|
||||||
|
|
||||||
System.out.println(grammar.getRules());
|
|
||||||
|
|
||||||
assertThat(grammar.getEpsilonSymbol()).isEqualTo("epsilon");
|
assertThat(grammar.getEpsilonSymbol()).isEqualTo("epsilon");
|
||||||
assertThat(grammar.getStartSymbol()).isEqualTo("E");
|
assertThat(grammar.getStartSymbol()).isEqualTo("E");
|
||||||
assertThat(grammar.getTerminals()).containsOnly("id", "+", "*", "(", ")");
|
assertThat(grammar.getTerminals()).containsOnly("id", "+", "*", "(", ")");
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package parser.typechecker;
|
|||||||
import lexer.StupsLexer;
|
import lexer.StupsLexer;
|
||||||
import org.antlr.v4.runtime.CharStreams;
|
import org.antlr.v4.runtime.CharStreams;
|
||||||
import org.antlr.v4.runtime.Lexer;
|
import org.antlr.v4.runtime.Lexer;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import parser.StupsParser;
|
import parser.StupsParser;
|
||||||
import parser.ast.AST;
|
import parser.ast.AST;
|
||||||
@ -22,11 +23,24 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
|||||||
|
|
||||||
class TypeTableTest {
|
class TypeTableTest {
|
||||||
|
|
||||||
private Lexer initLexer(String program) {
|
private static Grammar grammar;
|
||||||
|
private static StupsParser parser;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void init() throws IOException, URISyntaxException {
|
||||||
|
final Path path = Paths.get(TypeTableTest.class.getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||||
|
grammar = Grammar.fromFile(path);
|
||||||
|
parser = StupsParser.fromGrammar(grammar);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static AST getTree(String program) {
|
||||||
try {
|
try {
|
||||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("examplePrograms/" + program).toURI());
|
final Path path = Paths.get(TypeTableTest.class.getClassLoader().getResource("examplePrograms/" + program).toURI());
|
||||||
final String programCode = Files.readString(path, StandardCharsets.US_ASCII);
|
final String programCode = Files.readString(path, StandardCharsets.US_ASCII);
|
||||||
return new StupsLexer(CharStreams.fromString(programCode));
|
final Lexer lex = new StupsLexer(CharStreams.fromString(programCode));
|
||||||
|
final AST tree = parser.parse(lex.getAllTokens(), lex.getVocabulary());
|
||||||
|
tree.postprocess(grammar);
|
||||||
|
return tree;
|
||||||
} catch (Exception ignore) {
|
} catch (Exception ignore) {
|
||||||
ignore.printStackTrace();
|
ignore.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -35,14 +49,8 @@ class TypeTableTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testSingleSymbol() throws URISyntaxException, IOException {
|
void testSingleSymbol() {
|
||||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
final AST tree = getTree("SingleSymbol.stups");
|
||||||
final Grammar grammar = Grammar.fromFile(path);
|
|
||||||
final StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
|
||||||
|
|
||||||
final Lexer lex = this.initLexer("SingleSymbol.stups");
|
|
||||||
final AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
|
||||||
tree.postprocess(grammar);
|
|
||||||
|
|
||||||
final TypeTable table = TypeTable.fromAST(tree);
|
final TypeTable table = TypeTable.fromAST(tree);
|
||||||
|
|
||||||
@ -51,14 +59,8 @@ class TypeTableTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testMultipleSymbol() throws URISyntaxException, IOException {
|
void testMultipleSymbol() {
|
||||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
final AST tree = getTree("MultipleSymbol.stups");
|
||||||
final Grammar grammar = Grammar.fromFile(path);
|
|
||||||
final StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
|
||||||
|
|
||||||
final Lexer lex = this.initLexer("MultipleSymbol.stups");
|
|
||||||
final AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
|
||||||
tree.postprocess(grammar);
|
|
||||||
|
|
||||||
final TypeTable table = TypeTable.fromAST(tree);
|
final TypeTable table = TypeTable.fromAST(tree);
|
||||||
|
|
||||||
@ -72,14 +74,15 @@ class TypeTableTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testExistingSymbol() throws URISyntaxException, IOException {
|
void testExistingSymbol() {
|
||||||
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
final AST tree = getTree("ExistingSymbol.stups");
|
||||||
final Grammar grammar = Grammar.fromFile(path);
|
|
||||||
final StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
|
||||||
|
|
||||||
final Lexer lex = this.initLexer("ExistingSymbol.stups");
|
assertThatThrownBy(() -> TypeTable.fromAST(tree)).isInstanceOf(SymbolAlreadyDefinedException.class);
|
||||||
final AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
}
|
||||||
tree.postprocess(grammar);
|
|
||||||
|
@Test
|
||||||
|
void testExistingSymbol2() {
|
||||||
|
final AST tree = getTree("ExistingSymbol2.stups");
|
||||||
|
|
||||||
assertThatThrownBy(() -> TypeTable.fromAST(tree)).isInstanceOf(SymbolAlreadyDefinedException.class);
|
assertThatThrownBy(() -> TypeTable.fromAST(tree)).isInstanceOf(SymbolAlreadyDefinedException.class);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user