-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPLPScannerTest.java
More file actions
361 lines (341 loc) · 11.2 KB
/
PLPScannerTest.java
File metadata and controls
361 lines (341 loc) · 11.2 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/**
* JUunit tests for the Scanner
*/
package cop5556fa18;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import cop5556fa18.scanner.LexicalException;
import cop5556fa18.scanner.Token;
public class PLPScannerTest {
//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 boolean doPrint = true;
private void show(Object input) {
if (doPrint) {
System.out.println(input.toString());
}
}
/**
*Retrieves the next token and checks that it is an EOF token.
*Also checks that this was the last token.
*
* @param scanner
* @return the Token that was retrieved
*/
Token checkNextIsEOF(PLPScanner scanner) {
PLPScanner.Token token = scanner.nextToken();
assertEquals(PLPScanner.Kind.EOF, token.kind);
assertFalse(scanner.hasTokens());
return token;
}
/**
* Retrieves the next token and checks that its kind, position, length, line, and position in line
* match the given parameters.
*
* @param scanner
* @param kind
* @param pos
* @param length
* @param line
* @param pos_in_line
* @return the Token that was retrieved
*/
Token checkNext(PLPScanner scanner, PLPScanner.Kind kind, int pos, int length, int line, int pos_in_line) {
Token t = scanner.nextToken();
assertEquals(kind, t.kind);
assertEquals(pos, t.pos);
assertEquals(length, t.length);
assertEquals(line, t.line());
assertEquals(pos_in_line, t.posInLine());
return t;
}
/**
* Retrieves the next token and checks that its kind and length match the given
* parameters. The position, line, and position in line are ignored.
*
* @param scanner
* @param kind
* @param length
* @return the Token that was retrieved
*/
Token checkNext(PLPScanner scanner, PLPScanner.Kind kind, int length) {
Token t = scanner.nextToken();
assertEquals(kind, t.kind);
assertEquals(length, t.length);
return t;
}
/**
* Simple test case with an empty program. The only Token will be the EOF Token.
*
* @throws LexicalException
*/
@Test
public void testEmpty() throws LexicalException {
String input = ""; //The input is the empty string. This is legal
show(input); //Display the input
PLPScanner scanner = new PLPScanner(input).scan(); //Create a Scanner and initialize it
show(scanner); //Display the Scanner
checkNextIsEOF(scanner); //Check that the only token is the EOF token.
}
/**
* This example shows how to test that your scanner is behaving when the
* input is illegal. In this case, we are giving it an illegal character '~' in position 2
*
* The example shows catching the exception that is thrown by the scanner,
* looking at it, and checking its contents before rethrowing it. If caught
* but not rethrown, then JUnit won't get the exception and the test will fail.
*
* The test will work without putting the try-catch block around
* new Scanner(input).scan(); but then you won't be able to check
* or display the thrown exception.
*
* @throws LexicalException
*/
@Test
public void failIllegalChar() throws LexicalException {
String input = ";;~";
show(input);
thrown.expect(LexicalException.class); //Tell JUnit to expect a LexicalException
try {
new PLPScanner(input).scan();
} catch (LexicalException e) { //Catch the exception
show(e); //Display it
assertEquals(2,e.getPos()); //Check that it occurred in the expected position
throw e; //Rethrow exception so JUnit will see it
}
}
/**
* Using the two previous functions as a template. You can implement other JUnit test cases.
*
*/
/***************************Valid Cases************************/
@Test
public void vaildFloat() throws LexicalException {
String input = "float=-0.3145;"; //a valid float
show(input);
PLPScanner scanner = new PLPScanner(input).scan();
show(scanner);
checkNext(scanner, PLPScanner.Kind.KW_float,0,5,1,1);
}
@Test
public void vaildPrint() throws LexicalException {
String input = "print(\"abcd\"); print(score); print(B);"; //The input is the empty string. This is legal
show(input);
PLPScanner scanner = new PLPScanner(input).scan();
show(scanner);
checkNext(scanner, PLPScanner.Kind.KW_print,0,5,1,1);
checkNext(scanner, PLPScanner.Kind.LPAREN,5,1,1,6);
}
@Test
public void vailChar0() throws LexicalException {
String input = "\'\'"; // '' empty char
show(input);
PLPScanner scanner = new PLPScanner(input).scan();
show(scanner);
}
@Test
public void vailChar1() throws LexicalException {
String input = "\'a\'"; // 'a'
show(input);
PLPScanner scanner = new PLPScanner(input).scan();
show(scanner);
}
@Test
public void vaildIf() throws LexicalException {
String input = "if(a!=b){if(c==false){f='c';}a=x/0.1;};"; //valid if,char,float,div,assign,equal,not equal
show(input);
PLPScanner scanner = new PLPScanner(input).scan();
show(scanner);
}
@Test
public void vaildIdentifier() throws LexicalException {
String input = "_____a1_2_fc";//valid identifier
show(input);
PLPScanner scanner = new PLPScanner(input).scan();
show(scanner);
}
@Test
public void vaildBoolean() throws LexicalException {
String input = "boolean _adr13__ = true;";
show(input);
PLPScanner scanner = new PLPScanner(input).scan();
show(scanner);
}
@Test
public void vailAssign() throws LexicalException {
String input = "double d,a,f;";
show(input);
PLPScanner scanner = new PLPScanner(input).scan();
show(scanner);
}
@Test
public void vailExper() throws LexicalException {
String input = "t=(((4-2)*5.6)/3)+2; %{test valid comments}";
show(input);
PLPScanner scanner = new PLPScanner(input).scan();
show(scanner); //Display the Scanner
}
@Test
public void vailStr() throws LexicalException {
String input = " \"say \"Hi\" \" ";
show(input); //Display the input
PLPScanner scanner = new PLPScanner(input).scan();
show(scanner);
}
@Test
public void vailComm0() throws LexicalException {
String input = "%{test valid {{{{comments{}";
show(input); //Display the input
PLPScanner scanner = new PLPScanner(input).scan(); //Create a Scanner and initialize it
show(scanner); //Display the Scanner
}
@Test
public void validComm1() throws LexicalException {
String input = "%{This is a comment with %%%. \n New line. %}";
show(input); //Display the input
PLPScanner scanner = new PLPScanner(input).scan(); //Create a Scanner and initialize it
show(scanner); //Display the Scanner
}
@Test
public void validComm2() throws LexicalException {
String input = "%{This is a comment. \n New line. %}";
show(input); //Display the input
PLPScanner scanner = new PLPScanner(input).scan(); //Create a Scanner and initialize it
show(scanner); //Display the Scanner
}
/****************** Invalid Cases******************************/
@Test
public void failChar() throws LexicalException {
String input = "\'abcd\'";//invalid char
show(input);
thrown.expect(LexicalException.class); //Tell JUnit to expect a LexicalException
try {
new PLPScanner(input).scan();
} catch (LexicalException e) { //Catch the exception
show(e); //Display it
assertEquals(2,e.getPos()); //Check that it occurred in the expected position
throw e; //Rethrow exception so JUnit will see it
}
}
@Test
public void failIdentifier() throws LexicalException {
String input = "_1a_2_fc_";//invalid identifier
show(input);
thrown.expect(LexicalException.class); //Tell JUnit to expect a LexicalException
try {
new PLPScanner(input).scan();
} catch (LexicalException e) { //Catch the exception
show(e); //Display it
assertEquals(1,e.getPos()); //Check that it occurred in the expected position
throw e; //Rethrow exception so JUnit will see it
}
}
@Test
public void failInt() throws LexicalException {
String input = " 2147483648;";//test Int out range
show(input);
thrown.expect(LexicalException.class); //Tell JUnit to expect a LexicalException
try {
new PLPScanner(input).scan();
} catch (LexicalException e) { //Catch the exception
show(e); //Display it
assertEquals(11,e.getPos()); //Check that it occurred in the expected position
throw e; //Rethrow exception so JUnit will see it
}
}
@Test
public void failComm0() throws LexicalException {
String input = "%{affeafe%{}";//invalid comments: '%' followed by '{'
show(input);
thrown.expect(LexicalException.class);
try {
new PLPScanner(input).scan();
} catch (LexicalException e) {
show(e);
assertEquals(10,e.getPos());
throw e;
}
}
@Test
public void failComm1() throws LexicalException {
String input = "%{fafeagegag";//invalid comment: no end
show(input);
thrown.expect(LexicalException.class);
try {
new PLPScanner(input).scan();
} catch (LexicalException e) {
show(e);
assertEquals(12,e.getPos());
throw e;
}
}
@Test
public void failStr() throws LexicalException {
String input = "\"affeafe";//invalid str, lost "
show(input);
thrown.expect(LexicalException.class);
try {
new PLPScanner(input).scan();
} catch (LexicalException e) {
show(e);
assertEquals(8,e.getPos());
throw e;
}
}
@Test
public void failChar2() throws LexicalException {
String input = "\'a";//invalid char: lost '
show(input);
thrown.expect(LexicalException.class);
try {
new PLPScanner(input).scan();
} catch (LexicalException e) {
show(e);
assertEquals(2,e.getPos());
throw e;
}
}
@Test
public void failFloat0() throws LexicalException {
String input = "-340282366920938463463374607431768211458.9999;";//float out of range
show(input);
thrown.expect(LexicalException.class);
try {
new PLPScanner(input).scan();
} catch (LexicalException e) {
show(e);
assertEquals(45,e.getPos());
throw e;
}
@Test
public void failFloat1() throws LexicalException {
String input = "0.";//invalid float
show(input);
thrown.expect(LexicalException.class);
try {
new PLPScanner(input).scan();
} catch (LexicalException e) {
show(e);
assertEquals(2,e.getPos());
throw e;
}
}
@Test
public void failFloat2() throws LexicalException {
String input = "00.";//invalid float: '.' must follow by digit
show(input);
thrown.expect(LexicalException.class);
try {
new PLPScanner(input).scan();
} catch (LexicalException e) {
show(e);
assertEquals(3,e.getPos());
throw e;
}
}
}