author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Thu, 21 May 2015 17:19:17 +0100 | |
changeset 469 | 8dc4eb06316e |
parent 454 | a9cd5ea7cc36 |
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 |
|
454
a9cd5ea7cc36
Portability: fixes for Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
376
diff
changeset
|
3 |
"{ NameSpace: Smalltalk }" |
a9cd5ea7cc36
Portability: fixes for Smalltalk/X
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
376
diff
changeset
|
4 |
|
0 | 5 |
PPArithmeticParserTest subclass:#PPExpressionParserTest |
6 |
instanceVariableNames:'' |
|
7 |
classVariableNames:'' |
|
8 |
poolDictionaries:'' |
|
9 |
category:'PetitTests-Tests' |
|
10 |
! |
|
11 |
||
12 |
||
13 |
!PPExpressionParserTest class methodsFor:'testing'! |
|
14 |
||
15 |
shouldInheritSelectors |
|
16 |
^ true |
|
17 |
! ! |
|
18 |
||
19 |
!PPExpressionParserTest methodsFor:'accessing'! |
|
20 |
||
21 |
parserInstance |
|
22 |
| expression parens number | |
|
23 |
expression := PPExpressionParser new. |
|
187 | 24 |
parens := $( asParser trim , expression , $) asParser trim |
25 |
==> [ :value | value second ]. |
|
26 |
number := (#digit asParser plus , ($. asParser , #digit asParser plus) optional) flatten trim |
|
27 |
==> [ :value | value asNumber ]. |
|
0 | 28 |
expression term: parens / number. |
29 |
expression |
|
30 |
group: [ :g | |
|
187 | 31 |
g prefix: $- asParser trim do: [ :op :a | a negated ] ]; |
0 | 32 |
group: [ :g | |
187 | 33 |
g postfix: '++' asParser trim do: [ :a :op | a + 1 ]. |
34 |
g postfix: '--' asParser trim do: [ :a :op | a - 1 ] ]; |
|
0 | 35 |
group: [ :g | |
187 | 36 |
g right: $^ asParser trim do: [ :a :op :b | a raisedTo: b ] ]; |
0 | 37 |
group: [ :g | |
187 | 38 |
g left: $* asParser trim do: [ :a :op :b | a * b ]. |
39 |
g left: $/ asParser trim do: [ :a :op :b | a / b ] ]; |
|
0 | 40 |
group: [ :g | |
187 | 41 |
g left: $+ asParser trim do: [ :a :op :b | a + b ]. |
42 |
g left: $- asParser trim do: [ :a :op :b | a - b ] ]. |
|
0 | 43 |
^ expression end |
44 |
! ! |
|
45 |
||
46 |
!PPExpressionParserTest methodsFor:'testing'! |
|
47 |
||
48 |
testPostfixAdd |
|
49 |
self assert: '0++' is: 1. |
|
50 |
self assert: '0++++' is: 2. |
|
51 |
self assert: '0++++++' is: 3. |
|
52 |
||
53 |
self assert: '0+++1' is: 2. |
|
54 |
self assert: '0+++++1' is: 3. |
|
55 |
self assert: '0+++++++1' is: 4 |
|
56 |
! |
|
57 |
||
58 |
testPostfixSub |
|
59 |
self assert: '1--' is: 0. |
|
60 |
self assert: '2----' is: 0. |
|
61 |
self assert: '3------' is: 0. |
|
62 |
||
63 |
self assert: '2---1' is: 0. |
|
64 |
self assert: '3-----1' is: 0. |
|
65 |
self assert: '4-------1' is: 0. |
|
66 |
! |
|
67 |
||
68 |
testPrefixNegate |
|
69 |
self assert: '1' is: 1. |
|
70 |
self assert: '-1' is: -1. |
|
71 |
self assert: '--1' is: 1. |
|
72 |
self assert: '---1' is: -1 |
|
73 |
! ! |
|
74 |
||
75 |
!PPExpressionParserTest class methodsFor:'documentation'! |
|
76 |
||
13 | 77 |
version |
187 | 78 |
^ '$Header: /cvs/stx/stx/goodies/petitparser/PPExpressionParserTest.st,v 1.4 2014-03-04 14:34:13 cg Exp $' |
13 | 79 |
! |
80 |
||
81 |
version_CVS |
|
187 | 82 |
^ '$Header: /cvs/stx/stx/goodies/petitparser/PPExpressionParserTest.st,v 1.4 2014-03-04 14:34:13 cg Exp $' |
13 | 83 |
! |
84 |
||
0 | 85 |
version_SVN |
187 | 86 |
^ '$Id: PPExpressionParserTest.st,v 1.4 2014-03-04 14:34:13 cg Exp $' |
0 | 87 |
! ! |
187 | 88 |