blob: 52bd5c7d00a0a5defb232fc98caea9a4da4de447 [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
249def testWriteXML(): pass
250
251def testProcessingInstruction(): pass
252
253def testProcessingInstructionRepr(): pass
254
255def testTextRepr(): pass
256
257def testWriteText(): pass
258
259def testDocumentElement(): pass
260
261def testTooManyDocumentElements(): pass
262
263def testCreateElementNS(): pass
264
265def testCreatAttributeNS(): pass
266
267def testParse(): pass
268
269def testParseString(): pass
270
271def testComment(): pass
272
273def testAttrListItem(): pass
274
275def testAttrListItems(): pass
276
277def testAttrListItemNS(): pass
278
279def testAttrListKeys(): pass
280
281def testAttrListKeysNS(): pass
282
283def testAttrListValues(): pass
284
285def testAttrListLength(): pass
286
287def testAttrList__getitem__(): pass
288
289def testAttrList__setitem__(): pass
290
291def testSetAttrValueandNodeValue(): pass
292
293def testParseElement(): pass
294
295def testParseAttributes(): pass
296
297def testParseElementNamespaces(): pass
298
299def testParseAttributeNamespaces(): pass
300
301def testParseProcessingInstructions(): pass
302
303def testChildNodes(): pass
304
305def testFirstChild(): pass
306
307def testHasChildNodes(): pass
308
309def testCloneElementShallow(): pass
310
311def testCloneElementShallowCopiesAttributes(): pass
312
313def testCloneElementDeep(): pass
314
315def testCloneDocumentShallow(): pass
316
317def testCloneDocumentDeep(): pass
318
319def testCloneAttributeShallow(): pass
320
321def testCloneAttributeDeep(): pass
322
323def testClonePIShallow(): pass
324
325def testClonePIDeep(): pass
326
327
328names=globals().keys()
329names.sort()
Paul Prescod10d27662000-09-18 19:07:26 +0000330
331works=1
332
Paul Prescod7993bcc2000-07-01 14:54:16 +0000333for name in names:
334 if name.startswith( "test" ):
335 func=globals()[name]
336 try:
337 func()
338 print "Test Succeeded", name
339 if len( Node.allnodes ):
340 print "Garbage left over:"
Martin v. Löwis89c528b2000-09-19 16:22:10 +0000341 if verbose:
342 print Node.allnodes.items()[0:10]
343 else:
344 # Don't print specific nodes if repeatable results
345 # are needed
346 print len(Node.allnodes)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000347 Node.allnodes={}
348 except Exception, e :
Paul Prescod10d27662000-09-18 19:07:26 +0000349 works=0
Paul Prescod7993bcc2000-07-01 14:54:16 +0000350 print "Test Failed: ", name
351 apply( traceback.print_exception, sys.exc_info() )
352 print `e`
353 Node.allnodes={}
Paul Prescod10d27662000-09-18 19:07:26 +0000354
355if works:
356 print "All tests succeeded"
357else:
358 print "\n\n\n\n************ Check for failures!"
Paul Prescod7993bcc2000-07-01 14:54:16 +0000359
Guido van Rossum9e79a252000-09-21 20:10:39 +0000360Node.debug = None # Delete debug output collected in a StringIO object
361Node._debug = 0 # And reset debug mode