blob: dcf57d4526a5e8ec71dcb56c7b71742baba2320c [file] [log] [blame]
Lars Gustäbel96753b32000-09-24 12:24:24 +00001# regression test for SAX 2.0
2# $Id$
3
Martin v. Löwis80670bc2000-10-06 21:13:23 +00004from xml.sax import make_parser, ContentHandler, \
5 SAXException, SAXReaderNotAvailable, SAXParseException
Martin v. Löwis962c9e72000-10-06 17:41:52 +00006try:
7 make_parser()
Martin v. Löwis80670bc2000-10-06 21:13:23 +00008except SAXReaderNotAvailable:
Martin v. Löwis962c9e72000-10-06 17:41:52 +00009 # don't try to test this module if we cannot create a parser
10 raise ImportError("no XML parsers available")
Fred Drakeacd32d32001-07-19 16:10:15 +000011from xml.sax.saxutils import XMLGenerator, escape, quoteattr, XMLFilterBase
Lars Gustäbel96753b32000-09-24 12:24:24 +000012from xml.sax.expatreader import create_parser
Lars Gustäbelb7536d52000-09-24 18:53:56 +000013from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl
Lars Gustäbel96753b32000-09-24 12:24:24 +000014from cStringIO import StringIO
Marc-André Lemburg36619082001-01-17 19:11:13 +000015from test_support import verify, verbose, TestFailed, findfile
Lars Gustäbel96753b32000-09-24 12:24:24 +000016
17# ===== Utilities
18
19tests = 0
20fails = 0
21
22def 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äbel2fc52942000-10-24 15:35:07 +000032def test_make_parser2():
Tim Petersd2bf3b72001-01-18 02:22:22 +000033 try:
Lars Gustäbel2fc52942000-10-24 15:35:07 +000034 # 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 Petersd2bf3b72001-01-18 02:22:22 +000053
54
Lars Gustäbel96753b32000-09-24 12:24:24 +000055# ===========================================================================
56#
57# saxutils tests
58#
59# ===========================================================================
60
61# ===== escape
62
63def test_escape_basic():
64 return escape("Donald Duck & Co") == "Donald Duck & Co"
65
66def test_escape_all():
67 return escape("<Donald Duck & Co>") == "&lt;Donald Duck &amp; Co&gt;"
68
69def test_escape_extra():
70 return escape("Hei på deg", {"å" : "&aring;"}) == "Hei p&aring; deg"
71
Fred Drakeacd32d32001-07-19 16:10:15 +000072# ===== quoteattr
73
74def test_quoteattr_basic():
75 return quoteattr("Donald Duck & Co") == '"Donald Duck &amp; Co"'
76
77def test_single_quoteattr():
78 return (quoteattr('Includes "double" quotes')
79 == '\'Includes "double" quotes\'')
80
81def test_double_quoteattr():
82 return (quoteattr("Includes 'single' quotes")
83 == "\"Includes 'single' quotes\"")
84
85def test_single_double_quoteattr():
86 return (quoteattr("Includes 'single' and \"double\" quotes")
87 == "\"Includes 'single' and &quot;double&quot; quotes\"")
88
89# ===== make_parser
90
Martin v. Löwis962c9e72000-10-06 17:41:52 +000091def 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äbel96753b32000-09-24 12:24:24 +0000102# ===== XMLGenerator
103
104start = '<?xml version="1.0" encoding="iso-8859-1"?>\n'
105
106def 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
116def test_xmlgen_content():
117 result = StringIO()
118 gen = XMLGenerator(result)
Fred Drake004d5e62000-10-23 17:22:08 +0000119
Lars Gustäbel96753b32000-09-24 12:24:24 +0000120 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
128def test_xmlgen_pi():
129 result = StringIO()
130 gen = XMLGenerator(result)
Fred Drake004d5e62000-10-23 17:22:08 +0000131
Lars Gustäbel96753b32000-09-24 12:24:24 +0000132 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
140def test_xmlgen_content_escape():
141 result = StringIO()
142 gen = XMLGenerator(result)
Fred Drake004d5e62000-10-23 17:22:08 +0000143
Lars Gustäbel96753b32000-09-24 12:24:24 +0000144 gen.startDocument()
145 gen.startElement("doc", {})
146 gen.characters("<huhei&")
147 gen.endElement("doc")
148 gen.endDocument()
149
150 return result.getvalue() == start + "<doc>&lt;huhei&amp;</doc>"
151
Fred Drakec9fadf92001-08-07 19:17:06 +0000152def test_xmlgen_attr_escape():
153 result = StringIO()
154 gen = XMLGenerator(result)
155
156 gen.startDocument()
157 gen.startElement("doc", {"a": '"'})
158 gen.startElement("e", {"a": "'"})
159 gen.endElement("e")
160 gen.startElement("e", {"a": "'\""})
161 gen.endElement("e")
162 gen.endElement("doc")
163 gen.endDocument()
164
165 return result.getvalue() == start \
166 + "<doc a='\"'><e a=\"'\"></e><e a=\"'&quot;\"></e></doc>"
167
Lars Gustäbel96753b32000-09-24 12:24:24 +0000168def test_xmlgen_ignorable():
169 result = StringIO()
170 gen = XMLGenerator(result)
Fred Drake004d5e62000-10-23 17:22:08 +0000171
Lars Gustäbel96753b32000-09-24 12:24:24 +0000172 gen.startDocument()
173 gen.startElement("doc", {})
174 gen.ignorableWhitespace(" ")
175 gen.endElement("doc")
176 gen.endDocument()
177
178 return result.getvalue() == start + "<doc> </doc>"
179
180ns_uri = "http://www.python.org/xml-ns/saxtest/"
181
182def test_xmlgen_ns():
183 result = StringIO()
184 gen = XMLGenerator(result)
Fred Drake004d5e62000-10-23 17:22:08 +0000185
Lars Gustäbel96753b32000-09-24 12:24:24 +0000186 gen.startDocument()
187 gen.startPrefixMapping("ns1", ns_uri)
Lars Gustäbel6a7768a2000-09-27 08:12:17 +0000188 gen.startElementNS((ns_uri, "doc"), "ns1:doc", {})
Martin v. Löwiscf0a1cc2000-10-03 22:35:29 +0000189 # add an unqualified name
190 gen.startElementNS((None, "udoc"), None, {})
191 gen.endElementNS((None, "udoc"), None)
Lars Gustäbel6a7768a2000-09-27 08:12:17 +0000192 gen.endElementNS((ns_uri, "doc"), "ns1:doc")
Lars Gustäbel96753b32000-09-24 12:24:24 +0000193 gen.endPrefixMapping("ns1")
194 gen.endDocument()
195
Martin v. Löwiscf0a1cc2000-10-03 22:35:29 +0000196 return result.getvalue() == start + \
197 ('<ns1:doc xmlns:ns1="%s"><udoc></udoc></ns1:doc>' %
Lars Gustäbel96753b32000-09-24 12:24:24 +0000198 ns_uri)
199
200# ===== XMLFilterBase
201
202def test_filter_basic():
203 result = StringIO()
204 gen = XMLGenerator(result)
205 filter = XMLFilterBase()
206 filter.setContentHandler(gen)
Fred Drake004d5e62000-10-23 17:22:08 +0000207
Lars Gustäbel96753b32000-09-24 12:24:24 +0000208 filter.startDocument()
209 filter.startElement("doc", {})
210 filter.characters("content")
211 filter.ignorableWhitespace(" ")
212 filter.endElement("doc")
213 filter.endDocument()
214
215 return result.getvalue() == start + "<doc>content </doc>"
216
217# ===========================================================================
218#
219# expatreader tests
220#
221# ===========================================================================
222
Lars Gustäbel07025072000-10-24 16:00:22 +0000223# ===== XMLReader support
224
225def test_expat_file():
226 parser = create_parser()
227 result = StringIO()
228 xmlgen = XMLGenerator(result)
229
230 parser.setContentHandler(xmlgen)
231 parser.parse(open(findfile("test.xml")))
232
233 return result.getvalue() == xml_test_out
234
Lars Gustäbel96753b32000-09-24 12:24:24 +0000235# ===== DTDHandler support
236
237class TestDTDHandler:
238
239 def __init__(self):
240 self._notations = []
241 self._entities = []
Fred Drake004d5e62000-10-23 17:22:08 +0000242
Lars Gustäbel96753b32000-09-24 12:24:24 +0000243 def notationDecl(self, name, publicId, systemId):
244 self._notations.append((name, publicId, systemId))
245
246 def unparsedEntityDecl(self, name, publicId, systemId, ndata):
247 self._entities.append((name, publicId, systemId, ndata))
248
Lars Gustäbele292a242000-09-24 20:19:45 +0000249def test_expat_dtdhandler():
250 parser = create_parser()
251 handler = TestDTDHandler()
252 parser.setDTDHandler(handler)
Lars Gustäbel96753b32000-09-24 12:24:24 +0000253
Lars Gustäbele292a242000-09-24 20:19:45 +0000254 parser.feed('<!DOCTYPE doc [\n')
255 parser.feed(' <!ENTITY img SYSTEM "expat.gif" NDATA GIF>\n')
256 parser.feed(' <!NOTATION GIF PUBLIC "-//CompuServe//NOTATION Graphics Interchange Format 89a//EN">\n')
257 parser.feed(']>\n')
258 parser.feed('<doc></doc>')
259 parser.close()
Lars Gustäbel96753b32000-09-24 12:24:24 +0000260
Lars Gustäbele292a242000-09-24 20:19:45 +0000261 return handler._notations == [("GIF", "-//CompuServe//NOTATION Graphics Interchange Format 89a//EN", None)] and \
262 handler._entities == [("img", None, "expat.gif", "GIF")]
Lars Gustäbel96753b32000-09-24 12:24:24 +0000263
264# ===== EntityResolver support
265
Lars Gustäbele292a242000-09-24 20:19:45 +0000266class TestEntityResolver:
Lars Gustäbel96753b32000-09-24 12:24:24 +0000267
Lars Gustäbele292a242000-09-24 20:19:45 +0000268 def resolveEntity(self, publicId, systemId):
269 inpsrc = InputSource()
270 inpsrc.setByteStream(StringIO("<entity/>"))
271 return inpsrc
272
273def test_expat_entityresolver():
Lars Gustäbele292a242000-09-24 20:19:45 +0000274 parser = create_parser()
275 parser.setEntityResolver(TestEntityResolver())
276 result = StringIO()
277 parser.setContentHandler(XMLGenerator(result))
278
279 parser.feed('<!DOCTYPE doc [\n')
280 parser.feed(' <!ENTITY test SYSTEM "whatever">\n')
281 parser.feed(']>\n')
282 parser.feed('<doc>&test;</doc>')
283 parser.close()
284
285 return result.getvalue() == start + "<doc><entity></entity></doc>"
Fred Drake004d5e62000-10-23 17:22:08 +0000286
Lars Gustäbelab647872000-09-24 18:40:52 +0000287# ===== Attributes support
288
289class AttrGatherer(ContentHandler):
290
291 def startElement(self, name, attrs):
292 self._attrs = attrs
293
294 def startElementNS(self, name, qname, attrs):
295 self._attrs = attrs
Fred Drake004d5e62000-10-23 17:22:08 +0000296
Lars Gustäbelab647872000-09-24 18:40:52 +0000297def test_expat_attrs_empty():
298 parser = create_parser()
299 gather = AttrGatherer()
300 parser.setContentHandler(gather)
301
302 parser.feed("<doc/>")
303 parser.close()
304
305 return verify_empty_attrs(gather._attrs)
306
307def test_expat_attrs_wattr():
308 parser = create_parser()
309 gather = AttrGatherer()
310 parser.setContentHandler(gather)
311
312 parser.feed("<doc attr='val'/>")
313 parser.close()
314
315 return verify_attrs_wattr(gather._attrs)
316
317def test_expat_nsattrs_empty():
318 parser = create_parser(1)
319 gather = AttrGatherer()
320 parser.setContentHandler(gather)
321
322 parser.feed("<doc/>")
323 parser.close()
324
325 return verify_empty_nsattrs(gather._attrs)
326
327def test_expat_nsattrs_wattr():
328 parser = create_parser(1)
329 gather = AttrGatherer()
330 parser.setContentHandler(gather)
331
332 parser.feed("<doc xmlns:ns='%s' ns:attr='val'/>" % ns_uri)
333 parser.close()
334
335 attrs = gather._attrs
Fred Drake004d5e62000-10-23 17:22:08 +0000336
Lars Gustäbelab647872000-09-24 18:40:52 +0000337 return attrs.getLength() == 1 and \
338 attrs.getNames() == [(ns_uri, "attr")] and \
339 attrs.getQNames() == [] and \
340 len(attrs) == 1 and \
341 attrs.has_key((ns_uri, "attr")) and \
342 attrs.keys() == [(ns_uri, "attr")] and \
343 attrs.get((ns_uri, "attr")) == "val" and \
344 attrs.get((ns_uri, "attr"), 25) == "val" and \
345 attrs.items() == [((ns_uri, "attr"), "val")] and \
346 attrs.values() == ["val"] and \
347 attrs.getValue((ns_uri, "attr")) == "val" and \
348 attrs[(ns_uri, "attr")] == "val"
349
Lars Gustäbelb7536d52000-09-24 18:53:56 +0000350# ===== InputSource support
351
Martin v. Löwis33315b12000-09-24 20:30:24 +0000352xml_test_out = open(findfile("test.xml.out")).read()
Lars Gustäbelb7536d52000-09-24 18:53:56 +0000353
354def test_expat_inpsource_filename():
355 parser = create_parser()
356 result = StringIO()
357 xmlgen = XMLGenerator(result)
358
359 parser.setContentHandler(xmlgen)
Martin v. Löwis33315b12000-09-24 20:30:24 +0000360 parser.parse(findfile("test.xml"))
Lars Gustäbelb7536d52000-09-24 18:53:56 +0000361
362 return result.getvalue() == xml_test_out
363
364def test_expat_inpsource_sysid():
365 parser = create_parser()
366 result = StringIO()
367 xmlgen = XMLGenerator(result)
368
369 parser.setContentHandler(xmlgen)
Martin v. Löwis33315b12000-09-24 20:30:24 +0000370 parser.parse(InputSource(findfile("test.xml")))
Lars Gustäbelb7536d52000-09-24 18:53:56 +0000371
372 return result.getvalue() == xml_test_out
373
374def test_expat_inpsource_stream():
375 parser = create_parser()
376 result = StringIO()
377 xmlgen = XMLGenerator(result)
378
379 parser.setContentHandler(xmlgen)
380 inpsrc = InputSource()
Martin v. Löwis33315b12000-09-24 20:30:24 +0000381 inpsrc.setByteStream(open(findfile("test.xml")))
Lars Gustäbelb7536d52000-09-24 18:53:56 +0000382 parser.parse(inpsrc)
383
384 return result.getvalue() == xml_test_out
385
Lars Gustäbel2fc52942000-10-24 15:35:07 +0000386# ===== IncrementalParser support
387
388def test_expat_incremental():
389 result = StringIO()
390 xmlgen = XMLGenerator(result)
391 parser = create_parser()
392 parser.setContentHandler(xmlgen)
393
394 parser.feed("<doc>")
395 parser.feed("</doc>")
396 parser.close()
397
398 return result.getvalue() == start + "<doc></doc>"
399
400def test_expat_incremental_reset():
401 result = StringIO()
402 xmlgen = XMLGenerator(result)
403 parser = create_parser()
404 parser.setContentHandler(xmlgen)
405
406 parser.feed("<doc>")
407 parser.feed("text")
408
409 result = StringIO()
410 xmlgen = XMLGenerator(result)
411 parser.setContentHandler(xmlgen)
412 parser.reset()
413
414 parser.feed("<doc>")
415 parser.feed("text")
416 parser.feed("</doc>")
417 parser.close()
418
419 return result.getvalue() == start + "<doc>text</doc>"
420
421# ===== Locator support
422
423def test_expat_locator_noinfo():
424 result = StringIO()
425 xmlgen = XMLGenerator(result)
426 parser = create_parser()
427 parser.setContentHandler(xmlgen)
428
429 parser.feed("<doc>")
430 parser.feed("</doc>")
431 parser.close()
432
Fred Drake132dce22000-12-12 23:11:42 +0000433 return parser.getSystemId() is None and \
434 parser.getPublicId() is None and \
Tim Petersd2bf3b72001-01-18 02:22:22 +0000435 parser.getLineNumber() == 1
Lars Gustäbel2fc52942000-10-24 15:35:07 +0000436
437def test_expat_locator_withinfo():
438 result = StringIO()
439 xmlgen = XMLGenerator(result)
440 parser = create_parser()
441 parser.setContentHandler(xmlgen)
442 parser.parse(findfile("test.xml"))
443
444 return parser.getSystemId() == findfile("test.xml") and \
Fred Drake132dce22000-12-12 23:11:42 +0000445 parser.getPublicId() is None
Lars Gustäbel2fc52942000-10-24 15:35:07 +0000446
Martin v. Löwis80670bc2000-10-06 21:13:23 +0000447
448# ===========================================================================
449#
450# error reporting
451#
452# ===========================================================================
453
454def test_expat_inpsource_location():
455 parser = create_parser()
456 parser.setContentHandler(ContentHandler()) # do nothing
457 source = InputSource()
458 source.setByteStream(StringIO("<foo bar foobar>")) #ill-formed
459 name = "a file name"
460 source.setSystemId(name)
461 try:
462 parser.parse(source)
463 except SAXException, e:
464 return e.getSystemId() == name
465
466def test_expat_incomplete():
467 parser = create_parser()
468 parser.setContentHandler(ContentHandler()) # do nothing
469 try:
470 parser.parse(StringIO("<foo>"))
471 except SAXParseException:
472 return 1 # ok, error found
473 else:
474 return 0
475
476
Lars Gustäbelab647872000-09-24 18:40:52 +0000477# ===========================================================================
478#
479# xmlreader tests
480#
481# ===========================================================================
482
483# ===== AttributesImpl
484
485def verify_empty_attrs(attrs):
486 try:
487 attrs.getValue("attr")
488 gvk = 0
489 except KeyError:
490 gvk = 1
491
492 try:
493 attrs.getValueByQName("attr")
494 gvqk = 0
495 except KeyError:
496 gvqk = 1
497
498 try:
499 attrs.getNameByQName("attr")
500 gnqk = 0
501 except KeyError:
502 gnqk = 1
503
504 try:
505 attrs.getQNameByName("attr")
506 gqnk = 0
507 except KeyError:
508 gqnk = 1
Fred Drake004d5e62000-10-23 17:22:08 +0000509
Lars Gustäbelab647872000-09-24 18:40:52 +0000510 try:
511 attrs["attr"]
512 gik = 0
513 except KeyError:
514 gik = 1
Fred Drake004d5e62000-10-23 17:22:08 +0000515
Lars Gustäbelab647872000-09-24 18:40:52 +0000516 return attrs.getLength() == 0 and \
517 attrs.getNames() == [] and \
518 attrs.getQNames() == [] and \
519 len(attrs) == 0 and \
520 not attrs.has_key("attr") and \
521 attrs.keys() == [] and \
Fred Drake132dce22000-12-12 23:11:42 +0000522 attrs.get("attrs") is None and \
Lars Gustäbelab647872000-09-24 18:40:52 +0000523 attrs.get("attrs", 25) == 25 and \
524 attrs.items() == [] and \
525 attrs.values() == [] and \
526 gvk and gvqk and gnqk and gik and gqnk
527
528def verify_attrs_wattr(attrs):
529 return attrs.getLength() == 1 and \
530 attrs.getNames() == ["attr"] and \
531 attrs.getQNames() == ["attr"] and \
532 len(attrs) == 1 and \
533 attrs.has_key("attr") and \
534 attrs.keys() == ["attr"] and \
535 attrs.get("attr") == "val" and \
536 attrs.get("attr", 25) == "val" and \
537 attrs.items() == [("attr", "val")] and \
538 attrs.values() == ["val"] and \
539 attrs.getValue("attr") == "val" and \
540 attrs.getValueByQName("attr") == "val" and \
541 attrs.getNameByQName("attr") == "attr" and \
542 attrs["attr"] == "val" and \
543 attrs.getQNameByName("attr") == "attr"
544
545def test_attrs_empty():
546 return verify_empty_attrs(AttributesImpl({}))
547
548def test_attrs_wattr():
549 return verify_attrs_wattr(AttributesImpl({"attr" : "val"}))
550
551# ===== AttributesImpl
552
553def verify_empty_nsattrs(attrs):
554 try:
555 attrs.getValue((ns_uri, "attr"))
556 gvk = 0
557 except KeyError:
558 gvk = 1
559
560 try:
561 attrs.getValueByQName("ns:attr")
562 gvqk = 0
563 except KeyError:
564 gvqk = 1
565
566 try:
567 attrs.getNameByQName("ns:attr")
568 gnqk = 0
569 except KeyError:
570 gnqk = 1
571
572 try:
573 attrs.getQNameByName((ns_uri, "attr"))
574 gqnk = 0
575 except KeyError:
576 gqnk = 1
Fred Drake004d5e62000-10-23 17:22:08 +0000577
Lars Gustäbelab647872000-09-24 18:40:52 +0000578 try:
579 attrs[(ns_uri, "attr")]
580 gik = 0
581 except KeyError:
582 gik = 1
Fred Drake004d5e62000-10-23 17:22:08 +0000583
Lars Gustäbelab647872000-09-24 18:40:52 +0000584 return attrs.getLength() == 0 and \
585 attrs.getNames() == [] and \
586 attrs.getQNames() == [] and \
587 len(attrs) == 0 and \
588 not attrs.has_key((ns_uri, "attr")) and \
589 attrs.keys() == [] and \
Fred Drake132dce22000-12-12 23:11:42 +0000590 attrs.get((ns_uri, "attr")) is None and \
Lars Gustäbelab647872000-09-24 18:40:52 +0000591 attrs.get((ns_uri, "attr"), 25) == 25 and \
592 attrs.items() == [] and \
593 attrs.values() == [] and \
594 gvk and gvqk and gnqk and gik and gqnk
595
596def test_nsattrs_empty():
597 return verify_empty_nsattrs(AttributesNSImpl({}, {}))
598
599def test_nsattrs_wattr():
600 attrs = AttributesNSImpl({(ns_uri, "attr") : "val"},
601 {(ns_uri, "attr") : "ns:attr"})
Fred Drake004d5e62000-10-23 17:22:08 +0000602
Lars Gustäbelab647872000-09-24 18:40:52 +0000603 return attrs.getLength() == 1 and \
604 attrs.getNames() == [(ns_uri, "attr")] and \
605 attrs.getQNames() == ["ns:attr"] and \
606 len(attrs) == 1 and \
607 attrs.has_key((ns_uri, "attr")) and \
608 attrs.keys() == [(ns_uri, "attr")] and \
609 attrs.get((ns_uri, "attr")) == "val" and \
610 attrs.get((ns_uri, "attr"), 25) == "val" and \
611 attrs.items() == [((ns_uri, "attr"), "val")] and \
612 attrs.values() == ["val"] and \
613 attrs.getValue((ns_uri, "attr")) == "val" and \
614 attrs.getValueByQName("ns:attr") == "val" and \
615 attrs.getNameByQName("ns:attr") == (ns_uri, "attr") and \
616 attrs[(ns_uri, "attr")] == "val" and \
617 attrs.getQNameByName((ns_uri, "attr")) == "ns:attr"
Fred Drake004d5e62000-10-23 17:22:08 +0000618
Lars Gustäbelab647872000-09-24 18:40:52 +0000619
Lars Gustäbel96753b32000-09-24 12:24:24 +0000620# ===== Main program
621
Lars Gustäbelb7536d52000-09-24 18:53:56 +0000622def make_test_output():
623 parser = create_parser()
624 result = StringIO()
625 xmlgen = XMLGenerator(result)
626
627 parser.setContentHandler(xmlgen)
Martin v. Löwis33315b12000-09-24 20:30:24 +0000628 parser.parse(findfile("test.xml"))
Lars Gustäbelb7536d52000-09-24 18:53:56 +0000629
Martin v. Löwis33315b12000-09-24 20:30:24 +0000630 outf = open(findfile("test.xml.out"), "w")
Lars Gustäbelb7536d52000-09-24 18:53:56 +0000631 outf.write(result.getvalue())
632 outf.close()
633
Lars Gustäbel96753b32000-09-24 12:24:24 +0000634items = locals().items()
635items.sort()
636for (name, value) in items:
637 if name[ : 5] == "test_":
638 confirm(value(), name)
639
640print "%d tests, %d failures" % (tests, fails)
641if fails != 0:
642 raise TestFailed, "%d of %d tests failed" % (fails, tests)