Handle negative and float values in pluralize method
This commit is contained in:
+78
-10
@@ -30,17 +30,51 @@ def test_category_selection(manager, count, expected):
|
||||
assert manager.p("files.found", count) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize("count", [-1, -100, -0.5])
|
||||
def test_negative_counts_raise_value_error(manager, count):
|
||||
with pytest.raises(ValueError):
|
||||
@pytest.mark.parametrize(
|
||||
("count", "expected"),
|
||||
[
|
||||
(-1, "One file"),
|
||||
(-1.0, "One file"),
|
||||
(-0.0, "No files"),
|
||||
(-2, "-2 files"),
|
||||
(-100, "-100 files"),
|
||||
(-0.5, "-0.5 files"),
|
||||
(-1.5, "-1.5 files"),
|
||||
(0.5, "0.5 files"),
|
||||
(1.5, "1.5 files"),
|
||||
(0.999, "0.999 files"),
|
||||
(1.001, "1.001 files"),
|
||||
(1e-9, "1e-09 files"),
|
||||
(10**9 + 0.5, "1000000000.5 files"),
|
||||
],
|
||||
)
|
||||
def test_negative_and_fractional_counts(manager, count, expected):
|
||||
assert manager.p("files.found", count) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize("count", [float("inf"), float("-inf"), float("nan")])
|
||||
def test_non_finite_counts_use_many(manager, count):
|
||||
assert manager.p("files.found", count) == f"{count} files"
|
||||
|
||||
|
||||
def test_negative_count_does_not_raise_for_unknown_key(manager):
|
||||
assert manager.p("unknown.key", -1) == "unknown.key"
|
||||
assert manager.p("unknown.key", -2.5, default="{count} things") == "-2.5 things"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("count", [-1, -2.5, 0.5, 1.5])
|
||||
def test_negative_and_fractional_counts_use_active_language(manager, count):
|
||||
manager.set_language("de")
|
||||
expected = "Eine Datei" if abs(count) == 1 else f"{count} Dateien"
|
||||
assert manager.p("files.found", count) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize("count", ["3", "many", [1]])
|
||||
def test_non_numeric_count_raises_type_error(manager, count):
|
||||
with pytest.raises(TypeError):
|
||||
manager.p("files.found", count)
|
||||
|
||||
|
||||
def test_negative_count_raises_even_for_unknown_key(manager):
|
||||
with pytest.raises(ValueError):
|
||||
manager.p("unknown.key", -1)
|
||||
|
||||
|
||||
def test_returns_string(manager):
|
||||
assert isinstance(manager.p("files.found", 3), str)
|
||||
|
||||
@@ -94,9 +128,35 @@ def test_every_hash_count_occurrence_is_replaced(placeholders):
|
||||
assert placeholders.p("twice", 4) == "4 of 4"
|
||||
|
||||
|
||||
def test_hash_count_inserts_negative_and_fractional_counts(placeholders):
|
||||
assert placeholders.p("twice", -4) == "-4 of -4"
|
||||
assert placeholders.p("twice", 2.5) == "2.5 of 2.5"
|
||||
assert placeholders.p("mixed", -3) == "-3 files (-3)"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("count", "expected"),
|
||||
[(0, "Total: 0"), (1, "Total: 1"), (9, "Total: 9"), (None, "Total: None")],
|
||||
[(2.5, "2.50 files"), (-2, "-2.00 files"), (-1, "-1.00 file"), (0.125, "0.12 files")],
|
||||
)
|
||||
def test_count_placeholder_supports_format_specification(env, count, expected):
|
||||
env.install("en", {
|
||||
"language": "English",
|
||||
"measured": {"one": "{count:.2f} file", "many": "{count:.2f} files"},
|
||||
})
|
||||
assert env.make().p("measured", count) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("count", "expected"),
|
||||
[
|
||||
(0, "Total: 0"),
|
||||
(1, "Total: 1"),
|
||||
(9, "Total: 9"),
|
||||
(-1, "Total: -1"),
|
||||
(-2.5, "Total: -2.5"),
|
||||
(0.5, "Total: 0.5"),
|
||||
(None, "Total: None"),
|
||||
],
|
||||
)
|
||||
def test_plain_string_is_used_for_every_category(placeholders, count, expected):
|
||||
assert placeholders.p("plain", count) == expected
|
||||
@@ -117,7 +177,15 @@ def fallback(env):
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("count", "expected"),
|
||||
[(0, "No files"), (1, "One file"), (3, "3 files"), (None, "Count missing")],
|
||||
[
|
||||
(0, "No files"),
|
||||
(1, "One file"),
|
||||
(-1, "One file"),
|
||||
(3, "3 files"),
|
||||
(-3, "-3 files"),
|
||||
(0.5, "0.5 files"),
|
||||
(None, "Count missing"),
|
||||
],
|
||||
)
|
||||
def test_falls_back_to_english_categories(fallback, count, expected):
|
||||
fallback.set_language("fr")
|
||||
|
||||
Reference in New Issue
Block a user