Antoine Pitrou | d72402e | 2010-10-27 18:52:48 +0000 | [diff] [blame] | 1 | # regression test for SAX 2.0 |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 2 | # $Id$ |
| 3 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4 | from xml.sax import make_parser, ContentHandler, \ |
| 5 | SAXException, SAXReaderNotAvailable, SAXParseException |
R David Murray | a846f5a | 2013-03-18 00:18:12 -0400 | [diff] [blame] | 6 | import unittest |
Martin v. Löwis | 962c9e7 | 2000-10-06 17:41:52 +0000 | [diff] [blame] | 7 | try: |
| 8 | make_parser() |
Martin v. Löwis | 80670bc | 2000-10-06 21:13:23 +0000 | [diff] [blame] | 9 | except SAXReaderNotAvailable: |
Martin v. Löwis | 962c9e7 | 2000-10-06 17:41:52 +0000 | [diff] [blame] | 10 | # don't try to test this module if we cannot create a parser |
R David Murray | a846f5a | 2013-03-18 00:18:12 -0400 | [diff] [blame] | 11 | raise unittest.SkipTest("no XML parsers available") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12 | from xml.sax.saxutils import XMLGenerator, escape, unescape, quoteattr, \ |
| 13 | XMLFilterBase |
| 14 | from xml.sax.expatreader import create_parser |
Antoine Pitrou | 6b03ee6 | 2010-10-27 18:33:30 +0000 | [diff] [blame] | 15 | from xml.sax.handler import feature_namespaces |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 16 | from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 17 | from io import BytesIO, StringIO |
Georg Brandl | c502df4 | 2013-05-12 11:41:12 +0200 | [diff] [blame^] | 18 | import codecs |
Serhiy Storchaka | 1a4ed4c | 2013-02-02 12:17:05 +0200 | [diff] [blame] | 19 | import os.path |
Serhiy Storchaka | d520239 | 2013-02-02 10:31:17 +0200 | [diff] [blame] | 20 | import shutil |
| 21 | from test import support |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 22 | from test.support import findfile, run_unittest |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 23 | |
| 24 | TEST_XMLFILE = findfile("test.xml", subdir="xmltestdata") |
| 25 | TEST_XMLFILE_OUT = findfile("test.xml.out", subdir="xmltestdata") |
Victor Stinner | 6c6f851 | 2010-08-07 10:09:35 +0000 | [diff] [blame] | 26 | try: |
| 27 | TEST_XMLFILE.encode("utf8") |
| 28 | TEST_XMLFILE_OUT.encode("utf8") |
| 29 | except UnicodeEncodeError: |
| 30 | raise unittest.SkipTest("filename is not encodable to utf8") |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 31 | |
Serhiy Storchaka | 1a4ed4c | 2013-02-02 12:17:05 +0200 | [diff] [blame] | 32 | supports_nonascii_filenames = True |
| 33 | if not os.path.supports_unicode_filenames: |
| 34 | try: |
| 35 | support.TESTFN_UNICODE.encode(support.TESTFN_ENCODING) |
| 36 | except (UnicodeError, TypeError): |
| 37 | # Either the file system encoding is None, or the file name |
| 38 | # cannot be encoded in the file system encoding. |
| 39 | supports_nonascii_filenames = False |
| 40 | requires_nonascii_filenames = unittest.skipUnless( |
| 41 | supports_nonascii_filenames, |
| 42 | 'Requires non-ascii filenames support') |
| 43 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 44 | ns_uri = "http://www.python.org/xml-ns/saxtest/" |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 46 | class XmlTestBase(unittest.TestCase): |
| 47 | def verify_empty_attrs(self, attrs): |
| 48 | self.assertRaises(KeyError, attrs.getValue, "attr") |
| 49 | self.assertRaises(KeyError, attrs.getValueByQName, "attr") |
| 50 | self.assertRaises(KeyError, attrs.getNameByQName, "attr") |
| 51 | self.assertRaises(KeyError, attrs.getQNameByName, "attr") |
| 52 | self.assertRaises(KeyError, attrs.__getitem__, "attr") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 53 | self.assertEqual(attrs.getLength(), 0) |
| 54 | self.assertEqual(attrs.getNames(), []) |
| 55 | self.assertEqual(attrs.getQNames(), []) |
| 56 | self.assertEqual(len(attrs), 0) |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 57 | self.assertNotIn("attr", attrs) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 58 | self.assertEqual(list(attrs.keys()), []) |
| 59 | self.assertEqual(attrs.get("attrs"), None) |
| 60 | self.assertEqual(attrs.get("attrs", 25), 25) |
| 61 | self.assertEqual(list(attrs.items()), []) |
| 62 | self.assertEqual(list(attrs.values()), []) |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 64 | def verify_empty_nsattrs(self, attrs): |
| 65 | self.assertRaises(KeyError, attrs.getValue, (ns_uri, "attr")) |
| 66 | self.assertRaises(KeyError, attrs.getValueByQName, "ns:attr") |
| 67 | self.assertRaises(KeyError, attrs.getNameByQName, "ns:attr") |
| 68 | self.assertRaises(KeyError, attrs.getQNameByName, (ns_uri, "attr")) |
| 69 | self.assertRaises(KeyError, attrs.__getitem__, (ns_uri, "attr")) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 70 | self.assertEqual(attrs.getLength(), 0) |
| 71 | self.assertEqual(attrs.getNames(), []) |
| 72 | self.assertEqual(attrs.getQNames(), []) |
| 73 | self.assertEqual(len(attrs), 0) |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 74 | self.assertNotIn((ns_uri, "attr"), attrs) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 75 | self.assertEqual(list(attrs.keys()), []) |
| 76 | self.assertEqual(attrs.get((ns_uri, "attr")), None) |
| 77 | self.assertEqual(attrs.get((ns_uri, "attr"), 25), 25) |
| 78 | self.assertEqual(list(attrs.items()), []) |
| 79 | self.assertEqual(list(attrs.values()), []) |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 80 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 81 | def verify_attrs_wattr(self, attrs): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 82 | self.assertEqual(attrs.getLength(), 1) |
| 83 | self.assertEqual(attrs.getNames(), ["attr"]) |
| 84 | self.assertEqual(attrs.getQNames(), ["attr"]) |
| 85 | self.assertEqual(len(attrs), 1) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 86 | self.assertIn("attr", attrs) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 87 | self.assertEqual(list(attrs.keys()), ["attr"]) |
| 88 | self.assertEqual(attrs.get("attr"), "val") |
| 89 | self.assertEqual(attrs.get("attr", 25), "val") |
| 90 | self.assertEqual(list(attrs.items()), [("attr", "val")]) |
| 91 | self.assertEqual(list(attrs.values()), ["val"]) |
| 92 | self.assertEqual(attrs.getValue("attr"), "val") |
| 93 | self.assertEqual(attrs.getValueByQName("attr"), "val") |
| 94 | self.assertEqual(attrs.getNameByQName("attr"), "attr") |
| 95 | self.assertEqual(attrs["attr"], "val") |
| 96 | self.assertEqual(attrs.getQNameByName("attr"), "attr") |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 97 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 98 | class MakeParserTest(unittest.TestCase): |
| 99 | def test_make_parser2(self): |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 100 | # Creating parsers several times in a row should succeed. |
| 101 | # Testing this because there have been failures of this kind |
| 102 | # before. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 103 | from xml.sax import make_parser |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 104 | p = make_parser() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 105 | from xml.sax import make_parser |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 106 | p = make_parser() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 107 | from xml.sax import make_parser |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 108 | p = make_parser() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 109 | from xml.sax import make_parser |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 110 | p = make_parser() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 111 | from xml.sax import make_parser |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 112 | p = make_parser() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 113 | from xml.sax import make_parser |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 114 | p = make_parser() |
Tim Peters | d2bf3b7 | 2001-01-18 02:22:22 +0000 | [diff] [blame] | 115 | |
| 116 | |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 117 | # =========================================================================== |
| 118 | # |
| 119 | # saxutils tests |
| 120 | # |
| 121 | # =========================================================================== |
| 122 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 123 | class SaxutilsTest(unittest.TestCase): |
| 124 | # ===== escape |
| 125 | def test_escape_basic(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 126 | self.assertEqual(escape("Donald Duck & Co"), "Donald Duck & Co") |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 127 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 128 | def test_escape_all(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 129 | self.assertEqual(escape("<Donald Duck & Co>"), |
| 130 | "<Donald Duck & Co>") |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 131 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 132 | def test_escape_extra(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 133 | self.assertEqual(escape("Hei på deg", {"å" : "å"}), |
| 134 | "Hei på deg") |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 135 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 136 | # ===== unescape |
| 137 | def test_unescape_basic(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 138 | self.assertEqual(unescape("Donald Duck & Co"), "Donald Duck & Co") |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 139 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 140 | def test_unescape_all(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 141 | self.assertEqual(unescape("<Donald Duck & Co>"), |
| 142 | "<Donald Duck & Co>") |
Martin v. Löwis | 74b51ac | 2002-10-26 14:50:45 +0000 | [diff] [blame] | 143 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 144 | def test_unescape_extra(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 145 | self.assertEqual(unescape("Hei på deg", {"å" : "å"}), |
| 146 | "Hei på deg") |
Martin v. Löwis | 74b51ac | 2002-10-26 14:50:45 +0000 | [diff] [blame] | 147 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 148 | def test_unescape_amp_extra(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 149 | self.assertEqual(unescape("&foo;", {"&foo;": "splat"}), "&foo;") |
Martin v. Löwis | 74b51ac | 2002-10-26 14:50:45 +0000 | [diff] [blame] | 150 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 151 | # ===== quoteattr |
| 152 | def test_quoteattr_basic(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 153 | self.assertEqual(quoteattr("Donald Duck & Co"), |
| 154 | '"Donald Duck & Co"') |
Martin v. Löwis | 74b51ac | 2002-10-26 14:50:45 +0000 | [diff] [blame] | 155 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 156 | def test_single_quoteattr(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 157 | self.assertEqual(quoteattr('Includes "double" quotes'), |
| 158 | '\'Includes "double" quotes\'') |
Fred Drake | 32f3add | 2002-10-28 17:58:48 +0000 | [diff] [blame] | 159 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 160 | def test_double_quoteattr(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 161 | self.assertEqual(quoteattr("Includes 'single' quotes"), |
| 162 | "\"Includes 'single' quotes\"") |
Fred Drake | acd32d3 | 2001-07-19 16:10:15 +0000 | [diff] [blame] | 163 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 164 | def test_single_double_quoteattr(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 165 | self.assertEqual(quoteattr("Includes 'single' and \"double\" quotes"), |
| 166 | "\"Includes 'single' and "double" quotes\"") |
Fred Drake | acd32d3 | 2001-07-19 16:10:15 +0000 | [diff] [blame] | 167 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 168 | # ===== make_parser |
| 169 | def test_make_parser(self): |
Martin v. Löwis | 962c9e7 | 2000-10-06 17:41:52 +0000 | [diff] [blame] | 170 | # Creating a parser should succeed - it should fall back |
| 171 | # to the expatreader |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 172 | p = make_parser(['xml.parsers.no_such_parser']) |
Martin v. Löwis | 962c9e7 | 2000-10-06 17:41:52 +0000 | [diff] [blame] | 173 | |
| 174 | |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 175 | # ===== XMLGenerator |
| 176 | |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 177 | class XmlgenTest: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 178 | def test_xmlgen_basic(self): |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 179 | result = self.ioclass() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 180 | gen = XMLGenerator(result) |
| 181 | gen.startDocument() |
| 182 | gen.startElement("doc", {}) |
| 183 | gen.endElement("doc") |
| 184 | gen.endDocument() |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 185 | |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 186 | self.assertEqual(result.getvalue(), self.xml("<doc></doc>")) |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 187 | |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 188 | def test_xmlgen_basic_empty(self): |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 189 | result = self.ioclass() |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 190 | gen = XMLGenerator(result, short_empty_elements=True) |
| 191 | gen.startDocument() |
| 192 | gen.startElement("doc", {}) |
| 193 | gen.endElement("doc") |
| 194 | gen.endDocument() |
| 195 | |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 196 | self.assertEqual(result.getvalue(), self.xml("<doc/>")) |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 197 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 198 | def test_xmlgen_content(self): |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 199 | result = self.ioclass() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 200 | gen = XMLGenerator(result) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 201 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 202 | gen.startDocument() |
| 203 | gen.startElement("doc", {}) |
| 204 | gen.characters("huhei") |
| 205 | gen.endElement("doc") |
| 206 | gen.endDocument() |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 207 | |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 208 | self.assertEqual(result.getvalue(), self.xml("<doc>huhei</doc>")) |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 209 | |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 210 | def test_xmlgen_content_empty(self): |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 211 | result = self.ioclass() |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 212 | gen = XMLGenerator(result, short_empty_elements=True) |
| 213 | |
| 214 | gen.startDocument() |
| 215 | gen.startElement("doc", {}) |
| 216 | gen.characters("huhei") |
| 217 | gen.endElement("doc") |
| 218 | gen.endDocument() |
| 219 | |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 220 | self.assertEqual(result.getvalue(), self.xml("<doc>huhei</doc>")) |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 221 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 222 | def test_xmlgen_pi(self): |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 223 | result = self.ioclass() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 224 | gen = XMLGenerator(result) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 225 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 226 | gen.startDocument() |
| 227 | gen.processingInstruction("test", "data") |
| 228 | gen.startElement("doc", {}) |
| 229 | gen.endElement("doc") |
| 230 | gen.endDocument() |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 231 | |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 232 | self.assertEqual(result.getvalue(), |
| 233 | self.xml("<?test data?><doc></doc>")) |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 234 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 235 | def test_xmlgen_content_escape(self): |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 236 | result = self.ioclass() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 237 | gen = XMLGenerator(result) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 238 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 239 | gen.startDocument() |
| 240 | gen.startElement("doc", {}) |
| 241 | gen.characters("<huhei&") |
| 242 | gen.endElement("doc") |
| 243 | gen.endDocument() |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 244 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 245 | self.assertEqual(result.getvalue(), |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 246 | self.xml("<doc><huhei&</doc>")) |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 247 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 248 | def test_xmlgen_attr_escape(self): |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 249 | result = self.ioclass() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 250 | gen = XMLGenerator(result) |
Fred Drake | c9fadf9 | 2001-08-07 19:17:06 +0000 | [diff] [blame] | 251 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 252 | gen.startDocument() |
| 253 | gen.startElement("doc", {"a": '"'}) |
| 254 | gen.startElement("e", {"a": "'"}) |
| 255 | gen.endElement("e") |
| 256 | gen.startElement("e", {"a": "'\""}) |
| 257 | gen.endElement("e") |
| 258 | gen.startElement("e", {"a": "\n\r\t"}) |
| 259 | gen.endElement("e") |
| 260 | gen.endElement("doc") |
| 261 | gen.endDocument() |
Fred Drake | c9fadf9 | 2001-08-07 19:17:06 +0000 | [diff] [blame] | 262 | |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 263 | self.assertEqual(result.getvalue(), self.xml( |
| 264 | "<doc a='\"'><e a=\"'\"></e>" |
| 265 | "<e a=\"'"\"></e>" |
| 266 | "<e a=\" 	\"></e></doc>")) |
| 267 | |
| 268 | def test_xmlgen_encoding(self): |
| 269 | encodings = ('iso-8859-15', 'utf-8', 'utf-8-sig', |
| 270 | 'utf-16', 'utf-16be', 'utf-16le', |
| 271 | 'utf-32', 'utf-32be', 'utf-32le') |
| 272 | for encoding in encodings: |
| 273 | result = self.ioclass() |
| 274 | gen = XMLGenerator(result, encoding=encoding) |
| 275 | |
| 276 | gen.startDocument() |
| 277 | gen.startElement("doc", {"a": '\u20ac'}) |
| 278 | gen.characters("\u20ac") |
| 279 | gen.endElement("doc") |
| 280 | gen.endDocument() |
| 281 | |
| 282 | self.assertEqual(result.getvalue(), |
| 283 | self.xml('<doc a="\u20ac">\u20ac</doc>', encoding=encoding)) |
| 284 | |
| 285 | def test_xmlgen_unencodable(self): |
| 286 | result = self.ioclass() |
| 287 | gen = XMLGenerator(result, encoding='ascii') |
| 288 | |
| 289 | gen.startDocument() |
| 290 | gen.startElement("doc", {"a": '\u20ac'}) |
| 291 | gen.characters("\u20ac") |
| 292 | gen.endElement("doc") |
| 293 | gen.endDocument() |
| 294 | |
| 295 | self.assertEqual(result.getvalue(), |
| 296 | self.xml('<doc a="€">€</doc>', encoding='ascii')) |
Fred Drake | c9fadf9 | 2001-08-07 19:17:06 +0000 | [diff] [blame] | 297 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 298 | def test_xmlgen_ignorable(self): |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 299 | result = self.ioclass() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 300 | gen = XMLGenerator(result) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 301 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 302 | gen.startDocument() |
| 303 | gen.startElement("doc", {}) |
| 304 | gen.ignorableWhitespace(" ") |
| 305 | gen.endElement("doc") |
| 306 | gen.endDocument() |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 307 | |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 308 | self.assertEqual(result.getvalue(), self.xml("<doc> </doc>")) |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 309 | |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 310 | def test_xmlgen_ignorable_empty(self): |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 311 | result = self.ioclass() |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 312 | gen = XMLGenerator(result, short_empty_elements=True) |
| 313 | |
| 314 | gen.startDocument() |
| 315 | gen.startElement("doc", {}) |
| 316 | gen.ignorableWhitespace(" ") |
| 317 | gen.endElement("doc") |
| 318 | gen.endDocument() |
| 319 | |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 320 | self.assertEqual(result.getvalue(), self.xml("<doc> </doc>")) |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 321 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 322 | def test_xmlgen_ns(self): |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 323 | result = self.ioclass() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 324 | gen = XMLGenerator(result) |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 325 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 326 | gen.startDocument() |
| 327 | gen.startPrefixMapping("ns1", ns_uri) |
| 328 | gen.startElementNS((ns_uri, "doc"), "ns1:doc", {}) |
| 329 | # add an unqualified name |
| 330 | gen.startElementNS((None, "udoc"), None, {}) |
| 331 | gen.endElementNS((None, "udoc"), None) |
| 332 | gen.endElementNS((ns_uri, "doc"), "ns1:doc") |
| 333 | gen.endPrefixMapping("ns1") |
| 334 | gen.endDocument() |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 335 | |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 336 | self.assertEqual(result.getvalue(), self.xml( |
| 337 | '<ns1:doc xmlns:ns1="%s"><udoc></udoc></ns1:doc>' % |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 338 | ns_uri)) |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 339 | |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 340 | def test_xmlgen_ns_empty(self): |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 341 | result = self.ioclass() |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 342 | gen = XMLGenerator(result, short_empty_elements=True) |
| 343 | |
| 344 | gen.startDocument() |
| 345 | gen.startPrefixMapping("ns1", ns_uri) |
| 346 | gen.startElementNS((ns_uri, "doc"), "ns1:doc", {}) |
| 347 | # add an unqualified name |
| 348 | gen.startElementNS((None, "udoc"), None, {}) |
| 349 | gen.endElementNS((None, "udoc"), None) |
| 350 | gen.endElementNS((ns_uri, "doc"), "ns1:doc") |
| 351 | gen.endPrefixMapping("ns1") |
| 352 | gen.endDocument() |
| 353 | |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 354 | self.assertEqual(result.getvalue(), self.xml( |
| 355 | '<ns1:doc xmlns:ns1="%s"><udoc/></ns1:doc>' % |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 356 | ns_uri)) |
| 357 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 358 | def test_1463026_1(self): |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 359 | result = self.ioclass() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 360 | gen = XMLGenerator(result) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 361 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 362 | gen.startDocument() |
| 363 | gen.startElementNS((None, 'a'), 'a', {(None, 'b'):'c'}) |
| 364 | gen.endElementNS((None, 'a'), 'a') |
| 365 | gen.endDocument() |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 366 | |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 367 | self.assertEqual(result.getvalue(), self.xml('<a b="c"></a>')) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 368 | |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 369 | def test_1463026_1_empty(self): |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 370 | result = self.ioclass() |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 371 | gen = XMLGenerator(result, short_empty_elements=True) |
| 372 | |
| 373 | gen.startDocument() |
| 374 | gen.startElementNS((None, 'a'), 'a', {(None, 'b'):'c'}) |
| 375 | gen.endElementNS((None, 'a'), 'a') |
| 376 | gen.endDocument() |
| 377 | |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 378 | self.assertEqual(result.getvalue(), self.xml('<a b="c"/>')) |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 379 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 380 | def test_1463026_2(self): |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 381 | result = self.ioclass() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 382 | gen = XMLGenerator(result) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 383 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 384 | gen.startDocument() |
| 385 | gen.startPrefixMapping(None, 'qux') |
| 386 | gen.startElementNS(('qux', 'a'), 'a', {}) |
| 387 | gen.endElementNS(('qux', 'a'), 'a') |
| 388 | gen.endPrefixMapping(None) |
| 389 | gen.endDocument() |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 390 | |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 391 | self.assertEqual(result.getvalue(), self.xml('<a xmlns="qux"></a>')) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 392 | |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 393 | def test_1463026_2_empty(self): |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 394 | result = self.ioclass() |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 395 | gen = XMLGenerator(result, short_empty_elements=True) |
| 396 | |
| 397 | gen.startDocument() |
| 398 | gen.startPrefixMapping(None, 'qux') |
| 399 | gen.startElementNS(('qux', 'a'), 'a', {}) |
| 400 | gen.endElementNS(('qux', 'a'), 'a') |
| 401 | gen.endPrefixMapping(None) |
| 402 | gen.endDocument() |
| 403 | |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 404 | self.assertEqual(result.getvalue(), self.xml('<a xmlns="qux"/>')) |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 405 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 406 | def test_1463026_3(self): |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 407 | result = self.ioclass() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 408 | gen = XMLGenerator(result) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 409 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 410 | gen.startDocument() |
| 411 | gen.startPrefixMapping('my', 'qux') |
| 412 | gen.startElementNS(('qux', 'a'), 'a', {(None, 'b'):'c'}) |
| 413 | gen.endElementNS(('qux', 'a'), 'a') |
| 414 | gen.endPrefixMapping('my') |
| 415 | gen.endDocument() |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 416 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 417 | self.assertEqual(result.getvalue(), |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 418 | self.xml('<my:a xmlns:my="qux" b="c"></my:a>')) |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 419 | |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 420 | def test_1463026_3_empty(self): |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 421 | result = self.ioclass() |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 422 | gen = XMLGenerator(result, short_empty_elements=True) |
| 423 | |
| 424 | gen.startDocument() |
| 425 | gen.startPrefixMapping('my', 'qux') |
| 426 | gen.startElementNS(('qux', 'a'), 'a', {(None, 'b'):'c'}) |
| 427 | gen.endElementNS(('qux', 'a'), 'a') |
| 428 | gen.endPrefixMapping('my') |
| 429 | gen.endDocument() |
| 430 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 431 | self.assertEqual(result.getvalue(), |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 432 | self.xml('<my:a xmlns:my="qux" b="c"/>')) |
R. David Murray | a90032a | 2010-10-17 22:46:45 +0000 | [diff] [blame] | 433 | |
Antoine Pitrou | 6b03ee6 | 2010-10-27 18:33:30 +0000 | [diff] [blame] | 434 | def test_5027_1(self): |
| 435 | # The xml prefix (as in xml:lang below) is reserved and bound by |
| 436 | # definition to http://www.w3.org/XML/1998/namespace. XMLGenerator had |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 437 | # a bug whereby a KeyError is raised because this namespace is missing |
Antoine Pitrou | 6b03ee6 | 2010-10-27 18:33:30 +0000 | [diff] [blame] | 438 | # from a dictionary. |
| 439 | # |
| 440 | # This test demonstrates the bug by parsing a document. |
| 441 | test_xml = StringIO( |
| 442 | '<?xml version="1.0"?>' |
| 443 | '<a:g1 xmlns:a="http://example.com/ns">' |
| 444 | '<a:g2 xml:lang="en">Hello</a:g2>' |
| 445 | '</a:g1>') |
| 446 | |
| 447 | parser = make_parser() |
| 448 | parser.setFeature(feature_namespaces, True) |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 449 | result = self.ioclass() |
Antoine Pitrou | 6b03ee6 | 2010-10-27 18:33:30 +0000 | [diff] [blame] | 450 | gen = XMLGenerator(result) |
| 451 | parser.setContentHandler(gen) |
| 452 | parser.parse(test_xml) |
| 453 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 454 | self.assertEqual(result.getvalue(), |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 455 | self.xml( |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 456 | '<a:g1 xmlns:a="http://example.com/ns">' |
| 457 | '<a:g2 xml:lang="en">Hello</a:g2>' |
| 458 | '</a:g1>')) |
Antoine Pitrou | 6b03ee6 | 2010-10-27 18:33:30 +0000 | [diff] [blame] | 459 | |
| 460 | def test_5027_2(self): |
| 461 | # The xml prefix (as in xml:lang below) is reserved and bound by |
| 462 | # definition to http://www.w3.org/XML/1998/namespace. XMLGenerator had |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 463 | # a bug whereby a KeyError is raised because this namespace is missing |
Antoine Pitrou | 6b03ee6 | 2010-10-27 18:33:30 +0000 | [diff] [blame] | 464 | # from a dictionary. |
| 465 | # |
| 466 | # This test demonstrates the bug by direct manipulation of the |
| 467 | # XMLGenerator. |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 468 | result = self.ioclass() |
Antoine Pitrou | 6b03ee6 | 2010-10-27 18:33:30 +0000 | [diff] [blame] | 469 | gen = XMLGenerator(result) |
| 470 | |
| 471 | gen.startDocument() |
| 472 | gen.startPrefixMapping('a', 'http://example.com/ns') |
| 473 | gen.startElementNS(('http://example.com/ns', 'g1'), 'g1', {}) |
| 474 | lang_attr = {('http://www.w3.org/XML/1998/namespace', 'lang'): 'en'} |
| 475 | gen.startElementNS(('http://example.com/ns', 'g2'), 'g2', lang_attr) |
| 476 | gen.characters('Hello') |
| 477 | gen.endElementNS(('http://example.com/ns', 'g2'), 'g2') |
| 478 | gen.endElementNS(('http://example.com/ns', 'g1'), 'g1') |
| 479 | gen.endPrefixMapping('a') |
| 480 | gen.endDocument() |
| 481 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 482 | self.assertEqual(result.getvalue(), |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 483 | self.xml( |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 484 | '<a:g1 xmlns:a="http://example.com/ns">' |
| 485 | '<a:g2 xml:lang="en">Hello</a:g2>' |
| 486 | '</a:g1>')) |
Antoine Pitrou | 6b03ee6 | 2010-10-27 18:33:30 +0000 | [diff] [blame] | 487 | |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 488 | def test_no_close_file(self): |
| 489 | result = self.ioclass() |
| 490 | def func(out): |
| 491 | gen = XMLGenerator(out) |
| 492 | gen.startDocument() |
| 493 | gen.startElement("doc", {}) |
| 494 | func(result) |
| 495 | self.assertFalse(result.closed) |
| 496 | |
Serhiy Storchaka | a5f13d2 | 2013-02-25 13:46:10 +0200 | [diff] [blame] | 497 | def test_xmlgen_fragment(self): |
| 498 | result = self.ioclass() |
| 499 | gen = XMLGenerator(result) |
| 500 | |
| 501 | # Don't call gen.startDocument() |
| 502 | gen.startElement("foo", {"a": "1.0"}) |
| 503 | gen.characters("Hello") |
| 504 | gen.endElement("foo") |
| 505 | gen.startElement("bar", {"b": "2.0"}) |
| 506 | gen.endElement("bar") |
| 507 | # Don't call gen.endDocument() |
| 508 | |
| 509 | self.assertEqual(result.getvalue(), |
| 510 | self.xml('<foo a="1.0">Hello</foo><bar b="2.0"></bar>')[len(self.xml('')):]) |
| 511 | |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 512 | class StringXmlgenTest(XmlgenTest, unittest.TestCase): |
| 513 | ioclass = StringIO |
| 514 | |
| 515 | def xml(self, doc, encoding='iso-8859-1'): |
| 516 | return '<?xml version="1.0" encoding="%s"?>\n%s' % (encoding, doc) |
| 517 | |
| 518 | test_xmlgen_unencodable = None |
| 519 | |
| 520 | class BytesXmlgenTest(XmlgenTest, unittest.TestCase): |
| 521 | ioclass = BytesIO |
| 522 | |
| 523 | def xml(self, doc, encoding='iso-8859-1'): |
| 524 | return ('<?xml version="1.0" encoding="%s"?>\n%s' % |
| 525 | (encoding, doc)).encode(encoding, 'xmlcharrefreplace') |
| 526 | |
| 527 | class WriterXmlgenTest(BytesXmlgenTest): |
| 528 | class ioclass(list): |
| 529 | write = list.append |
| 530 | closed = False |
| 531 | |
| 532 | def seekable(self): |
| 533 | return True |
| 534 | |
| 535 | def tell(self): |
| 536 | # return 0 at start and not 0 after start |
| 537 | return len(self) |
| 538 | |
| 539 | def getvalue(self): |
| 540 | return b''.join(self) |
| 541 | |
Georg Brandl | c502df4 | 2013-05-12 11:41:12 +0200 | [diff] [blame^] | 542 | class StreamWriterXmlgenTest(XmlgenTest, unittest.TestCase): |
| 543 | def ioclass(self): |
| 544 | raw = BytesIO() |
| 545 | writer = codecs.getwriter('ascii')(raw, 'xmlcharrefreplace') |
| 546 | writer.getvalue = raw.getvalue |
| 547 | return writer |
| 548 | |
| 549 | def xml(self, doc, encoding='iso-8859-1'): |
| 550 | return ('<?xml version="1.0" encoding="%s"?>\n%s' % |
| 551 | (encoding, doc)).encode('ascii', 'xmlcharrefreplace') |
| 552 | |
| 553 | class StreamReaderWriterXmlgenTest(XmlgenTest, unittest.TestCase): |
| 554 | fname = support.TESTFN + '-codecs' |
| 555 | |
| 556 | def ioclass(self): |
| 557 | writer = codecs.open(self.fname, 'w', encoding='ascii', |
| 558 | errors='xmlcharrefreplace', buffering=0) |
| 559 | self.addCleanup(support.unlink, self.fname) |
| 560 | writer.getvalue = self.getvalue |
| 561 | return writer |
| 562 | |
| 563 | def getvalue(self): |
| 564 | with open(self.fname, 'rb') as f: |
| 565 | return f.read() |
| 566 | |
| 567 | def xml(self, doc, encoding='iso-8859-1'): |
| 568 | return ('<?xml version="1.0" encoding="%s"?>\n%s' % |
| 569 | (encoding, doc)).encode('ascii', 'xmlcharrefreplace') |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 570 | |
| 571 | start = b'<?xml version="1.0" encoding="iso-8859-1"?>\n' |
| 572 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 573 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 574 | class XMLFilterBaseTest(unittest.TestCase): |
| 575 | def test_filter_basic(self): |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 576 | result = BytesIO() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 577 | gen = XMLGenerator(result) |
| 578 | filter = XMLFilterBase() |
| 579 | filter.setContentHandler(gen) |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 580 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 581 | filter.startDocument() |
| 582 | filter.startElement("doc", {}) |
| 583 | filter.characters("content") |
| 584 | filter.ignorableWhitespace(" ") |
| 585 | filter.endElement("doc") |
| 586 | filter.endDocument() |
| 587 | |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 588 | self.assertEqual(result.getvalue(), start + b"<doc>content </doc>") |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 589 | |
| 590 | # =========================================================================== |
| 591 | # |
| 592 | # expatreader tests |
| 593 | # |
| 594 | # =========================================================================== |
| 595 | |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 596 | with open(TEST_XMLFILE_OUT, 'rb') as f: |
Benjamin Peterson | d5df36d | 2010-10-31 18:23:23 +0000 | [diff] [blame] | 597 | xml_test_out = f.read() |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 598 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 599 | class ExpatReaderTest(XmlTestBase): |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 600 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 601 | # ===== XMLReader support |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 602 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 603 | def test_expat_file(self): |
| 604 | parser = create_parser() |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 605 | result = BytesIO() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 606 | xmlgen = XMLGenerator(result) |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 607 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 608 | parser.setContentHandler(xmlgen) |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 609 | with open(TEST_XMLFILE, 'rb') as f: |
Benjamin Peterson | d5df36d | 2010-10-31 18:23:23 +0000 | [diff] [blame] | 610 | parser.parse(f) |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 611 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 612 | self.assertEqual(result.getvalue(), xml_test_out) |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 613 | |
Serhiy Storchaka | 1a4ed4c | 2013-02-02 12:17:05 +0200 | [diff] [blame] | 614 | @requires_nonascii_filenames |
Serhiy Storchaka | d520239 | 2013-02-02 10:31:17 +0200 | [diff] [blame] | 615 | def test_expat_file_nonascii(self): |
| 616 | fname = support.TESTFN_UNICODE |
| 617 | shutil.copyfile(TEST_XMLFILE, fname) |
| 618 | self.addCleanup(support.unlink, fname) |
| 619 | |
| 620 | parser = create_parser() |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 621 | result = BytesIO() |
Serhiy Storchaka | d520239 | 2013-02-02 10:31:17 +0200 | [diff] [blame] | 622 | xmlgen = XMLGenerator(result) |
| 623 | |
| 624 | parser.setContentHandler(xmlgen) |
| 625 | parser.parse(open(fname)) |
| 626 | |
| 627 | self.assertEqual(result.getvalue(), xml_test_out) |
| 628 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 629 | # ===== DTDHandler support |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 630 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 631 | class TestDTDHandler: |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 632 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 633 | def __init__(self): |
| 634 | self._notations = [] |
| 635 | self._entities = [] |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 636 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 637 | def notationDecl(self, name, publicId, systemId): |
| 638 | self._notations.append((name, publicId, systemId)) |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 639 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 640 | def unparsedEntityDecl(self, name, publicId, systemId, ndata): |
| 641 | self._entities.append((name, publicId, systemId, ndata)) |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 642 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 643 | def test_expat_dtdhandler(self): |
| 644 | parser = create_parser() |
| 645 | handler = self.TestDTDHandler() |
| 646 | parser.setDTDHandler(handler) |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 647 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 648 | parser.feed('<!DOCTYPE doc [\n') |
| 649 | parser.feed(' <!ENTITY img SYSTEM "expat.gif" NDATA GIF>\n') |
| 650 | parser.feed(' <!NOTATION GIF PUBLIC "-//CompuServe//NOTATION Graphics Interchange Format 89a//EN">\n') |
| 651 | parser.feed(']>\n') |
| 652 | parser.feed('<doc></doc>') |
| 653 | parser.close() |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 654 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 655 | self.assertEqual(handler._notations, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 656 | [("GIF", "-//CompuServe//NOTATION Graphics Interchange Format 89a//EN", None)]) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 657 | self.assertEqual(handler._entities, [("img", None, "expat.gif", "GIF")]) |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 658 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 659 | # ===== EntityResolver support |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 660 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 661 | class TestEntityResolver: |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 662 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 663 | def resolveEntity(self, publicId, systemId): |
| 664 | inpsrc = InputSource() |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 665 | inpsrc.setByteStream(BytesIO(b"<entity/>")) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 666 | return inpsrc |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 667 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 668 | def test_expat_entityresolver(self): |
| 669 | parser = create_parser() |
| 670 | parser.setEntityResolver(self.TestEntityResolver()) |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 671 | result = BytesIO() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 672 | parser.setContentHandler(XMLGenerator(result)) |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 673 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 674 | parser.feed('<!DOCTYPE doc [\n') |
| 675 | parser.feed(' <!ENTITY test SYSTEM "whatever">\n') |
| 676 | parser.feed(']>\n') |
| 677 | parser.feed('<doc>&test;</doc>') |
| 678 | parser.close() |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 679 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 680 | self.assertEqual(result.getvalue(), start + |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 681 | b"<doc><entity></entity></doc>") |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 682 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 683 | # ===== Attributes support |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 684 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 685 | class AttrGatherer(ContentHandler): |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 686 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 687 | def startElement(self, name, attrs): |
| 688 | self._attrs = attrs |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 689 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 690 | def startElementNS(self, name, qname, attrs): |
| 691 | self._attrs = attrs |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 692 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 693 | def test_expat_attrs_empty(self): |
| 694 | parser = create_parser() |
| 695 | gather = self.AttrGatherer() |
| 696 | parser.setContentHandler(gather) |
| 697 | |
| 698 | parser.feed("<doc/>") |
| 699 | parser.close() |
| 700 | |
| 701 | self.verify_empty_attrs(gather._attrs) |
| 702 | |
| 703 | def test_expat_attrs_wattr(self): |
| 704 | parser = create_parser() |
| 705 | gather = self.AttrGatherer() |
| 706 | parser.setContentHandler(gather) |
| 707 | |
| 708 | parser.feed("<doc attr='val'/>") |
| 709 | parser.close() |
| 710 | |
| 711 | self.verify_attrs_wattr(gather._attrs) |
| 712 | |
| 713 | def test_expat_nsattrs_empty(self): |
| 714 | parser = create_parser(1) |
| 715 | gather = self.AttrGatherer() |
| 716 | parser.setContentHandler(gather) |
| 717 | |
| 718 | parser.feed("<doc/>") |
| 719 | parser.close() |
| 720 | |
| 721 | self.verify_empty_nsattrs(gather._attrs) |
| 722 | |
| 723 | def test_expat_nsattrs_wattr(self): |
| 724 | parser = create_parser(1) |
| 725 | gather = self.AttrGatherer() |
| 726 | parser.setContentHandler(gather) |
| 727 | |
| 728 | parser.feed("<doc xmlns:ns='%s' ns:attr='val'/>" % ns_uri) |
| 729 | parser.close() |
| 730 | |
| 731 | attrs = gather._attrs |
| 732 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 733 | self.assertEqual(attrs.getLength(), 1) |
| 734 | self.assertEqual(attrs.getNames(), [(ns_uri, "attr")]) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 735 | self.assertTrue((attrs.getQNames() == [] or |
| 736 | attrs.getQNames() == ["ns:attr"])) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 737 | self.assertEqual(len(attrs), 1) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 738 | self.assertIn((ns_uri, "attr"), attrs) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 739 | self.assertEqual(attrs.get((ns_uri, "attr")), "val") |
| 740 | self.assertEqual(attrs.get((ns_uri, "attr"), 25), "val") |
| 741 | self.assertEqual(list(attrs.items()), [((ns_uri, "attr"), "val")]) |
| 742 | self.assertEqual(list(attrs.values()), ["val"]) |
| 743 | self.assertEqual(attrs.getValue((ns_uri, "attr")), "val") |
| 744 | self.assertEqual(attrs[(ns_uri, "attr")], "val") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 745 | |
| 746 | # ===== InputSource support |
| 747 | |
Benjamin Peterson | a7f4f5a | 2008-09-04 02:22:52 +0000 | [diff] [blame] | 748 | def test_expat_inpsource_filename(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 749 | parser = create_parser() |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 750 | result = BytesIO() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 751 | xmlgen = XMLGenerator(result) |
| 752 | |
| 753 | parser.setContentHandler(xmlgen) |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 754 | parser.parse(TEST_XMLFILE) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 755 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 756 | self.assertEqual(result.getvalue(), xml_test_out) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 757 | |
Benjamin Peterson | a7f4f5a | 2008-09-04 02:22:52 +0000 | [diff] [blame] | 758 | def test_expat_inpsource_sysid(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 759 | parser = create_parser() |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 760 | result = BytesIO() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 761 | xmlgen = XMLGenerator(result) |
| 762 | |
| 763 | parser.setContentHandler(xmlgen) |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 764 | parser.parse(InputSource(TEST_XMLFILE)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 765 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 766 | self.assertEqual(result.getvalue(), xml_test_out) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 767 | |
Serhiy Storchaka | 1a4ed4c | 2013-02-02 12:17:05 +0200 | [diff] [blame] | 768 | @requires_nonascii_filenames |
Serhiy Storchaka | d520239 | 2013-02-02 10:31:17 +0200 | [diff] [blame] | 769 | def test_expat_inpsource_sysid_nonascii(self): |
| 770 | fname = support.TESTFN_UNICODE |
| 771 | shutil.copyfile(TEST_XMLFILE, fname) |
| 772 | self.addCleanup(support.unlink, fname) |
| 773 | |
| 774 | parser = create_parser() |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 775 | result = BytesIO() |
Serhiy Storchaka | d520239 | 2013-02-02 10:31:17 +0200 | [diff] [blame] | 776 | xmlgen = XMLGenerator(result) |
| 777 | |
| 778 | parser.setContentHandler(xmlgen) |
| 779 | parser.parse(InputSource(fname)) |
| 780 | |
| 781 | self.assertEqual(result.getvalue(), xml_test_out) |
| 782 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 783 | def test_expat_inpsource_stream(self): |
| 784 | parser = create_parser() |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 785 | result = BytesIO() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 786 | xmlgen = XMLGenerator(result) |
| 787 | |
| 788 | parser.setContentHandler(xmlgen) |
| 789 | inpsrc = InputSource() |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 790 | with open(TEST_XMLFILE, 'rb') as f: |
Benjamin Peterson | d5df36d | 2010-10-31 18:23:23 +0000 | [diff] [blame] | 791 | inpsrc.setByteStream(f) |
| 792 | parser.parse(inpsrc) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 793 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 794 | self.assertEqual(result.getvalue(), xml_test_out) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 795 | |
| 796 | # ===== IncrementalParser support |
| 797 | |
| 798 | def test_expat_incremental(self): |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 799 | result = BytesIO() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 800 | xmlgen = XMLGenerator(result) |
| 801 | parser = create_parser() |
| 802 | parser.setContentHandler(xmlgen) |
| 803 | |
| 804 | parser.feed("<doc>") |
| 805 | parser.feed("</doc>") |
| 806 | parser.close() |
| 807 | |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 808 | self.assertEqual(result.getvalue(), start + b"<doc></doc>") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 809 | |
| 810 | def test_expat_incremental_reset(self): |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 811 | result = BytesIO() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 812 | xmlgen = XMLGenerator(result) |
| 813 | parser = create_parser() |
| 814 | parser.setContentHandler(xmlgen) |
| 815 | |
| 816 | parser.feed("<doc>") |
| 817 | parser.feed("text") |
| 818 | |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 819 | result = BytesIO() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 820 | xmlgen = XMLGenerator(result) |
| 821 | parser.setContentHandler(xmlgen) |
| 822 | parser.reset() |
| 823 | |
| 824 | parser.feed("<doc>") |
| 825 | parser.feed("text") |
| 826 | parser.feed("</doc>") |
| 827 | parser.close() |
| 828 | |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 829 | self.assertEqual(result.getvalue(), start + b"<doc>text</doc>") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 830 | |
| 831 | # ===== Locator support |
| 832 | |
| 833 | def test_expat_locator_noinfo(self): |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 834 | result = BytesIO() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 835 | xmlgen = XMLGenerator(result) |
| 836 | parser = create_parser() |
| 837 | parser.setContentHandler(xmlgen) |
| 838 | |
| 839 | parser.feed("<doc>") |
| 840 | parser.feed("</doc>") |
| 841 | parser.close() |
| 842 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 843 | self.assertEqual(parser.getSystemId(), None) |
| 844 | self.assertEqual(parser.getPublicId(), None) |
| 845 | self.assertEqual(parser.getLineNumber(), 1) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 846 | |
Benjamin Peterson | a7f4f5a | 2008-09-04 02:22:52 +0000 | [diff] [blame] | 847 | def test_expat_locator_withinfo(self): |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 848 | result = BytesIO() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 849 | xmlgen = XMLGenerator(result) |
| 850 | parser = create_parser() |
| 851 | parser.setContentHandler(xmlgen) |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 852 | parser.parse(TEST_XMLFILE) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 853 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 854 | self.assertEqual(parser.getSystemId(), TEST_XMLFILE) |
| 855 | self.assertEqual(parser.getPublicId(), None) |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 856 | |
Serhiy Storchaka | 1a4ed4c | 2013-02-02 12:17:05 +0200 | [diff] [blame] | 857 | @requires_nonascii_filenames |
Serhiy Storchaka | d520239 | 2013-02-02 10:31:17 +0200 | [diff] [blame] | 858 | def test_expat_locator_withinfo_nonascii(self): |
| 859 | fname = support.TESTFN_UNICODE |
| 860 | shutil.copyfile(TEST_XMLFILE, fname) |
| 861 | self.addCleanup(support.unlink, fname) |
| 862 | |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 863 | result = BytesIO() |
Serhiy Storchaka | d520239 | 2013-02-02 10:31:17 +0200 | [diff] [blame] | 864 | xmlgen = XMLGenerator(result) |
| 865 | parser = create_parser() |
| 866 | parser.setContentHandler(xmlgen) |
| 867 | parser.parse(fname) |
| 868 | |
| 869 | self.assertEqual(parser.getSystemId(), fname) |
| 870 | self.assertEqual(parser.getPublicId(), None) |
| 871 | |
Martin v. Löwis | 80670bc | 2000-10-06 21:13:23 +0000 | [diff] [blame] | 872 | |
| 873 | # =========================================================================== |
| 874 | # |
| 875 | # error reporting |
| 876 | # |
| 877 | # =========================================================================== |
| 878 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 879 | class ErrorReportingTest(unittest.TestCase): |
| 880 | def test_expat_inpsource_location(self): |
| 881 | parser = create_parser() |
| 882 | parser.setContentHandler(ContentHandler()) # do nothing |
| 883 | source = InputSource() |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 884 | source.setByteStream(BytesIO(b"<foo bar foobar>")) #ill-formed |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 885 | name = "a file name" |
| 886 | source.setSystemId(name) |
| 887 | try: |
| 888 | parser.parse(source) |
| 889 | self.fail() |
| 890 | except SAXException as e: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 891 | self.assertEqual(e.getSystemId(), name) |
Martin v. Löwis | 80670bc | 2000-10-06 21:13:23 +0000 | [diff] [blame] | 892 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 893 | def test_expat_incomplete(self): |
| 894 | parser = create_parser() |
| 895 | parser.setContentHandler(ContentHandler()) # do nothing |
| 896 | self.assertRaises(SAXParseException, parser.parse, StringIO("<foo>")) |
Martin v. Löwis | 80670bc | 2000-10-06 21:13:23 +0000 | [diff] [blame] | 897 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 898 | def test_sax_parse_exception_str(self): |
| 899 | # pass various values from a locator to the SAXParseException to |
| 900 | # make sure that the __str__() doesn't fall apart when None is |
| 901 | # passed instead of an integer line and column number |
| 902 | # |
| 903 | # use "normal" values for the locator: |
| 904 | str(SAXParseException("message", None, |
| 905 | self.DummyLocator(1, 1))) |
| 906 | # use None for the line number: |
| 907 | str(SAXParseException("message", None, |
| 908 | self.DummyLocator(None, 1))) |
| 909 | # use None for the column number: |
| 910 | str(SAXParseException("message", None, |
| 911 | self.DummyLocator(1, None))) |
| 912 | # use None for both: |
| 913 | str(SAXParseException("message", None, |
| 914 | self.DummyLocator(None, None))) |
Fred Drake | 6fd0b0d | 2004-03-20 08:15:30 +0000 | [diff] [blame] | 915 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 916 | class DummyLocator: |
| 917 | def __init__(self, lineno, colno): |
| 918 | self._lineno = lineno |
| 919 | self._colno = colno |
Fred Drake | 6fd0b0d | 2004-03-20 08:15:30 +0000 | [diff] [blame] | 920 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 921 | def getPublicId(self): |
| 922 | return "pubid" |
Fred Drake | 6fd0b0d | 2004-03-20 08:15:30 +0000 | [diff] [blame] | 923 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 924 | def getSystemId(self): |
| 925 | return "sysid" |
Fred Drake | 6fd0b0d | 2004-03-20 08:15:30 +0000 | [diff] [blame] | 926 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 927 | def getLineNumber(self): |
| 928 | return self._lineno |
Fred Drake | 6fd0b0d | 2004-03-20 08:15:30 +0000 | [diff] [blame] | 929 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 930 | def getColumnNumber(self): |
| 931 | return self._colno |
Martin v. Löwis | 80670bc | 2000-10-06 21:13:23 +0000 | [diff] [blame] | 932 | |
Lars Gustäbel | ab64787 | 2000-09-24 18:40:52 +0000 | [diff] [blame] | 933 | # =========================================================================== |
| 934 | # |
| 935 | # xmlreader tests |
| 936 | # |
| 937 | # =========================================================================== |
| 938 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 939 | class XmlReaderTest(XmlTestBase): |
Lars Gustäbel | ab64787 | 2000-09-24 18:40:52 +0000 | [diff] [blame] | 940 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 941 | # ===== AttributesImpl |
| 942 | def test_attrs_empty(self): |
| 943 | self.verify_empty_attrs(AttributesImpl({})) |
Lars Gustäbel | ab64787 | 2000-09-24 18:40:52 +0000 | [diff] [blame] | 944 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 945 | def test_attrs_wattr(self): |
| 946 | self.verify_attrs_wattr(AttributesImpl({"attr" : "val"})) |
Lars Gustäbel | ab64787 | 2000-09-24 18:40:52 +0000 | [diff] [blame] | 947 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 948 | def test_nsattrs_empty(self): |
| 949 | self.verify_empty_nsattrs(AttributesNSImpl({}, {})) |
Lars Gustäbel | ab64787 | 2000-09-24 18:40:52 +0000 | [diff] [blame] | 950 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 951 | def test_nsattrs_wattr(self): |
| 952 | attrs = AttributesNSImpl({(ns_uri, "attr") : "val"}, |
| 953 | {(ns_uri, "attr") : "ns:attr"}) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 954 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 955 | self.assertEqual(attrs.getLength(), 1) |
| 956 | self.assertEqual(attrs.getNames(), [(ns_uri, "attr")]) |
| 957 | self.assertEqual(attrs.getQNames(), ["ns:attr"]) |
| 958 | self.assertEqual(len(attrs), 1) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 959 | self.assertIn((ns_uri, "attr"), attrs) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 960 | self.assertEqual(list(attrs.keys()), [(ns_uri, "attr")]) |
| 961 | self.assertEqual(attrs.get((ns_uri, "attr")), "val") |
| 962 | self.assertEqual(attrs.get((ns_uri, "attr"), 25), "val") |
| 963 | self.assertEqual(list(attrs.items()), [((ns_uri, "attr"), "val")]) |
| 964 | self.assertEqual(list(attrs.values()), ["val"]) |
| 965 | self.assertEqual(attrs.getValue((ns_uri, "attr")), "val") |
| 966 | self.assertEqual(attrs.getValueByQName("ns:attr"), "val") |
| 967 | self.assertEqual(attrs.getNameByQName("ns:attr"), (ns_uri, "attr")) |
| 968 | self.assertEqual(attrs[(ns_uri, "attr")], "val") |
| 969 | self.assertEqual(attrs.getQNameByName((ns_uri, "attr")), "ns:attr") |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 970 | |
Lars Gustäbel | ab64787 | 2000-09-24 18:40:52 +0000 | [diff] [blame] | 971 | |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 972 | def test_main(): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 973 | run_unittest(MakeParserTest, |
| 974 | SaxutilsTest, |
Serhiy Storchaka | 88efc52 | 2013-02-10 14:29:52 +0200 | [diff] [blame] | 975 | StringXmlgenTest, |
| 976 | BytesXmlgenTest, |
| 977 | WriterXmlgenTest, |
Georg Brandl | c502df4 | 2013-05-12 11:41:12 +0200 | [diff] [blame^] | 978 | StreamWriterXmlgenTest, |
| 979 | StreamReaderWriterXmlgenTest, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 980 | ExpatReaderTest, |
| 981 | ErrorReportingTest, |
| 982 | XmlReaderTest) |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 983 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 984 | if __name__ == "__main__": |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 985 | test_main() |