First commit of the project
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
"""Tests for ``Ki18n.p`` (plural forms)."""
|
||||
|
||||
import pytest
|
||||
|
||||
from support import DE, EN
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Category selection
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("count", "expected"),
|
||||
[
|
||||
(0, "No files"),
|
||||
(1, "One file"),
|
||||
(2, "2 files"),
|
||||
(3, "3 files"),
|
||||
(5, "5 files"),
|
||||
(100, "100 files"),
|
||||
(10**9, "1000000000 files"),
|
||||
(None, "Count missing"),
|
||||
(0.0, "No files"),
|
||||
(1.0, "One file"),
|
||||
(2.5, "2.5 files"),
|
||||
],
|
||||
)
|
||||
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):
|
||||
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)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("count", "expected"),
|
||||
[(0, "Keine Dateien"), (1, "Eine Datei"), (4, "4 Dateien")],
|
||||
)
|
||||
def test_uses_active_language(manager, count, expected):
|
||||
manager.set_language("de")
|
||||
assert manager.p("files.found", count) == expected
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Placeholders
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def placeholders(env):
|
||||
env.install("en", {
|
||||
"language": "English",
|
||||
"braces": {"one": "{count} item", "many": "{count} items"},
|
||||
"mixed": {"one": "#count file ({count})", "many": "#count files ({count})"},
|
||||
"folder": {"one": "#count file in {folder}", "many": "#count files in {folder}"},
|
||||
"twice": {"one": "x", "many": "#count of #count"},
|
||||
"plain": "Total: {count}",
|
||||
})
|
||||
return env.make()
|
||||
|
||||
|
||||
def test_brace_count_placeholder(placeholders):
|
||||
assert placeholders.p("braces", 1) == "1 item"
|
||||
assert placeholders.p("braces", 7) == "7 items"
|
||||
|
||||
|
||||
def test_hash_and_brace_count_can_be_combined(placeholders):
|
||||
assert placeholders.p("mixed", 3) == "3 files (3)"
|
||||
|
||||
|
||||
def test_additional_keyword_placeholders(placeholders):
|
||||
assert placeholders.p("folder", 3, folder="docs") == "3 files in docs"
|
||||
assert placeholders.p("folder", 1, folder="docs") == "1 file in docs"
|
||||
|
||||
|
||||
def test_missing_keyword_placeholder_returns_text_with_count_replaced(placeholders):
|
||||
assert placeholders.p("folder", 3) == "3 files in {folder}"
|
||||
|
||||
|
||||
def test_every_hash_count_occurrence_is_replaced(placeholders):
|
||||
assert placeholders.p("twice", 4) == "4 of 4"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("count", "expected"),
|
||||
[(0, "Total: 0"), (1, "Total: 1"), (9, "Total: 9"), (None, "Total: None")],
|
||||
)
|
||||
def test_plain_string_is_used_for_every_category(placeholders, count, expected):
|
||||
assert placeholders.p("plain", count) == expected
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Fallbacks
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fallback(env):
|
||||
env.install("en", {**EN, "english.plain": "English plain {count}"})
|
||||
env.install("de", DE)
|
||||
env.install("fr", {"language": "Français"})
|
||||
return env.make()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("count", "expected"),
|
||||
[(0, "No files"), (1, "One file"), (3, "3 files"), (None, "Count missing")],
|
||||
)
|
||||
def test_falls_back_to_english_categories(fallback, count, expected):
|
||||
fallback.set_language("fr")
|
||||
assert fallback.p("files.found", count) == expected
|
||||
|
||||
|
||||
def test_english_plain_string_is_used_as_fallback(fallback):
|
||||
fallback.set_language("fr")
|
||||
assert fallback.p("english.plain", 2) == "English plain 2"
|
||||
|
||||
|
||||
def test_english_wins_over_default(fallback):
|
||||
fallback.set_language("fr")
|
||||
assert fallback.p("files.found", 3, default="Fallback") == "3 files"
|
||||
|
||||
|
||||
def test_current_language_wins_over_english(fallback):
|
||||
fallback.set_language("de")
|
||||
assert fallback.p("files.found", 3, default="Fallback") == "3 Dateien"
|
||||
|
||||
|
||||
def test_default_is_used_for_unknown_key(fallback):
|
||||
assert fallback.p("unknown", 3, default="Fallback") == "Fallback"
|
||||
|
||||
|
||||
def test_default_supports_count_placeholders(fallback):
|
||||
assert fallback.p("unknown", 3, default="{count} things") == "3 things"
|
||||
assert fallback.p("unknown", 2, default="#count things") == "2 things"
|
||||
|
||||
|
||||
def test_default_supports_additional_keywords(fallback):
|
||||
assert fallback.p("unknown", 3, default="{count} in {where}", where="here") == "3 in here"
|
||||
|
||||
|
||||
def test_key_is_used_when_nothing_else_is_available(fallback):
|
||||
assert fallback.p("unknown", 3) == "unknown"
|
||||
|
||||
|
||||
def test_without_english_resources_unknown_key_falls_back_to_default_or_key(env):
|
||||
env.install("de", DE)
|
||||
manager = env.make("de")
|
||||
assert manager.p("files.found", 1) == "Eine Datei"
|
||||
assert manager.p("unknown", 1) == "unknown"
|
||||
assert manager.p("unknown", 1, default="Vorgabe") == "Vorgabe"
|
||||
Reference in New Issue
Block a user