compiler/tests/FooScannerTest.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:#FooScannerTest
       
     6 	instanceVariableNames:'scanner result'
       
     7 	classVariableNames:''
       
     8 	poolDictionaries:''
       
     9 	category:'PetitCompiler-Tests-Scanner'
       
    10 !
       
    11 
       
    12 !FooScannerTest methodsFor:'as yet unclassified'!
       
    13 
       
    14 fail: stream rule: rule 
       
    15     scanner initialize.
       
    16     scanner stream: stream asPetitStream. 
       
    17     result := scanner perform: rule.
       
    18         
       
    19     self assert: result isEmpty
       
    20 !
       
    21 
       
    22 fail: stream token: token rule: rule
       
    23     self fail: stream token: token rule: rule position: stream size
       
    24 !
       
    25 
       
    26 fail: stream token: token rule: rule position: position
       
    27     scanner initialize.
       
    28     scanner stream: stream asPetitStream. 
       
    29     result := scanner perform: rule.
       
    30         
       
    31     self assert: (result at: token ifAbsent: [nil]) isNil.
       
    32 !
       
    33 
       
    34 parse: stream token: token rule: rule
       
    35     self parse: stream token: token rule: rule position: stream size.
       
    36 !
       
    37 
       
    38 parse: stream token: token rule: rule position: position
       
    39     scanner initialize.
       
    40     scanner stream: stream asPetitStream. 
       
    41     result := scanner perform: rule.
       
    42     
       
    43     self assert: (result includesKey: token).	
       
    44     self assert: (result at: token) = position.
       
    45 !
       
    46 
       
    47 setUp
       
    48     scanner := FooScanner new.
       
    49 !
       
    50 
       
    51 testA
       
    52     self parse: 'aaa' token: #a rule: #nextTokenA position: 1.
       
    53 !
       
    54 
       
    55 testAAorA
       
    56     self fail: 	'a' token: #aa rule: #nextTokenAAorA.
       
    57     self parse: 	'aa' token: #aa rule: #nextTokenAAorA.
       
    58     self parse: 	'aaa' token: #aa rule: #nextTokenAAorA position: 2.
       
    59 
       
    60     self parse: 	'a' token: #a rule: #nextTokenAAorA.
       
    61     self fail: 	'aa' token: #a rule: #nextTokenAAorA.
       
    62     self fail: 	'aaa' token: #a rule: #nextTokenAAorA.
       
    63 
       
    64     self fail: 'b' rule: #nextTokenAAorA.		
       
    65 !
       
    66 
       
    67 testAAplusA
       
    68     self parse: 'aaa' token: #AAplusA rule: #nextTokenAAplusA.
       
    69     self parse: 'aaaaa' token: #AAplusA rule: #nextTokenAAplusA.
       
    70 
       
    71     self fail: '' rule: #nextTokenAAplusA.
       
    72     self fail: 'a' rule: #nextTokenAAplusA.
       
    73     self fail: 'aa' rule: #nextTokenAAplusA.
       
    74     self fail: 'aaaa' rule: #nextTokenAAplusA.	
       
    75 !
       
    76 
       
    77 testAAstarA
       
    78     self parse: 'a' token: #AAstarA rule: #nextTokenAAstarA.
       
    79     self parse: 'aaa' token: #AAstarA rule: #nextTokenAAstarA.
       
    80     self parse: 'aaaaa' token: #AAstarA rule: #nextTokenAAstarA.
       
    81 
       
    82     self fail: '' rule: #nextTokenAAstarA.
       
    83     self fail: 'aa' rule: #nextTokenAAstarA.
       
    84     self fail: 'aaaa' rule: #nextTokenAAstarA.
       
    85 !
       
    86 
       
    87 testAB
       
    88     self parse: 'ab' token: #b rule: #nextTokenAB position: 2.
       
    89 !
       
    90 
       
    91 testABorBC
       
    92     self parse: 'ab' token: #ab rule: #nextTokenABorBC position: 2.
       
    93     self parse: 'bc' token: #bc rule: #nextTokenABorBC position: 2.
       
    94     
       
    95     self fail: 'ac' rule: #nextTokenABorBC.
       
    96 !
       
    97 
       
    98 testABstarA
       
    99     self parse: 'a' token: #ABstarA rule: #nextTokenABstarA position: 1.
       
   100     self parse: 'aa' token: #ABstarA rule: #nextTokenABstarA position: 1.
       
   101     self parse: 'aba' token: #ABstarA rule: #nextTokenABstarA position: 3.
       
   102     self parse: 'abaa' token: #ABstarA rule: #nextTokenABstarA position: 3.
       
   103     self parse: 'ababa' token: #ABstarA rule: #nextTokenABstarA position: 5.
       
   104 
       
   105     self fail: 'ab' rule: #nextTokenABstarA.
       
   106     self fail: 'abab' rule: #nextTokenABstarA.
       
   107 
       
   108     self fail: '' rule: #nextTokenABstarA.
       
   109 
       
   110 !
       
   111 
       
   112 testA_Bstar_A
       
   113     self parse: 'aa' token: #A_Bstar_A rule: #nextTokenA_Bstar_A.
       
   114     self parse: 'aba' token: #A_Bstar_A rule: #nextTokenA_Bstar_A.
       
   115 
       
   116     self fail: '' rule: #nextTokenABstarA.	
       
   117     self fail: 'ab' rule: #nextTokenABstarA.
       
   118 !
       
   119 
       
   120 testAorAA
       
   121     self fail: 	'a' token: #aa rule: #nextTokenAorAA.
       
   122     self fail: 	'aa' token: #aa rule: #nextTokenAorAA.
       
   123     self fail: 	'aaa' token: #aa rule: #nextTokenAorAA.
       
   124 
       
   125     self parse: 	'a' token: #a rule: #nextTokenAorAA position: 1.
       
   126     self parse: 	'aa' token: #a rule: #nextTokenAorAA position: 1.
       
   127     self parse: 	'aaa' token: #a rule: #nextTokenAorAA position: 1.
       
   128 
       
   129     self fail: 'b' rule: #nextTokenAAorA.		
       
   130 !
       
   131 
       
   132 testAorB
       
   133     self parse: 'a' token: #a rule: #nextTokenAorB.
       
   134     self parse: 'b' token: #b rule: #nextTokenAorB.	
       
   135 
       
   136     self parse: 'ab' token: #a rule: #nextTokenAorB position: 1.	
       
   137     self fail: 'c' rule: #nextTokenAorB.	
       
   138     self fail: 'c' rule: #nextTokenAorB.		
       
   139 !
       
   140 
       
   141 testAstarA
       
   142     self fail: '' rule: #nextTokenAstarA.
       
   143     self fail: 'a' rule: #nextTokenAstarA.
       
   144     self fail: 'aa' rule: #nextTokenAstarA.
       
   145     self fail: 'aaa' rule: #nextTokenAstarA.
       
   146 !
       
   147 
       
   148 testAstarB
       
   149     self parse: 'ab' token: #AstarB rule: #nextTokenAstarB.
       
   150     self parse: 'b' token: #AstarB rule: #nextTokenAstarB.
       
   151     self parse: 'aaab' token: #AstarB rule: #nextTokenAstarB.
       
   152 
       
   153     self fail: 'c' rule: #nextTokenAstarB.	
       
   154 !
       
   155 
       
   156 testAuorA
       
   157     self parse: 'a' token: #a1 rule: #nextTokenAuorA.
       
   158     self parse: 'a' token: #a2 rule: #nextTokenAuorA.
       
   159 
       
   160     self fail: 'b' rule: #nextTokenAuorA.	
       
   161 ! !
       
   162