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