Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f154574

Browse files
gh-63882: Implement empty tests in test_minidom (GH-156677)
25 tests were defined as "def testX(self): pass" and reported success without testing anything.
1 parent 01599e2 commit f154574

1 file changed

Lines changed: 292 additions & 25 deletions

File tree

Lib/test/test_minidom.py

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

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

487495
def testGetElementsByTagNameNS(self):
488496
d="""<foo xmlns:minidom='http://pyxml.sf.net/minidom'>
@@ -695,9 +703,34 @@ def testTextRepr(self):
695703
self.assertEqual(str(el), repr(el))
696704
self.assertEqual('<DOM Text node "\'foo\'">', str(el))
697705

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

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

702735
def testTooManyDocumentElements(self):
703736
doc = parseString("<doc/>")
@@ -707,25 +740,126 @@ def testTooManyDocumentElements(self):
707740
elem.unlink()
708741
doc.unlink()
709742

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

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

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

716-
def testParseString(self): pass
789+
self.assertRaises(ExpatError, parseString, "<doc>")
717790

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

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

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

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

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

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

730864
def testRemoveNamedItem(self):
731865
doc = parseString("<doc a=''/>")
@@ -746,29 +880,162 @@ def testRemoveNamedItemNS(self):
746880
self.assertRaises(xml.dom.NotFoundErr, attrs.removeNamedItemNS,
747881
"http://xml.python.org/", "b")
748882

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

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

753-
def testAttrList__getitem__(self): pass
897+
dom = parseString("<doc/>")
898+
self.assertEqual(dom.documentElement.attributes.length, 0)
899+
dom.unlink()
754900

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

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

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

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

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

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

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

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

771-
def testFirstChild(self): pass
1035+
dom = parseString("<doc/>")
1036+
self.assertIsNone(dom.documentElement.firstChild)
1037+
self.assertIsNone(dom.documentElement.lastChild)
1038+
dom.unlink()
7721039

7731040
def testHasChildNodes(self):
7741041
dom = parseString("<doc><foo/></doc>")

0 commit comments

Comments
 (0)