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: ΑΒΓΔΕ</p>
<greek attrs="ΖΗΘΙΚ"/>
<?greek ΛΜΝΞΟ?>
<!--ΛΜΝΞΟ-->
</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'ΛΜΝΞΟ'")
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'ΛΜΝΞΟ'")
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()
|