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.
18 import xorn
.xml_writer
20 def throws(fun
, *args
):
23 except Exception as 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
'"&<>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:])
58 assert data
.startswith(FIRST_LINE
)
59 assert data
.endswith('\n')
60 return data
[len(FIRST_LINE
):]
63 assert throws(perform_test
,
66 # not all elements closed
67 assert throws(perform_test
,
68 ('start_element', 'root'),
71 # closing element at root level
72 assert throws(perform_test
,
77 ('start_element', 'root'),
83 # invalid element name
84 assert throws(perform_test
,
85 ('start_element', '"root"'),
89 # multiple root elements
90 f
= StringIO
.StringIO()
91 w
= xorn
.xml_writer
.XMLWriter(f
.write
)
92 w
.start_element('root')
94 assert throws(w
.start_element
, 'root') == ValueError
98 ('start_element', 'root'),
99 ('start_element', 'foo'),
109 ('start_element', 'root', False),
110 ('start_element', 'foo'),
120 ('start_element', 'root', True),
121 ('start_element', 'foo'),
128 ### writing attributes ###
130 # attribute outside of element
131 assert throws(perform_test
,
132 ('write_attribute', 'foo', 'bar'),
136 ('start_element', 'root'),
137 ('write_attribute', 'foo', 'bar'),
144 ('start_element', 'root'),
145 ('write_attribute', 'foo', 'bar'),
146 ('write_attribute', 'bar', 'baz'),
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'),
160 # invalid attribute name
161 assert throws(perform_test
,
162 ('start_element', 'root'),
163 ('write_attribute', '"foo"', 'bar'),
168 ('start_element', 'root'),
169 ('write_attribute', 'foo', '"bar"'),
172 <root foo=""bar""/>
175 # attribute after character data
176 assert throws(perform_test
,
177 ('start_element', 'root'),
178 ('write_character_data', ''),
179 ('write_attribute', 'foo', 'bar'),
184 ('start_element', 'root'),
185 ('write_attribute', 'foo', 'bar'),
186 ('start_element', 'baz'),
196 ('start_element', 'root'),
197 ('start_element', 'foo'),
198 ('write_attribute', 'bar', 'baz'),
207 ### writing character data ###
210 ('start_element', 'root'),
211 ('write_character_data', 'foo'),
212 ('write_character_data', 'bar'),
213 ('start_element', 'baz'),
224 ('start_element', 'root'),
225 ('write_character_data', 'foo'),
226 ('start_element', 'bar'),
227 ('write_character_data', 'baz'),
240 ('start_element', 'root'),
241 ('start_element', 'foo'),
242 ('write_character_data', 'bar'),
243 ('write_character_data', 'baz'),
255 ('start_element', 'root'),
256 ('start_element', 'foo'),
257 ('write_character_data', 'bar'),
259 ('write_character_data', 'baz'),
271 ('start_element', 'root'),
272 ('start_element', 'foo'),
274 ('write_character_data', 'bar'),
275 ('write_character_data', 'baz'),
285 ('start_element', 'root'),
286 ('write_character_data', 'foo'),
287 ('start_element', 'bar'),
289 ('write_character_data', 'baz'),
300 ('start_element', 'root'),
301 ('write_character_data', 'foo&bar'),
310 ('start_element', 'root'),
311 ('write_character_data', 'foo'),
312 ('start_element', 'element'),
313 ('write_character_data', 'bar'),
315 ('write_character_data', 'baz'),
328 ('start_element', 'root', True),
329 ('write_character_data', 'foo'),
330 ('start_element', 'element'),
331 ('write_character_data', 'bar'),
333 ('write_character_data', 'baz'),
336 <root>foo<element>bar</element>baz</root>
340 ('start_element', 'root'),
341 ('write_character_data', 'foo'),
342 ('start_element', 'element', True),
343 ('write_character_data', 'bar'),
345 ('write_character_data', 'baz'),
350 <element>bar</element>
355 # character data before root element
356 assert throws(perform_test
,
357 ('write_character_data', 'foo'),
358 ('start_element', 'root'),
362 # character data after root element
363 f
= StringIO
.StringIO()
364 w
= xorn
.xml_writer
.XMLWriter(f
.write
)
365 w
.start_element('root')
367 assert throws(w
.write_character_data
, 'foo') == ValueError
370 ### writing CDATA section ###
373 ('start_element', 'root'),
374 ('write_cdata_section', 'foo'),
383 ('start_element', 'root'),
384 ('write_cdata_section', '<![CDATA[foo]]>'),
388 <![CDATA[<![CDATA[foo]]>]]><![CDATA[]]>
392 # CDATA section before root element
393 assert throws(perform_test
,
394 ('write_cdata_section', 'foo'),
395 ('start_element', 'root'),
399 # CDATA section after root element
400 f
= StringIO
.StringIO()
401 w
= xorn
.xml_writer
.XMLWriter(f
.write
)
402 w
.start_element('root')
404 assert throws(w
.write_cdata_section
, 'foo') == ValueError