Skip to content

Commit 04347ef

Browse files
[3.15] gh-63882: Implement empty tests in test_minidom (GH-156677) (#157428)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent d696e18 commit 04347ef

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'>
@@ -669,9 +677,34 @@ def testTextRepr(self):
669677
self.assertEqual(str(el), repr(el))
670678
self.assertEqual('<DOM Text node "\'foo\'">', str(el))
671679

672-
def testWriteText(self): pass
680+
def testWriteText(self):
681+
dom = parseString("<doc><a>text</a><b>&lt;&amp;&gt;</b></doc>")
682+
elem = dom.documentElement
683+
writer = io.StringIO()
684+
elem.writexml(writer)
685+
self.assertEqual(writer.getvalue(),
686+
"<doc><a>text</a><b>&lt;&amp;&gt;</b></doc>")
687+
writer = io.StringIO()
688+
elem.writexml(writer, indent=" ", addindent=" ", newl="\n")
689+
self.assertEqual(writer.getvalue(),
690+
" <doc>\n"
691+
" <a>text</a>\n"
692+
" <b>&lt;&amp;&gt;</b>\n"
693+
" </doc>\n")
694+
dom.unlink()
673695

674-
def testDocumentElement(self): pass
696+
def testDocumentElement(self):
697+
dom = parseString("<!-- comment --><doc/><?pi data?>")
698+
elem = dom.documentElement
699+
self.assertEqual(elem.tagName, "doc")
700+
self.assertIs(elem, dom.childNodes[1])
701+
dom.unlink()
702+
703+
dom = Document()
704+
self.assertIsNone(dom.documentElement)
705+
elem = dom.appendChild(dom.createElement("doc"))
706+
self.assertIs(dom.documentElement, elem)
707+
dom.unlink()
675708

676709
def testTooManyDocumentElements(self):
677710
doc = parseString("<doc/>")
@@ -681,25 +714,126 @@ def testTooManyDocumentElements(self):
681714
elem.unlink()
682715
doc.unlink()
683716

684-
def testCreateElementNS(self): pass
717+
def testCreateElementNS(self):
718+
dom = Document()
719+
elem = dom.createElementNS("http://xml.python.org/ns", "p:elem")
720+
self.assertEqual(elem.nodeType, Node.ELEMENT_NODE)
721+
self.assertEqual(elem.tagName, "p:elem")
722+
self.assertEqual(elem.nodeName, "p:elem")
723+
self.assertEqual(elem.namespaceURI, "http://xml.python.org/ns")
724+
self.assertEqual(elem.prefix, "p")
725+
self.assertEqual(elem.localName, "elem")
726+
self.assertIs(elem.ownerDocument, dom)
727+
self.assertIsNone(elem.parentNode)
728+
729+
elem = dom.createElementNS("http://xml.python.org/ns", "elem")
730+
self.assertEqual(elem.tagName, "elem")
731+
self.assertIsNone(elem.prefix)
732+
self.assertEqual(elem.localName, "elem")
733+
dom.unlink()
685734

686-
def testCreateAttributeNS(self): pass
735+
def testCreateAttributeNS(self):
736+
dom = Document()
737+
attr = dom.createAttributeNS("http://xml.python.org/ns", "p:attr")
738+
self.assertEqual(attr.nodeType, Node.ATTRIBUTE_NODE)
739+
self.assertEqual(attr.name, "p:attr")
740+
self.assertEqual(attr.nodeName, "p:attr")
741+
self.assertEqual(attr.namespaceURI, "http://xml.python.org/ns")
742+
self.assertEqual(attr.prefix, "p")
743+
self.assertEqual(attr.localName, "attr")
744+
self.assertEqual(attr.value, "")
745+
self.assertIs(attr.ownerDocument, dom)
746+
self.assertIsNone(attr.ownerElement)
747+
748+
elem = dom.appendChild(dom.createElement("doc"))
749+
elem.setAttributeNode(attr)
750+
self.assertIs(attr.ownerElement, elem)
751+
self.assertIs(elem.getAttributeNodeNS("http://xml.python.org/ns",
752+
"attr"), attr)
753+
dom.unlink()
687754

688-
def testParse(self): pass
755+
def testParse(self):
756+
# parsing from a file object is tested in testParseFromBinaryFile
757+
# and testParseFromTextFile
758+
dom = parse(tstfile)
759+
self.assertEqual(dom.nodeType, Node.DOCUMENT_NODE)
760+
self.assertEqual(dom.documentElement.tagName, "HTML")
761+
dom.unlink()
689762

690-
def testParseString(self): pass
763+
self.assertRaises(ExpatError, parseString, "<doc>")
691764

692-
def testComment(self): pass
765+
def testParseString(self):
766+
dom = parseString("<doc>text</doc>")
767+
self.assertEqual(dom.nodeType, Node.DOCUMENT_NODE)
768+
self.assertEqual(dom.documentElement.tagName, "doc")
769+
self.assertEqual(dom.documentElement.firstChild.data, "text")
770+
dom.unlink()
771+
772+
dom = parseString(b"<?xml version='1.0' encoding='utf-8'?>"
773+
b"<doc>\xc3\xa9</doc>")
774+
self.assertEqual(dom.documentElement.firstChild.data, "\xe9")
775+
dom.unlink()
776+
777+
def testComment(self):
778+
dom = Document()
779+
comment = dom.createComment("comment")
780+
self.assertEqual(comment.nodeType, Node.COMMENT_NODE)
781+
self.assertEqual(comment.nodeName, "#comment")
782+
self.assertEqual(comment.data, "comment")
783+
self.assertEqual(comment.nodeValue, "comment")
784+
self.assertIsNone(comment.attributes)
785+
dom.appendChild(comment)
786+
self.assertEqual(dom.toxml(),
787+
'<?xml version="1.0" ?><!--comment-->')
788+
dom.unlink()
693789

694-
def testAttrListItem(self): pass
790+
dom = parseString("<doc><!--comment--></doc>")
791+
comment = dom.documentElement.firstChild
792+
self.assertEqual(comment.nodeType, Node.COMMENT_NODE)
793+
self.assertEqual(comment.data, "comment")
794+
dom.unlink()
695795

696-
def testAttrListItems(self): pass
796+
def testAttrListItem(self):
797+
dom = parseString("<doc a='1' b='2'/>")
798+
attrs = dom.documentElement.attributes
799+
self.assertEqual(attrs.item(0).name, "a")
800+
self.assertEqual(attrs.item(1).name, "b")
801+
self.assertIsNone(attrs.item(2))
802+
dom.unlink()
697803

698-
def testAttrListItemNS(self): pass
804+
def testAttrListItems(self):
805+
dom = parseString("<doc a='1' b='2'/>")
806+
attrs = dom.documentElement.attributes
807+
self.assertEqual(attrs.items(), [("a", "1"), ("b", "2")])
808+
dom.unlink()
699809

700-
def testAttrListKeys(self): pass
810+
def testAttrListItemNS(self):
811+
dom = parseString("<doc xmlns:p='http://xml.python.org/ns' "
812+
"p:a='1' b='2'/>")
813+
attrs = dom.documentElement.attributes
814+
self.assertEqual(attrs.itemsNS(), [
815+
((xml.dom.XMLNS_NAMESPACE, "p"), "http://xml.python.org/ns"),
816+
(("http://xml.python.org/ns", "a"), "1"),
817+
((None, "b"), "2"),
818+
])
819+
dom.unlink()
701820

702-
def testAttrListKeysNS(self): pass
821+
def testAttrListKeys(self):
822+
dom = parseString("<doc a='1' b='2'/>")
823+
attrs = dom.documentElement.attributes
824+
self.assertEqual(list(attrs.keys()), ["a", "b"])
825+
dom.unlink()
826+
827+
def testAttrListKeysNS(self):
828+
dom = parseString("<doc xmlns:p='http://xml.python.org/ns' "
829+
"p:a='1' b='2'/>")
830+
attrs = dom.documentElement.attributes
831+
self.assertEqual(list(attrs.keysNS()), [
832+
(xml.dom.XMLNS_NAMESPACE, "p"),
833+
("http://xml.python.org/ns", "a"),
834+
(None, "b"),
835+
])
836+
dom.unlink()
703837

704838
def testRemoveNamedItem(self):
705839
doc = parseString("<doc a=''/>")
@@ -720,29 +854,161 @@ def testRemoveNamedItemNS(self):
720854
self.assertRaises(xml.dom.NotFoundErr, attrs.removeNamedItemNS,
721855
"http://xml.python.org/", "b")
722856

723-
def testAttrListValues(self): pass
857+
def testAttrListValues(self):
858+
dom = parseString("<doc a='1' b='2'/>")
859+
attrs = dom.documentElement.attributes
860+
self.assertEqual([attr.name for attr in attrs.values()], ["a", "b"])
861+
self.assertEqual([attr.value for attr in attrs.values()], ["1", "2"])
862+
dom.unlink()
863+
864+
def testAttrListLength(self):
865+
dom = parseString("<doc a='1' b='2'/>")
866+
attrs = dom.documentElement.attributes
867+
self.assertEqual(attrs.length, 2)
868+
self.assertEqual(len(attrs), 2)
869+
dom.unlink()
870+
871+
dom = parseString("<doc/>")
872+
self.assertEqual(dom.documentElement.attributes.length, 0)
873+
dom.unlink()
874+
875+
def testAttrList__getitem__(self):
876+
dom = parseString("<doc xmlns:p='http://xml.python.org/ns' "
877+
"p:a='1' b='2'/>")
878+
attrs = dom.documentElement.attributes
879+
self.assertEqual(attrs["b"].value, "2")
880+
self.assertEqual(attrs[("http://xml.python.org/ns", "a")].value, "1")
881+
self.assertRaises(KeyError, attrs.__getitem__, "missing")
882+
self.assertRaises(KeyError, attrs.__getitem__, (None, "missing"))
883+
dom.unlink()
724884

725-
def testAttrListLength(self): pass
885+
def testAttrList__setitem__(self):
886+
dom = parseString("<doc a='1'/>")
887+
elem = dom.documentElement
888+
attrs = elem.attributes
889+
attrs["a"] = "2"
890+
self.assertEqual(elem.getAttribute("a"), "2")
891+
attrs["b"] = "3"
892+
self.assertEqual(elem.getAttribute("b"), "3")
893+
self.assertEqual(attrs.length, 2)
894+
895+
attr = dom.createAttribute("c")
896+
attr.value = "4"
897+
attrs["c"] = attr
898+
self.assertIs(elem.getAttributeNode("c"), attr)
899+
self.assertEqual(elem.getAttribute("c"), "4")
900+
dom.unlink()
726901

727-
def testAttrList__getitem__(self): pass
902+
def testSetAttrValueandNodeValue(self):
903+
dom = parseString("<doc a='1'/>")
904+
attr = dom.documentElement.getAttributeNode("a")
905+
self.assertEqual(attr.value, "1")
906+
self.assertEqual(attr.nodeValue, "1")
907+
attr.value = "2"
908+
self.assertEqual(attr.nodeValue, "2")
909+
attr.nodeValue = "3"
910+
self.assertEqual(attr.value, "3")
911+
self.assertEqual(dom.documentElement.getAttribute("a"), "3")
912+
dom.unlink()
728913

729-
def testAttrList__setitem__(self): pass
914+
def testParseElement(self):
915+
dom = parseString("<doc><child/><child/></doc>")
916+
elem = dom.documentElement
917+
self.assertEqual(elem.nodeType, Node.ELEMENT_NODE)
918+
self.assertEqual(elem.tagName, "doc")
919+
self.assertIsNone(elem.namespaceURI)
920+
self.assertIs(elem.parentNode, dom)
921+
self.assertIs(elem.ownerDocument, dom)
922+
self.assertEqual([child.tagName for child in elem.childNodes],
923+
["child", "child"])
924+
dom.unlink()
730925

731-
def testSetAttrValueandNodeValue(self): pass
926+
def testParseAttributes(self):
927+
dom = parseString("<doc a='1' b='&amp;'/>")
928+
elem = dom.documentElement
929+
self.assertEqual(elem.getAttribute("a"), "1")
930+
self.assertEqual(elem.getAttribute("b"), "&")
931+
self.assertEqual(elem.getAttribute("missing"), "")
932+
self.assertTrue(elem.hasAttribute("a"))
933+
self.assertFalse(elem.hasAttribute("missing"))
934+
attr = elem.getAttributeNode("a")
935+
self.assertIsNone(attr.namespaceURI)
936+
dom.unlink()
732937

733-
def testParseElement(self): pass
938+
def testParseElementNamespaces(self):
939+
dom = parseString("<p:doc xmlns:p='http://xml.python.org/ns'"
940+
" xmlns='http://xml.python.org/default'>"
941+
"<child/></p:doc>")
942+
elem = dom.documentElement
943+
self.assertEqual(elem.tagName, "p:doc")
944+
self.assertEqual(elem.namespaceURI, "http://xml.python.org/ns")
945+
self.assertEqual(elem.prefix, "p")
946+
self.assertEqual(elem.localName, "doc")
947+
child = elem.getElementsByTagName("child")[0]
948+
self.assertEqual(child.namespaceURI, "http://xml.python.org/default")
949+
self.assertIsNone(child.prefix)
950+
self.assertEqual(child.localName, "child")
951+
dom.unlink()
734952

735-
def testParseAttributes(self): pass
953+
def testParseAttributeNamespaces(self):
954+
dom = parseString("<doc xmlns:p='http://xml.python.org/ns'"
955+
" p:a='1' b='2'/>")
956+
elem = dom.documentElement
957+
self.assertEqual(elem.getAttributeNS("http://xml.python.org/ns", "a"),
958+
"1")
959+
self.assertEqual(elem.getAttributeNS(None, "b"), "2")
960+
attr = elem.getAttributeNodeNS("http://xml.python.org/ns", "a")
961+
self.assertEqual(attr.name, "p:a")
962+
self.assertEqual(attr.prefix, "p")
963+
self.assertEqual(attr.localName, "a")
964+
declaration = elem.getAttributeNode("xmlns:p")
965+
self.assertEqual(declaration.namespaceURI, xml.dom.XMLNS_NAMESPACE)
966+
self.assertEqual(declaration.value, "http://xml.python.org/ns")
967+
dom.unlink()
736968

737-
def testParseElementNamespaces(self): pass
969+
def testParseProcessingInstructions(self):
970+
# the content of a processing instruction is tested
971+
# in testProcessingInstruction
972+
dom = parseString("<?before data?><doc/><?after?>")
973+
pi = dom.childNodes[0]
974+
self.assertEqual(pi.nodeType, Node.PROCESSING_INSTRUCTION_NODE)
975+
self.assertEqual(pi.target, "before")
976+
self.assertEqual(pi.data, "data")
977+
self.assertIs(pi.parentNode, dom)
978+
pi = dom.childNodes[2]
979+
self.assertEqual(pi.target, "after")
980+
self.assertEqual(pi.data, "")
981+
dom.unlink()
738982

739-
def testParseAttributeNamespaces(self): pass
983+
def testChildNodes(self):
984+
dom = parseString("<doc>text<child/><!--comment--></doc>")
985+
children = dom.documentElement.childNodes
986+
self.assertEqual(len(children), 3)
987+
self.assertEqual([child.nodeType for child in children],
988+
[Node.TEXT_NODE, Node.ELEMENT_NODE, Node.COMMENT_NODE])
989+
for child in children:
990+
self.assertIs(child.parentNode, dom.documentElement)
991+
dom.unlink()
740992

741-
def testParseProcessingInstructions(self): pass
993+
dom = parseString("<doc/>")
994+
self.assertEqual(len(dom.documentElement.childNodes), 0)
995+
dom.unlink()
742996

743-
def testChildNodes(self): pass
997+
def testFirstChild(self):
998+
dom = parseString("<doc><a/><b/></doc>")
999+
elem = dom.documentElement
1000+
self.assertEqual(elem.firstChild.tagName, "a")
1001+
self.assertEqual(elem.lastChild.tagName, "b")
1002+
self.assertIs(elem.firstChild, elem.childNodes[0])
1003+
self.assertIs(elem.lastChild, elem.childNodes[-1])
1004+
self.assertIsNone(elem.firstChild.previousSibling)
1005+
self.assertIs(elem.firstChild.nextSibling, elem.lastChild)
1006+
dom.unlink()
7441007

745-
def testFirstChild(self): pass
1008+
dom = parseString("<doc/>")
1009+
self.assertIsNone(dom.documentElement.firstChild)
1010+
self.assertIsNone(dom.documentElement.lastChild)
1011+
dom.unlink()
7461012

7471013
def testHasChildNodes(self):
7481014
dom = parseString("<doc><foo/></doc>")

0 commit comments

Comments
 (0)