lxml.tests.test_sax
1
2
3 """
4 Test cases related to SAX I/O
5 """
6
7 import unittest , sys , os . path
8
9 this_dir = os . path . dirname ( __file__ )
10 if this_dir not in sys . path :
11 sys . path . insert ( 0 , this_dir )
12
13 from common_imports import HelperTestCase , make_doctest , BytesIO , _bytes
14 from lxml import sax
15 from xml . dom import pulldom
16
17
19
21 tree = self . parse ( '<a>ab<b/>ba</a>' )
22 xml_out = self . _saxify_serialize ( tree )
23 self . assertEqual ( _bytes ( '<a>ab<b/>ba</a>' ) ,
24 xml_out )
25
27 tree = self . parse ( '<a>ab<b>bb</b>ba</a>' )
28 xml_out = self . _saxify_serialize ( tree )
29 self . assertEqual ( _bytes ( '<a>ab<b>bb</b>ba</a>' ) ,
30 xml_out )
31
37
39 tree = self . parse ( '<a>ab<?this and that?>ba</a>' )
40 xml_out = self . _saxify_serialize ( tree )
41 self . assertEqual ( _bytes ( '<a>ab<?this and that?>ba</a>' ) ,
42 xml_out )
43
49
51 tree = self . parse ( '<?this and that?><a>ab</a>' )
52 xml_out = self . _saxify_serialize ( tree )
53 self . assertEqual ( _bytes ( '<?this and that?><a>ab</a>' ) ,
54 xml_out )
55
57 tree = self . parse ( '<a aa="5">ab<b b="5"/>ba</a>' )
58 xml_out = self . _saxify_serialize ( tree )
59 self . assertEqual ( _bytes ( '<a aa="5">ab<b b="5"/>ba</a>' ) ,
60 xml_out )
61
70
72 tree = self . parse ( '<a xmlns="blaA">ab<b:b xmlns:b="blaB">bb</b:b>ba</a>' )
73 new_tree = self . _saxify_unsaxify ( tree )
74 root = new_tree . getroot ( )
75 self . assertEqual ( '{blaA}a' ,
76 root . tag )
77 self . assertEqual ( '{blaB}b' ,
78 root [ 0 ] . tag )
79
81 tree = self . parse ( '<a xmlns="blaA">ab<b:b xmlns:b="blaB">bb</b:b>ba</a>' )
82 handler = pulldom . SAX2DOM ( )
83 sax . saxify ( tree , handler )
84 dom = handler . document
85
86 self . assertEqual ( 'a' ,
87 dom . firstChild . localName )
88 self . assertEqual ( 'blaA' ,
89 dom . firstChild . namespaceURI )
90
91 children = dom . firstChild . childNodes
92 self . assertEqual ( 'ab' ,
93 children [ 0 ] . nodeValue )
94 self . assertEqual ( 'blaB' ,
95 children [ 1 ] . namespaceURI )
96 self . assertEqual ( 'ba' ,
97 children [ 2 ] . nodeValue )
98
111
130
132 handler = sax . ElementTreeContentHandler ( )
133 handler . startDocument ( )
134 handler . startPrefixMapping ( None , 'blaA' )
135 handler . startElementNS ( ( 'blaA' , 'a' ) , 'a' , { } )
136 handler . startPrefixMapping ( None , 'blaB' )
137 handler . startElementNS ( ( 'blaB' , 'b' ) , 'b' , { } )
138 handler . endElementNS ( ( 'blaB' , 'b' ) , 'b' )
139 handler . endPrefixMapping ( None )
140 handler . startElementNS ( ( 'blaA' , 'c' ) , 'c' , { } )
141 handler . endElementNS ( ( 'blaA' , 'c' ) , 'c' )
142 handler . endElementNS ( ( 'blaA' , 'a' ) , 'a' )
143 handler . endPrefixMapping ( None )
144 handler . endDocument ( )
145
146 new_tree = handler . etree
147 root = new_tree . getroot ( )
148 self . assertEqual ( '{blaA}a' ,
149 root . tag )
150 self . assertEqual ( '{blaB}b' ,
151 root [ 0 ] . tag )
152 self . assertEqual ( '{blaA}c' ,
153 root [ 1 ] . tag )
154
156 handler = sax . ElementTreeContentHandler ( )
157 handler . startDocument ( )
158 handler . startPrefixMapping ( None , 'blaA' )
159 handler . startElementNS ( ( None , 'a' ) , 'a' , { } )
160 handler . startPrefixMapping ( None , 'blaB' )
161 handler . startElementNS ( ( None , 'b' ) , 'b' , { } )
162 handler . endElementNS ( ( None , 'b' ) , 'b' )
163 handler . endPrefixMapping ( None )
164 handler . startElementNS ( ( None , 'c' ) , 'c' , { } )
165 handler . endElementNS ( ( None , 'c' ) , 'c' )
166 handler . endElementNS ( ( None , 'a' ) , 'a' )
167 handler . endPrefixMapping ( None )
168 handler . endDocument ( )
169
170 new_tree = handler . etree
171 root = new_tree . getroot ( )
172 self . assertEqual ( '{blaA}a' ,
173 root . tag )
174 self . assertEqual ( '{blaB}b' ,
175 root [ 0 ] . tag )
176 self . assertEqual ( '{blaA}c' ,
177 root [ 1 ] . tag )
178
180 handler = sax . ElementTreeContentHandler ( )
181 handler . startDocument ( )
182 handler . startPrefixMapping ( 'ns' , 'blaA' )
183 handler . startElementNS ( ( 'blaA' , 'a' ) , 'ns:a' , { } )
184 handler . startPrefixMapping ( 'ns' , 'blaB' )
185 handler . startElementNS ( ( 'blaB' , 'b' ) , 'ns:b' , { } )
186 handler . endElementNS ( ( 'blaB' , 'b' ) , 'ns:b' )
187 handler . endPrefixMapping ( 'ns' )
188 handler . startElementNS ( ( 'blaA' , 'c' ) , 'ns:c' , { } )
189 handler . endElementNS ( ( 'blaA' , 'c' ) , 'ns:c' )
190 handler . endElementNS ( ( 'blaA' , 'a' ) , 'ns:a' )
191 handler . endPrefixMapping ( 'ns' )
192 handler . endDocument ( )
193
194 new_tree = handler . etree
195 root = new_tree . getroot ( )
196 self . assertEqual ( '{blaA}a' ,
197 root . tag )
198 self . assertEqual ( '{blaB}b' ,
199 root [ 0 ] . tag )
200 self . assertEqual ( '{blaA}c' ,
201 root [ 1 ] . tag )
202
219
235
244
250
257
262
268
269
271 suite = unittest . TestSuite ( )
272 suite . addTests ( [ unittest . makeSuite ( ETreeSaxTestCase ) ] )
273 suite . addTests (
274 [ make_doctest ( '../../../doc/sax.txt' ) ] )
275 return suite
276
277
278 if __name__ == '__main__' :
279 print ( 'to test use test.py %s' % __file__ )
280