blob: 7afdf5d197d7c0d723d7a6da0d685289ce79e140 [file] [log] [blame]
Guido van Rossum6f8f92f2000-09-01 19:27:34 +00001# test for xml.dom.minidom
2
3from xml.dom.minidom import parse, Node, Document, parseString
Guido van Rossume8d2f552000-10-09 22:14:43 +00004import xml.parsers.expat
Guido van Rossum6f8f92f2000-09-01 19:27:34 +00005
6import os.path
7import sys
8import traceback
Jeremy Hyltond635b1d2000-09-26 17:32:27 +00009from test_support import verbose
Guido van Rossum6f8f92f2000-09-01 19:27:34 +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
17
Jeremy Hyltona8268e92000-10-16 17:33:50 +000018def confirm(test, testname = "Test"):
Jeremy Hyltond635b1d2000-09-26 17:32:27 +000019 if test:
20 print "Passed " + testname
21 else:
22 print "Failed " + testname
23 raise Exception
24
Jeremy Hyltona8268e92000-10-16 17:33:50 +000025Node._debug = 1
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000026
Jeremy Hyltond635b1d2000-09-26 17:32:27 +000027def testParseFromFile():
28 from StringIO import StringIO
Jeremy Hyltona8268e92000-10-16 17:33:50 +000029 dom = parse(StringIO(open(tstfile).read()))
Jeremy Hyltond635b1d2000-09-26 17:32:27 +000030 dom.unlink()
31 confirm(isinstance(dom,Document))
32
Jeremy Hyltona8268e92000-10-16 17:33:50 +000033def testGetElementsByTagName():
34 dom = parse(tstfile)
35 confirm(dom.getElementsByTagName("LI") == \
36 dom.documentElement.getElementsByTagName("LI"))
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000037 dom.unlink()
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000038
Jeremy Hyltona8268e92000-10-16 17:33:50 +000039def testInsertBefore():
40 dom = parse(tstfile)
41 docel = dom.documentElement
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000042 #docel.insertBefore( dom.createProcessingInstruction("a", "b"),
43 # docel.childNodes[1])
44
45 #docel.insertBefore( dom.createProcessingInstruction("a", "b"),
46 # docel.childNodes[0])
47
Jeremy Hyltona8268e92000-10-16 17:33:50 +000048 #confirm( docel.childNodes[0].tet == "a")
49 #confirm( docel.childNodes[2].tet == "a")
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000050 dom.unlink()
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000051
52def testAppendChild():
Jeremy Hyltona8268e92000-10-16 17:33:50 +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")
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000057 dom.unlink()
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000058
59def testNonZero():
Jeremy Hyltona8268e92000-10-16 17:33:50 +000060 dom = parse(tstfile)
61 confirm(dom)# should not be zero
62 dom.appendChild(dom.createComment("foo"))
63 confirm(not dom.childNodes[-1].childNodes)
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000064 dom.unlink()
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000065
66def testUnlink():
Jeremy Hyltona8268e92000-10-16 17:33:50 +000067 dom = parse(tstfile)
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000068 dom.unlink()
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000069
70def testElement():
Jeremy Hyltona8268e92000-10-16 17:33:50 +000071 dom = Document()
72 dom.appendChild(dom.createElement("abc"))
73 confirm(dom.documentElement)
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000074 dom.unlink()
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000075
76def testAAA():
Jeremy Hyltona8268e92000-10-16 17:33:50 +000077 dom = parseString("<abc/>")
78 el = dom.documentElement
79 el.setAttribute("spam", "jam2")
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000080 dom.unlink()
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000081
82def testAAB():
Jeremy Hyltona8268e92000-10-16 17:33:50 +000083 dom = parseString("<abc/>")
84 el = dom.documentElement
85 el.setAttribute("spam", "jam")
86 el.setAttribute("spam", "jam2")
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000087 dom.unlink()
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000088
89def testAddAttr():
Jeremy Hyltona8268e92000-10-16 17:33:50 +000090 dom = Document()
91 child = dom.appendChild(dom.createElement("abc"))
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000092
Jeremy Hyltona8268e92000-10-16 17:33:50 +000093 child.setAttribute("def", "ghi")
94 confirm(child.getAttribute("def") == "ghi")
95 confirm(child.attributes["def"].value == "ghi")
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000096
Jeremy Hyltona8268e92000-10-16 17:33:50 +000097 child.setAttribute("jkl", "mno")
98 confirm(child.getAttribute("jkl") == "mno")
99 confirm(child.attributes["jkl"].value == "mno")
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000100
Jeremy Hyltona8268e92000-10-16 17:33:50 +0000101 confirm(len(child.attributes) == 2)
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000102
Jeremy Hyltona8268e92000-10-16 17:33:50 +0000103 child.setAttribute("def", "newval")
104 confirm(child.getAttribute("def") == "newval")
105 confirm(child.attributes["def"].value == "newval")
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000106
Jeremy Hyltona8268e92000-10-16 17:33:50 +0000107 confirm(len(child.attributes) == 2)
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000108 dom.unlink()
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000109
110def testDeleteAttr():
Jeremy Hyltona8268e92000-10-16 17:33:50 +0000111 dom = Document()
112 child = dom.appendChild(dom.createElement("abc"))
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000113
Jeremy Hyltona8268e92000-10-16 17:33:50 +0000114 confirm(len(child.attributes) == 0)
115 child.setAttribute("def", "ghi")
116 confirm(len(child.attributes) == 1)
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000117 del child.attributes["def"]
Jeremy Hyltona8268e92000-10-16 17:33:50 +0000118 confirm(len(child.attributes) == 0)
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000119 dom.unlink()
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000120
121def testRemoveAttr():
Jeremy Hyltona8268e92000-10-16 17:33:50 +0000122 dom = Document()
123 child = dom.appendChild(dom.createElement("abc"))
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000124
Jeremy Hyltona8268e92000-10-16 17:33:50 +0000125 child.setAttribute("def", "ghi")
126 confirm(len(child.attributes) == 1)
127 child.removeAttribute("def")
128 confirm(len(child.attributes) == 0)
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000129
130 dom.unlink()
131
132def testRemoveAttrNS():
Jeremy Hyltona8268e92000-10-16 17:33:50 +0000133 dom = Document()
134 child = dom.appendChild(
135 dom.createElementNS("http://www.python.org", "python:abc"))
136 child.setAttributeNS("http://www.w3.org", "xmlns:python",
137 "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)
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000142
143 dom.unlink()
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000144
145def testRemoveAttributeNode():
Jeremy Hyltona8268e92000-10-16 17:33:50 +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)
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000153
154 dom.unlink()
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000155
156def testChangeAttr():
Jeremy Hyltona8268e92000-10-16 17:33:50 +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)
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000169 dom.unlink()
Guido van Rossum6f8f92f2000-09-01 19:27:34 +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 Hyltona8268e92000-10-16 17:33:50 +0000189 dom = Document()
190 el = dom.appendChild(dom.createElement("abc"))
191 string1 = repr(el)
192 string2 = str(el)
193 confirm(string1 == string2)
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000194 dom.unlink()
195
196# commented out until Fredrick's fix is checked in
197def _testElementReprAndStrUnicode():
Jeremy Hyltona8268e92000-10-16 17:33:50 +0000198 dom = Document()
199 el = dom.appendChild(dom.createElement(u"abc"))
200 string1 = repr(el)
201 string2 = str(el)
202 confirm(string1 == string2)
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000203 dom.unlink()
204
205# commented out until Fredrick's fix is checked in
206def _testElementReprAndStrUnicodeNS():
Jeremy Hyltona8268e92000-10-16 17:33:50 +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)
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000214 dom.unlink()
Jeremy Hyltona8268e92000-10-16 17:33:50 +0000215 confirm(len(Node.allnodes) == 0)
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000216
217def testAttributeRepr():
Jeremy Hyltona8268e92000-10-16 17:33:50 +0000218 dom = Document()
219 el = dom.appendChild(dom.createElement(u"abc"))
220 node = el.setAttribute("abc", "def")
221 confirm(str(node) == repr(node))
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000222 dom.unlink()
Jeremy Hyltona8268e92000-10-16 17:33:50 +0000223 confirm(len(Node.allnodes) == 0)
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000224
225def testTextNodeRepr(): pass
226
Guido van Rossume8d2f552000-10-09 22:14:43 +0000227def testWriteXML():
228 str = '<a b="c"/>'
229 dom = parseString(str)
230 domstr = dom.toxml()
231 dom.unlink()
232 confirm(str == domstr)
Jeremy Hyltona8268e92000-10-16 17:33:50 +0000233 confirm(len(Node.allnodes) == 0)
Guido van Rossum6f8f92f2000-09-01 19:27:34 +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
Jeremy Hyltona8268e92000-10-16 17:33:50 +0000311def testSiblings():
312 doc = parseString("<doc><?pi?>text?<elm/></doc>")
313 root = doc.documentElement
314 (pi, text, elm) = root.childNodes
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000315
Jeremy Hyltona8268e92000-10-16 17:33:50 +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
321 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
340def testSAX2DOM():
341 from xml.dom import pulldom
342
343 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")
350 sax2dom.characters("text")
351 sax2dom.endElement("doc")
352 sax2dom.endDocument()
353
354 doc = sax2dom.document
355 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
366 text3.nextSibling is None, "testSAX2DOM - siblings")
367
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
372 text3.parentNode is elm1, "testSAX2DOM - parents")
373
374 doc.unlink()
375
376# --- MAIN PROGRAM
377
378names = globals().keys()
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000379names.sort()
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000380
Jeremy Hyltona8268e92000-10-16 17:33:50 +0000381works = 1
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000382
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000383for name in names:
Jeremy Hyltona8268e92000-10-16 17:33:50 +0000384 if name.startswith("test"):
385 func = globals()[name]
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000386 try:
387 func()
388 print "Test Succeeded", name
Guido van Rossume8d2f552000-10-09 22:14:43 +0000389 confirm(len(Node.allnodes) == 0,
390 "assertion: len(Node.allnodes) == 0")
Jeremy Hyltona8268e92000-10-16 17:33:50 +0000391 if len(Node.allnodes):
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000392 print "Garbage left over:"
Jeremy Hyltond635b1d2000-09-26 17:32:27 +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 Hyltona8268e92000-10-16 17:33:50 +0000399 Node.allnodes = {}
400 except Exception, e:
401 works = 0
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000402 print "Test Failed: ", name
Jeremy Hyltona8268e92000-10-16 17:33:50 +0000403 traceback.print_exception(*sys.exc_info())
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000404 print `e`
Jeremy Hyltona8268e92000-10-16 17:33:50 +0000405 Node.allnodes = {}
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000406
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000407if works:
408 print "All tests succeeded"
409else:
410 print "\n\n\n\n************ Check for failures!"
411
412Node.debug = None # Delete debug output collected in a StringIO object
413Node._debug = 0 # And reset debug mode