1 # test for xml.dom.minidom
3 from xml
.dom
.minidom
import parse
, Node
, Document
, parseString
9 if __name__
== "__main__":
13 tstfile
= os
.path
.join(os
.path
.dirname(base
), "test.xml")
18 def testGetElementsByTagName( ):
20 assert dom
.getElementsByTagName( "LI" )==\
21 dom
.documentElement
.getElementsByTagName( "LI" )
24 assert( len( Node
.allnodes
))==0
26 def testInsertBefore( ):
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"
40 assert( len( Node
.allnodes
))==0
42 def testAppendChild():
44 dom
.documentElement
.appendChild( dom
.createComment( u
"Hello" ))
45 assert dom
.documentElement
.childNodes
[-1].nodeName
=="#comment"
46 assert dom
.documentElement
.childNodes
[-1].data
=="Hello"
49 assert( len( Node
.allnodes
))==0
53 assert dom
# should not be zero
54 dom
.appendChild( dom
.createComment( "foo" ) )
55 assert not dom
.childNodes
[-1].childNodes
58 assert( len( Node
.allnodes
))==0
64 assert( len( Node
.allnodes
))==0
68 dom
.appendChild( dom
.createElement( "abc" ) )
69 assert dom
.documentElement
72 assert( len( Node
.allnodes
))==0
75 dom
=parseString( "<abc/>" )
76 el
=dom
.documentElement
77 el
.setAttribute( "spam", "jam2" )
82 dom
=parseString( "<abc/>" )
83 el
=dom
.documentElement
84 el
.setAttribute( "spam", "jam" )
85 el
.setAttribute( "spam", "jam2" )
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
113 def testDeleteAttr():
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
123 assert( len( Node
.allnodes
))==0
125 def testRemoveAttr():
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
136 def testRemoveAttrNS():
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
150 def testRemoveAttributeNode():
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
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
178 assert len( Node
.allnodes
)==0
180 def testGetAttrList():
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():
199 el
=dom
.appendChild( dom
.createElement( "abc" ) )
202 assert string1
==string2
205 # commented out until Fredrick's fix is checked in
206 def _testElementReprAndStrUnicode():
208 el
=dom
.appendChild( dom
.createElement( u
"abc" ) )
211 assert string1
==string2
214 # commented out until Fredrick's fix is checked in
215 def _testElementReprAndStrUnicodeNS():
218 dom
.createElementNS( u
"http://www.slashdot.org", u
"slash:abc" ))
221 assert string1
==string2
222 assert string1
.find("slash:abc" )!=-1
225 def testAttributeRepr():
227 el
=dom
.appendChild( dom
.createElement( u
"abc" ) )
228 node
=el
.setAttribute( "abc", "def" )
229 assert str( node
) == repr( node
)
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()
316 if name
.startswith( "test" ):
320 print "Test Succeeded", name
321 if len( Node
.allnodes
):
322 print "Garbage left over:"
323 print Node
.allnodes
.items()[0:10]
325 except Exception, e
:
326 print "Test Failed: ", name
327 apply( traceback
.print_exception
, sys
.exc_info() )