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 | ab64787 | 2000-09-24 18:40:52 +0000 | [diff] [blame^] | 7 | from xml.sax.xmlreader import AttributesImpl, AttributesNSImpl |
| 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 |
| 10 | from test_support import verbose, TestFailed |
| 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) |
| 114 | gen.startElementNS((ns_uri, "doc"), "ns:doc", {}) |
| 115 | gen.endElementNS((ns_uri, "doc"), "ns:doc") |
| 116 | gen.endPrefixMapping("ns1") |
| 117 | gen.endDocument() |
| 118 | |
| 119 | return result.getvalue() == start + ('<ns1:doc xmlns:ns1="%s"></ns1:doc>' % |
| 120 | ns_uri) |
| 121 | |
| 122 | # ===== XMLFilterBase |
| 123 | |
| 124 | def test_filter_basic(): |
| 125 | result = StringIO() |
| 126 | gen = XMLGenerator(result) |
| 127 | filter = XMLFilterBase() |
| 128 | filter.setContentHandler(gen) |
| 129 | |
| 130 | filter.startDocument() |
| 131 | filter.startElement("doc", {}) |
| 132 | filter.characters("content") |
| 133 | filter.ignorableWhitespace(" ") |
| 134 | filter.endElement("doc") |
| 135 | filter.endDocument() |
| 136 | |
| 137 | return result.getvalue() == start + "<doc>content </doc>" |
| 138 | |
| 139 | # =========================================================================== |
| 140 | # |
| 141 | # expatreader tests |
| 142 | # |
| 143 | # =========================================================================== |
| 144 | |
| 145 | # ===== DTDHandler support |
| 146 | |
| 147 | class TestDTDHandler: |
| 148 | |
| 149 | def __init__(self): |
| 150 | self._notations = [] |
| 151 | self._entities = [] |
| 152 | |
| 153 | def notationDecl(self, name, publicId, systemId): |
| 154 | self._notations.append((name, publicId, systemId)) |
| 155 | |
| 156 | def unparsedEntityDecl(self, name, publicId, systemId, ndata): |
| 157 | self._entities.append((name, publicId, systemId, ndata)) |
| 158 | |
| 159 | # def test_expat_dtdhandler(): |
| 160 | # parser = create_parser() |
| 161 | # handler = TestDTDHandler() |
| 162 | # parser.setDTDHandler(handler) |
| 163 | |
| 164 | # parser.feed('<!DOCTYPE doc [\n') |
| 165 | # parser.feed(' <!ENTITY img SYSTEM "expat.gif" NDATA GIF>\n') |
| 166 | # parser.feed(' <!NOTATION GIF PUBLIC "-//CompuServe//NOTATION Graphics Interchange Format 89a//EN">\n') |
| 167 | # parser.feed(']>\n') |
| 168 | # parser.feed('<doc></doc>') |
| 169 | # parser.close() |
| 170 | |
| 171 | # return handler._notations == [("GIF", "-//CompuServe//NOTATION Graphics Interchange Format 89a//EN", None)] and \ |
| 172 | # handler._entities == [("img", None, "expat.gif", "GIF")] |
| 173 | |
| 174 | # ===== EntityResolver support |
| 175 | |
| 176 | # can't test this until InputSource is in place |
| 177 | |
Lars Gustäbel | ab64787 | 2000-09-24 18:40:52 +0000 | [diff] [blame^] | 178 | # ===== Attributes support |
| 179 | |
| 180 | class AttrGatherer(ContentHandler): |
| 181 | |
| 182 | def startElement(self, name, attrs): |
| 183 | self._attrs = attrs |
| 184 | |
| 185 | def startElementNS(self, name, qname, attrs): |
| 186 | self._attrs = attrs |
| 187 | |
| 188 | def test_expat_attrs_empty(): |
| 189 | parser = create_parser() |
| 190 | gather = AttrGatherer() |
| 191 | parser.setContentHandler(gather) |
| 192 | |
| 193 | parser.feed("<doc/>") |
| 194 | parser.close() |
| 195 | |
| 196 | return verify_empty_attrs(gather._attrs) |
| 197 | |
| 198 | def test_expat_attrs_wattr(): |
| 199 | parser = create_parser() |
| 200 | gather = AttrGatherer() |
| 201 | parser.setContentHandler(gather) |
| 202 | |
| 203 | parser.feed("<doc attr='val'/>") |
| 204 | parser.close() |
| 205 | |
| 206 | return verify_attrs_wattr(gather._attrs) |
| 207 | |
| 208 | def test_expat_nsattrs_empty(): |
| 209 | parser = create_parser(1) |
| 210 | gather = AttrGatherer() |
| 211 | parser.setContentHandler(gather) |
| 212 | |
| 213 | parser.feed("<doc/>") |
| 214 | parser.close() |
| 215 | |
| 216 | return verify_empty_nsattrs(gather._attrs) |
| 217 | |
| 218 | def test_expat_nsattrs_wattr(): |
| 219 | parser = create_parser(1) |
| 220 | gather = AttrGatherer() |
| 221 | parser.setContentHandler(gather) |
| 222 | |
| 223 | parser.feed("<doc xmlns:ns='%s' ns:attr='val'/>" % ns_uri) |
| 224 | parser.close() |
| 225 | |
| 226 | attrs = gather._attrs |
| 227 | |
| 228 | return attrs.getLength() == 1 and \ |
| 229 | attrs.getNames() == [(ns_uri, "attr")] and \ |
| 230 | attrs.getQNames() == [] and \ |
| 231 | len(attrs) == 1 and \ |
| 232 | attrs.has_key((ns_uri, "attr")) and \ |
| 233 | attrs.keys() == [(ns_uri, "attr")] and \ |
| 234 | attrs.get((ns_uri, "attr")) == "val" and \ |
| 235 | attrs.get((ns_uri, "attr"), 25) == "val" and \ |
| 236 | attrs.items() == [((ns_uri, "attr"), "val")] and \ |
| 237 | attrs.values() == ["val"] and \ |
| 238 | attrs.getValue((ns_uri, "attr")) == "val" and \ |
| 239 | attrs[(ns_uri, "attr")] == "val" |
| 240 | |
| 241 | # =========================================================================== |
| 242 | # |
| 243 | # xmlreader tests |
| 244 | # |
| 245 | # =========================================================================== |
| 246 | |
| 247 | # ===== AttributesImpl |
| 248 | |
| 249 | def verify_empty_attrs(attrs): |
| 250 | try: |
| 251 | attrs.getValue("attr") |
| 252 | gvk = 0 |
| 253 | except KeyError: |
| 254 | gvk = 1 |
| 255 | |
| 256 | try: |
| 257 | attrs.getValueByQName("attr") |
| 258 | gvqk = 0 |
| 259 | except KeyError: |
| 260 | gvqk = 1 |
| 261 | |
| 262 | try: |
| 263 | attrs.getNameByQName("attr") |
| 264 | gnqk = 0 |
| 265 | except KeyError: |
| 266 | gnqk = 1 |
| 267 | |
| 268 | try: |
| 269 | attrs.getQNameByName("attr") |
| 270 | gqnk = 0 |
| 271 | except KeyError: |
| 272 | gqnk = 1 |
| 273 | |
| 274 | try: |
| 275 | attrs["attr"] |
| 276 | gik = 0 |
| 277 | except KeyError: |
| 278 | gik = 1 |
| 279 | |
| 280 | return attrs.getLength() == 0 and \ |
| 281 | attrs.getNames() == [] and \ |
| 282 | attrs.getQNames() == [] and \ |
| 283 | len(attrs) == 0 and \ |
| 284 | not attrs.has_key("attr") and \ |
| 285 | attrs.keys() == [] and \ |
| 286 | attrs.get("attrs") == None and \ |
| 287 | attrs.get("attrs", 25) == 25 and \ |
| 288 | attrs.items() == [] and \ |
| 289 | attrs.values() == [] and \ |
| 290 | gvk and gvqk and gnqk and gik and gqnk |
| 291 | |
| 292 | def verify_attrs_wattr(attrs): |
| 293 | return attrs.getLength() == 1 and \ |
| 294 | attrs.getNames() == ["attr"] and \ |
| 295 | attrs.getQNames() == ["attr"] and \ |
| 296 | len(attrs) == 1 and \ |
| 297 | attrs.has_key("attr") and \ |
| 298 | attrs.keys() == ["attr"] and \ |
| 299 | attrs.get("attr") == "val" and \ |
| 300 | attrs.get("attr", 25) == "val" and \ |
| 301 | attrs.items() == [("attr", "val")] and \ |
| 302 | attrs.values() == ["val"] and \ |
| 303 | attrs.getValue("attr") == "val" and \ |
| 304 | attrs.getValueByQName("attr") == "val" and \ |
| 305 | attrs.getNameByQName("attr") == "attr" and \ |
| 306 | attrs["attr"] == "val" and \ |
| 307 | attrs.getQNameByName("attr") == "attr" |
| 308 | |
| 309 | def test_attrs_empty(): |
| 310 | return verify_empty_attrs(AttributesImpl({})) |
| 311 | |
| 312 | def test_attrs_wattr(): |
| 313 | return verify_attrs_wattr(AttributesImpl({"attr" : "val"})) |
| 314 | |
| 315 | # ===== AttributesImpl |
| 316 | |
| 317 | def verify_empty_nsattrs(attrs): |
| 318 | try: |
| 319 | attrs.getValue((ns_uri, "attr")) |
| 320 | gvk = 0 |
| 321 | except KeyError: |
| 322 | gvk = 1 |
| 323 | |
| 324 | try: |
| 325 | attrs.getValueByQName("ns:attr") |
| 326 | gvqk = 0 |
| 327 | except KeyError: |
| 328 | gvqk = 1 |
| 329 | |
| 330 | try: |
| 331 | attrs.getNameByQName("ns:attr") |
| 332 | gnqk = 0 |
| 333 | except KeyError: |
| 334 | gnqk = 1 |
| 335 | |
| 336 | try: |
| 337 | attrs.getQNameByName((ns_uri, "attr")) |
| 338 | gqnk = 0 |
| 339 | except KeyError: |
| 340 | gqnk = 1 |
| 341 | |
| 342 | try: |
| 343 | attrs[(ns_uri, "attr")] |
| 344 | gik = 0 |
| 345 | except KeyError: |
| 346 | gik = 1 |
| 347 | |
| 348 | return attrs.getLength() == 0 and \ |
| 349 | attrs.getNames() == [] and \ |
| 350 | attrs.getQNames() == [] and \ |
| 351 | len(attrs) == 0 and \ |
| 352 | not attrs.has_key((ns_uri, "attr")) and \ |
| 353 | attrs.keys() == [] and \ |
| 354 | attrs.get((ns_uri, "attr")) == None and \ |
| 355 | attrs.get((ns_uri, "attr"), 25) == 25 and \ |
| 356 | attrs.items() == [] and \ |
| 357 | attrs.values() == [] and \ |
| 358 | gvk and gvqk and gnqk and gik and gqnk |
| 359 | |
| 360 | def test_nsattrs_empty(): |
| 361 | return verify_empty_nsattrs(AttributesNSImpl({}, {})) |
| 362 | |
| 363 | def test_nsattrs_wattr(): |
| 364 | attrs = AttributesNSImpl({(ns_uri, "attr") : "val"}, |
| 365 | {(ns_uri, "attr") : "ns:attr"}) |
| 366 | |
| 367 | return attrs.getLength() == 1 and \ |
| 368 | attrs.getNames() == [(ns_uri, "attr")] and \ |
| 369 | attrs.getQNames() == ["ns:attr"] and \ |
| 370 | len(attrs) == 1 and \ |
| 371 | attrs.has_key((ns_uri, "attr")) and \ |
| 372 | attrs.keys() == [(ns_uri, "attr")] and \ |
| 373 | attrs.get((ns_uri, "attr")) == "val" and \ |
| 374 | attrs.get((ns_uri, "attr"), 25) == "val" and \ |
| 375 | attrs.items() == [((ns_uri, "attr"), "val")] and \ |
| 376 | attrs.values() == ["val"] and \ |
| 377 | attrs.getValue((ns_uri, "attr")) == "val" and \ |
| 378 | attrs.getValueByQName("ns:attr") == "val" and \ |
| 379 | attrs.getNameByQName("ns:attr") == (ns_uri, "attr") and \ |
| 380 | attrs[(ns_uri, "attr")] == "val" and \ |
| 381 | attrs.getQNameByName((ns_uri, "attr")) == "ns:attr" |
| 382 | |
| 383 | |
Lars Gustäbel | 96753b3 | 2000-09-24 12:24:24 +0000 | [diff] [blame] | 384 | # ===== Main program |
| 385 | |
| 386 | items = locals().items() |
| 387 | items.sort() |
| 388 | for (name, value) in items: |
| 389 | if name[ : 5] == "test_": |
| 390 | confirm(value(), name) |
| 391 | |
| 392 | print "%d tests, %d failures" % (tests, fails) |
| 393 | if fails != 0: |
| 394 | raise TestFailed, "%d of %d tests failed" % (fails, tests) |