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