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(): |
| 40 | dom = parse(tstfile) |
| 41 | docel = dom.documentElement |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 42 | #docel.insertBefore( dom.createProcessingInstruction("a", "b"), |
| 43 | # docel.childNodes[1]) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 44 | |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 45 | #docel.insertBefore( dom.createProcessingInstruction("a", "b"), |
| 46 | # docel.childNodes[0]) |
| 47 | |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 48 | #confirm( docel.childNodes[0].tet == "a") |
| 49 | #confirm( docel.childNodes[2].tet == "a") |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 50 | dom.unlink() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 51 | |
| 52 | def testAppendChild(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 53 | dom = parse(tstfile) |
| 54 | dom.documentElement.appendChild(dom.createComment(u"Hello")) |
| 55 | confirm(dom.documentElement.childNodes[-1].nodeName == "#comment") |
| 56 | confirm(dom.documentElement.childNodes[-1].data == "Hello") |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 57 | dom.unlink() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 58 | |
| 59 | def testNonZero(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 60 | dom = parse(tstfile) |
| 61 | confirm(dom)# should not be zero |
| 62 | dom.appendChild(dom.createComment("foo")) |
| 63 | confirm(not dom.childNodes[-1].childNodes) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 64 | dom.unlink() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 65 | |
| 66 | def testUnlink(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 67 | dom = parse(tstfile) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 68 | dom.unlink() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 69 | |
| 70 | def testElement(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 71 | dom = Document() |
| 72 | dom.appendChild(dom.createElement("abc")) |
| 73 | confirm(dom.documentElement) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 74 | dom.unlink() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 75 | |
| 76 | def testAAA(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 77 | dom = parseString("<abc/>") |
| 78 | el = dom.documentElement |
| 79 | el.setAttribute("spam", "jam2") |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 80 | dom.unlink() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 81 | |
| 82 | def testAAB(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 83 | dom = parseString("<abc/>") |
| 84 | el = dom.documentElement |
| 85 | el.setAttribute("spam", "jam") |
| 86 | el.setAttribute("spam", "jam2") |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 87 | dom.unlink() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 88 | |
| 89 | def testAddAttr(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 90 | dom = Document() |
| 91 | child = dom.appendChild(dom.createElement("abc")) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 92 | |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 93 | child.setAttribute("def", "ghi") |
| 94 | confirm(child.getAttribute("def") == "ghi") |
| 95 | confirm(child.attributes["def"].value == "ghi") |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 96 | |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 97 | child.setAttribute("jkl", "mno") |
| 98 | confirm(child.getAttribute("jkl") == "mno") |
| 99 | confirm(child.attributes["jkl"].value == "mno") |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 100 | |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 101 | confirm(len(child.attributes) == 2) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 102 | |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 103 | child.setAttribute("def", "newval") |
| 104 | confirm(child.getAttribute("def") == "newval") |
| 105 | confirm(child.attributes["def"].value == "newval") |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 106 | |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 107 | confirm(len(child.attributes) == 2) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 108 | dom.unlink() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 109 | |
| 110 | def testDeleteAttr(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 111 | dom = Document() |
| 112 | child = dom.appendChild(dom.createElement("abc")) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 113 | |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 114 | confirm(len(child.attributes) == 0) |
| 115 | child.setAttribute("def", "ghi") |
| 116 | confirm(len(child.attributes) == 1) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 117 | del child.attributes["def"] |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 118 | confirm(len(child.attributes) == 0) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 119 | dom.unlink() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 120 | |
| 121 | def testRemoveAttr(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 122 | dom = Document() |
| 123 | child = dom.appendChild(dom.createElement("abc")) |
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", "ghi") |
| 126 | confirm(len(child.attributes) == 1) |
| 127 | child.removeAttribute("def") |
| 128 | confirm(len(child.attributes) == 0) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 129 | |
| 130 | dom.unlink() |
| 131 | |
| 132 | def testRemoveAttrNS(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 133 | dom = Document() |
| 134 | child = dom.appendChild( |
| 135 | dom.createElementNS("http://www.python.org", "python:abc")) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 136 | child.setAttributeNS("http://www.w3.org", "xmlns:python", |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 137 | "http://www.python.org") |
| 138 | child.setAttributeNS("http://www.python.org", "python:abcattr", "foo") |
| 139 | confirm(len(child.attributes) == 2) |
| 140 | child.removeAttributeNS("http://www.python.org", "abcattr") |
| 141 | confirm(len(child.attributes) == 1) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 142 | |
| 143 | dom.unlink() |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 144 | |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 145 | def testRemoveAttributeNode(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 146 | dom = Document() |
| 147 | child = dom.appendChild(dom.createElement("foo")) |
| 148 | child.setAttribute("spam", "jam") |
| 149 | confirm(len(child.attributes) == 1) |
| 150 | node = child.getAttributeNode("spam") |
| 151 | child.removeAttributeNode(node) |
| 152 | confirm(len(child.attributes) == 0) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 153 | |
| 154 | dom.unlink() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 155 | |
| 156 | def testChangeAttr(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 157 | dom = parseString("<abc/>") |
| 158 | el = dom.documentElement |
| 159 | el.setAttribute("spam", "jam") |
| 160 | confirm(len(el.attributes) == 1) |
| 161 | el.setAttribute("spam", "bam") |
| 162 | confirm(len(el.attributes) == 1) |
| 163 | el.attributes["spam"] = "ham" |
| 164 | confirm(len(el.attributes) == 1) |
| 165 | el.setAttribute("spam2", "bam") |
| 166 | confirm(len(el.attributes) == 2) |
| 167 | el.attributes[ "spam2"] = "bam2" |
| 168 | confirm(len(el.attributes) == 2) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 169 | dom.unlink() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +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(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 189 | dom = Document() |
| 190 | el = dom.appendChild(dom.createElement("abc")) |
| 191 | string1 = repr(el) |
| 192 | string2 = str(el) |
| 193 | confirm(string1 == string2) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 194 | dom.unlink() |
| 195 | |
| 196 | # commented out until Fredrick's fix is checked in |
| 197 | def _testElementReprAndStrUnicode(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 198 | dom = Document() |
| 199 | el = dom.appendChild(dom.createElement(u"abc")) |
| 200 | string1 = repr(el) |
| 201 | string2 = str(el) |
| 202 | confirm(string1 == string2) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 203 | dom.unlink() |
| 204 | |
| 205 | # commented out until Fredrick's fix is checked in |
| 206 | def _testElementReprAndStrUnicodeNS(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 207 | dom = Document() |
| 208 | el = dom.appendChild( |
| 209 | dom.createElementNS(u"http://www.slashdot.org", u"slash:abc")) |
| 210 | string1 = repr(el) |
| 211 | string2 = str(el) |
| 212 | confirm(string1 == string2) |
| 213 | confirm(string1.find("slash:abc") != -1) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 214 | dom.unlink() |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 215 | confirm(len(Node.allnodes) == 0) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 216 | |
| 217 | def testAttributeRepr(): |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 218 | dom = Document() |
| 219 | el = dom.appendChild(dom.createElement(u"abc")) |
| 220 | node = el.setAttribute("abc", "def") |
| 221 | confirm(str(node) == repr(node)) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 222 | dom.unlink() |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 223 | confirm(len(Node.allnodes) == 0) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 224 | |
| 225 | def testTextNodeRepr(): pass |
| 226 | |
Martin v. Löwis | 0a84a33 | 2000-10-06 22:42:55 +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) |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 233 | confirm(len(Node.allnodes) == 0) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +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 | |
Lars Gustäbel | f27f5ab | 2000-10-11 22:36:00 +0000 | [diff] [blame] | 311 | def testSiblings(): |
| 312 | doc = parseString("<doc><?pi?>text?<elm/></doc>") |
| 313 | root = doc.documentElement |
| 314 | (pi, text, elm) = root.childNodes |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 315 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 316 | confirm(pi.nextSibling is text and |
| 317 | pi.previousSibling is None and |
| 318 | text.nextSibling is elm and |
| 319 | text.previousSibling is pi and |
| 320 | elm.nextSibling is None and |
Lars Gustäbel | f27f5ab | 2000-10-11 22:36:00 +0000 | [diff] [blame] | 321 | elm.previousSibling is text, "testSiblings") |
| 322 | |
| 323 | doc.unlink() |
| 324 | |
| 325 | def testParents(): |
| 326 | doc = parseString("<doc><elm1><elm2/><elm2><elm3/></elm2></elm1></doc>") |
| 327 | root = doc.documentElement |
| 328 | elm1 = root.childNodes[0] |
| 329 | (elm2a, elm2b) = elm1.childNodes |
| 330 | elm3 = elm2b.childNodes[0] |
| 331 | |
| 332 | confirm(root.parentNode is doc and |
| 333 | elm1.parentNode is root and |
| 334 | elm2a.parentNode is elm1 and |
| 335 | elm2b.parentNode is elm1 and |
| 336 | elm3.parentNode is elm2b, "testParents") |
| 337 | |
| 338 | doc.unlink() |
| 339 | |
Lars Gustäbel | 5bad5a4 | 2000-10-13 20:54:10 +0000 | [diff] [blame] | 340 | def testSAX2DOM(): |
Lars Gustäbel | f27f5ab | 2000-10-11 22:36:00 +0000 | [diff] [blame] | 341 | from xml.dom import pulldom |
| 342 | |
Lars Gustäbel | 5bad5a4 | 2000-10-13 20:54:10 +0000 | [diff] [blame] | 343 | sax2dom = pulldom.SAX2DOM() |
| 344 | sax2dom.startDocument() |
| 345 | sax2dom.startElement("doc", {}) |
| 346 | sax2dom.characters("text") |
| 347 | sax2dom.startElement("subelm", {}) |
| 348 | sax2dom.characters("text") |
| 349 | sax2dom.endElement("subelm") |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 350 | sax2dom.characters("text") |
Lars Gustäbel | 5bad5a4 | 2000-10-13 20:54:10 +0000 | [diff] [blame] | 351 | sax2dom.endElement("doc") |
| 352 | sax2dom.endDocument() |
Lars Gustäbel | f27f5ab | 2000-10-11 22:36:00 +0000 | [diff] [blame] | 353 | |
Lars Gustäbel | 5bad5a4 | 2000-10-13 20:54:10 +0000 | [diff] [blame] | 354 | doc = sax2dom.document |
Lars Gustäbel | f27f5ab | 2000-10-11 22:36:00 +0000 | [diff] [blame] | 355 | root = doc.documentElement |
| 356 | (text1, elm1, text2) = root.childNodes |
| 357 | text3 = elm1.childNodes[0] |
| 358 | |
| 359 | confirm(text1.previousSibling is None and |
| 360 | text1.nextSibling is elm1 and |
| 361 | elm1.previousSibling is text1 and |
| 362 | elm1.nextSibling is text2 and |
| 363 | text2.previousSibling is elm1 and |
| 364 | text2.nextSibling is None and |
| 365 | text3.previousSibling is None and |
Lars Gustäbel | 5bad5a4 | 2000-10-13 20:54:10 +0000 | [diff] [blame] | 366 | text3.nextSibling is None, "testSAX2DOM - siblings") |
Lars Gustäbel | f27f5ab | 2000-10-11 22:36:00 +0000 | [diff] [blame] | 367 | |
| 368 | confirm(root.parentNode is doc and |
| 369 | text1.parentNode is root and |
| 370 | elm1.parentNode is root and |
| 371 | text2.parentNode is root and |
Lars Gustäbel | 5bad5a4 | 2000-10-13 20:54:10 +0000 | [diff] [blame] | 372 | text3.parentNode is elm1, "testSAX2DOM - parents") |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 373 | |
Lars Gustäbel | f27f5ab | 2000-10-11 22:36:00 +0000 | [diff] [blame] | 374 | doc.unlink() |
| 375 | |
| 376 | # --- MAIN PROGRAM |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 377 | |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 378 | names = globals().keys() |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 379 | names.sort() |
Paul Prescod | 10d2766 | 2000-09-18 19:07:26 +0000 | [diff] [blame] | 380 | |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 381 | works = 1 |
Paul Prescod | 10d2766 | 2000-09-18 19:07:26 +0000 | [diff] [blame] | 382 | |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 383 | for name in names: |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 384 | if name.startswith("test"): |
| 385 | func = globals()[name] |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 386 | try: |
| 387 | func() |
| 388 | print "Test Succeeded", name |
Fred Drake | ebe7302 | 2000-10-09 19:57:39 +0000 | [diff] [blame] | 389 | confirm(len(Node.allnodes) == 0, |
| 390 | "assertion: len(Node.allnodes) == 0") |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 391 | if len(Node.allnodes): |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 392 | print "Garbage left over:" |
Martin v. Löwis | 89c528b | 2000-09-19 16:22:10 +0000 | [diff] [blame] | 393 | if verbose: |
| 394 | print Node.allnodes.items()[0:10] |
| 395 | else: |
| 396 | # Don't print specific nodes if repeatable results |
| 397 | # are needed |
| 398 | print len(Node.allnodes) |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 399 | Node.allnodes = {} |
| 400 | except Exception, e: |
| 401 | works = 0 |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 402 | print "Test Failed: ", name |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 403 | traceback.print_exception(*sys.exc_info()) |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 404 | print `e` |
Jeremy Hylton | 3b0c600 | 2000-10-12 17:31:36 +0000 | [diff] [blame] | 405 | Node.allnodes = {} |
Paul Prescod | 10d2766 | 2000-09-18 19:07:26 +0000 | [diff] [blame] | 406 | |
| 407 | if works: |
| 408 | print "All tests succeeded" |
| 409 | else: |
| 410 | print "\n\n\n\n************ Check for failures!" |
Paul Prescod | 7993bcc | 2000-07-01 14:54:16 +0000 | [diff] [blame] | 411 | |
Guido van Rossum | 9e79a25 | 2000-09-21 20:10:39 +0000 | [diff] [blame] | 412 | Node.debug = None # Delete debug output collected in a StringIO object |
| 413 | Node._debug = 0 # And reset debug mode |