| 1 |
"""Tests for number formatting engine lookup.""" |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from hassil.numbers import get_rbnf_engine |
| 6 |
|
| 7 |
|
| 8 |
def _words(language: str, number: int) -> set: |
| 9 |
"""Return every spelled-out form of a number in a language.""" |
| 10 |
return set(get_rbnf_engine(language).format_number(number).text_by_ruleset.values()) |
| 11 |
|
| 12 |
|
| 13 |
def test_exact_language() -> None: |
| 14 |
"""Test a language that unicode-rbnf supports directly.""" |
| 15 |
assert "two" in _words("en", 2) |
| 16 |
|
| 17 |
|
| 18 |
def test_language_family_fallback() -> None: |
| 19 |
"""Test that a region falls back to the language family.""" |
| 20 |
assert _words("en-US", 2) == _words("en", 2) |
| 21 |
|
| 22 |
|
| 23 |
def test_separator_is_normalized() -> None: |
| 24 |
"""Test that "-" is accepted where unicode-rbnf uses "_".""" |
| 25 |
# Serbian written in Latin script, not the Cyrillic of the "sr" family. |
| 26 |
assert "dva" in _words("sr-Latn", 2) |
| 27 |
assert "два" not in _words("sr-Latn", 2) |
| 28 |
|
| 29 |
|
| 30 |
@pytest.mark.parametrize("language", ["zh-TW", "zh-HK", "zh-MO"]) |
| 31 |
def test_traditional_chinese_regions(language: str) -> None: |
| 32 |
"""Test that regions using Traditional Chinese do not get Simplified rules.""" |
| 33 |
words = _words(language, 2) |
| 34 |
|
| 35 |
# 兩 is how the quantity 2 is spoken; the Simplified 两 must not be used. |
| 36 |
assert "兩" in words |
| 37 |
assert "两" not in words |
| 38 |
|
| 39 |
# 二 is written the same in both scripts and stays available. |
| 40 |
assert "二" in words |
| 41 |
|
| 42 |
|
| 43 |
def test_simplified_chinese_is_unchanged() -> None: |
| 44 |
"""Test that Simplified regions still get the Simplified rules.""" |
| 45 |
words = _words("zh-CN", 2) |
| 46 |
assert "两" in words |
| 47 |
assert "兩" not in words |
| 48 |
|
| 49 |
|
| 50 |
def test_unsupported_language() -> None: |
| 51 |
"""Test that an unsupported language still raises.""" |
| 52 |
with pytest.raises(ValueError): |
| 53 |
get_rbnf_engine("nonexistent-language") |
| 54 |
|
| 55 |
|
| 56 |
def test_engine_is_cached() -> None: |
| 57 |
"""Test that repeated lookups return the same engine.""" |
| 58 |
assert get_rbnf_engine("zh-TW") is get_rbnf_engine("zh-TW") |