islands/tests/XmlFeedParserTest.st
changeset 387 e2b2ccaa4de6
child 389 009c2e13973c
equal deleted inserted replaced
386:a409905f7f2d 387:e2b2ccaa4de6
       
     1 "{ Package: 'stx:goodies/petitparser/islands/tests' }"
       
     2 
       
     3 PPCompositeParserTest subclass:#XmlFeedParserTest
       
     4 	instanceVariableNames:'debugResult'
       
     5 	classVariableNames:''
       
     6 	poolDictionaries:''
       
     7 	category:'PetitIslands-Examples'
       
     8 !
       
     9 
       
    10 XmlFeedParserTest comment:''
       
    11 !
       
    12 
       
    13 !XmlFeedParserTest methodsFor:'as yet unclassified'!
       
    14 
       
    15 debug: aString rule: aSymbol
       
    16 	| production |
       
    17 	production := self parserInstanceFor: aSymbol.
       
    18 	debugResult := production end debug: aString.
       
    19 	result := debugResult children first result.
       
    20 	self
       
    21 		deny: result isPetitFailure
       
    22 		description: 'Unable to parse ' , aString printString.
       
    23 	^ result
       
    24 !
       
    25 
       
    26 feed01
       
    27 ^'
       
    28 <shoplist>
       
    29 <name>ABC Shop</name>
       
    30 <address>Here and there 123, 123 45 Somewhere</address>
       
    31 <item>
       
    32   <name>socks</name>
       
    33   <price>123</price>
       
    34   <availability>1</availability>
       
    35 </item>
       
    36 </shoplist>
       
    37 '
       
    38 !
       
    39 
       
    40 feed02
       
    41 ^'
       
    42 <shoplist>
       
    43 <name>ABC Shop</name>
       
    44 <address>Here and there 123, 123 45 Somewhere</address>
       
    45 <item>
       
    46   <name>socks</name>
       
    47   <price>123</price>
       
    48   <availability>1</availability>
       
    49 </item>
       
    50 
       
    51 
       
    52 <item>
       
    53   <name> shoes </name>
       
    54   <price>2345</price>
       
    55   <availability>1</availability>
       
    56 </item>
       
    57 
       
    58 </shoplist>
       
    59 '
       
    60 !
       
    61 
       
    62 feed03
       
    63 ^'
       
    64 <shoplist>
       
    65 <name>ABC Shop</name>
       
    66 <address>Here and there 123, 123 45 Somewhere</address>
       
    67 <item>
       
    68   <name>socks</name>
       
    69   <price>123</price>
       
    70   <availability>1</availability>
       
    71 </item>
       
    72 
       
    73 
       
    74 <item>
       
    75   <name> shoes </name>
       
    76   <price>2345</price>
       
    77   <!!-- this one is malformed -->
       
    78   <availability>1 </
       
    79 </item>
       
    80 
       
    81 <item>
       
    82   <name> shoes </name>
       
    83   <price>3456</price>
       
    84   <availability>0</availability>
       
    85 </item>
       
    86 
       
    87 
       
    88 </shoplist>
       
    89 '
       
    90 !
       
    91 
       
    92 parse: aString rule: aSymbol to: anObject
       
    93 	| production |
       
    94 	production := self parserInstanceFor: aSymbol.
       
    95 	result := production end parse: aString.
       
    96 	self
       
    97 		deny: result isPetitFailure
       
    98 		description: 'Unable to parse ' , aString printString.
       
    99 	self assert: result = anObject.
       
   100 	
       
   101 	^ result
       
   102 !
       
   103 
       
   104 parserClass
       
   105 	^ XmlFeedParser
       
   106 !
       
   107 
       
   108 testItem
       
   109 	self testItem01.
       
   110 	self testItem02.
       
   111 	self testItem03.
       
   112 	
       
   113 !
       
   114 
       
   115 testItem01
       
   116 	self debug: '
       
   117 	<item>
       
   118 		<name>abc</name>
       
   119 	</item>
       
   120 	' rule: #item.
       
   121 	
       
   122 	self assert: (result at: #name) = 'abc'.
       
   123 	
       
   124 !
       
   125 
       
   126 testItem02
       
   127 	self debug: '
       
   128 	<item>
       
   129 		<name>abc</name>
       
   130 		<price>123</price>
       
   131 	</item>
       
   132 	' rule: #item.
       
   133 	
       
   134 	self assert: (result at: #name) = 'abc'.
       
   135 	self assert: (result at: #price) = '123'.
       
   136 	
       
   137 !
       
   138 
       
   139 testItem03
       
   140 	self debug: '
       
   141 	<item>
       
   142 		<price>123</price>
       
   143 		<name>abc</name>
       
   144 	</item>
       
   145 	' rule: #item.
       
   146 	
       
   147 	self assert: (result at: #name) = 'abc'.
       
   148 	self assert: (result at: #price) = '123'.
       
   149 	
       
   150 !
       
   151 
       
   152 testShoplist
       
   153 	self testShoplist01.
       
   154 	self testShoplist02.
       
   155 	self testShoplist03.
       
   156 !
       
   157 
       
   158 testShoplist01
       
   159 	self debug: '
       
   160 <shoplist>
       
   161 	<item>
       
   162 		<name>abc</name>
       
   163 	</item>
       
   164 </shoplist>
       
   165 	' rule: #shoplist.
       
   166 	
       
   167 	self assert: result size = 1.
       
   168 	
       
   169 !
       
   170 
       
   171 testShoplist02
       
   172 	self debug: '
       
   173 <shoplist>
       
   174 	<name>xyz</name>
       
   175 	<item>
       
   176 		<name>abc</name>
       
   177 		<price>123</price>
       
   178 	</item>
       
   179 </shoplist>
       
   180 	' rule: #shoplist.
       
   181 	
       
   182 	self assert: result size = 1.
       
   183 	
       
   184 !
       
   185 
       
   186 testShoplist03
       
   187 	self debug: '
       
   188 <shoplist>
       
   189 	<name>xyz</name>
       
   190 	<item>
       
   191 		<name>abc</name>
       
   192 		<price>123</price>
       
   193 	</item>
       
   194 	<item>
       
   195 		<name>cde</name>
       
   196 		<price>345</price>
       
   197 	</item>
       
   198 </shoplist>
       
   199 	' rule: #shoplist.
       
   200 	
       
   201 	self assert: result size = 2.
       
   202 	
       
   203 !
       
   204 
       
   205 testSimpleElement
       
   206 	self parse: '<a>b</a>' rule: #simpleElement to: #('a' 'b').	
       
   207 	self parse: '<abc> def </ abc>' rule: #simpleElement to: #('abc' 'def').	
       
   208 		
       
   209 	self fail: '<a>b</b>' rule: #simpleElement.
       
   210 	
       
   211 !
       
   212 
       
   213 testStringValue
       
   214 	self parse: 'abc' rule: #stringValue to: 'abc'.	
       
   215 	self parse: ' def ' rule: #stringValue to: 'def'.
       
   216 	
       
   217 !
       
   218 
       
   219 testXmlFeed01
       
   220 	self parse: self feed01.
       
   221 	
       
   222 	self assert: result size = 1.
       
   223 	self assert: (result first at:#name) = 'socks'.
       
   224 	self assert: (result first at:#price) = '123'.
       
   225 	self assert: (result first at:#availability) = '1'.
       
   226 	
       
   227 !
       
   228 
       
   229 testXmlFeed02
       
   230 	self parse: self feed02.
       
   231 	
       
   232 	self assert: result size = 2.
       
   233 	self assert: (result first at:#name) = 'socks'.
       
   234 	self assert: (result first at:#price) = '123'.
       
   235 	self assert: (result first at:#availability) = '1'.
       
   236 
       
   237 	self assert: (result second at:#name) = 'shoes'.
       
   238 	self assert: (result second at:#price) = '2345'.
       
   239 	self assert: (result second at:#availability) = '1'.
       
   240 	
       
   241 !
       
   242 
       
   243 testXmlFeed03
       
   244 	self parse: self feed03.
       
   245 	
       
   246 	self assert: result size = 2.
       
   247 	self assert: (result first at:#name) = 'socks'.
       
   248 	self assert: (result first at:#price) = '123'.
       
   249 	self assert: (result first at:#availability) = '1'.
       
   250 
       
   251 	self assert: (result second at:#name) = 'shoes'.
       
   252 	self assert: (result second at:#price) = '3456'.
       
   253 	self assert: (result second at:#availability) = '0'.
       
   254 	
       
   255 ! !
       
   256