Bump version to 0.9.1.
[python/dscho.git] / Lib / test / test_minidom.py
blob73c8ac48e55adda4a9eb6020d07b30aeeda9dffd
1 # test for xml.dom.minidom
3 from xml.dom.minidom import parse, Node, Document, parseString
5 import os.path
6 import sys
7 import traceback
9 if __name__ == "__main__":
10 base = sys.argv[0]
11 else:
12 base = __file__
13 tstfile = os.path.join(os.path.dirname(base), "test.xml")
14 del base
16 Node._debug=1
18 def testGetElementsByTagName( ):
19 dom=parse( tstfile )
20 assert dom.getElementsByTagName( "LI" )==\
21 dom.documentElement.getElementsByTagName( "LI" )
22 dom.unlink()
23 dom=None
24 assert( len( Node.allnodes ))==0
26 def testInsertBefore( ):
27 dom=parse( tstfile )
28 docel=dom.documentElement
29 #docel.insertBefore( dom.createProcessingInstruction("a", "b"),
30 # docel.childNodes[1])
32 #docel.insertBefore( dom.createProcessingInstruction("a", "b"),
33 # docel.childNodes[0])
35 #assert docel.childNodes[0].target=="a"
36 #assert docel.childNodes[2].target=="a"
37 dom.unlink()
38 del dom
39 del docel
40 assert( len( Node.allnodes ))==0
42 def testAppendChild():
43 dom=parse( tstfile )
44 dom.documentElement.appendChild( dom.createComment( u"Hello" ))
45 assert dom.documentElement.childNodes[-1].nodeName=="#comment"
46 assert dom.documentElement.childNodes[-1].data=="Hello"
47 dom.unlink()
48 dom=None
49 assert( len( Node.allnodes ))==0
51 def testNonZero():
52 dom=parse( tstfile )
53 assert dom # should not be zero
54 dom.appendChild( dom.createComment( "foo" ) )
55 assert not dom.childNodes[-1].childNodes
56 dom.unlink()
57 dom=None
58 assert( len( Node.allnodes ))==0
60 def testUnlink():
61 dom=parse( tstfile )
62 dom.unlink()
63 dom=None
64 assert( len( Node.allnodes ))==0
66 def testElement():
67 dom=Document()
68 dom.appendChild( dom.createElement( "abc" ) )
69 assert dom.documentElement
70 dom.unlink()
71 dom=None
72 assert( len( Node.allnodes ))==0
74 def testAAA():
75 dom=parseString( "<abc/>" )
76 el=dom.documentElement
77 el.setAttribute( "spam", "jam2" )
78 dom.unlink()
79 dom=None
81 def testAAB():
82 dom=parseString( "<abc/>" )
83 el=dom.documentElement
84 el.setAttribute( "spam", "jam" )
85 el.setAttribute( "spam", "jam2" )
86 dom.unlink()
87 dom=None
89 def testAddAttr():
90 dom=Document()
91 child=dom.appendChild( dom.createElement( "abc" ) )
93 child.setAttribute( "def", "ghi" )
94 assert child.getAttribute( "def" )=="ghi"
95 assert child.attributes["def"].value=="ghi"
97 child.setAttribute( "jkl", "mno" )
98 assert child.getAttribute( "jkl" )=="mno"
99 assert child.attributes["jkl"].value=="mno"
101 assert len( child.attributes )==2
103 child.setAttribute( "def", "newval" )
104 assert child.getAttribute( "def" )=="newval"
105 assert child.attributes["def"].value=="newval"
107 assert len( child.attributes )==2
109 dom.unlink()
110 dom=None
111 child=None
113 def testDeleteAttr():
114 dom=Document()
115 child=dom.appendChild( dom.createElement( "abc" ) )
117 assert len( child.attributes)==0
118 child.setAttribute( "def", "ghi" )
119 assert len( child.attributes)==1
120 del child.attributes["def"]
121 assert len( child.attributes)==0
122 dom.unlink()
123 assert( len( Node.allnodes ))==0
125 def testRemoveAttr():
126 dom=Document()
127 child=dom.appendChild( dom.createElement( "abc" ) )
129 child.setAttribute( "def", "ghi" )
130 assert len( child.attributes)==1
131 child.removeAttribute("def" )
132 assert len( child.attributes)==0
134 dom.unlink()
136 def testRemoveAttrNS():
137 dom=Document()
138 child=dom.appendChild(
139 dom.createElementNS( "http://www.python.org", "python:abc" ) )
140 child.setAttributeNS( "http://www.w3.org", "xmlns:python",
141 "http://www.python.org" )
142 child.setAttributeNS( "http://www.python.org", "python:abcattr", "foo" )
143 assert len( child.attributes )==2
144 child.removeAttributeNS( "http://www.python.org", "abcattr" )
145 assert len( child.attributes )==1
147 dom.unlink()
148 dom=None
150 def testRemoveAttributeNode():
151 dom=Document()
152 child=dom.appendChild( dom.createElement( "foo" ) )
153 child.setAttribute( "spam", "jam" )
154 assert len( child.attributes )==1
155 node=child.getAttributeNode( "spam" )
156 child.removeAttributeNode( node )
157 assert len( child.attributes )==0
159 dom.unlink()
160 dom=None
161 assert len( Node.allnodes )==0
163 def testChangeAttr():
164 dom=parseString( "<abc/>" )
165 el=dom.documentElement
166 el.setAttribute( "spam", "jam" )
167 assert len( el.attributes )==1
168 el.setAttribute( "spam", "bam" )
169 assert len( el.attributes )==1
170 el.attributes["spam"]="ham"
171 assert len( el.attributes )==1
172 el.setAttribute( "spam2", "bam" )
173 assert len( el.attributes )==2
174 el.attributes[ "spam2"]= "bam2"
175 assert len( el.attributes )==2
176 dom.unlink()
177 dom=None
178 assert len( Node.allnodes )==0
180 def testGetAttrList():
181 pass
183 def testGetAttrValues(): pass
185 def testGetAttrLength(): pass
187 def testGetAttribute(): pass
189 def testGetAttributeNS(): pass
191 def testGetAttributeNode(): pass
193 def testGetElementsByTagNameNS(): pass
195 def testGetEmptyNodeListFromElementsByTagNameNS(): pass
197 def testElementReprAndStr():
198 dom=Document()
199 el=dom.appendChild( dom.createElement( "abc" ) )
200 string1=repr( el )
201 string2=str( el )
202 assert string1==string2
203 dom.unlink()
205 # commented out until Fredrick's fix is checked in
206 def _testElementReprAndStrUnicode():
207 dom=Document()
208 el=dom.appendChild( dom.createElement( u"abc" ) )
209 string1=repr( el )
210 string2=str( el )
211 assert string1==string2
212 dom.unlink()
214 # commented out until Fredrick's fix is checked in
215 def _testElementReprAndStrUnicodeNS():
216 dom=Document()
217 el=dom.appendChild(
218 dom.createElementNS( u"http://www.slashdot.org", u"slash:abc" ))
219 string1=repr( el )
220 string2=str( el )
221 assert string1==string2
222 assert string1.find("slash:abc" )!=-1
223 dom.unlink()
225 def testAttributeRepr():
226 dom=Document()
227 el=dom.appendChild( dom.createElement( u"abc" ) )
228 node=el.setAttribute( "abc", "def" )
229 assert str( node ) == repr( node )
230 dom.unlink()
232 def testTextNodeRepr(): pass
234 def testWriteXML(): pass
236 def testProcessingInstruction(): pass
238 def testProcessingInstructionRepr(): pass
240 def testTextRepr(): pass
242 def testWriteText(): pass
244 def testDocumentElement(): pass
246 def testTooManyDocumentElements(): pass
248 def testCreateElementNS(): pass
250 def testCreatAttributeNS(): pass
252 def testParse(): pass
254 def testParseString(): pass
256 def testComment(): pass
258 def testAttrListItem(): pass
260 def testAttrListItems(): pass
262 def testAttrListItemNS(): pass
264 def testAttrListKeys(): pass
266 def testAttrListKeysNS(): pass
268 def testAttrListValues(): pass
270 def testAttrListLength(): pass
272 def testAttrList__getitem__(): pass
274 def testAttrList__setitem__(): pass
276 def testSetAttrValueandNodeValue(): pass
278 def testParseElement(): pass
280 def testParseAttributes(): pass
282 def testParseElementNamespaces(): pass
284 def testParseAttributeNamespaces(): pass
286 def testParseProcessingInstructions(): pass
288 def testChildNodes(): pass
290 def testFirstChild(): pass
292 def testHasChildNodes(): pass
294 def testCloneElementShallow(): pass
296 def testCloneElementShallowCopiesAttributes(): pass
298 def testCloneElementDeep(): pass
300 def testCloneDocumentShallow(): pass
302 def testCloneDocumentDeep(): pass
304 def testCloneAttributeShallow(): pass
306 def testCloneAttributeDeep(): pass
308 def testClonePIShallow(): pass
310 def testClonePIDeep(): pass
313 names=globals().keys()
314 names.sort()
315 for name in names:
316 if name.startswith( "test" ):
317 func=globals()[name]
318 try:
319 func()
320 print "Test Succeeded", name
321 if len( Node.allnodes ):
322 print "Garbage left over:"
323 print Node.allnodes.items()[0:10]
324 Node.allnodes={}
325 except Exception, e :
326 print "Test Failed: ", name
327 apply( traceback.print_exception, sys.exc_info() )
328 print `e`
329 Node.allnodes={}
330 raise