8000 gh-63882: Adds tests for xml.dom.minidom by karlcow · Pull Request #24152 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-63882: Adds tests for xml.dom.minidom #24152

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

Closed
wants to merge 21 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
afefc8a
bpo-19683: Changes the docstring for the test file
karlcow Jan 7, 2021
24fd486
bpo-19683: Adds tests for toxml method
karlcow Jan 7, 2021
172eacd
bpo-19683: Changes the _repr_ for Node Element
karlcow Jan 7, 2021
0d60669
bpo-19683: Reorganize import for clarity
karlcow Jan 7, 2021
c106ce5
bpo-19683: Adds encoding test for toxml
karlcow Jan 7, 2021
847c6f2
bpo-19683: Reformats hasChildNodes test
karlcow Jan 7, 2021
c488947
bpo-19683: Adds test for childNodes
karlcow Jan 7, 2021
dc3f100
bpo-19683: Adds firstChild and lastChild tests
karlcow Jan 7, 2021
14ad87c
bpo-19683: Adds test for insertBefore DocumentFragment
karlcow Jan 11, 2021
a009350
bpo-19683: Adds test for insertBefore for invalid NodeType
karlcow Jan 15, 2021
709a640
bpo-19683: Adds test for insertBefore for text node
karlcow Jan 15, 2021
b6fe778
bpo-19683: Adds test for insertBefore without ref node
karlcow Jan 15, 2021
95aafaf
bpo-19683: Adds tests for insertBefore
karlcow Jan 19, 2021
896543e
bpo-19683: Adds tests for appendChild
karlcow Jan 19, 2021
4dc05ca
bpo-19683: Removes tests which are empty
karlcow Jan 19, 2021
ff99b90
bpo-19683: Adds tests firstChild, lastChild
karlcow Jan 19, 2021
e5654e8
bpo-19683: Adds tests for replaceChild to xml.dom.test_minidom
karlcow Jan 19, 2021
0da118b
bpo-19683: Adds tests for removeChild
karlcow Jan 19, 2021
d0d51f2
bpo-19683: Adds tests for normalize. Part 1
karlcow Feb 19, 2021
52c26c5
bpo-19683: Adds tests for normalize. Part 2
karlcow Feb 19, 2021
c8ef460
bpo-19683: Adds cloneNode tests. Part 1
karlcow Feb 19, 2021
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
Prev Previous commit
Next Next commit
bpo-19683: Adds tests for replaceChild to xml.dom.test_minidom
* Convert old tests to have a regular form
* Adds new tests to cover more cases.
  • Loading branch information
karlcow committed Jan 19, 2021
commit e5654e83bcd906310679fbe02b9492a503cd3548
97 changes: 79 additions & 18 deletions Lib/test/test_minidom.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,6 @@ def _create_fragment_test_nodes(self):
frag.appendChild(c3)
return dom, orig, c1, c2, c3, frag

def testReplaceChildFragment(self):
dom, orig, c1, c2, c3, frag = self._create_fragment_test_nodes()
dom.documentElement.replaceChild(frag, orig)
orig.unlink()
self.confirm(tuple(dom.documentElement.childNodes) == (c1, c2, c3),
"replaceChild(<fragment>)")
frag.unlink()
dom.unlink()

def testLegalChildren(self):
dom = Document()
elem = dom.createElement('element')
Expand Down Expand Up @@ -1228,15 +1219,6 @@ def testWholeText(self):
self.checkWholeText(text, "cabd")
self.checkWholeText(text2, "cabd")

def testPatch1094164(self):
doc = parseString("<doc><e/></doc>")
elem = doc.documentElement
e = elem.firstChild
self.confirm(e.parentNode is elem, "Before replaceChild()")
# Check that replacing a child with itself leaves the tree unchanged
elem.replaceChild(e, e)
self.confirm(e.parentNode is elem, "After replaceChild()")

def testReplaceWholeText(self):
def setup():
doc = parseString("<doc>a<e/>d</doc>")
Expand Down Expand Up @@ -1686,5 +1668,84 @@ def test_appendChild_return_value(self):
return_value = parentNode.appendChild(newNode)
self.assertEqual(return_value, newNode)

def test_replaceChild(self):
"""Test replaceChild for a simple node."""
dom = parseString('<parent><existing/></parent>')
parentNode = dom.documentElement
existingNode = parentNode.firstChild
newNode = dom.createElement('new')
# replace existingNode by newNode
parentNode.replaceChild(newNode, existingNode)
self.assertEqual(parentNode.toxml(), '<parent><new/></parent>')

def test_replaceChild_with_document_fragment_node(self):
"""Test replaceChild for DOCUMENT_FRAGMENT_NODE."""
# Preparing the test
dom = parseString('<parent><existing/></parent>')
parentNode = dom.documentElement
existingNode = parentNode.firstChild
newNode = dom.createElement('new')
fragment = DocumentFragment()
fragment.appendChild(newNode)
# replaceChild with document fragment
parentNode.replaceChild(fragment, existingNode)
self.assertEqual(parentNode.toxml(), '<parent><new/></parent>')
# after the replaceChild, the fragment should be empty
self.assertEqual(fragment.childNodes.length, 0)

def test_replaceChild_with_invalid_node_type(self):
"""Test replaceChild with invalid node type."""
# Preparing the test
dom = parseString('<parent><existing/></parent>')
parentNode = dom.documentElement
existingNode = parentNode.firstChild
doc = getDOMImplementation().createDocument(None, "doc", None)
# parentNode.replaceChild(doc, existingNode) will raise
self.assertRaises(xml.dom.HierarchyRequestErr,
parentNode.replaceChild, doc, existingNode)

def test_replaceChild_with_same_node(self):
"""Test replaceChild with same node."""
dom = parseString('<parent><existing/></parent>')
parentNode = dom.documentElement
existingNode = parentNode.firstChild
# replace existingNode by existingNode
parentNode.replaceChild(existingNode, existingNode)
self.assertEqual(parentNode.toxml(), '<parent><existing/></parent>')

def test_replaceChild_with_new_child_parent_not_None(self):
"""Test replaceChild with new child parent is not Nonce."""
dom = parseString('<parent><existing/></parent>')
parentNode = dom.documentElement
existingNode = parentNode.firstChild
newdom = parseString('<newparent><new/></newparent>')
newparentNode = newdom.documentElement
newNode = newparentNode.firstChild
# replace existingNode by newNode
parentNode.replaceChild(newNode, existingNode)
self.assertEqual(parentNode.toxml(), '<parent><new/></parent>')

def test_replaceChild_with_no_existing_node(self):
"""Test replaceChild with missing existing node."""
dom = parseString('<parent></parent>')
parentNode = dom.documentElement
existingNode = dom.createElement('existing')
newdom = parseString('<newparent><new/></newparent>')
newparentNode = newdom.documentElement
newNode = newparentNode.firstChild
# replace existingNode by newNode
self.assertRaises(xml.dom.NotFoundErr,
parentNode.replaceChild, newNode, existingNode)

def test_replaceChild_return_value(self):
"""Test replaceChild returned value."""
dom = parseString('<parent><existing/></parent>')
parentNode = dom.documentElement
existingNode = parentNode.firstChild
newNode = dom.createElement('new')
# replace a new node child
return_value = parentNode.replaceChild(newNode, existingNode)
self.assertEqual(return_value, existingNode)

if __name__ == "__main__":
unittest.main()
0