-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPLPParserTest.java
More file actions
215 lines (201 loc) · 5.82 KB
/
PLPParserTest.java
File metadata and controls
215 lines (201 loc) · 5.82 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package cop5556fa18;
import static org.junit.Assert.assertEquals;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import cop5556fa18.PLPScanner;
import cop5556fa18.PLPParser;
import cop5556fa18.PLPScanner.LexicalException;
import cop5556fa18.PLPParser.SyntaxException;
public class PLPParserTest {
//set Junit to be able to catch exceptions
@Rule
public ExpectedException thrown = ExpectedException.none();
//To make it easy to print objects and turn this output on and off
static final boolean doPrint = true;
private void show(Object input) {
if (doPrint) {
System.out.println(input.toString());
}
}
//creates and returns a parser for the given input.
private PLPParser makeParser(String input) throws LexicalException {
show(input);
PLPScanner scanner = new PLPScanner(input).scan();
show(scanner);
PLPParser parser = new PLPParser(scanner);
return parser;
}
/**
* An empty program. This throws an exception because it lacks an identifier and a block.
* The test case passes because the unit test expects an exception.
*
* @throws LexicalException
* @throws SyntaxException
*/
@Test
public void testEmpty() throws LexicalException, SyntaxException {
String input = "";
PLPParser parser = makeParser(input);
thrown.expect(SyntaxException.class);
parser.parse();
}
/**
* Smallest legal program.
*
* @throws LexicalException
* @throws SyntaxException
*/
@Test
public void testSmallest() throws LexicalException, SyntaxException {
String input = "b{}";
PLPParser parser = makeParser(input);
parser.parse();
}
//This test will fail in the starter code. However, it should pass in a complete parser.
@Test
public void testDec0() throws LexicalException, SyntaxException {
String input = "b{int c;}";
PLPParser parser = makeParser(input);
parser.parse();
}
@Test
public void testDec1() throws LexicalException, SyntaxException {
String input = "b{int c,d,f,g,h;}";
PLPParser parser = makeParser(input);
parser.parse();
}
@Test
public void testFun0() throws LexicalException, SyntaxException {
String input = "bbb{f=sin(aaa)+x;}";
PLPParser parser = makeParser(input);
parser.parse();
}
@Test
public void testFun1() throws LexicalException, SyntaxException {
String input = "bbvvv{f=log(x+a-c**2)*abs(-x);}";
PLPParser parser = makeParser(input);
parser.parse();
}
@Test
public void testPrint0() throws LexicalException, SyntaxException {
String input = "b{print (aaa);}";
PLPParser parser = makeParser(input);
parser.parse();
}
@Test
public void testPrint1() throws LexicalException, SyntaxException {
String input = "b{print 1+1;}";
PLPParser parser = makeParser(input);
parser.parse();
}
@Test
public void testIf0() throws LexicalException, SyntaxException {
String input = "b{if(a==b){print (aaa);};}";
PLPParser parser = makeParser(input);
parser.parse();
}
@Test
public void testExp0() throws LexicalException, SyntaxException {
String input = "b{a=b==c;}";
PLPParser parser = makeParser(input);
parser.parse();
}
@Test
public void testExp1() throws LexicalException, SyntaxException {
String input = "b{a=b|c&d;}";
PLPParser parser = makeParser(input);
parser.parse();
}
@Test
public void testExp2() throws LexicalException, SyntaxException {
String input = "b{a= 2*3.1%c-(h+j)**3/4;}";
PLPParser parser = makeParser(input);
parser.parse();
}
/* Invalid Test */
@Test
public void testInvalidExp() throws LexicalException, SyntaxException {
String input = "b{a=;}";
PLPParser parser = makeParser(input);
thrown.expect(SyntaxException.class);
try {
parser.parse();
} catch (SyntaxException e) {
show(e);
throw e;
}
}
@Test
public void testInvalidDeclar() throws LexicalException, SyntaxException {
String input = "b{int a,b,}";
PLPParser parser = makeParser(input);
thrown.expect(SyntaxException.class);
try {
parser.parse();
} catch (SyntaxException e) {
show(e);
throw e;
}
}
@Test
public void testInvalidStat() throws LexicalException, SyntaxException {
String input = "b{if(a==b)}";
PLPParser parser = makeParser(input);
thrown.expect(SyntaxException.class);
try {
parser.parse();
} catch (SyntaxException e) {
show(e);
throw e;
}
}
@Test
public void testInvalidStat1() throws LexicalException, SyntaxException {
String input = "b{while(a==b)}";
PLPParser parser = makeParser(input);
thrown.expect(SyntaxException.class);
try {
parser.parse();
} catch (SyntaxException e) {
show(e);
throw e;
}
}
@Test
public void testInvalidStat2() throws LexicalException, SyntaxException {
String input = "b{sleep(a}";
PLPParser parser = makeParser(input);
thrown.expect(SyntaxException.class);
try {
parser.parse();
} catch (SyntaxException e) {
show(e);
throw e;
}
}
@Test
public void testInvalidStat3() throws LexicalException, SyntaxException {
String input = "b{*f=1;}";
PLPParser parser = makeParser(input);
thrown.expect(SyntaxException.class);
try {
parser.parse();
} catch (SyntaxException e) {
show(e);
throw e;
}
}
@Test
public void testInvalidFun0() throws LexicalException, SyntaxException {
String input = "bbb{f=sin()+x;}";
PLPParser parser = makeParser(input);
thrown.expect(SyntaxException.class);
try {
parser.parse();
} catch (SyntaxException e) {
show(e);
throw e;
}
}
}