/
S006SimpleBooleanTest.java
110 lines (88 loc) · 3.91 KB
/
S006SimpleBooleanTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package org.meri.antlr_step_by_step.parsers;
import static org.junit.Assert.assertEquals;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.Tree;
import org.junit.Test;
import org.meri.antlr_step_by_step.parsers.S006SimpleBooleanCompiler.S006Error;
public class S006SimpleBooleanTest {
@Test
public void testExampleTree() {
//compile the expression
S006SimpleBooleanCompiler compiler = new S006SimpleBooleanCompiler();
CommonTree ast = compiler.compile("bwahaha && (winner || !looser)");
//check AST structure
assertEquals(S006SimpleBooleanParser.AND, ast.getType());
assertEquals(S006SimpleBooleanParser.NAME, ast.getChild(0).getType());
assertEquals("bwahaha", ast.getChild(0).getText());
Tree orSubTree = ast.getChild(1);
assertEquals(S006SimpleBooleanParser.OR, orSubTree.getType());
assertEquals(S006SimpleBooleanParser.NAME, orSubTree.getChild(0).getType());
assertEquals("winner", orSubTree.getChild(0).getText());
Tree notSubTree = orSubTree.getChild(1);
assertEquals(S006SimpleBooleanParser.NOT, notSubTree.getType());
assertEquals(S006SimpleBooleanParser.NAME, notSubTree.getChild(0).getType());
assertEquals("looser", notSubTree.getChild(0).getText());
}
@Test
public void testAnd() {
//compile the expression
S006SimpleBooleanCompiler compiler = new S006SimpleBooleanCompiler();
CommonTree ast = compiler.compile("aaa && bbb");
//check AST structure
assertEquals(S006SimpleBooleanParser.AND, ast.getType());
assertEquals(S006SimpleBooleanParser.NAME, ast.getChild(0).getType());
assertEquals(S006SimpleBooleanParser.NAME, ast.getChild(1).getType());
}
@Test
public void testOr() {
//compile the expression
S006SimpleBooleanCompiler compiler = new S006SimpleBooleanCompiler();
CommonTree ast = compiler.compile("aaa || bbb");
//check AST structure
assertEquals(S006SimpleBooleanParser.OR, ast.getType());
assertEquals(S006SimpleBooleanParser.NAME, ast.getChild(0).getType());
assertEquals(S006SimpleBooleanParser.NAME, ast.getChild(1).getType());
}
@Test
public void testNot() {
//compile the expression
S006SimpleBooleanCompiler compiler = new S006SimpleBooleanCompiler();
CommonTree ast = compiler.compile("!aaa");
//check AST structure
assertEquals(S006SimpleBooleanParser.NOT, ast.getType());
assertEquals(S006SimpleBooleanParser.NAME, ast.getChild(0).getType());
}
@Test
public void testComposedExpression() {
//compile the expression
S006SimpleBooleanCompiler compiler = new S006SimpleBooleanCompiler();
CommonTree ast = compiler.compile("aaa && bbb || !(aaa || ccc)");
//check AST structure
assertEquals(S006SimpleBooleanParser.AND, ast.getType());
assertEquals(S006SimpleBooleanParser.NAME, ast.getChild(0).getType());
Tree rigthSubTree = ast.getChild(1);
assertEquals(S006SimpleBooleanParser.OR, rigthSubTree.getType());
assertEquals(S006SimpleBooleanParser.NAME, rigthSubTree.getChild(0).getType());
Tree rigthSubSubTree = rigthSubTree.getChild(1);
assertEquals(S006SimpleBooleanParser.NOT, rigthSubSubTree.getType());
Tree rigthSubSubSubTree = rigthSubSubTree.getChild(0);
assertEquals(S006SimpleBooleanParser.OR, rigthSubSubSubTree.getType());
assertEquals(S006SimpleBooleanParser.NAME, rigthSubSubSubTree.getChild(0).getType());
assertEquals(S006SimpleBooleanParser.NAME, rigthSubSubSubTree.getChild(1).getType());
}
@Test
public void testAnotherCorrectExpression() {
//compile the expression
S006SimpleBooleanCompiler compiler = new S006SimpleBooleanCompiler();
CommonTree ast = compiler.compile("!(bwahaha)");
//check AST structure
assertEquals(S006SimpleBooleanParser.NOT, ast.getType());
assertEquals(S006SimpleBooleanParser.NAME, ast.getChild(0).getType());
}
@Test(expected=S006Error.class)
public void testCompletelyWrong() {
//compile the expression
S006SimpleBooleanCompiler compiler = new S006SimpleBooleanCompiler();
compiler.compile("Incorrect Expression");
}
}