blob: adac9900454de29c646d5f4437ab2593f6a35804 [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
Guido van Rossume2ae77b2001-10-24 20:42:55 +00007import os
Fred Drake17647f52000-07-03 16:37:42 +00008import 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__
Guido van Rossume2ae77b2001-10-24 20:42:55 +000016tstfile = os.path.join(os.path.dirname(base), "test"+os.extsep+"xml")
Fred Drake17647f52000-07-03 16:37:42 +000017del 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
Fred Drakee50959a2001-12-06 04:32:18 +000082def _create_fragment_test_nodes():
83 dom = parseString("<doc/>")
84 orig = dom.createTextNode("original")
85 c1 = dom.createTextNode("foo")
86 c2 = dom.createTextNode("bar")
87 c3 = dom.createTextNode("bat")
88 dom.documentElement.appendChild(orig)
89 frag = dom.createDocumentFragment()
90 frag.appendChild(c1)
91 frag.appendChild(c2)
92 frag.appendChild(c3)
93 return dom, orig, c1, c2, c3, frag
94
95def testInsertBeforeFragment():
96 dom, orig, c1, c2, c3, frag = _create_fragment_test_nodes()
97 dom.documentElement.insertBefore(frag, None)
98 confirm(tuple(dom.documentElement.childNodes) == (orig, c1, c2, c3),
99 "insertBefore(<fragment>, None)")
100 frag.unlink()
101 dom.unlink()
102 #
103 dom, orig, c1, c2, c3, frag = _create_fragment_test_nodes()
104 dom.documentElement.insertBefore(frag, orig)
105 confirm(tuple(dom.documentElement.childNodes) == (c1, c2, c3, orig),
106 "insertBefore(<fragment>, orig)")
107 frag.unlink()
108 dom.unlink()
109
Paul Prescod7993bcc2000-07-01 14:54:16 +0000110def testAppendChild():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000111 dom = parse(tstfile)
112 dom.documentElement.appendChild(dom.createComment(u"Hello"))
113 confirm(dom.documentElement.childNodes[-1].nodeName == "#comment")
114 confirm(dom.documentElement.childNodes[-1].data == "Hello")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000115 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000116
Fred Drakee50959a2001-12-06 04:32:18 +0000117def testAppendChildFragment():
118 dom, orig, c1, c2, c3, frag = _create_fragment_test_nodes()
119 dom.documentElement.appendChild(frag)
120 confirm(tuple(dom.documentElement.childNodes) == (orig, c1, c2, c3),
121 "appendChild(<fragment>)")
122 frag.unlink()
123 dom.unlink()
124
125def testReplaceChildFragment():
126 dom, orig, c1, c2, c3, frag = _create_fragment_test_nodes()
127 dom.documentElement.replaceChild(frag, orig)
128 orig.unlink()
129 confirm(tuple(dom.documentElement.childNodes) == (c1, c2, c3),
130 "replaceChild(<fragment>)")
131 frag.unlink()
132 dom.unlink()
133
Andrew M. Kuchlingad4a5582000-12-31 04:03:27 +0000134def testLegalChildren():
135 dom = Document()
136 elem = dom.createElement('element')
137 text = dom.createTextNode('text')
Fredrik Lundhf7850422001-01-17 21:51:36 +0000138
Andrew M. Kuchlingad4a5582000-12-31 04:03:27 +0000139 try: dom.appendChild(text)
140 except HierarchyRequestErr: pass
141 else:
142 print "dom.appendChild didn't raise HierarchyRequestErr"
143
144 dom.appendChild(elem)
145 try: dom.insertBefore(text, elem)
146 except HierarchyRequestErr: pass
147 else:
148 print "dom.appendChild didn't raise HierarchyRequestErr"
149
150 try: dom.replaceChild(text, elem)
151 except HierarchyRequestErr: pass
152 else:
153 print "dom.appendChild didn't raise HierarchyRequestErr"
154
Tim Peters0009c4e2001-02-21 07:29:48 +0000155 nodemap = elem.attributes
Andrew M. Kuchlingbc8f72c2001-02-21 01:30:26 +0000156 try: nodemap.setNamedItem(text)
157 except HierarchyRequestErr: pass
158 else:
159 print "NamedNodeMap.setNamedItem didn't raise HierarchyRequestErr"
160
161 try: nodemap.setNamedItemNS(text)
162 except HierarchyRequestErr: pass
163 else:
164 print "NamedNodeMap.setNamedItemNS didn't raise HierarchyRequestErr"
165
Andrew M. Kuchlingad4a5582000-12-31 04:03:27 +0000166 elem.appendChild(text)
Fredrik Lundhf7850422001-01-17 21:51:36 +0000167 dom.unlink()
Andrew M. Kuchlingad4a5582000-12-31 04:03:27 +0000168
Paul Prescod7993bcc2000-07-01 14:54:16 +0000169def testNonZero():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000170 dom = parse(tstfile)
171 confirm(dom)# should not be zero
172 dom.appendChild(dom.createComment("foo"))
173 confirm(not dom.childNodes[-1].childNodes)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000174 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000175
176def testUnlink():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000177 dom = parse(tstfile)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000178 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000179
180def testElement():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000181 dom = Document()
182 dom.appendChild(dom.createElement("abc"))
183 confirm(dom.documentElement)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000184 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000185
186def testAAA():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000187 dom = parseString("<abc/>")
188 el = dom.documentElement
189 el.setAttribute("spam", "jam2")
Fred Drakea1bde802000-11-21 22:02:43 +0000190 confirm(el.toxml() == '<abc spam="jam2"/>', "testAAA")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000191 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000192
193def testAAB():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000194 dom = parseString("<abc/>")
195 el = dom.documentElement
196 el.setAttribute("spam", "jam")
197 el.setAttribute("spam", "jam2")
Fred Drakea1bde802000-11-21 22:02:43 +0000198 confirm(el.toxml() == '<abc spam="jam2"/>', "testAAB")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000199 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000200
201def testAddAttr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000202 dom = Document()
203 child = dom.appendChild(dom.createElement("abc"))
Paul Prescod7993bcc2000-07-01 14:54:16 +0000204
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000205 child.setAttribute("def", "ghi")
206 confirm(child.getAttribute("def") == "ghi")
207 confirm(child.attributes["def"].value == "ghi")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000208
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000209 child.setAttribute("jkl", "mno")
210 confirm(child.getAttribute("jkl") == "mno")
211 confirm(child.attributes["jkl"].value == "mno")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000212
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000213 confirm(len(child.attributes) == 2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000214
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000215 child.setAttribute("def", "newval")
216 confirm(child.getAttribute("def") == "newval")
217 confirm(child.attributes["def"].value == "newval")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000218
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000219 confirm(len(child.attributes) == 2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000220 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000221
222def testDeleteAttr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000223 dom = Document()
224 child = dom.appendChild(dom.createElement("abc"))
Paul Prescod7993bcc2000-07-01 14:54:16 +0000225
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000226 confirm(len(child.attributes) == 0)
227 child.setAttribute("def", "ghi")
228 confirm(len(child.attributes) == 1)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000229 del child.attributes["def"]
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000230 confirm(len(child.attributes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000231 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000232
233def testRemoveAttr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000234 dom = Document()
235 child = dom.appendChild(dom.createElement("abc"))
Paul Prescod7993bcc2000-07-01 14:54:16 +0000236
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000237 child.setAttribute("def", "ghi")
238 confirm(len(child.attributes) == 1)
239 child.removeAttribute("def")
240 confirm(len(child.attributes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000241
242 dom.unlink()
243
244def testRemoveAttrNS():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000245 dom = Document()
246 child = dom.appendChild(
247 dom.createElementNS("http://www.python.org", "python:abc"))
Fred Drake004d5e62000-10-23 17:22:08 +0000248 child.setAttributeNS("http://www.w3.org", "xmlns:python",
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000249 "http://www.python.org")
250 child.setAttributeNS("http://www.python.org", "python:abcattr", "foo")
251 confirm(len(child.attributes) == 2)
252 child.removeAttributeNS("http://www.python.org", "abcattr")
253 confirm(len(child.attributes) == 1)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000254
255 dom.unlink()
Fred Drake004d5e62000-10-23 17:22:08 +0000256
Paul Prescod7993bcc2000-07-01 14:54:16 +0000257def testRemoveAttributeNode():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000258 dom = Document()
259 child = dom.appendChild(dom.createElement("foo"))
260 child.setAttribute("spam", "jam")
261 confirm(len(child.attributes) == 1)
262 node = child.getAttributeNode("spam")
263 child.removeAttributeNode(node)
264 confirm(len(child.attributes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000265
266 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000267
268def testChangeAttr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000269 dom = parseString("<abc/>")
270 el = dom.documentElement
271 el.setAttribute("spam", "jam")
272 confirm(len(el.attributes) == 1)
273 el.setAttribute("spam", "bam")
274 confirm(len(el.attributes) == 1)
275 el.attributes["spam"] = "ham"
276 confirm(len(el.attributes) == 1)
277 el.setAttribute("spam2", "bam")
278 confirm(len(el.attributes) == 2)
279 el.attributes[ "spam2"] = "bam2"
280 confirm(len(el.attributes) == 2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000281 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000282
283def testGetAttrList():
284 pass
285
286def testGetAttrValues(): pass
287
288def testGetAttrLength(): pass
289
290def testGetAttribute(): pass
291
292def testGetAttributeNS(): pass
293
294def testGetAttributeNode(): pass
295
Martin v. Löwis351c3d02001-06-03 14:27:02 +0000296def testGetElementsByTagNameNS():
297 d="""<foo xmlns:minidom="http://pyxml.sf.net/minidom">
298 <minidom:myelem/>
299 </foo>"""
300 dom = parseString(d)
301 elem = dom.getElementsByTagNameNS("http://pyxml.sf.net/minidom","myelem")
302 confirm(len(elem) == 1)
303 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000304
305def testGetEmptyNodeListFromElementsByTagNameNS(): pass
306
307def testElementReprAndStr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000308 dom = Document()
309 el = dom.appendChild(dom.createElement("abc"))
310 string1 = repr(el)
311 string2 = str(el)
312 confirm(string1 == string2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000313 dom.unlink()
314
315# commented out until Fredrick's fix is checked in
316def _testElementReprAndStrUnicode():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000317 dom = Document()
318 el = dom.appendChild(dom.createElement(u"abc"))
319 string1 = repr(el)
320 string2 = str(el)
321 confirm(string1 == string2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000322 dom.unlink()
323
324# commented out until Fredrick's fix is checked in
325def _testElementReprAndStrUnicodeNS():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000326 dom = Document()
327 el = dom.appendChild(
328 dom.createElementNS(u"http://www.slashdot.org", u"slash:abc"))
329 string1 = repr(el)
330 string2 = str(el)
331 confirm(string1 == string2)
332 confirm(string1.find("slash:abc") != -1)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000333 dom.unlink()
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000334 confirm(len(Node.allnodes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000335
336def testAttributeRepr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000337 dom = Document()
338 el = dom.appendChild(dom.createElement(u"abc"))
339 node = el.setAttribute("abc", "def")
340 confirm(str(node) == repr(node))
Paul Prescod7993bcc2000-07-01 14:54:16 +0000341 dom.unlink()
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000342 confirm(len(Node.allnodes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000343
344def testTextNodeRepr(): pass
345
Martin v. Löwis0a84a332000-10-06 22:42:55 +0000346def testWriteXML():
Martin v. Löwisfe28ca02001-02-06 01:16:48 +0000347 str = '<?xml version="1.0" ?>\n<a b="c"/>'
Martin v. Löwis0a84a332000-10-06 22:42:55 +0000348 dom = parseString(str)
349 domstr = dom.toxml()
350 dom.unlink()
351 confirm(str == domstr)
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000352 confirm(len(Node.allnodes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000353
354def testProcessingInstruction(): pass
355
356def testProcessingInstructionRepr(): pass
357
358def testTextRepr(): pass
359
360def testWriteText(): pass
361
362def testDocumentElement(): pass
363
Fred Drakea1bde802000-11-21 22:02:43 +0000364def testTooManyDocumentElements():
365 doc = parseString("<doc/>")
366 elem = doc.createElement("extra")
367 try:
368 doc.appendChild(elem)
Martin v. Löwis2bcb3232001-01-27 09:17:55 +0000369 except HierarchyRequestErr:
Fred Drakea1bde802000-11-21 22:02:43 +0000370 print "Caught expected exception when adding extra document element."
371 else:
372 print "Failed to catch expected exception when" \
373 " adding extra document element."
374 elem.unlink()
375 doc.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000376
377def testCreateElementNS(): pass
378
Andrew M. Kuchlingad4a5582000-12-31 04:03:27 +0000379def testCreateAttributeNS(): pass
Paul Prescod7993bcc2000-07-01 14:54:16 +0000380
381def testParse(): pass
382
383def testParseString(): pass
384
385def testComment(): pass
386
387def testAttrListItem(): pass
388
389def testAttrListItems(): pass
390
391def testAttrListItemNS(): pass
392
393def testAttrListKeys(): pass
394
395def testAttrListKeysNS(): pass
396
397def testAttrListValues(): pass
398
399def testAttrListLength(): pass
400
401def testAttrList__getitem__(): pass
402
403def testAttrList__setitem__(): pass
404
405def testSetAttrValueandNodeValue(): pass
406
407def testParseElement(): pass
408
409def testParseAttributes(): pass
410
411def testParseElementNamespaces(): pass
412
413def testParseAttributeNamespaces(): pass
414
415def testParseProcessingInstructions(): pass
416
417def testChildNodes(): pass
418
419def testFirstChild(): pass
420
421def testHasChildNodes(): pass
422
Fred Drakea1bde802000-11-21 22:02:43 +0000423def testCloneElementShallow():
424 dom, clone = _setupCloneElement(0)
425 confirm(len(clone.childNodes) == 0
Fred Drake946f7b12001-09-28 20:31:50 +0000426 and clone.childNodes.length == 0
Fred Drakea1bde802000-11-21 22:02:43 +0000427 and clone.parentNode is None
428 and clone.toxml() == '<doc attr="value"/>'
429 , "testCloneElementShallow")
430 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000431
Fred Drakea1bde802000-11-21 22:02:43 +0000432def testCloneElementDeep():
433 dom, clone = _setupCloneElement(1)
434 confirm(len(clone.childNodes) == 1
Fred Drake946f7b12001-09-28 20:31:50 +0000435 and clone.childNodes.length == 1
Fred Drakea1bde802000-11-21 22:02:43 +0000436 and clone.parentNode is None
437 and clone.toxml() == '<doc attr="value"><foo/></doc>'
438 , "testCloneElementDeep")
439 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000440
Fred Drakea1bde802000-11-21 22:02:43 +0000441def _setupCloneElement(deep):
442 dom = parseString("<doc attr='value'><foo/></doc>")
443 root = dom.documentElement
444 clone = root.cloneNode(deep)
445 _testCloneElementCopiesAttributes(
446 root, clone, "testCloneElement" + (deep and "Deep" or "Shallow"))
447 # mutilate the original so shared data is detected
448 root.tagName = root.nodeName = "MODIFIED"
449 root.setAttribute("attr", "NEW VALUE")
450 root.setAttribute("added", "VALUE")
451 return dom, clone
452
453def _testCloneElementCopiesAttributes(e1, e2, test):
454 attrs1 = e1.attributes
455 attrs2 = e2.attributes
456 keys1 = attrs1.keys()
457 keys2 = attrs2.keys()
458 keys1.sort()
459 keys2.sort()
460 confirm(keys1 == keys2, "clone of element has same attribute keys")
461 for i in range(len(keys1)):
462 a1 = attrs1.item(i)
463 a2 = attrs2.item(i)
464 confirm(a1 is not a2
465 and a1.value == a2.value
466 and a1.nodeValue == a2.nodeValue
467 and a1.namespaceURI == a2.namespaceURI
468 and a1.localName == a2.localName
469 , "clone of attribute node has proper attribute values")
470 confirm(a2.ownerElement is e2,
471 "clone of attribute node correctly owned")
Fredrik Lundhf7850422001-01-17 21:51:36 +0000472
Paul Prescod7993bcc2000-07-01 14:54:16 +0000473
474def testCloneDocumentShallow(): pass
475
476def testCloneDocumentDeep(): pass
477
478def testCloneAttributeShallow(): pass
479
480def testCloneAttributeDeep(): pass
481
482def testClonePIShallow(): pass
483
484def testClonePIDeep(): pass
485
Fred Drakea1bde802000-11-21 22:02:43 +0000486def testNormalize():
487 doc = parseString("<doc/>")
488 root = doc.documentElement
489 root.appendChild(doc.createTextNode("first"))
490 root.appendChild(doc.createTextNode("second"))
Fred Drake946f7b12001-09-28 20:31:50 +0000491 confirm(len(root.childNodes) == 2
492 and root.childNodes.length == 2, "testNormalize -- preparation")
Fred Drakea1bde802000-11-21 22:02:43 +0000493 doc.normalize()
494 confirm(len(root.childNodes) == 1
Fred Drake946f7b12001-09-28 20:31:50 +0000495 and root.childNodes.length == 1
Fred Drakea1bde802000-11-21 22:02:43 +0000496 and root.firstChild is root.lastChild
497 and root.firstChild.data == "firstsecond"
498 , "testNormalize -- result")
499 doc.unlink()
500
Fred Drake3277da02000-12-14 18:20:22 +0000501 doc = parseString("<doc/>")
502 root = doc.documentElement
503 root.appendChild(doc.createTextNode(""))
504 doc.normalize()
Fred Drake946f7b12001-09-28 20:31:50 +0000505 confirm(len(root.childNodes) == 0
506 and root.childNodes.length == 0,
Fred Drake3277da02000-12-14 18:20:22 +0000507 "testNormalize -- single empty node removed")
508 doc.unlink()
509
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000510def testSiblings():
511 doc = parseString("<doc><?pi?>text?<elm/></doc>")
512 root = doc.documentElement
513 (pi, text, elm) = root.childNodes
Paul Prescod7993bcc2000-07-01 14:54:16 +0000514
Fred Drake004d5e62000-10-23 17:22:08 +0000515 confirm(pi.nextSibling is text and
516 pi.previousSibling is None and
517 text.nextSibling is elm and
518 text.previousSibling is pi and
519 elm.nextSibling is None and
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000520 elm.previousSibling is text, "testSiblings")
521
522 doc.unlink()
523
524def testParents():
525 doc = parseString("<doc><elm1><elm2/><elm2><elm3/></elm2></elm1></doc>")
526 root = doc.documentElement
527 elm1 = root.childNodes[0]
528 (elm2a, elm2b) = elm1.childNodes
529 elm3 = elm2b.childNodes[0]
530
531 confirm(root.parentNode is doc and
532 elm1.parentNode is root and
533 elm2a.parentNode is elm1 and
534 elm2b.parentNode is elm1 and
535 elm3.parentNode is elm2b, "testParents")
536
537 doc.unlink()
538
Fred Drake946f7b12001-09-28 20:31:50 +0000539def testNodeListItem():
540 doc = parseString("<doc><e/><e/></doc>")
541 children = doc.childNodes
542 docelem = children[0]
543 confirm(children[0] is children.item(0)
544 and children.item(1) is None
545 and docelem.childNodes.item(0) is docelem.childNodes[0]
546 and docelem.childNodes.item(1) is docelem.childNodes[1]
547 and docelem.childNodes.item(0).childNodes.item(0) is None,
548 "test NodeList.item()")
549 doc.unlink()
550
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000551def testSAX2DOM():
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000552 from xml.dom import pulldom
553
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000554 sax2dom = pulldom.SAX2DOM()
555 sax2dom.startDocument()
556 sax2dom.startElement("doc", {})
557 sax2dom.characters("text")
558 sax2dom.startElement("subelm", {})
559 sax2dom.characters("text")
560 sax2dom.endElement("subelm")
Fred Drake004d5e62000-10-23 17:22:08 +0000561 sax2dom.characters("text")
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000562 sax2dom.endElement("doc")
563 sax2dom.endDocument()
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000564
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000565 doc = sax2dom.document
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000566 root = doc.documentElement
567 (text1, elm1, text2) = root.childNodes
568 text3 = elm1.childNodes[0]
569
570 confirm(text1.previousSibling is None and
571 text1.nextSibling is elm1 and
572 elm1.previousSibling is text1 and
573 elm1.nextSibling is text2 and
574 text2.previousSibling is elm1 and
575 text2.nextSibling is None and
576 text3.previousSibling is None and
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000577 text3.nextSibling is None, "testSAX2DOM - siblings")
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000578
579 confirm(root.parentNode is doc and
580 text1.parentNode is root and
581 elm1.parentNode is root and
582 text2.parentNode is root and
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000583 text3.parentNode is elm1, "testSAX2DOM - parents")
Fred Drake004d5e62000-10-23 17:22:08 +0000584
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000585 doc.unlink()
586
587# --- MAIN PROGRAM
Fred Drake004d5e62000-10-23 17:22:08 +0000588
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000589names = globals().keys()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000590names.sort()
Paul Prescod10d27662000-09-18 19:07:26 +0000591
Fred Drakeacfb3f62001-02-01 18:11:29 +0000592failed = []
Paul Prescod10d27662000-09-18 19:07:26 +0000593
Paul Prescod7993bcc2000-07-01 14:54:16 +0000594for name in names:
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000595 if name.startswith("test"):
596 func = globals()[name]
Paul Prescod7993bcc2000-07-01 14:54:16 +0000597 try:
598 func()
599 print "Test Succeeded", name
Fred Drakeebe73022000-10-09 19:57:39 +0000600 confirm(len(Node.allnodes) == 0,
601 "assertion: len(Node.allnodes) == 0")
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000602 if len(Node.allnodes):
Paul Prescod7993bcc2000-07-01 14:54:16 +0000603 print "Garbage left over:"
Martin v. Löwis89c528b2000-09-19 16:22:10 +0000604 if verbose:
605 print Node.allnodes.items()[0:10]
606 else:
607 # Don't print specific nodes if repeatable results
608 # are needed
609 print len(Node.allnodes)
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000610 Node.allnodes = {}
Fred Drakeacfb3f62001-02-01 18:11:29 +0000611 except:
612 failed.append(name)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000613 print "Test Failed: ", name
Fred Drake1703cf62000-12-15 21:31:59 +0000614 sys.stdout.flush()
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000615 traceback.print_exception(*sys.exc_info())
Fred Drakeacfb3f62001-02-01 18:11:29 +0000616 print `sys.exc_info()[1]`
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000617 Node.allnodes = {}
Paul Prescod10d27662000-09-18 19:07:26 +0000618
Fred Drakeacfb3f62001-02-01 18:11:29 +0000619if failed:
620 print "\n\n\n**** Check for failures in these tests:"
621 for name in failed:
622 print " " + name
623 print
Paul Prescod10d27662000-09-18 19:07:26 +0000624else:
Fred Drakeacfb3f62001-02-01 18:11:29 +0000625 print "All tests succeeded"
Paul Prescod7993bcc2000-07-01 14:54:16 +0000626
Guido van Rossum9e79a252000-09-21 20:10:39 +0000627Node.debug = None # Delete debug output collected in a StringIO object
628Node._debug = 0 # And reset debug mode