1
2
3 """
4 Tests for the incremental XML serialisation API.
5
6 Tests require Python 2.5 or later.
7 """
8
9 from __future__ import with_statement
10
11 import unittest
12 import tempfile, os, sys
13
14 this_dir = os.path.dirname(__file__)
15 if this_dir not in sys.path:
16 sys.path.insert(0, this_dir)
17
18 from common_imports import etree, BytesIO, HelperTestCase, skipIf
21 _file = None
22
28
30 with etree.xmlfile(self._file) as xf:
31 with xf.element('test'):
32 xf.write('toast')
33 self.assertXml('<test>toast</test>')
34
36 with etree.xmlfile(self._file) as xf:
37 with xf.element('test'):
38 with xf.element('toast'):
39 with xf.element('taste'):
40 xf.write('conTent')
41 self.assertXml('<test><toast><taste>conTent</taste></toast></test>')
42
44 with etree.xmlfile(self._file) as xf:
45 with xf.element('test'):
46 xf.write('con')
47 with xf.element('toast'):
48 xf.write('tent')
49 with xf.element('taste'):
50 xf.write('inside')
51 xf.write('tnet')
52 xf.write('noc')
53 self.assertXml('<test>con<toast>tent<taste>inside</taste>'
54 'tnet</toast>noc</test>')
55
60
72
74 with etree.xmlfile(self._file) as xf:
75 with xf.element('{nsURI}test', nsmap={'x': 'nsURI'}):
76 pass
77 self.assertXml('<x:test xmlns:x="nsURI"></x:test>')
78
80 with etree.xmlfile(self._file) as xf:
81 with xf.element('test', nsmap={'x': 'nsURI'}):
82 with xf.element('{nsURI}toast'):
83 pass
84 self.assertXml('<test xmlns:x="nsURI"><x:toast></x:toast></test>')
85
87 with etree.xmlfile(self._file) as xf:
88 with xf.element('{nsURI}test'):
89 pass
90 self.assertXml('<ns0:test xmlns:ns0="nsURI"></ns0:test>')
91
93 with etree.xmlfile(self._file) as xf:
94 with xf.element('test'):
95 with xf.element('{nsURI}toast'):
96 pass
97 self.assertXml('<test><ns0:toast xmlns:ns0="nsURI"></ns0:toast></test>')
98
100 with etree.xmlfile(self._file) as xf:
101 with xf.element('{nsURI}test', nsmap={None: 'nsURI'}):
102 pass
103 self.assertXml('<test xmlns="nsURI"></test>')
104
106 with etree.xmlfile(self._file) as xf:
107 with xf.element('{nsURI}test', nsmap={None: 'nsURI'}):
108 with xf.element('{nsURI}toast'):
109 pass
110 self.assertXml('<test xmlns="nsURI"><toast></toast></test>')
111
113 with etree.xmlfile(self._file) as xf:
114 with xf.element('{nsURI}test', nsmap={None: 'nsURI', 'p': 'ns2'}):
115 with xf.element('{nsURI}toast'):
116 pass
117 with xf.element('{ns2}toast'):
118 pass
119 self.assertXml(
120 '<test xmlns="nsURI" xmlns:p="ns2"><toast></toast><p:toast></p:toast></test>')
121
128
135
141
143 with etree.xmlfile(self._file) as xf:
144 with xf.element('test'):
145 xf.write('Comments: <!-- text -->\n')
146 xf.write('Entities: &')
147 self.assertXml(
148 '<test>Comments: <!-- text -->\nEntities: &amp;</test>')
149
155
157 with etree.xmlfile(self._file, buffered=False) as xf:
158 with xf.element('test'):
159 self.assertXml("<test>")
160 xf.write('toast')
161 self.assertXml("<test>toast")
162 with xf.element('taste'):
163 self.assertXml("<test>toast<taste>")
164 xf.write('some', etree.Element("more"), "toast")
165 self.assertXml("<test>toast<taste>some<more/>toast")
166 self.assertXml("<test>toast<taste>some<more/>toast</taste>")
167 xf.write('end')
168 self.assertXml("<test>toast<taste>some<more/>toast</taste>end")
169 self.assertXml("<test>toast<taste>some<more/>toast</taste>end</test>")
170 self.assertXml("<test>toast<taste>some<more/>toast</taste>end</test>")
171
185
187 try:
188 with etree.xmlfile(self._file) as xf:
189 xf.write('toast')
190 except etree.LxmlSyntaxError:
191 self.assertTrue(True)
192 else:
193 self.assertTrue(False)
194
196 with etree.xmlfile(self._file) as xf:
197 with xf.element('test'):
198 pass
199 try:
200 xf.write('toast')
201 except etree.LxmlSyntaxError:
202 self.assertTrue(True)
203 else:
204 self.assertTrue(False)
205
216
218 cm_exit = None
219 try:
220 with etree.xmlfile(self._file) as xf:
221 x = xf.element('test')
222 cm_exit = x.__exit__
223 x.__enter__()
224 raise ValueError('123')
225 except ValueError:
226 self.assertTrue(cm_exit)
227 try:
228 cm_exit(ValueError, ValueError("huhu"), None)
229 except etree.LxmlSyntaxError:
230 self.assertTrue(True)
231 else:
232 self.assertTrue(False)
233 else:
234 self.assertTrue(False)
235
237 pos = self._file.tell()
238 self._file.seek(0)
239 try:
240 return self._file.read()
241 finally:
242 self._file.seek(pos)
243
251
255
256 - def assertXml(self, expected, encoding='utf8'):
258
262 self._file = BytesIO()
263
265 with etree.xmlfile(self._file, close=True) as xf:
266 with xf.element('test'):
267 pass
268 self.assertRaises(ValueError, self._file.getvalue)
269
273 self._file = tempfile.TemporaryFile()
274
278 self._tmpfile = tempfile.NamedTemporaryFile()
279 self._file = self._tmpfile.name
280
282 try:
283 self._tmpfile.close()
284 finally:
285 if os.path.exists(self._tmpfile.name):
286 os.unlink(self._tmpfile.name)
287
289 self._tmpfile.seek(0)
290 return self._tmpfile.read()
291
293 self._tmpfile.seek(0)
294 return etree.parse(self._tmpfile)
295
296 @skipIf(True, "temp file behaviour is too platform specific here")
299
300 @skipIf(True, "temp file behaviour is too platform specific here")
303
313
315 assert not self.closed
316 self.closed = True
317 self._target.close()
318
322
324 return self._target.getvalue()
325
327 pos = self._file.tell()
328 self._target.seek(0)
329 try:
330 return etree.parse(self._target)
331 finally:
332 self._target.seek(pos)
333
339
346
350 self._file = BytesIO()
351
353
354 void_elements = set([
355 "area", "base", "br", "col", "embed", "hr", "img",
356 "input", "keygen", "link", "meta", "param",
357 "source", "track", "wbr"
358 ])
359
360
361 void_elements.difference_update([
362 'area', 'embed', 'keygen', 'source', 'track', 'wbr'
363 ])
364
365 for tag in sorted(void_elements):
366 with etree.htmlfile(self._file) as xf:
367 xf.write(etree.Element(tag))
368 self.assertXml('<%s>' % tag)
369 self._file = BytesIO()
370
380
385
387 with etree.htmlfile(self._file) as xf:
388 with xf.element("{some_ns}some_tag"):
389 pass
390 self.assertXml('<ns0:some_tag xmlns:ns0="some_ns"></ns0:some_tag>')
391
402
403 if __name__ == '__main__':
404 print('to test use test.py %s' % __file__)
405