tests/PPLambdaParserTest.st
author sr
Wed, 04 Jul 2018 15:23:55 +0200
changeset 607 019302eb9dd9
parent 562 8e07a5704f6b
permissions -rw-r--r--
build order was wrong
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
562
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     1
"{ Package: 'stx:goodies/petitparser/tests' }"
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     2
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     3
"{ NameSpace: Smalltalk }"
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
PPCompositeParserTest subclass:#PPLambdaParserTest
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
	instanceVariableNames:''
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
	classVariableNames:''
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
	poolDictionaries:''
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
	category:'PetitTests-Tests'
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
!
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
!PPLambdaParserTest methodsFor:'accessing'!
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
parserClass
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
	^ PPLambdaParser
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
! !
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
!PPLambdaParserTest methodsFor:'testing'!
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
testAbstraction
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
	self assert: '\x.y' is: #('x' 'y').
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
	self assert: '\x.\y.z' is: #('x' ('y' 'z'))
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
!
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
testApplication
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
	self assert: '(x x)' is: #('x' 'x').
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
	self assert: '(x y)' is: #('x' 'y').
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
	self assert: '((x y) z)' is: #(('x' 'y') 'z').
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
	self assert: '(x (y z))' is: #('x' ('y' 'z'))
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
!
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
testVariable
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
	self assert: 'x' is: 'x'.
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
	self assert: 'xy' is: 'xy'.
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
	self assert: 'x12' is: 'x12'
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
! !
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
!PPLambdaParserTest methodsFor:'testing-curch'!
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
testAnd
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
	self assert: self parserClass and = #('p' ('q' (('p' 'q') 'p')))
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
!
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
testFalse
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
	self assert: self parserClass false = #('x' ('y' 'y'))
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
!
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
testIfThenElse
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
	self assert: self parserClass ifthenelse = #('p' 'p')
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
!
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
testNot
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
	self assert: self parserClass not = #('p' ('a' ('b' (('p' 'b') 'a'))))
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
!
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
testOr
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
	self assert: self parserClass or = #('p' ('q' (('p' 'p') 'q')))
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
!
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
testTrue
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
	self assert: self parserClass true = #('x' ('y' 'x'))
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
! !
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
!PPLambdaParserTest methodsFor:'testing-utilities'!
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
testParseOnError
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
	| beenHere |
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
	result := self parserClass
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
		parse: '\x.y'
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
		onError: [ self fail ].
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
	self assert: result = #('x' 'y').
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
	beenHere := false.
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
	result := self parserClass
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
		parse: '\x.'
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
		onError: [ beenHere := true ].
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
	self assert: beenHere.
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
	beenHere := false.
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
	result := self parserClass
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
		parse: '\x.'
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
		onError: [ :fail | beenHere := true. fail ].
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
	self assert: beenHere.
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
	self assert: (result message findString: '$(') > 0.
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
	self assert: (result message findString: 'expected') > 0.
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
	self assert: (result position = 0).
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
	beenHere := false.
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
	result := self parserClass
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
		parse: '\x.'
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
		onError: [ :msg :pos | 
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
			self assert: (msg findString: '$(') > 0.
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
			self assert: (msg findString: 'expected') > 0.
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
			self assert: (pos = 0).
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
			beenHere := true ].
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
	self assert: result.
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
	self assert: beenHere
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
!
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
testParseStartingAtOnError
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
	| beenHere |
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
	result := self parserClass
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
		parse: 'x'
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
		startingAt: #variable
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
		onError: [ self fail ].
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
	self assert: result = 'x'.
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
	
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
	beenHere := false.
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
	result := self parserClass
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
		parse: '\'
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
		startingAt: #variable
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
		onError: [ beenHere := true ].
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
	self assert: beenHere.
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
	
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
	beenHere := false.
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   117
	result := self parserClass
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
		parse: '\'
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
		startingAt: #variable
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
		onError: [ :fail | beenHere := true. fail ].
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
	self assert: beenHere.
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
	self assert: result message = 'letter expected'.
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
	self assert: result position = 0.
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
	beenHere := false.
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   126
	result := self parserClass
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   127
		parse: '\'
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   128
		startingAt: #variable
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   129
		onError: [ :msg :pos | 
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   130
			self assert: msg = 'letter expected'.
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
			self assert: pos = 0.
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   132
			beenHere := true ].
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   133
	self assert: beenHere
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   134
!
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   135
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   136
testProductionAt
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   137
	self assert: (parser productionAt: #foo) isNil.
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   138
	self assert: (parser productionAt: #foo ifAbsent: [ true ]).
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   139
	
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   140
	self assert: (parser productionAt: #start) notNil.
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   141
	self assert: (parser productionAt: #start ifAbsent: [ true ]) notNil.
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   142
	
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   143
	self assert: (parser productionAt: #variable) notNil.
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   144
	self assert: (parser productionAt: #variable ifAbsent: [ true ]) notNil
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   145
! !
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   146
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   147
!PPLambdaParserTest class methodsFor:'documentation'!
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   148
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   149
version
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   150
    ^ '$Header$'
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   151
!
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   152
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   153
version_CVS
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   154
    ^ '$Header$'
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   155
! !
8e07a5704f6b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   156