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