Skip to content

Commit a4f6216

Browse files
[3.14] gh-63882: Implement empty tests in test_minidom (GH-156677) (GH-157426)
25 tests were defined as "def testX(self): pass" and reported success without testing anything. (cherry picked from commit f154574) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 38b79e8 commit a4f6216

1 file changed

Lines changed: 291 additions & 25 deletions

File tree

Lib/test/test_minidom.py

Lines changed: 291 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,15 @@ def testGetAttributeNS(self):
480480
self.assertEqual(child2.getAttributeNS("http://www.python.org", "missing"),
481481
'')
482482

483-
def testGetAttributeNode(self): pass
483+
def testGetAttributeNode(self):
484+
dom = parseString("<doc a='1'/>")
485+
elem = dom.documentElement
486+
attr = elem.getAttributeNode("a")
487+
self.assertEqual(attr.name, "a")
488+
self.assertEqual(attr.value, "1")
489+
self.assertIs(attr.ownerElement, elem)
490+
self.assertIsNone(elem.getAttributeNode("b"))
491+
dom.unlink()
484492

485493
def testGetElementsByTagNameNS(self):
486494
d="""<foo xmlns:minidom='http://pyxml.sf.net/minidom'>
@@ -693,9 +701,34 @@ def testTextRepr(self):
693701
self.assertEqual(str(el), repr(el))
694702
self.assertEqual('<DOM Text node "\'foo\'">', str(el))
695703

696-
def testWriteText(self): pass
704+
def testWriteText(self):
705+
dom = parseString("<doc><a>text</a><b>&lt;&amp;&gt;</b></doc>")
706+
elem = dom.documentElement
707+
writer = io.StringIO()
708+
elem.writexml(writer)
709+
self.assertEqual(writer.getvalue(),
710+
"<doc><a>text</a><b>&lt;&amp;&gt;</b></doc>")
711+
writer = io.StringIO()
712+
elem.writexml(writer, indent=" ", addindent=" ", newl="\n")
713+
self.assertEqual(writer.getvalue(),
714+
" <doc>\n"
715+
" <a>text</a>\n"
716+
" <b>&lt;&amp;&gt;</b>\n"
717+
" </doc>\n")
718+
dom.unlink()
697719

698-
def testDocumentElement(self): pass
720+
def testDocumentElement(self):
721+
dom = parseString("<!-- comment --><doc/><?pi data?>")
722+
elem = dom.documentElement
723+
self.assertEqual(elem.tagName, "doc")
724+
self.assertIs(elem, dom.childNodes[1])
725+
dom.unlink()
726+
727+
dom = Document()
728+
self.assertIsNone(dom.documentElement)
729+
elem = dom.appendChild(dom.createElement("doc"))
730+
self.assertIs(dom.documentElement, elem)
731+
dom.unlink()
699732

700733
def testTooManyDocumentElements(self):
701734
doc = parseString("<doc/>")
@@ -705,25 +738,126 @@ def testTooManyDocumentElements(self):
705738
elem.unlink()
706739
doc.unlink()
707740

708-
def testCreateElementNS(self): pass
741+
def testCreateElementNS(self):
742+
dom = Document()
743+
elem = dom.createElementNS("http://xml.python.org/ns", "p:elem")
744+
self.assertEqual(elem.nodeType, Node.ELEMENT_NODE)
745+
self.assertEqual(elem.tagName, "p:elem")
746+
self.assertEqual(elem.nodeName, "p:elem")
747+
self.assertEqual(elem.namespaceURI, "http://xml.python.org/ns")
748+
self.assertEqual(elem.prefix, "p")
749+
self.assertEqual(elem.localName, "elem")
750+
self.assertIs(elem.ownerDocument, dom)
751+
self.assertIsNone(elem.parentNode)
752+
753+
elem = dom.createElementNS("http://xml.python.org/ns", "elem")
754+
self.assertEqual(elem.tagName, "elem")
755+
self.assertIsNone(elem.prefix)
756+
self.assertEqual(elem.localName, "elem")
757+
dom.unlink()
709758

710-
def testCreateAttributeNS(self): pass
759+
def testCreateAttributeNS(self):
760+
dom = Document()
761+
attr = dom.createAttributeNS("http://xml.python.org/ns", "p:attr")
762+
self.assertEqual(attr.nodeType, Node.ATTRIBUTE_NODE)
763+
self.assertEqual(attr.name, "p:attr")
764+
self.assertEqual(attr.nodeName, "p:attr")
765+
self.assertEqual(attr.namespaceURI, "http://xml.python.org/ns")
766+
self.assertEqual(attr.prefix, "p")
767+
self.assertEqual(attr.localName, "attr")
768+
self.assertEqual(attr.value, "")
769+
self.assertIs(attr.ownerDocument, dom)
770+
self.assertIsNone(attr.ownerElement)
771+
772+
elem = dom.appendChild(dom.createElement("doc"))
773+
elem.setAttributeNode(attr)
774+
self.assertIs(attr.ownerElement, elem)
775+
self.assertIs(elem.getAttributeNodeNS("http://xml.python.org/ns",
776+
"attr"), attr)
777+
dom.unlink()
711778

712-
def testParse(self): pass
779+
def testParse(self):
780+
# parsing from a file object is tested in testParseFromBinaryFile
781+
# and testParseFromTextFile
782+
dom = parse(tstfile)
783+
self.assertEqual(dom.nodeType, Node.DOCUMENT_NODE)
784+
self.assertEqual(dom.documentElement.tagName, "HTML")
785+
dom.unlink()
713786

714-
def testParseString(self): pass
787+
self.assertRaises(ExpatError, parseString, "<doc>")
715788

716-
def testComment(self): pass
789+
def testParseString(self):
790+
dom = parseString("<doc>text</doc>")
791+
self.assertEqual(dom.nodeType, Node.DOCUMENT_NODE)
792+
self.assertEqual(dom.documentElement.tagName, "doc")
793+
self.assertEqual(dom.documentElement.firstChild.data, "text")
794+
dom.unlink()
795+
796+
dom = parseString(b"<?xml version='1.0' encoding='utf-8'?>"
797+
b"<doc>\xc3\xa9</doc>")
798+
self.assertEqual(dom.documentElement.firstChild.data, "\xe9")
799+
dom.unlink()
800+
801+
def testComment(self):
802+
dom = Document()
803+
comment = dom.createComment("comment")
804+
self.assertEqual(comment.nodeType, Node.COMMENT_NODE)
805+
self.assertEqual(comment.nodeName, "#comment")
806+
self.assertEqual(comment.data, "comment")
807+
self.assertEqual(comment.nodeValue, "comment")
808+
self.assertIsNone(comment.attributes)
809+
dom.appendChild(comment)
810+
self.assertEqual(dom.toxml(),
811+
'<?xml version="1.0" ?><!--comment-->')
812+
dom.unlink()
717813

718-
def testAttrListItem(self): pass
814+
dom = parseString("<doc><!--comment--></doc>")
815+
comment = dom.documentElement.firstChild
816+
self.assertEqual(comment.nodeType, Node.COMMENT_NODE)
817+
self.assertEqual(comment.data, "comment")
818+
dom.unlink()
819+
820+
def testAttrListItem(self):
821+
dom = parseString("<doc a='1' b='2'/>")
822+
attrs = dom.documentElement.attributes
823+
self.assertEqual(attrs.item(0).name, "a")
824+
self.assertEqual(attrs.item(1).name, "b")
825+
self.assertIsNone(attrs.item(2))
826+
dom.unlink()
719827

720-
def testAttrListItems(self): pass
828+
def testAttrListItems(self):
829+
dom = parseString("<doc a='1' b='2'/>")
830+
attrs = dom.documentElement.attributes
831+
self.assertEqual(attrs.items(), [("a", "1"), ("b", "2")])
832+
dom.unlink()
721833

722-
def testAttrListItemNS(self): pass
834+
def testAttrListItemNS(self):
835+
dom = parseString("<doc xmlns:p='http://xml.python.org/ns' "
836+
"p:a='1' b='2'/>")
837+
attrs = dom.documentElement.attributes
838+
self.assertEqual(attrs.itemsNS(), [
839+
((xml.dom.XMLNS_NAMESPACE, "p"), "http://xml.python.org/ns"),
840+
(("http://xml.python.org/ns", "a"), "1"),
841+
((None, "b"), "2"),
842+
])
843+
dom.unlink()
723844

724-
def testAttrListKeys(self): pass
845+
def testAttrListKeys(self):
846+
dom = parseString("<doc a='1' b='2'/>")
847+
attrs = dom.documentElement.attributes
848+
self.assertEqual(list(attrs.keys()), ["a", "b"])
849+
dom.unlink()
725850

726-
def testAttrListKeysNS(self): pass
851+
def testAttrListKeysNS(self):
852+
dom = parseString("<doc xmlns:p='http://xml.python.org/ns' "
853+
"p:a='1' b='2'/>")
854+
attrs = dom.documentElement.attributes
855+
self.assertEqual(list(attrs.keysNS()), [
856+
(xml.dom.XMLNS_NAMESPACE, "p"),
857+
("http://xml.python.org/ns", "a"),
858+
(None, "b"),
859+
])
860+
dom.unlink()
727861

728862
def testRemoveNamedItem(self):
729863
doc = parseString("<doc a=''/>")
@@ -744,29 +878,161 @@ def testRemoveNamedItemNS(self):
744878
self.assertRaises(xml.dom.NotFoundErr, attrs.removeNamedItemNS,
745879
"http://xml.python.org/", "b")
746880

747-
def testAttrListValues(self): pass
881+
def testAttrListValues(self):
882+
dom = parseString("<doc a='1' b='2'/>")
883+
attrs = dom.documentElement.attributes
884+
self.assertEqual([attr.name for attr in attrs.values()], ["a", "b"])
885+
self.assertEqual([attr.value for attr in attrs.values()], ["1", "2"])
886+
dom.unlink()
748887

749-
def testAttrListLength(self): pass
888+
def testAttrListLength(self):
889+
dom = parseString("<doc a='1' b='2'/>")
890+
attrs = dom.documentElement.attributes
891+
self.assertEqual(attrs.length, 2)
892+
self.assertEqual(len(attrs), 2)
893+
dom.unlink()
750894

751-
def testAttrList__getitem__(self): pass
895+
dom = parseString("<doc/>")
896+
self.assertEqual(dom.documentElement.attributes.length, 0)
897+
dom.unlink()
752898

753-
def testAttrList__setitem__(self): pass
899+
def testAttrList__getitem__(self):
900+
dom = parseString("<doc xmlns:p='http://xml.python.org/ns' "
901+
"p:a='1' b='2'/>")
902+
attrs = dom.documentElement.attributes
903+
self.assertEqual(attrs["b"].value, "2")
904+
self.assertEqual(attrs[("http://xml.python.org/ns", "a")].value, "1")
905+
self.assertRaises(KeyError, attrs.__getitem__, "missing")
906+
self.assertRaises(KeyError, attrs.__getitem__, (None, "missing"))
907+
dom.unlink()
754908

755-
def testSetAttrValueandNodeValue(self): pass
909+
def testAttrList__setitem__(self):
910+
dom = parseString("<doc a='1'/>")
911+
elem = dom.documentElement
912+
attrs = elem.attributes
913+
attrs["a"] = "2"
914+
self.assertEqual(elem.getAttribute("a"), "2")
915+
attrs["b"] = "3"
916+
self.assertEqual(elem.getAttribute("b"), "3")
917+
self.assertEqual(attrs.length, 2)
918+
919+
attr = dom.createAttribute("c")
920+
attr.value = "4"
921+
attrs["c"] = attr
922+
self.assertIs(elem.getAttributeNode("c"), attr)
923+
self.assertEqual(elem.getAttribute("c"), "4")
924+
dom.unlink()
756925

757-
def testParseElement(self): pass
926+
def testSetAttrValueandNodeValue(self):
927+
dom = parseString("<doc a='1'/>")
928+
attr = dom.documentElement.getAttributeNode("a")
929+
self.assertEqual(attr.value, "1")
930+
self.assertEqual(attr.nodeValue, "1")
931+
attr.value = "2"
932+
self.assertEqual(attr.nodeValue, "2")
933+
attr.nodeValue = "3"
934+
self.assertEqual(attr.value, "3")
935+
self.assertEqual(dom.documentElement.getAttribute("a"), "3")
936+
dom.unlink()
758937

759-
def testParseAttributes(self): pass
938+
def testParseElement(self):
939+
dom = parseString("<doc><child/><child/></doc>")
940+
elem = dom.documentElement
941+
self.assertEqual(elem.nodeType, Node.ELEMENT_NODE)
942+
self.assertEqual(elem.tagName, "doc")
943+
self.assertIsNone(elem.namespaceURI)
944+
self.assertIs(elem.parentNode, dom)
945+
self.assertIs(elem.ownerDocument, dom)
946+
self.assertEqual([child.tagName for child in elem.childNodes],
947+
["child", "child"])
948+
dom.unlink()
760949

761-
def testParseElementNamespaces(self): pass
950+
def testParseAttributes(self):
951+
dom = parseString("<doc a='1' b='&amp;'/>")
952+
elem = dom.documentElement
953+
self.assertEqual(elem.getAttribute("a"), "1")
954+
self.assertEqual(elem.getAttribute("b"), "&")
955+
self.assertEqual(elem.getAttribute("missing"), "")
956+
self.assertTrue(elem.hasAttribute("a"))
957+
self.assertFalse(elem.hasAttribute("missing"))
958+
attr = elem.getAttributeNode("a")
959+
self.assertIsNone(attr.namespaceURI)
960+
dom.unlink()
762961

763-
def testParseAttributeNamespaces(self): pass
962+
def testParseElementNamespaces(self):
963+
dom = parseString("<p:doc xmlns:p='http://xml.python.org/ns'"
964+
" xmlns='http://xml.python.org/default'>"
965+
"<child/></p:doc>")
966+
elem = dom.documentElement
967+
self.assertEqual(elem.tagName, "p:doc")
968+
self.assertEqual(elem.namespaceURI, "http://xml.python.org/ns")
969+
self.assertEqual(elem.prefix, "p")
970+
self.assertEqual(elem.localName, "doc")
971+
child = elem.getElementsByTagName("child")[0]
972+
self.assertEqual(child.namespaceURI, "http://xml.python.org/default")
973+
self.assertIsNone(child.prefix)
974+
self.assertEqual(child.localName, "child")
975+
dom.unlink()
764976

765-
def testParseProcessingInstructions(self): pass
977+
def testParseAttributeNamespaces(self):
978+
dom = parseString("<doc xmlns:p='http://xml.python.org/ns'"
979+
" p:a='1' b='2'/>")
980+
elem = dom.documentElement
981+
self.assertEqual(elem.getAttributeNS("http://xml.python.org/ns", "a"),
982+
"1")
983+
self.assertEqual(elem.getAttributeNS(None, "b"), "2")
984+
attr = elem.getAttributeNodeNS("http://xml.python.org/ns", "a")
985+
self.assertEqual(attr.name, "p:a")
986+
self.assertEqual(attr.prefix, "p")
987+
self.assertEqual(attr.localName, "a")
988+
declaration = elem.getAttributeNode("xmlns:p")
989+
self.assertEqual(declaration.namespaceURI, xml.dom.XMLNS_NAMESPACE)
990+
self.assertEqual(declaration.value, "http://xml.python.org/ns")
991+
dom.unlink()
766992

767-
def testChildNodes(self): pass
993+
def testParseProcessingInstructions(self):
994+
# the content of a processing instruction is tested
995+
# in testProcessingInstruction
996+
dom = parseString("<?before data?><doc/><?after?>")
997+
pi = dom.childNodes[0]
998+
self.assertEqual(pi.nodeType, Node.PROCESSING_INSTRUCTION_NODE)
999+
self.assertEqual(pi.target, "before")
1000+
self.assertEqual(pi.data, "data")
1001+
self.assertIs(pi.parentNode, dom)
1002+
pi = dom.childNodes[2]
1003+
self.assertEqual(pi.target, "after")
1004+
self.assertEqual(pi.data, "")
1005+
dom.unlink()
1006+
1007+
def testChildNodes(self):
1008+
dom = parseString("<doc>text<child/><!--comment--></doc>")
1009+
children = dom.documentElement.childNodes
1010+
self.assertEqual(len(children), 3)
1011+
self.assertEqual([child.nodeType for child in children],
1012+
[Node.TEXT_NODE, Node.ELEMENT_NODE, Node.COMMENT_NODE])
1013+
for child in children:
1014+
self.assertIs(child.parentNode, dom.documentElement)
1015+
dom.unlink()
1016+
1017+
dom = parseString("<doc/>")
1018+
self.assertEqual(len(dom.documentElement.childNodes), 0)
1019+
dom.unlink()
1020+
1021+
def testFirstChild(self):
1022+
dom = parseString("<doc><a/><b/></doc>")
1023+
elem = dom.documentElement
1024+
self.assertEqual(elem.firstChild.tagName, "a")
1025+
self.assertEqual(elem.lastChild.tagName, "b")
1026+
self.assertIs(elem.firstChild, elem.childNodes[0])
1027+
self.assertIs(elem.lastChild, elem.childNodes[-1])
1028+
self.assertIsNone(elem.firstChild.previousSibling)
1029+
self.assertIs(elem.firstChild.nextSibling, elem.lastChild)
1030+
dom.unlink()
7681031

769-
def testFirstChild(self): pass
1032+
dom = parseString("<doc/>")
1033+
self.assertIsNone(dom.documentElement.firstChild)
1034+
self.assertIsNone(dom.documentElement.lastChild)
1035+
dom.unlink()
7701036

7711037
def testHasChildNodes(self):
7721038
dom = parseString("<doc><foo/></doc>")

0 commit comments

Comments
 (0)