Skip to content

Commit

Permalink
Merge branch 'jinty-stop-clobbering-mappings'
Browse files Browse the repository at this point in the history
  • Loading branch information
tseaver committed May 30, 2016
2 parents c7ec5ff + 6575c39 commit 09859c1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 22 deletions.
7 changes: 6 additions & 1 deletion translationstring/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ def __new__(self, msgid, domain=None, default=None, mapping=None, context=None):
domain = domain or msgid.domain and msgid.domain[:]
context = context or msgid.context and msgid.context[:]
default = default or msgid.default and msgid.default[:]
mapping = mapping or msgid.mapping and msgid.mapping.copy()
if msgid.mapping:
if mapping:
for k, v in msgid.mapping.items():
mapping.setdefault(k, v)
else:
mapping = msgid.mapping.copy()
msgid = text_type(msgid)
self.domain = domain
self.context = context
Expand Down
76 changes: 55 additions & 21 deletions translationstring/tests/test__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
from translationstring.compat import u

class TestTranslationString(unittest.TestCase):

def _getTargetClass(self):
from translationstring import TranslationString
return TranslationString

def _makeOne(self, msgid, **kw):
klass = self._getTargetClass()
return klass(msgid, **kw)
Expand Down Expand Up @@ -117,7 +118,7 @@ def test___reduce__(self):
inst = self._makeOne('msgid', default='default', domain='domain',
mapping='mapping')
result = inst.__reduce__()
self.assertEqual(result, (klass, (u('msgid'), 'domain', u('default'),
self.assertEqual(result, (klass, (u('msgid'), 'domain', u('default'),
'mapping', None)))

def test___getstate__(self):
Expand All @@ -128,6 +129,7 @@ def test___getstate__(self):
(u('msgid'), 'domain', u('default'), 'mapping', None))

class TestTranslationStringFactory(unittest.TestCase):

def _makeOne(self, domain):
from translationstring import TranslationStringFactory
return TranslationStringFactory(domain)
Expand All @@ -144,7 +146,8 @@ def test_msgid_is_translation_string_override_domain(self):
user_factory = self._makeOne('user')
factory = self._makeOne('budge')

wrapped_inst = user_factory('wrapped_msgid', mapping={'a':1}, default='default')
wrapped_inst = user_factory(
'wrapped_msgid', mapping={'a':1}, default='default')
wrapper_inst = factory(wrapped_inst)

self.assertEqual(str(wrapper_inst), 'wrapped_msgid')
Expand All @@ -154,15 +157,31 @@ def test_msgid_is_translation_string_override_kwarg(self):
user_factory = self._makeOne('user')
factory = self._makeOne('budge')

wrapped_inst = user_factory('wrapped_msgid', mapping={'a':1}, default='default')
wrapper_inst = factory(wrapped_inst, mapping={'b':1}, default='other_default')
wrapped_inst = user_factory('wrapped_msgid', default='default')
wrapper_inst = factory(wrapped_inst, default='other_default')

self.assertEqual(str(wrapper_inst), 'wrapped_msgid')
self.assertEqual(wrapper_inst.mapping, {'b':1})
self.assertEqual(wrapper_inst.default, 'other_default')

def test_msgid_is_translation_string_update_mapping(self):
user_factory = self._makeOne('user')
factory = self._makeOne('budge')

# if the inner msgid defines a mapping
wrapped_inst = user_factory('wrapped_msgid: ${a} ${b} ${c}',
mapping={'a': 1, 'b': 1},
default='default')
wrapper_inst = factory(wrapped_inst,
mapping={'b': 2, 'c': 2},
default='other_default')

self.assertEqual(str(wrapper_inst), 'wrapped_msgid: ${a} ${b} ${c}')
# it must be present when wrapped
self.assertEqual(wrapper_inst.mapping, {'a': 1, 'b': 2, 'c': 2})


class TestChameleonTranslate(unittest.TestCase):

def _makeOne(self, translator):
from translationstring import ChameleonTranslate
return ChameleonTranslate(translator)
Expand Down Expand Up @@ -216,6 +235,7 @@ def translator(msg):


class TestTranslator(unittest.TestCase):

def _makeOne(self, translations=None, policy=None):
from translationstring import Translator
return Translator(translations, policy)
Expand All @@ -225,7 +245,7 @@ def test_translations_None_interpolation_required(self):
tstring = DummyTranslationString('$abc', mapping=True)
result = inst(tstring)
self.assertEqual(result, 'interpolated')

def test_translations_None_interpolation_not_required(self):
inst = self._makeOne()
tstring = DummyTranslationString('msgid', mapping=False)
Expand Down Expand Up @@ -276,7 +296,7 @@ def test_translations_None_interpolation_required(self):
inst = self._makeOne()
result = inst('$abc', '$abc', 1, mapping={'abc':1})
self.assertEqual(result, '1')

def test_translations_None_interpolation_not_required(self):
inst = self._makeOne()
result = inst('msgid', 'msgid', 1)
Expand All @@ -292,6 +312,7 @@ def policy(translations, singular, plural, n, domain, context):
self.assertEqual(result, 'translated')

class Test_ugettext_policy(unittest.TestCase):

def _callFUT(self, translations, tstring, domain, context):
from translationstring import ugettext_policy
return ugettext_policy(translations, tstring, domain, context)
Expand All @@ -314,6 +335,7 @@ def test_msgctxt_no_translation_found(self):
self.assertEqual(result, u('p\xf8f'))

class Test_dugettext_policy(unittest.TestCase):

def _callFUT(self, translations, tstring, domain, context=None):
from translationstring import dugettext_policy
return dugettext_policy(translations, tstring, domain, context)
Expand Down Expand Up @@ -357,14 +379,16 @@ def test_msgctxt_from_tstring(self):
translations = DummyTranslations('result')
tstring = DummyTranslationString(u('p\xf8f'), context='button')
result = self._callFUT(translations, tstring, None)
self.assertEqual(translations.params, ('messages', u('button\x04p\xf8f'),))
self.assertEqual(translations.params,
('messages', u('button\x04p\xf8f'),))
self.assertEqual(result, 'result')

def test_msgctxt_override(self):
translations = DummyTranslations('result')
tstring = DummyTranslationString(u('p\xf8f'), context='other')
result = self._callFUT(translations, tstring, None, context='button')
self.assertEqual(translations.params, ('messages', u('button\x04p\xf8f'),))
self.assertEqual(translations.params,
('messages', u('button\x04p\xf8f'),))
self.assertEqual(result, 'result')

def test_msgctxt_no_translation_found(self):
Expand All @@ -374,10 +398,12 @@ def test_msgctxt_no_translation_found(self):
self.assertEqual(result, u('p\xf8f'))

class Test_ungettext_policy(unittest.TestCase):

def _callFUT(self, translations, singular, plural, n, domain=None,
mapping=None, context=None):
from translationstring import ungettext_policy
return ungettext_policy(translations, singular, plural, n, domain, context)
return ungettext_policy(
translations, singular, plural, n, domain, context)

def test_it(self):
translations = DummyTranslations('result')
Expand All @@ -386,21 +412,27 @@ def test_it(self):

def test_msgctxt(self):
translations = DummyTranslations('result')
result = self._callFUT(translations, u('p\xf8f'), 'plural', 1, context='button')
self.assertEqual(translations.params, (u('button\x04p\xf8f'), 'plural', 1))
result = self._callFUT(
translations, u('p\xf8f'), 'plural', 1, context='button')
self.assertEqual(translations.params,
(u('button\x04p\xf8f'), 'plural', 1))
self.assertEqual(result, 'result')

def test_msgctxt_no_translation(self):
translations = DummyTranslations(u('button\x04p\xf8f'))
result = self._callFUT(translations, u('p\xf8f'), 'plural', 1, context='button')
self.assertEqual(translations.params, (u('button\x04p\xf8f'), 'plural', 1))
result = self._callFUT(
translations, u('p\xf8f'), 'plural', 1, context='button')
self.assertEqual(translations.params,
(u('button\x04p\xf8f'), 'plural', 1))
self.assertEqual(result, u('p\xf8f'))

class Test_dungettext_policy(unittest.TestCase):

def _callFUT(self, translations, singular, plural, n, domain=None,
mapping=None, context=None):
from translationstring import dungettext_policy
return dungettext_policy(translations, singular, plural, n, domain, context)
return dungettext_policy(
translations, singular, plural, n, domain, context)

def test_it_use_default_domain(self):
translations = DummyTranslations('result')
Expand All @@ -427,14 +459,15 @@ def test_it_translations_has_no_dungettext(self):
self.assertEqual(result, 'result')

class DummyTranslations(object):

def __init__(self, result, domain=None):
self.result = result
self.domain = domain

def gettext(self, tstring): # pragma: no cover
self.params = (tstring,)
return self.result

def ngettext(self, singular, plural, n): # pragma: no cover
self.params = (singular, plural, n)
return self.result
Expand All @@ -458,7 +491,9 @@ def dungettext(self, domain, singular, plural, n): # pragma: no cover
return self.result

class DummyTranslationString(text_type):
def __new__(cls, msgid='', domain=None, default=None, mapping=None, context=None):

def __new__(cls, msgid='', domain=None, default=None, mapping=None,
context=None):
self = text_type.__new__(cls, msgid)
text_type.__init__(self, msgid)
self.domain = domain
Expand All @@ -468,7 +503,6 @@ def __new__(cls, msgid='', domain=None, default=None, mapping=None, context=None
default = msgid
self.default = default
return self

def interpolate(self, translated=None):
return 'interpolated'

0 comments on commit 09859c1

Please sign in to comment.