author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Fri, 03 Oct 2014 01:59:10 +0100 | |
changeset 376 | a2656b27cace |
parent 375 | e2b2f08d054e |
child 377 | 6112a403a52d |
permissions | -rw-r--r-- |
376
a2656b27cace
Added monticelloName to package definition to ease export to .mcz
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
375
diff
changeset
|
1 |
"{ Package: 'stx:goodies/petitparser/tests' }" |
0 | 2 |
|
190 | 3 |
PPAbstractParserTest subclass:#PPObjectTest |
0 | 4 |
instanceVariableNames:'' |
5 |
classVariableNames:'' |
|
6 |
poolDictionaries:'' |
|
7 |
category:'PetitTests-Tests' |
|
8 |
! |
|
9 |
||
10 |
||
11 |
!PPObjectTest methodsFor:'parsers'! |
|
12 |
||
13 |
integer |
|
14 |
^ PPPredicateObjectParser |
|
15 |
on: [ :each | each isInteger ] |
|
16 |
message: 'integer expected' |
|
17 |
! |
|
18 |
||
19 |
string |
|
20 |
^ PPPredicateObjectParser |
|
21 |
on: [ :each | each isString ] |
|
22 |
message: 'string expected' |
|
23 |
! ! |
|
24 |
||
25 |
!PPObjectTest methodsFor:'testing'! |
|
26 |
||
27 |
testInteger |
|
28 |
self assert: self integer parse: #(123) to: 123. |
|
29 |
self assert: self integer fail: #('abc') |
|
30 |
! |
|
31 |
||
32 |
testString |
|
33 |
self assert: self string parse: #('abc') to: 'abc'. |
|
34 |
self assert: self string fail: #(123) |
|
35 |
! ! |
|
36 |
||
37 |
!PPObjectTest methodsFor:'testing-fancy'! |
|
38 |
||
39 |
testFibonacci |
|
40 |
"This parser accepts fibonacci sequences with arbitrary start pairs." |
|
34 | 41 |
|
0 | 42 |
| parser | |
43 |
parser := ((self integer , self integer) end ==> [ :pair | pair first + pair last ]) |
|
44 |
/ (self integer , (self integer , self integer) and >=> [ :stream :continuation | |
|
45 |
| result | |
|
46 |
result := continuation value. |
|
47 |
(result isPetitFailure or: [ result first + result last first ~= result last last ]) |
|
48 |
ifFalse: [ parser parseOn: stream ] |
|
49 |
ifTrue: [ PPFailure message: 'invalid fibonacci sequence' at: stream position ] ]). |
|
50 |
self assert: parser parse: #(1 1) to: 2. |
|
51 |
self assert: parser parse: #(1 1 2) to: 3. |
|
52 |
self assert: parser parse: #(1 1 2 3) to: 5. |
|
53 |
self assert: parser parse: #(1 1 2 3 5) to: 8. |
|
54 |
self assert: parser parse: #(1 1 2 3 5 8) to: 13. |
|
55 |
self assert: parser parse: #(1 1 2 3 5 8 13) to: 21. |
|
56 |
self assert: parser fail: #(). |
|
57 |
self assert: parser fail: #(1). |
|
58 |
self assert: parser fail: #(1 2 3 4) end: 2 |
|
34 | 59 |
|
0 | 60 |
! ! |
61 |
||
62 |
!PPObjectTest methodsFor:'testing-operators'! |
|
63 |
||
64 |
testChoice |
|
65 |
| parser | |
|
66 |
parser := self integer / self string. |
|
67 |
self assert: parser parse: #(123) to: 123. |
|
68 |
self assert: parser parse: #('abc') to: 'abc' |
|
69 |
! |
|
70 |
||
71 |
testSequence |
|
72 |
| parser | |
|
73 |
parser := self integer , self string. |
|
74 |
self assert: parser parse: #(123 'abc') to: #(123 'abc'). |
|
75 |
self assert: parser fail: #(123 456). |
|
76 |
self assert: parser fail: #('abc' 'def'). |
|
77 |
self assert: parser fail: #('abc' 123) |
|
34 | 78 |
|
0 | 79 |
! ! |
80 |
||
81 |
!PPObjectTest class methodsFor:'documentation'! |
|
82 |
||
34 | 83 |
version |
190 | 84 |
^ '$Header: /cvs/stx/stx/goodies/petitparser/PPObjectTest.st,v 1.4 2014-03-04 14:34:19 cg Exp $' |
34 | 85 |
! |
86 |
||
87 |
version_CVS |
|
190 | 88 |
^ '$Header: /cvs/stx/stx/goodies/petitparser/PPObjectTest.st,v 1.4 2014-03-04 14:34:19 cg Exp $' |
34 | 89 |
! |
90 |
||
0 | 91 |
version_SVN |
190 | 92 |
^ '$Id: PPObjectTest.st,v 1.4 2014-03-04 14:34:19 cg Exp $' |
0 | 93 |
! ! |
190 | 94 |