blob: 1760eb27974b7cdee2c1f138cf2de6cc95b973f4 [file] [log] [blame]
Lars Gustäbel96753b32000-09-24 12:24:24 +00001
2# regression test for SAX 2.0
3# $Id$
4
5from xml.sax.saxutils import XMLGenerator, escape, XMLFilterBase
6from xml.sax.expatreader import create_parser
Lars Gustäbelb7536d52000-09-24 18:53:56 +00007from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl
Lars Gustäbelab647872000-09-24 18:40:52 +00008from xml.sax.handler import ContentHandler
Lars Gustäbel96753b32000-09-24 12:24:24 +00009from cStringIO import StringIO
Martin v. Löwis33315b12000-09-24 20:30:24 +000010from test_support import verbose, TestFailed, findfile
Lars Gustäbel96753b32000-09-24 12:24:24 +000011
12# ===== Utilities
13
14tests = 0
15fails = 0
16
17def 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
35def test_escape_basic():
36 return escape("Donald Duck & Co") == "Donald Duck & Co"
37
38def test_escape_all():
39 return escape("<Donald Duck & Co>") == "&lt;Donald Duck &amp; Co&gt;"
40
41def test_escape_extra():
42 return escape("Hei på deg", {"å" : "&aring;"}) == "Hei p&aring; deg"
43
44# ===== XMLGenerator
45
46start = '<?xml version="1.0" encoding="iso-8859-1"?>\n'
47
48def 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
58def 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
70def 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
82def 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>&lt;huhei&amp;</doc>"
93
94def 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
106ns_uri = "http://www.python.org/xml-ns/saxtest/"
107
108def test_xmlgen_ns():
109 result = StringIO()
110 gen = XMLGenerator(result)
111
112 gen.startDocument()
113 gen.startPrefixMapping("ns1", ns_uri)
Lars Gustäbel6a7768a2000-09-27 08:12:17 +0000114 gen.startElementNS((ns_uri, "doc"), "ns1:doc", {})
Martin v. Löwiscf0a1cc2000-10-03 22:35:29 +0000115 # add an unqualified name
116 gen.startElementNS((None, "udoc"), None, {})
117 gen.endElementNS((None, "udoc"), None)
Lars Gustäbel6a7768a2000-09-27 08:12:17 +0000118 gen.endElementNS((ns_uri, "doc"), "ns1:doc")
Lars Gustäbel96753b32000-09-24 12:24:24 +0000119 gen.endPrefixMapping("ns1")
120 gen.endDocument()
121
Martin v. Löwiscf0a1cc2000-10-03 22:35:29 +0000122 return result.getvalue() == start + \
123 ('<ns1:doc xmlns:ns1="%s"><udoc></udoc></ns1:doc>' %
Lars Gustäbel96753b32000-09-24 12:24:24 +0000124 ns_uri)
125
126# ===== XMLFilterBase
127
128def 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
151class 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äbele292a242000-09-24 20:19:45 +0000163def test_expat_dtdhandler():
164 parser = create_parser()
165 handler = TestDTDHandler()
166 parser.setDTDHandler(handler)
Lars Gustäbel96753b32000-09-24 12:24:24 +0000167
Lars Gustäbele292a242000-09-24 20:19:45 +0000168 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äbel96753b32000-09-24 12:24:24 +0000174
Lars Gustäbele292a242000-09-24 20:19:45 +0000175 return handler._notations == [("GIF", "-//CompuServe//NOTATION Graphics Interchange Format 89a//EN", None)] and \
176 handler._entities == [("img", None, "expat.gif", "GIF")]
Lars Gustäbel96753b32000-09-24 12:24:24 +0000177
178# ===== EntityResolver support
179
Lars Gustäbele292a242000-09-24 20:19:45 +0000180class TestEntityResolver:
Lars Gustäbel96753b32000-09-24 12:24:24 +0000181
Lars Gustäbele292a242000-09-24 20:19:45 +0000182 def resolveEntity(self, publicId, systemId):
183 inpsrc = InputSource()
184 inpsrc.setByteStream(StringIO("<entity/>"))
185 return inpsrc
186
187def test_expat_entityresolver():
Lars Gustäbele292a242000-09-24 20:19:45 +0000188 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äbelab647872000-09-24 18:40:52 +0000201# ===== Attributes support
202
203class 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
211def 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
221def 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
231def 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
241def 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äbelb7536d52000-09-24 18:53:56 +0000264# ===== InputSource support
265
Martin v. Löwis33315b12000-09-24 20:30:24 +0000266xml_test_out = open(findfile("test.xml.out")).read()
Lars Gustäbelb7536d52000-09-24 18:53:56 +0000267
268def test_expat_inpsource_filename():
269 parser = create_parser()
270 result = StringIO()
271 xmlgen = XMLGenerator(result)
272
273 parser.setContentHandler(xmlgen)
Martin v. Löwis33315b12000-09-24 20:30:24 +0000274 parser.parse(findfile("test.xml"))
Lars Gustäbelb7536d52000-09-24 18:53:56 +0000275
276 return result.getvalue() == xml_test_out
277
278def test_expat_inpsource_sysid():
279 parser = create_parser()
280 result = StringIO()
281 xmlgen = XMLGenerator(result)
282
283 parser.setContentHandler(xmlgen)
Martin v. Löwis33315b12000-09-24 20:30:24 +0000284 parser.parse(InputSource(findfile("test.xml")))
Lars Gustäbelb7536d52000-09-24 18:53:56 +0000285
286 return result.getvalue() == xml_test_out
287
288def 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öwis33315b12000-09-24 20:30:24 +0000295 inpsrc.setByteStream(open(findfile("test.xml")))
Lars Gustäbelb7536d52000-09-24 18:53:56 +0000296 parser.parse(inpsrc)
297
298 return result.getvalue() == xml_test_out
299
Lars Gustäbelab647872000-09-24 18:40:52 +0000300# ===========================================================================
301#
302# xmlreader tests
303#
304# ===========================================================================
305
306# ===== AttributesImpl
307
308def 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
351def 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
368def test_attrs_empty():
369 return verify_empty_attrs(AttributesImpl({}))
370
371def test_attrs_wattr():
372 return verify_attrs_wattr(AttributesImpl({"attr" : "val"}))
373
374# ===== AttributesImpl
375
376def 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
419def test_nsattrs_empty():
420 return verify_empty_nsattrs(AttributesNSImpl({}, {}))
421
422def 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äbel96753b32000-09-24 12:24:24 +0000443# ===== Main program
444
Lars Gustäbelb7536d52000-09-24 18:53:56 +0000445def make_test_output():
446 parser = create_parser()
447 result = StringIO()
448 xmlgen = XMLGenerator(result)
449
450 parser.setContentHandler(xmlgen)
Martin v. Löwis33315b12000-09-24 20:30:24 +0000451 parser.parse(findfile("test.xml"))
Lars Gustäbelb7536d52000-09-24 18:53:56 +0000452
Martin v. Löwis33315b12000-09-24 20:30:24 +0000453 outf = open(findfile("test.xml.out"), "w")
Lars Gustäbelb7536d52000-09-24 18:53:56 +0000454 outf.write(result.getvalue())
455 outf.close()
456
Lars Gustäbel96753b32000-09-24 12:24:24 +0000457items = locals().items()
458items.sort()
459for (name, value) in items:
460 if name[ : 5] == "test_":
461 confirm(value(), name)
462
463print "%d tests, %d failures" % (tests, fails)
464if fails != 0:
465 raise TestFailed, "%d of %d tests failed" % (fails, tests)