blob: 9e00b4f79903d02f4283012dab0b06c7ec4ad078 [file] [log] [blame]
Fred Drake17647f52000-07-03 16:37:42 +00001# test for xml.dom.minidom
Paul Prescod7993bcc2000-07-01 14:54:16 +00002
Guido van Rossume2ae77b2001-10-24 20:42:55 +00003import os
Fred Drake17647f52000-07-03 16:37:42 +00004import sys
5import traceback
Fred Drakec441f7b2002-07-19 22:16:41 +00006
Barry Warsaw04f357c2002-07-23 19:04:11 +00007from test.test_support import verbose
Fred Drake17647f52000-07-03 16:37:42 +00008
Fred Drakec441f7b2002-07-19 22:16:41 +00009import xml.dom
10import xml.parsers.expat
11
12from xml.dom.minidom import parse, Node, Document, parseString
13
Fred Drake17647f52000-07-03 16:37:42 +000014if __name__ == "__main__":
15 base = sys.argv[0]
16else:
17 base = __file__
Guido van Rossume2ae77b2001-10-24 20:42:55 +000018tstfile = os.path.join(os.path.dirname(base), "test"+os.extsep+"xml")
Fred Drake17647f52000-07-03 16:37:42 +000019del base
Paul Prescod7993bcc2000-07-01 14:54:16 +000020
Jeremy Hylton3b0c6002000-10-12 17:31:36 +000021def confirm(test, testname = "Test"):
Fred Drakec441f7b2002-07-19 22:16:41 +000022 if not test:
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)
Fred Drakec441f7b2002-07-19 22:16:41 +0000140 except xml.dom.HierarchyRequestErr: pass
Andrew M. Kuchlingad4a5582000-12-31 04:03:27 +0000141 else:
142 print "dom.appendChild didn't raise HierarchyRequestErr"
143
144 dom.appendChild(elem)
145 try: dom.insertBefore(text, elem)
Fred Drakec441f7b2002-07-19 22:16:41 +0000146 except xml.dom.HierarchyRequestErr: pass
Andrew M. Kuchlingad4a5582000-12-31 04:03:27 +0000147 else:
148 print "dom.appendChild didn't raise HierarchyRequestErr"
149
150 try: dom.replaceChild(text, elem)
Fred Drakec441f7b2002-07-19 22:16:41 +0000151 except xml.dom.HierarchyRequestErr: pass
Andrew M. Kuchlingad4a5582000-12-31 04:03:27 +0000152 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)
Fred Drakec441f7b2002-07-19 22:16:41 +0000157 except xml.dom.HierarchyRequestErr: pass
Andrew M. Kuchlingbc8f72c2001-02-21 01:30:26 +0000158 else:
159 print "NamedNodeMap.setNamedItem didn't raise HierarchyRequestErr"
160
161 try: nodemap.setNamedItemNS(text)
Fred Drakec441f7b2002-07-19 22:16:41 +0000162 except xml.dom.HierarchyRequestErr: pass
Andrew M. Kuchlingbc8f72c2001-02-21 01:30:26 +0000163 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
Fred Drake2998a552001-12-06 18:27:48 +0000169def testNamedNodeMapSetItem():
170 dom = Document()
171 elem = dom.createElement('element')
172 attrs = elem.attributes
173 attrs["foo"] = "bar"
174 a = attrs.item(0)
175 confirm(a.ownerDocument is dom,
176 "NamedNodeMap.__setitem__() sets ownerDocument")
177 confirm(a.ownerElement is elem,
178 "NamedNodeMap.__setitem__() sets ownerElement")
179 confirm(a.value == "bar",
180 "NamedNodeMap.__setitem__() sets value")
181 confirm(a.nodeValue == "bar",
182 "NamedNodeMap.__setitem__() sets nodeValue")
183 elem.unlink()
184 dom.unlink()
185
Paul Prescod7993bcc2000-07-01 14:54:16 +0000186def testNonZero():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000187 dom = parse(tstfile)
188 confirm(dom)# should not be zero
189 dom.appendChild(dom.createComment("foo"))
190 confirm(not dom.childNodes[-1].childNodes)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000191 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000192
193def testUnlink():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000194 dom = parse(tstfile)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000195 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000196
197def testElement():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000198 dom = Document()
199 dom.appendChild(dom.createElement("abc"))
200 confirm(dom.documentElement)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000201 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000202
203def testAAA():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000204 dom = parseString("<abc/>")
205 el = dom.documentElement
206 el.setAttribute("spam", "jam2")
Fred Drakea1bde802000-11-21 22:02:43 +0000207 confirm(el.toxml() == '<abc spam="jam2"/>', "testAAA")
Fred Drake2998a552001-12-06 18:27:48 +0000208 a = el.getAttributeNode("spam")
209 confirm(a.ownerDocument is dom,
210 "setAttribute() sets ownerDocument")
211 confirm(a.ownerElement is dom.documentElement,
212 "setAttribute() sets ownerElement")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000213 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000214
215def testAAB():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000216 dom = parseString("<abc/>")
217 el = dom.documentElement
218 el.setAttribute("spam", "jam")
219 el.setAttribute("spam", "jam2")
Fred Drakea1bde802000-11-21 22:02:43 +0000220 confirm(el.toxml() == '<abc spam="jam2"/>', "testAAB")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000221 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000222
223def testAddAttr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000224 dom = Document()
225 child = dom.appendChild(dom.createElement("abc"))
Paul Prescod7993bcc2000-07-01 14:54:16 +0000226
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000227 child.setAttribute("def", "ghi")
228 confirm(child.getAttribute("def") == "ghi")
229 confirm(child.attributes["def"].value == "ghi")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000230
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000231 child.setAttribute("jkl", "mno")
232 confirm(child.getAttribute("jkl") == "mno")
233 confirm(child.attributes["jkl"].value == "mno")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000234
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000235 confirm(len(child.attributes) == 2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000236
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000237 child.setAttribute("def", "newval")
238 confirm(child.getAttribute("def") == "newval")
239 confirm(child.attributes["def"].value == "newval")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000240
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000241 confirm(len(child.attributes) == 2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000242 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000243
244def testDeleteAttr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000245 dom = Document()
246 child = dom.appendChild(dom.createElement("abc"))
Paul Prescod7993bcc2000-07-01 14:54:16 +0000247
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000248 confirm(len(child.attributes) == 0)
249 child.setAttribute("def", "ghi")
250 confirm(len(child.attributes) == 1)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000251 del child.attributes["def"]
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000252 confirm(len(child.attributes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000253 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000254
255def testRemoveAttr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000256 dom = Document()
257 child = dom.appendChild(dom.createElement("abc"))
Paul Prescod7993bcc2000-07-01 14:54:16 +0000258
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000259 child.setAttribute("def", "ghi")
260 confirm(len(child.attributes) == 1)
261 child.removeAttribute("def")
262 confirm(len(child.attributes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000263
264 dom.unlink()
265
266def testRemoveAttrNS():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000267 dom = Document()
268 child = dom.appendChild(
269 dom.createElementNS("http://www.python.org", "python:abc"))
Fred Drake004d5e62000-10-23 17:22:08 +0000270 child.setAttributeNS("http://www.w3.org", "xmlns:python",
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000271 "http://www.python.org")
272 child.setAttributeNS("http://www.python.org", "python:abcattr", "foo")
273 confirm(len(child.attributes) == 2)
274 child.removeAttributeNS("http://www.python.org", "abcattr")
275 confirm(len(child.attributes) == 1)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000276
277 dom.unlink()
Fred Drake004d5e62000-10-23 17:22:08 +0000278
Paul Prescod7993bcc2000-07-01 14:54:16 +0000279def testRemoveAttributeNode():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000280 dom = Document()
281 child = dom.appendChild(dom.createElement("foo"))
282 child.setAttribute("spam", "jam")
283 confirm(len(child.attributes) == 1)
284 node = child.getAttributeNode("spam")
285 child.removeAttributeNode(node)
286 confirm(len(child.attributes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000287
288 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000289
290def testChangeAttr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000291 dom = parseString("<abc/>")
292 el = dom.documentElement
293 el.setAttribute("spam", "jam")
294 confirm(len(el.attributes) == 1)
295 el.setAttribute("spam", "bam")
296 confirm(len(el.attributes) == 1)
297 el.attributes["spam"] = "ham"
298 confirm(len(el.attributes) == 1)
299 el.setAttribute("spam2", "bam")
300 confirm(len(el.attributes) == 2)
301 el.attributes[ "spam2"] = "bam2"
302 confirm(len(el.attributes) == 2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000303 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000304
305def testGetAttrList():
306 pass
307
308def testGetAttrValues(): pass
309
310def testGetAttrLength(): pass
311
312def testGetAttribute(): pass
313
314def testGetAttributeNS(): pass
315
316def testGetAttributeNode(): pass
317
Martin v. Löwis351c3d02001-06-03 14:27:02 +0000318def testGetElementsByTagNameNS():
319 d="""<foo xmlns:minidom="http://pyxml.sf.net/minidom">
320 <minidom:myelem/>
321 </foo>"""
322 dom = parseString(d)
323 elem = dom.getElementsByTagNameNS("http://pyxml.sf.net/minidom","myelem")
324 confirm(len(elem) == 1)
325 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000326
327def testGetEmptyNodeListFromElementsByTagNameNS(): pass
328
329def testElementReprAndStr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000330 dom = Document()
331 el = dom.appendChild(dom.createElement("abc"))
332 string1 = repr(el)
333 string2 = str(el)
334 confirm(string1 == string2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000335 dom.unlink()
336
337# commented out until Fredrick's fix is checked in
338def _testElementReprAndStrUnicode():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000339 dom = Document()
340 el = dom.appendChild(dom.createElement(u"abc"))
341 string1 = repr(el)
342 string2 = str(el)
343 confirm(string1 == string2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000344 dom.unlink()
345
346# commented out until Fredrick's fix is checked in
347def _testElementReprAndStrUnicodeNS():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000348 dom = Document()
349 el = dom.appendChild(
350 dom.createElementNS(u"http://www.slashdot.org", u"slash:abc"))
351 string1 = repr(el)
352 string2 = str(el)
353 confirm(string1 == string2)
354 confirm(string1.find("slash:abc") != -1)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000355 dom.unlink()
356
357def testAttributeRepr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000358 dom = Document()
359 el = dom.appendChild(dom.createElement(u"abc"))
360 node = el.setAttribute("abc", "def")
361 confirm(str(node) == repr(node))
Paul Prescod7993bcc2000-07-01 14:54:16 +0000362 dom.unlink()
363
364def testTextNodeRepr(): pass
365
Martin v. Löwis0a84a332000-10-06 22:42:55 +0000366def testWriteXML():
Martin v. Löwisfe28ca02001-02-06 01:16:48 +0000367 str = '<?xml version="1.0" ?>\n<a b="c"/>'
Martin v. Löwis0a84a332000-10-06 22:42:55 +0000368 dom = parseString(str)
369 domstr = dom.toxml()
370 dom.unlink()
371 confirm(str == domstr)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000372
373def testProcessingInstruction(): pass
374
375def testProcessingInstructionRepr(): pass
376
377def testTextRepr(): pass
378
379def testWriteText(): pass
380
381def testDocumentElement(): pass
382
Fred Drakea1bde802000-11-21 22:02:43 +0000383def testTooManyDocumentElements():
384 doc = parseString("<doc/>")
385 elem = doc.createElement("extra")
386 try:
387 doc.appendChild(elem)
Fred Drakec441f7b2002-07-19 22:16:41 +0000388 except xml.dom.HierarchyRequestErr:
389 pass
Fred Drakea1bde802000-11-21 22:02:43 +0000390 else:
391 print "Failed to catch expected exception when" \
392 " adding extra document element."
393 elem.unlink()
394 doc.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000395
396def testCreateElementNS(): pass
397
Andrew M. Kuchlingad4a5582000-12-31 04:03:27 +0000398def testCreateAttributeNS(): pass
Paul Prescod7993bcc2000-07-01 14:54:16 +0000399
400def testParse(): pass
401
402def testParseString(): pass
403
404def testComment(): pass
405
406def testAttrListItem(): pass
407
408def testAttrListItems(): pass
409
410def testAttrListItemNS(): pass
411
412def testAttrListKeys(): pass
413
414def testAttrListKeysNS(): pass
415
416def testAttrListValues(): pass
417
418def testAttrListLength(): pass
419
420def testAttrList__getitem__(): pass
421
422def testAttrList__setitem__(): pass
423
424def testSetAttrValueandNodeValue(): pass
425
426def testParseElement(): pass
427
428def testParseAttributes(): pass
429
430def testParseElementNamespaces(): pass
431
432def testParseAttributeNamespaces(): pass
433
434def testParseProcessingInstructions(): pass
435
436def testChildNodes(): pass
437
438def testFirstChild(): pass
439
440def testHasChildNodes(): pass
441
Fred Drakea1bde802000-11-21 22:02:43 +0000442def testCloneElementShallow():
443 dom, clone = _setupCloneElement(0)
444 confirm(len(clone.childNodes) == 0
Fred Drake946f7b12001-09-28 20:31:50 +0000445 and clone.childNodes.length == 0
Fred Drakea1bde802000-11-21 22:02:43 +0000446 and clone.parentNode is None
447 and clone.toxml() == '<doc attr="value"/>'
448 , "testCloneElementShallow")
449 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000450
Fred Drakea1bde802000-11-21 22:02:43 +0000451def testCloneElementDeep():
452 dom, clone = _setupCloneElement(1)
453 confirm(len(clone.childNodes) == 1
Fred Drake946f7b12001-09-28 20:31:50 +0000454 and clone.childNodes.length == 1
Fred Drakea1bde802000-11-21 22:02:43 +0000455 and clone.parentNode is None
456 and clone.toxml() == '<doc attr="value"><foo/></doc>'
457 , "testCloneElementDeep")
458 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000459
Fred Drakea1bde802000-11-21 22:02:43 +0000460def _setupCloneElement(deep):
461 dom = parseString("<doc attr='value'><foo/></doc>")
462 root = dom.documentElement
463 clone = root.cloneNode(deep)
464 _testCloneElementCopiesAttributes(
465 root, clone, "testCloneElement" + (deep and "Deep" or "Shallow"))
466 # mutilate the original so shared data is detected
467 root.tagName = root.nodeName = "MODIFIED"
468 root.setAttribute("attr", "NEW VALUE")
469 root.setAttribute("added", "VALUE")
470 return dom, clone
471
472def _testCloneElementCopiesAttributes(e1, e2, test):
473 attrs1 = e1.attributes
474 attrs2 = e2.attributes
475 keys1 = attrs1.keys()
476 keys2 = attrs2.keys()
477 keys1.sort()
478 keys2.sort()
479 confirm(keys1 == keys2, "clone of element has same attribute keys")
480 for i in range(len(keys1)):
481 a1 = attrs1.item(i)
482 a2 = attrs2.item(i)
483 confirm(a1 is not a2
484 and a1.value == a2.value
485 and a1.nodeValue == a2.nodeValue
486 and a1.namespaceURI == a2.namespaceURI
487 and a1.localName == a2.localName
488 , "clone of attribute node has proper attribute values")
489 confirm(a2.ownerElement is e2,
490 "clone of attribute node correctly owned")
Fredrik Lundhf7850422001-01-17 21:51:36 +0000491
Paul Prescod7993bcc2000-07-01 14:54:16 +0000492
493def testCloneDocumentShallow(): pass
494
495def testCloneDocumentDeep(): pass
496
497def testCloneAttributeShallow(): pass
498
499def testCloneAttributeDeep(): pass
500
501def testClonePIShallow(): pass
502
503def testClonePIDeep(): pass
504
Fred Drakea1bde802000-11-21 22:02:43 +0000505def testNormalize():
506 doc = parseString("<doc/>")
507 root = doc.documentElement
508 root.appendChild(doc.createTextNode("first"))
509 root.appendChild(doc.createTextNode("second"))
Fred Drake946f7b12001-09-28 20:31:50 +0000510 confirm(len(root.childNodes) == 2
511 and root.childNodes.length == 2, "testNormalize -- preparation")
Fred Drakea1bde802000-11-21 22:02:43 +0000512 doc.normalize()
513 confirm(len(root.childNodes) == 1
Fred Drake946f7b12001-09-28 20:31:50 +0000514 and root.childNodes.length == 1
Fred Drakea1bde802000-11-21 22:02:43 +0000515 and root.firstChild is root.lastChild
516 and root.firstChild.data == "firstsecond"
517 , "testNormalize -- result")
518 doc.unlink()
519
Fred Drake3277da02000-12-14 18:20:22 +0000520 doc = parseString("<doc/>")
521 root = doc.documentElement
522 root.appendChild(doc.createTextNode(""))
523 doc.normalize()
Fred Drake946f7b12001-09-28 20:31:50 +0000524 confirm(len(root.childNodes) == 0
525 and root.childNodes.length == 0,
Fred Drake3277da02000-12-14 18:20:22 +0000526 "testNormalize -- single empty node removed")
527 doc.unlink()
528
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000529def testSiblings():
530 doc = parseString("<doc><?pi?>text?<elm/></doc>")
531 root = doc.documentElement
532 (pi, text, elm) = root.childNodes
Paul Prescod7993bcc2000-07-01 14:54:16 +0000533
Fred Drake004d5e62000-10-23 17:22:08 +0000534 confirm(pi.nextSibling is text and
535 pi.previousSibling is None and
536 text.nextSibling is elm and
537 text.previousSibling is pi and
538 elm.nextSibling is None and
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000539 elm.previousSibling is text, "testSiblings")
540
541 doc.unlink()
542
543def testParents():
544 doc = parseString("<doc><elm1><elm2/><elm2><elm3/></elm2></elm1></doc>")
545 root = doc.documentElement
546 elm1 = root.childNodes[0]
547 (elm2a, elm2b) = elm1.childNodes
548 elm3 = elm2b.childNodes[0]
549
550 confirm(root.parentNode is doc and
551 elm1.parentNode is root and
552 elm2a.parentNode is elm1 and
553 elm2b.parentNode is elm1 and
554 elm3.parentNode is elm2b, "testParents")
555
556 doc.unlink()
557
Fred Drake946f7b12001-09-28 20:31:50 +0000558def testNodeListItem():
559 doc = parseString("<doc><e/><e/></doc>")
560 children = doc.childNodes
561 docelem = children[0]
562 confirm(children[0] is children.item(0)
563 and children.item(1) is None
564 and docelem.childNodes.item(0) is docelem.childNodes[0]
565 and docelem.childNodes.item(1) is docelem.childNodes[1]
566 and docelem.childNodes.item(0).childNodes.item(0) is None,
567 "test NodeList.item()")
568 doc.unlink()
569
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000570def testSAX2DOM():
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000571 from xml.dom import pulldom
572
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000573 sax2dom = pulldom.SAX2DOM()
574 sax2dom.startDocument()
575 sax2dom.startElement("doc", {})
576 sax2dom.characters("text")
577 sax2dom.startElement("subelm", {})
578 sax2dom.characters("text")
579 sax2dom.endElement("subelm")
Fred Drake004d5e62000-10-23 17:22:08 +0000580 sax2dom.characters("text")
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000581 sax2dom.endElement("doc")
582 sax2dom.endDocument()
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000583
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000584 doc = sax2dom.document
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000585 root = doc.documentElement
586 (text1, elm1, text2) = root.childNodes
587 text3 = elm1.childNodes[0]
588
589 confirm(text1.previousSibling is None and
590 text1.nextSibling is elm1 and
591 elm1.previousSibling is text1 and
592 elm1.nextSibling is text2 and
593 text2.previousSibling is elm1 and
594 text2.nextSibling is None and
595 text3.previousSibling is None and
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000596 text3.nextSibling is None, "testSAX2DOM - siblings")
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000597
598 confirm(root.parentNode is doc and
599 text1.parentNode is root and
600 elm1.parentNode is root and
601 text2.parentNode is root and
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000602 text3.parentNode is elm1, "testSAX2DOM - parents")
Fred Drake004d5e62000-10-23 17:22:08 +0000603
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000604 doc.unlink()
605
Martin v. Löwis7d650ca2002-06-30 15:05:00 +0000606def testEncodings():
607 doc = parseString('<foo>&#x20ac;</foo>')
608 confirm(doc.toxml() == u'<?xml version="1.0" ?>\n<foo>\u20ac</foo>'
609 and doc.toxml('utf-8') == '<?xml version="1.0" encoding="utf-8"?>\n<foo>\xe2\x82\xac</foo>'
610 and doc.toxml('iso-8859-15') == '<?xml version="1.0" encoding="iso-8859-15"?>\n<foo>\xa4</foo>',
611 "testEncodings - encoding EURO SIGN")
612 doc.unlink()
613
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000614# --- MAIN PROGRAM
Fred Drake004d5e62000-10-23 17:22:08 +0000615
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000616names = globals().keys()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000617names.sort()
Paul Prescod10d27662000-09-18 19:07:26 +0000618
Fred Drakeacfb3f62001-02-01 18:11:29 +0000619failed = []
Paul Prescod10d27662000-09-18 19:07:26 +0000620
Paul Prescod7993bcc2000-07-01 14:54:16 +0000621for name in names:
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000622 if name.startswith("test"):
623 func = globals()[name]
Paul Prescod7993bcc2000-07-01 14:54:16 +0000624 try:
625 func()
Fred Drakeebe73022000-10-09 19:57:39 +0000626 confirm(len(Node.allnodes) == 0,
627 "assertion: len(Node.allnodes) == 0")
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000628 if len(Node.allnodes):
Paul Prescod7993bcc2000-07-01 14:54:16 +0000629 print "Garbage left over:"
Martin v. Löwis89c528b2000-09-19 16:22:10 +0000630 if verbose:
631 print Node.allnodes.items()[0:10]
632 else:
633 # Don't print specific nodes if repeatable results
634 # are needed
635 print len(Node.allnodes)
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000636 Node.allnodes = {}
Fred Drakeacfb3f62001-02-01 18:11:29 +0000637 except:
638 failed.append(name)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000639 print "Test Failed: ", name
Fred Drake1703cf62000-12-15 21:31:59 +0000640 sys.stdout.flush()
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000641 traceback.print_exception(*sys.exc_info())
Fred Drakeacfb3f62001-02-01 18:11:29 +0000642 print `sys.exc_info()[1]`
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000643 Node.allnodes = {}
Paul Prescod10d27662000-09-18 19:07:26 +0000644
Fred Drakeacfb3f62001-02-01 18:11:29 +0000645if failed:
646 print "\n\n\n**** Check for failures in these tests:"
647 for name in failed:
648 print " " + name