diff options
author | Chris McDonough <chrism@plope.com> | 2010-08-10 05:05:24 +0000 |
---|---|---|
committer | Chris McDonough <chrism@plope.com> | 2010-08-10 05:05:24 +0000 |
commit | 604d6598f5c34d6498603f4403cf2463271cb6e7 (patch) | |
tree | b04fc885ee041f8cfbbfb0959e04d818c5ccd654 /src/zope | |
parent | 5761627158cc5c22e3932e22f6bc20a872671dd2 (diff) | |
download | zope-i18nmessageid-604d6598f5c34d6498603f4403cf2463271cb6e7.tar.gz |
Merge svn+ssh://svn.zope.org/repos/main/zope.i18nmessageid/branches/jython-compat to trunk.
Diffstat (limited to 'src/zope')
-rw-r--r-- | src/zope/i18nmessageid/message.py | 54 | ||||
-rw-r--r-- | src/zope/i18nmessageid/tests.py | 114 |
2 files changed, 113 insertions, 55 deletions
diff --git a/src/zope/i18nmessageid/message.py b/src/zope/i18nmessageid/message.py index 98b7828..cd24544 100644 --- a/src/zope/i18nmessageid/message.py +++ b/src/zope/i18nmessageid/message.py @@ -27,7 +27,7 @@ class Message(unicode): These are the doc tests from message.txt. Note that we have to create the message manually since MessageFactory would return the C implementation. - >>> from zope.i18nmessageid.message import pyMessage as Message + >>> from zope.i18nmessageid.message import Message >>> robot = Message(u"robot-message", 'futurama', u"${name} is a robot.") >>> robot @@ -39,10 +39,6 @@ class Message(unicode): u'${name} is a robot.' >>> robot.mapping - Only the python implementation has a _readonly attribute - >>> robot._readonly - True - >>> robot.domain = "planetexpress" Traceback (most recent call last): ... @@ -81,56 +77,14 @@ class Message(unicode): >>> args (u'fembot', None, None, None) - Change classes for pickle tests - >>> import zope.i18nmessageid.message - >>> oldMessage = zope.i18nmessageid.message.Message - >>> zope.i18nmessageid.message.Message = Message - - At first check if pickling and unpicklung from pyMessage to pyMessage works + Check if pickling and unpickling works >>> from pickle import dumps, loads >>> pystate = dumps(new_robot) >>> pickle_bot = loads(pystate) >>> pickle_bot, pickle_bot.domain, pickle_bot.default, pickle_bot.mapping (u'robot-message', 'futurama', u'${name} is a robot.', {u'name': u'Bender'}) - >>> pickle_bot._readonly - True - >>> from zope.i18nmessageid.message import pyMessage - >>> pickle_bot.__reduce__()[0] is pyMessage - True - >>> del pickle_bot - - At second check if cMessage is able to load the state of a pyMessage - >>> from _zope_i18nmessageid_message import Message - >>> zope.i18nmessageid.message.Message = Message - >>> c_bot = loads(pystate) - >>> c_bot, c_bot.domain, c_bot.default, c_bot.mapping - (u'robot-message', 'futurama', u'${name} is a robot.', {u'name': u'Bender'}) - >>> c_bot._readonly - Traceback (most recent call last): - AttributeError: 'zope.i18nmessageid.message.Message' object has no attribute '_readonly' - >>> from _zope_i18nmessageid_message import Message as cMessage - >>> c_bot.__reduce__()[0] is cMessage + >>> pickle_bot.__reduce__()[0] is Message True - - At last check if pyMessage can load a state of cMessage - >>> cstate = dumps(c_bot) - >>> del c_bot - >>> from zope.i18nmessageid.message import pyMessage as Message - >>> zope.i18nmessageid.message.Message = Message - >>> py_bot = loads(cstate) - >>> py_bot, py_bot.domain, py_bot.default, py_bot.mapping - (u'robot-message', 'futurama', u'${name} is a robot.', {u'name': u'Bender'}) - >>> py_bot._readonly - True - >>> py_bot.__reduce__()[0] is pyMessage - True - - Both pickle states should be equal - >>> pystate == cstate - True - - Finally restore classes for other unit tests - >>> zope.i18nmessageid.message.Message = oldMessage """ __slots__ = ('domain', 'default', 'mapping', '_readonly') @@ -173,7 +127,7 @@ pyMessage = Message try: from _zope_i18nmessageid_message import Message -except ImportError: +except ImportError: # pragma: no cover pass class MessageFactory(object): diff --git a/src/zope/i18nmessageid/tests.py b/src/zope/i18nmessageid/tests.py index 963fdb2..46d41ec 100644 --- a/src/zope/i18nmessageid/tests.py +++ b/src/zope/i18nmessageid/tests.py @@ -17,11 +17,115 @@ import unittest from doctest import DocFileSuite from doctest import DocTestSuite -def test_suite(): - return unittest.TestSuite(( +class PickleEqualityTests(unittest.TestCase): + def setUp(self): + # set the C version up as the used version + import zope.i18nmessageid.message + self.oldMessage = zope.i18nmessageid.message.Message + + def tearDown(self): + # set the original version back up as the used version + import zope.i18nmessageid.message + zope.i18nmessageid.message.Message = self.oldMessage + + def test_message_pickling(self): + from zope.i18nmessageid.message import pyMessage as Message + robot = Message(u"robot-message", 'futurama', u"${name} is a robot.") + + self.assertEqual(robot, u'robot-message') + self.failUnless(isinstance(robot, unicode)) + self.assertEqual(robot.default, u'${name} is a robot.') + self.assertEqual(robot.mapping, None) + + # Only the python implementation has a _readonly attribute + self.assertEqual(robot._readonly, True) + self.assertRaises( + TypeError, + robot.__setattr__, 'domain', "planetexpress") + self.assertRaises( + TypeError, + robot.__setattr__, 'default', u"${name} is not a robot.") + self.assertRaises( + TypeError, + robot.__setattr__, 'mapping', {u'name': u'Bender'}) + + new_robot = Message(robot, mapping={u'name': u'Bender'}) + self.assertEqual(new_robot, u'robot-message') + self.assertEqual(new_robot.domain, 'futurama') + self.assertEqual(new_robot.default, u'${name} is a robot.') + self.assertEqual(new_robot.mapping, {u'name': u'Bender'}) + + callable, args = new_robot.__reduce__() + self.failUnless(callable is Message) + self.assertEqual( + args, + (u'robot-message', 'futurama', u'${name} is a robot.', + {u'name': u'Bender'})) + + fembot = Message(u'fembot') + callable, args = fembot.__reduce__() + self.failUnless(callable is Message) + self.assertEqual(args, (u'fembot', None, None, None)) + + import zope.i18nmessageid.message + zope.i18nmessageid.message.Message = Message + + # First check if pickling and unpickling from pyMessage to + # pyMessage works + from pickle import dumps, loads + pystate = dumps(new_robot) + pickle_bot = loads(pystate) + self.assertEqual(pickle_bot, u'robot-message') + self.assertEqual(pickle_bot.domain, 'futurama') + self.assertEqual(pickle_bot.default, u'${name} is a robot.') + self.assertEqual(pickle_bot.mapping, {u'name': u'Bender'}) + self.assertEqual(pickle_bot._readonly, True) + + from zope.i18nmessageid.message import pyMessage + self.failUnless(pickle_bot.__reduce__()[0] is pyMessage) + del pickle_bot + + # Second check if cMessage is able to load the state of a pyMessage + from _zope_i18nmessageid_message import Message + zope.i18nmessageid.message.Message = Message + c_bot = loads(pystate) + self.assertEqual(c_bot, u'robot-message') + self.assertEqual(c_bot.domain, 'futurama') + self.assertEqual(c_bot.default, u'${name} is a robot.') + self.assertEqual(c_bot.mapping, {u'name': u'Bender'}) + self.failIf(hasattr(c_bot, '_readonly')) + from _zope_i18nmessageid_message import Message as cMessage + self.failUnless(c_bot.__reduce__()[0] is cMessage) + + # Last check if pyMessage can load a state of cMessage + cstate = dumps(c_bot) + del c_bot + from zope.i18nmessageid.message import pyMessage as Message + zope.i18nmessageid.message.Message = Message + py_bot = loads(cstate) + self.assertEqual(py_bot, u'robot-message') + self.assertEqual(py_bot.domain, 'futurama') + self.assertEqual(py_bot.default, u'${name} is a robot.') + self.assertEqual(py_bot.mapping, {u'name': u'Bender'}) + self.assertEqual(py_bot._readonly, True) + self.failUnless(py_bot.__reduce__()[0] is pyMessage) + + # Both pickle states should be equal + self.assertEqual(pystate, cstate) + +try: + from _zope_i18nmessageid_message import Message as import_test + def test_suite(): + return unittest.TestSuite(( DocTestSuite('zope.i18nmessageid.message'), DocFileSuite('messages.txt', package='zope.i18nmessageid'), + unittest.makeSuite(PickleEqualityTests), )) - -if __name__ == '__main__': - unittest.main(defaultTest="test_suite") +except ImportError: # pragma: no cover + # couldnt import C version + def test_suite(): + return unittest.TestSuite(( + DocTestSuite('zope.i18nmessageid.message'), + DocFileSuite('messages.txt', package='zope.i18nmessageid'), + )) + |