1 | n/a | # regression test for SAX 2.0 |
---|
2 | n/a | # $Id$ |
---|
3 | n/a | |
---|
4 | n/a | from xml.sax import make_parser, ContentHandler, \ |
---|
5 | n/a | SAXException, SAXReaderNotAvailable, SAXParseException |
---|
6 | n/a | import unittest |
---|
7 | n/a | try: |
---|
8 | n/a | make_parser() |
---|
9 | n/a | except SAXReaderNotAvailable: |
---|
10 | n/a | # don't try to test this module if we cannot create a parser |
---|
11 | n/a | raise unittest.SkipTest("no XML parsers available") |
---|
12 | n/a | from xml.sax.saxutils import XMLGenerator, escape, unescape, quoteattr, \ |
---|
13 | n/a | XMLFilterBase, prepare_input_source |
---|
14 | n/a | from xml.sax.expatreader import create_parser |
---|
15 | n/a | from xml.sax.handler import feature_namespaces |
---|
16 | n/a | from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl |
---|
17 | n/a | from io import BytesIO, StringIO |
---|
18 | n/a | import codecs |
---|
19 | n/a | import gc |
---|
20 | n/a | import os.path |
---|
21 | n/a | import shutil |
---|
22 | n/a | from test import support |
---|
23 | n/a | from test.support import findfile, run_unittest, TESTFN |
---|
24 | n/a | |
---|
25 | n/a | TEST_XMLFILE = findfile("test.xml", subdir="xmltestdata") |
---|
26 | n/a | TEST_XMLFILE_OUT = findfile("test.xml.out", subdir="xmltestdata") |
---|
27 | n/a | try: |
---|
28 | n/a | TEST_XMLFILE.encode("utf-8") |
---|
29 | n/a | TEST_XMLFILE_OUT.encode("utf-8") |
---|
30 | n/a | except UnicodeEncodeError: |
---|
31 | n/a | raise unittest.SkipTest("filename is not encodable to utf8") |
---|
32 | n/a | |
---|
33 | n/a | supports_nonascii_filenames = True |
---|
34 | n/a | if not os.path.supports_unicode_filenames: |
---|
35 | n/a | try: |
---|
36 | n/a | support.TESTFN_UNICODE.encode(support.TESTFN_ENCODING) |
---|
37 | n/a | except (UnicodeError, TypeError): |
---|
38 | n/a | # Either the file system encoding is None, or the file name |
---|
39 | n/a | # cannot be encoded in the file system encoding. |
---|
40 | n/a | supports_nonascii_filenames = False |
---|
41 | n/a | requires_nonascii_filenames = unittest.skipUnless( |
---|
42 | n/a | supports_nonascii_filenames, |
---|
43 | n/a | 'Requires non-ascii filenames support') |
---|
44 | n/a | |
---|
45 | n/a | ns_uri = "http://www.python.org/xml-ns/saxtest/" |
---|
46 | n/a | |
---|
47 | n/a | class XmlTestBase(unittest.TestCase): |
---|
48 | n/a | def verify_empty_attrs(self, attrs): |
---|
49 | n/a | self.assertRaises(KeyError, attrs.getValue, "attr") |
---|
50 | n/a | self.assertRaises(KeyError, attrs.getValueByQName, "attr") |
---|
51 | n/a | self.assertRaises(KeyError, attrs.getNameByQName, "attr") |
---|
52 | n/a | self.assertRaises(KeyError, attrs.getQNameByName, "attr") |
---|
53 | n/a | self.assertRaises(KeyError, attrs.__getitem__, "attr") |
---|
54 | n/a | self.assertEqual(attrs.getLength(), 0) |
---|
55 | n/a | self.assertEqual(attrs.getNames(), []) |
---|
56 | n/a | self.assertEqual(attrs.getQNames(), []) |
---|
57 | n/a | self.assertEqual(len(attrs), 0) |
---|
58 | n/a | self.assertNotIn("attr", attrs) |
---|
59 | n/a | self.assertEqual(list(attrs.keys()), []) |
---|
60 | n/a | self.assertEqual(attrs.get("attrs"), None) |
---|
61 | n/a | self.assertEqual(attrs.get("attrs", 25), 25) |
---|
62 | n/a | self.assertEqual(list(attrs.items()), []) |
---|
63 | n/a | self.assertEqual(list(attrs.values()), []) |
---|
64 | n/a | |
---|
65 | n/a | def verify_empty_nsattrs(self, attrs): |
---|
66 | n/a | self.assertRaises(KeyError, attrs.getValue, (ns_uri, "attr")) |
---|
67 | n/a | self.assertRaises(KeyError, attrs.getValueByQName, "ns:attr") |
---|
68 | n/a | self.assertRaises(KeyError, attrs.getNameByQName, "ns:attr") |
---|
69 | n/a | self.assertRaises(KeyError, attrs.getQNameByName, (ns_uri, "attr")) |
---|
70 | n/a | self.assertRaises(KeyError, attrs.__getitem__, (ns_uri, "attr")) |
---|
71 | n/a | self.assertEqual(attrs.getLength(), 0) |
---|
72 | n/a | self.assertEqual(attrs.getNames(), []) |
---|
73 | n/a | self.assertEqual(attrs.getQNames(), []) |
---|
74 | n/a | self.assertEqual(len(attrs), 0) |
---|
75 | n/a | self.assertNotIn((ns_uri, "attr"), attrs) |
---|
76 | n/a | self.assertEqual(list(attrs.keys()), []) |
---|
77 | n/a | self.assertEqual(attrs.get((ns_uri, "attr")), None) |
---|
78 | n/a | self.assertEqual(attrs.get((ns_uri, "attr"), 25), 25) |
---|
79 | n/a | self.assertEqual(list(attrs.items()), []) |
---|
80 | n/a | self.assertEqual(list(attrs.values()), []) |
---|
81 | n/a | |
---|
82 | n/a | def verify_attrs_wattr(self, attrs): |
---|
83 | n/a | self.assertEqual(attrs.getLength(), 1) |
---|
84 | n/a | self.assertEqual(attrs.getNames(), ["attr"]) |
---|
85 | n/a | self.assertEqual(attrs.getQNames(), ["attr"]) |
---|
86 | n/a | self.assertEqual(len(attrs), 1) |
---|
87 | n/a | self.assertIn("attr", attrs) |
---|
88 | n/a | self.assertEqual(list(attrs.keys()), ["attr"]) |
---|
89 | n/a | self.assertEqual(attrs.get("attr"), "val") |
---|
90 | n/a | self.assertEqual(attrs.get("attr", 25), "val") |
---|
91 | n/a | self.assertEqual(list(attrs.items()), [("attr", "val")]) |
---|
92 | n/a | self.assertEqual(list(attrs.values()), ["val"]) |
---|
93 | n/a | self.assertEqual(attrs.getValue("attr"), "val") |
---|
94 | n/a | self.assertEqual(attrs.getValueByQName("attr"), "val") |
---|
95 | n/a | self.assertEqual(attrs.getNameByQName("attr"), "attr") |
---|
96 | n/a | self.assertEqual(attrs["attr"], "val") |
---|
97 | n/a | self.assertEqual(attrs.getQNameByName("attr"), "attr") |
---|
98 | n/a | |
---|
99 | n/a | |
---|
100 | n/a | def xml_str(doc, encoding=None): |
---|
101 | n/a | if encoding is None: |
---|
102 | n/a | return doc |
---|
103 | n/a | return '<?xml version="1.0" encoding="%s"?>\n%s' % (encoding, doc) |
---|
104 | n/a | |
---|
105 | n/a | def xml_bytes(doc, encoding, decl_encoding=...): |
---|
106 | n/a | if decl_encoding is ...: |
---|
107 | n/a | decl_encoding = encoding |
---|
108 | n/a | return xml_str(doc, decl_encoding).encode(encoding, 'xmlcharrefreplace') |
---|
109 | n/a | |
---|
110 | n/a | def make_xml_file(doc, encoding, decl_encoding=...): |
---|
111 | n/a | if decl_encoding is ...: |
---|
112 | n/a | decl_encoding = encoding |
---|
113 | n/a | with open(TESTFN, 'w', encoding=encoding, errors='xmlcharrefreplace') as f: |
---|
114 | n/a | f.write(xml_str(doc, decl_encoding)) |
---|
115 | n/a | |
---|
116 | n/a | |
---|
117 | n/a | class ParseTest(unittest.TestCase): |
---|
118 | n/a | data = '<money value="$\xa3\u20ac\U0001017b">$\xa3\u20ac\U0001017b</money>' |
---|
119 | n/a | |
---|
120 | n/a | def tearDown(self): |
---|
121 | n/a | support.unlink(TESTFN) |
---|
122 | n/a | |
---|
123 | n/a | def check_parse(self, f): |
---|
124 | n/a | from xml.sax import parse |
---|
125 | n/a | result = StringIO() |
---|
126 | n/a | parse(f, XMLGenerator(result, 'utf-8')) |
---|
127 | n/a | self.assertEqual(result.getvalue(), xml_str(self.data, 'utf-8')) |
---|
128 | n/a | |
---|
129 | n/a | def test_parse_text(self): |
---|
130 | n/a | encodings = ('us-ascii', 'iso-8859-1', 'utf-8', |
---|
131 | n/a | 'utf-16', 'utf-16le', 'utf-16be') |
---|
132 | n/a | for encoding in encodings: |
---|
133 | n/a | self.check_parse(StringIO(xml_str(self.data, encoding))) |
---|
134 | n/a | make_xml_file(self.data, encoding) |
---|
135 | n/a | with open(TESTFN, 'r', encoding=encoding) as f: |
---|
136 | n/a | self.check_parse(f) |
---|
137 | n/a | self.check_parse(StringIO(self.data)) |
---|
138 | n/a | make_xml_file(self.data, encoding, None) |
---|
139 | n/a | with open(TESTFN, 'r', encoding=encoding) as f: |
---|
140 | n/a | self.check_parse(f) |
---|
141 | n/a | |
---|
142 | n/a | def test_parse_bytes(self): |
---|
143 | n/a | # UTF-8 is default encoding, US-ASCII is compatible with UTF-8, |
---|
144 | n/a | # UTF-16 is autodetected |
---|
145 | n/a | encodings = ('us-ascii', 'utf-8', 'utf-16', 'utf-16le', 'utf-16be') |
---|
146 | n/a | for encoding in encodings: |
---|
147 | n/a | self.check_parse(BytesIO(xml_bytes(self.data, encoding))) |
---|
148 | n/a | make_xml_file(self.data, encoding) |
---|
149 | n/a | self.check_parse(TESTFN) |
---|
150 | n/a | with open(TESTFN, 'rb') as f: |
---|
151 | n/a | self.check_parse(f) |
---|
152 | n/a | self.check_parse(BytesIO(xml_bytes(self.data, encoding, None))) |
---|
153 | n/a | make_xml_file(self.data, encoding, None) |
---|
154 | n/a | self.check_parse(TESTFN) |
---|
155 | n/a | with open(TESTFN, 'rb') as f: |
---|
156 | n/a | self.check_parse(f) |
---|
157 | n/a | # accept UTF-8 with BOM |
---|
158 | n/a | self.check_parse(BytesIO(xml_bytes(self.data, 'utf-8-sig', 'utf-8'))) |
---|
159 | n/a | make_xml_file(self.data, 'utf-8-sig', 'utf-8') |
---|
160 | n/a | self.check_parse(TESTFN) |
---|
161 | n/a | with open(TESTFN, 'rb') as f: |
---|
162 | n/a | self.check_parse(f) |
---|
163 | n/a | self.check_parse(BytesIO(xml_bytes(self.data, 'utf-8-sig', None))) |
---|
164 | n/a | make_xml_file(self.data, 'utf-8-sig', None) |
---|
165 | n/a | self.check_parse(TESTFN) |
---|
166 | n/a | with open(TESTFN, 'rb') as f: |
---|
167 | n/a | self.check_parse(f) |
---|
168 | n/a | # accept data with declared encoding |
---|
169 | n/a | self.check_parse(BytesIO(xml_bytes(self.data, 'iso-8859-1'))) |
---|
170 | n/a | make_xml_file(self.data, 'iso-8859-1') |
---|
171 | n/a | self.check_parse(TESTFN) |
---|
172 | n/a | with open(TESTFN, 'rb') as f: |
---|
173 | n/a | self.check_parse(f) |
---|
174 | n/a | # fail on non-UTF-8 incompatible data without declared encoding |
---|
175 | n/a | with self.assertRaises(SAXException): |
---|
176 | n/a | self.check_parse(BytesIO(xml_bytes(self.data, 'iso-8859-1', None))) |
---|
177 | n/a | make_xml_file(self.data, 'iso-8859-1', None) |
---|
178 | n/a | with support.check_warnings(('unclosed file', ResourceWarning)): |
---|
179 | n/a | # XXX Failed parser leaks an opened file. |
---|
180 | n/a | with self.assertRaises(SAXException): |
---|
181 | n/a | self.check_parse(TESTFN) |
---|
182 | n/a | # Collect leaked file. |
---|
183 | n/a | gc.collect() |
---|
184 | n/a | with open(TESTFN, 'rb') as f: |
---|
185 | n/a | with self.assertRaises(SAXException): |
---|
186 | n/a | self.check_parse(f) |
---|
187 | n/a | |
---|
188 | n/a | def test_parse_InputSource(self): |
---|
189 | n/a | # accept data without declared but with explicitly specified encoding |
---|
190 | n/a | make_xml_file(self.data, 'iso-8859-1', None) |
---|
191 | n/a | with open(TESTFN, 'rb') as f: |
---|
192 | n/a | input = InputSource() |
---|
193 | n/a | input.setByteStream(f) |
---|
194 | n/a | input.setEncoding('iso-8859-1') |
---|
195 | n/a | self.check_parse(input) |
---|
196 | n/a | |
---|
197 | n/a | def check_parseString(self, s): |
---|
198 | n/a | from xml.sax import parseString |
---|
199 | n/a | result = StringIO() |
---|
200 | n/a | parseString(s, XMLGenerator(result, 'utf-8')) |
---|
201 | n/a | self.assertEqual(result.getvalue(), xml_str(self.data, 'utf-8')) |
---|
202 | n/a | |
---|
203 | n/a | def test_parseString_text(self): |
---|
204 | n/a | encodings = ('us-ascii', 'iso-8859-1', 'utf-8', |
---|
205 | n/a | 'utf-16', 'utf-16le', 'utf-16be') |
---|
206 | n/a | for encoding in encodings: |
---|
207 | n/a | self.check_parseString(xml_str(self.data, encoding)) |
---|
208 | n/a | self.check_parseString(self.data) |
---|
209 | n/a | |
---|
210 | n/a | def test_parseString_bytes(self): |
---|
211 | n/a | # UTF-8 is default encoding, US-ASCII is compatible with UTF-8, |
---|
212 | n/a | # UTF-16 is autodetected |
---|
213 | n/a | encodings = ('us-ascii', 'utf-8', 'utf-16', 'utf-16le', 'utf-16be') |
---|
214 | n/a | for encoding in encodings: |
---|
215 | n/a | self.check_parseString(xml_bytes(self.data, encoding)) |
---|
216 | n/a | self.check_parseString(xml_bytes(self.data, encoding, None)) |
---|
217 | n/a | # accept UTF-8 with BOM |
---|
218 | n/a | self.check_parseString(xml_bytes(self.data, 'utf-8-sig', 'utf-8')) |
---|
219 | n/a | self.check_parseString(xml_bytes(self.data, 'utf-8-sig', None)) |
---|
220 | n/a | # accept data with declared encoding |
---|
221 | n/a | self.check_parseString(xml_bytes(self.data, 'iso-8859-1')) |
---|
222 | n/a | # fail on non-UTF-8 incompatible data without declared encoding |
---|
223 | n/a | with self.assertRaises(SAXException): |
---|
224 | n/a | self.check_parseString(xml_bytes(self.data, 'iso-8859-1', None)) |
---|
225 | n/a | |
---|
226 | n/a | class MakeParserTest(unittest.TestCase): |
---|
227 | n/a | def test_make_parser2(self): |
---|
228 | n/a | # Creating parsers several times in a row should succeed. |
---|
229 | n/a | # Testing this because there have been failures of this kind |
---|
230 | n/a | # before. |
---|
231 | n/a | from xml.sax import make_parser |
---|
232 | n/a | p = make_parser() |
---|
233 | n/a | from xml.sax import make_parser |
---|
234 | n/a | p = make_parser() |
---|
235 | n/a | from xml.sax import make_parser |
---|
236 | n/a | p = make_parser() |
---|
237 | n/a | from xml.sax import make_parser |
---|
238 | n/a | p = make_parser() |
---|
239 | n/a | from xml.sax import make_parser |
---|
240 | n/a | p = make_parser() |
---|
241 | n/a | from xml.sax import make_parser |
---|
242 | n/a | p = make_parser() |
---|
243 | n/a | |
---|
244 | n/a | |
---|
245 | n/a | # =========================================================================== |
---|
246 | n/a | # |
---|
247 | n/a | # saxutils tests |
---|
248 | n/a | # |
---|
249 | n/a | # =========================================================================== |
---|
250 | n/a | |
---|
251 | n/a | class SaxutilsTest(unittest.TestCase): |
---|
252 | n/a | # ===== escape |
---|
253 | n/a | def test_escape_basic(self): |
---|
254 | n/a | self.assertEqual(escape("Donald Duck & Co"), "Donald Duck & Co") |
---|
255 | n/a | |
---|
256 | n/a | def test_escape_all(self): |
---|
257 | n/a | self.assertEqual(escape("<Donald Duck & Co>"), |
---|
258 | n/a | "<Donald Duck & Co>") |
---|
259 | n/a | |
---|
260 | n/a | def test_escape_extra(self): |
---|
261 | n/a | self.assertEqual(escape("Hei pÃ¥ deg", {"Ã¥" : "å"}), |
---|
262 | n/a | "Hei på deg") |
---|
263 | n/a | |
---|
264 | n/a | # ===== unescape |
---|
265 | n/a | def test_unescape_basic(self): |
---|
266 | n/a | self.assertEqual(unescape("Donald Duck & Co"), "Donald Duck & Co") |
---|
267 | n/a | |
---|
268 | n/a | def test_unescape_all(self): |
---|
269 | n/a | self.assertEqual(unescape("<Donald Duck & Co>"), |
---|
270 | n/a | "<Donald Duck & Co>") |
---|
271 | n/a | |
---|
272 | n/a | def test_unescape_extra(self): |
---|
273 | n/a | self.assertEqual(unescape("Hei pÃ¥ deg", {"Ã¥" : "å"}), |
---|
274 | n/a | "Hei på deg") |
---|
275 | n/a | |
---|
276 | n/a | def test_unescape_amp_extra(self): |
---|
277 | n/a | self.assertEqual(unescape("&foo;", {"&foo;": "splat"}), "&foo;") |
---|
278 | n/a | |
---|
279 | n/a | # ===== quoteattr |
---|
280 | n/a | def test_quoteattr_basic(self): |
---|
281 | n/a | self.assertEqual(quoteattr("Donald Duck & Co"), |
---|
282 | n/a | '"Donald Duck & Co"') |
---|
283 | n/a | |
---|
284 | n/a | def test_single_quoteattr(self): |
---|
285 | n/a | self.assertEqual(quoteattr('Includes "double" quotes'), |
---|
286 | n/a | '\'Includes "double" quotes\'') |
---|
287 | n/a | |
---|
288 | n/a | def test_double_quoteattr(self): |
---|
289 | n/a | self.assertEqual(quoteattr("Includes 'single' quotes"), |
---|
290 | n/a | "\"Includes 'single' quotes\"") |
---|
291 | n/a | |
---|
292 | n/a | def test_single_double_quoteattr(self): |
---|
293 | n/a | self.assertEqual(quoteattr("Includes 'single' and \"double\" quotes"), |
---|
294 | n/a | "\"Includes 'single' and "double" quotes\"") |
---|
295 | n/a | |
---|
296 | n/a | # ===== make_parser |
---|
297 | n/a | def test_make_parser(self): |
---|
298 | n/a | # Creating a parser should succeed - it should fall back |
---|
299 | n/a | # to the expatreader |
---|
300 | n/a | p = make_parser(['xml.parsers.no_such_parser']) |
---|
301 | n/a | |
---|
302 | n/a | |
---|
303 | n/a | class PrepareInputSourceTest(unittest.TestCase): |
---|
304 | n/a | |
---|
305 | n/a | def setUp(self): |
---|
306 | n/a | self.file = support.TESTFN |
---|
307 | n/a | with open(self.file, "w") as tmp: |
---|
308 | n/a | tmp.write("This was read from a file.") |
---|
309 | n/a | |
---|
310 | n/a | def tearDown(self): |
---|
311 | n/a | support.unlink(self.file) |
---|
312 | n/a | |
---|
313 | n/a | def make_byte_stream(self): |
---|
314 | n/a | return BytesIO(b"This is a byte stream.") |
---|
315 | n/a | |
---|
316 | n/a | def make_character_stream(self): |
---|
317 | n/a | return StringIO("This is a character stream.") |
---|
318 | n/a | |
---|
319 | n/a | def checkContent(self, stream, content): |
---|
320 | n/a | self.assertIsNotNone(stream) |
---|
321 | n/a | self.assertEqual(stream.read(), content) |
---|
322 | n/a | stream.close() |
---|
323 | n/a | |
---|
324 | n/a | |
---|
325 | n/a | def test_character_stream(self): |
---|
326 | n/a | # If the source is an InputSource with a character stream, use it. |
---|
327 | n/a | src = InputSource(self.file) |
---|
328 | n/a | src.setCharacterStream(self.make_character_stream()) |
---|
329 | n/a | prep = prepare_input_source(src) |
---|
330 | n/a | self.assertIsNone(prep.getByteStream()) |
---|
331 | n/a | self.checkContent(prep.getCharacterStream(), |
---|
332 | n/a | "This is a character stream.") |
---|
333 | n/a | |
---|
334 | n/a | def test_byte_stream(self): |
---|
335 | n/a | # If the source is an InputSource that does not have a character |
---|
336 | n/a | # stream but does have a byte stream, use the byte stream. |
---|
337 | n/a | src = InputSource(self.file) |
---|
338 | n/a | src.setByteStream(self.make_byte_stream()) |
---|
339 | n/a | prep = prepare_input_source(src) |
---|
340 | n/a | self.assertIsNone(prep.getCharacterStream()) |
---|
341 | n/a | self.checkContent(prep.getByteStream(), |
---|
342 | n/a | b"This is a byte stream.") |
---|
343 | n/a | |
---|
344 | n/a | def test_system_id(self): |
---|
345 | n/a | # If the source is an InputSource that has neither a character |
---|
346 | n/a | # stream nor a byte stream, open the system ID. |
---|
347 | n/a | src = InputSource(self.file) |
---|
348 | n/a | prep = prepare_input_source(src) |
---|
349 | n/a | self.assertIsNone(prep.getCharacterStream()) |
---|
350 | n/a | self.checkContent(prep.getByteStream(), |
---|
351 | n/a | b"This was read from a file.") |
---|
352 | n/a | |
---|
353 | n/a | def test_string(self): |
---|
354 | n/a | # If the source is a string, use it as a system ID and open it. |
---|
355 | n/a | prep = prepare_input_source(self.file) |
---|
356 | n/a | self.assertIsNone(prep.getCharacterStream()) |
---|
357 | n/a | self.checkContent(prep.getByteStream(), |
---|
358 | n/a | b"This was read from a file.") |
---|
359 | n/a | |
---|
360 | n/a | def test_binary_file(self): |
---|
361 | n/a | # If the source is a binary file-like object, use it as a byte |
---|
362 | n/a | # stream. |
---|
363 | n/a | prep = prepare_input_source(self.make_byte_stream()) |
---|
364 | n/a | self.assertIsNone(prep.getCharacterStream()) |
---|
365 | n/a | self.checkContent(prep.getByteStream(), |
---|
366 | n/a | b"This is a byte stream.") |
---|
367 | n/a | |
---|
368 | n/a | def test_text_file(self): |
---|
369 | n/a | # If the source is a text file-like object, use it as a character |
---|
370 | n/a | # stream. |
---|
371 | n/a | prep = prepare_input_source(self.make_character_stream()) |
---|
372 | n/a | self.assertIsNone(prep.getByteStream()) |
---|
373 | n/a | self.checkContent(prep.getCharacterStream(), |
---|
374 | n/a | "This is a character stream.") |
---|
375 | n/a | |
---|
376 | n/a | |
---|
377 | n/a | # ===== XMLGenerator |
---|
378 | n/a | |
---|
379 | n/a | class XmlgenTest: |
---|
380 | n/a | def test_xmlgen_basic(self): |
---|
381 | n/a | result = self.ioclass() |
---|
382 | n/a | gen = XMLGenerator(result) |
---|
383 | n/a | gen.startDocument() |
---|
384 | n/a | gen.startElement("doc", {}) |
---|
385 | n/a | gen.endElement("doc") |
---|
386 | n/a | gen.endDocument() |
---|
387 | n/a | |
---|
388 | n/a | self.assertEqual(result.getvalue(), self.xml("<doc></doc>")) |
---|
389 | n/a | |
---|
390 | n/a | def test_xmlgen_basic_empty(self): |
---|
391 | n/a | result = self.ioclass() |
---|
392 | n/a | gen = XMLGenerator(result, short_empty_elements=True) |
---|
393 | n/a | gen.startDocument() |
---|
394 | n/a | gen.startElement("doc", {}) |
---|
395 | n/a | gen.endElement("doc") |
---|
396 | n/a | gen.endDocument() |
---|
397 | n/a | |
---|
398 | n/a | self.assertEqual(result.getvalue(), self.xml("<doc/>")) |
---|
399 | n/a | |
---|
400 | n/a | def test_xmlgen_content(self): |
---|
401 | n/a | result = self.ioclass() |
---|
402 | n/a | gen = XMLGenerator(result) |
---|
403 | n/a | |
---|
404 | n/a | gen.startDocument() |
---|
405 | n/a | gen.startElement("doc", {}) |
---|
406 | n/a | gen.characters("huhei") |
---|
407 | n/a | gen.endElement("doc") |
---|
408 | n/a | gen.endDocument() |
---|
409 | n/a | |
---|
410 | n/a | self.assertEqual(result.getvalue(), self.xml("<doc>huhei</doc>")) |
---|
411 | n/a | |
---|
412 | n/a | def test_xmlgen_content_empty(self): |
---|
413 | n/a | result = self.ioclass() |
---|
414 | n/a | gen = XMLGenerator(result, short_empty_elements=True) |
---|
415 | n/a | |
---|
416 | n/a | gen.startDocument() |
---|
417 | n/a | gen.startElement("doc", {}) |
---|
418 | n/a | gen.characters("huhei") |
---|
419 | n/a | gen.endElement("doc") |
---|
420 | n/a | gen.endDocument() |
---|
421 | n/a | |
---|
422 | n/a | self.assertEqual(result.getvalue(), self.xml("<doc>huhei</doc>")) |
---|
423 | n/a | |
---|
424 | n/a | def test_xmlgen_pi(self): |
---|
425 | n/a | result = self.ioclass() |
---|
426 | n/a | gen = XMLGenerator(result) |
---|
427 | n/a | |
---|
428 | n/a | gen.startDocument() |
---|
429 | n/a | gen.processingInstruction("test", "data") |
---|
430 | n/a | gen.startElement("doc", {}) |
---|
431 | n/a | gen.endElement("doc") |
---|
432 | n/a | gen.endDocument() |
---|
433 | n/a | |
---|
434 | n/a | self.assertEqual(result.getvalue(), |
---|
435 | n/a | self.xml("<?test data?><doc></doc>")) |
---|
436 | n/a | |
---|
437 | n/a | def test_xmlgen_content_escape(self): |
---|
438 | n/a | result = self.ioclass() |
---|
439 | n/a | gen = XMLGenerator(result) |
---|
440 | n/a | |
---|
441 | n/a | gen.startDocument() |
---|
442 | n/a | gen.startElement("doc", {}) |
---|
443 | n/a | gen.characters("<huhei&") |
---|
444 | n/a | gen.endElement("doc") |
---|
445 | n/a | gen.endDocument() |
---|
446 | n/a | |
---|
447 | n/a | self.assertEqual(result.getvalue(), |
---|
448 | n/a | self.xml("<doc><huhei&</doc>")) |
---|
449 | n/a | |
---|
450 | n/a | def test_xmlgen_attr_escape(self): |
---|
451 | n/a | result = self.ioclass() |
---|
452 | n/a | gen = XMLGenerator(result) |
---|
453 | n/a | |
---|
454 | n/a | gen.startDocument() |
---|
455 | n/a | gen.startElement("doc", {"a": '"'}) |
---|
456 | n/a | gen.startElement("e", {"a": "'"}) |
---|
457 | n/a | gen.endElement("e") |
---|
458 | n/a | gen.startElement("e", {"a": "'\""}) |
---|
459 | n/a | gen.endElement("e") |
---|
460 | n/a | gen.startElement("e", {"a": "\n\r\t"}) |
---|
461 | n/a | gen.endElement("e") |
---|
462 | n/a | gen.endElement("doc") |
---|
463 | n/a | gen.endDocument() |
---|
464 | n/a | |
---|
465 | n/a | self.assertEqual(result.getvalue(), self.xml( |
---|
466 | n/a | "<doc a='\"'><e a=\"'\"></e>" |
---|
467 | n/a | "<e a=\"'"\"></e>" |
---|
468 | n/a | "<e a=\" 	\"></e></doc>")) |
---|
469 | n/a | |
---|
470 | n/a | def test_xmlgen_encoding(self): |
---|
471 | n/a | encodings = ('iso-8859-15', 'utf-8', 'utf-8-sig', |
---|
472 | n/a | 'utf-16', 'utf-16be', 'utf-16le', |
---|
473 | n/a | 'utf-32', 'utf-32be', 'utf-32le') |
---|
474 | n/a | for encoding in encodings: |
---|
475 | n/a | result = self.ioclass() |
---|
476 | n/a | gen = XMLGenerator(result, encoding=encoding) |
---|
477 | n/a | |
---|
478 | n/a | gen.startDocument() |
---|
479 | n/a | gen.startElement("doc", {"a": '\u20ac'}) |
---|
480 | n/a | gen.characters("\u20ac") |
---|
481 | n/a | gen.endElement("doc") |
---|
482 | n/a | gen.endDocument() |
---|
483 | n/a | |
---|
484 | n/a | self.assertEqual(result.getvalue(), |
---|
485 | n/a | self.xml('<doc a="\u20ac">\u20ac</doc>', encoding=encoding)) |
---|
486 | n/a | |
---|
487 | n/a | def test_xmlgen_unencodable(self): |
---|
488 | n/a | result = self.ioclass() |
---|
489 | n/a | gen = XMLGenerator(result, encoding='ascii') |
---|
490 | n/a | |
---|
491 | n/a | gen.startDocument() |
---|
492 | n/a | gen.startElement("doc", {"a": '\u20ac'}) |
---|
493 | n/a | gen.characters("\u20ac") |
---|
494 | n/a | gen.endElement("doc") |
---|
495 | n/a | gen.endDocument() |
---|
496 | n/a | |
---|
497 | n/a | self.assertEqual(result.getvalue(), |
---|
498 | n/a | self.xml('<doc a="€">€</doc>', encoding='ascii')) |
---|
499 | n/a | |
---|
500 | n/a | def test_xmlgen_ignorable(self): |
---|
501 | n/a | result = self.ioclass() |
---|
502 | n/a | gen = XMLGenerator(result) |
---|
503 | n/a | |
---|
504 | n/a | gen.startDocument() |
---|
505 | n/a | gen.startElement("doc", {}) |
---|
506 | n/a | gen.ignorableWhitespace(" ") |
---|
507 | n/a | gen.endElement("doc") |
---|
508 | n/a | gen.endDocument() |
---|
509 | n/a | |
---|
510 | n/a | self.assertEqual(result.getvalue(), self.xml("<doc> </doc>")) |
---|
511 | n/a | |
---|
512 | n/a | def test_xmlgen_ignorable_empty(self): |
---|
513 | n/a | result = self.ioclass() |
---|
514 | n/a | gen = XMLGenerator(result, short_empty_elements=True) |
---|
515 | n/a | |
---|
516 | n/a | gen.startDocument() |
---|
517 | n/a | gen.startElement("doc", {}) |
---|
518 | n/a | gen.ignorableWhitespace(" ") |
---|
519 | n/a | gen.endElement("doc") |
---|
520 | n/a | gen.endDocument() |
---|
521 | n/a | |
---|
522 | n/a | self.assertEqual(result.getvalue(), self.xml("<doc> </doc>")) |
---|
523 | n/a | |
---|
524 | n/a | def test_xmlgen_encoding_bytes(self): |
---|
525 | n/a | encodings = ('iso-8859-15', 'utf-8', 'utf-8-sig', |
---|
526 | n/a | 'utf-16', 'utf-16be', 'utf-16le', |
---|
527 | n/a | 'utf-32', 'utf-32be', 'utf-32le') |
---|
528 | n/a | for encoding in encodings: |
---|
529 | n/a | result = self.ioclass() |
---|
530 | n/a | gen = XMLGenerator(result, encoding=encoding) |
---|
531 | n/a | |
---|
532 | n/a | gen.startDocument() |
---|
533 | n/a | gen.startElement("doc", {"a": '\u20ac'}) |
---|
534 | n/a | gen.characters("\u20ac".encode(encoding)) |
---|
535 | n/a | gen.ignorableWhitespace(" ".encode(encoding)) |
---|
536 | n/a | gen.endElement("doc") |
---|
537 | n/a | gen.endDocument() |
---|
538 | n/a | |
---|
539 | n/a | self.assertEqual(result.getvalue(), |
---|
540 | n/a | self.xml('<doc a="\u20ac">\u20ac </doc>', encoding=encoding)) |
---|
541 | n/a | |
---|
542 | n/a | def test_xmlgen_ns(self): |
---|
543 | n/a | result = self.ioclass() |
---|
544 | n/a | gen = XMLGenerator(result) |
---|
545 | n/a | |
---|
546 | n/a | gen.startDocument() |
---|
547 | n/a | gen.startPrefixMapping("ns1", ns_uri) |
---|
548 | n/a | gen.startElementNS((ns_uri, "doc"), "ns1:doc", {}) |
---|
549 | n/a | # add an unqualified name |
---|
550 | n/a | gen.startElementNS((None, "udoc"), None, {}) |
---|
551 | n/a | gen.endElementNS((None, "udoc"), None) |
---|
552 | n/a | gen.endElementNS((ns_uri, "doc"), "ns1:doc") |
---|
553 | n/a | gen.endPrefixMapping("ns1") |
---|
554 | n/a | gen.endDocument() |
---|
555 | n/a | |
---|
556 | n/a | self.assertEqual(result.getvalue(), self.xml( |
---|
557 | n/a | '<ns1:doc xmlns:ns1="%s"><udoc></udoc></ns1:doc>' % |
---|
558 | n/a | ns_uri)) |
---|
559 | n/a | |
---|
560 | n/a | def test_xmlgen_ns_empty(self): |
---|
561 | n/a | result = self.ioclass() |
---|
562 | n/a | gen = XMLGenerator(result, short_empty_elements=True) |
---|
563 | n/a | |
---|
564 | n/a | gen.startDocument() |
---|
565 | n/a | gen.startPrefixMapping("ns1", ns_uri) |
---|
566 | n/a | gen.startElementNS((ns_uri, "doc"), "ns1:doc", {}) |
---|
567 | n/a | # add an unqualified name |
---|
568 | n/a | gen.startElementNS((None, "udoc"), None, {}) |
---|
569 | n/a | gen.endElementNS((None, "udoc"), None) |
---|
570 | n/a | gen.endElementNS((ns_uri, "doc"), "ns1:doc") |
---|
571 | n/a | gen.endPrefixMapping("ns1") |
---|
572 | n/a | gen.endDocument() |
---|
573 | n/a | |
---|
574 | n/a | self.assertEqual(result.getvalue(), self.xml( |
---|
575 | n/a | '<ns1:doc xmlns:ns1="%s"><udoc/></ns1:doc>' % |
---|
576 | n/a | ns_uri)) |
---|
577 | n/a | |
---|
578 | n/a | def test_1463026_1(self): |
---|
579 | n/a | result = self.ioclass() |
---|
580 | n/a | gen = XMLGenerator(result) |
---|
581 | n/a | |
---|
582 | n/a | gen.startDocument() |
---|
583 | n/a | gen.startElementNS((None, 'a'), 'a', {(None, 'b'):'c'}) |
---|
584 | n/a | gen.endElementNS((None, 'a'), 'a') |
---|
585 | n/a | gen.endDocument() |
---|
586 | n/a | |
---|
587 | n/a | self.assertEqual(result.getvalue(), self.xml('<a b="c"></a>')) |
---|
588 | n/a | |
---|
589 | n/a | def test_1463026_1_empty(self): |
---|
590 | n/a | result = self.ioclass() |
---|
591 | n/a | gen = XMLGenerator(result, short_empty_elements=True) |
---|
592 | n/a | |
---|
593 | n/a | gen.startDocument() |
---|
594 | n/a | gen.startElementNS((None, 'a'), 'a', {(None, 'b'):'c'}) |
---|
595 | n/a | gen.endElementNS((None, 'a'), 'a') |
---|
596 | n/a | gen.endDocument() |
---|
597 | n/a | |
---|
598 | n/a | self.assertEqual(result.getvalue(), self.xml('<a b="c"/>')) |
---|
599 | n/a | |
---|
600 | n/a | def test_1463026_2(self): |
---|
601 | n/a | result = self.ioclass() |
---|
602 | n/a | gen = XMLGenerator(result) |
---|
603 | n/a | |
---|
604 | n/a | gen.startDocument() |
---|
605 | n/a | gen.startPrefixMapping(None, 'qux') |
---|
606 | n/a | gen.startElementNS(('qux', 'a'), 'a', {}) |
---|
607 | n/a | gen.endElementNS(('qux', 'a'), 'a') |
---|
608 | n/a | gen.endPrefixMapping(None) |
---|
609 | n/a | gen.endDocument() |
---|
610 | n/a | |
---|
611 | n/a | self.assertEqual(result.getvalue(), self.xml('<a xmlns="qux"></a>')) |
---|
612 | n/a | |
---|
613 | n/a | def test_1463026_2_empty(self): |
---|
614 | n/a | result = self.ioclass() |
---|
615 | n/a | gen = XMLGenerator(result, short_empty_elements=True) |
---|
616 | n/a | |
---|
617 | n/a | gen.startDocument() |
---|
618 | n/a | gen.startPrefixMapping(None, 'qux') |
---|
619 | n/a | gen.startElementNS(('qux', 'a'), 'a', {}) |
---|
620 | n/a | gen.endElementNS(('qux', 'a'), 'a') |
---|
621 | n/a | gen.endPrefixMapping(None) |
---|
622 | n/a | gen.endDocument() |
---|
623 | n/a | |
---|
624 | n/a | self.assertEqual(result.getvalue(), self.xml('<a xmlns="qux"/>')) |
---|
625 | n/a | |
---|
626 | n/a | def test_1463026_3(self): |
---|
627 | n/a | result = self.ioclass() |
---|
628 | n/a | gen = XMLGenerator(result) |
---|
629 | n/a | |
---|
630 | n/a | gen.startDocument() |
---|
631 | n/a | gen.startPrefixMapping('my', 'qux') |
---|
632 | n/a | gen.startElementNS(('qux', 'a'), 'a', {(None, 'b'):'c'}) |
---|
633 | n/a | gen.endElementNS(('qux', 'a'), 'a') |
---|
634 | n/a | gen.endPrefixMapping('my') |
---|
635 | n/a | gen.endDocument() |
---|
636 | n/a | |
---|
637 | n/a | self.assertEqual(result.getvalue(), |
---|
638 | n/a | self.xml('<my:a xmlns:my="qux" b="c"></my:a>')) |
---|
639 | n/a | |
---|
640 | n/a | def test_1463026_3_empty(self): |
---|
641 | n/a | result = self.ioclass() |
---|
642 | n/a | gen = XMLGenerator(result, short_empty_elements=True) |
---|
643 | n/a | |
---|
644 | n/a | gen.startDocument() |
---|
645 | n/a | gen.startPrefixMapping('my', 'qux') |
---|
646 | n/a | gen.startElementNS(('qux', 'a'), 'a', {(None, 'b'):'c'}) |
---|
647 | n/a | gen.endElementNS(('qux', 'a'), 'a') |
---|
648 | n/a | gen.endPrefixMapping('my') |
---|
649 | n/a | gen.endDocument() |
---|
650 | n/a | |
---|
651 | n/a | self.assertEqual(result.getvalue(), |
---|
652 | n/a | self.xml('<my:a xmlns:my="qux" b="c"/>')) |
---|
653 | n/a | |
---|
654 | n/a | def test_5027_1(self): |
---|
655 | n/a | # The xml prefix (as in xml:lang below) is reserved and bound by |
---|
656 | n/a | # definition to http://www.w3.org/XML/1998/namespace. XMLGenerator had |
---|
657 | n/a | # a bug whereby a KeyError is raised because this namespace is missing |
---|
658 | n/a | # from a dictionary. |
---|
659 | n/a | # |
---|
660 | n/a | # This test demonstrates the bug by parsing a document. |
---|
661 | n/a | test_xml = StringIO( |
---|
662 | n/a | '<?xml version="1.0"?>' |
---|
663 | n/a | '<a:g1 xmlns:a="http://example.com/ns">' |
---|
664 | n/a | '<a:g2 xml:lang="en">Hello</a:g2>' |
---|
665 | n/a | '</a:g1>') |
---|
666 | n/a | |
---|
667 | n/a | parser = make_parser() |
---|
668 | n/a | parser.setFeature(feature_namespaces, True) |
---|
669 | n/a | result = self.ioclass() |
---|
670 | n/a | gen = XMLGenerator(result) |
---|
671 | n/a | parser.setContentHandler(gen) |
---|
672 | n/a | parser.parse(test_xml) |
---|
673 | n/a | |
---|
674 | n/a | self.assertEqual(result.getvalue(), |
---|
675 | n/a | self.xml( |
---|
676 | n/a | '<a:g1 xmlns:a="http://example.com/ns">' |
---|
677 | n/a | '<a:g2 xml:lang="en">Hello</a:g2>' |
---|
678 | n/a | '</a:g1>')) |
---|
679 | n/a | |
---|
680 | n/a | def test_5027_2(self): |
---|
681 | n/a | # The xml prefix (as in xml:lang below) is reserved and bound by |
---|
682 | n/a | # definition to http://www.w3.org/XML/1998/namespace. XMLGenerator had |
---|
683 | n/a | # a bug whereby a KeyError is raised because this namespace is missing |
---|
684 | n/a | # from a dictionary. |
---|
685 | n/a | # |
---|
686 | n/a | # This test demonstrates the bug by direct manipulation of the |
---|
687 | n/a | # XMLGenerator. |
---|
688 | n/a | result = self.ioclass() |
---|
689 | n/a | gen = XMLGenerator(result) |
---|
690 | n/a | |
---|
691 | n/a | gen.startDocument() |
---|
692 | n/a | gen.startPrefixMapping('a', 'http://example.com/ns') |
---|
693 | n/a | gen.startElementNS(('http://example.com/ns', 'g1'), 'g1', {}) |
---|
694 | n/a | lang_attr = {('http://www.w3.org/XML/1998/namespace', 'lang'): 'en'} |
---|
695 | n/a | gen.startElementNS(('http://example.com/ns', 'g2'), 'g2', lang_attr) |
---|
696 | n/a | gen.characters('Hello') |
---|
697 | n/a | gen.endElementNS(('http://example.com/ns', 'g2'), 'g2') |
---|
698 | n/a | gen.endElementNS(('http://example.com/ns', 'g1'), 'g1') |
---|
699 | n/a | gen.endPrefixMapping('a') |
---|
700 | n/a | gen.endDocument() |
---|
701 | n/a | |
---|
702 | n/a | self.assertEqual(result.getvalue(), |
---|
703 | n/a | self.xml( |
---|
704 | n/a | '<a:g1 xmlns:a="http://example.com/ns">' |
---|
705 | n/a | '<a:g2 xml:lang="en">Hello</a:g2>' |
---|
706 | n/a | '</a:g1>')) |
---|
707 | n/a | |
---|
708 | n/a | def test_no_close_file(self): |
---|
709 | n/a | result = self.ioclass() |
---|
710 | n/a | def func(out): |
---|
711 | n/a | gen = XMLGenerator(out) |
---|
712 | n/a | gen.startDocument() |
---|
713 | n/a | gen.startElement("doc", {}) |
---|
714 | n/a | func(result) |
---|
715 | n/a | self.assertFalse(result.closed) |
---|
716 | n/a | |
---|
717 | n/a | def test_xmlgen_fragment(self): |
---|
718 | n/a | result = self.ioclass() |
---|
719 | n/a | gen = XMLGenerator(result) |
---|
720 | n/a | |
---|
721 | n/a | # Don't call gen.startDocument() |
---|
722 | n/a | gen.startElement("foo", {"a": "1.0"}) |
---|
723 | n/a | gen.characters("Hello") |
---|
724 | n/a | gen.endElement("foo") |
---|
725 | n/a | gen.startElement("bar", {"b": "2.0"}) |
---|
726 | n/a | gen.endElement("bar") |
---|
727 | n/a | # Don't call gen.endDocument() |
---|
728 | n/a | |
---|
729 | n/a | self.assertEqual(result.getvalue(), |
---|
730 | n/a | self.xml('<foo a="1.0">Hello</foo><bar b="2.0"></bar>')[len(self.xml('')):]) |
---|
731 | n/a | |
---|
732 | n/a | class StringXmlgenTest(XmlgenTest, unittest.TestCase): |
---|
733 | n/a | ioclass = StringIO |
---|
734 | n/a | |
---|
735 | n/a | def xml(self, doc, encoding='iso-8859-1'): |
---|
736 | n/a | return '<?xml version="1.0" encoding="%s"?>\n%s' % (encoding, doc) |
---|
737 | n/a | |
---|
738 | n/a | test_xmlgen_unencodable = None |
---|
739 | n/a | |
---|
740 | n/a | class BytesXmlgenTest(XmlgenTest, unittest.TestCase): |
---|
741 | n/a | ioclass = BytesIO |
---|
742 | n/a | |
---|
743 | n/a | def xml(self, doc, encoding='iso-8859-1'): |
---|
744 | n/a | return ('<?xml version="1.0" encoding="%s"?>\n%s' % |
---|
745 | n/a | (encoding, doc)).encode(encoding, 'xmlcharrefreplace') |
---|
746 | n/a | |
---|
747 | n/a | class WriterXmlgenTest(BytesXmlgenTest): |
---|
748 | n/a | class ioclass(list): |
---|
749 | n/a | write = list.append |
---|
750 | n/a | closed = False |
---|
751 | n/a | |
---|
752 | n/a | def seekable(self): |
---|
753 | n/a | return True |
---|
754 | n/a | |
---|
755 | n/a | def tell(self): |
---|
756 | n/a | # return 0 at start and not 0 after start |
---|
757 | n/a | return len(self) |
---|
758 | n/a | |
---|
759 | n/a | def getvalue(self): |
---|
760 | n/a | return b''.join(self) |
---|
761 | n/a | |
---|
762 | n/a | class StreamWriterXmlgenTest(XmlgenTest, unittest.TestCase): |
---|
763 | n/a | def ioclass(self): |
---|
764 | n/a | raw = BytesIO() |
---|
765 | n/a | writer = codecs.getwriter('ascii')(raw, 'xmlcharrefreplace') |
---|
766 | n/a | writer.getvalue = raw.getvalue |
---|
767 | n/a | return writer |
---|
768 | n/a | |
---|
769 | n/a | def xml(self, doc, encoding='iso-8859-1'): |
---|
770 | n/a | return ('<?xml version="1.0" encoding="%s"?>\n%s' % |
---|
771 | n/a | (encoding, doc)).encode('ascii', 'xmlcharrefreplace') |
---|
772 | n/a | |
---|
773 | n/a | class StreamReaderWriterXmlgenTest(XmlgenTest, unittest.TestCase): |
---|
774 | n/a | fname = support.TESTFN + '-codecs' |
---|
775 | n/a | |
---|
776 | n/a | def ioclass(self): |
---|
777 | n/a | writer = codecs.open(self.fname, 'w', encoding='ascii', |
---|
778 | n/a | errors='xmlcharrefreplace', buffering=0) |
---|
779 | n/a | def cleanup(): |
---|
780 | n/a | writer.close() |
---|
781 | n/a | support.unlink(self.fname) |
---|
782 | n/a | self.addCleanup(cleanup) |
---|
783 | n/a | def getvalue(): |
---|
784 | n/a | # Windows will not let use reopen without first closing |
---|
785 | n/a | writer.close() |
---|
786 | n/a | with open(writer.name, 'rb') as f: |
---|
787 | n/a | return f.read() |
---|
788 | n/a | writer.getvalue = getvalue |
---|
789 | n/a | return writer |
---|
790 | n/a | |
---|
791 | n/a | def xml(self, doc, encoding='iso-8859-1'): |
---|
792 | n/a | return ('<?xml version="1.0" encoding="%s"?>\n%s' % |
---|
793 | n/a | (encoding, doc)).encode('ascii', 'xmlcharrefreplace') |
---|
794 | n/a | |
---|
795 | n/a | start = b'<?xml version="1.0" encoding="iso-8859-1"?>\n' |
---|
796 | n/a | |
---|
797 | n/a | |
---|
798 | n/a | class XMLFilterBaseTest(unittest.TestCase): |
---|
799 | n/a | def test_filter_basic(self): |
---|
800 | n/a | result = BytesIO() |
---|
801 | n/a | gen = XMLGenerator(result) |
---|
802 | n/a | filter = XMLFilterBase() |
---|
803 | n/a | filter.setContentHandler(gen) |
---|
804 | n/a | |
---|
805 | n/a | filter.startDocument() |
---|
806 | n/a | filter.startElement("doc", {}) |
---|
807 | n/a | filter.characters("content") |
---|
808 | n/a | filter.ignorableWhitespace(" ") |
---|
809 | n/a | filter.endElement("doc") |
---|
810 | n/a | filter.endDocument() |
---|
811 | n/a | |
---|
812 | n/a | self.assertEqual(result.getvalue(), start + b"<doc>content </doc>") |
---|
813 | n/a | |
---|
814 | n/a | # =========================================================================== |
---|
815 | n/a | # |
---|
816 | n/a | # expatreader tests |
---|
817 | n/a | # |
---|
818 | n/a | # =========================================================================== |
---|
819 | n/a | |
---|
820 | n/a | with open(TEST_XMLFILE_OUT, 'rb') as f: |
---|
821 | n/a | xml_test_out = f.read() |
---|
822 | n/a | |
---|
823 | n/a | class ExpatReaderTest(XmlTestBase): |
---|
824 | n/a | |
---|
825 | n/a | # ===== XMLReader support |
---|
826 | n/a | |
---|
827 | n/a | def test_expat_binary_file(self): |
---|
828 | n/a | parser = create_parser() |
---|
829 | n/a | result = BytesIO() |
---|
830 | n/a | xmlgen = XMLGenerator(result) |
---|
831 | n/a | |
---|
832 | n/a | parser.setContentHandler(xmlgen) |
---|
833 | n/a | with open(TEST_XMLFILE, 'rb') as f: |
---|
834 | n/a | parser.parse(f) |
---|
835 | n/a | |
---|
836 | n/a | self.assertEqual(result.getvalue(), xml_test_out) |
---|
837 | n/a | |
---|
838 | n/a | def test_expat_text_file(self): |
---|
839 | n/a | parser = create_parser() |
---|
840 | n/a | result = BytesIO() |
---|
841 | n/a | xmlgen = XMLGenerator(result) |
---|
842 | n/a | |
---|
843 | n/a | parser.setContentHandler(xmlgen) |
---|
844 | n/a | with open(TEST_XMLFILE, 'rt', encoding='iso-8859-1') as f: |
---|
845 | n/a | parser.parse(f) |
---|
846 | n/a | |
---|
847 | n/a | self.assertEqual(result.getvalue(), xml_test_out) |
---|
848 | n/a | |
---|
849 | n/a | @requires_nonascii_filenames |
---|
850 | n/a | def test_expat_binary_file_nonascii(self): |
---|
851 | n/a | fname = support.TESTFN_UNICODE |
---|
852 | n/a | shutil.copyfile(TEST_XMLFILE, fname) |
---|
853 | n/a | self.addCleanup(support.unlink, fname) |
---|
854 | n/a | |
---|
855 | n/a | parser = create_parser() |
---|
856 | n/a | result = BytesIO() |
---|
857 | n/a | xmlgen = XMLGenerator(result) |
---|
858 | n/a | |
---|
859 | n/a | parser.setContentHandler(xmlgen) |
---|
860 | n/a | parser.parse(open(fname, 'rb')) |
---|
861 | n/a | |
---|
862 | n/a | self.assertEqual(result.getvalue(), xml_test_out) |
---|
863 | n/a | |
---|
864 | n/a | def test_expat_binary_file_bytes_name(self): |
---|
865 | n/a | fname = os.fsencode(TEST_XMLFILE) |
---|
866 | n/a | parser = create_parser() |
---|
867 | n/a | result = BytesIO() |
---|
868 | n/a | xmlgen = XMLGenerator(result) |
---|
869 | n/a | |
---|
870 | n/a | parser.setContentHandler(xmlgen) |
---|
871 | n/a | with open(fname, 'rb') as f: |
---|
872 | n/a | parser.parse(f) |
---|
873 | n/a | |
---|
874 | n/a | self.assertEqual(result.getvalue(), xml_test_out) |
---|
875 | n/a | |
---|
876 | n/a | def test_expat_binary_file_int_name(self): |
---|
877 | n/a | parser = create_parser() |
---|
878 | n/a | result = BytesIO() |
---|
879 | n/a | xmlgen = XMLGenerator(result) |
---|
880 | n/a | |
---|
881 | n/a | parser.setContentHandler(xmlgen) |
---|
882 | n/a | with open(TEST_XMLFILE, 'rb') as f: |
---|
883 | n/a | with open(f.fileno(), 'rb', closefd=False) as f2: |
---|
884 | n/a | parser.parse(f2) |
---|
885 | n/a | |
---|
886 | n/a | self.assertEqual(result.getvalue(), xml_test_out) |
---|
887 | n/a | |
---|
888 | n/a | # ===== DTDHandler support |
---|
889 | n/a | |
---|
890 | n/a | class TestDTDHandler: |
---|
891 | n/a | |
---|
892 | n/a | def __init__(self): |
---|
893 | n/a | self._notations = [] |
---|
894 | n/a | self._entities = [] |
---|
895 | n/a | |
---|
896 | n/a | def notationDecl(self, name, publicId, systemId): |
---|
897 | n/a | self._notations.append((name, publicId, systemId)) |
---|
898 | n/a | |
---|
899 | n/a | def unparsedEntityDecl(self, name, publicId, systemId, ndata): |
---|
900 | n/a | self._entities.append((name, publicId, systemId, ndata)) |
---|
901 | n/a | |
---|
902 | n/a | def test_expat_dtdhandler(self): |
---|
903 | n/a | parser = create_parser() |
---|
904 | n/a | handler = self.TestDTDHandler() |
---|
905 | n/a | parser.setDTDHandler(handler) |
---|
906 | n/a | |
---|
907 | n/a | parser.feed('<!DOCTYPE doc [\n') |
---|
908 | n/a | parser.feed(' <!ENTITY img SYSTEM "expat.gif" NDATA GIF>\n') |
---|
909 | n/a | parser.feed(' <!NOTATION GIF PUBLIC "-//CompuServe//NOTATION Graphics Interchange Format 89a//EN">\n') |
---|
910 | n/a | parser.feed(']>\n') |
---|
911 | n/a | parser.feed('<doc></doc>') |
---|
912 | n/a | parser.close() |
---|
913 | n/a | |
---|
914 | n/a | self.assertEqual(handler._notations, |
---|
915 | n/a | [("GIF", "-//CompuServe//NOTATION Graphics Interchange Format 89a//EN", None)]) |
---|
916 | n/a | self.assertEqual(handler._entities, [("img", None, "expat.gif", "GIF")]) |
---|
917 | n/a | |
---|
918 | n/a | # ===== EntityResolver support |
---|
919 | n/a | |
---|
920 | n/a | class TestEntityResolver: |
---|
921 | n/a | |
---|
922 | n/a | def resolveEntity(self, publicId, systemId): |
---|
923 | n/a | inpsrc = InputSource() |
---|
924 | n/a | inpsrc.setByteStream(BytesIO(b"<entity/>")) |
---|
925 | n/a | return inpsrc |
---|
926 | n/a | |
---|
927 | n/a | def test_expat_entityresolver(self): |
---|
928 | n/a | parser = create_parser() |
---|
929 | n/a | parser.setEntityResolver(self.TestEntityResolver()) |
---|
930 | n/a | result = BytesIO() |
---|
931 | n/a | parser.setContentHandler(XMLGenerator(result)) |
---|
932 | n/a | |
---|
933 | n/a | parser.feed('<!DOCTYPE doc [\n') |
---|
934 | n/a | parser.feed(' <!ENTITY test SYSTEM "whatever">\n') |
---|
935 | n/a | parser.feed(']>\n') |
---|
936 | n/a | parser.feed('<doc>&test;</doc>') |
---|
937 | n/a | parser.close() |
---|
938 | n/a | |
---|
939 | n/a | self.assertEqual(result.getvalue(), start + |
---|
940 | n/a | b"<doc><entity></entity></doc>") |
---|
941 | n/a | |
---|
942 | n/a | # ===== Attributes support |
---|
943 | n/a | |
---|
944 | n/a | class AttrGatherer(ContentHandler): |
---|
945 | n/a | |
---|
946 | n/a | def startElement(self, name, attrs): |
---|
947 | n/a | self._attrs = attrs |
---|
948 | n/a | |
---|
949 | n/a | def startElementNS(self, name, qname, attrs): |
---|
950 | n/a | self._attrs = attrs |
---|
951 | n/a | |
---|
952 | n/a | def test_expat_attrs_empty(self): |
---|
953 | n/a | parser = create_parser() |
---|
954 | n/a | gather = self.AttrGatherer() |
---|
955 | n/a | parser.setContentHandler(gather) |
---|
956 | n/a | |
---|
957 | n/a | parser.feed("<doc/>") |
---|
958 | n/a | parser.close() |
---|
959 | n/a | |
---|
960 | n/a | self.verify_empty_attrs(gather._attrs) |
---|
961 | n/a | |
---|
962 | n/a | def test_expat_attrs_wattr(self): |
---|
963 | n/a | parser = create_parser() |
---|
964 | n/a | gather = self.AttrGatherer() |
---|
965 | n/a | parser.setContentHandler(gather) |
---|
966 | n/a | |
---|
967 | n/a | parser.feed("<doc attr='val'/>") |
---|
968 | n/a | parser.close() |
---|
969 | n/a | |
---|
970 | n/a | self.verify_attrs_wattr(gather._attrs) |
---|
971 | n/a | |
---|
972 | n/a | def test_expat_nsattrs_empty(self): |
---|
973 | n/a | parser = create_parser(1) |
---|
974 | n/a | gather = self.AttrGatherer() |
---|
975 | n/a | parser.setContentHandler(gather) |
---|
976 | n/a | |
---|
977 | n/a | parser.feed("<doc/>") |
---|
978 | n/a | parser.close() |
---|
979 | n/a | |
---|
980 | n/a | self.verify_empty_nsattrs(gather._attrs) |
---|
981 | n/a | |
---|
982 | n/a | def test_expat_nsattrs_wattr(self): |
---|
983 | n/a | parser = create_parser(1) |
---|
984 | n/a | gather = self.AttrGatherer() |
---|
985 | n/a | parser.setContentHandler(gather) |
---|
986 | n/a | |
---|
987 | n/a | parser.feed("<doc xmlns:ns='%s' ns:attr='val'/>" % ns_uri) |
---|
988 | n/a | parser.close() |
---|
989 | n/a | |
---|
990 | n/a | attrs = gather._attrs |
---|
991 | n/a | |
---|
992 | n/a | self.assertEqual(attrs.getLength(), 1) |
---|
993 | n/a | self.assertEqual(attrs.getNames(), [(ns_uri, "attr")]) |
---|
994 | n/a | self.assertTrue((attrs.getQNames() == [] or |
---|
995 | n/a | attrs.getQNames() == ["ns:attr"])) |
---|
996 | n/a | self.assertEqual(len(attrs), 1) |
---|
997 | n/a | self.assertIn((ns_uri, "attr"), attrs) |
---|
998 | n/a | self.assertEqual(attrs.get((ns_uri, "attr")), "val") |
---|
999 | n/a | self.assertEqual(attrs.get((ns_uri, "attr"), 25), "val") |
---|
1000 | n/a | self.assertEqual(list(attrs.items()), [((ns_uri, "attr"), "val")]) |
---|
1001 | n/a | self.assertEqual(list(attrs.values()), ["val"]) |
---|
1002 | n/a | self.assertEqual(attrs.getValue((ns_uri, "attr")), "val") |
---|
1003 | n/a | self.assertEqual(attrs[(ns_uri, "attr")], "val") |
---|
1004 | n/a | |
---|
1005 | n/a | # ===== InputSource support |
---|
1006 | n/a | |
---|
1007 | n/a | def test_expat_inpsource_filename(self): |
---|
1008 | n/a | parser = create_parser() |
---|
1009 | n/a | result = BytesIO() |
---|
1010 | n/a | xmlgen = XMLGenerator(result) |
---|
1011 | n/a | |
---|
1012 | n/a | parser.setContentHandler(xmlgen) |
---|
1013 | n/a | parser.parse(TEST_XMLFILE) |
---|
1014 | n/a | |
---|
1015 | n/a | self.assertEqual(result.getvalue(), xml_test_out) |
---|
1016 | n/a | |
---|
1017 | n/a | def test_expat_inpsource_sysid(self): |
---|
1018 | n/a | parser = create_parser() |
---|
1019 | n/a | result = BytesIO() |
---|
1020 | n/a | xmlgen = XMLGenerator(result) |
---|
1021 | n/a | |
---|
1022 | n/a | parser.setContentHandler(xmlgen) |
---|
1023 | n/a | parser.parse(InputSource(TEST_XMLFILE)) |
---|
1024 | n/a | |
---|
1025 | n/a | self.assertEqual(result.getvalue(), xml_test_out) |
---|
1026 | n/a | |
---|
1027 | n/a | @requires_nonascii_filenames |
---|
1028 | n/a | def test_expat_inpsource_sysid_nonascii(self): |
---|
1029 | n/a | fname = support.TESTFN_UNICODE |
---|
1030 | n/a | shutil.copyfile(TEST_XMLFILE, fname) |
---|
1031 | n/a | self.addCleanup(support.unlink, fname) |
---|
1032 | n/a | |
---|
1033 | n/a | parser = create_parser() |
---|
1034 | n/a | result = BytesIO() |
---|
1035 | n/a | xmlgen = XMLGenerator(result) |
---|
1036 | n/a | |
---|
1037 | n/a | parser.setContentHandler(xmlgen) |
---|
1038 | n/a | parser.parse(InputSource(fname)) |
---|
1039 | n/a | |
---|
1040 | n/a | self.assertEqual(result.getvalue(), xml_test_out) |
---|
1041 | n/a | |
---|
1042 | n/a | def test_expat_inpsource_byte_stream(self): |
---|
1043 | n/a | parser = create_parser() |
---|
1044 | n/a | result = BytesIO() |
---|
1045 | n/a | xmlgen = XMLGenerator(result) |
---|
1046 | n/a | |
---|
1047 | n/a | parser.setContentHandler(xmlgen) |
---|
1048 | n/a | inpsrc = InputSource() |
---|
1049 | n/a | with open(TEST_XMLFILE, 'rb') as f: |
---|
1050 | n/a | inpsrc.setByteStream(f) |
---|
1051 | n/a | parser.parse(inpsrc) |
---|
1052 | n/a | |
---|
1053 | n/a | self.assertEqual(result.getvalue(), xml_test_out) |
---|
1054 | n/a | |
---|
1055 | n/a | def test_expat_inpsource_character_stream(self): |
---|
1056 | n/a | parser = create_parser() |
---|
1057 | n/a | result = BytesIO() |
---|
1058 | n/a | xmlgen = XMLGenerator(result) |
---|
1059 | n/a | |
---|
1060 | n/a | parser.setContentHandler(xmlgen) |
---|
1061 | n/a | inpsrc = InputSource() |
---|
1062 | n/a | with open(TEST_XMLFILE, 'rt', encoding='iso-8859-1') as f: |
---|
1063 | n/a | inpsrc.setCharacterStream(f) |
---|
1064 | n/a | parser.parse(inpsrc) |
---|
1065 | n/a | |
---|
1066 | n/a | self.assertEqual(result.getvalue(), xml_test_out) |
---|
1067 | n/a | |
---|
1068 | n/a | # ===== IncrementalParser support |
---|
1069 | n/a | |
---|
1070 | n/a | def test_expat_incremental(self): |
---|
1071 | n/a | result = BytesIO() |
---|
1072 | n/a | xmlgen = XMLGenerator(result) |
---|
1073 | n/a | parser = create_parser() |
---|
1074 | n/a | parser.setContentHandler(xmlgen) |
---|
1075 | n/a | |
---|
1076 | n/a | parser.feed("<doc>") |
---|
1077 | n/a | parser.feed("</doc>") |
---|
1078 | n/a | parser.close() |
---|
1079 | n/a | |
---|
1080 | n/a | self.assertEqual(result.getvalue(), start + b"<doc></doc>") |
---|
1081 | n/a | |
---|
1082 | n/a | def test_expat_incremental_reset(self): |
---|
1083 | n/a | result = BytesIO() |
---|
1084 | n/a | xmlgen = XMLGenerator(result) |
---|
1085 | n/a | parser = create_parser() |
---|
1086 | n/a | parser.setContentHandler(xmlgen) |
---|
1087 | n/a | |
---|
1088 | n/a | parser.feed("<doc>") |
---|
1089 | n/a | parser.feed("text") |
---|
1090 | n/a | |
---|
1091 | n/a | result = BytesIO() |
---|
1092 | n/a | xmlgen = XMLGenerator(result) |
---|
1093 | n/a | parser.setContentHandler(xmlgen) |
---|
1094 | n/a | parser.reset() |
---|
1095 | n/a | |
---|
1096 | n/a | parser.feed("<doc>") |
---|
1097 | n/a | parser.feed("text") |
---|
1098 | n/a | parser.feed("</doc>") |
---|
1099 | n/a | parser.close() |
---|
1100 | n/a | |
---|
1101 | n/a | self.assertEqual(result.getvalue(), start + b"<doc>text</doc>") |
---|
1102 | n/a | |
---|
1103 | n/a | # ===== Locator support |
---|
1104 | n/a | |
---|
1105 | n/a | def test_expat_locator_noinfo(self): |
---|
1106 | n/a | result = BytesIO() |
---|
1107 | n/a | xmlgen = XMLGenerator(result) |
---|
1108 | n/a | parser = create_parser() |
---|
1109 | n/a | parser.setContentHandler(xmlgen) |
---|
1110 | n/a | |
---|
1111 | n/a | parser.feed("<doc>") |
---|
1112 | n/a | parser.feed("</doc>") |
---|
1113 | n/a | parser.close() |
---|
1114 | n/a | |
---|
1115 | n/a | self.assertEqual(parser.getSystemId(), None) |
---|
1116 | n/a | self.assertEqual(parser.getPublicId(), None) |
---|
1117 | n/a | self.assertEqual(parser.getLineNumber(), 1) |
---|
1118 | n/a | |
---|
1119 | n/a | def test_expat_locator_withinfo(self): |
---|
1120 | n/a | result = BytesIO() |
---|
1121 | n/a | xmlgen = XMLGenerator(result) |
---|
1122 | n/a | parser = create_parser() |
---|
1123 | n/a | parser.setContentHandler(xmlgen) |
---|
1124 | n/a | parser.parse(TEST_XMLFILE) |
---|
1125 | n/a | |
---|
1126 | n/a | self.assertEqual(parser.getSystemId(), TEST_XMLFILE) |
---|
1127 | n/a | self.assertEqual(parser.getPublicId(), None) |
---|
1128 | n/a | |
---|
1129 | n/a | @requires_nonascii_filenames |
---|
1130 | n/a | def test_expat_locator_withinfo_nonascii(self): |
---|
1131 | n/a | fname = support.TESTFN_UNICODE |
---|
1132 | n/a | shutil.copyfile(TEST_XMLFILE, fname) |
---|
1133 | n/a | self.addCleanup(support.unlink, fname) |
---|
1134 | n/a | |
---|
1135 | n/a | result = BytesIO() |
---|
1136 | n/a | xmlgen = XMLGenerator(result) |
---|
1137 | n/a | parser = create_parser() |
---|
1138 | n/a | parser.setContentHandler(xmlgen) |
---|
1139 | n/a | parser.parse(fname) |
---|
1140 | n/a | |
---|
1141 | n/a | self.assertEqual(parser.getSystemId(), fname) |
---|
1142 | n/a | self.assertEqual(parser.getPublicId(), None) |
---|
1143 | n/a | |
---|
1144 | n/a | |
---|
1145 | n/a | # =========================================================================== |
---|
1146 | n/a | # |
---|
1147 | n/a | # error reporting |
---|
1148 | n/a | # |
---|
1149 | n/a | # =========================================================================== |
---|
1150 | n/a | |
---|
1151 | n/a | class ErrorReportingTest(unittest.TestCase): |
---|
1152 | n/a | def test_expat_inpsource_location(self): |
---|
1153 | n/a | parser = create_parser() |
---|
1154 | n/a | parser.setContentHandler(ContentHandler()) # do nothing |
---|
1155 | n/a | source = InputSource() |
---|
1156 | n/a | source.setByteStream(BytesIO(b"<foo bar foobar>")) #ill-formed |
---|
1157 | n/a | name = "a file name" |
---|
1158 | n/a | source.setSystemId(name) |
---|
1159 | n/a | try: |
---|
1160 | n/a | parser.parse(source) |
---|
1161 | n/a | self.fail() |
---|
1162 | n/a | except SAXException as e: |
---|
1163 | n/a | self.assertEqual(e.getSystemId(), name) |
---|
1164 | n/a | |
---|
1165 | n/a | def test_expat_incomplete(self): |
---|
1166 | n/a | parser = create_parser() |
---|
1167 | n/a | parser.setContentHandler(ContentHandler()) # do nothing |
---|
1168 | n/a | self.assertRaises(SAXParseException, parser.parse, StringIO("<foo>")) |
---|
1169 | n/a | self.assertEqual(parser.getColumnNumber(), 5) |
---|
1170 | n/a | self.assertEqual(parser.getLineNumber(), 1) |
---|
1171 | n/a | |
---|
1172 | n/a | def test_sax_parse_exception_str(self): |
---|
1173 | n/a | # pass various values from a locator to the SAXParseException to |
---|
1174 | n/a | # make sure that the __str__() doesn't fall apart when None is |
---|
1175 | n/a | # passed instead of an integer line and column number |
---|
1176 | n/a | # |
---|
1177 | n/a | # use "normal" values for the locator: |
---|
1178 | n/a | str(SAXParseException("message", None, |
---|
1179 | n/a | self.DummyLocator(1, 1))) |
---|
1180 | n/a | # use None for the line number: |
---|
1181 | n/a | str(SAXParseException("message", None, |
---|
1182 | n/a | self.DummyLocator(None, 1))) |
---|
1183 | n/a | # use None for the column number: |
---|
1184 | n/a | str(SAXParseException("message", None, |
---|
1185 | n/a | self.DummyLocator(1, None))) |
---|
1186 | n/a | # use None for both: |
---|
1187 | n/a | str(SAXParseException("message", None, |
---|
1188 | n/a | self.DummyLocator(None, None))) |
---|
1189 | n/a | |
---|
1190 | n/a | class DummyLocator: |
---|
1191 | n/a | def __init__(self, lineno, colno): |
---|
1192 | n/a | self._lineno = lineno |
---|
1193 | n/a | self._colno = colno |
---|
1194 | n/a | |
---|
1195 | n/a | def getPublicId(self): |
---|
1196 | n/a | return "pubid" |
---|
1197 | n/a | |
---|
1198 | n/a | def getSystemId(self): |
---|
1199 | n/a | return "sysid" |
---|
1200 | n/a | |
---|
1201 | n/a | def getLineNumber(self): |
---|
1202 | n/a | return self._lineno |
---|
1203 | n/a | |
---|
1204 | n/a | def getColumnNumber(self): |
---|
1205 | n/a | return self._colno |
---|
1206 | n/a | |
---|
1207 | n/a | # =========================================================================== |
---|
1208 | n/a | # |
---|
1209 | n/a | # xmlreader tests |
---|
1210 | n/a | # |
---|
1211 | n/a | # =========================================================================== |
---|
1212 | n/a | |
---|
1213 | n/a | class XmlReaderTest(XmlTestBase): |
---|
1214 | n/a | |
---|
1215 | n/a | # ===== AttributesImpl |
---|
1216 | n/a | def test_attrs_empty(self): |
---|
1217 | n/a | self.verify_empty_attrs(AttributesImpl({})) |
---|
1218 | n/a | |
---|
1219 | n/a | def test_attrs_wattr(self): |
---|
1220 | n/a | self.verify_attrs_wattr(AttributesImpl({"attr" : "val"})) |
---|
1221 | n/a | |
---|
1222 | n/a | def test_nsattrs_empty(self): |
---|
1223 | n/a | self.verify_empty_nsattrs(AttributesNSImpl({}, {})) |
---|
1224 | n/a | |
---|
1225 | n/a | def test_nsattrs_wattr(self): |
---|
1226 | n/a | attrs = AttributesNSImpl({(ns_uri, "attr") : "val"}, |
---|
1227 | n/a | {(ns_uri, "attr") : "ns:attr"}) |
---|
1228 | n/a | |
---|
1229 | n/a | self.assertEqual(attrs.getLength(), 1) |
---|
1230 | n/a | self.assertEqual(attrs.getNames(), [(ns_uri, "attr")]) |
---|
1231 | n/a | self.assertEqual(attrs.getQNames(), ["ns:attr"]) |
---|
1232 | n/a | self.assertEqual(len(attrs), 1) |
---|
1233 | n/a | self.assertIn((ns_uri, "attr"), attrs) |
---|
1234 | n/a | self.assertEqual(list(attrs.keys()), [(ns_uri, "attr")]) |
---|
1235 | n/a | self.assertEqual(attrs.get((ns_uri, "attr")), "val") |
---|
1236 | n/a | self.assertEqual(attrs.get((ns_uri, "attr"), 25), "val") |
---|
1237 | n/a | self.assertEqual(list(attrs.items()), [((ns_uri, "attr"), "val")]) |
---|
1238 | n/a | self.assertEqual(list(attrs.values()), ["val"]) |
---|
1239 | n/a | self.assertEqual(attrs.getValue((ns_uri, "attr")), "val") |
---|
1240 | n/a | self.assertEqual(attrs.getValueByQName("ns:attr"), "val") |
---|
1241 | n/a | self.assertEqual(attrs.getNameByQName("ns:attr"), (ns_uri, "attr")) |
---|
1242 | n/a | self.assertEqual(attrs[(ns_uri, "attr")], "val") |
---|
1243 | n/a | self.assertEqual(attrs.getQNameByName((ns_uri, "attr")), "ns:attr") |
---|
1244 | n/a | |
---|
1245 | n/a | |
---|
1246 | n/a | def test_main(): |
---|
1247 | n/a | run_unittest(MakeParserTest, |
---|
1248 | n/a | ParseTest, |
---|
1249 | n/a | SaxutilsTest, |
---|
1250 | n/a | PrepareInputSourceTest, |
---|
1251 | n/a | StringXmlgenTest, |
---|
1252 | n/a | BytesXmlgenTest, |
---|
1253 | n/a | WriterXmlgenTest, |
---|
1254 | n/a | StreamWriterXmlgenTest, |
---|
1255 | n/a | StreamReaderWriterXmlgenTest, |
---|
1256 | n/a | ExpatReaderTest, |
---|
1257 | n/a | ErrorReportingTest, |
---|
1258 | n/a | XmlReaderTest) |
---|
1259 | n/a | |
---|
1260 | n/a | if __name__ == "__main__": |
---|
1261 | n/a | test_main() |
---|