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