hardcoded python path in garchive and tragesym
[geda-gaf.git] / xorn / tests / python / xml_writer.py
blob3469c7befc33ca40b3a04d40f9c62b245ae6ff27
1 # Copyright (C) 2013-2020 Roland Lutz
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 import StringIO
18 import xorn.xml_writer
20 def throws(fun, *args):
21 try:
22 fun(*args)
23 except Exception as e:
24 return type(e)
26 assert throws(xorn.xml_writer.valid_name, int()) == TypeError
27 assert throws(xorn.xml_writer.valid_name, str()) == TypeError
28 assert throws(xorn.xml_writer.valid_name, unicode()) == None
30 assert xorn.xml_writer.valid_name(u'') == False
31 assert xorn.xml_writer.valid_name(u'foo') == True
32 assert xorn.xml_writer.valid_name(u'f\xf6\xf6') == True
33 assert xorn.xml_writer.valid_name(u'!foo') == False
34 assert xorn.xml_writer.valid_name(u'foo!') == False
35 assert xorn.xml_writer.valid_name(u'-foo') == False
36 assert xorn.xml_writer.valid_name(u'foo-') == True
38 assert throws(xorn.xml_writer.escape, int()) == TypeError
39 assert throws(xorn.xml_writer.escape, str()) == TypeError
40 assert throws(xorn.xml_writer.escape, unicode()) == None
42 assert xorn.xml_writer.escape(u'"&<>foo') == u'&quot;&amp;&lt;&gt;foo'
44 FIRST_LINE = u'<?xml version="1.0" encoding="UTF-8"?>'
46 def perform_test(*commands):
47 f = StringIO.StringIO()
48 w = xorn.xml_writer.XMLWriter(f.write)
50 for command in commands:
51 assert not w.is_done()
52 getattr(w, command[0])(*command[1:])
54 assert w.is_done()
55 data = f.getvalue()
56 f.close()
58 assert data.startswith(FIRST_LINE)
59 assert data.endswith('\n')
60 return data[len(FIRST_LINE):]
62 # nothing written
63 assert throws(perform_test,
64 ) == AssertionError
66 # not all elements closed
67 assert throws(perform_test,
68 ('start_element', 'root'),
69 ) == AssertionError
71 # closing element at root level
72 assert throws(perform_test,
73 ('end_element', ),
74 ) == ValueError
76 assert perform_test(
77 ('start_element', 'root'),
78 ('end_element', )
79 ) == '''
80 <root/>
81 '''
83 # invalid element name
84 assert throws(perform_test,
85 ('start_element', '"root"'),
86 ('end_element', )
87 ) == ValueError
89 # multiple root elements
90 f = StringIO.StringIO()
91 w = xorn.xml_writer.XMLWriter(f.write)
92 w.start_element('root')
93 w.end_element()
94 assert throws(w.start_element, 'root') == ValueError
95 f.close()
97 assert perform_test(
98 ('start_element', 'root'),
99 ('start_element', 'foo'),
100 ('end_element', ),
101 ('end_element', )
102 ) == '''
103 <root>
104 <foo/>
105 </root>
108 assert perform_test(
109 ('start_element', 'root', False),
110 ('start_element', 'foo'),
111 ('end_element', ),
112 ('end_element', )
113 ) == '''
114 <root>
115 <foo/>
116 </root>
119 assert perform_test(
120 ('start_element', 'root', True),
121 ('start_element', 'foo'),
122 ('end_element', ),
123 ('end_element', )
124 ) == '''
125 <root><foo/></root>
128 ### writing attributes ###
130 # attribute outside of element
131 assert throws(perform_test,
132 ('write_attribute', 'foo', 'bar'),
133 ) == ValueError
135 assert perform_test(
136 ('start_element', 'root'),
137 ('write_attribute', 'foo', 'bar'),
138 ('end_element', )
139 ) == '''
140 <root foo="bar"/>
143 assert perform_test(
144 ('start_element', 'root'),
145 ('write_attribute', 'foo', 'bar'),
146 ('write_attribute', 'bar', 'baz'),
147 ('end_element', )
148 ) == '''
149 <root foo="bar" bar="baz"/>
152 # duplicate attribute
153 assert throws(perform_test,
154 ('start_element', 'root'),
155 ('write_attribute', 'foo', 'bar'),
156 ('write_attribute', 'foo', 'bar'),
157 ('end_element', )
158 ) == ValueError
160 # invalid attribute name
161 assert throws(perform_test,
162 ('start_element', 'root'),
163 ('write_attribute', '"foo"', 'bar'),
164 ('end_element', )
165 ) == ValueError
167 assert perform_test(
168 ('start_element', 'root'),
169 ('write_attribute', 'foo', '"bar"'),
170 ('end_element', )
171 ) == '''
172 <root foo="&quot;bar&quot;"/>
175 # attribute after character data
176 assert throws(perform_test,
177 ('start_element', 'root'),
178 ('write_character_data', ''),
179 ('write_attribute', 'foo', 'bar'),
180 ('end_element', )
181 ) == ValueError
183 assert perform_test(
184 ('start_element', 'root'),
185 ('write_attribute', 'foo', 'bar'),
186 ('start_element', 'baz'),
187 ('end_element', ),
188 ('end_element', )
189 ) == '''
190 <root foo="bar">
191 <baz/>
192 </root>
195 assert perform_test(
196 ('start_element', 'root'),
197 ('start_element', 'foo'),
198 ('write_attribute', 'bar', 'baz'),
199 ('end_element', ),
200 ('end_element', )
201 ) == '''
202 <root>
203 <foo bar="baz"/>
204 </root>
207 ### writing character data ###
209 assert perform_test(
210 ('start_element', 'root'),
211 ('write_character_data', 'foo'),
212 ('write_character_data', 'bar'),
213 ('start_element', 'baz'),
214 ('end_element', ),
215 ('end_element', )
216 ) == '''
217 <root>
218 foobar
219 <baz/>
220 </root>
223 assert perform_test(
224 ('start_element', 'root'),
225 ('write_character_data', 'foo'),
226 ('start_element', 'bar'),
227 ('write_character_data', 'baz'),
228 ('end_element', ),
229 ('end_element', )
230 ) == '''
231 <root>
233 <bar>
235 </bar>
236 </root>
239 assert perform_test(
240 ('start_element', 'root'),
241 ('start_element', 'foo'),
242 ('write_character_data', 'bar'),
243 ('write_character_data', 'baz'),
244 ('end_element', ),
245 ('end_element', )
246 ) == '''
247 <root>
248 <foo>
249 barbaz
250 </foo>
251 </root>
254 assert perform_test(
255 ('start_element', 'root'),
256 ('start_element', 'foo'),
257 ('write_character_data', 'bar'),
258 ('end_element', ),
259 ('write_character_data', 'baz'),
260 ('end_element', )
261 ) == '''
262 <root>
263 <foo>
265 </foo>
267 </root>
270 assert perform_test(
271 ('start_element', 'root'),
272 ('start_element', 'foo'),
273 ('end_element', ),
274 ('write_character_data', 'bar'),
275 ('write_character_data', 'baz'),
276 ('end_element', )
277 ) == '''
278 <root>
279 <foo/>
280 barbaz
281 </root>
284 assert perform_test(
285 ('start_element', 'root'),
286 ('write_character_data', 'foo'),
287 ('start_element', 'bar'),
288 ('end_element', ),
289 ('write_character_data', 'baz'),
290 ('end_element', )
291 ) == '''
292 <root>
294 <bar/>
296 </root>
299 assert perform_test(
300 ('start_element', 'root'),
301 ('write_character_data', 'foo&bar'),
302 ('end_element', )
303 ) == '''
304 <root>
305 foo&amp;bar
306 </root>
309 assert perform_test(
310 ('start_element', 'root'),
311 ('write_character_data', 'foo'),
312 ('start_element', 'element'),
313 ('write_character_data', 'bar'),
314 ('end_element', ),
315 ('write_character_data', 'baz'),
316 ('end_element', )
317 ) == '''
318 <root>
320 <element>
322 </element>
324 </root>
327 assert perform_test(
328 ('start_element', 'root', True),
329 ('write_character_data', 'foo'),
330 ('start_element', 'element'),
331 ('write_character_data', 'bar'),
332 ('end_element', ),
333 ('write_character_data', 'baz'),
334 ('end_element', )
335 ) == '''
336 <root>foo<element>bar</element>baz</root>
339 assert perform_test(
340 ('start_element', 'root'),
341 ('write_character_data', 'foo'),
342 ('start_element', 'element', True),
343 ('write_character_data', 'bar'),
344 ('end_element', ),
345 ('write_character_data', 'baz'),
346 ('end_element', )
347 ) == '''
348 <root>
350 <element>bar</element>
352 </root>
355 # character data before root element
356 assert throws(perform_test,
357 ('write_character_data', 'foo'),
358 ('start_element', 'root'),
359 ('end_element', )
360 ) == ValueError
362 # character data after root element
363 f = StringIO.StringIO()
364 w = xorn.xml_writer.XMLWriter(f.write)
365 w.start_element('root')
366 w.end_element()
367 assert throws(w.write_character_data, 'foo') == ValueError
368 f.close()
370 ### writing CDATA section ###
372 assert perform_test(
373 ('start_element', 'root'),
374 ('write_cdata_section', 'foo'),
375 ('end_element', )
376 ) == '''
377 <root>
378 <![CDATA[foo]]>
379 </root>
382 assert perform_test(
383 ('start_element', 'root'),
384 ('write_cdata_section', '<![CDATA[foo]]>'),
385 ('end_element', )
386 ) == '''
387 <root>
388 <![CDATA[<![CDATA[foo]]>]]&gt;<![CDATA[]]>
389 </root>
392 # CDATA section before root element
393 assert throws(perform_test,
394 ('write_cdata_section', 'foo'),
395 ('start_element', 'root'),
396 ('end_element', )
397 ) == ValueError
399 # CDATA section after root element
400 f = StringIO.StringIO()
401 w = xorn.xml_writer.XMLWriter(f.write)
402 w.start_element('root')
403 w.end_element()
404 assert throws(w.write_cdata_section, 'foo') == ValueError
405 f.close()