blob: 002b03670cbfaf9d3fccb330e895bc46b200d42b [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
101 elem.appendChild(text)
Fredrik Lundhf7850422001-01-17 21:51:36 +0000102 dom.unlink()
Andrew M. Kuchlingad4a5582000-12-31 04:03:27 +0000103
Paul Prescod7993bcc2000-07-01 14:54:16 +0000104def testNonZero():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000105 dom = parse(tstfile)
106 confirm(dom)# should not be zero
107 dom.appendChild(dom.createComment("foo"))
108 confirm(not dom.childNodes[-1].childNodes)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000109 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000110
111def testUnlink():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000112 dom = parse(tstfile)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000113 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000114
115def testElement():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000116 dom = Document()
117 dom.appendChild(dom.createElement("abc"))
118 confirm(dom.documentElement)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000119 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000120
121def testAAA():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000122 dom = parseString("<abc/>")
123 el = dom.documentElement
124 el.setAttribute("spam", "jam2")
Fred Drakea1bde802000-11-21 22:02:43 +0000125 confirm(el.toxml() == '<abc spam="jam2"/>', "testAAA")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000126 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000127
128def testAAB():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000129 dom = parseString("<abc/>")
130 el = dom.documentElement
131 el.setAttribute("spam", "jam")
132 el.setAttribute("spam", "jam2")
Fred Drakea1bde802000-11-21 22:02:43 +0000133 confirm(el.toxml() == '<abc spam="jam2"/>', "testAAB")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000134 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000135
136def testAddAttr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000137 dom = Document()
138 child = dom.appendChild(dom.createElement("abc"))
Paul Prescod7993bcc2000-07-01 14:54:16 +0000139
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000140 child.setAttribute("def", "ghi")
141 confirm(child.getAttribute("def") == "ghi")
142 confirm(child.attributes["def"].value == "ghi")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000143
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000144 child.setAttribute("jkl", "mno")
145 confirm(child.getAttribute("jkl") == "mno")
146 confirm(child.attributes["jkl"].value == "mno")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000147
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000148 confirm(len(child.attributes) == 2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000149
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000150 child.setAttribute("def", "newval")
151 confirm(child.getAttribute("def") == "newval")
152 confirm(child.attributes["def"].value == "newval")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000153
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000154 confirm(len(child.attributes) == 2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000155 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000156
157def testDeleteAttr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000158 dom = Document()
159 child = dom.appendChild(dom.createElement("abc"))
Paul Prescod7993bcc2000-07-01 14:54:16 +0000160
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000161 confirm(len(child.attributes) == 0)
162 child.setAttribute("def", "ghi")
163 confirm(len(child.attributes) == 1)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000164 del child.attributes["def"]
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000165 confirm(len(child.attributes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000166 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000167
168def testRemoveAttr():
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 child.setAttribute("def", "ghi")
173 confirm(len(child.attributes) == 1)
174 child.removeAttribute("def")
175 confirm(len(child.attributes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000176
177 dom.unlink()
178
179def testRemoveAttrNS():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000180 dom = Document()
181 child = dom.appendChild(
182 dom.createElementNS("http://www.python.org", "python:abc"))
Fred Drake004d5e62000-10-23 17:22:08 +0000183 child.setAttributeNS("http://www.w3.org", "xmlns:python",
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000184 "http://www.python.org")
185 child.setAttributeNS("http://www.python.org", "python:abcattr", "foo")
186 confirm(len(child.attributes) == 2)
187 child.removeAttributeNS("http://www.python.org", "abcattr")
188 confirm(len(child.attributes) == 1)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000189
190 dom.unlink()
Fred Drake004d5e62000-10-23 17:22:08 +0000191
Paul Prescod7993bcc2000-07-01 14:54:16 +0000192def testRemoveAttributeNode():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000193 dom = Document()
194 child = dom.appendChild(dom.createElement("foo"))
195 child.setAttribute("spam", "jam")
196 confirm(len(child.attributes) == 1)
197 node = child.getAttributeNode("spam")
198 child.removeAttributeNode(node)
199 confirm(len(child.attributes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000200
201 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000202
203def testChangeAttr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000204 dom = parseString("<abc/>")
205 el = dom.documentElement
206 el.setAttribute("spam", "jam")
207 confirm(len(el.attributes) == 1)
208 el.setAttribute("spam", "bam")
209 confirm(len(el.attributes) == 1)
210 el.attributes["spam"] = "ham"
211 confirm(len(el.attributes) == 1)
212 el.setAttribute("spam2", "bam")
213 confirm(len(el.attributes) == 2)
214 el.attributes[ "spam2"] = "bam2"
215 confirm(len(el.attributes) == 2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000216 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000217
218def testGetAttrList():
219 pass
220
221def testGetAttrValues(): pass
222
223def testGetAttrLength(): pass
224
225def testGetAttribute(): pass
226
227def testGetAttributeNS(): pass
228
229def testGetAttributeNode(): pass
230
231def testGetElementsByTagNameNS(): pass
232
233def testGetEmptyNodeListFromElementsByTagNameNS(): pass
234
235def testElementReprAndStr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000236 dom = Document()
237 el = dom.appendChild(dom.createElement("abc"))
238 string1 = repr(el)
239 string2 = str(el)
240 confirm(string1 == string2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000241 dom.unlink()
242
243# commented out until Fredrick's fix is checked in
244def _testElementReprAndStrUnicode():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000245 dom = Document()
246 el = dom.appendChild(dom.createElement(u"abc"))
247 string1 = repr(el)
248 string2 = str(el)
249 confirm(string1 == string2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000250 dom.unlink()
251
252# commented out until Fredrick's fix is checked in
253def _testElementReprAndStrUnicodeNS():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000254 dom = Document()
255 el = dom.appendChild(
256 dom.createElementNS(u"http://www.slashdot.org", u"slash:abc"))
257 string1 = repr(el)
258 string2 = str(el)
259 confirm(string1 == string2)
260 confirm(string1.find("slash:abc") != -1)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000261 dom.unlink()
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000262 confirm(len(Node.allnodes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000263
264def testAttributeRepr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000265 dom = Document()
266 el = dom.appendChild(dom.createElement(u"abc"))
267 node = el.setAttribute("abc", "def")
268 confirm(str(node) == repr(node))
Paul Prescod7993bcc2000-07-01 14:54:16 +0000269 dom.unlink()
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000270 confirm(len(Node.allnodes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000271
272def testTextNodeRepr(): pass
273
Martin v. Löwis0a84a332000-10-06 22:42:55 +0000274def testWriteXML():
275 str = '<a b="c"/>'
276 dom = parseString(str)
277 domstr = dom.toxml()
278 dom.unlink()
279 confirm(str == domstr)
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000280 confirm(len(Node.allnodes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000281
282def testProcessingInstruction(): pass
283
284def testProcessingInstructionRepr(): pass
285
286def testTextRepr(): pass
287
288def testWriteText(): pass
289
290def testDocumentElement(): pass
291
Fred Drakea1bde802000-11-21 22:02:43 +0000292def testTooManyDocumentElements():
293 doc = parseString("<doc/>")
294 elem = doc.createElement("extra")
295 try:
296 doc.appendChild(elem)
Martin v. Löwis2bcb3232001-01-27 09:17:55 +0000297 except HierarchyRequestErr:
Fred Drakea1bde802000-11-21 22:02:43 +0000298 print "Caught expected exception when adding extra document element."
299 else:
300 print "Failed to catch expected exception when" \
301 " adding extra document element."
302 elem.unlink()
303 doc.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000304
305def testCreateElementNS(): pass
306
Andrew M. Kuchlingad4a5582000-12-31 04:03:27 +0000307def testCreateAttributeNS(): pass
Paul Prescod7993bcc2000-07-01 14:54:16 +0000308
309def testParse(): pass
310
311def testParseString(): pass
312
313def testComment(): pass
314
315def testAttrListItem(): pass
316
317def testAttrListItems(): pass
318
319def testAttrListItemNS(): pass
320
321def testAttrListKeys(): pass
322
323def testAttrListKeysNS(): pass
324
325def testAttrListValues(): pass
326
327def testAttrListLength(): pass
328
329def testAttrList__getitem__(): pass
330
331def testAttrList__setitem__(): pass
332
333def testSetAttrValueandNodeValue(): pass
334
335def testParseElement(): pass
336
337def testParseAttributes(): pass
338
339def testParseElementNamespaces(): pass
340
341def testParseAttributeNamespaces(): pass
342
343def testParseProcessingInstructions(): pass
344
345def testChildNodes(): pass
346
347def testFirstChild(): pass
348
349def testHasChildNodes(): pass
350
Fred Drakea1bde802000-11-21 22:02:43 +0000351def testCloneElementShallow():
352 dom, clone = _setupCloneElement(0)
353 confirm(len(clone.childNodes) == 0
354 and clone.parentNode is None
355 and clone.toxml() == '<doc attr="value"/>'
356 , "testCloneElementShallow")
357 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000358
Fred Drakea1bde802000-11-21 22:02:43 +0000359def testCloneElementDeep():
360 dom, clone = _setupCloneElement(1)
361 confirm(len(clone.childNodes) == 1
362 and clone.parentNode is None
363 and clone.toxml() == '<doc attr="value"><foo/></doc>'
364 , "testCloneElementDeep")
365 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000366
Fred Drakea1bde802000-11-21 22:02:43 +0000367def _setupCloneElement(deep):
368 dom = parseString("<doc attr='value'><foo/></doc>")
369 root = dom.documentElement
370 clone = root.cloneNode(deep)
371 _testCloneElementCopiesAttributes(
372 root, clone, "testCloneElement" + (deep and "Deep" or "Shallow"))
373 # mutilate the original so shared data is detected
374 root.tagName = root.nodeName = "MODIFIED"
375 root.setAttribute("attr", "NEW VALUE")
376 root.setAttribute("added", "VALUE")
377 return dom, clone
378
379def _testCloneElementCopiesAttributes(e1, e2, test):
380 attrs1 = e1.attributes
381 attrs2 = e2.attributes
382 keys1 = attrs1.keys()
383 keys2 = attrs2.keys()
384 keys1.sort()
385 keys2.sort()
386 confirm(keys1 == keys2, "clone of element has same attribute keys")
387 for i in range(len(keys1)):
388 a1 = attrs1.item(i)
389 a2 = attrs2.item(i)
390 confirm(a1 is not a2
391 and a1.value == a2.value
392 and a1.nodeValue == a2.nodeValue
393 and a1.namespaceURI == a2.namespaceURI
394 and a1.localName == a2.localName
395 , "clone of attribute node has proper attribute values")
396 confirm(a2.ownerElement is e2,
397 "clone of attribute node correctly owned")
Fredrik Lundhf7850422001-01-17 21:51:36 +0000398
Paul Prescod7993bcc2000-07-01 14:54:16 +0000399
400def testCloneDocumentShallow(): pass
401
402def testCloneDocumentDeep(): pass
403
404def testCloneAttributeShallow(): pass
405
406def testCloneAttributeDeep(): pass
407
408def testClonePIShallow(): pass
409
410def testClonePIDeep(): pass
411
Fred Drakea1bde802000-11-21 22:02:43 +0000412def testNormalize():
413 doc = parseString("<doc/>")
414 root = doc.documentElement
415 root.appendChild(doc.createTextNode("first"))
416 root.appendChild(doc.createTextNode("second"))
417 confirm(len(root.childNodes) == 2, "testNormalize -- preparation")
418 doc.normalize()
419 confirm(len(root.childNodes) == 1
420 and root.firstChild is root.lastChild
421 and root.firstChild.data == "firstsecond"
422 , "testNormalize -- result")
423 doc.unlink()
424
Fred Drake3277da02000-12-14 18:20:22 +0000425 doc = parseString("<doc/>")
426 root = doc.documentElement
427 root.appendChild(doc.createTextNode(""))
428 doc.normalize()
429 confirm(len(root.childNodes) == 0,
430 "testNormalize -- single empty node removed")
431 doc.unlink()
432
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000433def testSiblings():
434 doc = parseString("<doc><?pi?>text?<elm/></doc>")
435 root = doc.documentElement
436 (pi, text, elm) = root.childNodes
Paul Prescod7993bcc2000-07-01 14:54:16 +0000437
Fred Drake004d5e62000-10-23 17:22:08 +0000438 confirm(pi.nextSibling is text and
439 pi.previousSibling is None and
440 text.nextSibling is elm and
441 text.previousSibling is pi and
442 elm.nextSibling is None and
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000443 elm.previousSibling is text, "testSiblings")
444
445 doc.unlink()
446
447def testParents():
448 doc = parseString("<doc><elm1><elm2/><elm2><elm3/></elm2></elm1></doc>")
449 root = doc.documentElement
450 elm1 = root.childNodes[0]
451 (elm2a, elm2b) = elm1.childNodes
452 elm3 = elm2b.childNodes[0]
453
454 confirm(root.parentNode is doc and
455 elm1.parentNode is root and
456 elm2a.parentNode is elm1 and
457 elm2b.parentNode is elm1 and
458 elm3.parentNode is elm2b, "testParents")
459
460 doc.unlink()
461
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000462def testSAX2DOM():
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000463 from xml.dom import pulldom
464
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000465 sax2dom = pulldom.SAX2DOM()
466 sax2dom.startDocument()
467 sax2dom.startElement("doc", {})
468 sax2dom.characters("text")
469 sax2dom.startElement("subelm", {})
470 sax2dom.characters("text")
471 sax2dom.endElement("subelm")
Fred Drake004d5e62000-10-23 17:22:08 +0000472 sax2dom.characters("text")
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000473 sax2dom.endElement("doc")
474 sax2dom.endDocument()
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000475
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000476 doc = sax2dom.document
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000477 root = doc.documentElement
478 (text1, elm1, text2) = root.childNodes
479 text3 = elm1.childNodes[0]
480
481 confirm(text1.previousSibling is None and
482 text1.nextSibling is elm1 and
483 elm1.previousSibling is text1 and
484 elm1.nextSibling is text2 and
485 text2.previousSibling is elm1 and
486 text2.nextSibling is None and
487 text3.previousSibling is None and
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000488 text3.nextSibling is None, "testSAX2DOM - siblings")
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000489
490 confirm(root.parentNode is doc and
491 text1.parentNode is root and
492 elm1.parentNode is root and
493 text2.parentNode is root and
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000494 text3.parentNode is elm1, "testSAX2DOM - parents")
Fred Drake004d5e62000-10-23 17:22:08 +0000495
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000496 doc.unlink()
497
498# --- MAIN PROGRAM
Fred Drake004d5e62000-10-23 17:22:08 +0000499
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000500names = globals().keys()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000501names.sort()
Paul Prescod10d27662000-09-18 19:07:26 +0000502
Fred Drakeacfb3f62001-02-01 18:11:29 +0000503failed = []
Paul Prescod10d27662000-09-18 19:07:26 +0000504
Paul Prescod7993bcc2000-07-01 14:54:16 +0000505for name in names:
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000506 if name.startswith("test"):
507 func = globals()[name]
Paul Prescod7993bcc2000-07-01 14:54:16 +0000508 try:
509 func()
510 print "Test Succeeded", name
Fred Drakeebe73022000-10-09 19:57:39 +0000511 confirm(len(Node.allnodes) == 0,
512 "assertion: len(Node.allnodes) == 0")
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000513 if len(Node.allnodes):
Paul Prescod7993bcc2000-07-01 14:54:16 +0000514 print "Garbage left over:"
Martin v. Löwis89c528b2000-09-19 16:22:10 +0000515 if verbose:
516 print Node.allnodes.items()[0:10]
517 else:
518 # Don't print specific nodes if repeatable results
519 # are needed
520 print len(Node.allnodes)
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000521 Node.allnodes = {}
Fred Drakeacfb3f62001-02-01 18:11:29 +0000522 except:
523 failed.append(name)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000524 print "Test Failed: ", name
Fred Drake1703cf62000-12-15 21:31:59 +0000525 sys.stdout.flush()
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000526 traceback.print_exception(*sys.exc_info())
Fred Drakeacfb3f62001-02-01 18:11:29 +0000527 print `sys.exc_info()[1]`
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000528 Node.allnodes = {}
Paul Prescod10d27662000-09-18 19:07:26 +0000529
Fred Drakeacfb3f62001-02-01 18:11:29 +0000530if failed:
531 print "\n\n\n**** Check for failures in these tests:"
532 for name in failed:
533 print " " + name
534 print
Paul Prescod10d27662000-09-18 19:07:26 +0000535else:
Fred Drakeacfb3f62001-02-01 18:11:29 +0000536 print "All tests succeeded"
Paul Prescod7993bcc2000-07-01 14:54:16 +0000537
Guido van Rossum9e79a252000-09-21 20:10:39 +0000538Node.debug = None # Delete debug output collected in a StringIO object
539Node._debug = 0 # And reset debug mode