Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 88 additions & 94 deletions Lib/test/test_minidom.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,41 +102,38 @@ def testInsertBefore(self):
elem = root.childNodes[0]
nelem = dom.createElement("element")
root.insertBefore(nelem, elem)
self.confirm(len(root.childNodes) == 2
and root.childNodes.length == 2
and root.childNodes[0] is nelem
and root.childNodes.item(0) is nelem
and root.childNodes[1] is elem
and root.childNodes.item(1) is elem
and root.firstChild is nelem
and root.lastChild is elem
and root.toxml() == "<doc><element/><foo/></doc>"
, "testInsertBefore -- node properly placed in tree")
self.assertEqual(len(root.childNodes), 2)
self.assertEqual(root.childNodes.length, 2)
self.assertIs(root.childNodes[0], nelem)
self.assertIs(root.childNodes.item(0), nelem)
self.assertIs(root.childNodes[1], elem)
self.assertIs(root.childNodes.item(1), elem)
self.assertIs(root.firstChild, nelem)
self.assertIs(root.lastChild, elem)
self.assertEqual(root.toxml(), "<doc><element/><foo/></doc>")
nelem = dom.createElement("element")
root.insertBefore(nelem, None)
self.confirm(len(root.childNodes) == 3
and root.childNodes.length == 3
and root.childNodes[1] is elem
and root.childNodes.item(1) is elem
and root.childNodes[2] is nelem
and root.childNodes.item(2) is nelem
and root.lastChild is nelem
and nelem.previousSibling is elem
and root.toxml() == "<doc><element/><foo/><element/></doc>"
, "testInsertBefore -- node properly placed in tree")
self.assertEqual(len(root.childNodes), 3)
self.assertEqual(root.childNodes.length, 3)
self.assertIs(root.childNodes[1], elem)
self.assertIs(root.childNodes.item(1), elem)
self.assertIs(root.childNodes[2], nelem)
self.assertIs(root.childNodes.item(2), nelem)
self.assertIs(root.lastChild, nelem)
self.assertIs(nelem.previousSibling, elem)
self.assertEqual(root.toxml(), "<doc><element/><foo/><element/></doc>")
nelem2 = dom.createElement("bar")
root.insertBefore(nelem2, nelem)
self.confirm(len(root.childNodes) == 4
and root.childNodes.length == 4
and root.childNodes[2] is nelem2
and root.childNodes.item(2) is nelem2
and root.childNodes[3] is nelem
and root.childNodes.item(3) is nelem
and nelem2.nextSibling is nelem
and nelem.previousSibling is nelem2
and root.toxml() ==
"<doc><element/><foo/><bar/><element/></doc>"
, "testInsertBefore -- node properly placed in tree")
self.assertEqual(len(root.childNodes), 4)
self.assertEqual(root.childNodes.length, 4)
self.assertIs(root.childNodes[2], nelem2)
self.assertIs(root.childNodes.item(2), nelem2)
self.assertIs(root.childNodes[3], nelem)
self.assertIs(root.childNodes.item(3), nelem)
self.assertIs(nelem2.nextSibling, nelem)
self.assertIs(nelem.previousSibling, nelem2)
self.assertEqual(root.toxml(),
"<doc><element/><foo/><bar/><element/></doc>")
dom.unlink()

