summaryrefslogtreecommitdiff
path: root/oslo_i18n/tests
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2014-10-30 11:54:05 +0800
committerPeng Wu <alexepico@gmail.com>2015-07-01 14:56:54 +0800
commit9e3132bc2f06da87b5c78257a267c5431f433e74 (patch)
treedbbec248988442e6737dde0b47881a746f5894c4 /oslo_i18n/tests
parente9429faafee6a3404d0297bd0d0b14d2077d2891 (diff)
downloadoslo-i18n-9e3132bc2f06da87b5c78257a267c5431f433e74.tar.gz
Support contextual and plural form of gettext functions
This draft patch is created for more-gettext-support blueprint review. Refer URL: https://blueprints.launchpad.net/oslo.i18n/+spec/more-gettext-support https://review.openstack.org/#q,topic:bp/more-gettext-support,n,z Implements: blueprint more-gettext-support Depends-on: I258eac447ecc7b71fb02952077cf3ef3ecfe12bb Change-Id: Ic16d902ddfe94cfb5cfabe1c4f612ff001a8fa53
Diffstat (limited to 'oslo_i18n/tests')
-rw-r--r--oslo_i18n/tests/test_factory.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/oslo_i18n/tests/test_factory.py b/oslo_i18n/tests/test_factory.py
index 771728e..5d1373f 100644
--- a/oslo_i18n/tests/test_factory.py
+++ b/oslo_i18n/tests/test_factory.py
@@ -22,6 +22,9 @@ from oslo_i18n import _factory
from oslo_i18n import _lazy
from oslo_i18n import _message
+# magic gettext number to separate context from message
+CONTEXT_SEPARATOR = "\x04"
+
class TranslatorFactoryTest(test_base.BaseTestCase):
@@ -89,3 +92,57 @@ class TranslatorFactoryTest(test_base.BaseTestCase):
tf = _factory.TranslatorFactory('domain')
tf._make_log_translation_func('mylevel')
mtf.assert_called_with('domain-log-mylevel')
+
+ def test_contextual_form_py2(self):
+ _lazy.enable_lazy(False)
+ with mock.patch.object(six, 'PY3', False):
+ with mock.patch('gettext.translation') as translation:
+ trans = mock.Mock()
+ translation.return_value = trans
+ trans.gettext.side_effect = AssertionError(
+ 'should have called ugettext')
+ trans.ugettext.return_value = "some text"
+ tf = _factory.TranslatorFactory('domain')
+ tf.contextual_form('context', 'some text')
+ trans.ugettext.assert_called_with(
+ "%s%s%s" % ('context', CONTEXT_SEPARATOR, 'some text'))
+
+ def test_contextual_form_py3(self):
+ _lazy.enable_lazy(False)
+ with mock.patch.object(six, 'PY3', True):
+ with mock.patch('gettext.translation') as translation:
+ trans = mock.Mock()
+ translation.return_value = trans
+ trans.ugettext.side_effect = AssertionError(
+ 'should have called gettext')
+ trans.gettext.return_value = "some text"
+ tf = _factory.TranslatorFactory('domain')
+ tf.contextual_form('context', 'some text')
+ trans.gettext.assert_called_with(
+ "%s%s%s" % ('context', CONTEXT_SEPARATOR, 'some text'))
+
+ def test_plural_form_py2(self):
+ _lazy.enable_lazy(False)
+ with mock.patch.object(six, 'PY3', False):
+ with mock.patch('gettext.translation') as translation:
+ trans = mock.Mock()
+ translation.return_value = trans
+ trans.ngettext.side_effect = AssertionError(
+ 'should have called ungettext')
+ tf = _factory.TranslatorFactory('domain')
+ tf.plural_form('single', 'plural', 1)
+ trans.ungettext.assert_called_with(
+ 'single', 'plural', 1)
+
+ def test_plural_form_py3(self):
+ _lazy.enable_lazy(False)
+ with mock.patch.object(six, 'PY3', True):
+ with mock.patch('gettext.translation') as translation:
+ trans = mock.Mock()
+ translation.return_value = trans
+ trans.ungettext.side_effect = AssertionError(
+ 'should have called ngettext')
+ tf = _factory.TranslatorFactory('domain')
+ tf.plural_form('single', 'plural', 1)
+ trans.ngettext.assert_called_with(
+ 'single', 'plural', 1)