tests/PPExpressionParserTest.st
author sr
Wed, 04 Jul 2018 15:23:55 +0200
changeset 607 019302eb9dd9
parent 558 c49130c8ee2a
permissions -rw-r--r--
build order was wrong
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
558
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     1
"{ Package: 'stx:goodies/petitparser/tests' }"
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     2
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     3
"{ NameSpace: Smalltalk }"
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
PPArithmeticParserTest subclass:#PPExpressionParserTest
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
	instanceVariableNames:''
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
	classVariableNames:''
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
	poolDictionaries:''
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
	category:'PetitTests-Tests'
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
!
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
!PPExpressionParserTest class methodsFor:'testing'!
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
shouldInheritSelectors
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
	^ true
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
! !
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
!PPExpressionParserTest methodsFor:'accessing'!
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
parserInstance
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
	| expression parens number |
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
	expression := PPExpressionParser new.
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
	parens := $( asParser trim , expression , $) asParser trim
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
		==> [ :value | value second ].
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
	number := (#digit asParser plus , ($. asParser , #digit asParser plus) optional) flatten trim
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
		==> [ :value | value asNumber ].
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
	expression term: parens / number.
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
	expression
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
		group: [ :g |
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
			g prefix: $- asParser trim do: [ :op :a | a negated ] ];
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
		group: [ :g |
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
			g postfix: '++' asParser trim do: [ :a :op | a + 1 ].
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
			g postfix: '--' asParser trim do: [ :a :op | a - 1 ] ];
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
		group: [ :g |
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
			g right: $^ asParser trim do: [ :a :op :b | a raisedTo: b ] ];
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
		group: [ :g |
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
			g left: $* asParser trim do: [ :a :op :b | a * b ].
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
			g left: $/ asParser trim do: [ :a :op :b | a / b ] ];
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
		group: [ :g |
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
			g left: $+ asParser trim do: [ :a :op :b | a + b ].
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
			g left: $- asParser trim do: [ :a :op :b | a - b ] ].
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
	^ expression end
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
! !
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
!PPExpressionParserTest methodsFor:'testing'!
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
testPostfixAdd
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
	self assert: '0++' is: 1.
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
	self assert: '0++++' is: 2.
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
	self assert: '0++++++' is: 3.
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
	self assert: '0+++1' is: 2.
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
	self assert: '0+++++1' is: 3.
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
	self assert: '0+++++++1' is: 4
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
!
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
testPostfixSub
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
	self assert: '1--' is: 0.
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
	self assert: '2----' is: 0.
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
	self assert: '3------' is: 0.
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
	self assert: '2---1' is: 0.
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
	self assert: '3-----1' is: 0.
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
	self assert: '4-------1' is: 0.
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
!
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
testPrefixNegate
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
	self assert: '1' is: 1.
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
	self assert: '-1' is: -1.
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
	self assert: '--1' is: 1.
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
	self assert: '---1' is: -1
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
! !
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
!PPExpressionParserTest class methodsFor:'documentation'!
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
version
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
    ^ '$Header$'
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
!
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
version_CVS
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
    ^ '$Header$'
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
! !
c49130c8ee2a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84