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