compiler/tests/PEGFsaStateTest.st
changeset 502 1e45d3c96ec5
child 515 b5316ef15274
equal deleted inserted replaced
464:f6d77fee9811 502:1e45d3c96ec5
       
     1 "{ Package: 'stx:goodies/petitparser/compiler/tests' }"
       
     2 
       
     3 "{ NameSpace: Smalltalk }"
       
     4 
       
     5 TestCase subclass:#PEGFsaStateTest
       
     6 	instanceVariableNames:'state t1 t2 t3 t4 anotherState'
       
     7 	classVariableNames:''
       
     8 	poolDictionaries:''
       
     9 	category:'PetitCompiler-Tests-FSA'
       
    10 !
       
    11 
       
    12 !PEGFsaStateTest methodsFor:'as yet unclassified'!
       
    13 
       
    14 setUp
       
    15     state := PEGFsaState new name: #state; retval: #state; yourself.
       
    16     anotherState := PEGFsaState new name: #anotherState; retval: #anotherState; yourself.
       
    17 
       
    18     t1 := PEGFsaTransition new.
       
    19     t2 := PEGFsaTransition new.
       
    20     t3 := PEGFsaTransition new.
       
    21     t4 := PEGFsaTransition new.
       
    22     
       
    23 !
       
    24 
       
    25 testCopy
       
    26     state addTransition: t1.
       
    27     anotherState := state copy.
       
    28     
       
    29     self assert: (state = anotherState).
       
    30     self assert: (state == anotherState) not.
       
    31     
       
    32     state retval: #foo.
       
    33     self assert: (state = anotherState) not.
       
    34 
       
    35     anotherState retval: #foo.
       
    36     self assert: (state = anotherState).
       
    37     
       
    38     state addTransition: t2.
       
    39     self assert: (state = anotherState) not.
       
    40     
       
    41     anotherState addTransition: t2.
       
    42     self assert: (state = anotherState).
       
    43     
       
    44 !
       
    45 
       
    46 testCopy2
       
    47     state addTransition: t1.
       
    48     anotherState := state copy.
       
    49     
       
    50     self assert: (state = anotherState).
       
    51     self assert: (state == anotherState) not.
       
    52     
       
    53     state addTransition: t2.
       
    54     self assert: (state = anotherState) not.
       
    55     
       
    56     anotherState addTransition: t2 copy.
       
    57     self assert: (state = anotherState).
       
    58     
       
    59 !
       
    60 
       
    61 testCopy3
       
    62     state addTransition: t1.
       
    63     anotherState := state copy.
       
    64     
       
    65     self assert: (state = anotherState).
       
    66     self assert: (state == anotherState) not.
       
    67     
       
    68     t1 addCharacter: $x.
       
    69     self assert: (state = anotherState) not.
       
    70     
       
    71     anotherState transitions anyOne addCharacter: $x.	
       
    72     self assert: (state = anotherState).
       
    73     
       
    74 !
       
    75 
       
    76 testEquals
       
    77     state addTransition: t1.
       
    78     anotherState addTransition: t2.
       
    79 
       
    80     state retval: #baz.
       
    81     anotherState retval: #baz.
       
    82     
       
    83     t1 destination: #foo.
       
    84     t2 destination: #bar.
       
    85         
       
    86     self assert: (state equals: anotherState) not
       
    87 !
       
    88 
       
    89 testEquals2
       
    90     state addTransition: t1.
       
    91     anotherState addTransition: t2.
       
    92 
       
    93     state retval: #baz.
       
    94     anotherState retval: #baz.
       
    95     
       
    96     t1 destination: #foo.
       
    97     t2 destination: #foo.
       
    98         
       
    99     self assert: (state equals: anotherState).
       
   100 !
       
   101 
       
   102 testEquals3
       
   103     state addTransition: t1.
       
   104     anotherState addTransition: t2.
       
   105 
       
   106     state retval: #bar.
       
   107     anotherState retval: #baz.
       
   108     
       
   109     t1 destination: #foo.
       
   110     t2 destination: #foo.
       
   111         
       
   112     self assert: (state equals: anotherState) not
       
   113 !
       
   114 
       
   115 testEquals4
       
   116     state addTransition: t1.
       
   117     anotherState addTransition: t2.
       
   118 
       
   119     state retval: #bar.
       
   120     anotherState retval: #bar.
       
   121     
       
   122     state priority: 0.
       
   123     anotherState priority: -1.
       
   124     
       
   125     t1 destination: #foo.
       
   126     t2 destination: #foo.
       
   127         
       
   128     self assert: (state equals: anotherState) not
       
   129 !
       
   130 
       
   131 testEquals5
       
   132     state addTransition: t1.
       
   133     state addTransition: t2.
       
   134     anotherState addTransition: t2.
       
   135     anotherState addTransition: t3.
       
   136 
       
   137     state retval: #bar.
       
   138     anotherState retval: #bar.
       
   139     
       
   140     state priority: -1.
       
   141     anotherState priority: -1.
       
   142     
       
   143     t1 destination: #foobar.
       
   144     t2 destination: #foo.
       
   145     t3 destination: #foobar.
       
   146         
       
   147     self assert: (state equals: anotherState)
       
   148 !
       
   149 
       
   150 testEquals6
       
   151     state addTransition: t1.
       
   152     state addTransition: t2.
       
   153     anotherState addTransition: t1.
       
   154 
       
   155     state retval: #bar.
       
   156     anotherState retval: #bar.
       
   157     
       
   158     state priority: -1.
       
   159     anotherState priority: -1.
       
   160     
       
   161     t1 destination: #foo.
       
   162     t2 destination: #bar.
       
   163         
       
   164     self assert: (state equals: anotherState) not
       
   165 !
       
   166 
       
   167 testJoin
       
   168     | newState |
       
   169     state addTransition: t1.
       
   170     anotherState addTransition: t2.
       
   171     state final: true.
       
   172     
       
   173     t1 destination: #t1.
       
   174     t2 destination: #t2.
       
   175     
       
   176     newState := state join: anotherState.
       
   177     
       
   178     self assert: (newState transitions contains: [ :t | t = t1 ]).
       
   179     self assert: (newState transitions contains: [ :t | t = t2 ]).
       
   180     self assert: (newState isFinal).	
       
   181 !
       
   182 
       
   183 testJoin2
       
   184     | newState |
       
   185     state addTransition: t1.
       
   186     anotherState addTransition: t2.
       
   187     state final: true.
       
   188     
       
   189     t1 destination: #t1.
       
   190     t2 destination: #t2.
       
   191     
       
   192     newState := anotherState join: state.
       
   193     
       
   194     self assert: (newState transitions contains: [ :t | t = t1 ]).
       
   195     self assert: (newState transitions contains: [ :t | t = t2 ]).
       
   196     self assert: (newState isFinal).	
       
   197 !
       
   198 
       
   199 testTransitionPairs
       
   200     state addTransition: t1.
       
   201     state addTransition: t2.
       
   202     state addTransition: t3.
       
   203     
       
   204     self assert: state transitions size = 3.
       
   205     self assert: state transitionPairs size = 3.
       
   206     self assert: (state transitionPairs includes: (PEGFsaPair with: t1 with: t2)).
       
   207     self assert: (state transitionPairs includes: (PEGFsaPair with: t1 with: t3)).
       
   208     self assert: (state transitionPairs includes: (PEGFsaPair with: t2 with: t3)).		
       
   209 ! !
       
   210