|
1 "{ Package: 'stx:goodies/petitparser/parsers/java' }" |
|
2 |
|
3 "{ NameSpace: Smalltalk }" |
|
4 |
|
5 PPJavaLexiconTest subclass:#PPJavaSyntaxTest |
|
6 instanceVariableNames:'' |
|
7 classVariableNames:'' |
|
8 poolDictionaries:'' |
|
9 category:'PetitJava-Tests' |
|
10 ! |
|
11 |
|
12 PPJavaSyntaxTest comment:'' |
|
13 ! |
|
14 |
|
15 !PPJavaSyntaxTest methodsFor:'accessing'! |
|
16 |
|
17 parserClass |
|
18 ^ PPJavaSyntax |
|
19 ! ! |
|
20 |
|
21 !PPJavaSyntaxTest methodsFor:'testing'! |
|
22 |
|
23 testAdditiveExpression1 |
|
24 |
|
25 self parse: 'true' |
|
26 rule: #additiveExpression |
|
27 ! |
|
28 |
|
29 testAdditiveExpression2 |
|
30 |
|
31 self parse: '3 + 5' |
|
32 rule: #additiveExpression |
|
33 ! |
|
34 |
|
35 testAdditiveExpression3 |
|
36 |
|
37 self parse: '4 + 8 - 2 + 9' |
|
38 rule: #additiveExpression |
|
39 ! |
|
40 |
|
41 testAndExpression1 |
|
42 |
|
43 self parse: 'true' |
|
44 rule: #andExpression |
|
45 ! |
|
46 |
|
47 testBlock1 |
|
48 |
|
49 self parse: '{ }' |
|
50 rule: #block |
|
51 ! |
|
52 |
|
53 testBlock2 |
|
54 |
|
55 self |
|
56 parse: |
|
57 '{ |
|
58 System.out.println("Hello World!!"); |
|
59 System.out.println("Hello World!!"); |
|
60 }' |
|
61 rule: #block |
|
62 ! |
|
63 |
|
64 testCompilationUnit1 |
|
65 |
|
66 self parse: 'package foo; |
|
67 public class CU1 {}' |
|
68 rule: #compilationUnit |
|
69 ! |
|
70 |
|
71 testCompilationUnit2 |
|
72 |
|
73 self parse: 'package foo; |
|
74 import foo.Bar; |
|
75 public class CU2 { |
|
76 }' |
|
77 rule: #compilationUnit |
|
78 ! |
|
79 |
|
80 testCompilationUnit3 |
|
81 |
|
82 self parse: 'class myfirstjavaprog |
|
83 { |
|
84 public static void main(String args[]) |
|
85 { |
|
86 System.out.println("Hello World!!"); |
|
87 } |
|
88 }' |
|
89 rule: #compilationUnit |
|
90 ! |
|
91 |
|
92 testCompilationUnit4 |
|
93 |
|
94 self parse: ' |
|
95 public class OddEven { |
|
96 private int input; |
|
97 public static void main(String[] args) { |
|
98 OddEven number = new OddEven(); |
|
99 number.showDialog(); } |
|
100 public void showDialog() { |
|
101 |
|
102 try { |
|
103 |
|
104 input = Integer.parseInt(JOptionPane.showInputDialog("Please Enter A Number")); |
|
105 calculate(); |
|
106 } catch (NumberFormatException e) { |
|
107 |
|
108 System.err.println("ERROR: Invalid input. Please type in a numerical value."); |
|
109 } |
|
110 } |
|
111 |
|
112 private void calculate() { |
|
113 if (input % 2 == 0) { |
|
114 System.out.println("Even"); |
|
115 } else { |
|
116 System.out.println("Odd"); |
|
117 } |
|
118 } |
|
119 }' |
|
120 rule: #compilationUnit |
|
121 ! |
|
122 |
|
123 testCompilationUnit5 |
|
124 |
|
125 self parse: 'class myfirstjavaprog |
|
126 { |
|
127 |
|
128 public myfirstjavaprog() { |
|
129 |
|
130 } |
|
131 |
|
132 public static void main(String args[]) |
|
133 { |
|
134 System.out.println("Hello World!!"); |
|
135 } |
|
136 }' |
|
137 rule: #compilationUnit |
|
138 ! |
|
139 |
|
140 testConditionalAndExpression1 |
|
141 |
|
142 self parse: 'true' |
|
143 rule: #conditionalAndExpression |
|
144 ! |
|
145 |
|
146 testConditionalOrExpression1 |
|
147 |
|
148 self parse: 'true' |
|
149 rule: #conditionalOrExpression |
|
150 ! |
|
151 |
|
152 testEqualityExpression1 |
|
153 |
|
154 self |
|
155 parse: 'true' |
|
156 rule: #equalityExpression |
|
157 ! |
|
158 |
|
159 testExclusiveOrExpression1 |
|
160 |
|
161 self parse: 'true' |
|
162 rule: #exclusiveOrExpression |
|
163 ! |
|
164 |
|
165 testExpression1 |
|
166 |
|
167 self parse: '1 + 2' |
|
168 rule: #expression |
|
169 ! |
|
170 |
|
171 testExpression2 |
|
172 |
|
173 self parse: 'true' |
|
174 rule: #expression |
|
175 ! |
|
176 |
|
177 testExpression3 |
|
178 |
|
179 self parse: 'a.method()' |
|
180 rule: #expression |
|
181 ! |
|
182 |
|
183 testExpression4 |
|
184 |
|
185 self parse: 'a' |
|
186 rule: #expression |
|
187 ! |
|
188 |
|
189 testExpression5 |
|
190 |
|
191 self parse: 'a += 5' |
|
192 rule: #expression |
|
193 ! |
|
194 |
|
195 testFormalParameters1 |
|
196 |
|
197 self |
|
198 parse: ' |
|
199 (String s, Object parameterType) |
|
200 ' |
|
201 rule: #formalParameters |
|
202 ! |
|
203 |
|
204 testFormalParameters2 |
|
205 |
|
206 self |
|
207 parse: ' |
|
208 (Object ... parameterType) |
|
209 ' |
|
210 rule: #formalParameters |
|
211 ! |
|
212 |
|
213 testFormalParameters3 |
|
214 |
|
215 self |
|
216 parse: '(String name, Class<?>... parameterTypes) |
|
217 ' |
|
218 rule: #formalParameters |
|
219 ! |
|
220 |
|
221 testFormalParameters4 |
|
222 |
|
223 self |
|
224 parse: '(int one, int two, int three, int four)' |
|
225 rule: #formalParameters |
|
226 ! |
|
227 |
|
228 testFormalParameters5 |
|
229 |
|
230 self |
|
231 parse: '()' |
|
232 rule: #formalParameters |
|
233 ! |
|
234 |
|
235 testIfStatement1 |
|
236 |
|
237 self parse: 'if (true) {}' |
|
238 rule: #ifStatement |
|
239 ! |
|
240 |
|
241 testIfStatement2 |
|
242 |
|
243 self parse: 'if (true) {} else {}' |
|
244 rule: #ifStatement |
|
245 ! |
|
246 |
|
247 testIfStatement3 |
|
248 |
|
249 self parse: ' |
|
250 if (true) |
|
251 {int a = 0; a = 1;} |
|
252 else |
|
253 {return false;}' |
|
254 rule: #ifStatement |
|
255 ! |
|
256 |
|
257 testIfStatement4 |
|
258 |
|
259 self parse: 'if (true) a = 0;' |
|
260 rule: #ifStatement |
|
261 ! |
|
262 |
|
263 testIfStatement5 |
|
264 |
|
265 self parse: 'if (a < 4) {}' |
|
266 rule: #ifStatement |
|
267 ! |
|
268 |
|
269 testImportDeclaration1 |
|
270 |
|
271 self parse: 'import foo.Top;' |
|
272 rule: #importDeclaration |
|
273 ! |
|
274 |
|
275 testImportDeclaration2 |
|
276 |
|
277 self parse: 'import foo.Top2.*;' |
|
278 rule: #importDeclaration |
|
279 ! |
|
280 |
|
281 testInclusiveOrExpression1 |
|
282 |
|
283 self parse: 'true' |
|
284 rule: #inclusiveOrExpression |
|
285 ! |
|
286 |
|
287 testInstanceofExpression1 |
|
288 |
|
289 self |
|
290 parse: 'true' |
|
291 rule: #instanceofExpression |
|
292 ! |
|
293 |
|
294 testLocalVariableDeclaration1 |
|
295 |
|
296 self parse: 'Type name' |
|
297 rule: #localVariableDeclaration |
|
298 ! |
|
299 |
|
300 testLocalVariableDeclaration2 |
|
301 |
|
302 self parse: 'Type name = value, name2' |
|
303 rule: #localVariableDeclaration |
|
304 ! |
|
305 |
|
306 testLocalVariableDeclarationStatement1 |
|
307 |
|
308 self parse: 'int i = 5;' |
|
309 rule: #localVariableDeclarationStatement |
|
310 ! |
|
311 |
|
312 testLocalVariableDeclarationStatement2 |
|
313 |
|
314 self parse: 'int i, j = 6;' |
|
315 rule: #localVariableDeclarationStatement |
|
316 ! |
|
317 |
|
318 testLocalVariableDeclarationStatement3 |
|
319 |
|
320 self parse: 'Srting str, in, g;' |
|
321 rule: #localVariableDeclarationStatement |
|
322 ! |
|
323 |
|
324 testMethodDeclaration3 |
|
325 |
|
326 self |
|
327 parse: ' |
|
328 public void getMethod(String s, Object ... parameterType) |
|
329 { |
|
330 } |
|
331 ' |
|
332 rule: #methodDeclaration |
|
333 ! |
|
334 |
|
335 testMethodDeclaration4 |
|
336 |
|
337 self |
|
338 parse: ' |
|
339 public void getMethod(String s, int o) |
|
340 { |
|
341 } |
|
342 ' |
|
343 rule: #methodDeclaration |
|
344 ! |
|
345 |
|
346 testMultiplicativeExpression1 |
|
347 |
|
348 self parse: 'true' |
|
349 rule: #multiplicativeExpression |
|
350 ! |
|
351 |
|
352 testMultiplicativeExpression2 |
|
353 |
|
354 self parse: '3 * 5' |
|
355 rule: #multiplicativeExpression |
|
356 ! |
|
357 |
|
358 testMultiplicativeExpression3 |
|
359 |
|
360 self parse: '4 * 8 / 2 * 9' |
|
361 rule: #multiplicativeExpression |
|
362 ! |
|
363 |
|
364 testNormalParameterDecl1 |
|
365 |
|
366 self parse: 'final int integers[]' |
|
367 rule: #normalParameterDecl |
|
368 ! |
|
369 |
|
370 testNormalParameterDecl2 |
|
371 |
|
372 self parse: 'String str' |
|
373 rule: #normalParameterDecl |
|
374 ! |
|
375 |
|
376 testPackageDeclaration1 |
|
377 |
|
378 self parse: 'package foo;' |
|
379 rule: #packageDeclaration |
|
380 ! |
|
381 |
|
382 testPackageDeclaration2 |
|
383 |
|
384 self parse: 'package foo.Bar;' |
|
385 rule: #packageDeclaration |
|
386 ! |
|
387 |
|
388 testPackageDeclaration3 |
|
389 |
|
390 self fail: 'package ;' |
|
391 rule: #packageDeclaration |
|
392 ! |
|
393 |
|
394 testPrimary1 |
|
395 |
|
396 self |
|
397 parse: 'true' |
|
398 rule: #primary |
|
399 ! |
|
400 |
|
401 testPrimary2 |
|
402 |
|
403 self |
|
404 parse: '"Java string"' |
|
405 rule: #primary |
|
406 ! |
|
407 |
|
408 testPrimaryWithselectors1 |
|
409 |
|
410 self |
|
411 parse: 'true' |
|
412 rule: #primaryWithselectors |
|
413 ! |
|
414 |
|
415 testPrimaryWithselectors2 |
|
416 |
|
417 self |
|
418 parse: 'foo().bar()' |
|
419 rule: #primaryWithselectors |
|
420 ! |
|
421 |
|
422 testQualifiedName1 |
|
423 |
|
424 self parse: 'a.a' |
|
425 rule: #qualifiedName |
|
426 ! |
|
427 |
|
428 testRelationalExpression1 |
|
429 |
|
430 self parse: 'true' |
|
431 rule: #relationalExpression |
|
432 ! |
|
433 |
|
434 testRelationalExpression2 |
|
435 |
|
436 self parse: '3 > 5' |
|
437 rule: #relationalExpression |
|
438 ! |
|
439 |
|
440 testReturnStatement1 |
|
441 |
|
442 self parse: 'return true;' |
|
443 rule: #returnStatement |
|
444 ! |
|
445 |
|
446 testReturnStatement2 |
|
447 |
|
448 self parse: 'return;' |
|
449 rule: #returnStatement |
|
450 ! |
|
451 |
|
452 testShiftExpression1 |
|
453 |
|
454 self parse: 'true' |
|
455 rule: #shiftExpression |
|
456 ! |
|
457 |
|
458 testStatement1 |
|
459 |
|
460 self parse: 'System.out.println("Hello World!!");' |
|
461 rule: #statement |
|
462 ! |
|
463 |
|
464 testStatement2 |
|
465 |
|
466 self parse: 'return true;' |
|
467 rule: #statement |
|
468 ! |
|
469 |
|
470 testStatement3 |
|
471 |
|
472 self parse: '{}' |
|
473 rule: #statement |
|
474 ! |
|
475 |
|
476 testUnaryExpression1 |
|
477 |
|
478 self parse: 'a' |
|
479 rule: #unaryExpression |
|
480 ! |
|
481 |
|
482 testUnaryExpression2 |
|
483 |
|
484 self parse: 'true' |
|
485 rule: #unaryExpression |
|
486 ! |
|
487 |
|
488 testUnaryExpressionNotPlusMinus1 |
|
489 |
|
490 self |
|
491 parse: 'true' |
|
492 rule: #unaryExpressionNotPlusMinus |
|
493 ! |
|
494 |
|
495 testVariableDeclarator1 |
|
496 |
|
497 self parse: 'var' |
|
498 rule: #variableDeclarator |
|
499 ! |
|
500 |
|
501 testVariableDeclarator2 |
|
502 |
|
503 self parse: 'var[][]' |
|
504 rule: #variableDeclarator |
|
505 ! |
|
506 |
|
507 testVariableDeclarator3 |
|
508 |
|
509 self parse: 'var = a.methd()' |
|
510 rule: #variableDeclarator |
|
511 ! |
|
512 |
|
513 testWhileStatement1 |
|
514 |
|
515 self parse: 'while (true) object.executeMethod();' |
|
516 rule: #whileStatement |
|
517 ! |
|
518 |
|
519 testWhileStatement2 |
|
520 |
|
521 self parse: 'while (3>2) {block; of; statements;}' |
|
522 rule: #whileStatement |
|
523 ! ! |
|
524 |
|
525 !PPJavaSyntaxTest methodsFor:'testing-classes'! |
|
526 |
|
527 testClassDeclaration1 |
|
528 |
|
529 self parse: '//comment |
|
530 class myfirstjavaprog |
|
531 { |
|
532 public static void main(String args[]) |
|
533 { |
|
534 System.out.println("Hello World!!"); |
|
535 } |
|
536 }' |
|
537 rule: #classDeclaration |
|
538 ! |
|
539 |
|
540 testMethodDeclaration1 |
|
541 |
|
542 self |
|
543 parse: 'public void aMethod() { }' |
|
544 rule: #methodDeclaration |
|
545 ! |
|
546 |
|
547 testMethodDeclaration2 |
|
548 |
|
549 self |
|
550 parse: 'public aMethod() { }' |
|
551 rule: #methodDeclaration |
|
552 ! ! |
|
553 |