Fred Drake | 17647f5 | 2000-07-03 16:37:42 +0000 | [diff] [blame] | 1 | # test for xml.dom.minidom |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 2 | |
Fred Drake | 17647f5 | 2000-07-03 16:37:42 +0000 | [diff] [blame] | 3 | from xml.dom.minidom import parse, Node, Document, parseString |
| 4 | |
| 5 | import os.path |
| 6 | import sys |
| 7 | import traceback |
| 8 | |
| 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 |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 15 | |
| 16 | Node._debug=1 |
| 17 | |
| 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 |
| 25 | |
| 26 | def testInsertBefore( ): |
| 27 | dom=parse( tstfile ) |
| 28 | docel=dom.documentElement |
| 29 | #docel.insertBefore( dom.createProcessingInstruction("a", "b"), |
| 30 | # docel.childNodes[1]) |
| 31 | |
| 32 | #docel.insertBefore( dom.createProcessingInstruction("a", "b"), |
| 33 | # docel.childNodes[0]) |
| 34 | |
| 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 |
| 41 | |
| 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 |
| 50 | |
| 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 |
| 59 | |
| 60 | def testUnlink(): |
| 61 | dom=parse( tstfile ) |
| 62 | dom.unlink() |
| 63 | dom=None |
| 64 | assert( len( Node.allnodes ))==0 |
| 65 | |
| 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 |
| 73 | |
| 74 | def testAAA(): |
| 75 | dom=parseString( "<abc/>" ) |
| 76 | el=dom.documentElement |
| 77 | el.setAttribute( "spam", "jam2" ) |
| 78 | dom.unlink() |
| 79 | dom=None |
| 80 | |
| 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 |
| 88 | |
| 89 | def testAddAttr(): |
| 90 | dom=Document() |
| 91 | child=dom.appendChild( dom.createElement( "abc" ) ) |
| 92 | |
| 93 | child.setAttribute( "def", "ghi" ) |
| 94 | assert child.getAttribute( "def" )=="ghi" |
| 95 | assert child.attributes["def"].value=="ghi" |
| 96 | |
| 97 | child.setAttribute( "jkl", "mno" ) |
| 98 | assert child.getAttribute( "jkl" )=="mno" |
| 99 | assert child.attributes["jkl"].value=="mno" |
| 100 | |
| 101 | assert len( child.attributes )==2 |
| 102 | |
| 103 | child.setAttribute( "def", "newval" ) |
| 104 | assert child.getAttribute( "def" )=="newval" |
| 105 | assert child.attributes["def"].value=="newval" |
| 106 | |
| 107 | assert len( child.attributes )==2 |
| 108 | |
| 109 | dom.unlink() |
| 110 | dom=None |
| 111 | child=None |
| 112 | |
| 113 | def testDeleteAttr(): |
| 114 | dom=Document() |
| 115 | child=dom.appendChild( dom.createElement( "abc" ) ) |
| 116 | |
| 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 |
| 124 | |
| 125 | def testRemoveAttr(): |
| 126 | dom=Document() |
| 127 | child=dom.appendChild( dom.createElement( "abc" ) ) |
| 128 | |
| 129 | child.setAttribute( "def", "ghi" ) |
| 130 | assert len( child.attributes)==1 |
| 131 | child.removeAttribute("def" ) |
| 132 | assert len( child.attributes)==0 |
| 133 | |
| 134 | dom.unlink() |
| 135 | |
| 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 |
| 146 | |
| 147 | dom.unlink() |
| 148 | dom=None |
| 149 | |
| 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 |
| 158 | |
| 159 | dom.unlink() |
| 160 | dom=None |
| 161 | assert len( Node.allnodes )==0 |
| 162 | |
| 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 |
| 179 | |
| 180 | def testGetAttrList(): |
| 181 | pass |
| 182 | |
| 183 | def testGetAttrValues(): pass |
| 184 | |
| 185 | def testGetAttrLength(): pass |
| 186 | |
| 187 | def testGetAttribute(): pass |
| 188 | |
| 189 | def testGetAttributeNS(): pass |
| 190 | |
| 191 | def testGetAttributeNode(): pass |
| 192 | |
| 193 | def testGetElementsByTagNameNS(): pass |
| 194 | |
| 195 | def testGetEmptyNodeListFromElementsByTagNameNS(): pass |
| 196 | |
| 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() |
| 204 | |
| 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() |
| 213 | |
| 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() |
| 224 | |
| 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() |
| 231 | |
| 232 | def testTextNodeRepr(): pass |
| 233 | |
| 234 | def testWriteXML(): pass |
| 235 | |
| 236 | def testProcessingInstruction(): pass |
| 237 | |
| 238 | def testProcessingInstructionRepr(): pass |
| 239 | |
| 240 | def testTextRepr(): pass |
| 241 | |
| 242 | def testWriteText(): pass |
| 243 | |
| 244 | def testDocumentElement(): pass |
| 245 | |
| 246 | def testTooManyDocumentElements(): pass |
| 247 | |
| 248 | def testCreateElementNS(): pass |
| 249 | |
| 250 | def testCreatAttributeNS(): pass |
| 251 | |
| 252 | def testParse(): pass |
| 253 | |
| 254 | def testParseString(): pass |
| 255 | |
| 256 | def testComment(): pass |
| 257 | |
| 258 | def testAttrListItem(): pass |
| 259 | |
| 260 | def testAttrListItems(): pass |
| 261 | |
| 262 | def testAttrListItemNS(): pass |
| 263 | |
| 264 | def testAttrListKeys(): pass |
| 265 | |
| 266 | def testAttrListKeysNS(): pass |
| 267 | |
| 268 | def testAttrListValues(): pass |
| 269 | |
| 270 | def testAttrListLength(): pass |
| 271 | |
| 272 | def testAttrList__getitem__(): pass |
| 273 | |
| 274 | def testAttrList__setitem__(): pass |
| 275 | |
| 276 | def testSetAttrValueandNodeValue(): pass |
| 277 | |
| 278 | def testParseElement(): pass |
| 279 | |
| 280 | def testParseAttributes(): pass |
| 281 | |
| 282 | def testParseElementNamespaces(): pass |
| 283 | |
| 284 | def testParseAttributeNamespaces(): pass |
| 285 | |
| 286 | def testParseProcessingInstructions(): pass |
| 287 | |
| 288 | def testChildNodes(): pass |
| 289 | |
| 290 | def testFirstChild(): pass |
| 291 | |
| 292 | def testHasChildNodes(): pass |
| 293 | |
| 294 | def testCloneElementShallow(): pass |
| 295 | |
| 296 | def testCloneElementShallowCopiesAttributes(): pass |
| 297 | |
| 298 | def testCloneElementDeep(): pass |
| 299 | |
| 300 | def testCloneDocumentShallow(): pass |
| 301 | |
| 302 | def testCloneDocumentDeep(): pass |
| 303 | |
| 304 | def testCloneAttributeShallow(): pass |
| 305 | |
| 306 | def testCloneAttributeDeep(): pass |
| 307 | |
| 308 | def testClonePIShallow(): pass |
| 309 | |
| 310 | def testClonePIDeep(): pass |
| 311 | |
| 312 | |
| 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 |
| 331 | |