blob: 796f6a055cf921febd1da23c83feb2e6528c5357 [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 Hyltond635b1d2000-09-26 17:32:27 +000018def confirm( test, testname="Test" ):
19 if test:
20 print "Passed " + testname
21 else:
22 print "Failed " + testname
23 raise Exception
24
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000025Node._debug=1
26
Jeremy Hyltond635b1d2000-09-26 17:32:27 +000027def testParseFromFile():
28 from StringIO import StringIO
29 dom=parse( StringIO( open( tstfile ).read() ) )
30 dom.unlink()
31 confirm(isinstance(dom,Document))
32
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000033def testGetElementsByTagName( ):
34 dom=parse( tstfile )
Jeremy Hyltond635b1d2000-09-26 17:32:27 +000035 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
39def testInsertBefore( ):
40 dom=parse( tstfile )
41 docel=dom.documentElement
42 #docel.insertBefore( dom.createProcessingInstruction("a", "b"),
43 # docel.childNodes[1])
44
45 #docel.insertBefore( dom.createProcessingInstruction("a", "b"),
46 # docel.childNodes[0])
47
Jeremy Hyltond635b1d2000-09-26 17:32:27 +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():
53 dom=parse( tstfile )
54 dom.documentElement.appendChild( dom.createComment( u"Hello" ))
Jeremy Hyltond635b1d2000-09-26 17:32:27 +000055 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():
60 dom=parse( tstfile )
Jeremy Hyltond635b1d2000-09-26 17:32:27 +000061 confirm( dom )# should not be zero
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000062 dom.appendChild( dom.createComment( "foo" ) )
Jeremy Hyltond635b1d2000-09-26 17:32:27 +000063 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():
67 dom=parse( tstfile )
68 dom.unlink()
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000069
70def testElement():
71 dom=Document()
72 dom.appendChild( dom.createElement( "abc" ) )
Jeremy Hyltond635b1d2000-09-26 17:32:27 +000073 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():
77 dom=parseString( "<abc/>" )
78 el=dom.documentElement
79 el.setAttribute( "spam", "jam2" )
80 dom.unlink()
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000081
82def testAAB():
83 dom=parseString( "<abc/>" )
84 el=dom.documentElement
85 el.setAttribute( "spam", "jam" )
86 el.setAttribute( "spam", "jam2" )
87 dom.unlink()
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000088
89def testAddAttr():
90 dom=Document()
91 child=dom.appendChild( dom.createElement( "abc" ) )
92
93 child.setAttribute( "def", "ghi" )
Jeremy Hyltond635b1d2000-09-26 17:32:27 +000094 confirm( child.getAttribute( "def" )=="ghi" )
95 confirm( child.attributes["def"].value=="ghi" )
Guido van Rossum6f8f92f2000-09-01 19:27:34 +000096
97 child.setAttribute( "jkl", "mno" )
Jeremy Hyltond635b1d2000-09-26 17:32:27 +000098 confirm( child.getAttribute( "jkl" )=="mno" )
99 confirm( child.attributes["jkl"].value=="mno" )
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000100
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000101 confirm( len( child.attributes )==2 )
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000102
103 child.setAttribute( "def", "newval" )
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000104 confirm( child.getAttribute( "def" )=="newval" )
105 confirm( child.attributes["def"].value=="newval" )
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000106
Jeremy Hyltond635b1d2000-09-26 17:32:27 +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():
111 dom=Document()
112 child=dom.appendChild( dom.createElement( "abc" ) )
113
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000114 confirm( len( child.attributes)==0 )
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000115 child.setAttribute( "def", "ghi" )
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000116 confirm( len( child.attributes)==1 )
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000117 del child.attributes["def"]
Jeremy Hyltond635b1d2000-09-26 17:32:27 +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():
122 dom=Document()
123 child=dom.appendChild( dom.createElement( "abc" ) )
124
125 child.setAttribute( "def", "ghi" )
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000126 confirm( len( child.attributes)==1 )
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000127 child.removeAttribute("def" )
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000128 confirm( len( child.attributes)==0 )
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000129
130 dom.unlink()
131
132def testRemoveAttrNS():
133 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" )
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000139 confirm( len( child.attributes )==2 )
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000140 child.removeAttributeNS( "http://www.python.org", "abcattr" )
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000141 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():
146 dom=Document()
147 child=dom.appendChild( dom.createElement( "foo" ) )
148 child.setAttribute( "spam", "jam" )
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000149 confirm( len( child.attributes )==1 )
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000150 node=child.getAttributeNode( "spam" )
151 child.removeAttributeNode( node )
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000152 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():
157 dom=parseString( "<abc/>" )
158 el=dom.documentElement
159 el.setAttribute( "spam", "jam" )
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000160 confirm( len( el.attributes )==1 )
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000161 el.setAttribute( "spam", "bam" )
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000162 confirm( len( el.attributes )==1 )
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000163 el.attributes["spam"]="ham"
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000164 confirm( len( el.attributes )==1 )
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000165 el.setAttribute( "spam2", "bam" )
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000166 confirm( len( el.attributes )==2 )
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000167 el.attributes[ "spam2"]= "bam2"
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000168 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():
189 dom=Document()
190 el=dom.appendChild( dom.createElement( "abc" ) )
191 string1=repr( el )
192 string2=str( el )
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000193 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():
198 dom=Document()
199 el=dom.appendChild( dom.createElement( u"abc" ) )
200 string1=repr( el )
201 string2=str( el )
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000202 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():
207 dom=Document()
208 el=dom.appendChild(
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000209 dom.createElementNS( u"http://www.slashdot.org", u"slash:abc" ))
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000210 string1=repr( el )
211 string2=str( el )
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000212 confirm( string1==string2 )
213 confirm( string1.find("slash:abc" )!=-1 )
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000214 dom.unlink()
Guido van Rossume8d2f552000-10-09 22:14:43 +0000215 confirm( len( Node.allnodes )==0 )
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000216
217def testAttributeRepr():
218 dom=Document()
219 el=dom.appendChild( dom.createElement( u"abc" ) )
220 node=el.setAttribute( "abc", "def" )
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000221 confirm( str( node ) == repr( node ) )
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000222 dom.unlink()
Guido van Rossume8d2f552000-10-09 22:14:43 +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)
233 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
311
312names=globals().keys()
313names.sort()
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000314
315works=1
316
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000317for name in names:
318 if name.startswith( "test" ):
319 func=globals()[name]
320 try:
321 func()
322 print "Test Succeeded", name
Guido van Rossume8d2f552000-10-09 22:14:43 +0000323 confirm(len(Node.allnodes) == 0,
324 "assertion: len(Node.allnodes) == 0")
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000325 if len( Node.allnodes ):
326 print "Garbage left over:"
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000327 if verbose:
328 print Node.allnodes.items()[0:10]
329 else:
330 # Don't print specific nodes if repeatable results
331 # are needed
332 print len(Node.allnodes)
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000333 Node.allnodes={}
334 except Exception, e :
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000335 works=0
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000336 print "Test Failed: ", name
337 apply( traceback.print_exception, sys.exc_info() )
338 print `e`
339 Node.allnodes={}
Guido van Rossum6f8f92f2000-09-01 19:27:34 +0000340
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000341if works:
342 print "All tests succeeded"
343else:
344 print "\n\n\n\n************ Check for failures!"
345
346Node.debug = None # Delete debug output collected in a StringIO object
347Node._debug = 0 # And reset debug mode