From: Aarni Koskela Date: Sat, 31 Jan 2026 12:49:40 +0000 (+0100) Subject: Simplify some tests X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8e57bbb8f8f3d793e5f42e1a6dd3c955358cf947;p=thirdparty%2Fbabel.git Simplify some tests --- diff --git a/tests/messages/frontend/test_cli.py b/tests/messages/frontend/test_cli.py index 200632ec..fb085984 100644 --- a/tests/messages/frontend/test_cli.py +++ b/tests/messages/frontend/test_cli.py @@ -59,11 +59,7 @@ def test_usage(cli): 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): diff --git a/tests/test_core.py b/tests/test_core.py index 461d7078..4e56250d 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -78,7 +78,6 @@ def test_hash(): class TestLocaleClass: - def test_attributes(self): locale = Locale('en', 'US') assert locale.language == 'en' @@ -158,8 +157,7 @@ class TestLocaleClass: 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' @@ -194,10 +192,9 @@ class TestLocaleClass: 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%' diff --git a/tests/test_date_intervals.py b/tests/test_date_intervals.py index c0532c9d..e9c4c7cd 100644 --- a/tests/test_date_intervals.py +++ b/tests/test_date_intervals.py @@ -1,5 +1,7 @@ import datetime +import pytest + from babel import dates from babel.util import UTC @@ -8,20 +10,17 @@ TEST_TIME = TEST_DT.time() 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(): @@ -34,27 +33,31 @@ def test_format_interval_in_tz(timezone_getter): 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' diff --git a/tests/test_languages.py b/tests/test_languages.py index 41fcc9e8..4a8b2dc6 100644 --- a/tests/test_languages.py +++ b/tests/test_languages.py @@ -10,7 +10,5 @@ def test_official_languages(): 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 diff --git a/tests/test_localedata.py b/tests/test_localedata.py index 03cbed1d..5962a53c 100644 --- a/tests/test_localedata.py +++ b/tests/test_localedata.py @@ -14,7 +14,6 @@ import os import pickle import random import sys -import tempfile import pytest @@ -54,7 +53,10 @@ def test_merge_with_alias_and_resolve(): 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(): @@ -136,16 +138,15 @@ def test_locale_identifiers_cache(monkeypatch): 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") diff --git a/tests/test_numbers_parsing.py b/tests/test_numbers_parsing.py index 0b1d03ca..16eae65a 100644 --- a/tests/test_numbers_parsing.py +++ b/tests/test_numbers_parsing.py @@ -18,10 +18,11 @@ from babel import numbers 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): diff --git a/tests/test_plural.py b/tests/test_plural.py index bde356bc..94d917de 100644 --- a/tests/test_plural.py +++ b/tests/test_plural.py @@ -13,7 +13,7 @@ import decimal import pytest -from babel import localedata, plural +from babel import Locale, localedata, plural EPSILON = decimal.Decimal("0.0001") @@ -67,8 +67,8 @@ def test_plural_other_is_ignored(): 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(): @@ -82,8 +82,8 @@ 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(): @@ -133,7 +133,6 @@ def test_plural_within_rules(): 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' diff --git a/tests/test_support_translations.py b/tests/test_support_translations.py index 7e6dc59f..bf2eb2b9 100644 --- a/tests/test_support_translations.py +++ b/tests/test_support_translations.py @@ -186,15 +186,19 @@ def get_gettext_method_names(obj): 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):