210
|
1 |
"{ Package: 'stx:goodies/petitparser/analyzer/tests' }"
|
|
2 |
|
|
3 |
PPAbstractParserTest subclass:#PPSearcherTest
|
|
4 |
instanceVariableNames:'searcher'
|
|
5 |
classVariableNames:''
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'PetitAnalyzer-Tests'
|
|
8 |
!
|
|
9 |
|
|
10 |
|
|
11 |
!PPSearcherTest methodsFor:'running'!
|
|
12 |
|
|
13 |
setUp
|
|
14 |
searcher := PPSearcher new
|
|
15 |
! !
|
|
16 |
|
|
17 |
!PPSearcherTest methodsFor:'testing'!
|
|
18 |
|
|
19 |
testAnyPattern
|
|
20 |
| result |
|
|
21 |
searcher
|
|
22 |
matches: PPPattern any
|
|
23 |
do: [ :parser :answer | answer add: parser; yourself ].
|
|
24 |
|
|
25 |
result := searcher
|
|
26 |
execute: ($a asParser)
|
|
27 |
initialAnswer: OrderedCollection new.
|
|
28 |
self assert: result size = 1.
|
|
29 |
|
|
30 |
result := searcher
|
|
31 |
execute: ($a asParser star)
|
|
32 |
initialAnswer: OrderedCollection new.
|
|
33 |
self assert: result size = 2.
|
|
34 |
|
|
35 |
result := searcher
|
|
36 |
execute: ($a asParser , $b asParser)
|
|
37 |
initialAnswer: OrderedCollection new.
|
|
38 |
self assert: result size = 3
|
|
39 |
!
|
|
40 |
|
|
41 |
testClassPattern
|
|
42 |
| result |
|
|
43 |
searcher
|
|
44 |
matches: (PPPattern class: PPLiteralObjectParser)
|
|
45 |
do: [ :parser :answer | answer add: parser; yourself ].
|
|
46 |
|
|
47 |
result := searcher
|
|
48 |
execute: ($a asParser)
|
|
49 |
initialAnswer: OrderedCollection new.
|
|
50 |
self assert: result size = 1.
|
|
51 |
self assert: (result allSatisfy: [ :each | each class = PPLiteralObjectParser ]).
|
|
52 |
|
|
53 |
result := searcher
|
|
54 |
execute: ('abc' asParser)
|
|
55 |
initialAnswer: OrderedCollection new.
|
|
56 |
self assert: result isEmpty.
|
|
57 |
|
|
58 |
result := searcher
|
|
59 |
execute: (#any asParser)
|
|
60 |
initialAnswer: OrderedCollection new.
|
|
61 |
self assert: result isEmpty.
|
|
62 |
|
|
63 |
result := searcher
|
|
64 |
execute: ($a asParser / #any asParser , $b asParser)
|
|
65 |
initialAnswer: OrderedCollection new.
|
|
66 |
self assert: result size = 2.
|
|
67 |
self assert: (result allSatisfy: [ :each | each class = PPLiteralObjectParser ])
|
|
68 |
!
|
|
69 |
|
|
70 |
testKindPattern
|
|
71 |
| result |
|
|
72 |
searcher
|
|
73 |
matches: (PPPattern kind: PPLiteralParser)
|
|
74 |
do: [ :parser :answer | answer add: parser; yourself ].
|
|
75 |
|
|
76 |
result := searcher
|
|
77 |
execute: ($a asParser)
|
|
78 |
initialAnswer: OrderedCollection new.
|
|
79 |
self assert: result size = 1.
|
|
80 |
self assert: (result allSatisfy: [ :each | each class = PPLiteralObjectParser ]).
|
|
81 |
|
|
82 |
result := searcher
|
|
83 |
execute: ('abc' asParser)
|
|
84 |
initialAnswer: OrderedCollection new.
|
|
85 |
self assert: result size = 1.
|
|
86 |
self assert: (result allSatisfy: [ :each | each class = PPLiteralSequenceParser ]).
|
|
87 |
|
|
88 |
result := searcher
|
|
89 |
execute: (#any asParser)
|
|
90 |
initialAnswer: OrderedCollection new.
|
|
91 |
self assert: result isEmpty.
|
|
92 |
|
|
93 |
result := searcher
|
|
94 |
execute: ($a asParser / #any asParser , $b asParser)
|
|
95 |
initialAnswer: OrderedCollection new.
|
|
96 |
self assert: result size = 2.
|
|
97 |
self assert: (result allSatisfy: [ :each | each class = PPLiteralObjectParser ])
|
|
98 |
!
|
|
99 |
|
|
100 |
testMatchesAny
|
|
101 |
| result |
|
|
102 |
searcher
|
|
103 |
matchesAnyOf: (Array
|
|
104 |
with: $a asParser
|
|
105 |
with: $b asParser)
|
|
106 |
do: [ :parser :answer | answer add: parser; yourself ].
|
|
107 |
|
|
108 |
result := searcher
|
|
109 |
execute: $a asParser , $b asParser , $c asParser
|
|
110 |
initialAnswer: OrderedCollection new.
|
|
111 |
|
|
112 |
self assert: result size = 2.
|
|
113 |
self assert: result first literal = $a.
|
|
114 |
self assert: result last literal = $b
|
|
115 |
!
|
|
116 |
|
|
117 |
testMultiplePattern
|
|
118 |
| result |
|
|
119 |
searcher
|
|
120 |
matches: $a asParser
|
|
121 |
do: [ :parser :answer | answer first add: parser. answer ].
|
|
122 |
searcher
|
|
123 |
matches: PPPattern any
|
|
124 |
do: [ :parser :answer | answer second add: parser. answer ].
|
|
125 |
|
|
126 |
result := searcher
|
|
127 |
execute: $a asParser , $a asParser , $b asParser
|
|
128 |
initialAnswer: (Array
|
|
129 |
with: OrderedCollection new
|
|
130 |
with: OrderedCollection new).
|
|
131 |
|
|
132 |
self assert: result first size = 2.
|
|
133 |
self assert: result first first literal = $a.
|
|
134 |
self assert: result first last literal = $a.
|
|
135 |
|
|
136 |
self assert: result last size = 2.
|
|
137 |
self assert: result last first class = PPSequenceParser.
|
|
138 |
self assert: result last last literal = $b
|
|
139 |
!
|
|
140 |
|
|
141 |
testNamePattern
|
|
142 |
| result |
|
|
143 |
searcher
|
|
144 |
matches: (PPPattern name: 'foo')
|
|
145 |
do: [ :parser :answer | answer add: parser; yourself ].
|
|
146 |
|
|
147 |
result := searcher
|
|
148 |
execute: ($a asParser)
|
|
149 |
initialAnswer: OrderedCollection new.
|
|
150 |
self assert: result isEmpty.
|
|
151 |
|
|
152 |
result := searcher
|
|
153 |
execute: ($a asParser name: 'foo')
|
|
154 |
initialAnswer: OrderedCollection new.
|
|
155 |
self assert: result size = 1.
|
|
156 |
self assert: result first name = 'foo'.
|
|
157 |
|
|
158 |
result := searcher
|
|
159 |
execute: ($a asParser name: 'bar') , ($b asParser name: 'foo')
|
|
160 |
initialAnswer: OrderedCollection new.
|
|
161 |
self assert: result size = 1.
|
|
162 |
self assert: result first name = 'foo'
|
|
163 |
!
|
|
164 |
|
|
165 |
testNewPattern
|
|
166 |
self
|
|
167 |
should: [ PPPattern new ]
|
|
168 |
raise: Error
|
|
169 |
!
|
|
170 |
|
|
171 |
testRecursivePattern
|
|
172 |
| recursive |
|
|
173 |
recursive := PPDelegateParser new.
|
|
174 |
recursive setParser: recursive.
|
|
175 |
searcher
|
|
176 |
matches: recursive
|
|
177 |
do: [ :parser :answer | parser ].
|
|
178 |
|
|
179 |
self assert: (searcher execute: recursive) = recursive.
|
|
180 |
self assert: (searcher execute: $a asParser) isNil.
|
|
181 |
self assert: (searcher execute: $a asParser / $b asParser star) isNil
|
|
182 |
!
|
|
183 |
|
|
184 |
testRepeatedPattern
|
|
185 |
| pattern result |
|
|
186 |
searcher
|
|
187 |
matches: (pattern := PPPattern any) , pattern
|
|
188 |
do: [ :parser :answer | answer add: parser; yourself ].
|
|
189 |
|
|
190 |
result := searcher
|
|
191 |
execute: ($a asParser , $b asParser)
|
|
192 |
initialAnswer: OrderedCollection new.
|
|
193 |
self assert: result isEmpty.
|
|
194 |
|
|
195 |
result := searcher
|
|
196 |
execute: $a asParser , $a asParser
|
|
197 |
initialAnswer: OrderedCollection new.
|
|
198 |
self assert: result size = 1.
|
|
199 |
|
|
200 |
result := searcher
|
|
201 |
execute: ($a asParser , ($a asParser , $b asParser))
|
|
202 |
initialAnswer: OrderedCollection new.
|
|
203 |
self assert: result isEmpty.
|
|
204 |
|
|
205 |
result := searcher
|
|
206 |
execute: ($b asParser , ($a asParser , $a asParser))
|
|
207 |
initialAnswer: OrderedCollection new.
|
|
208 |
self assert: result size = 1
|
|
209 |
! !
|
|
210 |
|
|
211 |
!PPSearcherTest methodsFor:'testing-copy'!
|
|
212 |
|
|
213 |
testCopyMatchAction
|
|
214 |
| old new |
|
|
215 |
old := $a asParser ==> [ :token | $b ].
|
|
216 |
new := old copyInContext: Dictionary new.
|
|
217 |
self assert: (old match: old inContext: Dictionary new).
|
|
218 |
self assert: (old match: new inContext: Dictionary new).
|
|
219 |
self deny: (old == new).
|
|
220 |
!
|
|
221 |
|
|
222 |
testCopyMatchDelegate
|
|
223 |
| old new |
|
|
224 |
old := $a asParser token trim.
|
|
225 |
new := old copyInContext: Dictionary new.
|
|
226 |
self assert: (old match: old inContext: Dictionary new).
|
|
227 |
self assert: (old match: new inContext: Dictionary new).
|
|
228 |
self deny: (old == new).
|
|
229 |
!
|
|
230 |
|
|
231 |
testCopyMatchEpsilon
|
|
232 |
| old new |
|
|
233 |
old := nil asParser.
|
|
234 |
new := old copyInContext: Dictionary new.
|
|
235 |
self assert: (old match: old inContext: Dictionary new).
|
|
236 |
self assert: (old match: new inContext: Dictionary new).
|
|
237 |
self deny: (old == new).
|
|
238 |
!
|
|
239 |
|
|
240 |
testCopyMatchFailure
|
|
241 |
| old new |
|
|
242 |
old := PPFailingParser message: 'problem'.
|
|
243 |
new := old copyInContext: Dictionary new.
|
|
244 |
self assert: (old match: old inContext: Dictionary new).
|
|
245 |
self assert: (old match: new inContext: Dictionary new).
|
|
246 |
self deny: (old == new).
|
|
247 |
!
|
|
248 |
|
|
249 |
testCopyMatchList
|
|
250 |
| old new |
|
|
251 |
old := $a asParser , $b asParser , $c asParser.
|
|
252 |
new := old copyInContext: Dictionary new.
|
|
253 |
self assert: (old match: old inContext: Dictionary new).
|
|
254 |
self assert: (old match: new inContext: Dictionary new).
|
|
255 |
self deny: (old == new).
|
|
256 |
!
|
|
257 |
|
|
258 |
testCopyMatchLiteral
|
|
259 |
| old new |
|
|
260 |
old := $a asParser.
|
|
261 |
new := old copyInContext: Dictionary new.
|
|
262 |
self assert: (old match: old inContext: Dictionary new).
|
|
263 |
self assert: (old match: new inContext: Dictionary new).
|
|
264 |
self deny: (old == new).
|
|
265 |
!
|
|
266 |
|
|
267 |
testCopyMatchPluggable
|
|
268 |
| old new |
|
|
269 |
old := [ :stream | ] asParser.
|
|
270 |
new := old copyInContext: Dictionary new.
|
|
271 |
self assert: (old match: old inContext: Dictionary new).
|
|
272 |
self assert: (old match: new inContext: Dictionary new).
|
|
273 |
self deny: (old == new).
|
|
274 |
!
|
|
275 |
|
|
276 |
testCopyMatchPredicate
|
|
277 |
| old new |
|
|
278 |
old := #word asParser.
|
|
279 |
new := old copyInContext: Dictionary new.
|
|
280 |
self assert: (old match: old inContext: Dictionary new).
|
|
281 |
self assert: (old match: new inContext: Dictionary new).
|
|
282 |
self deny: (old == new).
|
|
283 |
!
|
|
284 |
|
|
285 |
testCopyMatchPredicateSequence
|
|
286 |
| old new |
|
|
287 |
old := 'foo' asParser caseInsensitive.
|
|
288 |
new := old copyInContext: Dictionary new.
|
|
289 |
self assert: (old match: old inContext: Dictionary new).
|
|
290 |
self assert: (old match: new inContext: Dictionary new).
|
|
291 |
self deny: (old == new).
|
|
292 |
!
|
|
293 |
|
|
294 |
testCopyMatchRecursiveDelegate
|
|
295 |
| old new |
|
|
296 |
old := PPDelegateParser new.
|
|
297 |
old setParser: old.
|
|
298 |
new := old copyInContext: Dictionary new.
|
|
299 |
self assert: (old match: old inContext: Dictionary new).
|
|
300 |
self assert: (old match: new inContext: Dictionary new).
|
|
301 |
self deny: (old == new).
|
|
302 |
!
|
|
303 |
|
|
304 |
testCopyMatchRecursiveList
|
|
305 |
| old new |
|
|
306 |
old := PPChoiceParser new.
|
|
307 |
old setParsers: (Array with: old).
|
|
308 |
new := old copyInContext: Dictionary new.
|
|
309 |
self assert: (old match: old inContext: Dictionary new).
|
|
310 |
self assert: (old match: new inContext: Dictionary new).
|
|
311 |
self deny: (old == new).
|
|
312 |
!
|
|
313 |
|
|
314 |
testCopyMatchRepetition
|
|
315 |
| old new |
|
|
316 |
old := #word asParser star.
|
|
317 |
new := old copyInContext: Dictionary new.
|
|
318 |
self assert: (old match: old inContext: Dictionary new).
|
|
319 |
self assert: (old match: new inContext: Dictionary new).
|
|
320 |
self deny: (old == new).
|
|
321 |
!
|
|
322 |
|
|
323 |
testCopyMatchRepetitionGreedy
|
|
324 |
| old new |
|
|
325 |
old := #word asParser starGreedy: #digit asParser.
|
|
326 |
new := old copyInContext: Dictionary new.
|
|
327 |
self assert: (old match: old inContext: Dictionary new).
|
|
328 |
self assert: (old match: new inContext: Dictionary new).
|
|
329 |
self deny: (old == new).
|
|
330 |
!
|
|
331 |
|
|
332 |
testCopyMatchRepetitionLazy
|
|
333 |
| old new |
|
|
334 |
old := #word asParser starLazy: #digit asParser.
|
|
335 |
new := old copyInContext: Dictionary new.
|
|
336 |
self assert: (old match: old inContext: Dictionary new).
|
|
337 |
self assert: (old match: new inContext: Dictionary new).
|
|
338 |
self deny: (old == new).
|
|
339 |
!
|
|
340 |
|
|
341 |
testCopyMatchRepetitionMinMax
|
|
342 |
| old new |
|
|
343 |
old := #word asParser min: 5 max: 10.
|
|
344 |
new := old copyInContext: Dictionary new.
|
|
345 |
self assert: (old match: old inContext: Dictionary new).
|
|
346 |
self assert: (old match: new inContext: Dictionary new).
|
|
347 |
self deny: (old == new).
|
|
348 |
! !
|
|
349 |
|
|
350 |
!PPSearcherTest methodsFor:'testing-lists'!
|
|
351 |
|
|
352 |
testListInfix
|
|
353 |
| result |
|
|
354 |
searcher
|
|
355 |
matches: PPListPattern any , $a asParser , PPListPattern any
|
|
356 |
do: [ :parser :answer | true ].
|
|
357 |
|
|
358 |
result := searcher
|
|
359 |
execute: $a asParser , $a asParser
|
|
360 |
initialAnswer: false.
|
|
361 |
self assert: result.
|
|
362 |
|
|
363 |
result := searcher
|
|
364 |
execute: $a asParser , $b asParser
|
|
365 |
initialAnswer: false.
|
|
366 |
self assert: result.
|
|
367 |
|
|
368 |
result := searcher
|
|
369 |
execute: $b asParser , $a asParser
|
|
370 |
initialAnswer: false.
|
|
371 |
self assert: result.
|
|
372 |
|
|
373 |
result := searcher
|
|
374 |
execute: $b asParser , $b asParser
|
|
375 |
initialAnswer: false.
|
|
376 |
self deny: result.
|
|
377 |
|
|
378 |
result := searcher
|
|
379 |
execute: $a asParser , $a asParser , $a asParser
|
|
380 |
initialAnswer: false.
|
|
381 |
self assert: result.
|
|
382 |
|
|
383 |
result := searcher
|
|
384 |
execute: $b asParser , $a asParser , $a asParser
|
|
385 |
initialAnswer: false.
|
|
386 |
self assert: result.
|
|
387 |
|
|
388 |
result := searcher
|
|
389 |
execute: $a asParser , $b asParser , $a asParser
|
|
390 |
initialAnswer: false.
|
|
391 |
self assert: result.
|
|
392 |
|
|
393 |
result := searcher
|
|
394 |
execute: $a asParser , $a asParser , $b asParser
|
|
395 |
initialAnswer: false.
|
|
396 |
self assert: result.
|
|
397 |
|
|
398 |
result := searcher
|
|
399 |
execute: $b asParser , $b asParser , $a asParser
|
|
400 |
initialAnswer: false.
|
|
401 |
self assert: result.
|
|
402 |
|
|
403 |
result := searcher
|
|
404 |
execute: $a asParser , $b asParser , $b asParser
|
|
405 |
initialAnswer: false.
|
|
406 |
self assert: result.
|
|
407 |
|
|
408 |
result := searcher
|
|
409 |
execute: $b asParser , $a asParser , $b asParser
|
|
410 |
initialAnswer: false.
|
|
411 |
self assert: result.
|
|
412 |
|
|
413 |
result := searcher
|
|
414 |
execute: $b asParser , $b asParser , $b asParser
|
|
415 |
initialAnswer: false.
|
|
416 |
self deny: result
|
|
417 |
!
|
|
418 |
|
|
419 |
testListPostfix
|
|
420 |
| result |
|
|
421 |
searcher
|
|
422 |
matches: PPListPattern any , $b asParser
|
|
423 |
do: [ :parser :answer | true ].
|
|
424 |
|
|
425 |
result := searcher
|
|
426 |
execute: $a asParser , $a asParser
|
|
427 |
initialAnswer: false.
|
|
428 |
self deny: result.
|
|
429 |
|
|
430 |
result := searcher
|
|
431 |
execute: $a asParser , $b asParser
|
|
432 |
initialAnswer: false.
|
|
433 |
self assert: result.
|
|
434 |
|
|
435 |
result := searcher
|
|
436 |
execute: $b asParser , $a asParser
|
|
437 |
initialAnswer: false.
|
|
438 |
self deny: result.
|
|
439 |
|
|
440 |
result := searcher
|
|
441 |
execute: $b asParser , $b asParser
|
|
442 |
initialAnswer: false.
|
|
443 |
self assert: result.
|
|
444 |
|
|
445 |
result := searcher
|
|
446 |
execute: $a asParser , $a asParser , $a asParser
|
|
447 |
initialAnswer: false.
|
|
448 |
self deny: result.
|
|
449 |
|
|
450 |
result := searcher
|
|
451 |
execute: $b asParser , $a asParser , $a asParser
|
|
452 |
initialAnswer: false.
|
|
453 |
self deny: result.
|
|
454 |
|
|
455 |
result := searcher
|
|
456 |
execute: $a asParser , $b asParser , $a asParser
|
|
457 |
initialAnswer: false.
|
|
458 |
self deny: result.
|
|
459 |
|
|
460 |
result := searcher
|
|
461 |
execute: $a asParser , $a asParser , $b asParser
|
|
462 |
initialAnswer: false.
|
|
463 |
self assert: result.
|
|
464 |
|
|
465 |
result := searcher
|
|
466 |
execute: $b asParser , $b asParser , $a asParser
|
|
467 |
initialAnswer: false.
|
|
468 |
self deny: result.
|
|
469 |
|
|
470 |
result := searcher
|
|
471 |
execute: $a asParser , $b asParser , $b asParser
|
|
472 |
initialAnswer: false.
|
|
473 |
self assert: result.
|
|
474 |
|
|
475 |
result := searcher
|
|
476 |
execute: $b asParser , $a asParser , $b asParser
|
|
477 |
initialAnswer: false.
|
|
478 |
self assert: result.
|
|
479 |
|
|
480 |
result := searcher
|
|
481 |
execute: $b asParser , $b asParser , $b asParser
|
|
482 |
initialAnswer: false.
|
|
483 |
self assert: result
|
|
484 |
!
|
|
485 |
|
|
486 |
testListPrefix
|
|
487 |
| result |
|
|
488 |
searcher
|
|
489 |
matches: $a asParser , PPListPattern any
|
|
490 |
do: [ :parser :answer | true ].
|
|
491 |
|
|
492 |
result := searcher
|
|
493 |
execute: $a asParser , $a asParser
|
|
494 |
initialAnswer: false.
|
|
495 |
self assert: result.
|
|
496 |
|
|
497 |
result := searcher
|
|
498 |
execute: $a asParser , $b asParser
|
|
499 |
initialAnswer: false.
|
|
500 |
self assert: result.
|
|
501 |
|
|
502 |
result := searcher
|
|
503 |
execute: $b asParser , $a asParser
|
|
504 |
initialAnswer: false.
|
|
505 |
self deny: result.
|
|
506 |
|
|
507 |
result := searcher
|
|
508 |
execute: $b asParser , $b asParser
|
|
509 |
initialAnswer: false.
|
|
510 |
self deny: result.
|
|
511 |
|
|
512 |
|
|
513 |
|
|
514 |
result := searcher
|
|
515 |
execute: $a asParser , $a asParser , $a asParser
|
|
516 |
initialAnswer: false.
|
|
517 |
self assert: result.
|
|
518 |
|
|
519 |
result := searcher
|
|
520 |
execute: $b asParser , $a asParser , $a asParser
|
|
521 |
initialAnswer: false.
|
|
522 |
self deny: result.
|
|
523 |
|
|
524 |
result := searcher
|
|
525 |
execute: $a asParser , $b asParser , $a asParser
|
|
526 |
initialAnswer: false.
|
|
527 |
self assert: result.
|
|
528 |
|
|
529 |
result := searcher
|
|
530 |
execute: $a asParser , $a asParser , $b asParser
|
|
531 |
initialAnswer: false.
|
|
532 |
self assert: result.
|
|
533 |
|
|
534 |
result := searcher
|
|
535 |
execute: $b asParser , $b asParser , $a asParser
|
|
536 |
initialAnswer: false.
|
|
537 |
self deny: result.
|
|
538 |
|
|
539 |
result := searcher
|
|
540 |
execute: $a asParser , $b asParser , $b asParser
|
|
541 |
initialAnswer: false.
|
|
542 |
self assert: result.
|
|
543 |
|
|
544 |
result := searcher
|
|
545 |
execute: $b asParser , $a asParser , $b asParser
|
|
546 |
initialAnswer: false.
|
|
547 |
self deny: result.
|
|
548 |
|
|
549 |
result := searcher
|
|
550 |
execute: $b asParser , $b asParser , $b asParser
|
|
551 |
initialAnswer: false.
|
|
552 |
self deny: result
|
|
553 |
! !
|
|
554 |
|
|
555 |
!PPSearcherTest class methodsFor:'documentation'!
|
|
556 |
|
|
557 |
version
|
|
558 |
^ '$Header: /cvs/stx/stx/goodies/petitparser/analyzer/tests/PPSearcherTest.st,v 1.1 2014-03-04 15:43:09 cg Exp $'
|
|
559 |
!
|
|
560 |
|
|
561 |
version_CVS
|
|
562 |
^ '$Header: /cvs/stx/stx/goodies/petitparser/analyzer/tests/PPSearcherTest.st,v 1.1 2014-03-04 15:43:09 cg Exp $'
|
|
563 |
! !
|
|
564 |
|