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