Martin v. Löwis | a729daf | 2002-08-04 17:28:33 +0000 | [diff] [blame] | 1 | # regression test for SAX 2.0 -*- coding: iso-8859-1 -*- |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 2 | # $Id$ |
| 3 | |
Martin v. Löwis | 80670bc | 2000-10-06 21:13:23 +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") |
Martin v. Löwis | 74b51ac | 2002-10-26 14:50:45 +0000 | [diff] [blame] | 11 | from xml.sax.saxutils import XMLGenerator, escape, unescape, quoteattr, \ |
| 12 | XMLFilterBase |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 13 | from xml.sax.expatreader import create_parser |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 14 | from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 15 | from cStringIO import StringIO |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 16 | from test.test_support import verify, verbose, TestFailed, findfile |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 17 | import os |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 18 | |
| 19 | # ===== Utilities |
| 20 | |
| 21 | tests = 0 |
Fred Drake | 32f3add | 2002-10-28 17:58:48 +0000 | [diff] [blame] | 22 | failures = [] |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 23 | |
| 24 | def confirm(outcome, name): |
Fred Drake | 32f3add | 2002-10-28 17:58:48 +0000 | [diff] [blame] | 25 | global tests |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 26 | |
| 27 | tests = tests + 1 |
| 28 | if outcome: |
Fred Drake | 32f3add | 2002-10-28 17:58:48 +0000 | [diff] [blame] | 29 | if verbose: |
Andrew M. Kuchling | 09e2cb0 | 2004-06-01 12:48:19 +0000 | [diff] [blame] | 30 | print "Passed", name |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 31 | else: |
Fred Drake | 32f3add | 2002-10-28 17:58:48 +0000 | [diff] [blame] | 32 | failures.append(name) |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 33 | |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 34 | def test_make_parser2(): |
Tim Peters | d2bf3b7 | 2001-01-18 02:22:22 +0000 | [diff] [blame] | 35 | try: |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 36 | # Creating parsers several times in a row should succeed. |
| 37 | # Testing this because there have been failures of this kind |
| 38 | # before. |
| 39 | from xml.sax import make_parser |
| 40 | p = make_parser() |
| 41 | from xml.sax import make_parser |
| 42 | p = make_parser() |
| 43 | from xml.sax import make_parser |
| 44 | p = make_parser() |
| 45 | from xml.sax import make_parser |
| 46 | p = make_parser() |
| 47 | from xml.sax import make_parser |
| 48 | p = make_parser() |
| 49 | from xml.sax import make_parser |
| 50 | p = make_parser() |
| 51 | except: |
| 52 | return 0 |
| 53 | else: |
| 54 | return p |
Tim Peters | d2bf3b7 | 2001-01-18 02:22:22 +0000 | [diff] [blame] | 55 | |
| 56 | |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 57 | # =========================================================================== |
| 58 | # |
| 59 | # saxutils tests |
| 60 | # |
| 61 | # =========================================================================== |
| 62 | |
| 63 | # ===== escape |
| 64 | |
| 65 | def test_escape_basic(): |
| 66 | return escape("Donald Duck & Co") == "Donald Duck & Co" |
| 67 | |
| 68 | def test_escape_all(): |
| 69 | return escape("<Donald Duck & Co>") == "<Donald Duck & Co>" |
| 70 | |
| 71 | def test_escape_extra(): |
| 72 | return escape("Hei på deg", {"å" : "å"}) == "Hei på deg" |
| 73 | |
Martin v. Löwis | 74b51ac | 2002-10-26 14:50:45 +0000 | [diff] [blame] | 74 | # ===== unescape |
| 75 | |
| 76 | def test_unescape_basic(): |
| 77 | return unescape("Donald Duck & Co") == "Donald Duck & Co" |
| 78 | |
| 79 | def test_unescape_all(): |
| 80 | return unescape("<Donald Duck & Co>") == "<Donald Duck & Co>" |
| 81 | |
| 82 | def test_unescape_extra(): |
| 83 | return unescape("Hei på deg", {"å" : "å"}) == "Hei på deg" |
| 84 | |
Fred Drake | 32f3add | 2002-10-28 17:58:48 +0000 | [diff] [blame] | 85 | def test_unescape_amp_extra(): |
| 86 | return unescape("&foo;", {"&foo;": "splat"}) == "&foo;" |
| 87 | |
Fred Drake | acd32d3 | 2001-07-19 16:10:15 +0000 | [diff] [blame] | 88 | # ===== quoteattr |
| 89 | |
| 90 | def test_quoteattr_basic(): |
| 91 | return quoteattr("Donald Duck & Co") == '"Donald Duck & Co"' |
| 92 | |
| 93 | def test_single_quoteattr(): |
| 94 | return (quoteattr('Includes "double" quotes') |
| 95 | == '\'Includes "double" quotes\'') |
| 96 | |
| 97 | def test_double_quoteattr(): |
| 98 | return (quoteattr("Includes 'single' quotes") |
| 99 | == "\"Includes 'single' quotes\"") |
| 100 | |
| 101 | def test_single_double_quoteattr(): |
| 102 | return (quoteattr("Includes 'single' and \"double\" quotes") |
| 103 | == "\"Includes 'single' and "double" quotes\"") |
| 104 | |
| 105 | # ===== make_parser |
| 106 | |
Martin v. Löwis | 962c9e7 | 2000-10-06 17:41:52 +0000 | [diff] [blame] | 107 | def test_make_parser(): |
| 108 | try: |
| 109 | # Creating a parser should succeed - it should fall back |
| 110 | # to the expatreader |
| 111 | p = make_parser(['xml.parsers.no_such_parser']) |
| 112 | except: |
| 113 | return 0 |
| 114 | else: |
| 115 | return p |
| 116 | |
| 117 | |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 118 | # ===== XMLGenerator |
| 119 | |
| 120 | start = '<?xml version="1.0" encoding="iso-8859-1"?>\n' |
| 121 | |
| 122 | def test_xmlgen_basic(): |
| 123 | result = StringIO() |
| 124 | gen = XMLGenerator(result) |
| 125 | gen.startDocument() |
| 126 | gen.startElement("doc", {}) |
| 127 | gen.endElement("doc") |
| 128 | gen.endDocument() |
| 129 | |
| 130 | return result.getvalue() == start + "<doc></doc>" |
| 131 | |
| 132 | def test_xmlgen_content(): |
| 133 | result = StringIO() |
| 134 | gen = XMLGenerator(result) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 135 | |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 136 | gen.startDocument() |
| 137 | gen.startElement("doc", {}) |
| 138 | gen.characters("huhei") |
| 139 | gen.endElement("doc") |
| 140 | gen.endDocument() |
| 141 | |
| 142 | return result.getvalue() == start + "<doc>huhei</doc>" |
| 143 | |
| 144 | def test_xmlgen_pi(): |
| 145 | result = StringIO() |
| 146 | gen = XMLGenerator(result) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 147 | |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 148 | gen.startDocument() |
| 149 | gen.processingInstruction("test", "data") |
| 150 | gen.startElement("doc", {}) |
| 151 | gen.endElement("doc") |
| 152 | gen.endDocument() |
| 153 | |
| 154 | return result.getvalue() == start + "<?test data?><doc></doc>" |
| 155 | |
| 156 | def test_xmlgen_content_escape(): |
| 157 | result = StringIO() |
| 158 | gen = XMLGenerator(result) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 159 | |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 160 | gen.startDocument() |
| 161 | gen.startElement("doc", {}) |
| 162 | gen.characters("<huhei&") |
| 163 | gen.endElement("doc") |
| 164 | gen.endDocument() |
| 165 | |
| 166 | return result.getvalue() == start + "<doc><huhei&</doc>" |
| 167 | |
Fred Drake | c9fadf9 | 2001-08-07 19:17:06 +0000 | [diff] [blame] | 168 | def test_xmlgen_attr_escape(): |
| 169 | result = StringIO() |
| 170 | gen = XMLGenerator(result) |
| 171 | |
| 172 | gen.startDocument() |
| 173 | gen.startElement("doc", {"a": '"'}) |
| 174 | gen.startElement("e", {"a": "'"}) |
| 175 | gen.endElement("e") |
| 176 | gen.startElement("e", {"a": "'\""}) |
| 177 | gen.endElement("e") |
| 178 | gen.endElement("doc") |
| 179 | gen.endDocument() |
| 180 | |
| 181 | return result.getvalue() == start \ |
| 182 | + "<doc a='\"'><e a=\"'\"></e><e a=\"'"\"></e></doc>" |
| 183 | |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 184 | def test_xmlgen_ignorable(): |
| 185 | result = StringIO() |
| 186 | gen = XMLGenerator(result) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 187 | |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 188 | gen.startDocument() |
| 189 | gen.startElement("doc", {}) |
| 190 | gen.ignorableWhitespace(" ") |
| 191 | gen.endElement("doc") |
| 192 | gen.endDocument() |
| 193 | |
| 194 | return result.getvalue() == start + "<doc> </doc>" |
| 195 | |
| 196 | ns_uri = "http://www.python.org/xml-ns/saxtest/" |
| 197 | |
| 198 | def test_xmlgen_ns(): |
| 199 | result = StringIO() |
| 200 | gen = XMLGenerator(result) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 201 | |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 202 | gen.startDocument() |
| 203 | gen.startPrefixMapping("ns1", ns_uri) |
Lars Gustäbel | 6a7768a | 2000-09-27 08:12:17 +0000 | [diff] [blame] | 204 | gen.startElementNS((ns_uri, "doc"), "ns1:doc", {}) |
Martin v. Löwis | cf0a1cc | 2000-10-03 22:35:29 +0000 | [diff] [blame] | 205 | # add an unqualified name |
| 206 | gen.startElementNS((None, "udoc"), None, {}) |
| 207 | gen.endElementNS((None, "udoc"), None) |
Lars Gustäbel | 6a7768a | 2000-09-27 08:12:17 +0000 | [diff] [blame] | 208 | gen.endElementNS((ns_uri, "doc"), "ns1:doc") |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 209 | gen.endPrefixMapping("ns1") |
| 210 | gen.endDocument() |
| 211 | |
Martin v. Löwis | cf0a1cc | 2000-10-03 22:35:29 +0000 | [diff] [blame] | 212 | return result.getvalue() == start + \ |
| 213 | ('<ns1:doc xmlns:ns1="%s"><udoc></udoc></ns1:doc>' % |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 214 | ns_uri) |
| 215 | |
| 216 | # ===== XMLFilterBase |
| 217 | |
| 218 | def test_filter_basic(): |
| 219 | result = StringIO() |
| 220 | gen = XMLGenerator(result) |
| 221 | filter = XMLFilterBase() |
| 222 | filter.setContentHandler(gen) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 223 | |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 224 | filter.startDocument() |
| 225 | filter.startElement("doc", {}) |
| 226 | filter.characters("content") |
| 227 | filter.ignorableWhitespace(" ") |
| 228 | filter.endElement("doc") |
| 229 | filter.endDocument() |
| 230 | |
| 231 | return result.getvalue() == start + "<doc>content </doc>" |
| 232 | |
| 233 | # =========================================================================== |
| 234 | # |
| 235 | # expatreader tests |
| 236 | # |
| 237 | # =========================================================================== |
| 238 | |
Lars Gustäbel | 0702507 | 2000-10-24 16:00:22 +0000 | [diff] [blame] | 239 | # ===== XMLReader support |
| 240 | |
| 241 | def test_expat_file(): |
| 242 | parser = create_parser() |
| 243 | result = StringIO() |
| 244 | xmlgen = XMLGenerator(result) |
| 245 | |
| 246 | parser.setContentHandler(xmlgen) |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 247 | parser.parse(open(findfile("test"+os.extsep+"xml"))) |
Lars Gustäbel | 0702507 | 2000-10-24 16:00:22 +0000 | [diff] [blame] | 248 | |
| 249 | return result.getvalue() == xml_test_out |
| 250 | |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 251 | # ===== DTDHandler support |
| 252 | |
| 253 | class TestDTDHandler: |
| 254 | |
| 255 | def __init__(self): |
| 256 | self._notations = [] |
| 257 | self._entities = [] |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 258 | |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 259 | def notationDecl(self, name, publicId, systemId): |
| 260 | self._notations.append((name, publicId, systemId)) |
| 261 | |
| 262 | def unparsedEntityDecl(self, name, publicId, systemId, ndata): |
| 263 | self._entities.append((name, publicId, systemId, ndata)) |
| 264 | |
Lars Gustäbel | e292a24 | 2000-09-24 20:19:45 +0000 | [diff] [blame] | 265 | def test_expat_dtdhandler(): |
| 266 | parser = create_parser() |
| 267 | handler = TestDTDHandler() |
| 268 | parser.setDTDHandler(handler) |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 269 | |
Lars Gustäbel | e292a24 | 2000-09-24 20:19:45 +0000 | [diff] [blame] | 270 | parser.feed('<!DOCTYPE doc [\n') |
| 271 | parser.feed(' <!ENTITY img SYSTEM "expat.gif" NDATA GIF>\n') |
| 272 | parser.feed(' <!NOTATION GIF PUBLIC "-//CompuServe//NOTATION Graphics Interchange Format 89a//EN">\n') |
| 273 | parser.feed(']>\n') |
| 274 | parser.feed('<doc></doc>') |
| 275 | parser.close() |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 276 | |
Lars Gustäbel | e292a24 | 2000-09-24 20:19:45 +0000 | [diff] [blame] | 277 | return handler._notations == [("GIF", "-//CompuServe//NOTATION Graphics Interchange Format 89a//EN", None)] and \ |
| 278 | handler._entities == [("img", None, "expat.gif", "GIF")] |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 279 | |
| 280 | # ===== EntityResolver support |
| 281 | |
Lars Gustäbel | e292a24 | 2000-09-24 20:19:45 +0000 | [diff] [blame] | 282 | class TestEntityResolver: |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 283 | |
Lars Gustäbel | e292a24 | 2000-09-24 20:19:45 +0000 | [diff] [blame] | 284 | def resolveEntity(self, publicId, systemId): |
| 285 | inpsrc = InputSource() |
| 286 | inpsrc.setByteStream(StringIO("<entity/>")) |
| 287 | return inpsrc |
| 288 | |
| 289 | def test_expat_entityresolver(): |
Lars Gustäbel | e292a24 | 2000-09-24 20:19:45 +0000 | [diff] [blame] | 290 | parser = create_parser() |
| 291 | parser.setEntityResolver(TestEntityResolver()) |
| 292 | result = StringIO() |
| 293 | parser.setContentHandler(XMLGenerator(result)) |
| 294 | |
| 295 | parser.feed('<!DOCTYPE doc [\n') |
| 296 | parser.feed(' <!ENTITY test SYSTEM "whatever">\n') |
| 297 | parser.feed(']>\n') |
| 298 | parser.feed('<doc>&test;</doc>') |
| 299 | parser.close() |
| 300 | |
| 301 | return result.getvalue() == start + "<doc><entity></entity></doc>" |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 302 | |
Lars Gustäbel | ab64787 | 2000-09-24 18:40:52 +0000 | [diff] [blame] | 303 | # ===== Attributes support |
| 304 | |
| 305 | class AttrGatherer(ContentHandler): |
| 306 | |
| 307 | def startElement(self, name, attrs): |
| 308 | self._attrs = attrs |
| 309 | |
| 310 | def startElementNS(self, name, qname, attrs): |
| 311 | self._attrs = attrs |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 312 | |
Lars Gustäbel | ab64787 | 2000-09-24 18:40:52 +0000 | [diff] [blame] | 313 | def test_expat_attrs_empty(): |
| 314 | parser = create_parser() |
| 315 | gather = AttrGatherer() |
| 316 | parser.setContentHandler(gather) |
| 317 | |
| 318 | parser.feed("<doc/>") |
| 319 | parser.close() |
| 320 | |
| 321 | return verify_empty_attrs(gather._attrs) |
| 322 | |
| 323 | def test_expat_attrs_wattr(): |
| 324 | parser = create_parser() |
| 325 | gather = AttrGatherer() |
| 326 | parser.setContentHandler(gather) |
| 327 | |
| 328 | parser.feed("<doc attr='val'/>") |
| 329 | parser.close() |
| 330 | |
| 331 | return verify_attrs_wattr(gather._attrs) |
| 332 | |
| 333 | def test_expat_nsattrs_empty(): |
| 334 | parser = create_parser(1) |
| 335 | gather = AttrGatherer() |
| 336 | parser.setContentHandler(gather) |
| 337 | |
| 338 | parser.feed("<doc/>") |
| 339 | parser.close() |
| 340 | |
| 341 | return verify_empty_nsattrs(gather._attrs) |
| 342 | |
| 343 | def test_expat_nsattrs_wattr(): |
| 344 | parser = create_parser(1) |
| 345 | gather = AttrGatherer() |
| 346 | parser.setContentHandler(gather) |
| 347 | |
| 348 | parser.feed("<doc xmlns:ns='%s' ns:attr='val'/>" % ns_uri) |
| 349 | parser.close() |
| 350 | |
| 351 | attrs = gather._attrs |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 352 | |
Lars Gustäbel | ab64787 | 2000-09-24 18:40:52 +0000 | [diff] [blame] | 353 | return attrs.getLength() == 1 and \ |
| 354 | attrs.getNames() == [(ns_uri, "attr")] and \ |
Fred Drake | d2909c9 | 2002-09-12 17:02:01 +0000 | [diff] [blame] | 355 | (attrs.getQNames() == [] or attrs.getQNames() == ["ns:attr"]) and \ |
Lars Gustäbel | ab64787 | 2000-09-24 18:40:52 +0000 | [diff] [blame] | 356 | len(attrs) == 1 and \ |
| 357 | attrs.has_key((ns_uri, "attr")) and \ |
| 358 | attrs.keys() == [(ns_uri, "attr")] and \ |
| 359 | attrs.get((ns_uri, "attr")) == "val" and \ |
| 360 | attrs.get((ns_uri, "attr"), 25) == "val" and \ |
| 361 | attrs.items() == [((ns_uri, "attr"), "val")] and \ |
| 362 | attrs.values() == ["val"] and \ |
| 363 | attrs.getValue((ns_uri, "attr")) == "val" and \ |
| 364 | attrs[(ns_uri, "attr")] == "val" |
| 365 | |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 366 | # ===== InputSource support |
| 367 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 368 | xml_test_out = open(findfile("test"+os.extsep+"xml"+os.extsep+"out")).read() |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 369 | |
| 370 | def test_expat_inpsource_filename(): |
| 371 | parser = create_parser() |
| 372 | result = StringIO() |
| 373 | xmlgen = XMLGenerator(result) |
| 374 | |
| 375 | parser.setContentHandler(xmlgen) |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 376 | parser.parse(findfile("test"+os.extsep+"xml")) |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 377 | |
| 378 | return result.getvalue() == xml_test_out |
| 379 | |
| 380 | def test_expat_inpsource_sysid(): |
| 381 | parser = create_parser() |
| 382 | result = StringIO() |
| 383 | xmlgen = XMLGenerator(result) |
| 384 | |
| 385 | parser.setContentHandler(xmlgen) |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 386 | parser.parse(InputSource(findfile("test"+os.extsep+"xml"))) |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 387 | |
| 388 | return result.getvalue() == xml_test_out |
| 389 | |
| 390 | def test_expat_inpsource_stream(): |
| 391 | parser = create_parser() |
| 392 | result = StringIO() |
| 393 | xmlgen = XMLGenerator(result) |
| 394 | |
| 395 | parser.setContentHandler(xmlgen) |
| 396 | inpsrc = InputSource() |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 397 | inpsrc.setByteStream(open(findfile("test"+os.extsep+"xml"))) |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 398 | parser.parse(inpsrc) |
| 399 | |
| 400 | return result.getvalue() == xml_test_out |
| 401 | |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 402 | # ===== IncrementalParser support |
| 403 | |
| 404 | def test_expat_incremental(): |
| 405 | result = StringIO() |
| 406 | xmlgen = XMLGenerator(result) |
| 407 | parser = create_parser() |
| 408 | parser.setContentHandler(xmlgen) |
| 409 | |
| 410 | parser.feed("<doc>") |
| 411 | parser.feed("</doc>") |
| 412 | parser.close() |
| 413 | |
| 414 | return result.getvalue() == start + "<doc></doc>" |
| 415 | |
| 416 | def test_expat_incremental_reset(): |
| 417 | result = StringIO() |
| 418 | xmlgen = XMLGenerator(result) |
| 419 | parser = create_parser() |
| 420 | parser.setContentHandler(xmlgen) |
| 421 | |
| 422 | parser.feed("<doc>") |
| 423 | parser.feed("text") |
| 424 | |
| 425 | result = StringIO() |
| 426 | xmlgen = XMLGenerator(result) |
| 427 | parser.setContentHandler(xmlgen) |
| 428 | parser.reset() |
| 429 | |
| 430 | parser.feed("<doc>") |
| 431 | parser.feed("text") |
| 432 | parser.feed("</doc>") |
| 433 | parser.close() |
| 434 | |
| 435 | return result.getvalue() == start + "<doc>text</doc>" |
| 436 | |
| 437 | # ===== Locator support |
| 438 | |
| 439 | def test_expat_locator_noinfo(): |
| 440 | result = StringIO() |
| 441 | xmlgen = XMLGenerator(result) |
| 442 | parser = create_parser() |
| 443 | parser.setContentHandler(xmlgen) |
| 444 | |
| 445 | parser.feed("<doc>") |
| 446 | parser.feed("</doc>") |
| 447 | parser.close() |
| 448 | |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 449 | return parser.getSystemId() is None and \ |
| 450 | parser.getPublicId() is None and \ |
Tim Peters | d2bf3b7 | 2001-01-18 02:22:22 +0000 | [diff] [blame] | 451 | parser.getLineNumber() == 1 |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 452 | |
| 453 | def test_expat_locator_withinfo(): |
| 454 | result = StringIO() |
| 455 | xmlgen = XMLGenerator(result) |
| 456 | parser = create_parser() |
| 457 | parser.setContentHandler(xmlgen) |
| 458 | parser.parse(findfile("test.xml")) |
| 459 | |
| 460 | return parser.getSystemId() == findfile("test.xml") and \ |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 461 | parser.getPublicId() is None |
Lars Gustäbel | 2fc5294 | 2000-10-24 15:35:07 +0000 | [diff] [blame] | 462 | |
Martin v. Löwis | 80670bc | 2000-10-06 21:13:23 +0000 | [diff] [blame] | 463 | |
| 464 | # =========================================================================== |
| 465 | # |
| 466 | # error reporting |
| 467 | # |
| 468 | # =========================================================================== |
| 469 | |
| 470 | def test_expat_inpsource_location(): |
| 471 | parser = create_parser() |
| 472 | parser.setContentHandler(ContentHandler()) # do nothing |
| 473 | source = InputSource() |
| 474 | source.setByteStream(StringIO("<foo bar foobar>")) #ill-formed |
| 475 | name = "a file name" |
| 476 | source.setSystemId(name) |
| 477 | try: |
| 478 | parser.parse(source) |
| 479 | except SAXException, e: |
| 480 | return e.getSystemId() == name |
| 481 | |
| 482 | def test_expat_incomplete(): |
| 483 | parser = create_parser() |
| 484 | parser.setContentHandler(ContentHandler()) # do nothing |
| 485 | try: |
| 486 | parser.parse(StringIO("<foo>")) |
| 487 | except SAXParseException: |
| 488 | return 1 # ok, error found |
| 489 | else: |
| 490 | return 0 |
| 491 | |
Fred Drake | 6fd0b0d | 2004-03-20 08:15:30 +0000 | [diff] [blame] | 492 | def test_sax_parse_exception_str(): |
| 493 | # pass various values from a locator to the SAXParseException to |
| 494 | # make sure that the __str__() doesn't fall apart when None is |
| 495 | # passed instead of an integer line and column number |
| 496 | # |
| 497 | # use "normal" values for the locator: |
| 498 | str(SAXParseException("message", None, |
| 499 | DummyLocator(1, 1))) |
| 500 | # use None for the line number: |
| 501 | str(SAXParseException("message", None, |
| 502 | DummyLocator(None, 1))) |
| 503 | # use None for the column number: |
| 504 | str(SAXParseException("message", None, |
| 505 | DummyLocator(1, None))) |
| 506 | # use None for both: |
| 507 | str(SAXParseException("message", None, |
| 508 | DummyLocator(None, None))) |
| 509 | return 1 |
| 510 | |
| 511 | class DummyLocator: |
| 512 | def __init__(self, lineno, colno): |
| 513 | self._lineno = lineno |
| 514 | self._colno = colno |
| 515 | |
| 516 | def getPublicId(self): |
| 517 | return "pubid" |
| 518 | |
| 519 | def getSystemId(self): |
| 520 | return "sysid" |
| 521 | |
| 522 | def getLineNumber(self): |
| 523 | return self._lineno |
| 524 | |
| 525 | def getColumnNumber(self): |
| 526 | return self._colno |
Martin v. Löwis | 80670bc | 2000-10-06 21:13:23 +0000 | [diff] [blame] | 527 | |
Lars Gustäbel | ab64787 | 2000-09-24 18:40:52 +0000 | [diff] [blame] | 528 | # =========================================================================== |
| 529 | # |
| 530 | # xmlreader tests |
| 531 | # |
| 532 | # =========================================================================== |
| 533 | |
| 534 | # ===== AttributesImpl |
| 535 | |
| 536 | def verify_empty_attrs(attrs): |
| 537 | try: |
| 538 | attrs.getValue("attr") |
| 539 | gvk = 0 |
| 540 | except KeyError: |
| 541 | gvk = 1 |
| 542 | |
| 543 | try: |
| 544 | attrs.getValueByQName("attr") |
| 545 | gvqk = 0 |
| 546 | except KeyError: |
| 547 | gvqk = 1 |
| 548 | |
| 549 | try: |
| 550 | attrs.getNameByQName("attr") |
| 551 | gnqk = 0 |
| 552 | except KeyError: |
| 553 | gnqk = 1 |
| 554 | |
| 555 | try: |
| 556 | attrs.getQNameByName("attr") |
| 557 | gqnk = 0 |
| 558 | except KeyError: |
| 559 | gqnk = 1 |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 560 | |
Lars Gustäbel | ab64787 | 2000-09-24 18:40:52 +0000 | [diff] [blame] | 561 | try: |
| 562 | attrs["attr"] |
| 563 | gik = 0 |
| 564 | except KeyError: |
| 565 | gik = 1 |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 566 | |
Lars Gustäbel | ab64787 | 2000-09-24 18:40:52 +0000 | [diff] [blame] | 567 | return attrs.getLength() == 0 and \ |
| 568 | attrs.getNames() == [] and \ |
| 569 | attrs.getQNames() == [] and \ |
| 570 | len(attrs) == 0 and \ |
| 571 | not attrs.has_key("attr") and \ |
| 572 | attrs.keys() == [] and \ |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 573 | attrs.get("attrs") is None and \ |
Lars Gustäbel | ab64787 | 2000-09-24 18:40:52 +0000 | [diff] [blame] | 574 | attrs.get("attrs", 25) == 25 and \ |
| 575 | attrs.items() == [] and \ |
| 576 | attrs.values() == [] and \ |
| 577 | gvk and gvqk and gnqk and gik and gqnk |
| 578 | |
| 579 | def verify_attrs_wattr(attrs): |
| 580 | return attrs.getLength() == 1 and \ |
| 581 | attrs.getNames() == ["attr"] and \ |
| 582 | attrs.getQNames() == ["attr"] and \ |
| 583 | len(attrs) == 1 and \ |
| 584 | attrs.has_key("attr") and \ |
| 585 | attrs.keys() == ["attr"] and \ |
| 586 | attrs.get("attr") == "val" and \ |
| 587 | attrs.get("attr", 25) == "val" and \ |
| 588 | attrs.items() == [("attr", "val")] and \ |
| 589 | attrs.values() == ["val"] and \ |
| 590 | attrs.getValue("attr") == "val" and \ |
| 591 | attrs.getValueByQName("attr") == "val" and \ |
| 592 | attrs.getNameByQName("attr") == "attr" and \ |
| 593 | attrs["attr"] == "val" and \ |
| 594 | attrs.getQNameByName("attr") == "attr" |
| 595 | |
| 596 | def test_attrs_empty(): |
| 597 | return verify_empty_attrs(AttributesImpl({})) |
| 598 | |
| 599 | def test_attrs_wattr(): |
| 600 | return verify_attrs_wattr(AttributesImpl({"attr" : "val"})) |
| 601 | |
| 602 | # ===== AttributesImpl |
| 603 | |
| 604 | def verify_empty_nsattrs(attrs): |
| 605 | try: |
| 606 | attrs.getValue((ns_uri, "attr")) |
| 607 | gvk = 0 |
| 608 | except KeyError: |
| 609 | gvk = 1 |
| 610 | |
| 611 | try: |
| 612 | attrs.getValueByQName("ns:attr") |
| 613 | gvqk = 0 |
| 614 | except KeyError: |
| 615 | gvqk = 1 |
| 616 | |
| 617 | try: |
| 618 | attrs.getNameByQName("ns:attr") |
| 619 | gnqk = 0 |
| 620 | except KeyError: |
| 621 | gnqk = 1 |
| 622 | |
| 623 | try: |
| 624 | attrs.getQNameByName((ns_uri, "attr")) |
| 625 | gqnk = 0 |
| 626 | except KeyError: |
| 627 | gqnk = 1 |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 628 | |
Lars Gustäbel | ab64787 | 2000-09-24 18:40:52 +0000 | [diff] [blame] | 629 | try: |
| 630 | attrs[(ns_uri, "attr")] |
| 631 | gik = 0 |
| 632 | except KeyError: |
| 633 | gik = 1 |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 634 | |
Lars Gustäbel | ab64787 | 2000-09-24 18:40:52 +0000 | [diff] [blame] | 635 | return attrs.getLength() == 0 and \ |
| 636 | attrs.getNames() == [] and \ |
| 637 | attrs.getQNames() == [] and \ |
| 638 | len(attrs) == 0 and \ |
| 639 | not attrs.has_key((ns_uri, "attr")) and \ |
| 640 | attrs.keys() == [] and \ |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 641 | attrs.get((ns_uri, "attr")) is None and \ |
Lars Gustäbel | ab64787 | 2000-09-24 18:40:52 +0000 | [diff] [blame] | 642 | attrs.get((ns_uri, "attr"), 25) == 25 and \ |
| 643 | attrs.items() == [] and \ |
| 644 | attrs.values() == [] and \ |
| 645 | gvk and gvqk and gnqk and gik and gqnk |
| 646 | |
| 647 | def test_nsattrs_empty(): |
| 648 | return verify_empty_nsattrs(AttributesNSImpl({}, {})) |
| 649 | |
| 650 | def test_nsattrs_wattr(): |
| 651 | attrs = AttributesNSImpl({(ns_uri, "attr") : "val"}, |
| 652 | {(ns_uri, "attr") : "ns:attr"}) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 653 | |
Lars Gustäbel | ab64787 | 2000-09-24 18:40:52 +0000 | [diff] [blame] | 654 | return attrs.getLength() == 1 and \ |
| 655 | attrs.getNames() == [(ns_uri, "attr")] and \ |
| 656 | attrs.getQNames() == ["ns:attr"] and \ |
| 657 | len(attrs) == 1 and \ |
| 658 | attrs.has_key((ns_uri, "attr")) and \ |
| 659 | attrs.keys() == [(ns_uri, "attr")] and \ |
| 660 | attrs.get((ns_uri, "attr")) == "val" and \ |
| 661 | attrs.get((ns_uri, "attr"), 25) == "val" and \ |
| 662 | attrs.items() == [((ns_uri, "attr"), "val")] and \ |
| 663 | attrs.values() == ["val"] and \ |
| 664 | attrs.getValue((ns_uri, "attr")) == "val" and \ |
| 665 | attrs.getValueByQName("ns:attr") == "val" and \ |
| 666 | attrs.getNameByQName("ns:attr") == (ns_uri, "attr") and \ |
| 667 | attrs[(ns_uri, "attr")] == "val" and \ |
| 668 | attrs.getQNameByName((ns_uri, "attr")) == "ns:attr" |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 669 | |
Lars Gustäbel | ab64787 | 2000-09-24 18:40:52 +0000 | [diff] [blame] | 670 | |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 671 | # ===== Main program |
| 672 | |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 673 | def make_test_output(): |
| 674 | parser = create_parser() |
| 675 | result = StringIO() |
| 676 | xmlgen = XMLGenerator(result) |
| 677 | |
| 678 | parser.setContentHandler(xmlgen) |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 679 | parser.parse(findfile("test"+os.extsep+"xml")) |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 680 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 681 | outf = open(findfile("test"+os.extsep+"xml"+os.extsep+"out"), "w") |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 682 | outf.write(result.getvalue()) |
| 683 | outf.close() |
| 684 | |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 685 | items = locals().items() |
| 686 | items.sort() |
| 687 | for (name, value) in items: |
| 688 | if name[ : 5] == "test_": |
| 689 | confirm(value(), name) |
Michael W. Hudson | 3bfed9c | 2004-08-03 10:17:34 +0000 | [diff] [blame] | 690 | # We delete the items variable so that the assignment to items above |
| 691 | # doesn't pick up the old value of items (which messes with attempts |
| 692 | # to find reference leaks). |
| 693 | del items |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 694 | |
Fred Drake | 32f3add | 2002-10-28 17:58:48 +0000 | [diff] [blame] | 695 | if verbose: |
| 696 | print "%d tests, %d failures" % (tests, len(failures)) |
| 697 | if failures: |
| 698 | raise TestFailed("%d of %d tests failed: %s" |
| 699 | % (len(failures), tests, ", ".join(failures))) |