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