]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Simplify some tests
authorAarni Koskela <akx@iki.fi>
Sat, 31 Jan 2026 12:49:40 +0000 (13:49 +0100)
committerAarni Koskela <akx@iki.fi>
Sun, 1 Feb 2026 17:44:40 +0000 (18:44 +0100)
tests/messages/frontend/test_cli.py
tests/test_core.py
tests/test_date_intervals.py
tests/test_languages.py
tests/test_localedata.py
tests/test_numbers_parsing.py
tests/test_plural.py
tests/test_support_translations.py

index 200632ec3505d2123c315fe446a7d55564ac9c01..fb085984e8db9fdf341b2da5ab710119df3ff417 100644 (file)
@@ -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):
index 461d7078268be7d31a45710675573c707c9382ae..4e56250d4997f4123deb5a3d557a3dd0505ef925 100644 (file)
@@ -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%'
index c0532c9d2f67e38015d746e53c3d4008f2ccfa0a..e9c4c7cd8b02c51a3d35fc955caba44716033dd5 100644 (file)
@@ -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'
index 41fcc9e83a64ca39968e8de4b671fc9a5bc0973a..4a8b2dc60296e331bd8fba02211c6d861971499f 100644 (file)
@@ -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
index 03cbed1dcc91ca077926a6b426c1f7f40456cd80..5962a53cb705ef2ad1eceb81106712093ec333cc 100644 (file)
@@ -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")
index 0b1d03cad2dee08d297a5493eadec368b552faf3..16eae65aa6a61847c7846094bb87bce7866917aa 100644 (file)
@@ -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):
index bde356bc663886ff55bdc9753f5d107ac2d2f3db..94d917de41e574c4bbda29a549309aba751d66ef 100644 (file)
@@ -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'
index 7e6dc59f96d82ea8ec41ca7030569dca4b52487b..bf2eb2b98de66f21ec2c7478d3a1dff40f7a2a10 100644 (file)
@@ -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):