blob: 2a290aa4c8ab3dada182b8594c1721ac622316fa [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
Fred Drake946f7b12001-09-28 20:31:50 +000047 and root.childNodes.length == 2
Fred Drakea1bde802000-11-21 22:02:43 +000048 and root.childNodes[0] is nelem
Fred Drake946f7b12001-09-28 20:31:50 +000049 and root.childNodes.item(0) is nelem
Fred Drakea1bde802000-11-21 22:02:43 +000050 and root.childNodes[1] is elem
Fred Drake946f7b12001-09-28 20:31:50 +000051 and root.childNodes.item(1) is elem
Fred Drakea1bde802000-11-21 22:02:43 +000052 and root.firstChild is nelem
53 and root.lastChild is elem
54 and root.toxml() == "<doc><element/><foo/></doc>"
55 , "testInsertBefore -- node properly placed in tree")
56 nelem = dom.createElement("element")
57 root.insertBefore(nelem, None)
58 confirm(len(root.childNodes) == 3
Fred Drake946f7b12001-09-28 20:31:50 +000059 and root.childNodes.length == 3
Fred Drakea1bde802000-11-21 22:02:43 +000060 and root.childNodes[1] is elem
Fred Drake946f7b12001-09-28 20:31:50 +000061 and root.childNodes.item(1) is elem
Fred Drakea1bde802000-11-21 22:02:43 +000062 and root.childNodes[2] is nelem
Fred Drake946f7b12001-09-28 20:31:50 +000063 and root.childNodes.item(2) is nelem
Fred Drakea1bde802000-11-21 22:02:43 +000064 and root.lastChild is nelem
65 and nelem.previousSibling is elem
66 and root.toxml() == "<doc><element/><foo/><element/></doc>"
67 , "testInsertBefore -- node properly placed in tree")
68 nelem2 = dom.createElement("bar")
69 root.insertBefore(nelem2, nelem)
70 confirm(len(root.childNodes) == 4
Fred Drake946f7b12001-09-28 20:31:50 +000071 and root.childNodes.length == 4
Fred Drakea1bde802000-11-21 22:02:43 +000072 and root.childNodes[2] is nelem2
Fred Drake946f7b12001-09-28 20:31:50 +000073 and root.childNodes.item(2) is nelem2
Fred Drakea1bde802000-11-21 22:02:43 +000074 and root.childNodes[3] is nelem
Fred Drake946f7b12001-09-28 20:31:50 +000075 and root.childNodes.item(3) is nelem
Fred Drakea1bde802000-11-21 22:02:43 +000076 and nelem2.nextSibling is nelem
77 and nelem.previousSibling is nelem2
78 and root.toxml() == "<doc><element/><foo/><bar/><element/></doc>"
79 , "testInsertBefore -- node properly placed in tree")
Paul Prescod7993bcc2000-07-01 14:54:16 +000080 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +000081
82def testAppendChild():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +000083 dom = parse(tstfile)
84 dom.documentElement.appendChild(dom.createComment(u"Hello"))
85 confirm(dom.documentElement.childNodes[-1].nodeName == "#comment")
86 confirm(dom.documentElement.childNodes[-1].data == "Hello")
Paul Prescod7993bcc2000-07-01 14:54:16 +000087 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +000088
Andrew M. Kuchlingad4a5582000-12-31 04:03:27 +000089def testLegalChildren():
90 dom = Document()
91 elem = dom.createElement('element')
92 text = dom.createTextNode('text')
Fredrik Lundhf7850422001-01-17 21:51:36 +000093
Andrew M. Kuchlingad4a5582000-12-31 04:03:27 +000094 try: dom.appendChild(text)
95 except HierarchyRequestErr: pass
96 else:
97 print "dom.appendChild didn't raise HierarchyRequestErr"
98
99 dom.appendChild(elem)
100 try: dom.insertBefore(text, elem)
101 except HierarchyRequestErr: pass
102 else:
103 print "dom.appendChild didn't raise HierarchyRequestErr"
104
105 try: dom.replaceChild(text, elem)
106 except HierarchyRequestErr: pass
107 else:
108 print "dom.appendChild didn't raise HierarchyRequestErr"
109
Tim Peters0009c4e2001-02-21 07:29:48 +0000110 nodemap = elem.attributes
Andrew M. Kuchlingbc8f72c2001-02-21 01:30:26 +0000111 try: nodemap.setNamedItem(text)
112 except HierarchyRequestErr: pass
113 else:
114 print "NamedNodeMap.setNamedItem didn't raise HierarchyRequestErr"
115
116 try: nodemap.setNamedItemNS(text)
117 except HierarchyRequestErr: pass
118 else:
119 print "NamedNodeMap.setNamedItemNS didn't raise HierarchyRequestErr"
120
Andrew M. Kuchlingad4a5582000-12-31 04:03:27 +0000121 elem.appendChild(text)
Fredrik Lundhf7850422001-01-17 21:51:36 +0000122 dom.unlink()
Andrew M. Kuchlingad4a5582000-12-31 04:03:27 +0000123
Paul Prescod7993bcc2000-07-01 14:54:16 +0000124def testNonZero():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000125 dom = parse(tstfile)
126 confirm(dom)# should not be zero
127 dom.appendChild(dom.createComment("foo"))
128 confirm(not dom.childNodes[-1].childNodes)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000129 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000130
131def testUnlink():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000132 dom = parse(tstfile)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000133 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000134
135def testElement():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000136 dom = Document()
137 dom.appendChild(dom.createElement("abc"))
138 confirm(dom.documentElement)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000139 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000140
141def testAAA():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000142 dom = parseString("<abc/>")
143 el = dom.documentElement
144 el.setAttribute("spam", "jam2")
Fred Drakea1bde802000-11-21 22:02:43 +0000145 confirm(el.toxml() == '<abc spam="jam2"/>', "testAAA")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000146 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000147
148def testAAB():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000149 dom = parseString("<abc/>")
150 el = dom.documentElement
151 el.setAttribute("spam", "jam")
152 el.setAttribute("spam", "jam2")
Fred Drakea1bde802000-11-21 22:02:43 +0000153 confirm(el.toxml() == '<abc spam="jam2"/>', "testAAB")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000154 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000155
156def testAddAttr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000157 dom = Document()
158 child = dom.appendChild(dom.createElement("abc"))
Paul Prescod7993bcc2000-07-01 14:54:16 +0000159
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000160 child.setAttribute("def", "ghi")
161 confirm(child.getAttribute("def") == "ghi")
162 confirm(child.attributes["def"].value == "ghi")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000163
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000164 child.setAttribute("jkl", "mno")
165 confirm(child.getAttribute("jkl") == "mno")
166 confirm(child.attributes["jkl"].value == "mno")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000167
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000168 confirm(len(child.attributes) == 2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000169
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000170 child.setAttribute("def", "newval")
171 confirm(child.getAttribute("def") == "newval")
172 confirm(child.attributes["def"].value == "newval")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000173
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000174 confirm(len(child.attributes) == 2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000175 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000176
177def testDeleteAttr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000178 dom = Document()
179 child = dom.appendChild(dom.createElement("abc"))
Paul Prescod7993bcc2000-07-01 14:54:16 +0000180
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000181 confirm(len(child.attributes) == 0)
182 child.setAttribute("def", "ghi")
183 confirm(len(child.attributes) == 1)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000184 del child.attributes["def"]
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000185 confirm(len(child.attributes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000186 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000187
188def testRemoveAttr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000189 dom = Document()
190 child = dom.appendChild(dom.createElement("abc"))
Paul Prescod7993bcc2000-07-01 14:54:16 +0000191
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000192 child.setAttribute("def", "ghi")
193 confirm(len(child.attributes) == 1)
194 child.removeAttribute("def")
195 confirm(len(child.attributes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000196
197 dom.unlink()
198
199def testRemoveAttrNS():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000200 dom = Document()
201 child = dom.appendChild(
202 dom.createElementNS("http://www.python.org", "python:abc"))
Fred Drake004d5e62000-10-23 17:22:08 +0000203 child.setAttributeNS("http://www.w3.org", "xmlns:python",
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000204 "http://www.python.org")
205 child.setAttributeNS("http://www.python.org", "python:abcattr", "foo")
206 confirm(len(child.attributes) == 2)
207 child.removeAttributeNS("http://www.python.org", "abcattr")
208 confirm(len(child.attributes) == 1)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000209
210 dom.unlink()
Fred Drake004d5e62000-10-23 17:22:08 +0000211
Paul Prescod7993bcc2000-07-01 14:54:16 +0000212def testRemoveAttributeNode():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000213 dom = Document()
214 child = dom.appendChild(dom.createElement("foo"))
215 child.setAttribute("spam", "jam")
216 confirm(len(child.attributes) == 1)
217 node = child.getAttributeNode("spam")
218 child.removeAttributeNode(node)
219 confirm(len(child.attributes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000220
221 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000222
223def testChangeAttr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000224 dom = parseString("<abc/>")
225 el = dom.documentElement
226 el.setAttribute("spam", "jam")
227 confirm(len(el.attributes) == 1)
228 el.setAttribute("spam", "bam")
229 confirm(len(el.attributes) == 1)
230 el.attributes["spam"] = "ham"
231 confirm(len(el.attributes) == 1)
232 el.setAttribute("spam2", "bam")
233 confirm(len(el.attributes) == 2)
234 el.attributes[ "spam2"] = "bam2"
235 confirm(len(el.attributes) == 2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000236 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000237
238def testGetAttrList():
239 pass
240
241def testGetAttrValues(): pass
242
243def testGetAttrLength(): pass
244
245def testGetAttribute(): pass
246
247def testGetAttributeNS(): pass
248
249def testGetAttributeNode(): pass
250
Martin v. Löwis351c3d02001-06-03 14:27:02 +0000251def testGetElementsByTagNameNS():
252 d="""<foo xmlns:minidom="http://pyxml.sf.net/minidom">
253 <minidom:myelem/>
254 </foo>"""
255 dom = parseString(d)
256 elem = dom.getElementsByTagNameNS("http://pyxml.sf.net/minidom","myelem")
257 confirm(len(elem) == 1)
258 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000259
260def testGetEmptyNodeListFromElementsByTagNameNS(): pass
261
262def testElementReprAndStr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000263 dom = Document()
264 el = dom.appendChild(dom.createElement("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 _testElementReprAndStrUnicode():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000272 dom = Document()
273 el = dom.appendChild(dom.createElement(u"abc"))
274 string1 = repr(el)
275 string2 = str(el)
276 confirm(string1 == string2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000277 dom.unlink()
278
279# commented out until Fredrick's fix is checked in
280def _testElementReprAndStrUnicodeNS():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000281 dom = Document()
282 el = dom.appendChild(
283 dom.createElementNS(u"http://www.slashdot.org", u"slash:abc"))
284 string1 = repr(el)
285 string2 = str(el)
286 confirm(string1 == string2)
287 confirm(string1.find("slash:abc") != -1)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000288 dom.unlink()
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000289 confirm(len(Node.allnodes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000290
291def testAttributeRepr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000292 dom = Document()
293 el = dom.appendChild(dom.createElement(u"abc"))
294 node = el.setAttribute("abc", "def")
295 confirm(str(node) == repr(node))
Paul Prescod7993bcc2000-07-01 14:54:16 +0000296 dom.unlink()
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000297 confirm(len(Node.allnodes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000298
299def testTextNodeRepr(): pass
300
Martin v. Löwis0a84a332000-10-06 22:42:55 +0000301def testWriteXML():
Martin v. Löwisfe28ca02001-02-06 01:16:48 +0000302 str = '<?xml version="1.0" ?>\n<a b="c"/>'
Martin v. Löwis0a84a332000-10-06 22:42:55 +0000303 dom = parseString(str)
304 domstr = dom.toxml()
305 dom.unlink()
306 confirm(str == domstr)
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000307 confirm(len(Node.allnodes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000308
309def testProcessingInstruction(): pass
310
311def testProcessingInstructionRepr(): pass
312
313def testTextRepr(): pass
314
315def testWriteText(): pass
316
317def testDocumentElement(): pass
318
Fred Drakea1bde802000-11-21 22:02:43 +0000319def testTooManyDocumentElements():
320 doc = parseString("<doc/>")
321 elem = doc.createElement("extra")
322 try:
323 doc.appendChild(elem)
Martin v. Löwis2bcb3232001-01-27 09:17:55 +0000324 except HierarchyRequestErr:
Fred Drakea1bde802000-11-21 22:02:43 +0000325 print "Caught expected exception when adding extra document element."
326 else:
327 print "Failed to catch expected exception when" \
328 " adding extra document element."
329 elem.unlink()
330 doc.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000331
332def testCreateElementNS(): pass
333
Andrew M. Kuchlingad4a5582000-12-31 04:03:27 +0000334def testCreateAttributeNS(): pass
Paul Prescod7993bcc2000-07-01 14:54:16 +0000335
336def testParse(): pass
337
338def testParseString(): pass
339
340def testComment(): pass
341
342def testAttrListItem(): pass
343
344def testAttrListItems(): pass
345
346def testAttrListItemNS(): pass
347
348def testAttrListKeys(): pass
349
350def testAttrListKeysNS(): pass
351
352def testAttrListValues(): pass
353
354def testAttrListLength(): pass
355
356def testAttrList__getitem__(): pass
357
358def testAttrList__setitem__(): pass
359
360def testSetAttrValueandNodeValue(): pass
361
362def testParseElement(): pass
363
364def testParseAttributes(): pass
365
366def testParseElementNamespaces(): pass
367
368def testParseAttributeNamespaces(): pass
369
370def testParseProcessingInstructions(): pass
371
372def testChildNodes(): pass
373
374def testFirstChild(): pass
375
376def testHasChildNodes(): pass
377
Fred Drakea1bde802000-11-21 22:02:43 +0000378def testCloneElementShallow():
379 dom, clone = _setupCloneElement(0)
380 confirm(len(clone.childNodes) == 0
Fred Drake946f7b12001-09-28 20:31:50 +0000381 and clone.childNodes.length == 0
Fred Drakea1bde802000-11-21 22:02:43 +0000382 and clone.parentNode is None
383 and clone.toxml() == '<doc attr="value"/>'
384 , "testCloneElementShallow")
385 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000386
Fred Drakea1bde802000-11-21 22:02:43 +0000387def testCloneElementDeep():
388 dom, clone = _setupCloneElement(1)
389 confirm(len(clone.childNodes) == 1
Fred Drake946f7b12001-09-28 20:31:50 +0000390 and clone.childNodes.length == 1
Fred Drakea1bde802000-11-21 22:02:43 +0000391 and clone.parentNode is None
392 and clone.toxml() == '<doc attr="value"><foo/></doc>'
393 , "testCloneElementDeep")
394 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000395
Fred Drakea1bde802000-11-21 22:02:43 +0000396def _setupCloneElement(deep):
397 dom = parseString("<doc attr='value'><foo/></doc>")
398 root = dom.documentElement
399 clone = root.cloneNode(deep)
400 _testCloneElementCopiesAttributes(
401 root, clone, "testCloneElement" + (deep and "Deep" or "Shallow"))
402 # mutilate the original so shared data is detected
403 root.tagName = root.nodeName = "MODIFIED"
404 root.setAttribute("attr", "NEW VALUE")
405 root.setAttribute("added", "VALUE")
406 return dom, clone
407
408def _testCloneElementCopiesAttributes(e1, e2, test):
409 attrs1 = e1.attributes
410 attrs2 = e2.attributes
411 keys1 = attrs1.keys()
412 keys2 = attrs2.keys()
413 keys1.sort()
414 keys2.sort()
415 confirm(keys1 == keys2, "clone of element has same attribute keys")
416 for i in range(len(keys1)):
417 a1 = attrs1.item(i)
418 a2 = attrs2.item(i)
419 confirm(a1 is not a2
420 and a1.value == a2.value
421 and a1.nodeValue == a2.nodeValue
422 and a1.namespaceURI == a2.namespaceURI
423 and a1.localName == a2.localName
424 , "clone of attribute node has proper attribute values")
425 confirm(a2.ownerElement is e2,
426 "clone of attribute node correctly owned")
Fredrik Lundhf7850422001-01-17 21:51:36 +0000427
Paul Prescod7993bcc2000-07-01 14:54:16 +0000428
429def testCloneDocumentShallow(): pass
430
431def testCloneDocumentDeep(): pass
432
433def testCloneAttributeShallow(): pass
434
435def testCloneAttributeDeep(): pass
436
437def testClonePIShallow(): pass
438
439def testClonePIDeep(): pass
440
Fred Drakea1bde802000-11-21 22:02:43 +0000441def testNormalize():
442 doc = parseString("<doc/>")
443 root = doc.documentElement
444 root.appendChild(doc.createTextNode("first"))
445 root.appendChild(doc.createTextNode("second"))
Fred Drake946f7b12001-09-28 20:31:50 +0000446 confirm(len(root.childNodes) == 2
447 and root.childNodes.length == 2, "testNormalize -- preparation")
Fred Drakea1bde802000-11-21 22:02:43 +0000448 doc.normalize()
449 confirm(len(root.childNodes) == 1
Fred Drake946f7b12001-09-28 20:31:50 +0000450 and root.childNodes.length == 1
Fred Drakea1bde802000-11-21 22:02:43 +0000451 and root.firstChild is root.lastChild
452 and root.firstChild.data == "firstsecond"
453 , "testNormalize -- result")
454 doc.unlink()
455
Fred Drake3277da02000-12-14 18:20:22 +0000456 doc = parseString("<doc/>")
457 root = doc.documentElement
458 root.appendChild(doc.createTextNode(""))
459 doc.normalize()
Fred Drake946f7b12001-09-28 20:31:50 +0000460 confirm(len(root.childNodes) == 0
461 and root.childNodes.length == 0,
Fred Drake3277da02000-12-14 18:20:22 +0000462 "testNormalize -- single empty node removed")
463 doc.unlink()
464
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000465def testSiblings():
466 doc = parseString("<doc><?pi?>text?<elm/></doc>")
467 root = doc.documentElement
468 (pi, text, elm) = root.childNodes
Paul Prescod7993bcc2000-07-01 14:54:16 +0000469
Fred Drake004d5e62000-10-23 17:22:08 +0000470 confirm(pi.nextSibling is text and
471 pi.previousSibling is None and
472 text.nextSibling is elm and
473 text.previousSibling is pi and
474 elm.nextSibling is None and
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000475 elm.previousSibling is text, "testSiblings")
476
477 doc.unlink()
478
479def testParents():
480 doc = parseString("<doc><elm1><elm2/><elm2><elm3/></elm2></elm1></doc>")
481 root = doc.documentElement
482 elm1 = root.childNodes[0]
483 (elm2a, elm2b) = elm1.childNodes
484 elm3 = elm2b.childNodes[0]
485
486 confirm(root.parentNode is doc and
487 elm1.parentNode is root and
488 elm2a.parentNode is elm1 and
489 elm2b.parentNode is elm1 and
490 elm3.parentNode is elm2b, "testParents")
491
492 doc.unlink()
493
Fred Drake946f7b12001-09-28 20:31:50 +0000494def testNodeListItem():
495 doc = parseString("<doc><e/><e/></doc>")
496 children = doc.childNodes
497 docelem = children[0]
498 confirm(children[0] is children.item(0)
499 and children.item(1) is None
500 and docelem.childNodes.item(0) is docelem.childNodes[0]
501 and docelem.childNodes.item(1) is docelem.childNodes[1]
502 and docelem.childNodes.item(0).childNodes.item(0) is None,
503 "test NodeList.item()")
504 doc.unlink()
505
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000506def testSAX2DOM():
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000507 from xml.dom import pulldom
508
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000509 sax2dom = pulldom.SAX2DOM()
510 sax2dom.startDocument()
511 sax2dom.startElement("doc", {})
512 sax2dom.characters("text")
513 sax2dom.startElement("subelm", {})
514 sax2dom.characters("text")
515 sax2dom.endElement("subelm")
Fred Drake004d5e62000-10-23 17:22:08 +0000516 sax2dom.characters("text")
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000517 sax2dom.endElement("doc")
518 sax2dom.endDocument()
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000519
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000520 doc = sax2dom.document
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000521 root = doc.documentElement
522 (text1, elm1, text2) = root.childNodes
523 text3 = elm1.childNodes[0]
524
525 confirm(text1.previousSibling is None and
526 text1.nextSibling is elm1 and
527 elm1.previousSibling is text1 and
528 elm1.nextSibling is text2 and
529 text2.previousSibling is elm1 and
530 text2.nextSibling is None and
531 text3.previousSibling is None and
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000532 text3.nextSibling is None, "testSAX2DOM - siblings")
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000533
534 confirm(root.parentNode is doc and
535 text1.parentNode is root and
536 elm1.parentNode is root and
537 text2.parentNode is root and
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000538 text3.parentNode is elm1, "testSAX2DOM - parents")
Fred Drake004d5e62000-10-23 17:22:08 +0000539
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000540 doc.unlink()
541
542# --- MAIN PROGRAM
Fred Drake004d5e62000-10-23 17:22:08 +0000543
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000544names = globals().keys()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000545names.sort()
Paul Prescod10d27662000-09-18 19:07:26 +0000546
Fred Drakeacfb3f62001-02-01 18:11:29 +0000547failed = []
Paul Prescod10d27662000-09-18 19:07:26 +0000548
Paul Prescod7993bcc2000-07-01 14:54:16 +0000549for name in names:
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000550 if name.startswith("test"):
551 func = globals()[name]
Paul Prescod7993bcc2000-07-01 14:54:16 +0000552 try:
553 func()
554 print "Test Succeeded", name
Fred Drakeebe73022000-10-09 19:57:39 +0000555 confirm(len(Node.allnodes) == 0,
556 "assertion: len(Node.allnodes) == 0")
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000557 if len(Node.allnodes):
Paul Prescod7993bcc2000-07-01 14:54:16 +0000558 print "Garbage left over:"
Martin v. Löwis89c528b2000-09-19 16:22:10 +0000559 if verbose:
560 print Node.allnodes.items()[0:10]
561 else:
562 # Don't print specific nodes if repeatable results
563 # are needed
564 print len(Node.allnodes)
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000565 Node.allnodes = {}
Fred Drakeacfb3f62001-02-01 18:11:29 +0000566 except:
567 failed.append(name)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000568 print "Test Failed: ", name
Fred Drake1703cf62000-12-15 21:31:59 +0000569 sys.stdout.flush()
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000570 traceback.print_exception(*sys.exc_info())
Fred Drakeacfb3f62001-02-01 18:11:29 +0000571 print `sys.exc_info()[1]`
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000572 Node.allnodes = {}
Paul Prescod10d27662000-09-18 19:07:26 +0000573
Fred Drakeacfb3f62001-02-01 18:11:29 +0000574if failed:
575 print "\n\n\n**** Check for failures in these tests:"
576 for name in failed:
577 print " " + name
578 print
Paul Prescod10d27662000-09-18 19:07:26 +0000579else:
Fred Drakeacfb3f62001-02-01 18:11:29 +0000580 print "All tests succeeded"
Paul Prescod7993bcc2000-07-01 14:54:16 +0000581
Guido van Rossum9e79a252000-09-21 20:10:39 +0000582Node.debug = None # Delete debug output collected in a StringIO object
583Node._debug = 0 # And reset debug mode