blob: ce7170d8129801bda6b39dbcb8285b25910fa69c [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
Barry Warsawc79dff62000-09-26 18:00:20 +00004import xml.parsers.expat
Fred Drake17647f52000-07-03 16:37:42 +00005
6import os.path
7import sys
8import traceback
Martin v. Löwis89c528b2000-09-19 16:22:10 +00009from test_support import verbose
Fred Drake17647f52000-07-03 16:37:42 +000010
11if __name__ == "__main__":
12 base = sys.argv[0]
13else:
14 base = __file__
15tstfile = os.path.join(os.path.dirname(base), "test.xml")
16del base
Paul Prescod7993bcc2000-07-01 14:54:16 +000017
Jeremy Hylton3b0c6002000-10-12 17:31:36 +000018def confirm(test, testname = "Test"):
Fred Drake004d5e62000-10-23 17:22:08 +000019 if test:
Paul Prescod10d27662000-09-18 19:07:26 +000020 print "Passed " + testname
Fred Drake004d5e62000-10-23 17:22:08 +000021 else:
Paul Prescod10d27662000-09-18 19:07:26 +000022 print "Failed " + testname
23 raise Exception
24
Jeremy Hylton3b0c6002000-10-12 17:31:36 +000025Node._debug = 1
Paul Prescod7993bcc2000-07-01 14:54:16 +000026
Paul Prescod10d27662000-09-18 19:07:26 +000027def testParseFromFile():
28 from StringIO import StringIO
Jeremy Hylton3b0c6002000-10-12 17:31:36 +000029 dom = parse(StringIO(open(tstfile).read()))
Paul Prescod4c799192000-09-19 19:33:02 +000030 dom.unlink()
Martin v. Löwis89c528b2000-09-19 16:22:10 +000031 confirm(isinstance(dom,Document))
Paul Prescod10d27662000-09-18 19:07:26 +000032
Jeremy Hylton3b0c6002000-10-12 17:31:36 +000033def testGetElementsByTagName():
34 dom = parse(tstfile)
35 confirm(dom.getElementsByTagName("LI") == \
36 dom.documentElement.getElementsByTagName("LI"))
Paul Prescod7993bcc2000-07-01 14:54:16 +000037 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +000038
Jeremy Hylton3b0c6002000-10-12 17:31:36 +000039def testInsertBefore():
40 dom = parse(tstfile)
41 docel = dom.documentElement
Paul Prescod7993bcc2000-07-01 14:54:16 +000042 #docel.insertBefore( dom.createProcessingInstruction("a", "b"),
43 # docel.childNodes[1])
Fred Drake004d5e62000-10-23 17:22:08 +000044
Paul Prescod7993bcc2000-07-01 14:54:16 +000045 #docel.insertBefore( dom.createProcessingInstruction("a", "b"),
46 # docel.childNodes[0])
47
Jeremy Hylton3b0c6002000-10-12 17:31:36 +000048 #confirm( docel.childNodes[0].tet == "a")
49 #confirm( docel.childNodes[2].tet == "a")
Paul Prescod7993bcc2000-07-01 14:54:16 +000050 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +000051
52def testAppendChild():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +000053 dom = parse(tstfile)
54 dom.documentElement.appendChild(dom.createComment(u"Hello"))
55 confirm(dom.documentElement.childNodes[-1].nodeName == "#comment")
56 confirm(dom.documentElement.childNodes[-1].data == "Hello")
Paul Prescod7993bcc2000-07-01 14:54:16 +000057 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +000058
59def testNonZero():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +000060 dom = parse(tstfile)
61 confirm(dom)# should not be zero
62 dom.appendChild(dom.createComment("foo"))
63 confirm(not dom.childNodes[-1].childNodes)
Paul Prescod7993bcc2000-07-01 14:54:16 +000064 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +000065
66def testUnlink():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +000067 dom = parse(tstfile)
Paul Prescod7993bcc2000-07-01 14:54:16 +000068 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +000069
70def testElement():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +000071 dom = Document()
72 dom.appendChild(dom.createElement("abc"))
73 confirm(dom.documentElement)
Paul Prescod7993bcc2000-07-01 14:54:16 +000074 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +000075
76def testAAA():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +000077 dom = parseString("<abc/>")
78 el = dom.documentElement
79 el.setAttribute("spam", "jam2")
Paul Prescod7993bcc2000-07-01 14:54:16 +000080 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +000081
82def testAAB():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +000083 dom = parseString("<abc/>")
84 el = dom.documentElement
85 el.setAttribute("spam", "jam")
86 el.setAttribute("spam", "jam2")
Paul Prescod7993bcc2000-07-01 14:54:16 +000087 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +000088
89def testAddAttr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +000090 dom = Document()
91 child = dom.appendChild(dom.createElement("abc"))
Paul Prescod7993bcc2000-07-01 14:54:16 +000092
Jeremy Hylton3b0c6002000-10-12 17:31:36 +000093 child.setAttribute("def", "ghi")
94 confirm(child.getAttribute("def") == "ghi")
95 confirm(child.attributes["def"].value == "ghi")
Paul Prescod7993bcc2000-07-01 14:54:16 +000096
Jeremy Hylton3b0c6002000-10-12 17:31:36 +000097 child.setAttribute("jkl", "mno")
98 confirm(child.getAttribute("jkl") == "mno")
99 confirm(child.attributes["jkl"].value == "mno")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000100
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000101 confirm(len(child.attributes) == 2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000102
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000103 child.setAttribute("def", "newval")
104 confirm(child.getAttribute("def") == "newval")
105 confirm(child.attributes["def"].value == "newval")
Paul Prescod7993bcc2000-07-01 14:54:16 +0000106
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000107 confirm(len(child.attributes) == 2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000108 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000109
110def testDeleteAttr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000111 dom = Document()
112 child = dom.appendChild(dom.createElement("abc"))
Paul Prescod7993bcc2000-07-01 14:54:16 +0000113
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000114 confirm(len(child.attributes) == 0)
115 child.setAttribute("def", "ghi")
116 confirm(len(child.attributes) == 1)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000117 del child.attributes["def"]
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000118 confirm(len(child.attributes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000119 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000120
121def testRemoveAttr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000122 dom = Document()
123 child = dom.appendChild(dom.createElement("abc"))
Paul Prescod7993bcc2000-07-01 14:54:16 +0000124
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000125 child.setAttribute("def", "ghi")
126 confirm(len(child.attributes) == 1)
127 child.removeAttribute("def")
128 confirm(len(child.attributes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000129
130 dom.unlink()
131
132def testRemoveAttrNS():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000133 dom = Document()
134 child = dom.appendChild(
135 dom.createElementNS("http://www.python.org", "python:abc"))
Fred Drake004d5e62000-10-23 17:22:08 +0000136 child.setAttributeNS("http://www.w3.org", "xmlns:python",
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000137 "http://www.python.org")
138 child.setAttributeNS("http://www.python.org", "python:abcattr", "foo")
139 confirm(len(child.attributes) == 2)
140 child.removeAttributeNS("http://www.python.org", "abcattr")
141 confirm(len(child.attributes) == 1)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000142
143 dom.unlink()
Fred Drake004d5e62000-10-23 17:22:08 +0000144
Paul Prescod7993bcc2000-07-01 14:54:16 +0000145def testRemoveAttributeNode():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000146 dom = Document()
147 child = dom.appendChild(dom.createElement("foo"))
148 child.setAttribute("spam", "jam")
149 confirm(len(child.attributes) == 1)
150 node = child.getAttributeNode("spam")
151 child.removeAttributeNode(node)
152 confirm(len(child.attributes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000153
154 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000155
156def testChangeAttr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000157 dom = parseString("<abc/>")
158 el = dom.documentElement
159 el.setAttribute("spam", "jam")
160 confirm(len(el.attributes) == 1)
161 el.setAttribute("spam", "bam")
162 confirm(len(el.attributes) == 1)
163 el.attributes["spam"] = "ham"
164 confirm(len(el.attributes) == 1)
165 el.setAttribute("spam2", "bam")
166 confirm(len(el.attributes) == 2)
167 el.attributes[ "spam2"] = "bam2"
168 confirm(len(el.attributes) == 2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000169 dom.unlink()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000170
171def testGetAttrList():
172 pass
173
174def testGetAttrValues(): pass
175
176def testGetAttrLength(): pass
177
178def testGetAttribute(): pass
179
180def testGetAttributeNS(): pass
181
182def testGetAttributeNode(): pass
183
184def testGetElementsByTagNameNS(): pass
185
186def testGetEmptyNodeListFromElementsByTagNameNS(): pass
187
188def testElementReprAndStr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000189 dom = Document()
190 el = dom.appendChild(dom.createElement("abc"))
191 string1 = repr(el)
192 string2 = str(el)
193 confirm(string1 == string2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000194 dom.unlink()
195
196# commented out until Fredrick's fix is checked in
197def _testElementReprAndStrUnicode():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000198 dom = Document()
199 el = dom.appendChild(dom.createElement(u"abc"))
200 string1 = repr(el)
201 string2 = str(el)
202 confirm(string1 == string2)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000203 dom.unlink()
204
205# commented out until Fredrick's fix is checked in
206def _testElementReprAndStrUnicodeNS():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000207 dom = Document()
208 el = dom.appendChild(
209 dom.createElementNS(u"http://www.slashdot.org", u"slash:abc"))
210 string1 = repr(el)
211 string2 = str(el)
212 confirm(string1 == string2)
213 confirm(string1.find("slash:abc") != -1)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000214 dom.unlink()
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000215 confirm(len(Node.allnodes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000216
217def testAttributeRepr():
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000218 dom = Document()
219 el = dom.appendChild(dom.createElement(u"abc"))
220 node = el.setAttribute("abc", "def")
221 confirm(str(node) == repr(node))
Paul Prescod7993bcc2000-07-01 14:54:16 +0000222 dom.unlink()
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000223 confirm(len(Node.allnodes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000224
225def testTextNodeRepr(): pass
226
Martin v. Löwis0a84a332000-10-06 22:42:55 +0000227def testWriteXML():
228 str = '<a b="c"/>'
229 dom = parseString(str)
230 domstr = dom.toxml()
231 dom.unlink()
232 confirm(str == domstr)
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000233 confirm(len(Node.allnodes) == 0)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000234
235def testProcessingInstruction(): pass
236
237def testProcessingInstructionRepr(): pass
238
239def testTextRepr(): pass
240
241def testWriteText(): pass
242
243def testDocumentElement(): pass
244
245def testTooManyDocumentElements(): pass
246
247def testCreateElementNS(): pass
248
249def testCreatAttributeNS(): pass
250
251def testParse(): pass
252
253def testParseString(): pass
254
255def testComment(): pass
256
257def testAttrListItem(): pass
258
259def testAttrListItems(): pass
260
261def testAttrListItemNS(): pass
262
263def testAttrListKeys(): pass
264
265def testAttrListKeysNS(): pass
266
267def testAttrListValues(): pass
268
269def testAttrListLength(): pass
270
271def testAttrList__getitem__(): pass
272
273def testAttrList__setitem__(): pass
274
275def testSetAttrValueandNodeValue(): pass
276
277def testParseElement(): pass
278
279def testParseAttributes(): pass
280
281def testParseElementNamespaces(): pass
282
283def testParseAttributeNamespaces(): pass
284
285def testParseProcessingInstructions(): pass
286
287def testChildNodes(): pass
288
289def testFirstChild(): pass
290
291def testHasChildNodes(): pass
292
293def testCloneElementShallow(): pass
294
295def testCloneElementShallowCopiesAttributes(): pass
296
297def testCloneElementDeep(): pass
298
299def testCloneDocumentShallow(): pass
300
301def testCloneDocumentDeep(): pass
302
303def testCloneAttributeShallow(): pass
304
305def testCloneAttributeDeep(): pass
306
307def testClonePIShallow(): pass
308
309def testClonePIDeep(): pass
310
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000311def testSiblings():
312 doc = parseString("<doc><?pi?>text?<elm/></doc>")
313 root = doc.documentElement
314 (pi, text, elm) = root.childNodes
Paul Prescod7993bcc2000-07-01 14:54:16 +0000315
Fred Drake004d5e62000-10-23 17:22:08 +0000316 confirm(pi.nextSibling is text and
317 pi.previousSibling is None and
318 text.nextSibling is elm and
319 text.previousSibling is pi and
320 elm.nextSibling is None and
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000321 elm.previousSibling is text, "testSiblings")
322
323 doc.unlink()
324
325def testParents():
326 doc = parseString("<doc><elm1><elm2/><elm2><elm3/></elm2></elm1></doc>")
327 root = doc.documentElement
328 elm1 = root.childNodes[0]
329 (elm2a, elm2b) = elm1.childNodes
330 elm3 = elm2b.childNodes[0]
331
332 confirm(root.parentNode is doc and
333 elm1.parentNode is root and
334 elm2a.parentNode is elm1 and
335 elm2b.parentNode is elm1 and
336 elm3.parentNode is elm2b, "testParents")
337
338 doc.unlink()
339
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000340def testSAX2DOM():
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000341 from xml.dom import pulldom
342
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000343 sax2dom = pulldom.SAX2DOM()
344 sax2dom.startDocument()
345 sax2dom.startElement("doc", {})
346 sax2dom.characters("text")
347 sax2dom.startElement("subelm", {})
348 sax2dom.characters("text")
349 sax2dom.endElement("subelm")
Fred Drake004d5e62000-10-23 17:22:08 +0000350 sax2dom.characters("text")
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000351 sax2dom.endElement("doc")
352 sax2dom.endDocument()
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000353
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000354 doc = sax2dom.document
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000355 root = doc.documentElement
356 (text1, elm1, text2) = root.childNodes
357 text3 = elm1.childNodes[0]
358
359 confirm(text1.previousSibling is None and
360 text1.nextSibling is elm1 and
361 elm1.previousSibling is text1 and
362 elm1.nextSibling is text2 and
363 text2.previousSibling is elm1 and
364 text2.nextSibling is None and
365 text3.previousSibling is None and
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000366 text3.nextSibling is None, "testSAX2DOM - siblings")
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000367
368 confirm(root.parentNode is doc and
369 text1.parentNode is root and
370 elm1.parentNode is root and
371 text2.parentNode is root and
Lars Gustäbel5bad5a42000-10-13 20:54:10 +0000372 text3.parentNode is elm1, "testSAX2DOM - parents")
Fred Drake004d5e62000-10-23 17:22:08 +0000373
Lars Gustäbelf27f5ab2000-10-11 22:36:00 +0000374 doc.unlink()
375
376# --- MAIN PROGRAM
Fred Drake004d5e62000-10-23 17:22:08 +0000377
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000378names = globals().keys()
Paul Prescod7993bcc2000-07-01 14:54:16 +0000379names.sort()
Paul Prescod10d27662000-09-18 19:07:26 +0000380
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000381works = 1
Paul Prescod10d27662000-09-18 19:07:26 +0000382
Paul Prescod7993bcc2000-07-01 14:54:16 +0000383for name in names:
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000384 if name.startswith("test"):
385 func = globals()[name]
Paul Prescod7993bcc2000-07-01 14:54:16 +0000386 try:
387 func()
388 print "Test Succeeded", name
Fred Drakeebe73022000-10-09 19:57:39 +0000389 confirm(len(Node.allnodes) == 0,
390 "assertion: len(Node.allnodes) == 0")
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000391 if len(Node.allnodes):
Paul Prescod7993bcc2000-07-01 14:54:16 +0000392 print "Garbage left over:"
Martin v. Löwis89c528b2000-09-19 16:22:10 +0000393 if verbose:
394 print Node.allnodes.items()[0:10]
395 else:
396 # Don't print specific nodes if repeatable results
397 # are needed
398 print len(Node.allnodes)
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000399 Node.allnodes = {}
400 except Exception, e:
401 works = 0
Paul Prescod7993bcc2000-07-01 14:54:16 +0000402 print "Test Failed: ", name
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000403 traceback.print_exception(*sys.exc_info())
Paul Prescod7993bcc2000-07-01 14:54:16 +0000404 print `e`
Jeremy Hylton3b0c6002000-10-12 17:31:36 +0000405 Node.allnodes = {}
Paul Prescod10d27662000-09-18 19:07:26 +0000406
407if works:
408 print "All tests succeeded"
409else:
410 print "\n\n\n\n************ Check for failures!"
Paul Prescod7993bcc2000-07-01 14:54:16 +0000411
Guido van Rossum9e79a252000-09-21 20:10:39 +0000412Node.debug = None # Delete debug output collected in a StringIO object
413Node._debug = 0 # And reset debug mode