def _create_fragment_test_nodes(self):
Expand Down Expand Up @@ -342,8 +339,8 @@ def testRemoveAttributeNode(self):
self.assertRaises(xml.dom.NotFoundErr, child.removeAttributeNode,
None)
self.assertIs(node, child.removeAttributeNode(node))
self.confirm(len(child.attributes) == 0
and child.getAttributeNode("spam") is None)
self.assertEqual(len(child.attributes), 0)
self.assertIsNone(child.getAttributeNode("spam"))
dom2 = Document()
child2 = dom2.appendChild(dom2.createElement("foo"))
node2 = child2.getAttributeNode("spam")
Expand All @@ -366,33 +363,34 @@ def testChangeAttr(self):
# Set this attribute to be an ID and make sure that doesn't change
# when changing the value:
el.setIdAttribute("spam")
self.confirm(len(el.attributes) == 1
and el.attributes["spam"].value == "bam"
and el.attributes["spam"].nodeValue == "bam"
and el.getAttribute("spam") == "bam"
and el.getAttributeNode("spam").isId)
self.assertEqual(len(el.attributes), 1)
self.assertEqual(el.attributes["spam"].value, "bam")
self.assertEqual(el.attributes["spam"].nodeValue, "bam")
self.assertEqual(el.getAttribute("spam"), "bam")
self.assertTrue(el.getAttributeNode("spam").isId)
el.attributes["spam"] = "ham"
self.confirm(len(el.attributes) == 1
and el.attributes["spam"].value == "ham"
and el.attributes["spam"].nodeValue == "ham"
and el.getAttribute("spam") == "ham"
and el.attributes["spam"].isId)
self.assertEqual(len(el.attributes), 1)
self.assertEqual(el.attributes["spam"].value, "ham")
self.assertEqual(el.attributes["spam"].nodeValue, "ham")
self.assertEqual(el.getAttribute("spam"), "ham")
self.assertTrue(el.attributes["spam"].isId)
el.setAttribute("spam2", "bam")
self.confirm(len(el.attributes) == 2
and el.attributes["spam"].value == "ham"
and el.attributes["spam"].nodeValue == "ham"
and el.getAttribute("spam") == "ham"
and el.attributes["spam2"].value == "bam"
and el.attributes["spam2"].nodeValue == "bam"
and el.getAttribute("spam2") == "bam")
self.assertEqual(len(el.attributes), 2)
self.assertEqual(el.attributes["spam"].value, "ham")
self.assertEqual(el.attributes["spam"].nodeValue, "ham")
self.assertEqual(el.getAttribute("spam"), "ham")
self.assertEqual(el.attributes["spam2"].value, "bam")
self.assertEqual(el.attributes["spam2"].nodeValue, "bam")
self.assertEqual(el.getAttribute("spam2"), "bam")
el.attributes["spam2"] = "bam2"
self.confirm(len(el.attributes) == 2
and el.attributes["spam"].value == "ham"
and el.attributes["spam"].nodeValue == "ham"
and el.getAttribute("spam") == "ham"
and el.attributes["spam2"].value == "bam2"
and el.attributes["spam2"].nodeValue == "bam2"
and el.getAttribute("spam2") == "bam2")

self.assertEqual(len(el.attributes), 2)
self.assertEqual(el.attributes["spam"].value, "ham")
self.assertEqual(el.attributes["spam"].nodeValue, "ham")
self.assertEqual(el.getAttribute("spam"), "ham")
self.assertEqual(el.attributes["spam2"].value, "bam2")
self.assertEqual(el.attributes["spam2"].nodeValue, "bam2")
self.assertEqual(el.getAttribute("spam2"), "bam2")
dom.unlink()

def testGetAttrList(self):
Expand Down Expand Up @@ -433,12 +431,12 @@ def testGetElementsByTagNameNS(self):
dom = parseString(d)
elems = dom.getElementsByTagNameNS("http://pyxml.sf.net/minidom",
"myelem")
self.confirm(len(elems) == 1
and elems[0].namespaceURI == "http://pyxml.sf.net/minidom"
and elems[0].localName == "myelem"
and elems[0].prefix == "minidom"
and elems[0].tagName == "minidom:myelem"
and elems[0].nodeName == "minidom:myelem")
self.assertEqual(len(elems), 1)
self.assertEqual(elems[0].namespaceURI, "http://pyxml.sf.net/minidom")
self.assertEqual(elems[0].localName, "myelem")
self.assertEqual(elems[0].prefix, "minidom")
self.assertEqual(elems[0].tagName, "minidom:myelem")
self.assertEqual(elems[0].nodeName, "minidom:myelem")
dom.unlink()

