Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/test/test_minidom.py

    r2 r391  
    11# test for xml.dom.minidom
    22
    3 import os
    4 import sys
    53import pickle
    64from StringIO import StringIO
    7 from test.test_support import verbose, run_unittest, TestSkipped
     5from test.test_support import verbose, run_unittest, findfile
    86import unittest
    97
     
    1614
    1715
    18 if __name__ == "__main__":
    19     base = sys.argv[0]
    20 else:
    21     base = __file__
    22 tstfile = os.path.join(os.path.dirname(base), "test"+os.extsep+"xml")
    23 del base
     16tstfile = findfile("test.xml", subdir="xmltestdata")
     17
    2418
    2519# The tests of DocumentType importing use these helpers to construct
     
    5347
    5448class MinidomTest(unittest.TestCase):
    55     def tearDown(self):
    56         try:
    57             Node.allnodes
    58         except AttributeError:
    59             # We don't actually have the minidom from the standard library,
    60             # but are picking up the PyXML version from site-packages.
    61             pass
    62         else:
    63             self.confirm(len(Node.allnodes) == 0,
    64                     "assertion: len(Node.allnodes) == 0")
    65             if len(Node.allnodes):
    66                 print "Garbage left over:"
    67                 if verbose:
    68                     print Node.allnodes.items()[0:10]
    69                 else:
    70                     # Don't print specific nodes if repeatable results
    71                     # are needed
    72                     print len(Node.allnodes)
    73             Node.allnodes = {}
    74 
    7549    def confirm(self, test, testname = "Test"):
    7650        self.assertTrue(test, testname)
     
    440414        string2 = str(el)
    441415        self.confirm(string1 == string2)
    442         self.confirm(string1.find("slash:abc") != -1)
     416        self.confirm("slash:abc" in string1)
    443417        dom.unlink()
    444418
     
    465439        dom.unlink()
    466440        self.confirm(domstr == str.replace("\n", "\r\n"))
     441
     442    def test_toprettyxml_with_text_nodes(self):
     443        # see issue #4147, text nodes are not indented
     444        decl = '<?xml version="1.0" ?>\n'
     445        self.assertEqual(parseString('<B>A</B>').toprettyxml(),
     446                         decl + '<B>A</B>\n')
     447        self.assertEqual(parseString('<C>A<B>A</B></C>').toprettyxml(),
     448                         decl + '<C>\n\tA\n\t<B>A</B>\n</C>\n')
     449        self.assertEqual(parseString('<C><B>A</B>A</C>').toprettyxml(),
     450                         decl + '<C>\n\t<B>A</B>\n\tA\n</C>\n')
     451        self.assertEqual(parseString('<C><B>A</B><B>A</B></C>').toprettyxml(),
     452                         decl + '<C>\n\t<B>A</B>\n\t<B>A</B>\n</C>\n')
     453        self.assertEqual(parseString('<C><B>A</B>A<B>A</B></C>').toprettyxml(),
     454                         decl + '<C>\n\t<B>A</B>\n\tA\n\t<B>A</B>\n</C>\n')
     455
     456    def test_toprettyxml_with_adjacent_text_nodes(self):
     457        # see issue #4147, adjacent text nodes are indented normally
     458        dom = Document()
     459        elem = dom.createElement(u'elem')
     460        elem.appendChild(dom.createTextNode(u'TEXT'))
     461        elem.appendChild(dom.createTextNode(u'TEXT'))
     462        dom.appendChild(elem)
     463        decl = '<?xml version="1.0" ?>\n'
     464        self.assertEqual(dom.toprettyxml(),
     465                         decl + '<elem>\n\tTEXT\n\tTEXT\n</elem>\n')
     466
     467    def test_toprettyxml_preserves_content_of_text_node(self):
     468        # see issue #4147
     469        for str in ('<B>A</B>', '<A><B>C</B></A>'):
     470            dom = parseString(str)
     471            dom2 = parseString(dom.toprettyxml())
     472            self.assertEqual(
     473                dom.getElementsByTagName('B')[0].childNodes[0].toxml(),
     474                dom2.getElementsByTagName('B')[0].childNodes[0].toxml())
    467475
    468476    def testProcessingInstruction(self):
     
    735743        doc = parseString("<doc attr='value'/>")
    736744        attr = doc.documentElement.getAttributeNode("attr")
    737         self.failIfEqual(attr, None)
     745        self.assertNotEqual(attr, None)
    738746        clone = attr.cloneNode(deep)
    739747        self.confirm(not clone.isSameNode(attr))
     
    755763        doc = parseString("<?target data?><doc/>")
    756764        pi = doc.firstChild
    757         self.assertEquals(pi.nodeType, Node.PROCESSING_INSTRUCTION_NODE)
     765        self.assertEqual(pi.nodeType, Node.PROCESSING_INSTRUCTION_NODE)
    758766        clone = pi.cloneNode(deep)
    759767        self.confirm(clone.target == pi.target
     
    791799        doc.unlink()
    792800
     801    def testNormalizeCombineAndNextSibling(self):
     802        doc = parseString("<doc/>")
     803        root = doc.documentElement
     804        root.appendChild(doc.createTextNode("first"))
     805        root.appendChild(doc.createTextNode("second"))
     806        root.appendChild(doc.createElement("i"))
     807        self.confirm(len(root.childNodes) == 3
     808                and root.childNodes.length == 3,
     809                "testNormalizeCombineAndNextSibling -- preparation")
     810        doc.normalize()
     811        self.confirm(len(root.childNodes) == 2
     812                and root.childNodes.length == 2
     813                and root.firstChild.data == "firstsecond"
     814                and root.firstChild is not root.lastChild
     815                and root.firstChild.nextSibling is root.lastChild
     816                and root.firstChild.previousSibling is None
     817                and root.lastChild.previousSibling is root.firstChild
     818                and root.lastChild.nextSibling is None
     819                , "testNormalizeCombinedAndNextSibling -- result")
     820        doc.unlink()
     821
     822    def testNormalizeDeleteWithPrevSibling(self):
     823        doc = parseString("<doc/>")
     824        root = doc.documentElement
     825        root.appendChild(doc.createTextNode("first"))
     826        root.appendChild(doc.createTextNode(""))
     827        self.confirm(len(root.childNodes) == 2
     828                and root.childNodes.length == 2,
     829                "testNormalizeDeleteWithPrevSibling -- preparation")
     830        doc.normalize()
     831        self.confirm(len(root.childNodes) == 1
     832                and root.childNodes.length == 1
     833                and root.firstChild.data == "first"
     834                and root.firstChild is root.lastChild
     835                and root.firstChild.nextSibling is None
     836                and root.firstChild.previousSibling is None
     837                , "testNormalizeDeleteWithPrevSibling -- result")
     838        doc.unlink()
     839
     840    def testNormalizeDeleteWithNextSibling(self):
     841        doc = parseString("<doc/>")
     842        root = doc.documentElement
     843        root.appendChild(doc.createTextNode(""))
     844        root.appendChild(doc.createTextNode("second"))
     845        self.confirm(len(root.childNodes) == 2
     846                and root.childNodes.length == 2,
     847                "testNormalizeDeleteWithNextSibling -- preparation")
     848        doc.normalize()
     849        self.confirm(len(root.childNodes) == 1
     850                and root.childNodes.length == 1
     851                and root.firstChild.data == "second"
     852                and root.firstChild is root.lastChild
     853                and root.firstChild.nextSibling is None
     854                and root.firstChild.previousSibling is None
     855                , "testNormalizeDeleteWithNextSibling -- result")
     856        doc.unlink()
     857
     858    def testNormalizeDeleteWithTwoNonTextSiblings(self):
     859        doc = parseString("<doc/>")
     860        root = doc.documentElement
     861        root.appendChild(doc.createElement("i"))
     862        root.appendChild(doc.createTextNode(""))
     863        root.appendChild(doc.createElement("i"))
     864        self.confirm(len(root.childNodes) == 3
     865                and root.childNodes.length == 3,
     866                "testNormalizeDeleteWithTwoSiblings -- preparation")
     867        doc.normalize()
     868        self.confirm(len(root.childNodes) == 2
     869                and root.childNodes.length == 2
     870                and root.firstChild is not root.lastChild
     871                and root.firstChild.nextSibling is root.lastChild
     872                and root.firstChild.previousSibling is None
     873                and root.lastChild.previousSibling is root.firstChild
     874                and root.lastChild.nextSibling is None
     875                , "testNormalizeDeleteWithTwoSiblings -- result")
     876        doc.unlink()
     877
     878    def testNormalizeDeleteAndCombine(self):
     879        doc = parseString("<doc/>")
     880        root = doc.documentElement
     881        root.appendChild(doc.createTextNode(""))
     882        root.appendChild(doc.createTextNode("second"))
     883        root.appendChild(doc.createTextNode(""))
     884        root.appendChild(doc.createTextNode("fourth"))
     885        root.appendChild(doc.createTextNode(""))
     886        self.confirm(len(root.childNodes) == 5
     887                and root.childNodes.length == 5,
     888                "testNormalizeDeleteAndCombine -- preparation")
     889        doc.normalize()
     890        self.confirm(len(root.childNodes) == 1
     891                and root.childNodes.length == 1
     892                and root.firstChild is root.lastChild
     893                and root.firstChild.data == "secondfourth"
     894                and root.firstChild.previousSibling is None
     895                and root.firstChild.nextSibling is None
     896                , "testNormalizeDeleteAndCombine -- result")
     897        doc.unlink()
     898
     899    def testNormalizeRecursion(self):
     900        doc = parseString("<doc>"
     901                            "<o>"
     902                              "<i/>"
     903                              "t"
     904                              #
     905                              #x
     906                            "</o>"
     907                            "<o>"
     908                              "<o>"
     909                                "t2"
     910                                #x2
     911                              "</o>"
     912                              "t3"
     913                              #x3
     914                            "</o>"
     915                            #
     916                          "</doc>")
     917        root = doc.documentElement
     918        root.childNodes[0].appendChild(doc.createTextNode(""))
     919        root.childNodes[0].appendChild(doc.createTextNode("x"))
     920        root.childNodes[1].childNodes[0].appendChild(doc.createTextNode("x2"))
     921        root.childNodes[1].appendChild(doc.createTextNode("x3"))
     922        root.appendChild(doc.createTextNode(""))
     923        self.confirm(len(root.childNodes) == 3
     924                and root.childNodes.length == 3
     925                and len(root.childNodes[0].childNodes) == 4
     926                and root.childNodes[0].childNodes.length == 4
     927                and len(root.childNodes[1].childNodes) == 3
     928                and root.childNodes[1].childNodes.length == 3
     929                and len(root.childNodes[1].childNodes[0].childNodes) == 2
     930                and root.childNodes[1].childNodes[0].childNodes.length == 2
     931                , "testNormalize2 -- preparation")
     932        doc.normalize()
     933        self.confirm(len(root.childNodes) == 2
     934                and root.childNodes.length == 2
     935                and len(root.childNodes[0].childNodes) == 2
     936                and root.childNodes[0].childNodes.length == 2
     937                and len(root.childNodes[1].childNodes) == 2
     938                and root.childNodes[1].childNodes.length == 2
     939                and len(root.childNodes[1].childNodes[0].childNodes) == 1
     940                and root.childNodes[1].childNodes[0].childNodes.length == 1
     941                , "testNormalize2 -- childNodes lengths")
     942        self.confirm(root.childNodes[0].childNodes[1].data == "tx"
     943                and root.childNodes[1].childNodes[0].childNodes[0].data == "t2x2"
     944                and root.childNodes[1].childNodes[1].data == "t3x3"
     945                , "testNormalize2 -- joined text fields")
     946        self.confirm(root.childNodes[0].childNodes[1].nextSibling is None
     947                and root.childNodes[0].childNodes[1].previousSibling
     948                        is root.childNodes[0].childNodes[0]
     949                and root.childNodes[0].childNodes[0].previousSibling is None
     950                and root.childNodes[0].childNodes[0].nextSibling
     951                        is root.childNodes[0].childNodes[1]
     952                and root.childNodes[1].childNodes[1].nextSibling is None
     953                and root.childNodes[1].childNodes[1].previousSibling
     954                        is root.childNodes[1].childNodes[0]
     955                and root.childNodes[1].childNodes[0].previousSibling is None
     956                and root.childNodes[1].childNodes[0].nextSibling
     957                        is root.childNodes[1].childNodes[1]
     958                , "testNormalize2 -- sibling pointers")
     959        doc.unlink()
     960
     961
     962    def testBug0777884(self):
     963        doc = parseString("<o>text</o>")
     964        text = doc.documentElement.childNodes[0]
     965        self.assertEqual(text.nodeType, Node.TEXT_NODE)
     966        # Should run quietly, doing nothing.
     967        text.normalize()
     968        doc.unlink()
     969
    793970    def testBug1433694(self):
    794971        doc = parseString("<o><i/>t</o>")
     
    796973        node.childNodes[1].nodeValue = ""
    797974        node.normalize()
    798         self.confirm(node.childNodes[-1].nextSibling == None,
     975        self.confirm(node.childNodes[-1].nextSibling is None,
    799976                     "Final child's .nextSibling should be None")
    800977
     
    8841061                "testEncodings - encoding EURO SIGN")
    8851062
    886         # Verify that character decoding errors throw exceptions instead
     1063        # Verify that character decoding errors raise exceptions instead
    8871064        # of crashing
    8881065        self.assertRaises(UnicodeDecodeError, parseString,
     
    10641241        elem = doc.documentElement
    10651242        text = elem.childNodes[0]
    1066         self.assertEquals(text.nodeType, Node.TEXT_NODE)
     1243        self.assertEqual(text.nodeType, Node.TEXT_NODE)
    10671244
    10681245        self.checkWholeText(text, "a")
     
    13211498        self.assertRaises(ValueError, doc.toxml)
    13221499
     1500    def testEmptyXMLNSValue(self):
     1501        doc = parseString("<element xmlns=''>\n"
     1502                          "<foo/>\n</element>")
     1503        doc2 = parseString(doc.toxml())
     1504        self.confirm(doc2.namespaceURI == xml.dom.EMPTY_NAMESPACE)
     1505
     1506
    13231507def test_main():
    13241508    run_unittest(MinidomTest)
Note: See TracChangeset for help on using the changeset viewer.