blob: d9fee5bca6233c1fa422cb8e04ba475552e6195e [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
Paul Prescod10d27662000-09-18 19:07:26 +000018def confirm( test, testname="Test" ):
19 if test:
20 print "Passed " + testname
21 else:
22 print "Failed " + testname
23 raise Exception
24
Paul Prescod7993bcc2000-07-01 14:54:16 +000025Node._debug=1
26
Paul Prescod10d27662000-09-18 19:07:26 +000027def testParseFromFile():
28 from StringIO import StringIO
29 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
Paul Prescod7993bcc2000-07-01 14:54:16 +000033def testGetElementsByTagName( ):
34 dom=parse( tstfile )
Paul Prescod10d27662000-09-18 19:07:26 +000035 confirm( dom.getElementsByTagName( "LI" )==\
36 dom.documentElement.getElementsByTagName( "LI" ) )
Paul Prescod7993bcc2000-07-01 14:54:16 +000037 dom.unlink()
38 dom=None
Paul Prescod10d27662000-09-18 19:07:26 +000039 confirm (len( Node.allnodes )==0)
Paul Prescod7993bcc2000-07-01 14:54:16 +000040
41def testInsertBefore( ):
42 dom=parse( tstfile )
43 docel=dom.documentElement
44 #docel.insertBefore( dom.createProcessingInstruction("a", "b"),
45 # docel.childNodes[1])
46
47 #docel.insertBefore( dom.createProcessingInstruction("a", "b"),
48 # docel.childNodes[0])
49
Paul Prescod10d27662000-09-18 19:07:26 +000050 #confirm( docel.childNodes[0].tet=="a" )
51 #confirm( docel.childNodes[2].tet=="a" )
Paul Prescod7993bcc2000-07-01 14:54:16 +000052 dom.unlink()
53 del dom
54 del docel
Paul Prescod10d27662000-09-18 19:07:26 +000055 confirm( len( Node.allnodes )==0)
Paul Prescod7993bcc2000-07-01 14:54:16 +000056
57def testAppendChild():
58 dom=parse( tstfile )
59 dom.documentElement.appendChild( dom.createComment( u"Hello" ))
Paul Prescod10d27662000-09-18 19:07:26 +000060 confirm( dom.documentElement.childNodes[-1].nodeName=="#comment" )
61 confirm( dom.documentElement.childNodes[-1].data=="Hello" )
Paul Prescod7993bcc2000-07-01 14:54:16 +000062 dom.unlink()
63 dom=None
Paul Prescod10d27662000-09-18 19:07:26 +000064 confirm( len( Node.allnodes )==0 )
Paul Prescod7993bcc2000-07-01 14:54:16 +000065
66def testNonZero():
67 dom=parse( tstfile )
Paul Prescod10d27662000-09-18 19:07:26 +000068 confirm( dom )# should not be zero
Paul Prescod7993bcc2000-07-01 14:54:16 +000069 dom.appendChild( dom.createComment( "foo" ) )
Paul Prescod10d27662000-09-18 19:07:26 +000070 confirm( not dom.childNodes[-1].childNodes )
Paul Prescod7993bcc2000-07-01 14:54:16 +000071 dom.unlink()
72 dom=None
Paul Prescod10d27662000-09-18 19:07:26 +000073 confirm( len( Node.allnodes )==0 )
Paul Prescod7993bcc2000-07-01 14:54:16 +000074
75def testUnlink():
76 dom=parse( tstfile )
77 dom.unlink()
78 dom=None
Paul Prescod10d27662000-09-18 19:07:26 +000079 confirm( len( Node.allnodes )==0 )
Paul Prescod7993bcc2000-07-01 14:54:16 +000080
81def testElement():
82 dom=Document()
83 dom.appendChild( dom.createElement( "abc" ) )
Paul Prescod10d27662000-09-18 19:07:26 +000084 confirm( dom.documentElement )
Paul Prescod7993bcc2000-07-01 14:54:16 +000085 dom.unlink()
86 dom=None
Paul Prescod10d27662000-09-18 19:07:26 +000087 confirm( len( Node.allnodes )==0 )
Paul Prescod7993bcc2000-07-01 14:54:16 +000088
89def testAAA():
90 dom=parseString( "<abc/>" )
91 el=dom.documentElement
92 el.setAttribute( "spam", "jam2" )
93 dom.unlink()
94 dom=None
95
96def testAAB():
97 dom=parseString( "<abc/>" )
98 el=dom.documentElement
99 el.setAttribute( "spam", "jam" )
100 el.setAttribute( "spam", "jam2" )
101 dom.unlink()
102 dom=None
103
104def testAddAttr():
105 dom=Document()
106 child=dom.appendChild( dom.createElement( "abc" ) )
107
108 child.setAttribute( "def", "ghi" )
Paul Prescod10d27662000-09-18 19:07:26 +0000109 confirm( child.getAttribute( "def" )=="ghi" )
110 confirm( child.attributes["def"].value=="ghi" )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000111
112 child.setAttribute( "jkl", "mno" )
Paul Prescod10d27662000-09-18 19:07:26 +0000113 confirm( child.getAttribute( "jkl" )=="mno" )
114 confirm( child.attributes["jkl"].value=="mno" )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000115
Paul Prescod10d27662000-09-18 19:07:26 +0000116 confirm( len( child.attributes )==2 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000117
118 child.setAttribute( "def", "newval" )
Paul Prescod10d27662000-09-18 19:07:26 +0000119 confirm( child.getAttribute( "def" )=="newval" )
120 confirm( child.attributes["def"].value=="newval" )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000121
Paul Prescod10d27662000-09-18 19:07:26 +0000122 confirm( len( child.attributes )==2 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000123
124 dom.unlink()
125 dom=None
126 child=None
127
128def testDeleteAttr():
129 dom=Document()
130 child=dom.appendChild( dom.createElement( "abc" ) )
131
Paul Prescod10d27662000-09-18 19:07:26 +0000132 confirm( len( child.attributes)==0 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000133 child.setAttribute( "def", "ghi" )
Paul Prescod10d27662000-09-18 19:07:26 +0000134 confirm( len( child.attributes)==1 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000135 del child.attributes["def"]
Paul Prescod10d27662000-09-18 19:07:26 +0000136 confirm( len( child.attributes)==0 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000137 dom.unlink()
Paul Prescod10d27662000-09-18 19:07:26 +0000138 confirm( len( Node.allnodes )==0 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000139
140def testRemoveAttr():
141 dom=Document()
142 child=dom.appendChild( dom.createElement( "abc" ) )
143
144 child.setAttribute( "def", "ghi" )
Paul Prescod10d27662000-09-18 19:07:26 +0000145 confirm( len( child.attributes)==1 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000146 child.removeAttribute("def" )
Paul Prescod10d27662000-09-18 19:07:26 +0000147 confirm( len( child.attributes)==0 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000148
149 dom.unlink()
150
151def testRemoveAttrNS():
152 dom=Document()
153 child=dom.appendChild(
154 dom.createElementNS( "http://www.python.org", "python:abc" ) )
155 child.setAttributeNS( "http://www.w3.org", "xmlns:python",
156 "http://www.python.org" )
157 child.setAttributeNS( "http://www.python.org", "python:abcattr", "foo" )
Paul Prescod10d27662000-09-18 19:07:26 +0000158 confirm( len( child.attributes )==2 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000159 child.removeAttributeNS( "http://www.python.org", "abcattr" )
Paul Prescod10d27662000-09-18 19:07:26 +0000160 confirm( len( child.attributes )==1 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000161
162 dom.unlink()
163 dom=None
164
165def testRemoveAttributeNode():
166 dom=Document()
167 child=dom.appendChild( dom.createElement( "foo" ) )
168 child.setAttribute( "spam", "jam" )
Paul Prescod10d27662000-09-18 19:07:26 +0000169 confirm( len( child.attributes )==1 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000170 node=child.getAttributeNode( "spam" )
171 child.removeAttributeNode( node )
Paul Prescod10d27662000-09-18 19:07:26 +0000172 confirm( len( child.attributes )==0 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000173
174 dom.unlink()
175 dom=None
Paul Prescod10d27662000-09-18 19:07:26 +0000176 confirm( len( Node.allnodes )==0 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000177
178def testChangeAttr():
179 dom=parseString( "<abc/>" )
180 el=dom.documentElement
181 el.setAttribute( "spam", "jam" )
Paul Prescod10d27662000-09-18 19:07:26 +0000182 confirm( len( el.attributes )==1 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000183 el.setAttribute( "spam", "bam" )
Paul Prescod10d27662000-09-18 19:07:26 +0000184 confirm( len( el.attributes )==1 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000185 el.attributes["spam"]="ham"
Paul Prescod10d27662000-09-18 19:07:26 +0000186 confirm( len( el.attributes )==1 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000187 el.setAttribute( "spam2", "bam" )
Paul Prescod10d27662000-09-18 19:07:26 +0000188 confirm( len( el.attributes )==2 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000189 el.attributes[ "spam2"]= "bam2"
Paul Prescod10d27662000-09-18 19:07:26 +0000190 confirm( len( el.attributes )==2 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000191 dom.unlink()
192 dom=None
Paul Prescod10d27662000-09-18 19:07:26 +0000193 confirm( len( Node.allnodes )==0 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000194
195def testGetAttrList():
196 pass
197
198def testGetAttrValues(): pass
199
200def testGetAttrLength(): pass
201
202def testGetAttribute(): pass
203
204def testGetAttributeNS(): pass
205
206def testGetAttributeNode(): pass
207
208def testGetElementsByTagNameNS(): pass
209
210def testGetEmptyNodeListFromElementsByTagNameNS(): pass
211
212def testElementReprAndStr():
213 dom=Document()
214 el=dom.appendChild( dom.createElement( "abc" ) )
215 string1=repr( el )
216 string2=str( el )
Paul Prescod10d27662000-09-18 19:07:26 +0000217 confirm( string1==string2 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000218 dom.unlink()
219
220# commented out until Fredrick's fix is checked in
221def _testElementReprAndStrUnicode():
222 dom=Document()
223 el=dom.appendChild( dom.createElement( u"abc" ) )
224 string1=repr( el )
225 string2=str( el )
Paul Prescod10d27662000-09-18 19:07:26 +0000226 confirm( string1==string2 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000227 dom.unlink()
228
229# commented out until Fredrick's fix is checked in
230def _testElementReprAndStrUnicodeNS():
231 dom=Document()
232 el=dom.appendChild(
Guido van Rossumc77593d2000-09-22 09:23:08 +0000233 dom.createElementNS( u"http://www.slashdot.org", u"slash:abc" ))
Paul Prescod7993bcc2000-07-01 14:54:16 +0000234 string1=repr( el )
235 string2=str( el )
Paul Prescod10d27662000-09-18 19:07:26 +0000236 confirm( string1==string2 )
237 confirm( string1.find("slash:abc" )!=-1 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000238 dom.unlink()
239
240def testAttributeRepr():
241 dom=Document()
242 el=dom.appendChild( dom.createElement( u"abc" ) )
243 node=el.setAttribute( "abc", "def" )
Paul Prescod10d27662000-09-18 19:07:26 +0000244 confirm( str( node ) == repr( node ) )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000245 dom.unlink()
246
247def testTextNodeRepr(): pass
248
Martin v. Löwis0a84a332000-10-06 22:42:55 +0000249def testWriteXML():
250 str = '<a b="c"/>'
251 dom = parseString(str)
252 domstr = dom.toxml()
253 dom.unlink()
254 confirm(str == domstr)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000255
256def testProcessingInstruction(): pass
257
258def testProcessingInstructionRepr(): pass
259
260def testTextRepr(): pass
261
262def testWriteText(): pass
263
264def testDocumentElement(): pass
265
266def testTooManyDocumentElements(): pass
267
268def testCreateElementNS(): pass
269
270def testCreatAttributeNS(): pass
271
272def testParse(): pass
273
274def testParseString(): pass
275
276def testComment(): pass
277
278def testAttrListItem(): pass
279
280def testAttrListItems(): pass
281
282def testAttrListItemNS(): pass
283
284def testAttrListKeys(): pass
285
286def testAttrListKeysNS(): pass
287
288def testAttrListValues(): pass
289
290def testAttrListLength(): pass
291
292def testAttrList__getitem__(): pass
293
294def testAttrList__setitem__(): pass
295
296def testSetAttrValueandNodeValue(): pass
297
298def testParseElement(): pass
299
300def testParseAttributes(): pass
301
302def testParseElementNamespaces(): pass
303
304def testParseAttributeNamespaces(): pass
305
306def testParseProcessingInstructions(): pass
307
308def testChildNodes(): pass
309
310def testFirstChild(): pass
311
312def testHasChildNodes(): pass
313
314def testCloneElementShallow(): pass
315
316def testCloneElementShallowCopiesAttributes(): pass
317
318def testCloneElementDeep(): pass
319
320def testCloneDocumentShallow(): pass
321
322def testCloneDocumentDeep(): pass
323
324def testCloneAttributeShallow(): pass
325
326def testCloneAttributeDeep(): pass
327
328def testClonePIShallow(): pass
329
330def testClonePIDeep(): pass
331
332
333names=globals().keys()
334names.sort()
Paul Prescod10d27662000-09-18 19:07:26 +0000335
336works=1
337
Paul Prescod7993bcc2000-07-01 14:54:16 +0000338for name in names:
339 if name.startswith( "test" ):
340 func=globals()[name]
341 try:
342 func()
343 print "Test Succeeded", name
344 if len( Node.allnodes ):
345 print "Garbage left over:"
Martin v. Löwis89c528b2000-09-19 16:22:10 +0000346 if verbose:
347 print Node.allnodes.items()[0:10]
348 else:
349 # Don't print specific nodes if repeatable results
350 # are needed
351 print len(Node.allnodes)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000352 Node.allnodes={}
353 except Exception, e :
Paul Prescod10d27662000-09-18 19:07:26 +0000354 works=0
Paul Prescod7993bcc2000-07-01 14:54:16 +0000355 print "Test Failed: ", name
356 apply( traceback.print_exception, sys.exc_info() )
357 print `e`
358 Node.allnodes={}
Paul Prescod10d27662000-09-18 19:07:26 +0000359
360if works:
361 print "All tests succeeded"
362else:
363 print "\n\n\n\n************ Check for failures!"
Paul Prescod7993bcc2000-07-01 14:54:16 +0000364
Guido van Rossum9e79a252000-09-21 20:10:39 +0000365Node.debug = None # Delete debug output collected in a StringIO object
366Node._debug = 0 # And reset debug mode