blob: 5ff1beb90b6f43071e8006adfc35523696fd0588 [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
242def testGetElementsByTagNameNS(): pass
243
244def testGetEmptyNodeListFromElementsByTagNameNS(): pass
245
246def testElementReprAndStr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000247 dom = Document()
248 el = dom.appendChild(dom.createElement("abc"))
249 string1 = repr(el)
250 string2 = str(el)
251 confirm(string1 == string2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000252 dom.unlink()
253
254# commented out until Fredrick's fix is checked in
255def _testElementReprAndStrUnicode():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000256 dom = Document()
257 el = dom.appendChild(dom.createElement(u"abc"))
258 string1 = repr(el)
259 string2 = str(el)
260 confirm(string1 == string2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000261 dom.unlink()
262
263# commented out until Fredrick's fix is checked in
264def _testElementReprAndStrUnicodeNS():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000265 dom = Document()
266 el = dom.appendChild(
267 dom.createElementNS(u"http://www.slashdot.org", u"slash:abc"))
268 string1 = repr(el)
269 string2 = str(el)
270 confirm(string1 == string2)
271 confirm(string1.find("slash:abc") != -1)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000272 dom.unlink()
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000273 confirm(len(Node.allnodes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000274
275def testAttributeRepr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000276 dom = Document()
277 el = dom.appendChild(dom.createElement(u"abc"))
278 node = el.setAttribute("abc", "def")
279 confirm(str(node) == repr(node))
Paul Prescod7993bcc2000-07-01 14:54:16 +0000280 dom.unlink()
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000281 confirm(len(Node.allnodes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000282
283def testTextNodeRepr(): pass
284
Martin v. Löwis0a84a332000-10-06 22:42:55 +0000285def testWriteXML():
Martin v. Löwisfe28ca02001-02-06 01:16:48 +0000286 str = '<?xml version="1.0" ?>\n<a b="c"/>'
Martin v. Löwis0a84a332000-10-06 22:42:55 +0000287 dom = parseString(str)
288 domstr = dom.toxml()
289 dom.unlink()
290 confirm(str == domstr)
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000291 confirm(len(Node.allnodes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000292
293def testProcessingInstruction(): pass
294
295def testProcessingInstructionRepr(): pass
296
297def testTextRepr(): pass
298
299def testWriteText(): pass
300
301def testDocumentElement(): pass
302
Fred Drakea1bde802000-11-21 22:02:43 +0000303def testTooManyDocumentElements():
304 doc = parseString("<doc/>")
305 elem = doc.createElement("extra")
306 try:
307 doc.appendChild(elem)
Martin v. Löwis2bcb3232001-01-27 09:17:55 +0000308 except HierarchyRequestErr:
Fred Drakea1bde802000-11-21 22:02:43 +0000309 print "Caught expected exception when adding extra document element."
310 else:
311 print "Failed to catch expected exception when" \
312 " adding extra document element."
313 elem.unlink()
314 doc.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000315
316def testCreateElementNS(): pass
317
Andrew M. Kuchlingad4a5582000-12-31 04:03:27 +0000318def testCreateAttributeNS(): pass
Paul Prescod7993bcc2000-07-01 14:54:16 +0000319
320def testParse(): pass
321
322def testParseString(): pass
323
324def testComment(): pass
325
326def testAttrListItem(): pass
327
328def testAttrListItems(): pass
329
330def testAttrListItemNS(): pass
331
332def testAttrListKeys(): pass
333
334def testAttrListKeysNS(): pass
335
336def testAttrListValues(): pass
337
338def testAttrListLength(): pass
339
340def testAttrList__getitem__(): pass
341
342def testAttrList__setitem__(): pass
343
344def testSetAttrValueandNodeValue(): pass
345
346def testParseElement(): pass
347
348def testParseAttributes(): pass
349
350def testParseElementNamespaces(): pass
351
352def testParseAttributeNamespaces(): pass
353
354def testParseProcessingInstructions(): pass
355
356def testChildNodes(): pass
357
358def testFirstChild(): pass
359
360def testHasChildNodes(): pass
361
Fred Drakea1bde802000-11-21 22:02:43 +0000362def testCloneElementShallow():
363 dom, clone = _setupCloneElement(0)
364 confirm(len(clone.childNodes) == 0
365 and clone.parentNode is None
366 and clone.toxml() == '<doc attr="value"/>'
367 , "testCloneElementShallow")
368 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000369
Fred Drakea1bde802000-11-21 22:02:43 +0000370def testCloneElementDeep():
371 dom, clone = _setupCloneElement(1)
372 confirm(len(clone.childNodes) == 1
373 and clone.parentNode is None
374 and clone.toxml() == '<doc attr="value"><foo/></doc>'
375 , "testCloneElementDeep")
376 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000377
Fred Drakea1bde802000-11-21 22:02:43 +0000378def _setupCloneElement(deep):
379 dom = parseString("<doc attr='value'><foo/></doc>")
380 root = dom.documentElement
381 clone = root.cloneNode(deep)
382 _testCloneElementCopiesAttributes(
383 root, clone, "testCloneElement" + (deep and "Deep" or "Shallow"))
384 # mutilate the original so shared data is detected
385 root.tagName = root.nodeName = "MODIFIED"
386 root.setAttribute("attr", "NEW VALUE")
387 root.setAttribute("added", "VALUE")
388 return dom, clone
389
390def _testCloneElementCopiesAttributes(e1, e2, test):
391 attrs1 = e1.attributes
392 attrs2 = e2.attributes
393 keys1 = attrs1.keys()
394 keys2 = attrs2.keys()
395 keys1.sort()
396 keys2.sort()
397 confirm(keys1 == keys2, "clone of element has same attribute keys")
398 for i in range(len(keys1)):
399 a1 = attrs1.item(i)
400 a2 = attrs2.item(i)
401 confirm(a1 is not a2
402 and a1.value == a2.value
403 and a1.nodeValue == a2.nodeValue
404 and a1.namespaceURI == a2.namespaceURI
405 and a1.localName == a2.localName
406 , "clone of attribute node has proper attribute values")
407 confirm(a2.ownerElement is e2,
408 "clone of attribute node correctly owned")
Fredrik Lundhf7850422001-01-17 21:51:36 +0000409
Paul Prescod7993bcc2000-07-01 14:54:16 +0000410
411def testCloneDocumentShallow(): pass
412
413def testCloneDocumentDeep(): pass
414
415def testCloneAttributeShallow(): pass
416
417def testCloneAttributeDeep(): pass
418
419def testClonePIShallow(): pass
420
421def testClonePIDeep(): pass
422
Fred Drakea1bde802000-11-21 22:02:43 +0000423def testNormalize():
424 doc = parseString("<doc/>")
425 root = doc.documentElement
426 root.appendChild(doc.createTextNode("first"))
427 root.appendChild(doc.createTextNode("second"))
428 confirm(len(root.childNodes) == 2, "testNormalize -- preparation")
429 doc.normalize()
430 confirm(len(root.childNodes) == 1
431 and root.firstChild is root.lastChild
432 and root.firstChild.data == "firstsecond"
433 , "testNormalize -- result")
434 doc.unlink()
435
Fred Drake3277da02000-12-14 18:20:22 +0000436 doc = parseString("<doc/>")
437 root = doc.documentElement
438 root.appendChild(doc.createTextNode(""))
439 doc.normalize()
440 confirm(len(root.childNodes) == 0,
441 "testNormalize -- single empty node removed")
442 doc.unlink()
443
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000444def testSiblings():
445 doc = parseString("<doc><?pi?>text?<elm/></doc>")
446 root = doc.documentElement
447 (pi, text, elm) = root.childNodes
Paul Prescod7993bcc2000-07-01 14:54:16 +0000448
Fred Drake004d5e62000-10-23 17:22:08 +0000449 confirm(pi.nextSibling is text and
450 pi.previousSibling is None and
451 text.nextSibling is elm and
452 text.previousSibling is pi and
453 elm.nextSibling is None and
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000454 elm.previousSibling is text, "testSiblings")
455
456 doc.unlink()
457
458def testParents():
459 doc = parseString("<doc><elm1><elm2/><elm2><elm3/></elm2></elm1></doc>")
460 root = doc.documentElement
461 elm1 = root.childNodes[0]
462 (elm2a, elm2b) = elm1.childNodes
463 elm3 = elm2b.childNodes[0]
464
465 confirm(root.parentNode is doc and
466 elm1.parentNode is root and
467 elm2a.parentNode is elm1 and
468 elm2b.parentNode is elm1 and
469 elm3.parentNode is elm2b, "testParents")
470
471 doc.unlink()
472
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000473def testSAX2DOM():
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000474 from xml.dom import pulldom
475
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000476 sax2dom = pulldom.SAX2DOM()
477 sax2dom.startDocument()
478 sax2dom.startElement("doc", {})
479 sax2dom.characters("text")
480 sax2dom.startElement("subelm", {})
481 sax2dom.characters("text")
482 sax2dom.endElement("subelm")
Fred Drake004d5e62000-10-23 17:22:08 +0000483 sax2dom.characters("text")
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000484 sax2dom.endElement("doc")
485 sax2dom.endDocument()
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000486
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000487 doc = sax2dom.document
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000488 root = doc.documentElement
489 (text1, elm1, text2) = root.childNodes
490 text3 = elm1.childNodes[0]
491
492 confirm(text1.previousSibling is None and
493 text1.nextSibling is elm1 and
494 elm1.previousSibling is text1 and
495 elm1.nextSibling is text2 and
496 text2.previousSibling is elm1 and
497 text2.nextSibling is None and
498 text3.previousSibling is None and
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000499 text3.nextSibling is None, "testSAX2DOM - siblings")
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000500
501 confirm(root.parentNode is doc and
502 text1.parentNode is root and
503 elm1.parentNode is root and
504 text2.parentNode is root and
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000505 text3.parentNode is elm1, "testSAX2DOM - parents")
Fred Drake004d5e62000-10-23 17:22:08 +0000506
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000507 doc.unlink()
508
509# --- MAIN PROGRAM
Fred Drake004d5e62000-10-23 17:22:08 +0000510
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000511names = globals().keys()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000512names.sort()
Paul Prescod10d27662000-09-18 19:07:26 +0000513
Fred Drakeacfb3f62001-02-01 18:11:29 +0000514failed = []
Paul Prescod10d27662000-09-18 19:07:26 +0000515
Paul Prescod7993bcc2000-07-01 14:54:16 +0000516for name in names:
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000517 if name.startswith("test"):
518 func = globals()[name]
Paul Prescod7993bcc2000-07-01 14:54:16 +0000519 try:
520 func()
521 print "Test Succeeded", name
Fred Drakeebe73022000-10-09 19:57:39 +0000522 confirm(len(Node.allnodes) == 0,
523 "assertion: len(Node.allnodes) == 0")
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000524 if len(Node.allnodes):
Paul Prescod7993bcc2000-07-01 14:54:16 +0000525 print "Garbage left over:"
Martin v. Löwis89c528b2000-09-19 16:22:10 +0000526 if verbose:
527 print Node.allnodes.items()[0:10]
528 else:
529 # Don't print specific nodes if repeatable results
530 # are needed
531 print len(Node.allnodes)
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000532 Node.allnodes = {}
Fred Drakeacfb3f62001-02-01 18:11:29 +0000533 except:
534 failed.append(name)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000535 print "Test Failed: ", name
Fred Drake1703cf62000-12-15 21:31:59 +0000536 sys.stdout.flush()
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000537 traceback.print_exception(*sys.exc_info())
Fred Drakeacfb3f62001-02-01 18:11:29 +0000538 print `sys.exc_info()[1]`
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000539 Node.allnodes = {}
Paul Prescod10d27662000-09-18 19:07:26 +0000540
Fred Drakeacfb3f62001-02-01 18:11:29 +0000541if failed:
542 print "\n\n\n**** Check for failures in these tests:"
543 for name in failed:
544 print " " + name
545 print
Paul Prescod10d27662000-09-18 19:07:26 +0000546else:
Fred Drakeacfb3f62001-02-01 18:11:29 +0000547 print "All tests succeeded"
Paul Prescod7993bcc2000-07-01 14:54:16 +0000548
Guido van Rossum9e79a252000-09-21 20:10:39 +0000549Node.debug = None # Delete debug output collected in a StringIO object
550Node._debug = 0 # And reset debug mode