def get_empty_nodelist_from_elements_by_tagName_ns_helper(self, doc, nsuri,
Expand Down Expand Up @@ -589,17 +587,17 @@ def test_toprettyxml_preserves_content_of_text_node(self):
def testProcessingInstruction(self):
dom = parseString('<e><?mypi \t\n data \t\n ?></e>')
pi = dom.documentElement.firstChild
self.confirm(pi.target == "mypi"
and pi.data == "data \t\n "
and pi.nodeName == "mypi"
and pi.nodeType == Node.PROCESSING_INSTRUCTION_NODE
and pi.attributes is None
and not pi.hasChildNodes()
and len(pi.childNodes) == 0
and pi.firstChild is None
and pi.lastChild is None
and pi.localName is None
and pi.namespaceURI == xml.dom.EMPTY_NAMESPACE)
self.assertEqual(pi.target, "mypi")
self.assertEqual(pi.data, "data \t\n ")
self.assertEqual(pi.nodeName, "mypi")
self.assertEqual(pi.nodeType, Node.PROCESSING_INSTRUCTION_NODE)
self.assertIsNone(pi.attributes)
self.assertFalse(pi.hasChildNodes())
self.assertEqual(len(pi.childNodes), 0)
self.assertIsNone(pi.firstChild)
self.assertIsNone(pi.lastChild)
self.assertIsNone(pi.localName)
self.assertEqual(pi.namespaceURI, xml.dom.EMPTY_NAMESPACE)

def testProcessingInstructionRepr(self): pass

Expand Down Expand Up @@ -695,19 +693,16 @@ def _testCloneElementCopiesAttributes(self, e1, e2, test):
keys2 = list(attrs2.keys())
keys1.sort()
keys2.sort()
self.assertEqual(keys1, keys2,
"clone of element has same attribute keys")
self.assertEqual(keys1, keys2)
for i in range(len(keys1)):
a1 = attrs1.item(i)
a2 = attrs2.item(i)
self.confirm(a1 is not a2
and a1.value == a2.value
and a1.nodeValue == a2.nodeValue
and a1.namespaceURI == a2.namespaceURI
and a1.localName == a2.localName
, "clone of attribute node has proper attribute values")
self.assertIs(a2.ownerElement, e2,
"clone of attribute node correctly owned")
self.assertIsNot(a1, a2)
self.assertEqual(a1.value, a2.value)
self.assertEqual(a1.nodeValue, a2.nodeValue)
self.assertEqual(a1.namespaceURI,a2.namespaceURI)
self.assertEqual(a1.localName, a2.localName)
self.assertIs(a2.ownerElement, e2)

def _setupCloneElement(self, deep):
dom = parseString("<doc attr='value'><foo/></doc>")
Expand All @@ -723,20 +718,19 @@ def _setupCloneElement(self, deep):

def testCloneElementShallow(self):
dom, clone = self._setupCloneElement(0)
self.confirm(len(clone.childNodes) == 0
and clone.childNodes.length == 0
and clone.parentNode is None
and clone.toxml() == '<doc attr="value"/>'
, "testCloneElementShallow")
self.assertEqual(len(clone.childNodes), 0)
self.assertEqual(clone.childNodes.length, 0)
self.assertIsNone(clone.parentNode)
self.assertEqual(clone.toxml(), '<doc attr="value"/>')

dom.unlink()

def testCloneElementDeep(self):
dom, clone = self._setupCloneElement(1)
self.confirm(len(clone.childNodes) == 1
and clone.childNodes.length == 1
and clone.parentNode is None
and clone.toxml() == '<doc attr="value"><foo/></doc>'
, "testCloneElementDeep")
self.assertEqual(len(clone.childNodes), 1)
self.assertEqual(clone.childNodes.length, 1)
self.assertIsNone(clone.parentNode)
self.assertTrue(clone.toxml(), '<doc attr="value"><foo/></doc>')
dom.unlink()

def testCloneDocumentShallow(self):
Expand Down
Loading