refactor tests

This commit is contained in:
ChUrl
2020-12-15 17:53:02 +01:00
parent 0e7364cf1d
commit 5390856fd8
7 changed files with 113 additions and 198 deletions

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

View File

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

View File

@ -83,10 +83,8 @@ class ASTBalancerTest {
@Test
void testTree1Flip() {
final AST tree = tree1();
System.out.println("Before:\n" + 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(1).getName()).isEqualTo("expr");
@ -95,11 +93,9 @@ class ASTBalancerTest {
@Test
void testTree1Flip2x() {
final AST tree = tree1();
System.out.println("Before:\n" + tree);
ASTBalancer.flip(tree);
ASTBalancer.flip(tree);
System.out.println("After:\n" + tree);
assertThat(tree).isEqualTo(tree1());
}
@ -107,10 +103,8 @@ class ASTBalancerTest {
@Test
void testTree2Flip() {
final AST tree = tree2();
System.out.println("Before:\n" + 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(1).getName()).isEqualTo("expr");
@ -121,11 +115,9 @@ class ASTBalancerTest {
@Test
void testTree2Flip2x() {
final AST tree = tree2();
System.out.println("Before:\n" + tree);
ASTBalancer.flip(tree);
ASTBalancer.flip(tree);
System.out.println("After:\n" + tree);
assertThat(tree).isEqualTo(tree2());
}
@ -134,10 +126,8 @@ class ASTBalancerTest {
void testTree1LeftPrecedence() {
final AST tree = tree1();
ASTBalancer.flip(tree);
System.out.println("Before:\n" + tree);
ASTBalancer.leftPrecedence(tree);
System.out.println("After:\n" + tree);
assertThat(tree.size()).isEqualTo(3);
assertThat(tree.getRoot().getValue()).isEqualTo("SUB");
@ -147,10 +137,8 @@ class ASTBalancerTest {
void testTree2LeftPrecedence() {
final AST tree = tree2();
ASTBalancer.flip(tree);
System.out.println("Before:\n" + tree);
ASTBalancer.leftPrecedence(tree);
System.out.println("After:\n" + tree);
assertThat(tree.size()).isEqualTo(5);
assertThat(tree.getRoot().getValue()).isEqualTo("SUB");
@ -161,14 +149,12 @@ class ASTBalancerTest {
final AST tree = tree2();
ASTBalancer.flip(tree);
ASTBalancer.leftPrecedence(tree);
System.out.println("Before:\n" + tree);
final AST tree1 = tree2();
ASTBalancer.flip(tree1);
ASTBalancer.leftPrecedence(tree1);
ASTBalancer.operatorPrecedence(tree);
System.out.println("After:\n" + tree);
assertThat(tree).isEqualTo(tree1);
}
@ -178,12 +164,10 @@ class ASTBalancerTest {
final AST tree = tree3();
ASTBalancer.flip(tree);
ASTBalancer.leftPrecedence(tree);
System.out.println("Before:\n" + tree);
assertThat(tree.getRoot().getValue()).isEqualTo("MUL");
ASTBalancer.operatorPrecedence(tree);
System.out.println("After:\n" + tree);
assertThat(tree.size()).isEqualTo(5);
assertThat(tree.getRoot().getValue()).isEqualTo("SUB");

View File

@ -3,6 +3,7 @@ package parser.ast;
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.api.Test;
import parser.StupsParser;
import parser.grammar.Grammar;
@ -18,11 +19,22 @@ import static org.assertj.core.api.Assertions.assertThat;
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 {
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);
return new StupsLexer(CharStreams.fromString(programCode));
final Lexer lex = new StupsLexer(CharStreams.fromString(programCode));
return parser.parse(lex.getAllTokens(), lex.getVocabulary());
} catch (Exception ignore) {
ignore.printStackTrace();
}
@ -31,65 +43,39 @@ class ASTCompacterTest {
}
@Test
void testDeleteChildren() throws URISyntaxException, IOException {
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
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());
void testDeleteChildren() {
final AST tree = getTree("GeneralOperator.stups");
final long before = tree.size();
ASTCompacter.deleteChildren(tree, grammar);
assertThat(before - tree.size()).isEqualTo(3);
System.out.println(tree);
}
@Test
void testPromote() throws URISyntaxException, IOException {
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
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());
void testPromote() {
final AST tree = getTree("GeneralOperator.stups");
final long before = tree.size();
ASTCompacter.promote(tree, grammar);
assertThat(before - tree.size()).isEqualTo(14);
System.out.println(tree);
}
@Test
void testDeleteEmpty() throws URISyntaxException, IOException {
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
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());
void testDeleteEmpty() {
final AST tree = getTree("GeneralOperator.stups");
ASTCompacter.deleteChildren(tree, grammar);
final long before = tree.size();
ASTCompacter.deleteIfEmpty(tree, grammar);
assertThat(before - tree.size()).isEqualTo(2);
System.out.println(tree);
}
@Test
void testClean() throws URISyntaxException, IOException {
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
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());
void testClean() {
final AST tree = getTree("GeneralOperator.stups");
ASTCompacter.clean(tree, grammar);

View File

@ -11,7 +11,6 @@ class ASTTest {
final ASTNode root = new ASTNode("Wurzel", 1);
final AST tree = new AST(root);
System.out.println(tree);
assertThat(tree).hasToString("Wurzel\n");
}
@ -26,7 +25,6 @@ class ASTTest {
root.addChild(childB);
final AST tree = new AST(root);
System.out.println(tree);
assertThat(tree).hasToString("Wurzel\n├── A\n└── B\n");
}
@ -41,7 +39,6 @@ class ASTTest {
childA.addChild(childB);
final AST tree = new AST(root);
System.out.println(tree);
assertThat(tree).hasToString("Wurzel\n└── A\n └── B\n");
}

View File

@ -11,9 +11,9 @@ import static org.assertj.core.api.Assertions.assertThat;
class GrammarTest {
private Path getPath(String name) {
private static Path getPath(String name) {
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) {
e.printStackTrace();
}
@ -23,7 +23,7 @@ class GrammarTest {
@Test
void testSimpleGrammar0() throws IOException {
final Path path = this.getPath("SimpleGrammar0.grammar");
final Path path = getPath("SimpleGrammar0.grammar");
final Grammar grammar = Grammar.fromFile(path);
assert grammar != null;
@ -39,13 +39,11 @@ class GrammarTest {
@Test
void testSimpleGrammar1() throws IOException {
final Path path = this.getPath("SimpleGrammar1.grammar");
final Path path = getPath("SimpleGrammar1.grammar");
final Grammar grammar = Grammar.fromFile(path);
assert grammar != null;
System.out.println(grammar.getRules());
assertThat(grammar.getEpsilonSymbol()).isEqualTo("epsilon");
assertThat(grammar.getStartSymbol()).isEqualTo("E");
assertThat(grammar.getTerminals()).containsOnly("id", "+", "*", "(", ")");

View File

@ -3,6 +3,7 @@ package parser.typechecker;
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.api.Test;
import parser.StupsParser;
import parser.ast.AST;
@ -22,11 +23,24 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
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 {
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);
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) {
ignore.printStackTrace();
}
@ -35,14 +49,8 @@ class TypeTableTest {
}
@Test
void testSingleSymbol() throws URISyntaxException, IOException {
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
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);
void testSingleSymbol() {
final AST tree = getTree("SingleSymbol.stups");
final TypeTable table = TypeTable.fromAST(tree);
@ -51,14 +59,8 @@ class TypeTableTest {
}
@Test
void testMultipleSymbol() throws URISyntaxException, IOException {
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
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);
void testMultipleSymbol() {
final AST tree = getTree("MultipleSymbol.stups");
final TypeTable table = TypeTable.fromAST(tree);
@ -72,14 +74,15 @@ class TypeTableTest {
}
@Test
void testExistingSymbol() throws URISyntaxException, IOException {
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
final Grammar grammar = Grammar.fromFile(path);
final StupsParser stupsParser = StupsParser.fromGrammar(grammar);
void testExistingSymbol() {
final AST tree = getTree("ExistingSymbol.stups");
final Lexer lex = this.initLexer("ExistingSymbol.stups");
final AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
tree.postprocess(grammar);
assertThatThrownBy(() -> TypeTable.fromAST(tree)).isInstanceOf(SymbolAlreadyDefinedException.class);
}
@Test
void testExistingSymbol2() {
final AST tree = getTree("ExistingSymbol2.stups");
assertThatThrownBy(() -> TypeTable.fromAST(tree)).isInstanceOf(SymbolAlreadyDefinedException.class);
}