Lib.test.test_pulldom.py Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jython-standalone Show documentation
Show all versions of jython-standalone Show documentation
Jython is an implementation of the high-level, dynamic, object-oriented
language Python written in 100% Pure Java, and seamlessly integrated with
the Java platform. It thus allows you to run Python on any Java platform.
"""
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 = """\
Some greek: ΑΒΓΔΕ
"""
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()