Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions tests/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,3 +470,24 @@ def test_intword_unit_has_no_format_placeholder(locale: str) -> None:
pytest.skip("Generate .mo with scripts/generate-translation-binaries.sh")
finally:
humanize.i18n.deactivate()


def test_activate_populates_translation_cache(monkeypatch: pytest.MonkeyPatch) -> None:
"""activate() caches translation on first call (cache-miss branch)."""
import gettext

fake = gettext.NullTranslations()
monkeypatch.setattr(
humanize.i18n.gettext_module,
"translation",
lambda *args, **kwargs: fake,
)
humanize.i18n._TRANSLATIONS.pop("de_DE", None)
try:
result = humanize.i18n.activate("de_DE")
assert result is fake
assert humanize.i18n._TRANSLATIONS["de_DE"] is fake
assert humanize.i18n._CURRENT.locale == "de_DE"
finally:
humanize.i18n._TRANSLATIONS.pop("de_DE", None)
humanize.i18n.deactivate()
22 changes: 22 additions & 0 deletions tests/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,25 @@ def test_clamp(test_args: list[typing.Any], expected: str) -> None:
)
def test_metric(test_args: list[typing.Any], expected: str) -> None:
assert humanize.metric(*test_args) == expected


def test_format_not_finite_returns_empty_for_finite() -> None:
"""_format_not_finite falls back to empty string for finite values."""
assert number._format_not_finite(42.0) == ""
assert number._format_not_finite(0) == ""
assert number._format_not_finite(-1.5) == ""


def test_intcomma_with_custom_separators(monkeypatch: pytest.MonkeyPatch) -> None:
"""intcomma uses thousands_separator/decimal_separator helpers."""
monkeypatch.setattr(number, "thousands_separator", lambda: ".")
monkeypatch.setattr(number, "decimal_separator", lambda: ",")
assert humanize.intcomma(1234) == "1.234"
assert humanize.intcomma(1234.5, ndigits=2) == "1.234,50"
assert humanize.intcomma("14308,40", ndigits=1) == "14.308,4"


def test_clamp_invalid_format_raises() -> None:
"""clamp raises ValueError for non-string, non-callable format."""
with pytest.raises(ValueError, match="Invalid format"):
humanize.clamp(42, format=123) # type: ignore[arg-type]
Loading