initial checkin draft
authorClaus Gittinger <cg@exept.de>
Mon, 02 Jul 2018 07:34:58 +0200
changeset 560 d1fbb249043c
parent 559 3725143c88f1
child 561 9885688dbd3b
initial checkin
tests/PPExtensionTest.st
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/PPExtensionTest.st	Mon Jul 02 07:34:58 2018 +0200
@@ -0,0 +1,154 @@
+"{ Encoding: utf8 }"
+
+"{ Package: 'stx:goodies/petitparser/tests' }"
+
+"{ NameSpace: Smalltalk }"
+
+PPAbstractParserTest subclass:#PPExtensionTest
+	instanceVariableNames:''
+	classVariableNames:''
+	poolDictionaries:''
+	category:'PetitTests-Tests'
+!
+
+
+!PPExtensionTest methodsFor:'testing-parser'!
+
+testCharacter
+	| parser |
+	parser := $a asParser.
+	self assert: parser parse: 'a' to: $a.
+	self assert: parser fail: 'b'
+!
+
+testChoice
+	| parser |
+	parser := #(1 2) asChoiceParser.
+	self assert: parser parse: #(1) to: 1.
+	self assert: parser parse: #(2) to: 2.
+	self assert: parser parse: #(1 2) to: 1 end: 1.
+	self assert: parser parse: #(2 1) to: 2 end: 1.
+	self assert: parser fail: #().
+	self assert: parser fail: #(3)
+!
+
+testClosure
+	| parser |
+	parser := [ :stream | stream upTo: $s ] asParser.
+	self assert: parser parse: '' to: ''.
+	self assert: parser parse: 'a' to: 'a'.
+	self assert: parser parse: 'aa' to: 'aa'.
+	self assert: parser parse: 's' to: ''.
+	self assert: parser parse: 'as' to: 'a'.
+	self assert: parser parse: 'aas' to: 'aa'.
+	self assert: parser parse: 'sa' to: '' end: 1.
+	self assert: parser parse: 'saa' to: '' end: 1.
+	
+	parser := [ :stream | stream upTo: $s. PPFailure message: 'stream' at: stream position ] asParser.
+	self assert: parser fail: ''.
+	self assert: parser fail: 's'.
+	self assert: parser fail: 'as'
+	
+!
+
+testEpsilon
+	| parser |
+	parser := nil asParser.
+	self assert: parser asParser = parser
+!
+
+testOrdered
+	| parser |
+	parser := #(1 2) asParser.
+	self assert: parser parse: #(1 2) to: #(1 2).
+	self assert: parser parse: #(1 2 3) to: #(1 2) end: 2.
+	self assert: parser fail: #().
+	self assert: parser fail: #(1).
+	self assert: parser fail: #(1 1).
+	self assert: parser fail: #(1 1 2)
+!
+
+testParser
+	| parser |
+	parser := $a asParser.
+	self assert: parser asParser = parser
+!
+
+testRange
+        | parser |
+        parser := ($a to: $c) asParser.
+        self assert: parser parse: 'a' to: $a.
+        self assert: parser parse: 'b' to: $b.
+        self assert: parser parse: 'c' to: $c.
+        self assert: parser fail: 'd'
+!
+
+testSequence
+	| parser |
+	parser := #(1 2) asSequenceParser.
+	self assert: parser parse: #(1 2) to: #(1 2).
+	self assert: parser parse: #(1 2 3) to: #(1 2) end: 2.
+	self assert: parser fail: #().
+	self assert: parser fail: #(1).
+	self assert: parser fail: #(1 1).
+	self assert: parser fail: #(1 1 2)
+!
+
+testString
+	| parser |
+	parser := 'ab' asParser.
+	self assert: parser parse: 'ab' to: 'ab'.
+	self assert: parser parse: 'aba' to: 'ab' end: 2.
+	self assert: parser parse: 'abb' to: 'ab' end: 2.
+	self assert: parser fail: 'a'.
+	self assert: parser fail: 'ac'
+!
+
+testSymbol
+	| parser |
+	parser := #any asParser.
+	self assert: parser parse: 'a'.
+	self assert: parser fail: ''
+!
+
+testUnordered
+	| parser |
+	parser := #(1 2) asSet asParser.
+	self assert: parser parse: #(1) to: 1.
+	self assert: parser parse: #(2) to: 2.
+	self assert: parser parse: #(1 2) to: 1 end: 1.
+	self assert: parser parse: #(2 1) to: 2 end: 1.
+	self assert: parser fail: #().
+	self assert: parser fail: #(3)
+! !
+
+!PPExtensionTest methodsFor:'testing-stream'!
+
+testStream
+	| stream |
+	stream := 'abc' readStream asPetitStream.
+	self assert: (stream class = PPStream).
+	self assert: (stream printString = '·abc').
+	self assert: (stream peek) = $a.
+	self assert: (stream uncheckedPeek = $a).
+	self assert: (stream next) = $a.
+	self assert: (stream printString = 'a·bc').
+	self assert: (stream asPetitStream = stream)
+!
+
+testText
+	| stream |
+	stream := 'abc' asText asPetitStream.
+	self assert: stream class = PPStream
+! !
+
+!PPExtensionTest class methodsFor:'documentation'!
+
+version
+    ^ '$Header$'
+!
+
+version_CVS
+    ^ '$Header$'
+! !
+