File: test_pulldom.py

package info (click to toggle)
jython 2.5.3-16%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,772 kB
  • ctags: 106,434
  • sloc: python: 351,322; java: 216,349; xml: 1,584; sh: 330; perl: 114; ansic: 102; makefile: 45
file content (89 lines) | stat: -rw-r--r-- 3,260 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
	This is not an exhaustive pulldom test suite.
	Instead, it is a place to put jython-specific tests,
	relating to bugs like this one, for example.

	"xml.dom.Node.data returns bytestrings of decoded unicode"
	http://bugs.jython.org/issue1583

	amak.
"""

import StringIO
import unittest
from xml.dom import pulldom
from test import test_support

class UnicodeTests(unittest.TestCase):

    testDoc = """\
<?xml version="1.0" encoding="ascii"?>
<document>
    <p>Some greek: &#x391;&#x392;&#x393;&#x394;&#x395;</p>
    <greek attrs="&#x396;&#x397;&#x398;&#x399;&#x39a;"/>
    <?greek &#x39b;&#x39c;&#x39d;&#x39e;&#x39f;?>
    <!--&#x39b;&#x39c;&#x39d;&#x39e;&#x39f;-->
</document>
"""

    def setUp(self):
        self.testFile = StringIO.StringIO(self.testDoc)

    def testTextNodes(self):
        text = []
        for event, node in pulldom.parse(self.testFile):
            if event == pulldom.CHARACTERS:
                text.append(node.data)
        try:
            result = u"".join(text)
            self.failUnlessEqual(repr(result), r"u'\n    Some greek: \u0391\u0392\u0393\u0394\u0395\n    \n    \n    \n'")
        except Exception, x:
            self.fail("Unexpected exception joining text pieces: %s" % str(x))

    def testAttributes(self):
        attrText = []
        for event, node in pulldom.parse(self.testFile):
            if event == pulldom.START_ELEMENT:
                for attrIx in range(node.attributes.length):
                    attrText.append(node.attributes.item(attrIx).value)
        try:
            result = u"".join(attrText)
            self.failUnlessEqual(repr(result), r"u'\u0396\u0397\u0398\u0399\u039a'")
        except Exception, x:
            self.fail("Unexpected exception joining attribute text pieces: %s" % str(x))

    def testProcessingInstruction(self):
        piText = []
        for event, node in pulldom.parse(self.testFile):
            if event == pulldom.PROCESSING_INSTRUCTION:
                piText.append(node.data)
        try:
            result = u"".join(piText)
            # Weird how the repr for PI data is different from text and char data.
            # Still, the whole xml.dom.* and xml.sax.* hierarchy is rather a 
            # labyrinthine mess under jython, mostly because it's so old, and
            # yet survived through major evolutionary changes in both jython and java.
            self.failUnlessEqual(repr(result), r"u'&#x39b;&#x39c;&#x39d;&#x39e;&#x39f;'")
        except Exception, x:
            self.fail("Unexpected exception joining pi data pieces: %s" % str(x))

    def testComment(self):
        commentText = []
        for event, node in pulldom.parse(self.testFile):
            if event == pulldom.COMMENT:
                commentText.append(node.data)
        try:
            result = u"".join(commentText)
            self.failUnlessEqual(repr(result), r"u'&#x39b;&#x39c;&#x39d;&#x39e;&#x39f;'")
        except Exception, x:
            self.fail("Unexpected exception joining comment data pieces: %s" % str(x))

def test_main():
    tests = [
        UnicodeTests,
    ]
    suites = [unittest.makeSuite(klass, 'test') for klass in tests]
    test_support.run_suite(unittest.TestSuite(suites))

if __name__ == "__main__":
    test_main()