mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
Added request.ugettext() and request.ungettext() functions; added corresponding unit tests
This commit is contained in:
parent
c081ce5460
commit
c161784973
@ -38,7 +38,7 @@ to the caller.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from inspect import isclass
|
from inspect import isclass
|
||||||
import request
|
from request import ugettext, ungettext
|
||||||
|
|
||||||
|
|
||||||
class PrivateError(StandardError):
|
class PrivateError(StandardError):
|
||||||
@ -159,7 +159,7 @@ class PublicError(StandardError):
|
|||||||
def __init__(self, message=None, **kw):
|
def __init__(self, message=None, **kw):
|
||||||
self.kw = kw
|
self.kw = kw
|
||||||
if message is None:
|
if message is None:
|
||||||
message = self.get_format(request._) % kw
|
message = self.get_format() % kw
|
||||||
StandardError.__init__(self, message)
|
StandardError.__init__(self, message)
|
||||||
|
|
||||||
def get_format(self, _):
|
def get_format(self, _):
|
||||||
|
@ -32,12 +32,20 @@ from constants import OVERRIDE_ERROR
|
|||||||
context = threading.local()
|
context = threading.local()
|
||||||
|
|
||||||
|
|
||||||
def _(message):
|
def ugettext(message):
|
||||||
if hasattr(context, 'gettext'):
|
if hasattr(context, 'ugettext'):
|
||||||
return context.gettext(message)
|
return context.ugettext(message)
|
||||||
return message.decode('UTF-8')
|
return message.decode('UTF-8')
|
||||||
|
|
||||||
|
|
||||||
|
def ungettext(singular, plural, n):
|
||||||
|
if hasattr(context, 'ungettext'):
|
||||||
|
return context.ungettext(singular, plural, n)
|
||||||
|
if n == 1:
|
||||||
|
return singular.decode('UTF-8')
|
||||||
|
return plural.decode('UTF-8')
|
||||||
|
|
||||||
|
|
||||||
def set_languages(*languages):
|
def set_languages(*languages):
|
||||||
if hasattr(context, 'languages'):
|
if hasattr(context, 'languages'):
|
||||||
raise StandardError(OVERRIDE_ERROR %
|
raise StandardError(OVERRIDE_ERROR %
|
||||||
|
@ -23,11 +23,77 @@ Test the `ipalib.request` module.
|
|||||||
|
|
||||||
import threading
|
import threading
|
||||||
import locale
|
import locale
|
||||||
from tests.util import raises, TempDir
|
from tests.util import raises, TempDir, DummyUGettext, DummyUNGettext
|
||||||
from ipalib.constants import OVERRIDE_ERROR
|
from ipalib.constants import OVERRIDE_ERROR
|
||||||
from ipalib import request
|
from ipalib import request
|
||||||
|
|
||||||
|
|
||||||
|
def assert_equal(val1, val2):
|
||||||
|
assert type(val1) is type(val2), '%r != %r' % (val1, val2)
|
||||||
|
assert val1 == val2, '%r != %r' % (val1, val2)
|
||||||
|
|
||||||
|
|
||||||
|
def test_ugettext():
|
||||||
|
"""
|
||||||
|
Test the `ipalib.request.ugettext` function.
|
||||||
|
"""
|
||||||
|
f = request.ugettext
|
||||||
|
context = request.context
|
||||||
|
message = 'Hello, world!'
|
||||||
|
|
||||||
|
# Test with no context.ugettext:
|
||||||
|
assert not hasattr(context, 'ugettext')
|
||||||
|
assert_equal(f(message), u'Hello, world!')
|
||||||
|
|
||||||
|
# Test with dummy context.ugettext:
|
||||||
|
assert not hasattr(context, 'ugettext')
|
||||||
|
dummy = DummyUGettext()
|
||||||
|
context.ugettext = dummy
|
||||||
|
assert f(message) is dummy.translation
|
||||||
|
assert dummy.message is message
|
||||||
|
|
||||||
|
# Cleanup
|
||||||
|
del context.ugettext
|
||||||
|
assert not hasattr(context, 'ugettext')
|
||||||
|
|
||||||
|
|
||||||
|
def test_ungettext():
|
||||||
|
"""
|
||||||
|
Test the `ipalib.request.ungettext` function.
|
||||||
|
"""
|
||||||
|
f = request.ungettext
|
||||||
|
context = request.context
|
||||||
|
singular = 'Goose'
|
||||||
|
plural = 'Geese'
|
||||||
|
|
||||||
|
# Test with no context.ungettext:
|
||||||
|
assert not hasattr(context, 'ungettext')
|
||||||
|
assert_equal(f(singular, plural, 1), u'Goose')
|
||||||
|
assert_equal(f(singular, plural, 2), u'Geese')
|
||||||
|
|
||||||
|
# Test singular with dummy context.ungettext
|
||||||
|
assert not hasattr(context, 'ungettext')
|
||||||
|
dummy = DummyUNGettext()
|
||||||
|
context.ungettext = dummy
|
||||||
|
assert f(singular, plural, 1) is dummy.translation_singular
|
||||||
|
assert dummy.singular is singular
|
||||||
|
assert dummy.plural is plural
|
||||||
|
assert dummy.n == 1
|
||||||
|
del context.ungettext
|
||||||
|
assert not hasattr(context, 'ungettext')
|
||||||
|
|
||||||
|
# Test plural with dummy context.ungettext
|
||||||
|
assert not hasattr(context, 'ungettext')
|
||||||
|
dummy = DummyUNGettext()
|
||||||
|
context.ungettext = dummy
|
||||||
|
assert f(singular, plural, 2) is dummy.translation_plural
|
||||||
|
assert dummy.singular is singular
|
||||||
|
assert dummy.plural is plural
|
||||||
|
assert dummy.n == 2
|
||||||
|
del context.ungettext
|
||||||
|
assert not hasattr(context, 'ungettext')
|
||||||
|
|
||||||
|
|
||||||
def test_set_languages():
|
def test_set_languages():
|
||||||
"""
|
"""
|
||||||
Test the `ipalib.request.set_languages` function.
|
Test the `ipalib.request.set_languages` function.
|
||||||
|
@ -277,3 +277,38 @@ class PluginTester(object):
|
|||||||
(api, home) = self.finalize(*plugins, **kw)
|
(api, home) = self.finalize(*plugins, **kw)
|
||||||
o = api[namespace][self.plugin.__name__]
|
o = api[namespace][self.plugin.__name__]
|
||||||
return (o, api, home)
|
return (o, api, home)
|
||||||
|
|
||||||
|
|
||||||
|
class DummyUGettext(object):
|
||||||
|
__called = False
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.translation = u'The translation'
|
||||||
|
|
||||||
|
def __call__(self, message):
|
||||||
|
assert type(message) is str
|
||||||
|
assert self.__called is False
|
||||||
|
self.__called = True
|
||||||
|
self.message = message
|
||||||
|
return self.translation
|
||||||
|
|
||||||
|
|
||||||
|
class DummyUNGettext(object):
|
||||||
|
__called = False
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.translation_singular = u'The singular translation'
|
||||||
|
self.translation_plural = u'The plural translation'
|
||||||
|
|
||||||
|
def __call__(self, singular, plural, n):
|
||||||
|
assert type(singular) is str
|
||||||
|
assert type(plural) is str
|
||||||
|
assert type(n) is int
|
||||||
|
assert self.__called is False
|
||||||
|
self.__called = True
|
||||||
|
self.singular = singular
|
||||||
|
self.plural = plural
|
||||||
|
self.n = n
|
||||||
|
if n == 1:
|
||||||
|
return self.translation_singular
|
||||||
|
return self.translation_plural
|
||||||
|
Loading…
Reference in New Issue
Block a user