gh-63882: Break down and tests in test_minidom (#133026)

This commit is contained in:
Stan Ulbrych 2025-05-05 17:07:24 +01:00 committed by GitHub
parent f5b784741d
commit e294e161a8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -102,41 +102,38 @@ class MinidomTest(unittest.TestCase):
elem = root.childNodes[0] elem = root.childNodes[0]
nelem = dom.createElement("element") nelem = dom.createElement("element")
root.insertBefore(nelem, elem) root.insertBefore(nelem, elem)
self.confirm(len(root.childNodes) == 2 self.assertEqual(len(root.childNodes), 2)
and root.childNodes.length == 2 self.assertEqual(root.childNodes.length, 2)
and root.childNodes[0] is nelem self.assertIs(root.childNodes[0], nelem)
and root.childNodes.item(0) is nelem self.assertIs(root.childNodes.item(0), nelem)
and root.childNodes[1] is elem self.assertIs(root.childNodes[1], elem)
and root.childNodes.item(1) is elem self.assertIs(root.childNodes.item(1), elem)
and root.firstChild is nelem self.assertIs(root.firstChild, nelem)
and root.lastChild is elem self.assertIs(root.lastChild, elem)
and root.toxml() == "<doc><element/><foo/></doc>" self.assertEqual(root.toxml(), "<doc><element/><foo/></doc>")
, "testInsertBefore -- node properly placed in tree")
nelem = dom.createElement("element") nelem = dom.createElement("element")
root.insertBefore(nelem, None) root.insertBefore(nelem, None)
self.confirm(len(root.childNodes) == 3 self.assertEqual(len(root.childNodes), 3)
and root.childNodes.length == 3 self.assertEqual(root.childNodes.length, 3)
and root.childNodes[1] is elem self.assertIs(root.childNodes[1], elem)
and root.childNodes.item(1) is elem self.assertIs(root.childNodes.item(1), elem)
and root.childNodes[2] is nelem self.assertIs(root.childNodes[2], nelem)
and root.childNodes.item(2) is nelem self.assertIs(root.childNodes.item(2), nelem)
and root.lastChild is nelem self.assertIs(root.lastChild, nelem)
and nelem.previousSibling is elem self.assertIs(nelem.previousSibling, elem)
and root.toxml() == "<doc><element/><foo/><element/></doc>" self.assertEqual(root.toxml(), "<doc><element/><foo/><element/></doc>")
, "testInsertBefore -- node properly placed in tree")
nelem2 = dom.createElement("bar") nelem2 = dom.createElement("bar")
root.insertBefore(nelem2, nelem) root.insertBefore(nelem2, nelem)
self.confirm(len(root.childNodes) == 4 self.assertEqual(len(root.childNodes), 4)
and root.childNodes.length == 4 self.assertEqual(root.childNodes.length, 4)
and root.childNodes[2] is nelem2 self.assertIs(root.childNodes[2], nelem2)
and root.childNodes.item(2) is nelem2 self.assertIs(root.childNodes.item(2), nelem2)
and root.childNodes[3] is nelem self.assertIs(root.childNodes[3], nelem)
and root.childNodes.item(3) is nelem self.assertIs(root.childNodes.item(3), nelem)
and nelem2.nextSibling is nelem self.assertIs(nelem2.nextSibling, nelem)
and nelem.previousSibling is nelem2 self.assertIs(nelem.previousSibling, nelem2)
and root.toxml() == self.assertEqual(root.toxml(),
"<doc><element/><foo/><bar/><element/></doc>" "<doc><element/><foo/><bar/><element/></doc>")
, "testInsertBefore -- node properly placed in tree")
dom.unlink() dom.unlink()
def _create_fragment_test_nodes(self): def _create_fragment_test_nodes(self):
@ -342,8 +339,8 @@ class MinidomTest(unittest.TestCase):
self.assertRaises(xml.dom.NotFoundErr, child.removeAttributeNode, self.assertRaises(xml.dom.NotFoundErr, child.removeAttributeNode,
None) None)
self.assertIs(node, child.removeAttributeNode(node)) self.assertIs(node, child.removeAttributeNode(node))
self.confirm(len(child.attributes) == 0 self.assertEqual(len(child.attributes), 0)
and child.getAttributeNode("spam") is None) self.assertIsNone(child.getAttributeNode("spam"))
dom2 = Document() dom2 = Document()
child2 = dom2.appendChild(dom2.createElement("foo")) child2 = dom2.appendChild(dom2.createElement("foo"))
node2 = child2.getAttributeNode("spam") node2 = child2.getAttributeNode("spam")
@ -366,33 +363,34 @@ class MinidomTest(unittest.TestCase):
# Set this attribute to be an ID and make sure that doesn't change # Set this attribute to be an ID and make sure that doesn't change
# when changing the value: # when changing the value:
el.setIdAttribute("spam") el.setIdAttribute("spam")
self.confirm(len(el.attributes) == 1 self.assertEqual(len(el.attributes), 1)
and el.attributes["spam"].value == "bam" self.assertEqual(el.attributes["spam"].value, "bam")
and el.attributes["spam"].nodeValue == "bam" self.assertEqual(el.attributes["spam"].nodeValue, "bam")
and el.getAttribute("spam") == "bam" self.assertEqual(el.getAttribute("spam"), "bam")
and el.getAttributeNode("spam").isId) self.assertTrue(el.getAttributeNode("spam").isId)
el.attributes["spam"] = "ham" el.attributes["spam"] = "ham"
self.confirm(len(el.attributes) == 1 self.assertEqual(len(el.attributes), 1)
and el.attributes["spam"].value == "ham" self.assertEqual(el.attributes["spam"].value, "ham")
and el.attributes["spam"].nodeValue == "ham" self.assertEqual(el.attributes["spam"].nodeValue, "ham")
and el.getAttribute("spam") == "ham" self.assertEqual(el.getAttribute("spam"), "ham")
and el.attributes["spam"].isId) self.assertTrue(el.attributes["spam"].isId)
el.setAttribute("spam2", "bam") el.setAttribute("spam2", "bam")
self.confirm(len(el.attributes) == 2 self.assertEqual(len(el.attributes), 2)
and el.attributes["spam"].value == "ham" self.assertEqual(el.attributes["spam"].value, "ham")
and el.attributes["spam"].nodeValue == "ham" self.assertEqual(el.attributes["spam"].nodeValue, "ham")
and el.getAttribute("spam") == "ham" self.assertEqual(el.getAttribute("spam"), "ham")
and el.attributes["spam2"].value == "bam" self.assertEqual(el.attributes["spam2"].value, "bam")
and el.attributes["spam2"].nodeValue == "bam" self.assertEqual(el.attributes["spam2"].nodeValue, "bam")
and el.getAttribute("spam2") == "bam") self.assertEqual(el.getAttribute("spam2"), "bam")
el.attributes["spam2"] = "bam2" el.attributes["spam2"] = "bam2"
self.confirm(len(el.attributes) == 2
and el.attributes["spam"].value == "ham" self.assertEqual(len(el.attributes), 2)
and el.attributes["spam"].nodeValue == "ham" self.assertEqual(el.attributes["spam"].value, "ham")
and el.getAttribute("spam") == "ham" self.assertEqual(el.attributes["spam"].nodeValue, "ham")
and el.attributes["spam2"].value == "bam2" self.assertEqual(el.getAttribute("spam"), "ham")
and el.attributes["spam2"].nodeValue == "bam2" self.assertEqual(el.attributes["spam2"].value, "bam2")
and el.getAttribute("spam2") == "bam2") self.assertEqual(el.attributes["spam2"].nodeValue, "bam2")
self.assertEqual(el.getAttribute("spam2"), "bam2")
dom.unlink() dom.unlink()
def testGetAttrList(self): def testGetAttrList(self):
@ -448,12 +446,12 @@ class MinidomTest(unittest.TestCase):
dom = parseString(d) dom = parseString(d)
elems = dom.getElementsByTagNameNS("http://pyxml.sf.net/minidom", elems = dom.getElementsByTagNameNS("http://pyxml.sf.net/minidom",
"myelem") "myelem")
self.confirm(len(elems) == 1 self.assertEqual(len(elems), 1)
and elems[0].namespaceURI == "http://pyxml.sf.net/minidom" self.assertEqual(elems[0].namespaceURI, "http://pyxml.sf.net/minidom")
and elems[0].localName == "myelem" self.assertEqual(elems[0].localName, "myelem")
and elems[0].prefix == "minidom" self.assertEqual(elems[0].prefix, "minidom")
and elems[0].tagName == "minidom:myelem" self.assertEqual(elems[0].tagName, "minidom:myelem")
and elems[0].nodeName == "minidom:myelem") self.assertEqual(elems[0].nodeName, "minidom:myelem")
dom.unlink() dom.unlink()
def get_empty_nodelist_from_elements_by_tagName_ns_helper(self, doc, nsuri, def get_empty_nodelist_from_elements_by_tagName_ns_helper(self, doc, nsuri,
@ -602,17 +600,17 @@ class MinidomTest(unittest.TestCase):
def testProcessingInstruction(self): def testProcessingInstruction(self):
dom = parseString('<e><?mypi \t\n data \t\n ?></e>') dom = parseString('<e><?mypi \t\n data \t\n ?></e>')
pi = dom.documentElement.firstChild pi = dom.documentElement.firstChild
self.confirm(pi.target == "mypi" self.assertEqual(pi.target, "mypi")
and pi.data == "data \t\n " self.assertEqual(pi.data, "data \t\n ")
and pi.nodeName == "mypi" self.assertEqual(pi.nodeName, "mypi")
and pi.nodeType == Node.PROCESSING_INSTRUCTION_NODE self.assertEqual(pi.nodeType, Node.PROCESSING_INSTRUCTION_NODE)
and pi.attributes is None self.assertIsNone(pi.attributes)
and not pi.hasChildNodes() self.assertFalse(pi.hasChildNodes())
and len(pi.childNodes) == 0 self.assertEqual(len(pi.childNodes), 0)
and pi.firstChild is None self.assertIsNone(pi.firstChild)
and pi.lastChild is None self.assertIsNone(pi.lastChild)
and pi.localName is None self.assertIsNone(pi.localName)
and pi.namespaceURI == xml.dom.EMPTY_NAMESPACE) self.assertEqual(pi.namespaceURI, xml.dom.EMPTY_NAMESPACE)
def testProcessingInstructionRepr(self): def testProcessingInstructionRepr(self):
dom = parseString('<e><?mypi \t\n data \t\n ?></e>') dom = parseString('<e><?mypi \t\n data \t\n ?></e>')
@ -718,19 +716,16 @@ class MinidomTest(unittest.TestCase):
keys2 = list(attrs2.keys()) keys2 = list(attrs2.keys())
keys1.sort() keys1.sort()
keys2.sort() keys2.sort()
self.assertEqual(keys1, keys2, self.assertEqual(keys1, keys2)
"clone of element has same attribute keys")
for i in range(len(keys1)): for i in range(len(keys1)):
a1 = attrs1.item(i) a1 = attrs1.item(i)
a2 = attrs2.item(i) a2 = attrs2.item(i)
self.confirm(a1 is not a2 self.assertIsNot(a1, a2)
and a1.value == a2.value self.assertEqual(a1.value, a2.value)
and a1.nodeValue == a2.nodeValue self.assertEqual(a1.nodeValue, a2.nodeValue)
and a1.namespaceURI == a2.namespaceURI self.assertEqual(a1.namespaceURI,a2.namespaceURI)
and a1.localName == a2.localName self.assertEqual(a1.localName, a2.localName)
, "clone of attribute node has proper attribute values") self.assertIs(a2.ownerElement, e2)
self.assertIs(a2.ownerElement, e2,
"clone of attribute node correctly owned")
def _setupCloneElement(self, deep): def _setupCloneElement(self, deep):
dom = parseString("<doc attr='value'><foo/></doc>") dom = parseString("<doc attr='value'><foo/></doc>")
@ -746,20 +741,19 @@ class MinidomTest(unittest.TestCase):
def testCloneElementShallow(self): def testCloneElementShallow(self):
dom, clone = self._setupCloneElement(0) dom, clone = self._setupCloneElement(0)
self.confirm(len(clone.childNodes) == 0 self.assertEqual(len(clone.childNodes), 0)
and clone.childNodes.length == 0 self.assertEqual(clone.childNodes.length, 0)
and clone.parentNode is None self.assertIsNone(clone.parentNode)
and clone.toxml() == '<doc attr="value"/>' self.assertEqual(clone.toxml(), '<doc attr="value"/>')
, "testCloneElementShallow")
dom.unlink() dom.unlink()
def testCloneElementDeep(self): def testCloneElementDeep(self):
dom, clone = self._setupCloneElement(1) dom, clone = self._setupCloneElement(1)
self.confirm(len(clone.childNodes) == 1 self.assertEqual(len(clone.childNodes), 1)
and clone.childNodes.length == 1 self.assertEqual(clone.childNodes.length, 1)
and clone.parentNode is None self.assertIsNone(clone.parentNode)
and clone.toxml() == '<doc attr="value"><foo/></doc>' self.assertTrue(clone.toxml(), '<doc attr="value"><foo/></doc>')
, "testCloneElementDeep")
dom.unlink() dom.unlink()
def testCloneDocumentShallow(self): def testCloneDocumentShallow(self):