Skip to content

Commit

Permalink
Trim trailing whitespace, wrap long lines, let class statement breathe.
Browse files Browse the repository at this point in the history
  • Loading branch information
tseaver committed May 30, 2016
1 parent 5bfc3ac commit 6575c39
Showing 1 changed file with 43 additions and 20 deletions.
63 changes: 43 additions & 20 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 @@ -165,15 +168,20 @@ def test_msgid_is_translation_string_update_mapping(self):
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')
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 @@ -227,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 @@ -236,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 @@ -287,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 @@ -303,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 @@ -325,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 @@ -368,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 @@ -385,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 @@ -397,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 @@ -438,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 @@ -469,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 @@ -479,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 6575c39

Please sign in to comment.