diff options
Diffstat (limited to 'Lib/test/test_minidom.py')
| -rw-r--r-- | Lib/test/test_minidom.py | 77 | 
1 files changed, 37 insertions, 40 deletions
diff --git a/Lib/test/test_minidom.py b/Lib/test/test_minidom.py index 4a69b00..0427ba3 100644 --- a/Lib/test/test_minidom.py +++ b/Lib/test/test_minidom.py @@ -4,9 +4,7 @@ import pickle  from test.support import verbose, run_unittest, findfile  import unittest -import xml.dom  import xml.dom.minidom -import xml.parsers.expat  from xml.dom.minidom import parse, Node, Document, parseString  from xml.dom.minidom import getDOMImplementation @@ -14,7 +12,6 @@ from xml.dom.minidom import getDOMImplementation  tstfile = findfile("test.xml", subdir="xmltestdata") -  # The tests of DocumentType importing use these helpers to construct  # the documents to work with, since not all DOM builders actually  # create the DocumentType nodes. @@ -50,7 +47,7 @@ class MinidomTest(unittest.TestCase):      def checkWholeText(self, node, s):          t = node.wholeText -        self.confirm(t == s, "looking for %s, found %s" % (repr(s), repr(t))) +        self.confirm(t == s, "looking for %r, found %r" % (s, t))      def testParseFromFile(self):          with open(tstfile) as file: @@ -282,6 +279,7 @@ class MinidomTest(unittest.TestCase):          child.setAttribute("def", "ghi")          self.confirm(len(child.attributes) == 1) +        self.assertRaises(xml.dom.NotFoundErr, child.removeAttribute, "foo")          child.removeAttribute("def")          self.confirm(len(child.attributes) == 0)          dom.unlink() @@ -293,6 +291,8 @@ class MinidomTest(unittest.TestCase):          child.setAttributeNS("http://www.w3.org", "xmlns:python",                                                  "http://www.python.org")          child.setAttributeNS("http://www.python.org", "python:abcattr", "foo") +        self.assertRaises(xml.dom.NotFoundErr, child.removeAttributeNS, +            "foo", "http://www.python.org")          self.confirm(len(child.attributes) == 2)          child.removeAttributeNS("http://www.python.org", "abcattr")          self.confirm(len(child.attributes) == 1) @@ -304,11 +304,23 @@ class MinidomTest(unittest.TestCase):          child.setAttribute("spam", "jam")          self.confirm(len(child.attributes) == 1)          node = child.getAttributeNode("spam") +        self.assertRaises(xml.dom.NotFoundErr, child.removeAttributeNode, +            None)          child.removeAttributeNode(node)          self.confirm(len(child.attributes) == 0                  and child.getAttributeNode("spam") is None) +        dom2 = Document() +        child2 = dom2.appendChild(dom.createElement("foo")) +        self.assertRaises(xml.dom.NotFoundErr, child.removeAttributeNode, +            node)          dom.unlink() +    def testHasAttribute(self): +        dom = Document() +        child = dom.appendChild(dom.createElement("foo")) +        child.setAttribute("spam", "jam") +        self.confirm(child.hasAttribute("spam")) +      def testChangeAttr(self):          dom = parseString("<abc/>")          el = dom.documentElement @@ -592,7 +604,13 @@ class MinidomTest(unittest.TestCase):      def testFirstChild(self): pass -    def testHasChildNodes(self): pass +    def testHasChildNodes(self): +        dom = parseString("<doc><foo/></doc>") +        doc = dom.documentElement +        self.assertTrue(dom.hasChildNodes()) +        dom2 = parseString("<doc/>") +        doc2 = dom2.documentElement +        self.assertFalse(doc2.hasChildNodes())      def _testCloneElementCopiesAttributes(self, e1, e2, test):          attrs1 = e1.attributes @@ -1041,41 +1059,6 @@ class MinidomTest(unittest.TestCase):                  "test NodeList.item()")          doc.unlink() -    def testSAX2DOM(self): -        from xml.dom import pulldom - -        sax2dom = pulldom.SAX2DOM() -        sax2dom.startDocument() -        sax2dom.startElement("doc", {}) -        sax2dom.characters("text") -        sax2dom.startElement("subelm", {}) -        sax2dom.characters("text") -        sax2dom.endElement("subelm") -        sax2dom.characters("text") -        sax2dom.endElement("doc") -        sax2dom.endDocument() - -        doc = sax2dom.document -        root = doc.documentElement -        (text1, elm1, text2) = root.childNodes -        text3 = elm1.childNodes[0] - -        self.confirm(text1.previousSibling is None and -                text1.nextSibling is elm1 and -                elm1.previousSibling is text1 and -                elm1.nextSibling is text2 and -                text2.previousSibling is elm1 and -                text2.nextSibling is None and -                text3.previousSibling is None and -                text3.nextSibling is None, "testSAX2DOM - siblings") - -        self.confirm(root.parentNode is doc and -                text1.parentNode is root and -                elm1.parentNode is root and -                text2.parentNode is root and -                text3.parentNode is elm1, "testSAX2DOM - parents") -        doc.unlink() -      def testEncodings(self):          doc = parseString('<foo>€</foo>')          self.assertEqual(doc.toxml(), @@ -1084,6 +1067,11 @@ class MinidomTest(unittest.TestCase):              b'<?xml version="1.0" encoding="utf-8"?><foo>\xe2\x82\xac</foo>')          self.assertEqual(doc.toxml('iso-8859-15'),              b'<?xml version="1.0" encoding="iso-8859-15"?><foo>\xa4</foo>') +        self.assertEqual(doc.toxml('us-ascii'), +            b'<?xml version="1.0" encoding="us-ascii"?><foo>€</foo>') +        self.assertEqual(doc.toxml('utf-16'), +            '<?xml version="1.0" encoding="utf-16"?>' +            '<foo>\u20ac</foo>'.encode('utf-16'))          # Verify that character decoding errors throw exceptions instead          # of crashing @@ -1522,12 +1510,21 @@ class MinidomTest(unittest.TestCase):          doc.appendChild(doc.createComment("foo--bar"))          self.assertRaises(ValueError, doc.toxml) +      def testEmptyXMLNSValue(self):          doc = parseString("<element xmlns=''>\n"                            "<foo/>\n</element>")          doc2 = parseString(doc.toxml())          self.confirm(doc2.namespaceURI == xml.dom.EMPTY_NAMESPACE) +    def testDocRemoveChild(self): +        doc = parse(tstfile) +        title_tag = doc.documentElement.getElementsByTagName("TITLE")[0] +        self.assertRaises( xml.dom.NotFoundErr, doc.removeChild, title_tag) +        num_children_before = len(doc.childNodes) +        doc.removeChild(doc.childNodes[0]) +        num_children_after = len(doc.childNodes) +        self.assertTrue(num_children_after == num_children_before - 1)  def test_main():      run_unittest(MinidomTest)  | 
