Debian Codegraph @

tests/benchmarks/benchmark_plural.py

raw
1 from __future__ import annotations
2
3 import pytest
4
5 from babel import Locale
6 from babel.plural import PluralRule, to_gettext
7
8 # A synthetic ruleset, comparable in complexity to real CLDR rule sets.
9 RULES = {
10 "one": "v in 0 and i mod 10 in 1..2 and i mod 100 not in 11..12",
11 "few": "v in 0 and i mod 10 in 3..6",
12 "many": "v in 0 and i mod 10 in 7..9 or v in 0 and i mod 100 in 11..12",
13 }
14
15
16 @pytest.fixture
17 def plural_rule() -> PluralRule:
18 return PluralRule(RULES)
19
20
21 def test_plural_rule_parse(benchmark):
22 assert benchmark(lambda: PluralRule(RULES).tags) == frozenset({"one", "few", "many"})
23
24
25 def test_plural_rule_call_int(benchmark, plural_rule: PluralRule):
26 assert benchmark(lambda: plural_rule(21)) == "one"
27
28
29 def test_plural_rule_call_float(benchmark, plural_rule):
30 # Floats go through the Decimal path in extract_operands.
31 assert benchmark(lambda: plural_rule(2.5)) == "other"
32
33
34 def test_locale_plural_form(benchmark):
35 locale = Locale.parse("fi")
36 assert benchmark(lambda: locale.plural_form(1)) == "one"
37
38
39 def test_to_gettext(benchmark):
40 expected = to_gettext(RULES)
41 assert expected.startswith("nplurals=4; plural=(")
42 assert benchmark(lambda: to_gettext(RULES)) == expected