4
|
1 |
"{ Package: 'stx:goodies/petitparser' }"
|
0
|
2 |
|
|
3 |
PPAbstractParseTest subclass:#PPObjectTest
|
|
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
|
|
84 |
^ '$Header: /cvs/stx/stx/goodies/petitparser/PPObjectTest.st,v 1.3 2012-05-04 22:04:44 vrany Exp $'
|
|
85 |
!
|
|
86 |
|
|
87 |
version_CVS
|
|
88 |
^ '$Header: /cvs/stx/stx/goodies/petitparser/PPObjectTest.st,v 1.3 2012-05-04 22:04:44 vrany Exp $'
|
|
89 |
!
|
|
90 |
|
0
|
91 |
version_SVN
|
34
|
92 |
^ '§Id: PPObjectTest.st 4 2010-12-18 17:02:23Z kursjan §'
|
0
|
93 |
! !
|