Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 1 | |
| 2 | # regression test for SAX 2.0 |
| 3 | # $Id$ |
| 4 | |
| 5 | from xml.sax.saxutils import XMLGenerator, escape, XMLFilterBase |
| 6 | from xml.sax.expatreader import create_parser |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 7 | from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl |
Lars Gustäbel | ab64787 | 2000-09-24 18:40:52 +0000 | [diff] [blame] | 8 | from xml.sax.handler import ContentHandler |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 9 | from cStringIO import StringIO |
Martin v. Löwis | 33315b1 | 2000-09-24 20:30:24 +0000 | [diff] [blame] | 10 | from test_support import verbose, TestFailed, findfile |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 11 | |
| 12 | # ===== Utilities |
| 13 | |
| 14 | tests = 0 |
| 15 | fails = 0 |
| 16 | |
| 17 | def confirm(outcome, name): |
| 18 | global tests, fails |
| 19 | |
| 20 | tests = tests + 1 |
| 21 | if outcome: |
| 22 | print "Passed", name |
| 23 | else: |
| 24 | print "Failed", name |
| 25 | fails = fails + 1 |
| 26 | |
| 27 | # =========================================================================== |
| 28 | # |
| 29 | # saxutils tests |
| 30 | # |
| 31 | # =========================================================================== |
| 32 | |
| 33 | # ===== escape |
| 34 | |
| 35 | def test_escape_basic(): |
| 36 | return escape("Donald Duck & Co") == "Donald Duck & Co" |
| 37 | |
| 38 | def test_escape_all(): |
| 39 | return escape("<Donald Duck & Co>") == "<Donald Duck & Co>" |
| 40 | |
| 41 | def test_escape_extra(): |
| 42 | return escape("Hei på deg", {"å" : "å"}) == "Hei på deg" |
| 43 | |
| 44 | # ===== XMLGenerator |
| 45 | |
| 46 | start = '<?xml version="1.0" encoding="iso-8859-1"?>\n' |
| 47 | |
| 48 | def test_xmlgen_basic(): |
| 49 | result = StringIO() |
| 50 | gen = XMLGenerator(result) |
| 51 | gen.startDocument() |
| 52 | gen.startElement("doc", {}) |
| 53 | gen.endElement("doc") |
| 54 | gen.endDocument() |
| 55 | |
| 56 | return result.getvalue() == start + "<doc></doc>" |
| 57 | |
| 58 | def test_xmlgen_content(): |
| 59 | result = StringIO() |
| 60 | gen = XMLGenerator(result) |
| 61 | |
| 62 | gen.startDocument() |
| 63 | gen.startElement("doc", {}) |
| 64 | gen.characters("huhei") |
| 65 | gen.endElement("doc") |
| 66 | gen.endDocument() |
| 67 | |
| 68 | return result.getvalue() == start + "<doc>huhei</doc>" |
| 69 | |
| 70 | def test_xmlgen_pi(): |
| 71 | result = StringIO() |
| 72 | gen = XMLGenerator(result) |
| 73 | |
| 74 | gen.startDocument() |
| 75 | gen.processingInstruction("test", "data") |
| 76 | gen.startElement("doc", {}) |
| 77 | gen.endElement("doc") |
| 78 | gen.endDocument() |
| 79 | |
| 80 | return result.getvalue() == start + "<?test data?><doc></doc>" |
| 81 | |
| 82 | def test_xmlgen_content_escape(): |
| 83 | result = StringIO() |
| 84 | gen = XMLGenerator(result) |
| 85 | |
| 86 | gen.startDocument() |
| 87 | gen.startElement("doc", {}) |
| 88 | gen.characters("<huhei&") |
| 89 | gen.endElement("doc") |
| 90 | gen.endDocument() |
| 91 | |
| 92 | return result.getvalue() == start + "<doc><huhei&</doc>" |
| 93 | |
| 94 | def test_xmlgen_ignorable(): |
| 95 | result = StringIO() |
| 96 | gen = XMLGenerator(result) |
| 97 | |
| 98 | gen.startDocument() |
| 99 | gen.startElement("doc", {}) |
| 100 | gen.ignorableWhitespace(" ") |
| 101 | gen.endElement("doc") |
| 102 | gen.endDocument() |
| 103 | |
| 104 | return result.getvalue() == start + "<doc> </doc>" |
| 105 | |
| 106 | ns_uri = "http://www.python.org/xml-ns/saxtest/" |
| 107 | |
| 108 | def test_xmlgen_ns(): |
| 109 | result = StringIO() |
| 110 | gen = XMLGenerator(result) |
| 111 | |
| 112 | gen.startDocument() |
| 113 | gen.startPrefixMapping("ns1", ns_uri) |
Lars Gustäbel | 6a7768a | 2000-09-27 08:12:17 +0000 | [diff] [blame] | 114 | gen.startElementNS((ns_uri, "doc"), "ns1:doc", {}) |
Martin v. Löwis | cf0a1cc | 2000-10-03 22:35:29 +0000 | [diff] [blame^] | 115 | # add an unqualified name |
| 116 | gen.startElementNS((None, "udoc"), None, {}) |
| 117 | gen.endElementNS((None, "udoc"), None) |
Lars Gustäbel | 6a7768a | 2000-09-27 08:12:17 +0000 | [diff] [blame] | 118 | gen.endElementNS((ns_uri, "doc"), "ns1:doc") |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 119 | gen.endPrefixMapping("ns1") |
| 120 | gen.endDocument() |
| 121 | |
Martin v. Löwis | cf0a1cc | 2000-10-03 22:35:29 +0000 | [diff] [blame^] | 122 | return result.getvalue() == start + \ |
| 123 | ('<ns1:doc xmlns:ns1="%s"><udoc></udoc></ns1:doc>' % |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 124 | ns_uri) |
| 125 | |
| 126 | # ===== XMLFilterBase |
| 127 | |
| 128 | def test_filter_basic(): |
| 129 | result = StringIO() |
| 130 | gen = XMLGenerator(result) |
| 131 | filter = XMLFilterBase() |
| 132 | filter.setContentHandler(gen) |
| 133 | |
| 134 | filter.startDocument() |
| 135 | filter.startElement("doc", {}) |
| 136 | filter.characters("content") |
| 137 | filter.ignorableWhitespace(" ") |
| 138 | filter.endElement("doc") |
| 139 | filter.endDocument() |
| 140 | |
| 141 | return result.getvalue() == start + "<doc>content </doc>" |
| 142 | |
| 143 | # =========================================================================== |
| 144 | # |
| 145 | # expatreader tests |
| 146 | # |
| 147 | # =========================================================================== |
| 148 | |
| 149 | # ===== DTDHandler support |
| 150 | |
| 151 | class TestDTDHandler: |
| 152 | |
| 153 | def __init__(self): |
| 154 | self._notations = [] |
| 155 | self._entities = [] |
| 156 | |
| 157 | def notationDecl(self, name, publicId, systemId): |
| 158 | self._notations.append((name, publicId, systemId)) |
| 159 | |
| 160 | def unparsedEntityDecl(self, name, publicId, systemId, ndata): |
| 161 | self._entities.append((name, publicId, systemId, ndata)) |
| 162 | |
Lars Gustäbel | e292a24 | 2000-09-24 20:19:45 +0000 | [diff] [blame] | 163 | def test_expat_dtdhandler(): |
| 164 | parser = create_parser() |
| 165 | handler = TestDTDHandler() |
| 166 | parser.setDTDHandler(handler) |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 167 | |
Lars Gustäbel | e292a24 | 2000-09-24 20:19:45 +0000 | [diff] [blame] | 168 | parser.feed('<!DOCTYPE doc [\n') |
| 169 | parser.feed(' <!ENTITY img SYSTEM "expat.gif" NDATA GIF>\n') |
| 170 | parser.feed(' <!NOTATION GIF PUBLIC "-//CompuServe//NOTATION Graphics Interchange Format 89a//EN">\n') |
| 171 | parser.feed(']>\n') |
| 172 | parser.feed('<doc></doc>') |
| 173 | parser.close() |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 174 | |
Lars Gustäbel | e292a24 | 2000-09-24 20:19:45 +0000 | [diff] [blame] | 175 | return handler._notations == [("GIF", "-//CompuServe//NOTATION Graphics Interchange Format 89a//EN", None)] and \ |
| 176 | handler._entities == [("img", None, "expat.gif", "GIF")] |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 177 | |
| 178 | # ===== EntityResolver support |
| 179 | |
Lars Gustäbel | e292a24 | 2000-09-24 20:19:45 +0000 | [diff] [blame] | 180 | class TestEntityResolver: |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 181 | |
Lars Gustäbel | e292a24 | 2000-09-24 20:19:45 +0000 | [diff] [blame] | 182 | def resolveEntity(self, publicId, systemId): |
| 183 | inpsrc = InputSource() |
| 184 | inpsrc.setByteStream(StringIO("<entity/>")) |
| 185 | return inpsrc |
| 186 | |
| 187 | def test_expat_entityresolver(): |
Lars Gustäbel | e292a24 | 2000-09-24 20:19:45 +0000 | [diff] [blame] | 188 | parser = create_parser() |
| 189 | parser.setEntityResolver(TestEntityResolver()) |
| 190 | result = StringIO() |
| 191 | parser.setContentHandler(XMLGenerator(result)) |
| 192 | |
| 193 | parser.feed('<!DOCTYPE doc [\n') |
| 194 | parser.feed(' <!ENTITY test SYSTEM "whatever">\n') |
| 195 | parser.feed(']>\n') |
| 196 | parser.feed('<doc>&test;</doc>') |
| 197 | parser.close() |
| 198 | |
| 199 | return result.getvalue() == start + "<doc><entity></entity></doc>" |
| 200 | |
Lars Gustäbel | ab64787 | 2000-09-24 18:40:52 +0000 | [diff] [blame] | 201 | # ===== Attributes support |
| 202 | |
| 203 | class AttrGatherer(ContentHandler): |
| 204 | |
| 205 | def startElement(self, name, attrs): |
| 206 | self._attrs = attrs |
| 207 | |
| 208 | def startElementNS(self, name, qname, attrs): |
| 209 | self._attrs = attrs |
| 210 | |
| 211 | def test_expat_attrs_empty(): |
| 212 | parser = create_parser() |
| 213 | gather = AttrGatherer() |
| 214 | parser.setContentHandler(gather) |
| 215 | |
| 216 | parser.feed("<doc/>") |
| 217 | parser.close() |
| 218 | |
| 219 | return verify_empty_attrs(gather._attrs) |
| 220 | |
| 221 | def test_expat_attrs_wattr(): |
| 222 | parser = create_parser() |
| 223 | gather = AttrGatherer() |
| 224 | parser.setContentHandler(gather) |
| 225 | |
| 226 | parser.feed("<doc attr='val'/>") |
| 227 | parser.close() |
| 228 | |
| 229 | return verify_attrs_wattr(gather._attrs) |
| 230 | |
| 231 | def test_expat_nsattrs_empty(): |
| 232 | parser = create_parser(1) |
| 233 | gather = AttrGatherer() |
| 234 | parser.setContentHandler(gather) |
| 235 | |
| 236 | parser.feed("<doc/>") |
| 237 | parser.close() |
| 238 | |
| 239 | return verify_empty_nsattrs(gather._attrs) |
| 240 | |
| 241 | def test_expat_nsattrs_wattr(): |
| 242 | parser = create_parser(1) |
| 243 | gather = AttrGatherer() |
| 244 | parser.setContentHandler(gather) |
| 245 | |
| 246 | parser.feed("<doc xmlns:ns='%s' ns:attr='val'/>" % ns_uri) |
| 247 | parser.close() |
| 248 | |
| 249 | attrs = gather._attrs |
| 250 | |
| 251 | return attrs.getLength() == 1 and \ |
| 252 | attrs.getNames() == [(ns_uri, "attr")] and \ |
| 253 | attrs.getQNames() == [] and \ |
| 254 | len(attrs) == 1 and \ |
| 255 | attrs.has_key((ns_uri, "attr")) and \ |
| 256 | attrs.keys() == [(ns_uri, "attr")] and \ |
| 257 | attrs.get((ns_uri, "attr")) == "val" and \ |
| 258 | attrs.get((ns_uri, "attr"), 25) == "val" and \ |
| 259 | attrs.items() == [((ns_uri, "attr"), "val")] and \ |
| 260 | attrs.values() == ["val"] and \ |
| 261 | attrs.getValue((ns_uri, "attr")) == "val" and \ |
| 262 | attrs[(ns_uri, "attr")] == "val" |
| 263 | |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 264 | # ===== InputSource support |
| 265 | |
Martin v. Löwis | 33315b1 | 2000-09-24 20:30:24 +0000 | [diff] [blame] | 266 | xml_test_out = open(findfile("test.xml.out")).read() |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 267 | |
| 268 | def test_expat_inpsource_filename(): |
| 269 | parser = create_parser() |
| 270 | result = StringIO() |
| 271 | xmlgen = XMLGenerator(result) |
| 272 | |
| 273 | parser.setContentHandler(xmlgen) |
Martin v. Löwis | 33315b1 | 2000-09-24 20:30:24 +0000 | [diff] [blame] | 274 | parser.parse(findfile("test.xml")) |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 275 | |
| 276 | return result.getvalue() == xml_test_out |
| 277 | |
| 278 | def test_expat_inpsource_sysid(): |
| 279 | parser = create_parser() |
| 280 | result = StringIO() |
| 281 | xmlgen = XMLGenerator(result) |
| 282 | |
| 283 | parser.setContentHandler(xmlgen) |
Martin v. Löwis | 33315b1 | 2000-09-24 20:30:24 +0000 | [diff] [blame] | 284 | parser.parse(InputSource(findfile("test.xml"))) |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 285 | |
| 286 | return result.getvalue() == xml_test_out |
| 287 | |
| 288 | def test_expat_inpsource_stream(): |
| 289 | parser = create_parser() |
| 290 | result = StringIO() |
| 291 | xmlgen = XMLGenerator(result) |
| 292 | |
| 293 | parser.setContentHandler(xmlgen) |
| 294 | inpsrc = InputSource() |
Martin v. Löwis | 33315b1 | 2000-09-24 20:30:24 +0000 | [diff] [blame] | 295 | inpsrc.setByteStream(open(findfile("test.xml"))) |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 296 | parser.parse(inpsrc) |
| 297 | |
| 298 | return result.getvalue() == xml_test_out |
| 299 | |
Lars Gustäbel | ab64787 | 2000-09-24 18:40:52 +0000 | [diff] [blame] | 300 | # =========================================================================== |
| 301 | # |
| 302 | # xmlreader tests |
| 303 | # |
| 304 | # =========================================================================== |
| 305 | |
| 306 | # ===== AttributesImpl |
| 307 | |
| 308 | def verify_empty_attrs(attrs): |
| 309 | try: |
| 310 | attrs.getValue("attr") |
| 311 | gvk = 0 |
| 312 | except KeyError: |
| 313 | gvk = 1 |
| 314 | |
| 315 | try: |
| 316 | attrs.getValueByQName("attr") |
| 317 | gvqk = 0 |
| 318 | except KeyError: |
| 319 | gvqk = 1 |
| 320 | |
| 321 | try: |
| 322 | attrs.getNameByQName("attr") |
| 323 | gnqk = 0 |
| 324 | except KeyError: |
| 325 | gnqk = 1 |
| 326 | |
| 327 | try: |
| 328 | attrs.getQNameByName("attr") |
| 329 | gqnk = 0 |
| 330 | except KeyError: |
| 331 | gqnk = 1 |
| 332 | |
| 333 | try: |
| 334 | attrs["attr"] |
| 335 | gik = 0 |
| 336 | except KeyError: |
| 337 | gik = 1 |
| 338 | |
| 339 | return attrs.getLength() == 0 and \ |
| 340 | attrs.getNames() == [] and \ |
| 341 | attrs.getQNames() == [] and \ |
| 342 | len(attrs) == 0 and \ |
| 343 | not attrs.has_key("attr") and \ |
| 344 | attrs.keys() == [] and \ |
| 345 | attrs.get("attrs") == None and \ |
| 346 | attrs.get("attrs", 25) == 25 and \ |
| 347 | attrs.items() == [] and \ |
| 348 | attrs.values() == [] and \ |
| 349 | gvk and gvqk and gnqk and gik and gqnk |
| 350 | |
| 351 | def verify_attrs_wattr(attrs): |
| 352 | return attrs.getLength() == 1 and \ |
| 353 | attrs.getNames() == ["attr"] and \ |
| 354 | attrs.getQNames() == ["attr"] and \ |
| 355 | len(attrs) == 1 and \ |
| 356 | attrs.has_key("attr") and \ |
| 357 | attrs.keys() == ["attr"] and \ |
| 358 | attrs.get("attr") == "val" and \ |
| 359 | attrs.get("attr", 25) == "val" and \ |
| 360 | attrs.items() == [("attr", "val")] and \ |
| 361 | attrs.values() == ["val"] and \ |
| 362 | attrs.getValue("attr") == "val" and \ |
| 363 | attrs.getValueByQName("attr") == "val" and \ |
| 364 | attrs.getNameByQName("attr") == "attr" and \ |
| 365 | attrs["attr"] == "val" and \ |
| 366 | attrs.getQNameByName("attr") == "attr" |
| 367 | |
| 368 | def test_attrs_empty(): |
| 369 | return verify_empty_attrs(AttributesImpl({})) |
| 370 | |
| 371 | def test_attrs_wattr(): |
| 372 | return verify_attrs_wattr(AttributesImpl({"attr" : "val"})) |
| 373 | |
| 374 | # ===== AttributesImpl |
| 375 | |
| 376 | def verify_empty_nsattrs(attrs): |
| 377 | try: |
| 378 | attrs.getValue((ns_uri, "attr")) |
| 379 | gvk = 0 |
| 380 | except KeyError: |
| 381 | gvk = 1 |
| 382 | |
| 383 | try: |
| 384 | attrs.getValueByQName("ns:attr") |
| 385 | gvqk = 0 |
| 386 | except KeyError: |
| 387 | gvqk = 1 |
| 388 | |
| 389 | try: |
| 390 | attrs.getNameByQName("ns:attr") |
| 391 | gnqk = 0 |
| 392 | except KeyError: |
| 393 | gnqk = 1 |
| 394 | |
| 395 | try: |
| 396 | attrs.getQNameByName((ns_uri, "attr")) |
| 397 | gqnk = 0 |
| 398 | except KeyError: |
| 399 | gqnk = 1 |
| 400 | |
| 401 | try: |
| 402 | attrs[(ns_uri, "attr")] |
| 403 | gik = 0 |
| 404 | except KeyError: |
| 405 | gik = 1 |
| 406 | |
| 407 | return attrs.getLength() == 0 and \ |
| 408 | attrs.getNames() == [] and \ |
| 409 | attrs.getQNames() == [] and \ |
| 410 | len(attrs) == 0 and \ |
| 411 | not attrs.has_key((ns_uri, "attr")) and \ |
| 412 | attrs.keys() == [] and \ |
| 413 | attrs.get((ns_uri, "attr")) == None and \ |
| 414 | attrs.get((ns_uri, "attr"), 25) == 25 and \ |
| 415 | attrs.items() == [] and \ |
| 416 | attrs.values() == [] and \ |
| 417 | gvk and gvqk and gnqk and gik and gqnk |
| 418 | |
| 419 | def test_nsattrs_empty(): |
| 420 | return verify_empty_nsattrs(AttributesNSImpl({}, {})) |
| 421 | |
| 422 | def test_nsattrs_wattr(): |
| 423 | attrs = AttributesNSImpl({(ns_uri, "attr") : "val"}, |
| 424 | {(ns_uri, "attr") : "ns:attr"}) |
| 425 | |
| 426 | return attrs.getLength() == 1 and \ |
| 427 | attrs.getNames() == [(ns_uri, "attr")] and \ |
| 428 | attrs.getQNames() == ["ns:attr"] and \ |
| 429 | len(attrs) == 1 and \ |
| 430 | attrs.has_key((ns_uri, "attr")) and \ |
| 431 | attrs.keys() == [(ns_uri, "attr")] and \ |
| 432 | attrs.get((ns_uri, "attr")) == "val" and \ |
| 433 | attrs.get((ns_uri, "attr"), 25) == "val" and \ |
| 434 | attrs.items() == [((ns_uri, "attr"), "val")] and \ |
| 435 | attrs.values() == ["val"] and \ |
| 436 | attrs.getValue((ns_uri, "attr")) == "val" and \ |
| 437 | attrs.getValueByQName("ns:attr") == "val" and \ |
| 438 | attrs.getNameByQName("ns:attr") == (ns_uri, "attr") and \ |
| 439 | attrs[(ns_uri, "attr")] == "val" and \ |
| 440 | attrs.getQNameByName((ns_uri, "attr")) == "ns:attr" |
| 441 | |
| 442 | |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 443 | # ===== Main program |
| 444 | |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 445 | def make_test_output(): |
| 446 | parser = create_parser() |
| 447 | result = StringIO() |
| 448 | xmlgen = XMLGenerator(result) |
| 449 | |
| 450 | parser.setContentHandler(xmlgen) |
Martin v. Löwis | 33315b1 | 2000-09-24 20:30:24 +0000 | [diff] [blame] | 451 | parser.parse(findfile("test.xml")) |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 452 | |
Martin v. Löwis | 33315b1 | 2000-09-24 20:30:24 +0000 | [diff] [blame] | 453 | outf = open(findfile("test.xml.out"), "w") |
Lars Gustäbel | b7536d5 | 2000-09-24 18:53:56 +0000 | [diff] [blame] | 454 | outf.write(result.getvalue()) |
| 455 | outf.close() |
| 456 | |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 457 | items = locals().items() |
| 458 | items.sort() |
| 459 | for (name, value) in items: |
| 460 | if name[ : 5] == "test_": |
| 461 | confirm(value(), name) |
| 462 | |
| 463 | print "%d tests, %d failures" % (tests, fails) |
| 464 | if fails != 0: |
| 465 | raise TestFailed, "%d of %d tests failed" % (fails, tests) |