initial checkin draft
authorClaus Gittinger <cg@exept.de>
Mon, 02 Jul 2018 07:40:19 +0200
changeset 576 07daf10d3285
parent 575 2130bbd6e714
child 577 726c3fc9c3ef
initial checkin
tests/PPTokenTest.st
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/PPTokenTest.st	Mon Jul 02 07:40:19 2018 +0200
@@ -0,0 +1,166 @@
+"{ Package: 'stx:goodies/petitparser/tests' }"
+
+"{ NameSpace: Smalltalk }"
+
+PPAbstractParserTest subclass:#PPTokenTest
+	instanceVariableNames:''
+	classVariableNames:''
+	poolDictionaries:''
+	category:'PetitTests-Tests'
+!
+
+
+!PPTokenTest methodsFor:'accessing'!
+
+identifier
+	^ #word asParser plus token
+! !
+
+!PPTokenTest methodsFor:'testing'!
+
+testCollection
+	| input result |
+	input := 'foo    '.
+	result := self 
+		parse: input
+		using: self identifier.
+	self assert: (result collection = input).
+	self assert: (result collection == input)
+!
+
+testInitialize
+	PPToken initialize
+!
+
+testNew
+	self should: [ PPToken new ] raise: Error.
+	
+!
+
+testPrinting
+	| result |
+	result := PPToken on: 'var'.
+	self assert: (result printString findString: 'PPToken[1,3]') > 0
+!
+
+testSize
+	| result |
+	result := self 
+		parse: 'foo'
+		using: self identifier.
+	self assert: result size = 3
+!
+
+testStart
+	| result |
+	result := self 
+		parse: 'foo'
+		using: self identifier.
+	self assert: result start = 1
+!
+
+testStop
+	| result |
+	result := self 
+		parse: 'foo'
+		using: self identifier.
+	self assert: result stop = 3
+!
+
+testValue
+	| result |
+	result := PPToken on: 'var'.
+	self should: [ result value ] raise: Notification
+! !
+
+!PPTokenTest methodsFor:'testing-comparing'!
+
+testEquality
+	| token1 token2 |
+	token1 := self  parse: 'foo' using: self identifier.
+	token2 := self  parse: 'foo' using: self identifier.
+	self deny: token1 == token2.
+	self assert: token1 = token2.
+	self assert: token1 hash = token2 hash.
+! !
+
+!PPTokenTest methodsFor:'testing-copying'!
+
+testCopyFromTo
+	| result other |
+	result := PPToken on: 'abc'.
+	other := result copyFrom: 2 to: 2.
+	
+	self assert: other size = 1.
+	self assert: other start = 2.
+	self assert: other stop = 2.
+	self assert: other collection = result collection
+! !
+
+!PPTokenTest methodsFor:'testing-querying'!
+
+testColumn
+        | input parser result cr |
+
+        cr := Smalltalk isSmalltalkX 
+                ifTrue:[ Character return]
+                ifFalse:[ Character cr ].
+        input := '1' , (String with:cr) , '12' , (String with: cr with: Character lf) , '123' , (String with: Character lf) , '1234'.
+        parser := #any asParser token star.
+        result := parser parse: input.
+        result 
+                with:  #(1 2 1 2 3 4 1 2 3 4 1 2 3 4)
+                do: [ :token :line | self assert: token column = line ]
+!
+
+testLine
+        | input parser result cr|
+
+        cr := Smalltalk isSmalltalkX 
+            ifTrue:[Character return] 
+            ifFalse:[Character cr].
+        input := '1' , (String with: cr) , '12' , (String with: cr with: Character lf) , '123' , (String with: Character lf) , '1234'.
+        parser := #any asParser token star.
+        result := parser parse: input.
+        result 
+                with: #(1 1 2 2 2 2 3 3 3 3 4 4 4 4) 
+                do: [ :token :line | self assert: token line = line ]
+! !
+
+!PPTokenTest methodsFor:'testing-values'!
+
+testInputValue
+	| input result |
+	input := 'foo'.
+	result := self 
+		parse: input
+		using: self identifier.
+	self assert: result inputValue = input.
+	self deny: result inputValue == input
+!
+
+testParsedValue
+	| input result |
+	input := 'foo'.
+	result := self 
+		parse: input
+		using: self identifier.
+	self assert: result parsedValue = #($f $o $o)
+! !
+
+!PPTokenTest methodsFor:'utilities'!
+
+parse: aString using: aParser
+	^ aParser parse: aString
+! !
+
+!PPTokenTest class methodsFor:'documentation'!
+
+version
+    ^ '$Header$'
+!
+
+version_CVS
+    ^ '$Header$'
+! !
+