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
19 from common_imports import HelperTestCase
20
22 _file = None
23
29
31 with etree.xmlfile(self._file) as xf:
32 with xf.element('test'):
33 xf.write('toast')
34 self.assertXml('<test>toast</test>')
35
37 with etree.xmlfile(self._file) as xf:
38 with xf.element('test'):
39 with xf.element('toast'):
40 with xf.element('taste'):
41 xf.write('conTent')
42 self.assertXml('<test><toast><taste>conTent</taste></toast></test>')
43
45 with etree.xmlfile(self._file) as xf:
46 with xf.element('test'):
47 xf.write('con')
48 with xf.element('toast'):
49 xf.write('tent')
50 with xf.element('taste'):
51 xf.write('inside')
52 xf.write('tnet')
53 xf.write('noc')
54 self.assertXml('<test>con<toast>tent<taste>inside</taste>'
55 'tnet</toast>noc</test>')
56
61
73
75 with etree.xmlfile(self._file) as xf:
76 with xf.element('{nsURI}test', nsmap={'x': 'nsURI'}):
77 pass
78 self.assertXml('<x:test xmlns:x="nsURI"></x:test>')
79
81 with etree.xmlfile(self._file) as xf:
82 with xf.element('test', nsmap={'x': 'nsURI'}):
83 with xf.element('{nsURI}toast'):
84 pass
85 self.assertXml('<test xmlns:x="nsURI"><x:toast></x:toast></test>')
86
88 with etree.xmlfile(self._file) as xf:
89 with xf.element('{nsURI}test'):
90 pass
91 self.assertXml('<ns0:test xmlns:ns0="nsURI"></ns0:test>')
92
94 with etree.xmlfile(self._file) as xf:
95 with xf.element('test'):
96 with xf.element('{nsURI}toast'):
97 pass
98 self.assertXml('<test><ns0:toast xmlns:ns0="nsURI"></ns0:toast></test>')
99
101 with etree.xmlfile(self._file) as xf:
102 with xf.element('{nsURI}test', nsmap={None: 'nsURI'}):
103 pass
104 self.assertXml('<test xmlns="nsURI"></test>')
105
107 with etree.xmlfile(self._file) as xf:
108 with xf.element('{nsURI}test', nsmap={None: 'nsURI'}):
109 with xf.element('{nsURI}toast'):
110 pass
111 self.assertXml('<test xmlns="nsURI"><toast></toast></test>')
112
119
126
132
134 with etree.xmlfile(self._file) as xf:
135 with xf.element('test'):
136 xf.write('Comments: <!-- text -->\n')
137 xf.write('Entities: &')
138 self.assertXml(
139 '<test>Comments: <!-- text -->\nEntities: &amp;</test>')
140
146
148 try:
149 with etree.xmlfile(self._file) as xf:
150 xf.write('toast')
151 except etree.LxmlSyntaxError:
152 self.assertTrue(True)
153 else:
154 self.assertTrue(False)
155
157 with etree.xmlfile(self._file) as xf:
158 with xf.element('test'):
159 pass
160 try:
161 xf.write('toast')
162 except etree.LxmlSyntaxError:
163 self.assertTrue(True)
164 else:
165 self.assertTrue(False)
166
177
179 cm_exit = None
180 try:
181 with etree.xmlfile(self._file) as xf:
182 x = xf.element('test')
183 cm_exit = x.__exit__
184 x.__enter__()
185 raise ValueError('123')
186 except ValueError:
187 self.assertTrue(cm_exit)
188 try:
189 cm_exit(ValueError, ValueError("huhu"), None)
190 except etree.LxmlSyntaxError:
191 self.assertTrue(True)
192 else:
193 self.assertTrue(False)
194 else:
195 self.assertTrue(False)
196
200
204
207
208 - def assertXml(self, expected, encoding='utf8'):
210
211
214 self._file = BytesIO()
215
218 self._file = tempfile.NamedTemporaryFile()
219
226
230
232 return self._target.getvalue()
233
235 self._target.seek(0)
236 return etree.parse(self._target)
237
238
246
247 if __name__ == '__main__':
248 print('to test use test.py %s' % __file__)
249