summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDoug Hellmann <doug@doughellmann.com>2014-08-11 19:12:32 -0400
committerDoug Hellmann <doug@doughellmann.com>2014-08-12 11:35:30 -0400
commita9d8efcf6cd74b3d2a75b10b26b45296a318504e (patch)
tree75785b45f300be2f8c6a0d393cdae5de8866685e
parentdac0f3ce5f3abb42662c0064c4a5738483ddcb0a (diff)
downloadoslo-i18n-a9d8efcf6cd74b3d2a75b10b26b45296a318504e.tar.gz
Add a test fixture for translatable strings0.2.0
Change-Id: I96aec806392672ff69a3af6c3e0a04a621656f78
-rw-r--r--doc/source/api.rst5
-rw-r--r--oslo/i18n/fixture.py65
-rw-r--r--tests/test_fixture.py38
3 files changed, 108 insertions, 0 deletions
diff --git a/doc/source/api.rst b/doc/source/api.rst
index 441cde4..4c46c62 100644
--- a/doc/source/api.rst
+++ b/doc/source/api.rst
@@ -31,3 +31,8 @@ oslo.i18n.log
.. automodule:: oslo.i18n.log
:members:
+oslo.i18n.fixture
+=================
+
+.. automodule:: oslo.i18n.fixture
+ :members:
diff --git a/oslo/i18n/fixture.py b/oslo/i18n/fixture.py
new file mode 100644
index 0000000..4a91a2f
--- /dev/null
+++ b/oslo/i18n/fixture.py
@@ -0,0 +1,65 @@
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+"""Test fixtures for working with oslo.i18n.
+
+"""
+
+import fixtures
+import six
+
+from oslo.i18n import _message
+
+
+class Translation(fixtures.Fixture):
+ """Fixture for managing translatable strings.
+
+ This class provides methods for creating translatable strings
+ using both lazy translation and immediate translation. It can be
+ used to generate the different types of messages returned from
+ oslo.i18n to test code that may need to know about the type to
+ handle them differently (for example, error handling in WSGI apps,
+ or logging).
+
+ Use this class to generate messages instead of toggling the global
+ lazy flag and using the regular translation factory.
+
+ """
+
+ def __init__(self, domain='test-domain'):
+ """Initialize the fixture.
+
+ :param domain: The translation domain. This is not expected to
+ coincide with an actual set of message
+ catalogs, but it can.
+ :type domain: str
+ """
+ self.domain = domain
+
+ def lazy(self, msg):
+ """Return a lazily translated message.
+
+ :param msg: Input message string. May optionally include
+ positional or named string interpolation markers.
+ :type msg: str or unicode
+
+ """
+ return _message.Message(msg, domain=self.domain)
+
+ def immediate(self, msg):
+ """Return a string as though it had been translated immediately.
+
+ :param msg: Input message string. May optionally include
+ positional or named string interpolation markers.
+ :type msg: str or unicode
+
+ """
+ return six.text_type(msg)
diff --git a/tests/test_fixture.py b/tests/test_fixture.py
new file mode 100644
index 0000000..66f9994
--- /dev/null
+++ b/tests/test_fixture.py
@@ -0,0 +1,38 @@
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+from oslotest import base as test_base
+import six
+
+from oslo.i18n import _message
+from oslo.i18n import fixture
+
+
+class FixtureTest(test_base.BaseTestCase):
+
+ def setUp(self):
+ super(FixtureTest, self).setUp()
+ self.trans_fixture = self.useFixture(fixture.Translation())
+
+ def test_lazy(self):
+ msg = self.trans_fixture.lazy('this is a lazy message')
+ self.assertIsInstance(msg, _message.Message)
+ self.assertEqual(msg.msgid, 'this is a lazy message')
+
+ def test_immediate(self):
+ msg = self.trans_fixture.immediate('this is a lazy message')
+ # Python 2.6 does not have assertNotIsInstance
+ self.assertFalse(isinstance(msg, _message.Message))
+ self.assertIsInstance(msg, six.text_type)
+ self.assertEqual(msg, u'this is a lazy message')