diff options
Diffstat (limited to 'Lib/test/test_minidom.py')
-rw-r--r-- | Lib/test/test_minidom.py | 75 |
1 files changed, 50 insertions, 25 deletions
diff --git a/Lib/test/test_minidom.py b/Lib/test/test_minidom.py index ca0f0f77ebd..80812c8cfae 100644 --- a/Lib/test/test_minidom.py +++ b/Lib/test/test_minidom.py @@ -1,8 +1,7 @@ # test for xml.dom.minidom import pickle -from StringIO import StringIO -from test.test_support import verbose, run_unittest, findfile +from test.support import verbose, run_unittest, findfile import unittest import xml.dom @@ -54,9 +53,10 @@ class MinidomTest(unittest.TestCase): self.confirm(t == s, "looking for %s, found %s" % (repr(s), repr(t))) def testParseFromFile(self): - dom = parse(StringIO(open(tstfile).read())) - dom.unlink() - self.confirm(isinstance(dom,Document)) + with open(tstfile) as file: + dom = parse(file) + dom.unlink() + self.confirm(isinstance(dom, Document)) def testGetElementsByTagName(self): dom = parse(tstfile) @@ -139,7 +139,7 @@ class MinidomTest(unittest.TestCase): def testAppendChild(self): dom = parse(tstfile) - dom.documentElement.appendChild(dom.createComment(u"Hello")) + dom.documentElement.appendChild(dom.createComment("Hello")) self.confirm(dom.documentElement.childNodes[-1].nodeName == "#comment") self.confirm(dom.documentElement.childNodes[-1].data == "Hello") dom.unlink() @@ -209,7 +209,14 @@ class MinidomTest(unittest.TestCase): def testUnlink(self): dom = parse(tstfile) + self.assertTrue(dom.childNodes) dom.unlink() + self.assertFalse(dom.childNodes) + + def testContext(self): + with parse(tstfile) as dom: + self.assertTrue(dom.childNodes) + self.assertFalse(dom.childNodes) def testElement(self): dom = Document() @@ -343,13 +350,31 @@ class MinidomTest(unittest.TestCase): def testGetAttrList(self): pass - def testGetAttrValues(self): pass + def testGetAttrValues(self): + pass - def testGetAttrLength(self): pass + def testGetAttrLength(self): + pass - def testGetAttribute(self): pass + def testGetAttribute(self): + dom = Document() + child = dom.appendChild( + dom.createElementNS("http://www.python.org", "python:abc")) + self.assertEqual(child.getAttribute('missing'), '') - def testGetAttributeNS(self): pass + def testGetAttributeNS(self): + dom = Document() + child = dom.appendChild( + dom.createElementNS("http://www.python.org", "python:abc")) + child.setAttributeNS("http://www.w3.org", "xmlns:python", + "http://www.python.org") + self.assertEqual(child.getAttributeNS("http://www.w3.org", "python"), + 'http://www.python.org') + self.assertEqual(child.getAttributeNS("http://www.w3.org", "other"), + '') + child2 = child.appendChild(dom.createElement('abc')) + self.assertEqual(child2.getAttributeNS("http://www.python.org", "missing"), + '') def testGetAttributeNode(self): pass @@ -400,7 +425,7 @@ class MinidomTest(unittest.TestCase): def testElementReprAndStrUnicode(self): dom = Document() - el = dom.appendChild(dom.createElement(u"abc")) + el = dom.appendChild(dom.createElement("abc")) string1 = repr(el) string2 = str(el) self.confirm(string1 == string2) @@ -409,7 +434,7 @@ class MinidomTest(unittest.TestCase): def testElementReprAndStrUnicodeNS(self): dom = Document() el = dom.appendChild( - dom.createElementNS(u"http://www.slashdot.org", u"slash:abc")) + dom.createElementNS("http://www.slashdot.org", "slash:abc")) string1 = repr(el) string2 = str(el) self.confirm(string1 == string2) @@ -418,7 +443,7 @@ class MinidomTest(unittest.TestCase): def testAttributeRepr(self): dom = Document() - el = dom.appendChild(dom.createElement(u"abc")) + el = dom.appendChild(dom.createElement("abc")) node = el.setAttribute("abc", "def") self.confirm(str(node) == repr(node)) dom.unlink() @@ -456,9 +481,9 @@ class MinidomTest(unittest.TestCase): def test_toprettyxml_with_adjacent_text_nodes(self): # see issue #4147, adjacent text nodes are indented normally dom = Document() - elem = dom.createElement(u'elem') - elem.appendChild(dom.createTextNode(u'TEXT')) - elem.appendChild(dom.createTextNode(u'TEXT')) + elem = dom.createElement('elem') + elem.appendChild(dom.createTextNode('TEXT')) + elem.appendChild(dom.createTextNode('TEXT')) dom.appendChild(elem) decl = '<?xml version="1.0" ?>\n' self.assertEqual(dom.toprettyxml(), @@ -572,8 +597,8 @@ class MinidomTest(unittest.TestCase): def _testCloneElementCopiesAttributes(self, e1, e2, test): attrs1 = e1.attributes attrs2 = e2.attributes - keys1 = attrs1.keys() - keys2 = attrs2.keys() + keys1 = list(attrs1.keys()) + keys2 = list(attrs2.keys()) keys1.sort() keys2.sort() self.confirm(keys1 == keys2, "clone of element has same attribute keys") @@ -1053,17 +1078,17 @@ class MinidomTest(unittest.TestCase): def testEncodings(self): doc = parseString('<foo>€</foo>') - self.confirm(doc.toxml() == u'<?xml version="1.0" ?><foo>\u20ac</foo>' - and doc.toxml('utf-8') == - '<?xml version="1.0" encoding="utf-8"?><foo>\xe2\x82\xac</foo>' - and doc.toxml('iso-8859-15') == - '<?xml version="1.0" encoding="iso-8859-15"?><foo>\xa4</foo>', - "testEncodings - encoding EURO SIGN") + self.assertEqual(doc.toxml(), + '<?xml version="1.0" ?><foo>\u20ac</foo>') + self.assertEqual(doc.toxml('utf-8'), + 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>') # Verify that character decoding errors raise exceptions instead # of crashing self.assertRaises(UnicodeDecodeError, parseString, - '<fran\xe7ais>Comment \xe7a va ? Tr\xe8s bien ?</fran\xe7ais>') + b'<fran\xe7ais>Comment \xe7a va ? Tr\xe8s bien ?</fran\xe7ais>') doc.unlink() |