| 1 |
import inspect |
| 2 |
import io |
| 3 |
import os |
| 4 |
import shutil |
| 5 |
import sys |
| 6 |
import tempfile |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from babel import support |
| 11 |
from babel.messages import Catalog |
| 12 |
from babel.messages.mofile import write_mo |
| 13 |
|
| 14 |
SKIP_LGETTEXT = sys.version_info >= (3, 8) |
| 15 |
|
| 16 |
messages1 = [ |
| 17 |
('foo', {'string': 'Voh'}), |
| 18 |
('foo', {'string': 'VohCTX', 'context': 'foo'}), |
| 19 |
(('foo1', 'foos1'), {'string': ('Voh1', 'Vohs1')}), |
| 20 |
(('foo1', 'foos1'), {'string': ('VohCTX1', 'VohsCTX1'), 'context': 'foo'}), |
| 21 |
] |
| 22 |
|
| 23 |
messages2 = [ |
| 24 |
('foo', {'string': 'VohD'}), |
| 25 |
('foo', {'string': 'VohCTXD', 'context': 'foo'}), |
| 26 |
(('foo1', 'foos1'), {'string': ('VohD1', 'VohsD1')}), |
| 27 |
(('foo1', 'foos1'), {'string': ('VohCTXD1', 'VohsCTXD1'), 'context': 'foo'}), |
| 28 |
] |
| 29 |
|
| 30 |
|
| 31 |
@pytest.fixture(autouse=True) |
| 32 |
def use_en_us_locale(monkeypatch): |
| 33 |
# Use a locale which won't fail to run the tests |
| 34 |
monkeypatch.setenv('LANG', 'en_US.UTF-8') |
| 35 |
|
| 36 |
|
| 37 |
@pytest.fixture() |
| 38 |
def translations() -> support.Translations: |
| 39 |
catalog1 = Catalog(locale='en_GB', domain='messages') |
| 40 |
catalog2 = Catalog(locale='en_GB', domain='messages1') |
| 41 |
for ids, kwargs in messages1: |
| 42 |
catalog1.add(ids, **kwargs) |
| 43 |
for ids, kwargs in messages2: |
| 44 |
catalog2.add(ids, **kwargs) |
| 45 |
catalog1_fp = io.BytesIO() |
| 46 |
catalog2_fp = io.BytesIO() |
| 47 |
write_mo(catalog1_fp, catalog1) |
| 48 |
catalog1_fp.seek(0) |
| 49 |
write_mo(catalog2_fp, catalog2) |
| 50 |
catalog2_fp.seek(0) |
| 51 |
translations1 = support.Translations(catalog1_fp) |
| 52 |
translations2 = support.Translations(catalog2_fp, domain='messages1') |
| 53 |
return translations1.add(translations2, merge=False) |
| 54 |
|
| 55 |
|
| 56 |
@pytest.fixture(scope='module') |
| 57 |
def empty_translations() -> support.Translations: |
| 58 |
fp = io.BytesIO() |
| 59 |
write_mo(fp, Catalog(locale='de')) |
| 60 |
fp.seek(0) |
| 61 |
return support.Translations(fp=fp) |
| 62 |
|
| 63 |
|
| 64 |
@pytest.fixture(scope='module') |
| 65 |
def null_translations() -> support.NullTranslations: |
| 66 |
fp = io.BytesIO() |
| 67 |
write_mo(fp, Catalog(locale='de')) |
| 68 |
fp.seek(0) |
| 69 |
return support.NullTranslations(fp=fp) |
| 70 |
|
| 71 |
|
| 72 |
def assert_equal_type_too(expected, result) -> None: |
| 73 |
assert expected == result |
| 74 |
assert type(expected) is type(result), ( |
| 75 |
f'instance types do not match: {type(expected)!r}!={type(result)!r}' |
| 76 |
) |
| 77 |
|
| 78 |
|
| 79 |
def test_pgettext(translations): |
| 80 |
assert_equal_type_too('Voh', translations.gettext('foo')) |
| 81 |
assert_equal_type_too('VohCTX', translations.pgettext('foo', 'foo')) |
| 82 |
assert_equal_type_too('VohCTX1', translations.pgettext('foo', 'foo1')) |
| 83 |
|
| 84 |
|
| 85 |
def test_pgettext_fallback(translations): |
| 86 |
fallback = translations._fallback |
| 87 |
translations._fallback = support.NullTranslations() |
| 88 |
assert translations.pgettext('foo', 'bar') == 'bar' |
| 89 |
translations._fallback = fallback |
| 90 |
|
| 91 |
|
| 92 |
def test_upgettext(translations): |
| 93 |
assert_equal_type_too('Voh', translations.ugettext('foo')) |
| 94 |
assert_equal_type_too('VohCTX', translations.upgettext('foo', 'foo')) |
| 95 |
|
| 96 |
|
| 97 |
@pytest.mark.skipif(SKIP_LGETTEXT, reason='lgettext is deprecated') |
| 98 |
def test_lpgettext(translations): |
| 99 |
assert_equal_type_too(b'Voh', translations.lgettext('foo')) |
| 100 |
assert_equal_type_too(b'VohCTX', translations.lpgettext('foo', 'foo')) |
| 101 |
|
| 102 |
|
| 103 |
def test_npgettext(translations): |
| 104 |
assert_equal_type_too('Voh1', translations.ngettext('foo1', 'foos1', 1)) |
| 105 |
assert_equal_type_too('Vohs1', translations.ngettext('foo1', 'foos1', 2)) |
| 106 |
assert_equal_type_too('VohCTX1', translations.npgettext('foo', 'foo1', 'foos1', 1)) |
| 107 |
assert_equal_type_too('VohsCTX1', translations.npgettext('foo', 'foo1', 'foos1', 2)) |
| 108 |
|
| 109 |
|
| 110 |
def test_unpgettext(translations): |
| 111 |
assert_equal_type_too('Voh1', translations.ungettext('foo1', 'foos1', 1)) |
| 112 |
assert_equal_type_too('Vohs1', translations.ungettext('foo1', 'foos1', 2)) |
| 113 |
assert_equal_type_too('VohCTX1', translations.unpgettext('foo', 'foo1', 'foos1', 1)) |
| 114 |
assert_equal_type_too('VohsCTX1', translations.unpgettext('foo', 'foo1', 'foos1', 2)) |
| 115 |
|
| 116 |
|
| 117 |
@pytest.mark.skipif(SKIP_LGETTEXT, reason='lgettext is deprecated') |
| 118 |
def test_lnpgettext(translations): |
| 119 |
assert_equal_type_too(b'Voh1', translations.lngettext('foo1', 'foos1', 1)) |
| 120 |
assert_equal_type_too(b'Vohs1', translations.lngettext('foo1', 'foos1', 2)) |
| 121 |
assert_equal_type_too(b'VohCTX1', translations.lnpgettext('foo', 'foo1', 'foos1', 1)) |
| 122 |
assert_equal_type_too(b'VohsCTX1', translations.lnpgettext('foo', 'foo1', 'foos1', 2)) |
| 123 |
|
| 124 |
|
| 125 |
def test_dpgettext(translations): |
| 126 |
assert_equal_type_too('VohD', translations.dgettext('messages1', 'foo')) |
| 127 |
assert_equal_type_too('VohCTXD', translations.dpgettext('messages1', 'foo', 'foo')) |
| 128 |
|
| 129 |
|
| 130 |
def test_dupgettext(translations): |
| 131 |
assert_equal_type_too('VohD', translations.dugettext('messages1', 'foo')) |
| 132 |
assert_equal_type_too('VohCTXD', translations.dupgettext('messages1', 'foo', 'foo')) |
| 133 |
|
| 134 |
|
| 135 |
@pytest.mark.skipif(SKIP_LGETTEXT, reason='lgettext is deprecated') |
| 136 |
def test_ldpgettext(translations): |
| 137 |
assert_equal_type_too(b'VohD', translations.ldgettext('messages1', 'foo')) |
| 138 |
assert_equal_type_too(b'VohCTXD', translations.ldpgettext('messages1', 'foo', 'foo')) |
| 139 |
|
| 140 |
|
| 141 |
def test_dnpgettext(translations): |
| 142 |
assert_equal_type_too('VohD1', translations.dngettext('messages1', 'foo1', 'foos1', 1)) |
| 143 |
assert_equal_type_too('VohsD1', translations.dngettext('messages1', 'foo1', 'foos1', 2)) |
| 144 |
assert_equal_type_too('VohCTXD1', translations.dnpgettext('messages1', 'foo', 'foo1', 'foos1', 1)) |
| 145 |
assert_equal_type_too('VohsCTXD1', translations.dnpgettext('messages1', 'foo', 'foo1', 'foos1', 2)) |
| 146 |
|
| 147 |
|
| 148 |
def test_dunpgettext(translations): |
| 149 |
assert_equal_type_too('VohD1', translations.dungettext('messages1', 'foo1', 'foos1', 1)) |
| 150 |
assert_equal_type_too('VohsD1', translations.dungettext('messages1', 'foo1', 'foos1', 2)) |
| 151 |
assert_equal_type_too('VohCTXD1', translations.dunpgettext('messages1', 'foo', 'foo1', 'foos1', 1)) |
| 152 |
assert_equal_type_too('VohsCTXD1', translations.dunpgettext('messages1', 'foo', 'foo1', 'foos1', 2)) |
| 153 |
|
| 154 |
|
| 155 |
@pytest.mark.skipif(SKIP_LGETTEXT, reason='lgettext is deprecated') |
| 156 |
def test_ldnpgettext(translations): |
| 157 |
assert_equal_type_too(b'VohD1', translations.ldngettext('messages1', 'foo1', 'foos1', 1)) |
| 158 |
assert_equal_type_too(b'VohsD1', translations.ldngettext('messages1', 'foo1', 'foos1', 2)) |
| 159 |
assert_equal_type_too(b'VohCTXD1', translations.ldnpgettext('messages1', 'foo', 'foo1', 'foos1', 1)) |
| 160 |
assert_equal_type_too(b'VohsCTXD1', translations.ldnpgettext('messages1', 'foo', 'foo1', 'foos1', 2)) |
| 161 |
|
| 162 |
|
| 163 |
def test_load(translations): |
| 164 |
tempdir = tempfile.mkdtemp() |
| 165 |
try: |
| 166 |
messages_dir = os.path.join(tempdir, 'fr', 'LC_MESSAGES') |
| 167 |
os.makedirs(messages_dir) |
| 168 |
catalog = Catalog(locale='fr', domain='messages') |
| 169 |
catalog.add('foo', 'bar') |
| 170 |
with open(os.path.join(messages_dir, 'messages.mo'), 'wb') as f: |
| 171 |
write_mo(f, catalog) |
| 172 |
|
| 173 |
translations = support.Translations.load(tempdir, locales=('fr',), domain='messages') |
| 174 |
assert translations.gettext('foo') == 'bar' |
| 175 |
finally: |
| 176 |
shutil.rmtree(tempdir) |
| 177 |
|
| 178 |
|
| 179 |
def get_gettext_method_names(obj): |
| 180 |
names = [name for name in dir(obj) if 'gettext' in name] |
| 181 |
if SKIP_LGETTEXT: |
| 182 |
# Remove deprecated l*gettext functions |
| 183 |
names = [name for name in names if not name.startswith('l')] |
| 184 |
return names |
| 185 |
|
| 186 |
|
| 187 |
def test_null_translations_have_same_methods(empty_translations, null_translations): |
| 188 |
for name in get_gettext_method_names(empty_translations): |
| 189 |
assert hasattr(null_translations, name), f'NullTranslations does not provide method {name!r}' |
| 190 |
|
| 191 |
|
| 192 |
def test_null_translations_method_signature_compatibility(empty_translations, null_translations): |
| 193 |
for name in get_gettext_method_names(empty_translations): |
| 194 |
assert ( |
| 195 |
inspect.getfullargspec(getattr(empty_translations, name)) == |
| 196 |
inspect.getfullargspec(getattr(null_translations, name)) |
| 197 |
) |
| 198 |
|
| 199 |
|
| 200 |
def test_null_translations_same_return_values(empty_translations, null_translations): |
| 201 |
data = { |
| 202 |
'message': 'foo', |
| 203 |
'domain': 'domain', |
| 204 |
'context': 'tests', |
| 205 |
'singular': 'bar', |
| 206 |
'plural': 'baz', |
| 207 |
'num': 1, |
| 208 |
'msgid1': 'bar', |
| 209 |
'msgid2': 'baz', |
| 210 |
'n': 1, |
| 211 |
} |
| 212 |
for name in get_gettext_method_names(empty_translations): |
| 213 |
method = getattr(empty_translations, name) |
| 214 |
null_method = getattr(null_translations, name) |
| 215 |
signature = inspect.getfullargspec(method) |
| 216 |
parameter_names = [name for name in signature.args if name != 'self'] |
| 217 |
values = [data[name] for name in parameter_names] |
| 218 |
assert method(*values) == null_method(*values) |
| 219 |
|
| 220 |
|
| 221 |
def test_catalog_merge_files(): |
| 222 |
# Refs issues #92, #162 |
| 223 |
t1 = support.Translations() |
| 224 |
assert t1.files == [] |
| 225 |
t1._catalog['foo'] = 'bar' |
| 226 |
fp = io.BytesIO() |
| 227 |
write_mo(fp, Catalog()) |
| 228 |
fp.seek(0) |
| 229 |
fp.name = 'pro.mo' |
| 230 |
t2 = support.Translations(fp) |
| 231 |
assert t2.files == ['pro.mo'] |
| 232 |
t2._catalog['bar'] = 'quux' |
| 233 |
t1.merge(t2) |
| 234 |
assert t1.files == ['pro.mo'] |
| 235 |
assert set(t1._catalog.keys()) == {'', 'foo', 'bar'} |