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
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
78
85
91
98
104
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
147
153
155 with etree.xmlfile(self._file) as xf:
156 with xf.element('test'):
157 xf.write('Comments: <!-- text -->\n')
158 xf.write('Entities: &')
159 self.assertXml(
160 '<test>Comments: <!-- text -->\nEntities: &amp;</test>')
161
167
169 with etree.xmlfile(self._file, buffered=False) as xf:
170 with xf.element('test'):
171 self.assertXml("<test>")
172 xf.write('toast')
173 self.assertXml("<test>toast")
174 with xf.element('taste'):
175 self.assertXml("<test>toast<taste>")
176 xf.write('some', etree.Element("more"), "toast")
177 self.assertXml("<test>toast<taste>some<more/>toast")
178 self.assertXml("<test>toast<taste>some<more/>toast</taste>")
179 xf.write('end')
180 self.assertXml("<test>toast<taste>some<more/>toast</taste>end")
181 self.assertXml("<test>toast<taste>some<more/>toast</taste>end</test>")
182 self.assertXml("<test>toast<taste>some<more/>toast</taste>end</test>")
183
197
199 try:
200 with etree.xmlfile(self._file) as xf:
201 xf.write('toast')
202 except etree.LxmlSyntaxError:
203 self.assertTrue(True)
204 else:
205 self.assertTrue(False)
206
208 with etree.xmlfile(self._file) as xf:
209 with xf.element('test'):
210 pass
211 try:
212 xf.write('toast')
213 except etree.LxmlSyntaxError:
214 self.assertTrue(True)
215 else:
216 self.assertTrue(False)
217
228
230 cm_exit = None
231 try:
232 with etree.xmlfile(self._file) as xf:
233 x = xf.element('test')
234 cm_exit = x.__exit__
235 x.__enter__()
236 raise ValueError('123')
237 except ValueError:
238 self.assertTrue(cm_exit)
239 try:
240 cm_exit(ValueError, ValueError("huhu"), None)
241 except etree.LxmlSyntaxError:
242 self.assertTrue(True)
243 else:
244 self.assertTrue(False)
245 else:
246 self.assertTrue(False)
247
249 pos = self._file.tell()
250 self._file.seek(0)
251 try:
252 return self._file.read()
253 finally:
254 self._file.seek(pos)
255
263
267
268 - def assertXml(self, expected, encoding='utf8'):
270
274 self._file = BytesIO()
275
281
285 self._file = tempfile.TemporaryFile()
286
290 self._tmpfile = tempfile.NamedTemporaryFile()
291 self._file = self._tmpfile.name
292
294 try:
295 self._tmpfile.close()
296 finally:
297 if os.path.exists(self._tmpfile.name):
298 os.unlink(self._tmpfile.name)
299
301 self._tmpfile.seek(0)
302 return self._tmpfile.read()
303
305 self._tmpfile.seek(0)
306 return etree.parse(self._tmpfile)
307
308 @skipIf(True, "temp file behaviour is too platform specific here")
311
312 @skipIf(True, "temp file behaviour is too platform specific here")
315
325
327 assert not self.closed
328 self.closed = True
329 self._target.close()
330
334
336 return self._target.getvalue()
337
339 pos = self._file.tell()
340 self._target.seek(0)
341 try:
342 return etree.parse(self._target)
343 finally:
344 self._target.seek(pos)
345
351
358
362 self._file = BytesIO()
363
365
366 void_elements = set([
367 "area", "base", "br", "col", "embed", "hr", "img",
368 "input", "keygen", "link", "meta", "param",
369 "source", "track", "wbr"
370 ])
371
372
373 void_elements.difference_update([
374 'area', 'embed', 'keygen', 'source', 'track', 'wbr'
375 ])
376
377 for tag in sorted(void_elements):
378 with etree.htmlfile(self._file) as xf:
379 xf.write(etree.Element(tag))
380 self.assertXml('<%s>' % tag)
381 self._file = BytesIO()
382
392
397
403
414
415 if __name__ == '__main__':
416 print('to test use test.py %s' % __file__)
417