Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 1 | # test for xml.dom.minidom |
| 2 | |
| 3 | from xml.dom.minidom import parse, Node, Document, parseString |
Guido van Rossum | e8d2f55 | 2000-10-09 22:14:43 +0000 | [diff] [blame^] | 4 | import xml.parsers.expat |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 5 | |
| 6 | import os.path |
| 7 | import sys |
| 8 | import traceback |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 9 | from test_support import verbose |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 10 | |
| 11 | if __name__ == "__main__": |
| 12 | base = sys.argv[0] |
| 13 | else: |
| 14 | base = __file__ |
| 15 | tstfile = os.path.join(os.path.dirname(base), "test.xml") |
| 16 | del base |
| 17 | |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 18 | def confirm( test, testname="Test" ): |
| 19 | if test: |
| 20 | print "Passed " + testname |
| 21 | else: |
| 22 | print "Failed " + testname |
| 23 | raise Exception |
| 24 | |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 25 | Node._debug=1 |
| 26 | |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 27 | def testParseFromFile(): |
| 28 | from StringIO import StringIO |
| 29 | dom=parse( StringIO( open( tstfile ).read() ) ) |
| 30 | dom.unlink() |
| 31 | confirm(isinstance(dom,Document)) |
| 32 | |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 33 | def testGetElementsByTagName( ): |
| 34 | dom=parse( tstfile ) |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 35 | confirm( dom.getElementsByTagName( "LI" )==\ |
| 36 | dom.documentElement.getElementsByTagName( "LI" ) ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 37 | dom.unlink() |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 38 | |
| 39 | def 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 Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 48 | #confirm( docel.childNodes[0].tet=="a" ) |
| 49 | #confirm( docel.childNodes[2].tet=="a" ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 50 | dom.unlink() |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 51 | |
| 52 | def testAppendChild(): |
| 53 | dom=parse( tstfile ) |
| 54 | dom.documentElement.appendChild( dom.createComment( u"Hello" )) |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 55 | confirm( dom.documentElement.childNodes[-1].nodeName=="#comment" ) |
| 56 | confirm( dom.documentElement.childNodes[-1].data=="Hello" ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 57 | dom.unlink() |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 58 | |
| 59 | def testNonZero(): |
| 60 | dom=parse( tstfile ) |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 61 | confirm( dom )# should not be zero |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 62 | dom.appendChild( dom.createComment( "foo" ) ) |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 63 | confirm( not dom.childNodes[-1].childNodes ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 64 | dom.unlink() |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 65 | |
| 66 | def testUnlink(): |
| 67 | dom=parse( tstfile ) |
| 68 | dom.unlink() |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 69 | |
| 70 | def testElement(): |
| 71 | dom=Document() |
| 72 | dom.appendChild( dom.createElement( "abc" ) ) |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 73 | confirm( dom.documentElement ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 74 | dom.unlink() |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 75 | |
| 76 | def testAAA(): |
| 77 | dom=parseString( "<abc/>" ) |
| 78 | el=dom.documentElement |
| 79 | el.setAttribute( "spam", "jam2" ) |
| 80 | dom.unlink() |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 81 | |
| 82 | def testAAB(): |
| 83 | dom=parseString( "<abc/>" ) |
| 84 | el=dom.documentElement |
| 85 | el.setAttribute( "spam", "jam" ) |
| 86 | el.setAttribute( "spam", "jam2" ) |
| 87 | dom.unlink() |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 88 | |
| 89 | def testAddAttr(): |
| 90 | dom=Document() |
| 91 | child=dom.appendChild( dom.createElement( "abc" ) ) |
| 92 | |
| 93 | child.setAttribute( "def", "ghi" ) |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 94 | confirm( child.getAttribute( "def" )=="ghi" ) |
| 95 | confirm( child.attributes["def"].value=="ghi" ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 96 | |
| 97 | child.setAttribute( "jkl", "mno" ) |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 98 | confirm( child.getAttribute( "jkl" )=="mno" ) |
| 99 | confirm( child.attributes["jkl"].value=="mno" ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 100 | |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 101 | confirm( len( child.attributes )==2 ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 102 | |
| 103 | child.setAttribute( "def", "newval" ) |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 104 | confirm( child.getAttribute( "def" )=="newval" ) |
| 105 | confirm( child.attributes["def"].value=="newval" ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 106 | |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 107 | confirm( len( child.attributes )==2 ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 108 | dom.unlink() |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 109 | |
| 110 | def testDeleteAttr(): |
| 111 | dom=Document() |
| 112 | child=dom.appendChild( dom.createElement( "abc" ) ) |
| 113 | |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 114 | confirm( len( child.attributes)==0 ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 115 | child.setAttribute( "def", "ghi" ) |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 116 | confirm( len( child.attributes)==1 ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 117 | del child.attributes["def"] |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 118 | confirm( len( child.attributes)==0 ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 119 | dom.unlink() |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 120 | |
| 121 | def testRemoveAttr(): |
| 122 | dom=Document() |
| 123 | child=dom.appendChild( dom.createElement( "abc" ) ) |
| 124 | |
| 125 | child.setAttribute( "def", "ghi" ) |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 126 | confirm( len( child.attributes)==1 ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 127 | child.removeAttribute("def" ) |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 128 | confirm( len( child.attributes)==0 ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 129 | |
| 130 | dom.unlink() |
| 131 | |
| 132 | def 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 Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 139 | confirm( len( child.attributes )==2 ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 140 | child.removeAttributeNS( "http://www.python.org", "abcattr" ) |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 141 | confirm( len( child.attributes )==1 ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 142 | |
| 143 | dom.unlink() |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 144 | |
| 145 | def testRemoveAttributeNode(): |
| 146 | dom=Document() |
| 147 | child=dom.appendChild( dom.createElement( "foo" ) ) |
| 148 | child.setAttribute( "spam", "jam" ) |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 149 | confirm( len( child.attributes )==1 ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 150 | node=child.getAttributeNode( "spam" ) |
| 151 | child.removeAttributeNode( node ) |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 152 | confirm( len( child.attributes )==0 ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 153 | |
| 154 | dom.unlink() |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 155 | |
| 156 | def testChangeAttr(): |
| 157 | dom=parseString( "<abc/>" ) |
| 158 | el=dom.documentElement |
| 159 | el.setAttribute( "spam", "jam" ) |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 160 | confirm( len( el.attributes )==1 ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 161 | el.setAttribute( "spam", "bam" ) |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 162 | confirm( len( el.attributes )==1 ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 163 | el.attributes["spam"]="ham" |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 164 | confirm( len( el.attributes )==1 ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 165 | el.setAttribute( "spam2", "bam" ) |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 166 | confirm( len( el.attributes )==2 ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 167 | el.attributes[ "spam2"]= "bam2" |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 168 | confirm( len( el.attributes )==2 ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 169 | dom.unlink() |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 170 | |
| 171 | def testGetAttrList(): |
| 172 | pass |
| 173 | |
| 174 | def testGetAttrValues(): pass |
| 175 | |
| 176 | def testGetAttrLength(): pass |
| 177 | |
| 178 | def testGetAttribute(): pass |
| 179 | |
| 180 | def testGetAttributeNS(): pass |
| 181 | |
| 182 | def testGetAttributeNode(): pass |
| 183 | |
| 184 | def testGetElementsByTagNameNS(): pass |
| 185 | |
| 186 | def testGetEmptyNodeListFromElementsByTagNameNS(): pass |
| 187 | |
| 188 | def testElementReprAndStr(): |
| 189 | dom=Document() |
| 190 | el=dom.appendChild( dom.createElement( "abc" ) ) |
| 191 | string1=repr( el ) |
| 192 | string2=str( el ) |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 193 | confirm( string1==string2 ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 194 | dom.unlink() |
| 195 | |
| 196 | # commented out until Fredrick's fix is checked in |
| 197 | def _testElementReprAndStrUnicode(): |
| 198 | dom=Document() |
| 199 | el=dom.appendChild( dom.createElement( u"abc" ) ) |
| 200 | string1=repr( el ) |
| 201 | string2=str( el ) |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 202 | confirm( string1==string2 ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 203 | dom.unlink() |
| 204 | |
| 205 | # commented out until Fredrick's fix is checked in |
| 206 | def _testElementReprAndStrUnicodeNS(): |
| 207 | dom=Document() |
| 208 | el=dom.appendChild( |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 209 | dom.createElementNS( u"http://www.slashdot.org", u"slash:abc" )) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 210 | string1=repr( el ) |
| 211 | string2=str( el ) |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 212 | confirm( string1==string2 ) |
| 213 | confirm( string1.find("slash:abc" )!=-1 ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 214 | dom.unlink() |
Guido van Rossum | e8d2f55 | 2000-10-09 22:14:43 +0000 | [diff] [blame^] | 215 | confirm( len( Node.allnodes )==0 ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 216 | |
| 217 | def testAttributeRepr(): |
| 218 | dom=Document() |
| 219 | el=dom.appendChild( dom.createElement( u"abc" ) ) |
| 220 | node=el.setAttribute( "abc", "def" ) |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 221 | confirm( str( node ) == repr( node ) ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 222 | dom.unlink() |
Guido van Rossum | e8d2f55 | 2000-10-09 22:14:43 +0000 | [diff] [blame^] | 223 | confirm( len( Node.allnodes )==0 ) |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 224 | |
| 225 | def testTextNodeRepr(): pass |
| 226 | |
Guido van Rossum | e8d2f55 | 2000-10-09 22:14:43 +0000 | [diff] [blame^] | 227 | def 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 Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 234 | |
| 235 | def testProcessingInstruction(): pass |
| 236 | |
| 237 | def testProcessingInstructionRepr(): pass |
| 238 | |
| 239 | def testTextRepr(): pass |
| 240 | |
| 241 | def testWriteText(): pass |
| 242 | |
| 243 | def testDocumentElement(): pass |
| 244 | |
| 245 | def testTooManyDocumentElements(): pass |
| 246 | |
| 247 | def testCreateElementNS(): pass |
| 248 | |
| 249 | def testCreatAttributeNS(): pass |
| 250 | |
| 251 | def testParse(): pass |
| 252 | |
| 253 | def testParseString(): pass |
| 254 | |
| 255 | def testComment(): pass |
| 256 | |
| 257 | def testAttrListItem(): pass |
| 258 | |
| 259 | def testAttrListItems(): pass |
| 260 | |
| 261 | def testAttrListItemNS(): pass |
| 262 | |
| 263 | def testAttrListKeys(): pass |
| 264 | |
| 265 | def testAttrListKeysNS(): pass |
| 266 | |
| 267 | def testAttrListValues(): pass |
| 268 | |
| 269 | def testAttrListLength(): pass |
| 270 | |
| 271 | def testAttrList__getitem__(): pass |
| 272 | |
| 273 | def testAttrList__setitem__(): pass |
| 274 | |
| 275 | def testSetAttrValueandNodeValue(): pass |
| 276 | |
| 277 | def testParseElement(): pass |
| 278 | |
| 279 | def testParseAttributes(): pass |
| 280 | |
| 281 | def testParseElementNamespaces(): pass |
| 282 | |
| 283 | def testParseAttributeNamespaces(): pass |
| 284 | |
| 285 | def testParseProcessingInstructions(): pass |
| 286 | |
| 287 | def testChildNodes(): pass |
| 288 | |
| 289 | def testFirstChild(): pass |
| 290 | |
| 291 | def testHasChildNodes(): pass |
| 292 | |
| 293 | def testCloneElementShallow(): pass |
| 294 | |
| 295 | def testCloneElementShallowCopiesAttributes(): pass |
| 296 | |
| 297 | def testCloneElementDeep(): pass |
| 298 | |
| 299 | def testCloneDocumentShallow(): pass |
| 300 | |
| 301 | def testCloneDocumentDeep(): pass |
| 302 | |
| 303 | def testCloneAttributeShallow(): pass |
| 304 | |
| 305 | def testCloneAttributeDeep(): pass |
| 306 | |
| 307 | def testClonePIShallow(): pass |
| 308 | |
| 309 | def testClonePIDeep(): pass |
| 310 | |
| 311 | |
| 312 | names=globals().keys() |
| 313 | names.sort() |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 314 | |
| 315 | works=1 |
| 316 | |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 317 | for name in names: |
| 318 | if name.startswith( "test" ): |
| 319 | func=globals()[name] |
| 320 | try: |
| 321 | func() |
| 322 | print "Test Succeeded", name |
Guido van Rossum | e8d2f55 | 2000-10-09 22:14:43 +0000 | [diff] [blame^] | 323 | confirm(len(Node.allnodes) == 0, |
| 324 | "assertion: len(Node.allnodes) == 0") |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 325 | if len( Node.allnodes ): |
| 326 | print "Garbage left over:" |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 327 | 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 Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 333 | Node.allnodes={} |
| 334 | except Exception, e : |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 335 | works=0 |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 336 | print "Test Failed: ", name |
| 337 | apply( traceback.print_exception, sys.exc_info() ) |
| 338 | print `e` |
| 339 | Node.allnodes={} |
Guido van Rossum | 6f8f92f | 2000-09-01 19:27:34 +0000 | [diff] [blame] | 340 | |
Jeremy Hylton | d635b1d | 2000-09-26 17:32:27 +0000 | [diff] [blame] | 341 | if works: |
| 342 | print "All tests succeeded" |
| 343 | else: |
| 344 | print "\n\n\n\n************ Check for failures!" |
| 345 | |
| 346 | Node.debug = None # Delete debug output collected in a StringIO object |
| 347 | Node._debug = 0 # And reset debug mode |