blob: 8a63535e2fc8dd9250460d8ac922189130499811 [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
4
5import os.path
6import sys
7import traceback
Martin v. Löwis89c528b2000-09-19 16:22:10 +00008from test_support import verbose
Fred Drake17647f52000-07-03 16:37:42 +00009
10if __name__ == "__main__":
11 base = sys.argv[0]
12else:
13 base = __file__
14tstfile = os.path.join(os.path.dirname(base), "test.xml")
15del base
Paul Prescod7993bcc2000-07-01 14:54:16 +000016
Paul Prescod10d27662000-09-18 19:07:26 +000017def confirm( test, testname="Test" ):
18 if test:
19 print "Passed " + testname
20 else:
21 print "Failed " + testname
22 raise Exception
23
Paul Prescod7993bcc2000-07-01 14:54:16 +000024Node._debug=1
25
Paul Prescod10d27662000-09-18 19:07:26 +000026def testParseFromFile():
27 from StringIO import StringIO
28 dom=parse( StringIO( open( tstfile ).read() ) )
Paul Prescod4c799192000-09-19 19:33:02 +000029 dom.unlink()
Martin v. Löwis89c528b2000-09-19 16:22:10 +000030 confirm(isinstance(dom,Document))
Paul Prescod10d27662000-09-18 19:07:26 +000031
Paul Prescod7993bcc2000-07-01 14:54:16 +000032def testGetElementsByTagName( ):
33 dom=parse( tstfile )
Paul Prescod10d27662000-09-18 19:07:26 +000034 confirm( dom.getElementsByTagName( "LI" )==\
35 dom.documentElement.getElementsByTagName( "LI" ) )
Paul Prescod7993bcc2000-07-01 14:54:16 +000036 dom.unlink()
37 dom=None
Paul Prescod10d27662000-09-18 19:07:26 +000038 confirm (len( Node.allnodes )==0)
Paul Prescod7993bcc2000-07-01 14:54:16 +000039
40def testInsertBefore( ):
41 dom=parse( tstfile )
42 docel=dom.documentElement
43 #docel.insertBefore( dom.createProcessingInstruction("a", "b"),
44 # docel.childNodes[1])
45
46 #docel.insertBefore( dom.createProcessingInstruction("a", "b"),
47 # docel.childNodes[0])
48
Paul Prescod10d27662000-09-18 19:07:26 +000049 #confirm( docel.childNodes[0].tet=="a" )
50 #confirm( docel.childNodes[2].tet=="a" )
Paul Prescod7993bcc2000-07-01 14:54:16 +000051 dom.unlink()
52 del dom
53 del docel
Paul Prescod10d27662000-09-18 19:07:26 +000054 confirm( len( Node.allnodes )==0)
Paul Prescod7993bcc2000-07-01 14:54:16 +000055
56def testAppendChild():
57 dom=parse( tstfile )
58 dom.documentElement.appendChild( dom.createComment( u"Hello" ))
Paul Prescod10d27662000-09-18 19:07:26 +000059 confirm( dom.documentElement.childNodes[-1].nodeName=="#comment" )
60 confirm( dom.documentElement.childNodes[-1].data=="Hello" )
Paul Prescod7993bcc2000-07-01 14:54:16 +000061 dom.unlink()
62 dom=None
Paul Prescod10d27662000-09-18 19:07:26 +000063 confirm( len( Node.allnodes )==0 )
Paul Prescod7993bcc2000-07-01 14:54:16 +000064
65def testNonZero():
66 dom=parse( tstfile )
Paul Prescod10d27662000-09-18 19:07:26 +000067 confirm( dom )# should not be zero
Paul Prescod7993bcc2000-07-01 14:54:16 +000068 dom.appendChild( dom.createComment( "foo" ) )
Paul Prescod10d27662000-09-18 19:07:26 +000069 confirm( not dom.childNodes[-1].childNodes )
Paul Prescod7993bcc2000-07-01 14:54:16 +000070 dom.unlink()
71 dom=None
Paul Prescod10d27662000-09-18 19:07:26 +000072 confirm( len( Node.allnodes )==0 )
Paul Prescod7993bcc2000-07-01 14:54:16 +000073
74def testUnlink():
75 dom=parse( tstfile )
76 dom.unlink()
77 dom=None
Paul Prescod10d27662000-09-18 19:07:26 +000078 confirm( len( Node.allnodes )==0 )
Paul Prescod7993bcc2000-07-01 14:54:16 +000079
80def testElement():
81 dom=Document()
82 dom.appendChild( dom.createElement( "abc" ) )
Paul Prescod10d27662000-09-18 19:07:26 +000083 confirm( dom.documentElement )
Paul Prescod7993bcc2000-07-01 14:54:16 +000084 dom.unlink()
85 dom=None
Paul Prescod10d27662000-09-18 19:07:26 +000086 confirm( len( Node.allnodes )==0 )
Paul Prescod7993bcc2000-07-01 14:54:16 +000087
88def testAAA():
89 dom=parseString( "<abc/>" )
90 el=dom.documentElement
91 el.setAttribute( "spam", "jam2" )
92 dom.unlink()
93 dom=None
94
95def testAAB():
96 dom=parseString( "<abc/>" )
97 el=dom.documentElement
98 el.setAttribute( "spam", "jam" )
99 el.setAttribute( "spam", "jam2" )
100 dom.unlink()
101 dom=None
102
103def testAddAttr():
104 dom=Document()
105 child=dom.appendChild( dom.createElement( "abc" ) )
106
107 child.setAttribute( "def", "ghi" )
Paul Prescod10d27662000-09-18 19:07:26 +0000108 confirm( child.getAttribute( "def" )=="ghi" )
109 confirm( child.attributes["def"].value=="ghi" )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000110
111 child.setAttribute( "jkl", "mno" )
Paul Prescod10d27662000-09-18 19:07:26 +0000112 confirm( child.getAttribute( "jkl" )=="mno" )
113 confirm( child.attributes["jkl"].value=="mno" )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000114
Paul Prescod10d27662000-09-18 19:07:26 +0000115 confirm( len( child.attributes )==2 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000116
117 child.setAttribute( "def", "newval" )
Paul Prescod10d27662000-09-18 19:07:26 +0000118 confirm( child.getAttribute( "def" )=="newval" )
119 confirm( child.attributes["def"].value=="newval" )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000120
Paul Prescod10d27662000-09-18 19:07:26 +0000121 confirm( len( child.attributes )==2 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000122
123 dom.unlink()
124 dom=None
125 child=None
126
127def testDeleteAttr():
128 dom=Document()
129 child=dom.appendChild( dom.createElement( "abc" ) )
130
Paul Prescod10d27662000-09-18 19:07:26 +0000131 confirm( len( child.attributes)==0 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000132 child.setAttribute( "def", "ghi" )
Paul Prescod10d27662000-09-18 19:07:26 +0000133 confirm( len( child.attributes)==1 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000134 del child.attributes["def"]
Paul Prescod10d27662000-09-18 19:07:26 +0000135 confirm( len( child.attributes)==0 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000136 dom.unlink()
Paul Prescod10d27662000-09-18 19:07:26 +0000137 confirm( len( Node.allnodes )==0 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000138
139def testRemoveAttr():
140 dom=Document()
141 child=dom.appendChild( dom.createElement( "abc" ) )
142
143 child.setAttribute( "def", "ghi" )
Paul Prescod10d27662000-09-18 19:07:26 +0000144 confirm( len( child.attributes)==1 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000145 child.removeAttribute("def" )
Paul Prescod10d27662000-09-18 19:07:26 +0000146 confirm( len( child.attributes)==0 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000147
148 dom.unlink()
149
150def testRemoveAttrNS():
151 dom=Document()
152 child=dom.appendChild(
153 dom.createElementNS( "http://www.python.org", "python:abc" ) )
154 child.setAttributeNS( "http://www.w3.org", "xmlns:python",
155 "http://www.python.org" )
156 child.setAttributeNS( "http://www.python.org", "python:abcattr", "foo" )
Paul Prescod10d27662000-09-18 19:07:26 +0000157 confirm( len( child.attributes )==2 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000158 child.removeAttributeNS( "http://www.python.org", "abcattr" )
Paul Prescod10d27662000-09-18 19:07:26 +0000159 confirm( len( child.attributes )==1 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000160
161 dom.unlink()
162 dom=None
163
164def testRemoveAttributeNode():
165 dom=Document()
166 child=dom.appendChild( dom.createElement( "foo" ) )
167 child.setAttribute( "spam", "jam" )
Paul Prescod10d27662000-09-18 19:07:26 +0000168 confirm( len( child.attributes )==1 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000169 node=child.getAttributeNode( "spam" )
170 child.removeAttributeNode( node )
Paul Prescod10d27662000-09-18 19:07:26 +0000171 confirm( len( child.attributes )==0 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000172
173 dom.unlink()
174 dom=None
Paul Prescod10d27662000-09-18 19:07:26 +0000175 confirm( len( Node.allnodes )==0 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000176
177def testChangeAttr():
178 dom=parseString( "<abc/>" )
179 el=dom.documentElement
180 el.setAttribute( "spam", "jam" )
Paul Prescod10d27662000-09-18 19:07:26 +0000181 confirm( len( el.attributes )==1 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000182 el.setAttribute( "spam", "bam" )
Paul Prescod10d27662000-09-18 19:07:26 +0000183 confirm( len( el.attributes )==1 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000184 el.attributes["spam"]="ham"
Paul Prescod10d27662000-09-18 19:07:26 +0000185 confirm( len( el.attributes )==1 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000186 el.setAttribute( "spam2", "bam" )
Paul Prescod10d27662000-09-18 19:07:26 +0000187 confirm( len( el.attributes )==2 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000188 el.attributes[ "spam2"]= "bam2"
Paul Prescod10d27662000-09-18 19:07:26 +0000189 confirm( len( el.attributes )==2 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000190 dom.unlink()
191 dom=None
Paul Prescod10d27662000-09-18 19:07:26 +0000192 confirm( len( Node.allnodes )==0 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000193
194def testGetAttrList():
195 pass
196
197def testGetAttrValues(): pass
198
199def testGetAttrLength(): pass
200
201def testGetAttribute(): pass
202
203def testGetAttributeNS(): pass
204
205def testGetAttributeNode(): pass
206
207def testGetElementsByTagNameNS(): pass
208
209def testGetEmptyNodeListFromElementsByTagNameNS(): pass
210
211def testElementReprAndStr():
212 dom=Document()
213 el=dom.appendChild( dom.createElement( "abc" ) )
214 string1=repr( el )
215 string2=str( el )
Paul Prescod10d27662000-09-18 19:07:26 +0000216 confirm( string1==string2 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000217 dom.unlink()
218
219# commented out until Fredrick's fix is checked in
220def _testElementReprAndStrUnicode():
221 dom=Document()
222 el=dom.appendChild( dom.createElement( u"abc" ) )
223 string1=repr( el )
224 string2=str( el )
Paul Prescod10d27662000-09-18 19:07:26 +0000225 confirm( string1==string2 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000226 dom.unlink()
227
228# commented out until Fredrick's fix is checked in
229def _testElementReprAndStrUnicodeNS():
230 dom=Document()
231 el=dom.appendChild(
Guido van Rossumc77593d2000-09-22 09:23:08 +0000232 dom.createElementNS( u"http://www.slashdot.org", u"slash:abc" ))
Paul Prescod7993bcc2000-07-01 14:54:16 +0000233 string1=repr( el )
234 string2=str( el )
Paul Prescod10d27662000-09-18 19:07:26 +0000235 confirm( string1==string2 )
236 confirm( string1.find("slash:abc" )!=-1 )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000237 dom.unlink()
238
239def testAttributeRepr():
240 dom=Document()
241 el=dom.appendChild( dom.createElement( u"abc" ) )
242 node=el.setAttribute( "abc", "def" )
Paul Prescod10d27662000-09-18 19:07:26 +0000243 confirm( str( node ) == repr( node ) )
Paul Prescod7993bcc2000-07-01 14:54:16 +0000244 dom.unlink()
245
246def testTextNodeRepr(): pass
247
248def testWriteXML(): pass
249
250def testProcessingInstruction(): pass
251
252def testProcessingInstructionRepr(): pass
253
254def testTextRepr(): pass
255
256def testWriteText(): pass
257
258def testDocumentElement(): pass
259
260def testTooManyDocumentElements(): pass
261
262def testCreateElementNS(): pass
263
264def testCreatAttributeNS(): pass
265
266def testParse(): pass
267
268def testParseString(): pass
269
270def testComment(): pass
271
272def testAttrListItem(): pass
273
274def testAttrListItems(): pass
275
276def testAttrListItemNS(): pass
277
278def testAttrListKeys(): pass
279
280def testAttrListKeysNS(): pass
281
282def testAttrListValues(): pass
283
284def testAttrListLength(): pass
285
286def testAttrList__getitem__(): pass
287
288def testAttrList__setitem__(): pass
289
290def testSetAttrValueandNodeValue(): pass
291
292def testParseElement(): pass
293
294def testParseAttributes(): pass
295
296def testParseElementNamespaces(): pass
297
298def testParseAttributeNamespaces(): pass
299
300def testParseProcessingInstructions(): pass
301
302def testChildNodes(): pass
303
304def testFirstChild(): pass
305
306def testHasChildNodes(): pass
307
308def testCloneElementShallow(): pass
309
310def testCloneElementShallowCopiesAttributes(): pass
311
312def testCloneElementDeep(): pass
313
314def testCloneDocumentShallow(): pass
315
316def testCloneDocumentDeep(): pass
317
318def testCloneAttributeShallow(): pass
319
320def testCloneAttributeDeep(): pass
321
322def testClonePIShallow(): pass
323
324def testClonePIDeep(): pass
325
326
327names=globals().keys()
328names.sort()
Paul Prescod10d27662000-09-18 19:07:26 +0000329
330works=1
331
Paul Prescod7993bcc2000-07-01 14:54:16 +0000332for name in names:
333 if name.startswith( "test" ):
334 func=globals()[name]
335 try:
336 func()
337 print "Test Succeeded", name
338 if len( Node.allnodes ):
339 print "Garbage left over:"
Martin v. Löwis89c528b2000-09-19 16:22:10 +0000340 if verbose:
341 print Node.allnodes.items()[0:10]
342 else:
343 # Don't print specific nodes if repeatable results
344 # are needed
345 print len(Node.allnodes)
Paul Prescod7993bcc2000-07-01 14:54:16 +0000346 Node.allnodes={}
347 except Exception, e :
Paul Prescod10d27662000-09-18 19:07:26 +0000348 works=0
Paul Prescod7993bcc2000-07-01 14:54:16 +0000349 print "Test Failed: ", name
350 apply( traceback.print_exception, sys.exc_info() )
351 print `e`
352 Node.allnodes={}
Paul Prescod10d27662000-09-18 19:07:26 +0000353
354if works:
355 print "All tests succeeded"
356else:
357 print "\n\n\n\n************ Check for failures!"
Paul Prescod7993bcc2000-07-01 14:54:16 +0000358
Guido van Rossum9e79a252000-09-21 20:10:39 +0000359Node.debug = None # Delete debug output collected in a StringIO object
360Node._debug = 0 # And reset debug mode