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 |
Barry Warsaw | c79dff6 | 2000-09-26 18:00:20 +0000 | [diff] [blame] | 4 | import xml.parsers.expat |
Fred Drake | 17647f5 | 2000-07-03 16:37:42 +0000 | [diff] [blame] | 5 | |
| 6 | import os.path |
| 7 | import sys |
| 8 | import traceback |
Martin v. Löwis | 89c528b | 2000-09-19 16:22:10 +0000 | [diff] [blame] | 9 | from test_support import verbose |
Fred Drake | 17647f5 | 2000-07-03 16:37:42 +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 |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 17 | |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 18 | def confirm(test, testname = "Test"): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 19 | if test: |
Paul Prescod | 10d2766 | 2000-09-18 19:07:26 +0000 | [diff] [blame] | 20 | print "Passed " + testname |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 21 | else: |
Paul Prescod | 10d2766 | 2000-09-18 19:07:26 +0000 | [diff] [blame] | 22 | print "Failed " + testname |
| 23 | raise Exception |
| 24 | |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 25 | Node._debug = 1 |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 26 | |
Paul Prescod | 10d2766 | 2000-09-18 19:07:26 +0000 | [diff] [blame] | 27 | def testParseFromFile(): |
| 28 | from StringIO import StringIO |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 29 | dom = parse(StringIO(open(tstfile).read())) |
Paul Prescod | 4c79919 | 2000-09-19 19:33:02 +0000 | [diff] [blame] | 30 | dom.unlink() |
Martin v. Löwis | 89c528b | 2000-09-19 16:22:10 +0000 | [diff] [blame] | 31 | confirm(isinstance(dom,Document)) |
Paul Prescod | 10d2766 | 2000-09-18 19:07:26 +0000 | [diff] [blame] | 32 | |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 33 | def testGetElementsByTagName(): |
| 34 | dom = parse(tstfile) |
| 35 | confirm(dom.getElementsByTagName("LI") == \ |
| 36 | dom.documentElement.getElementsByTagName("LI")) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 37 | dom.unlink() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 38 | |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 39 | def testInsertBefore(): |
Fred Drake | a1bde80 | 2000-11-21 22:02:43 +0000 | [diff] [blame] | 40 | dom = parseString("<doc><foo/></doc>") |
| 41 | root = dom.documentElement |
| 42 | elem = root.childNodes[0] |
| 43 | nelem = dom.createElement("element") |
| 44 | root.insertBefore(nelem, elem) |
| 45 | confirm(len(root.childNodes) == 2 |
| 46 | and root.childNodes[0] is nelem |
| 47 | and root.childNodes[1] is elem |
| 48 | and root.firstChild is nelem |
| 49 | and root.lastChild is elem |
| 50 | and root.toxml() == "<doc><element/><foo/></doc>" |
| 51 | , "testInsertBefore -- node properly placed in tree") |
| 52 | nelem = dom.createElement("element") |
| 53 | root.insertBefore(nelem, None) |
| 54 | confirm(len(root.childNodes) == 3 |
| 55 | and root.childNodes[1] is elem |
| 56 | and root.childNodes[2] is nelem |
| 57 | and root.lastChild is nelem |
| 58 | and nelem.previousSibling is elem |
| 59 | and root.toxml() == "<doc><element/><foo/><element/></doc>" |
| 60 | , "testInsertBefore -- node properly placed in tree") |
| 61 | nelem2 = dom.createElement("bar") |
| 62 | root.insertBefore(nelem2, nelem) |
| 63 | confirm(len(root.childNodes) == 4 |
| 64 | and root.childNodes[2] is nelem2 |
| 65 | and root.childNodes[3] is nelem |
| 66 | and nelem2.nextSibling is nelem |
| 67 | and nelem.previousSibling is nelem2 |
| 68 | and root.toxml() == "<doc><element/><foo/><bar/><element/></doc>" |
| 69 | , "testInsertBefore -- node properly placed in tree") |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 70 | dom.unlink() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 71 | |
| 72 | def testAppendChild(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 73 | dom = parse(tstfile) |
| 74 | dom.documentElement.appendChild(dom.createComment(u"Hello")) |
| 75 | confirm(dom.documentElement.childNodes[-1].nodeName == "#comment") |
| 76 | confirm(dom.documentElement.childNodes[-1].data == "Hello") |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 77 | dom.unlink() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 78 | |
| 79 | def testNonZero(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 80 | dom = parse(tstfile) |
| 81 | confirm(dom)# should not be zero |
| 82 | dom.appendChild(dom.createComment("foo")) |
| 83 | confirm(not dom.childNodes[-1].childNodes) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 84 | dom.unlink() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 85 | |
| 86 | def testUnlink(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 87 | dom = parse(tstfile) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 88 | dom.unlink() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 89 | |
| 90 | def testElement(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 91 | dom = Document() |
| 92 | dom.appendChild(dom.createElement("abc")) |
| 93 | confirm(dom.documentElement) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 94 | dom.unlink() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 95 | |
| 96 | def testAAA(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 97 | dom = parseString("<abc/>") |
| 98 | el = dom.documentElement |
| 99 | el.setAttribute("spam", "jam2") |
Fred Drake | a1bde80 | 2000-11-21 22:02:43 +0000 | [diff] [blame] | 100 | confirm(el.toxml() == '<abc spam="jam2"/>', "testAAA") |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 101 | dom.unlink() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 102 | |
| 103 | def testAAB(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 104 | dom = parseString("<abc/>") |
| 105 | el = dom.documentElement |
| 106 | el.setAttribute("spam", "jam") |
| 107 | el.setAttribute("spam", "jam2") |
Fred Drake | a1bde80 | 2000-11-21 22:02:43 +0000 | [diff] [blame] | 108 | confirm(el.toxml() == '<abc spam="jam2"/>', "testAAB") |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 109 | dom.unlink() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 110 | |
| 111 | def testAddAttr(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 112 | dom = Document() |
| 113 | child = dom.appendChild(dom.createElement("abc")) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 114 | |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 115 | child.setAttribute("def", "ghi") |
| 116 | confirm(child.getAttribute("def") == "ghi") |
| 117 | confirm(child.attributes["def"].value == "ghi") |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 118 | |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 119 | child.setAttribute("jkl", "mno") |
| 120 | confirm(child.getAttribute("jkl") == "mno") |
| 121 | confirm(child.attributes["jkl"].value == "mno") |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 122 | |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 123 | confirm(len(child.attributes) == 2) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 124 | |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 125 | child.setAttribute("def", "newval") |
| 126 | confirm(child.getAttribute("def") == "newval") |
| 127 | confirm(child.attributes["def"].value == "newval") |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 128 | |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 129 | confirm(len(child.attributes) == 2) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 130 | dom.unlink() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 131 | |
| 132 | def testDeleteAttr(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 133 | dom = Document() |
| 134 | child = dom.appendChild(dom.createElement("abc")) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 135 | |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 136 | confirm(len(child.attributes) == 0) |
| 137 | child.setAttribute("def", "ghi") |
| 138 | confirm(len(child.attributes) == 1) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 139 | del child.attributes["def"] |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 140 | confirm(len(child.attributes) == 0) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 141 | dom.unlink() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 142 | |
| 143 | def testRemoveAttr(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 144 | dom = Document() |
| 145 | child = dom.appendChild(dom.createElement("abc")) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 146 | |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 147 | child.setAttribute("def", "ghi") |
| 148 | confirm(len(child.attributes) == 1) |
| 149 | child.removeAttribute("def") |
| 150 | confirm(len(child.attributes) == 0) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 151 | |
| 152 | dom.unlink() |
| 153 | |
| 154 | def testRemoveAttrNS(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 155 | dom = Document() |
| 156 | child = dom.appendChild( |
| 157 | dom.createElementNS("http://www.python.org", "python:abc")) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 158 | child.setAttributeNS("http://www.w3.org", "xmlns:python", |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 159 | "http://www.python.org") |
| 160 | child.setAttributeNS("http://www.python.org", "python:abcattr", "foo") |
| 161 | confirm(len(child.attributes) == 2) |
| 162 | child.removeAttributeNS("http://www.python.org", "abcattr") |
| 163 | confirm(len(child.attributes) == 1) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 164 | |
| 165 | dom.unlink() |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 166 | |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 167 | def testRemoveAttributeNode(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 168 | dom = Document() |
| 169 | child = dom.appendChild(dom.createElement("foo")) |
| 170 | child.setAttribute("spam", "jam") |
| 171 | confirm(len(child.attributes) == 1) |
| 172 | node = child.getAttributeNode("spam") |
| 173 | child.removeAttributeNode(node) |
| 174 | confirm(len(child.attributes) == 0) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 175 | |
| 176 | dom.unlink() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 177 | |
| 178 | def testChangeAttr(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 179 | dom = parseString("<abc/>") |
| 180 | el = dom.documentElement |
| 181 | el.setAttribute("spam", "jam") |
| 182 | confirm(len(el.attributes) == 1) |
| 183 | el.setAttribute("spam", "bam") |
| 184 | confirm(len(el.attributes) == 1) |
| 185 | el.attributes["spam"] = "ham" |
| 186 | confirm(len(el.attributes) == 1) |
| 187 | el.setAttribute("spam2", "bam") |
| 188 | confirm(len(el.attributes) == 2) |
| 189 | el.attributes[ "spam2"] = "bam2" |
| 190 | confirm(len(el.attributes) == 2) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 191 | dom.unlink() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 192 | |
| 193 | def testGetAttrList(): |
| 194 | pass |
| 195 | |
| 196 | def testGetAttrValues(): pass |
| 197 | |
| 198 | def testGetAttrLength(): pass |
| 199 | |
| 200 | def testGetAttribute(): pass |
| 201 | |
| 202 | def testGetAttributeNS(): pass |
| 203 | |
| 204 | def testGetAttributeNode(): pass |
| 205 | |
| 206 | def testGetElementsByTagNameNS(): pass |
| 207 | |
| 208 | def testGetEmptyNodeListFromElementsByTagNameNS(): pass |
| 209 | |
| 210 | def testElementReprAndStr(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 211 | dom = Document() |
| 212 | el = dom.appendChild(dom.createElement("abc")) |
| 213 | string1 = repr(el) |
| 214 | string2 = str(el) |
| 215 | confirm(string1 == string2) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 216 | dom.unlink() |
| 217 | |
| 218 | # commented out until Fredrick's fix is checked in |
| 219 | def _testElementReprAndStrUnicode(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 220 | dom = Document() |
| 221 | el = dom.appendChild(dom.createElement(u"abc")) |
| 222 | string1 = repr(el) |
| 223 | string2 = str(el) |
| 224 | confirm(string1 == string2) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 225 | dom.unlink() |
| 226 | |
| 227 | # commented out until Fredrick's fix is checked in |
| 228 | def _testElementReprAndStrUnicodeNS(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 229 | dom = Document() |
| 230 | el = dom.appendChild( |
| 231 | dom.createElementNS(u"http://www.slashdot.org", u"slash:abc")) |
| 232 | string1 = repr(el) |
| 233 | string2 = str(el) |
| 234 | confirm(string1 == string2) |
| 235 | confirm(string1.find("slash:abc") != -1) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 236 | dom.unlink() |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 237 | confirm(len(Node.allnodes) == 0) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 238 | |
| 239 | def testAttributeRepr(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 240 | dom = Document() |
| 241 | el = dom.appendChild(dom.createElement(u"abc")) |
| 242 | node = el.setAttribute("abc", "def") |
| 243 | confirm(str(node) == repr(node)) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 244 | dom.unlink() |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 245 | confirm(len(Node.allnodes) == 0) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 246 | |
| 247 | def testTextNodeRepr(): pass |
| 248 | |
Martin v. Löwis | 0a84a33 | 2000-10-06 22:42:55 +0000 | [diff] [blame] | 249 | def testWriteXML(): |
| 250 | str = '<a b="c"/>' |
| 251 | dom = parseString(str) |
| 252 | domstr = dom.toxml() |
| 253 | dom.unlink() |
| 254 | confirm(str == domstr) |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 255 | confirm(len(Node.allnodes) == 0) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 256 | |
| 257 | def testProcessingInstruction(): pass |
| 258 | |
| 259 | def testProcessingInstructionRepr(): pass |
| 260 | |
| 261 | def testTextRepr(): pass |
| 262 | |
| 263 | def testWriteText(): pass |
| 264 | |
| 265 | def testDocumentElement(): pass |
| 266 | |
Fred Drake | a1bde80 | 2000-11-21 22:02:43 +0000 | [diff] [blame] | 267 | def testTooManyDocumentElements(): |
| 268 | doc = parseString("<doc/>") |
| 269 | elem = doc.createElement("extra") |
| 270 | try: |
| 271 | doc.appendChild(elem) |
| 272 | except TypeError: |
| 273 | print "Caught expected exception when adding extra document element." |
| 274 | else: |
| 275 | print "Failed to catch expected exception when" \ |
| 276 | " adding extra document element." |
| 277 | elem.unlink() |
| 278 | doc.unlink() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 279 | |
| 280 | def testCreateElementNS(): pass |
| 281 | |
| 282 | def testCreatAttributeNS(): pass |
| 283 | |
| 284 | def testParse(): pass |
| 285 | |
| 286 | def testParseString(): pass |
| 287 | |
| 288 | def testComment(): pass |
| 289 | |
| 290 | def testAttrListItem(): pass |
| 291 | |
| 292 | def testAttrListItems(): pass |
| 293 | |
| 294 | def testAttrListItemNS(): pass |
| 295 | |
| 296 | def testAttrListKeys(): pass |
| 297 | |
| 298 | def testAttrListKeysNS(): pass |
| 299 | |
| 300 | def testAttrListValues(): pass |
| 301 | |
| 302 | def testAttrListLength(): pass |
| 303 | |
| 304 | def testAttrList__getitem__(): pass |
| 305 | |
| 306 | def testAttrList__setitem__(): pass |
| 307 | |
| 308 | def testSetAttrValueandNodeValue(): pass |
| 309 | |
| 310 | def testParseElement(): pass |
| 311 | |
| 312 | def testParseAttributes(): pass |
| 313 | |
| 314 | def testParseElementNamespaces(): pass |
| 315 | |
| 316 | def testParseAttributeNamespaces(): pass |
| 317 | |
| 318 | def testParseProcessingInstructions(): pass |
| 319 | |
| 320 | def testChildNodes(): pass |
| 321 | |
| 322 | def testFirstChild(): pass |
| 323 | |
| 324 | def testHasChildNodes(): pass |
| 325 | |
Fred Drake | a1bde80 | 2000-11-21 22:02:43 +0000 | [diff] [blame] | 326 | def testCloneElementShallow(): |
| 327 | dom, clone = _setupCloneElement(0) |
| 328 | confirm(len(clone.childNodes) == 0 |
| 329 | and clone.parentNode is None |
| 330 | and clone.toxml() == '<doc attr="value"/>' |
| 331 | , "testCloneElementShallow") |
| 332 | dom.unlink() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 333 | |
Fred Drake | a1bde80 | 2000-11-21 22:02:43 +0000 | [diff] [blame] | 334 | def testCloneElementDeep(): |
| 335 | dom, clone = _setupCloneElement(1) |
| 336 | confirm(len(clone.childNodes) == 1 |
| 337 | and clone.parentNode is None |
| 338 | and clone.toxml() == '<doc attr="value"><foo/></doc>' |
| 339 | , "testCloneElementDeep") |
| 340 | dom.unlink() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 341 | |
Fred Drake | a1bde80 | 2000-11-21 22:02:43 +0000 | [diff] [blame] | 342 | def _setupCloneElement(deep): |
| 343 | dom = parseString("<doc attr='value'><foo/></doc>") |
| 344 | root = dom.documentElement |
| 345 | clone = root.cloneNode(deep) |
| 346 | _testCloneElementCopiesAttributes( |
| 347 | root, clone, "testCloneElement" + (deep and "Deep" or "Shallow")) |
| 348 | # mutilate the original so shared data is detected |
| 349 | root.tagName = root.nodeName = "MODIFIED" |
| 350 | root.setAttribute("attr", "NEW VALUE") |
| 351 | root.setAttribute("added", "VALUE") |
| 352 | return dom, clone |
| 353 | |
| 354 | def _testCloneElementCopiesAttributes(e1, e2, test): |
| 355 | attrs1 = e1.attributes |
| 356 | attrs2 = e2.attributes |
| 357 | keys1 = attrs1.keys() |
| 358 | keys2 = attrs2.keys() |
| 359 | keys1.sort() |
| 360 | keys2.sort() |
| 361 | confirm(keys1 == keys2, "clone of element has same attribute keys") |
| 362 | for i in range(len(keys1)): |
| 363 | a1 = attrs1.item(i) |
| 364 | a2 = attrs2.item(i) |
| 365 | confirm(a1 is not a2 |
| 366 | and a1.value == a2.value |
| 367 | and a1.nodeValue == a2.nodeValue |
| 368 | and a1.namespaceURI == a2.namespaceURI |
| 369 | and a1.localName == a2.localName |
| 370 | , "clone of attribute node has proper attribute values") |
| 371 | confirm(a2.ownerElement is e2, |
| 372 | "clone of attribute node correctly owned") |
| 373 | |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 374 | |
| 375 | def testCloneDocumentShallow(): pass |
| 376 | |
| 377 | def testCloneDocumentDeep(): pass |
| 378 | |
| 379 | def testCloneAttributeShallow(): pass |
| 380 | |
| 381 | def testCloneAttributeDeep(): pass |
| 382 | |
| 383 | def testClonePIShallow(): pass |
| 384 | |
| 385 | def testClonePIDeep(): pass |
| 386 | |
Fred Drake | a1bde80 | 2000-11-21 22:02:43 +0000 | [diff] [blame] | 387 | def testNormalize(): |
| 388 | doc = parseString("<doc/>") |
| 389 | root = doc.documentElement |
| 390 | root.appendChild(doc.createTextNode("first")) |
| 391 | root.appendChild(doc.createTextNode("second")) |
| 392 | confirm(len(root.childNodes) == 2, "testNormalize -- preparation") |
| 393 | doc.normalize() |
| 394 | confirm(len(root.childNodes) == 1 |
| 395 | and root.firstChild is root.lastChild |
| 396 | and root.firstChild.data == "firstsecond" |
| 397 | , "testNormalize -- result") |
| 398 | doc.unlink() |
| 399 | |
Lars Gustäbel | f27f5ab | 2000-10-11 22:36:00 +0000 | [diff] [blame] | 400 | def testSiblings(): |
| 401 | doc = parseString("<doc><?pi?>text?<elm/></doc>") |
| 402 | root = doc.documentElement |
| 403 | (pi, text, elm) = root.childNodes |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 404 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 405 | confirm(pi.nextSibling is text and |
| 406 | pi.previousSibling is None and |
| 407 | text.nextSibling is elm and |
| 408 | text.previousSibling is pi and |
| 409 | elm.nextSibling is None and |
Lars Gustäbel | f27f5ab | 2000-10-11 22:36:00 +0000 | [diff] [blame] | 410 | elm.previousSibling is text, "testSiblings") |
| 411 | |
| 412 | doc.unlink() |
| 413 | |
| 414 | def testParents(): |
| 415 | doc = parseString("<doc><elm1><elm2/><elm2><elm3/></elm2></elm1></doc>") |
| 416 | root = doc.documentElement |
| 417 | elm1 = root.childNodes[0] |
| 418 | (elm2a, elm2b) = elm1.childNodes |
| 419 | elm3 = elm2b.childNodes[0] |
| 420 | |
| 421 | confirm(root.parentNode is doc and |
| 422 | elm1.parentNode is root and |
| 423 | elm2a.parentNode is elm1 and |
| 424 | elm2b.parentNode is elm1 and |
| 425 | elm3.parentNode is elm2b, "testParents") |
| 426 | |
| 427 | doc.unlink() |
| 428 | |
Lars Gustäbel | 5bad5a4 | 2000-10-13 20:54:10 +0000 | [diff] [blame] | 429 | def testSAX2DOM(): |
Lars Gustäbel | f27f5ab | 2000-10-11 22:36:00 +0000 | [diff] [blame] | 430 | from xml.dom import pulldom |
| 431 | |
Lars Gustäbel | 5bad5a4 | 2000-10-13 20:54:10 +0000 | [diff] [blame] | 432 | sax2dom = pulldom.SAX2DOM() |
| 433 | sax2dom.startDocument() |
| 434 | sax2dom.startElement("doc", {}) |
| 435 | sax2dom.characters("text") |
| 436 | sax2dom.startElement("subelm", {}) |
| 437 | sax2dom.characters("text") |
| 438 | sax2dom.endElement("subelm") |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 439 | sax2dom.characters("text") |
Lars Gustäbel | 5bad5a4 | 2000-10-13 20:54:10 +0000 | [diff] [blame] | 440 | sax2dom.endElement("doc") |
| 441 | sax2dom.endDocument() |
Lars Gustäbel | f27f5ab | 2000-10-11 22:36:00 +0000 | [diff] [blame] | 442 | |
Lars Gustäbel | 5bad5a4 | 2000-10-13 20:54:10 +0000 | [diff] [blame] | 443 | doc = sax2dom.document |
Lars Gustäbel | f27f5ab | 2000-10-11 22:36:00 +0000 | [diff] [blame] | 444 | root = doc.documentElement |
| 445 | (text1, elm1, text2) = root.childNodes |
| 446 | text3 = elm1.childNodes[0] |
| 447 | |
| 448 | confirm(text1.previousSibling is None and |
| 449 | text1.nextSibling is elm1 and |
| 450 | elm1.previousSibling is text1 and |
| 451 | elm1.nextSibling is text2 and |
| 452 | text2.previousSibling is elm1 and |
| 453 | text2.nextSibling is None and |
| 454 | text3.previousSibling is None and |
Lars Gustäbel | 5bad5a4 | 2000-10-13 20:54:10 +0000 | [diff] [blame] | 455 | text3.nextSibling is None, "testSAX2DOM - siblings") |
Lars Gustäbel | f27f5ab | 2000-10-11 22:36:00 +0000 | [diff] [blame] | 456 | |
| 457 | confirm(root.parentNode is doc and |
| 458 | text1.parentNode is root and |
| 459 | elm1.parentNode is root and |
| 460 | text2.parentNode is root and |
Lars Gustäbel | 5bad5a4 | 2000-10-13 20:54:10 +0000 | [diff] [blame] | 461 | text3.parentNode is elm1, "testSAX2DOM - parents") |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 462 | |
Lars Gustäbel | f27f5ab | 2000-10-11 22:36:00 +0000 | [diff] [blame] | 463 | doc.unlink() |
| 464 | |
| 465 | # --- MAIN PROGRAM |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 466 | |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 467 | names = globals().keys() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 468 | names.sort() |
Paul Prescod | 10d2766 | 2000-09-18 19:07:26 +0000 | [diff] [blame] | 469 | |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 470 | works = 1 |
Paul Prescod | 10d2766 | 2000-09-18 19:07:26 +0000 | [diff] [blame] | 471 | |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 472 | for name in names: |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 473 | if name.startswith("test"): |
| 474 | func = globals()[name] |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 475 | try: |
| 476 | func() |
| 477 | print "Test Succeeded", name |
Fred Drake | ebe7302 | 2000-10-09 19:57:39 +0000 | [diff] [blame] | 478 | confirm(len(Node.allnodes) == 0, |
| 479 | "assertion: len(Node.allnodes) == 0") |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 480 | if len(Node.allnodes): |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 481 | print "Garbage left over:" |
Martin v. Löwis | 89c528b | 2000-09-19 16:22:10 +0000 | [diff] [blame] | 482 | if verbose: |
| 483 | print Node.allnodes.items()[0:10] |
| 484 | else: |
| 485 | # Don't print specific nodes if repeatable results |
| 486 | # are needed |
| 487 | print len(Node.allnodes) |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 488 | Node.allnodes = {} |
| 489 | except Exception, e: |
| 490 | works = 0 |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 491 | print "Test Failed: ", name |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 492 | traceback.print_exception(*sys.exc_info()) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 493 | print `e` |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 494 | Node.allnodes = {} |
Paul Prescod | 10d2766 | 2000-09-18 19:07:26 +0000 | [diff] [blame] | 495 | |
| 496 | if works: |
| 497 | print "All tests succeeded" |
| 498 | else: |
| 499 | print "\n\n\n\n************ Check for failures!" |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 500 | |
Guido van Rossum | 9e79a25 | 2000-09-21 20:10:39 +0000 | [diff] [blame] | 501 | Node.debug = None # Delete debug output collected in a StringIO object |
| 502 | Node._debug = 0 # And reset debug mode |