with pytest.raises(SystemExit) as ei:
cli.run(["pybabel"])
assert ei.value.code == 2
- assert sys.stderr.getvalue().lower() == """\
-usage: pybabel command [options] [args]
-
-pybabel: error: no valid command or option passed. try the -h/--help option for more information.
-"""
+ assert "error: no valid command or option passed" in sys.stderr.getvalue().lower()
def test_list_locales(cli):
class TestLocaleClass:
-
def test_attributes(self):
locale = Locale('en', 'US')
assert locale.language == 'en'
assert Locale('es', 'CO').territories['DE'] == 'Alemania'
def test_variants_property(self):
- assert (Locale('de', 'DE').variants['1901'] ==
- 'Alte deutsche Rechtschreibung')
+ assert Locale('de', 'DE').variants['1901'] == 'Alte deutsche Rechtschreibung'
def test_currencies_property(self):
assert Locale('en').currencies['COP'] == 'Colombian Peso'
assert Locale('en', 'US').decimal_formats[None].pattern == '#,##0.###'
def test_currency_formats_property(self):
- assert (Locale('en', 'US').currency_formats['standard'].pattern ==
- '\xa4#,##0.00')
- assert (Locale('en', 'US').currency_formats['accounting'].pattern ==
- '\xa4#,##0.00;(\xa4#,##0.00)')
+ en_us_currency_format = Locale('en', 'US').currency_formats
+ assert en_us_currency_format['standard'].pattern == '\xa4#,##0.00'
+ assert en_us_currency_format['accounting'].pattern == '\xa4#,##0.00;(\xa4#,##0.00)'
def test_percent_formats_property(self):
assert Locale('en', 'US').percent_formats[None].pattern == '#,##0%'
import datetime
+import pytest
+
from babel import dates
from babel.util import UTC
TEST_DATE = TEST_DT.date()
-def test_format_interval_same_instant_1():
- assert dates.format_interval(TEST_DT, TEST_DT, "yMMMd", fuzzy=False, locale="fi") == "8.1.2016"
-
-
-def test_format_interval_same_instant_2():
- assert dates.format_interval(TEST_DT, TEST_DT, "xxx", fuzzy=False, locale="fi") == "8.1.2016 11.46.15"
-
-
-def test_format_interval_same_instant_3():
- assert dates.format_interval(TEST_TIME, TEST_TIME, "xxx", fuzzy=False, locale="fi") == "11.46.15"
-
-
-def test_format_interval_same_instant_4():
- assert dates.format_interval(TEST_DATE, TEST_DATE, "xxx", fuzzy=False, locale="fi") == "8.1.2016"
+@pytest.parametrize(
+ ("start", "end", "skeleton", "expected"),
+ [
+ (TEST_DT, TEST_DT, "yMMMd", "8.1.2016"),
+ (TEST_DT, TEST_DT, "xxx", "8.1.2016 11.46.15"),
+ (TEST_TIME, TEST_TIME, "xxx", "11.46.15"),
+ (TEST_DATE, TEST_DATE, "xxx", "8.1.2016"),
+ ],
+)
+def test_format_interval_same_instant(start, end, skeleton, expected):
+ assert dates.format_interval(start, end, skeleton, fuzzy=False, locale="fi") == expected
def test_format_interval_no_difference():
t1 = TEST_DT.replace(tzinfo=UTC)
t2 = t1 + datetime.timedelta(minutes=18)
hki_tz = timezone_getter("Europe/Helsinki")
- assert dates.format_interval(t1, t2, "Hmv", tzinfo=hki_tz, locale="fi") == "13.46\u201314.04 aikavyöhyke: Suomi"
+ formatted = dates.format_interval(t1, t2, "Hmv", tzinfo=hki_tz, locale="fi")
+ assert formatted == "13.46\u201314.04 aikavyöhyke: Suomi"
def test_format_interval_12_hour():
t2 = TEST_DT
t1 = t2 - datetime.timedelta(hours=1)
- assert dates.format_interval(t1, t2, "hm", locale="en") == "10:46\u2009\u2013\u200911:46\u202fAM"
+ formatted = dates.format_interval(t1, t2, "hm", locale="en")
+ assert formatted == "10:46\u2009\u2013\u200911:46\u202fAM"
def test_format_interval_invalid_skeleton():
t1 = TEST_DATE
t2 = TEST_DATE + datetime.timedelta(days=1)
- assert dates.format_interval(t1, t2, "mumumu", fuzzy=False, locale="fi") == "8.1.2016\u20139.1.2016"
+ formatted = dates.format_interval(t1, t2, "mumumu", fuzzy=False, locale="fi")
+ assert formatted == "8.1.2016\u20139.1.2016"
assert dates.format_interval(t1, t2, fuzzy=False, locale="fi") == "8.1.2016\u20139.1.2016"
def test_issue_825():
- assert dates.format_timedelta(
+ formatted = dates.format_timedelta(
datetime.timedelta(hours=1),
granularity='hour',
threshold=100,
format='short',
locale='pt',
- ) == '1 h'
+ )
+ assert formatted == '1 h'
def test_get_language_info():
- assert (
- set(get_territory_language_info("HU")) ==
- {"hu", "fr", "en", "de", "ro", "hr", "sk", "sl"}
- )
+ expected = {"hu", "fr", "en", "de", "ro", "hr", "sk", "sl"}
+ assert set(get_territory_language_info("HU")) == expected
import pickle
import random
import sys
-import tempfile
import pytest
localedata.merge(d1, d2)
assert d1 == {'x': {'a': 1, 'b': 12, 'c': 3, 'd': 14}, 'y': (alias, {'b': 22, 'e': 25})}
d = localedata.LocaleDataDict(d1)
- assert dict(d.items()) == {'x': {'a': 1, 'b': 12, 'c': 3, 'd': 14}, 'y': {'a': 1, 'b': 22, 'c': 3, 'd': 14, 'e': 25}}
+ assert dict(d.items()) == {
+ 'x': {'a': 1, 'b': 12, 'c': 3, 'd': 14},
+ 'y': {'a': 1, 'b': 22, 'c': 3, 'd': 14, 'e': 25},
+ }
def test_load():
assert len(listdir_calls) == 2
-def test_locale_name_cleanup():
+def test_locale_name_cleanup(tmp_path):
"""
Test that locale identifiers are cleaned up to avoid directory traversal.
"""
- no_exist_name = os.path.join(tempfile.gettempdir(), "babel%d.dat" % random.randint(1, 99999))
- with open(no_exist_name, "wb") as f:
- pickle.dump({}, f)
+ no_exist_path = tmp_path / f"babel{random.randint(1, 99999):d}.dat"
+ no_exist_path.write_bytes(pickle.dumps({}))
try:
- name = os.path.splitext(os.path.relpath(no_exist_name, localedata._dirname))[0]
+ name = os.path.splitext(os.path.relpath(no_exist_path, localedata._dirname))[0]
except ValueError:
if sys.platform == "win32":
pytest.skip("unable to form relpath")
def test_can_parse_decimals():
- assert decimal.Decimal('1099.98') == numbers.parse_decimal('1,099.98', locale='en_US')
- assert decimal.Decimal('1099.98') == numbers.parse_decimal('1.099,98', locale='de')
- assert decimal.Decimal('1099.98') == numbers.parse_decimal('1,099.98', locale='ar', numbering_system="default")
- assert decimal.Decimal('1099.98') == numbers.parse_decimal('1٬099٫98', locale='ar_EG', numbering_system="default")
+ v = decimal.Decimal('1099.98')
+ assert numbers.parse_decimal('1,099.98', locale='en_US') == v
+ assert numbers.parse_decimal('1.099,98', locale='de') == v
+ assert numbers.parse_decimal('1,099.98', locale='ar', numbering_system="default") == v # fmt: skip
+ assert numbers.parse_decimal('1٬099٫98', locale='ar_EG', numbering_system="default") == v # fmt: skip
with pytest.raises(numbers.NumberFormatError):
numbers.parse_decimal('2,109,998', locale='de')
with pytest.raises(numbers.UnsupportedNumberingSystemError):
import pytest
-from babel import localedata, plural
+from babel import Locale, localedata, plural
EPSILON = decimal.Decimal("0.0001")
def test_to_javascript():
- assert (plural.to_javascript({'one': 'n is 1'})
- == "(function(n) { return (n == 1) ? 'one' : 'other'; })")
+ src = plural.to_javascript({'one': 'n is 1'})
+ assert src == "(function(n) { return (n == 1) ? 'one' : 'other'; })"
def test_to_python():
def test_to_gettext():
- assert (plural.to_gettext({'one': 'n is 1', 'two': 'n is 2'})
- == 'nplurals=3; plural=((n == 1) ? 0 : (n == 2) ? 1 : 2);')
+ src = plural.to_gettext({'one': 'n is 1', 'two': 'n is 2'})
+ assert src == 'nplurals=3; plural=((n == 1) ? 0 : (n == 2) ? 1 : 2);'
def test_in_range_list():
def test_locales_with_no_plural_rules_have_default():
- from babel import Locale
pf = Locale.parse('ii').plural_form
assert pf(1) == 'other'
assert pf(2) == 'other'
def test_null_translations_have_same_methods(empty_translations, null_translations):
for name in get_gettext_method_names(empty_translations):
- assert hasattr(null_translations, name), f'NullTranslations does not provide method {name!r}'
+ assert hasattr(null_translations, name), (
+ f'NullTranslations does not provide method {name!r}'
+ )
-def test_null_translations_method_signature_compatibility(empty_translations, null_translations):
+def test_null_translations_method_signature_compatibility(
+ empty_translations,
+ null_translations,
+):
for name in get_gettext_method_names(empty_translations):
- assert (
- inspect.getfullargspec(getattr(empty_translations, name)) ==
- inspect.getfullargspec(getattr(null_translations, name))
- )
+ spec1 = inspect.getfullargspec(getattr(empty_translations, name))
+ spec2 = inspect.getfullargspec(getattr(null_translations, name))
+ assert spec1 == spec2
def test_null_translations_same_return_values(empty_translations, null_translations):