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