compiler/tests/PEGFsaStateTest.st
changeset 502 1e45d3c96ec5
child 515 b5316ef15274
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/compiler/tests/PEGFsaStateTest.st	Fri Jul 24 15:06:54 2015 +0100
@@ -0,0 +1,210 @@
+"{ Package: 'stx:goodies/petitparser/compiler/tests' }"
+
+"{ NameSpace: Smalltalk }"
+
+TestCase subclass:#PEGFsaStateTest
+	instanceVariableNames:'state t1 t2 t3 t4 anotherState'
+	classVariableNames:''
+	poolDictionaries:''
+	category:'PetitCompiler-Tests-FSA'
+!
+
+!PEGFsaStateTest methodsFor:'as yet unclassified'!
+
+setUp
+    state := PEGFsaState new name: #state; retval: #state; yourself.
+    anotherState := PEGFsaState new name: #anotherState; retval: #anotherState; yourself.
+
+    t1 := PEGFsaTransition new.
+    t2 := PEGFsaTransition new.
+    t3 := PEGFsaTransition new.
+    t4 := PEGFsaTransition new.
+    
+!
+
+testCopy
+    state addTransition: t1.
+    anotherState := state copy.
+    
+    self assert: (state = anotherState).
+    self assert: (state == anotherState) not.
+    
+    state retval: #foo.
+    self assert: (state = anotherState) not.
+
+    anotherState retval: #foo.
+    self assert: (state = anotherState).
+    
+    state addTransition: t2.
+    self assert: (state = anotherState) not.
+    
+    anotherState addTransition: t2.
+    self assert: (state = anotherState).
+    
+!
+
+testCopy2
+    state addTransition: t1.
+    anotherState := state copy.
+    
+    self assert: (state = anotherState).
+    self assert: (state == anotherState) not.
+    
+    state addTransition: t2.
+    self assert: (state = anotherState) not.
+    
+    anotherState addTransition: t2 copy.
+    self assert: (state = anotherState).
+    
+!
+
+testCopy3
+    state addTransition: t1.
+    anotherState := state copy.
+    
+    self assert: (state = anotherState).
+    self assert: (state == anotherState) not.
+    
+    t1 addCharacter: $x.
+    self assert: (state = anotherState) not.
+    
+    anotherState transitions anyOne addCharacter: $x.	
+    self assert: (state = anotherState).
+    
+!
+
+testEquals
+    state addTransition: t1.
+    anotherState addTransition: t2.
+
+    state retval: #baz.
+    anotherState retval: #baz.
+    
+    t1 destination: #foo.
+    t2 destination: #bar.
+        
+    self assert: (state equals: anotherState) not
+!
+
+testEquals2
+    state addTransition: t1.
+    anotherState addTransition: t2.
+
+    state retval: #baz.
+    anotherState retval: #baz.
+    
+    t1 destination: #foo.
+    t2 destination: #foo.
+        
+    self assert: (state equals: anotherState).
+!
+
+testEquals3
+    state addTransition: t1.
+    anotherState addTransition: t2.
+
+    state retval: #bar.
+    anotherState retval: #baz.
+    
+    t1 destination: #foo.
+    t2 destination: #foo.
+        
+    self assert: (state equals: anotherState) not
+!
+
+testEquals4
+    state addTransition: t1.
+    anotherState addTransition: t2.
+
+    state retval: #bar.
+    anotherState retval: #bar.
+    
+    state priority: 0.
+    anotherState priority: -1.
+    
+    t1 destination: #foo.
+    t2 destination: #foo.
+        
+    self assert: (state equals: anotherState) not
+!
+
+testEquals5
+    state addTransition: t1.
+    state addTransition: t2.
+    anotherState addTransition: t2.
+    anotherState addTransition: t3.
+
+    state retval: #bar.
+    anotherState retval: #bar.
+    
+    state priority: -1.
+    anotherState priority: -1.
+    
+    t1 destination: #foobar.
+    t2 destination: #foo.
+    t3 destination: #foobar.
+        
+    self assert: (state equals: anotherState)
+!
+
+testEquals6
+    state addTransition: t1.
+    state addTransition: t2.
+    anotherState addTransition: t1.
+
+    state retval: #bar.
+    anotherState retval: #bar.
+    
+    state priority: -1.
+    anotherState priority: -1.
+    
+    t1 destination: #foo.
+    t2 destination: #bar.
+        
+    self assert: (state equals: anotherState) not
+!
+
+testJoin
+    | newState |
+    state addTransition: t1.
+    anotherState addTransition: t2.
+    state final: true.
+    
+    t1 destination: #t1.
+    t2 destination: #t2.
+    
+    newState := state join: anotherState.
+    
+    self assert: (newState transitions contains: [ :t | t = t1 ]).
+    self assert: (newState transitions contains: [ :t | t = t2 ]).
+    self assert: (newState isFinal).	
+!
+
+testJoin2
+    | newState |
+    state addTransition: t1.
+    anotherState addTransition: t2.
+    state final: true.
+    
+    t1 destination: #t1.
+    t2 destination: #t2.
+    
+    newState := anotherState join: state.
+    
+    self assert: (newState transitions contains: [ :t | t = t1 ]).
+    self assert: (newState transitions contains: [ :t | t = t2 ]).
+    self assert: (newState isFinal).	
+!
+
+testTransitionPairs
+    state addTransition: t1.
+    state addTransition: t2.
+    state addTransition: t3.
+    
+    self assert: state transitions size = 3.
+    self assert: state transitionPairs size = 3.
+    self assert: (state transitionPairs includes: (PEGFsaPair with: t1 with: t2)).
+    self assert: (state transitionPairs includes: (PEGFsaPair with: t1 with: t3)).
+    self assert: (state transitionPairs includes: (PEGFsaPair with: t2 with: t3)).		
+! !
+