blob: bdef6f5eebdd23784e9a563f6b074587b1e9f92c [file] [log] [blame]
Fred Drake17647f52000-07-03 16:37:42 +00001# test for xml.dom.minidom
Paul Prescod7993bcc2000-07-01 14:54:16 +00002
Fred Drake17647f52000-07-03 16:37:42 +00003from xml.dom.minidom import parse, Node, Document, parseString
Andrew M. Kuchling6d0cee12001-01-02 20:56:42 +00004from xml.dom import HierarchyRequestErr
Barry Warsawc79dff62000-09-26 18:00:20 +00005import xml.parsers.expat
Fred Drake17647f52000-07-03 16:37:42 +00006
7import os.path
8import sys
9import traceback
Fredrik Lundhf7850422001-01-17 21:51:36 +000010from test_support import verbose
Fred Drake17647f52000-07-03 16:37:42 +000011
12if __name__ == "__main__":
13 base = sys.argv[0]
14else:
15 base = __file__
16tstfile = os.path.join(os.path.dirname(base), "test.xml")
17del base
Paul Prescod7993bcc2000-07-01 14:54:16 +000018
Jeremy Hylton3b0c6002000-10-12 17:31:36 +000019def confirm(test, testname = "Test"):
Fred Drake004d5e62000-10-23 17:22:08 +000020 if test:
Paul Prescod10d27662000-09-18 19:07:26 +000021 print "Passed " + testname
Fred Drake004d5e62000-10-23 17:22:08 +000022 else:
Paul Prescod10d27662000-09-18 19:07:26 +000023 print "Failed " + testname
24 raise Exception
25
Jeremy Hylton3b0c6002000-10-12 17:31:36 +000026Node._debug = 1
Paul Prescod7993bcc2000-07-01 14:54:16 +000027
Paul Prescod10d27662000-09-18 19:07:26 +000028def testParseFromFile():
29 from StringIO import StringIO
Jeremy Hylton3b0c6002000-10-12 17:31:36 +000030 dom = parse(StringIO(open(tstfile).read()))
Paul Prescod4c799192000-09-19 19:33:02 +000031 dom.unlink()
Martin v. Löwis89c528b2000-09-19 16:22:10 +000032 confirm(isinstance(dom,Document))
Paul Prescod10d27662000-09-18 19:07:26 +000033
Jeremy Hylton3b0c6002000-10-12 17:31:36 +000034def testGetElementsByTagName():
35 dom = parse(tstfile)
36 confirm(dom.getElementsByTagName("LI") == \
37 dom.documentElement.getElementsByTagName("LI"))
Paul Prescod7993bcc2000-07-01 14:54:16 +000038 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +000039
Jeremy Hylton3b0c6002000-10-12 17:31:36 +000040def testInsertBefore():
Fred Drakea1bde802000-11-21 22:02:43 +000041 dom = parseString("<doc><foo/></doc>")
42 root = dom.documentElement
43 elem = root.childNodes[0]
44 nelem = dom.createElement("element")
45 root.insertBefore(nelem, elem)
46 confirm(len(root.childNodes) == 2
47 and root.childNodes[0] is nelem
48 and root.childNodes[1] is elem
49 and root.firstChild is nelem
50 and root.lastChild is elem
51 and root.toxml() == "<doc><element/><foo/></doc>"
52 , "testInsertBefore -- node properly placed in tree")
53 nelem = dom.createElement("element")
54 root.insertBefore(nelem, None)
55 confirm(len(root.childNodes) == 3
56 and root.childNodes[1] is elem
57 and root.childNodes[2] is nelem
58 and root.lastChild is nelem
59 and nelem.previousSibling is elem
60 and root.toxml() == "<doc><element/><foo/><element/></doc>"
61 , "testInsertBefore -- node properly placed in tree")
62 nelem2 = dom.createElement("bar")
63 root.insertBefore(nelem2, nelem)
64 confirm(len(root.childNodes) == 4
65 and root.childNodes[2] is nelem2
66 and root.childNodes[3] is nelem
67 and nelem2.nextSibling is nelem
68 and nelem.previousSibling is nelem2
69 and root.toxml() == "<doc><element/><foo/><bar/><element/></doc>"
70 , "testInsertBefore -- node properly placed in tree")
Paul Prescod7993bcc2000-07-01 14:54:16 +000071 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +000072
73def testAppendChild():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +000074 dom = parse(tstfile)
75 dom.documentElement.appendChild(dom.createComment(u"Hello"))
76 confirm(dom.documentElement.childNodes[-1].nodeName == "#comment")
77 confirm(dom.documentElement.childNodes[-1].data == "Hello")
Paul Prescod7993bcc2000-07-01 14:54:16 +000078 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +000079
Andrew M. Kuchlingad4a5582000-12-31 04:03:27 +000080def testLegalChildren():
81 dom = Document()
82 elem = dom.createElement('element')
83 text = dom.createTextNode('text')
Fredrik Lundhf7850422001-01-17 21:51:36 +000084
Andrew M. Kuchlingad4a5582000-12-31 04:03:27 +000085 try: dom.appendChild(text)
86 except HierarchyRequestErr: pass
87 else:
88 print "dom.appendChild didn't raise HierarchyRequestErr"
89
90 dom.appendChild(elem)
91 try: dom.insertBefore(text, elem)
92 except HierarchyRequestErr: pass
93 else:
94 print "dom.appendChild didn't raise HierarchyRequestErr"
95
96 try: dom.replaceChild(text, elem)
97 except HierarchyRequestErr: pass
98 else:
99 print "dom.appendChild didn't raise HierarchyRequestErr"
100
Tim Peters0009c4e2001-02-21 07:29:48 +0000101 nodemap = elem.attributes
Andrew M. Kuchlingbc8f72c2001-02-21 01:30:26 +0000102 try: nodemap.setNamedItem(text)
103 except HierarchyRequestErr: pass
104 else:
105 print "NamedNodeMap.setNamedItem didn't raise HierarchyRequestErr"
106
107 try: nodemap.setNamedItemNS(text)
108 except HierarchyRequestErr: pass
109 else:
110 print "NamedNodeMap.setNamedItemNS didn't raise HierarchyRequestErr"
111
Andrew M. Kuchlingad4a5582000-12-31 04:03:27 +0000112 elem.appendChild(text)
Fredrik Lundhf7850422001-01-17 21:51:36 +0000113 dom.unlink()
Andrew M. Kuchlingad4a5582000-12-31 04:03:27 +0000114
Paul Prescod7993bcc2000-07-01 14:54:16 +0000115def testNonZero():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000116 dom = parse(tstfile)
117 confirm(dom)# should not be zero
118 dom.appendChild(dom.createComment("foo"))
119 confirm(not dom.childNodes[-1].childNodes)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000120 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000121
122def testUnlink():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000123 dom = parse(tstfile)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000124 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000125
126def testElement():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000127 dom = Document()
128 dom.appendChild(dom.createElement("abc"))
129 confirm(dom.documentElement)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000130 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000131
132def testAAA():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000133 dom = parseString("<abc/>")
134 el = dom.documentElement
135 el.setAttribute("spam", "jam2")
Fred Drakea1bde802000-11-21 22:02:43 +0000136 confirm(el.toxml() == '<abc spam="jam2"/>', "testAAA")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000137 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000138
139def testAAB():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000140 dom = parseString("<abc/>")
141 el = dom.documentElement
142 el.setAttribute("spam", "jam")
143 el.setAttribute("spam", "jam2")
Fred Drakea1bde802000-11-21 22:02:43 +0000144 confirm(el.toxml() == '<abc spam="jam2"/>', "testAAB")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000145 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000146
147def testAddAttr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000148 dom = Document()
149 child = dom.appendChild(dom.createElement("abc"))
Paul Prescod7993bcc2000-07-01 14:54:16 +0000150
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000151 child.setAttribute("def", "ghi")
152 confirm(child.getAttribute("def") == "ghi")
153 confirm(child.attributes["def"].value == "ghi")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000154
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000155 child.setAttribute("jkl", "mno")
156 confirm(child.getAttribute("jkl") == "mno")
157 confirm(child.attributes["jkl"].value == "mno")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000158
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000159 confirm(len(child.attributes) == 2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000160
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000161 child.setAttribute("def", "newval")
162 confirm(child.getAttribute("def") == "newval")
163 confirm(child.attributes["def"].value == "newval")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000164
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000165 confirm(len(child.attributes) == 2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000166 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000167
168def testDeleteAttr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000169 dom = Document()
170 child = dom.appendChild(dom.createElement("abc"))
Paul Prescod7993bcc2000-07-01 14:54:16 +0000171
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000172 confirm(len(child.attributes) == 0)
173 child.setAttribute("def", "ghi")
174 confirm(len(child.attributes) == 1)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000175 del child.attributes["def"]
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000176 confirm(len(child.attributes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000177 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000178
179def testRemoveAttr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000180 dom = Document()
181 child = dom.appendChild(dom.createElement("abc"))
Paul Prescod7993bcc2000-07-01 14:54:16 +0000182
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000183 child.setAttribute("def", "ghi")
184 confirm(len(child.attributes) == 1)
185 child.removeAttribute("def")
186 confirm(len(child.attributes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000187
188 dom.unlink()
189
190def testRemoveAttrNS():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000191 dom = Document()
192 child = dom.appendChild(
193 dom.createElementNS("http://www.python.org", "python:abc"))
Fred Drake004d5e62000-10-23 17:22:08 +0000194 child.setAttributeNS("http://www.w3.org", "xmlns:python",
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000195 "http://www.python.org")
196 child.setAttributeNS("http://www.python.org", "python:abcattr", "foo")
197 confirm(len(child.attributes) == 2)
198 child.removeAttributeNS("http://www.python.org", "abcattr")
199 confirm(len(child.attributes) == 1)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000200
201 dom.unlink()
Fred Drake004d5e62000-10-23 17:22:08 +0000202
Paul Prescod7993bcc2000-07-01 14:54:16 +0000203def testRemoveAttributeNode():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000204 dom = Document()
205 child = dom.appendChild(dom.createElement("foo"))
206 child.setAttribute("spam", "jam")
207 confirm(len(child.attributes) == 1)
208 node = child.getAttributeNode("spam")
209 child.removeAttributeNode(node)
210 confirm(len(child.attributes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000211
212 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000213
214def testChangeAttr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000215 dom = parseString("<abc/>")
216 el = dom.documentElement
217 el.setAttribute("spam", "jam")
218 confirm(len(el.attributes) == 1)
219 el.setAttribute("spam", "bam")
220 confirm(len(el.attributes) == 1)
221 el.attributes["spam"] = "ham"
222 confirm(len(el.attributes) == 1)
223 el.setAttribute("spam2", "bam")
224 confirm(len(el.attributes) == 2)
225 el.attributes[ "spam2"] = "bam2"
226 confirm(len(el.attributes) == 2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000227 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000228
229def testGetAttrList():
230 pass
231
232def testGetAttrValues(): pass
233
234def testGetAttrLength(): pass
235
236def testGetAttribute(): pass
237
238def testGetAttributeNS(): pass
239
240def testGetAttributeNode(): pass
241
Martin v. Löwis351c3d02001-06-03 14:27:02 +0000242def testGetElementsByTagNameNS():
243 d="""<foo xmlns:minidom="http://pyxml.sf.net/minidom">
244 <minidom:myelem/>
245 </foo>"""
246 dom = parseString(d)
247 elem = dom.getElementsByTagNameNS("http://pyxml.sf.net/minidom","myelem")
248 confirm(len(elem) == 1)
249 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000250
251def testGetEmptyNodeListFromElementsByTagNameNS(): pass
252
253def testElementReprAndStr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000254 dom = Document()
255 el = dom.appendChild(dom.createElement("abc"))
256 string1 = repr(el)
257 string2 = str(el)
258 confirm(string1 == string2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000259 dom.unlink()
260
261# commented out until Fredrick's fix is checked in
262def _testElementReprAndStrUnicode():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000263 dom = Document()
264 el = dom.appendChild(dom.createElement(u"abc"))
265 string1 = repr(el)
266 string2 = str(el)
267 confirm(string1 == string2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000268 dom.unlink()
269
270# commented out until Fredrick's fix is checked in
271def _testElementReprAndStrUnicodeNS():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000272 dom = Document()
273 el = dom.appendChild(
274 dom.createElementNS(u"http://www.slashdot.org", u"slash:abc"))
275 string1 = repr(el)
276 string2 = str(el)
277 confirm(string1 == string2)
278 confirm(string1.find("slash:abc") != -1)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000279 dom.unlink()
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000280 confirm(len(Node.allnodes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000281
282def testAttributeRepr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000283 dom = Document()
284 el = dom.appendChild(dom.createElement(u"abc"))
285 node = el.setAttribute("abc", "def")
286 confirm(str(node) == repr(node))
Paul Prescod7993bcc2000-07-01 14:54:16 +0000287 dom.unlink()
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000288 confirm(len(Node.allnodes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000289
290def testTextNodeRepr(): pass
291
Martin v. Löwis0a84a332000-10-06 22:42:55 +0000292def testWriteXML():
Martin v. Löwisfe28ca02001-02-06 01:16:48 +0000293 str = '<?xml version="1.0" ?>\n<a b="c"/>'
Martin v. Löwis0a84a332000-10-06 22:42:55 +0000294 dom = parseString(str)
295 domstr = dom.toxml()
296 dom.unlink()
297 confirm(str == domstr)
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000298 confirm(len(Node.allnodes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000299
300def testProcessingInstruction(): pass
301
302def testProcessingInstructionRepr(): pass
303
304def testTextRepr(): pass
305
306def testWriteText(): pass
307
308def testDocumentElement(): pass
309
Fred Drakea1bde802000-11-21 22:02:43 +0000310def testTooManyDocumentElements():
311 doc = parseString("<doc/>")
312 elem = doc.createElement("extra")
313 try:
314 doc.appendChild(elem)
Martin v. Löwis2bcb3232001-01-27 09:17:55 +0000315 except HierarchyRequestErr:
Fred Drakea1bde802000-11-21 22:02:43 +0000316 print "Caught expected exception when adding extra document element."
317 else:
318 print "Failed to catch expected exception when" \
319 " adding extra document element."
320 elem.unlink()
321 doc.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000322
323def testCreateElementNS(): pass
324
Andrew M. Kuchlingad4a5582000-12-31 04:03:27 +0000325def testCreateAttributeNS(): pass
Paul Prescod7993bcc2000-07-01 14:54:16 +0000326
327def testParse(): pass
328
329def testParseString(): pass
330
331def testComment(): pass
332
333def testAttrListItem(): pass
334
335def testAttrListItems(): pass
336
337def testAttrListItemNS(): pass
338
339def testAttrListKeys(): pass
340
341def testAttrListKeysNS(): pass
342
343def testAttrListValues(): pass
344
345def testAttrListLength(): pass
346
347def testAttrList__getitem__(): pass
348
349def testAttrList__setitem__(): pass
350
351def testSetAttrValueandNodeValue(): pass
352
353def testParseElement(): pass
354
355def testParseAttributes(): pass
356
357def testParseElementNamespaces(): pass
358
359def testParseAttributeNamespaces(): pass
360
361def testParseProcessingInstructions(): pass
362
363def testChildNodes(): pass
364
365def testFirstChild(): pass
366
367def testHasChildNodes(): pass
368
Fred Drakea1bde802000-11-21 22:02:43 +0000369def testCloneElementShallow():
370 dom, clone = _setupCloneElement(0)
371 confirm(len(clone.childNodes) == 0
372 and clone.parentNode is None
373 and clone.toxml() == '<doc attr="value"/>'
374 , "testCloneElementShallow")
375 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000376
Fred Drakea1bde802000-11-21 22:02:43 +0000377def testCloneElementDeep():
378 dom, clone = _setupCloneElement(1)
379 confirm(len(clone.childNodes) == 1
380 and clone.parentNode is None
381 and clone.toxml() == '<doc attr="value"><foo/></doc>'
382 , "testCloneElementDeep")
383 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000384
Fred Drakea1bde802000-11-21 22:02:43 +0000385def _setupCloneElement(deep):
386 dom = parseString("<doc attr='value'><foo/></doc>")
387 root = dom.documentElement
388 clone = root.cloneNode(deep)
389 _testCloneElementCopiesAttributes(
390 root, clone, "testCloneElement" + (deep and "Deep" or "Shallow"))
391 # mutilate the original so shared data is detected
392 root.tagName = root.nodeName = "MODIFIED"
393 root.setAttribute("attr", "NEW VALUE")
394 root.setAttribute("added", "VALUE")
395 return dom, clone
396
397def _testCloneElementCopiesAttributes(e1, e2, test):
398 attrs1 = e1.attributes
399 attrs2 = e2.attributes
400 keys1 = attrs1.keys()
401 keys2 = attrs2.keys()
402 keys1.sort()
403 keys2.sort()
404 confirm(keys1 == keys2, "clone of element has same attribute keys")
405 for i in range(len(keys1)):
406 a1 = attrs1.item(i)
407 a2 = attrs2.item(i)
408 confirm(a1 is not a2
409 and a1.value == a2.value
410 and a1.nodeValue == a2.nodeValue
411 and a1.namespaceURI == a2.namespaceURI
412 and a1.localName == a2.localName
413 , "clone of attribute node has proper attribute values")
414 confirm(a2.ownerElement is e2,
415 "clone of attribute node correctly owned")
Fredrik Lundhf7850422001-01-17 21:51:36 +0000416
Paul Prescod7993bcc2000-07-01 14:54:16 +0000417
418def testCloneDocumentShallow(): pass
419
420def testCloneDocumentDeep(): pass
421
422def testCloneAttributeShallow(): pass
423
424def testCloneAttributeDeep(): pass
425
426def testClonePIShallow(): pass
427
428def testClonePIDeep(): pass
429
Fred Drakea1bde802000-11-21 22:02:43 +0000430def testNormalize():
431 doc = parseString("<doc/>")
432 root = doc.documentElement
433 root.appendChild(doc.createTextNode("first"))
434 root.appendChild(doc.createTextNode("second"))
435 confirm(len(root.childNodes) == 2, "testNormalize -- preparation")
436 doc.normalize()
437 confirm(len(root.childNodes) == 1
438 and root.firstChild is root.lastChild
439 and root.firstChild.data == "firstsecond"
440 , "testNormalize -- result")
441 doc.unlink()
442
Fred Drake3277da02000-12-14 18:20:22 +0000443 doc = parseString("<doc/>")
444 root = doc.documentElement
445 root.appendChild(doc.createTextNode(""))
446 doc.normalize()
447 confirm(len(root.childNodes) == 0,
448 "testNormalize -- single empty node removed")
449 doc.unlink()
450
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000451def testSiblings():
452 doc = parseString("<doc><?pi?>text?<elm/></doc>")
453 root = doc.documentElement
454 (pi, text, elm) = root.childNodes
Paul Prescod7993bcc2000-07-01 14:54:16 +0000455
Fred Drake004d5e62000-10-23 17:22:08 +0000456 confirm(pi.nextSibling is text and
457 pi.previousSibling is None and
458 text.nextSibling is elm and
459 text.previousSibling is pi and
460 elm.nextSibling is None and
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000461 elm.previousSibling is text, "testSiblings")
462
463 doc.unlink()
464
465def testParents():
466 doc = parseString("<doc><elm1><elm2/><elm2><elm3/></elm2></elm1></doc>")
467 root = doc.documentElement
468 elm1 = root.childNodes[0]
469 (elm2a, elm2b) = elm1.childNodes
470 elm3 = elm2b.childNodes[0]
471
472 confirm(root.parentNode is doc and
473 elm1.parentNode is root and
474 elm2a.parentNode is elm1 and
475 elm2b.parentNode is elm1 and
476 elm3.parentNode is elm2b, "testParents")
477
478 doc.unlink()
479
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000480def testSAX2DOM():
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000481 from xml.dom import pulldom
482
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000483 sax2dom = pulldom.SAX2DOM()
484 sax2dom.startDocument()
485 sax2dom.startElement("doc", {})
486 sax2dom.characters("text")
487 sax2dom.startElement("subelm", {})
488 sax2dom.characters("text")
489 sax2dom.endElement("subelm")
Fred Drake004d5e62000-10-23 17:22:08 +0000490 sax2dom.characters("text")
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000491 sax2dom.endElement("doc")
492 sax2dom.endDocument()
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000493
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000494 doc = sax2dom.document
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000495 root = doc.documentElement
496 (text1, elm1, text2) = root.childNodes
497 text3 = elm1.childNodes[0]
498
499 confirm(text1.previousSibling is None and
500 text1.nextSibling is elm1 and
501 elm1.previousSibling is text1 and
502 elm1.nextSibling is text2 and
503 text2.previousSibling is elm1 and
504 text2.nextSibling is None and
505 text3.previousSibling is None and
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000506 text3.nextSibling is None, "testSAX2DOM - siblings")
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000507
508 confirm(root.parentNode is doc and
509 text1.parentNode is root and
510 elm1.parentNode is root and
511 text2.parentNode is root and
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000512 text3.parentNode is elm1, "testSAX2DOM - parents")
Fred Drake004d5e62000-10-23 17:22:08 +0000513
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000514 doc.unlink()
515
516# --- MAIN PROGRAM
Fred Drake004d5e62000-10-23 17:22:08 +0000517
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000518names = globals().keys()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000519names.sort()
Paul Prescod10d27662000-09-18 19:07:26 +0000520
Fred Drakeacfb3f62001-02-01 18:11:29 +0000521failed = []
Paul Prescod10d27662000-09-18 19:07:26 +0000522
Paul Prescod7993bcc2000-07-01 14:54:16 +0000523for name in names:
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000524 if name.startswith("test"):
525 func = globals()[name]
Paul Prescod7993bcc2000-07-01 14:54:16 +0000526 try:
527 func()
528 print "Test Succeeded", name
Fred Drakeebe73022000-10-09 19:57:39 +0000529 confirm(len(Node.allnodes) == 0,
530 "assertion: len(Node.allnodes) == 0")
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000531 if len(Node.allnodes):
Paul Prescod7993bcc2000-07-01 14:54:16 +0000532 print "Garbage left over:"
Martin v. Löwis89c528b2000-09-19 16:22:10 +0000533 if verbose:
534 print Node.allnodes.items()[0:10]
535 else:
536 # Don't print specific nodes if repeatable results
537 # are needed
538 print len(Node.allnodes)
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000539 Node.allnodes = {}
Fred Drakeacfb3f62001-02-01 18:11:29 +0000540 except:
541 failed.append(name)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000542 print "Test Failed: ", name
Fred Drake1703cf62000-12-15 21:31:59 +0000543 sys.stdout.flush()
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000544 traceback.print_exception(*sys.exc_info())
Fred Drakeacfb3f62001-02-01 18:11:29 +0000545 print `sys.exc_info()[1]`
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000546 Node.allnodes = {}
Paul Prescod10d27662000-09-18 19:07:26 +0000547
Fred Drakeacfb3f62001-02-01 18:11:29 +0000548if failed:
549 print "\n\n\n**** Check for failures in these tests:"
550 for name in failed:
551 print " " + name
552 print
Paul Prescod10d27662000-09-18 19:07:26 +0000553else:
Fred Drakeacfb3f62001-02-01 18:11:29 +0000554 print "All tests succeeded"
Paul Prescod7993bcc2000-07-01 14:54:16 +0000555
Guido van Rossum9e79a252000-09-21 20:10:39 +0000556Node.debug = None # Delete debug output collected in a StringIO object
557Node._debug = 0 # And reset debug mode