diff options
author | Stephan Richter <stephan.richter@gmail.com> | 2004-12-17 22:06:30 +0000 |
---|---|---|
committer | Stephan Richter <stephan.richter@gmail.com> | 2004-12-17 22:06:30 +0000 |
commit | 76c89986d36c1b48851a9f48af1534d34e96a027 (patch) | |
tree | 3399fa17399ed8e8b2962737258f91a7a510a506 | |
download | zope-i18n-monolithic-zope3-srichter-blow-services.tar.gz |
Starting to remove deprecation warnings caused by the changes in monolithic-zope3-srichter-blow-services
zope.component.
-rw-r--r-- | tests/test_itranslationdomain.py | 101 | ||||
-rw-r--r-- | tests/test_translationdomain.py | 140 |
2 files changed, 241 insertions, 0 deletions
diff --git a/tests/test_itranslationdomain.py b/tests/test_itranslationdomain.py new file mode 100644 index 0000000..9313d00 --- /dev/null +++ b/tests/test_itranslationdomain.py @@ -0,0 +1,101 @@ +############################################################################## +# +# Copyright (c) 2001, 2002 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""This is an 'abstract' test for the ITranslationDomain interface. + +$Id$ +""" +import unittest +from zope.interface.verify import verifyObject +from zope.interface import implements + +import zope.component as capi +from zope.component.tests.placelesssetup import PlacelessSetup + +from zope.i18n.negotiator import negotiator +from zope.i18n.interfaces import INegotiator, IUserPreferredLanguages +from zope.i18n.interfaces import ITranslationDomain + +class Environment(object): + + implements(IUserPreferredLanguages) + + def __init__(self, langs=()): + self.langs = langs + + def getPreferredLanguages(self): + return self.langs + +class TestITranslationDomain(PlacelessSetup): + + # This should be overwritten by every class that inherits this test + def _getTranslationDomain(self): + pass + + def setUp(self): + super(TestITranslationDomain, self).setUp() + self._domain = self._getTranslationDomain() + + # Setup the negotiator utility + capi.provideUtility(negotiator, INegotiator) + + def testInterface(self): + verifyObject(ITranslationDomain, self._domain) + + def testSimpleTranslate(self): + translate = self._domain.translate + eq = self.assertEqual + # Test that a given message id is properly translated in a supported + # language + eq(translate('short_greeting', target_language='de'), 'Hallo!') + # Same test, but use the context argument + context = Environment(('de', 'en')) + eq(translate('short_greeting', context=context), 'Hallo!') + + def testDynamicTranslate(self): + translate = self._domain.translate + eq = self.assertEqual + # Testing both translation and interpolation + eq(translate('greeting', mapping={'name': 'Stephan'}, + target_language='de'), + 'Hallo Stephan, wie geht es Dir?') + # Testing default value interpolation + eq(translate('greeting', mapping={'name': 'Philipp'}, + target_language='fr', + default="Hello $name, how are you?"), + 'Hello Philipp, how are you?') + + def testNoTranslation(self): + translate = self._domain.translate + eq = self.assertEqual + # Test that an unknown message id returns None as a translation + eq(translate('glorp_smurf_hmpf', target_language='en'), + None) + # Test default value behaviour + eq(translate('glorp_smurf_hmpf', target_language='en', + default='Glorp Smurf Hmpf'), + 'Glorp Smurf Hmpf') + + def testNoTargetLanguage(self): + translate = self._domain.translate + eq = self.assertEqual + # Test that default is returned when no language can be negotiated + context = Environment(('xx', )) + eq(translate('short_greeting', context=context, default=42), 42) + + # Test that default is returned when there's no destination language + eq(translate('short_greeting', default=42), 42) + + +def test_suite(): + return unittest.TestSuite() # Deliberately empty diff --git a/tests/test_translationdomain.py b/tests/test_translationdomain.py new file mode 100644 index 0000000..22f39e0 --- /dev/null +++ b/tests/test_translationdomain.py @@ -0,0 +1,140 @@ +############################################################################## +# +# Copyright (c) 2001, 2002 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""This module tests the regular persistent Translation Domain. + +$Id$ +""" +import unittest, os +from zope.i18n.translationdomain import TranslationDomain +from zope.i18n.gettextmessagecatalog import GettextMessageCatalog +from zope.i18n.tests.test_itranslationdomain import \ + TestITranslationDomain, Environment +from zope.i18n import MessageIDFactory +from zope.i18n.interfaces import ITranslationDomain +import zope.component as capi + +def testdir(): + from zope.i18n import tests + return os.path.dirname(tests.__file__) + + +class TestGlobalTranslationDomain(unittest.TestCase, TestITranslationDomain): + + def setUp(self): + TestITranslationDomain.setUp(self) + + def _getTranslationDomain(self): + domain = TranslationDomain('default') + path = testdir() + en_catalog = GettextMessageCatalog('en', 'default', + os.path.join(path, 'en-default.mo')) + de_catalog = GettextMessageCatalog('de', 'default', + os.path.join(path, 'de-default.mo')) + + domain.addCatalog(en_catalog) + domain.addCatalog(de_catalog) + return domain + + def testNoTargetLanguage(self): + # Having a fallback would interfere with this test + self._domain.setLanguageFallbacks([]) + TestITranslationDomain.testNoTargetLanguage(self) + + def testSimpleNoTranslate(self): + translate = self._domain.translate + eq = self.assertEqual + # Unset fallback translation languages + self._domain.setLanguageFallbacks([]) + + # Test that a translation in an unsupported language returns the + # default, if there is no fallback language + eq(translate('short_greeting', target_language='es'), + None) + eq(translate('short_greeting', target_language='es', + default='short_greeting'), + 'short_greeting') + + # Same test, but use the context argument instead of target_language + context = Environment() + eq(translate('short_greeting', context=context), + None) + eq(translate('short_greeting', context=context, + default='short_greeting'), + 'short_greeting') + + def testEmptyStringTranslate(self): + translate = self._domain.translate + self.assertEqual(translate(u'', target_language='en'), u'') + self.assertEqual(translate(u'', target_language='foo'), u'') + + def testStringTranslate(self): + self.assertEqual( + self._domain.translate(u'short_greeting', target_language='en'), + u'Hello!') + + def testMessageIDTranslate(self): + factory = MessageIDFactory('default') + translate = self._domain.translate + msgid = factory(u'short_greeting', 'default') + self.assertEqual(translate(msgid, target_language='en'), u'Hello!') + # MessageID attributes override arguments + msgid = factory('43-not-there', 'this ${that} the other') + msgid.mapping["that"] = "THAT" + self.assertEqual( + translate(msgid, target_language='en', default="default", + mapping={"that": "that"}), "this THAT the other") + + def testMessageIDTranslateForDifferentDomain(self): + domain = TranslationDomain('other') + path = testdir() + en_catalog = GettextMessageCatalog('en', 'other', + os.path.join(path, 'en-default.mo')) + domain.addCatalog(en_catalog) + + capi.provideUtility(domain, ITranslationDomain, 'other') + + factory = MessageIDFactory('other') + msgid = factory(u'short_greeting', 'default') + self.assertEqual( + self._domain.translate(msgid, target_language='en'), u'Hello!') + + def testSimpleFallbackTranslation(self): + translate = self._domain.translate + eq = self.assertEqual + # Test that a translation in an unsupported language returns a + # translation in the fallback language (by default, English) + eq(translate('short_greeting', target_language='es'), + u'Hello!') + # Same test, but use the context argument instead of target_language + context = Environment() + eq(translate('short_greeting', context=context), + u'Hello!') + + def testInterpolationWithoutTranslation(self): + translate = self._domain.translate + self.assertEqual( + translate('42-not-there', target_language="en", + default="this ${that} the other", + mapping={"that": "THAT"}), + "this THAT the other") + + +def test_suite(): + suite = unittest.TestSuite() + suite.addTest(unittest.makeSuite(TestGlobalTranslationDomain)) + return suite + + +if __name__ == '__main__': + unittest.TextTestRunner().run(test_suite()) |