blob: 1e7e03758554bd23b646efa1d049ab73d0befa1b [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äbelab647872000-09-24 18:40:52 +00007from xml.sax.xmlreader import AttributesImpl, AttributesNSImpl
8from xml.sax.handler import ContentHandler
Lars Gustäbel96753b32000-09-24 12:24:24 +00009from cStringIO import StringIO
10from test_support import verbose, TestFailed
11
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)
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
124def 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
147class 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äbelab647872000-09-24 18:40:52 +0000178# ===== Attributes support
179
180class 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
188def 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
198def 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
208def 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
218def 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
249def 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
292def 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
309def test_attrs_empty():
310 return verify_empty_attrs(AttributesImpl({}))
311
312def test_attrs_wattr():
313 return verify_attrs_wattr(AttributesImpl({"attr" : "val"}))
314
315# ===== AttributesImpl
316
317def 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
360def test_nsattrs_empty():
361 return verify_empty_nsattrs(AttributesNSImpl({}, {}))
362
363def 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äbel96753b32000-09-24 12:24:24 +0000384# ===== Main program
385
386items = locals().items()
387items.sort()
388for (name, value) in items:
389 if name[ : 5] == "test_":
390 confirm(value(), name)
391
392print "%d tests, %d failures" % (tests, fails)
393if fails != 0:
394 raise TestFailed, "%d of %d tests failed" % (fails, tests)