| 1 |
# |
| 2 |
# Copyright (C) 2007-2011 Edgewall Software, 2013-2025 the Babel team |
| 3 |
# All rights reserved. |
| 4 |
# |
| 5 |
# This software is licensed as described in the file LICENSE, which |
| 6 |
# you should have received as part of this distribution. The terms |
| 7 |
# are also available at https://github.com/python-babel/babel/blob/master/LICENSE. |
| 8 |
# |
| 9 |
# This software consists of voluntary contributions made by many |
| 10 |
# individuals. For the exact contribution history, see the revision |
| 11 |
# history and logs, available at https://github.com/python-babel/babel/commits/master/. |
| 12 |
import decimal |
| 13 |
|
| 14 |
import pytest |
| 15 |
|
| 16 |
from babel import localedata, plural |
| 17 |
|
| 18 |
EPSILON = decimal.Decimal("0.0001") |
| 19 |
|
| 20 |
|
| 21 |
def test_plural_rule(): |
| 22 |
rule = plural.PluralRule({'one': 'n is 1'}) |
| 23 |
assert rule(1) == 'one' |
| 24 |
assert rule(2) == 'other' |
| 25 |
|
| 26 |
rule = plural.PluralRule({'one': 'n is 1'}) |
| 27 |
assert rule.rules == {'one': 'n is 1'} |
| 28 |
|
| 29 |
|
| 30 |
def test_plural_rule_operands_i(): |
| 31 |
rule = plural.PluralRule({'one': 'i is 1'}) |
| 32 |
assert rule(1.2) == 'one' |
| 33 |
assert rule(2) == 'other' |
| 34 |
|
| 35 |
|
| 36 |
def test_plural_rule_operands_v(): |
| 37 |
rule = plural.PluralRule({'one': 'v is 2'}) |
| 38 |
assert rule(decimal.Decimal('1.20')) == 'one' |
| 39 |
assert rule(decimal.Decimal('1.2')) == 'other' |
| 40 |
assert rule(2) == 'other' |
| 41 |
|
| 42 |
|
| 43 |
def test_plural_rule_operands_w(): |
| 44 |
rule = plural.PluralRule({'one': 'w is 2'}) |
| 45 |
assert rule(decimal.Decimal('1.23')) == 'one' |
| 46 |
assert rule(decimal.Decimal('1.20')) == 'other' |
| 47 |
assert rule(1.2) == 'other' |
| 48 |
|
| 49 |
|
| 50 |
def test_plural_rule_operands_f(): |
| 51 |
rule = plural.PluralRule({'one': 'f is 20'}) |
| 52 |
assert rule(decimal.Decimal('1.23')) == 'other' |
| 53 |
assert rule(decimal.Decimal('1.20')) == 'one' |
| 54 |
assert rule(1.2) == 'other' |
| 55 |
|
| 56 |
|
| 57 |
def test_plural_rule_operands_t(): |
| 58 |
rule = plural.PluralRule({'one': 't = 5'}) |
| 59 |
assert rule(decimal.Decimal('1.53')) == 'other' |
| 60 |
assert rule(decimal.Decimal('1.50')) == 'one' |
| 61 |
assert rule(1.5) == 'one' |
| 62 |
|
| 63 |
|
| 64 |
def test_plural_other_is_ignored(): |
| 65 |
rule = plural.PluralRule({'one': 'n is 1', 'other': '@integer 2'}) |
| 66 |
assert rule(1) == 'one' |
| 67 |
|
| 68 |
|
| 69 |
def test_to_javascript(): |
| 70 |
assert (plural.to_javascript({'one': 'n is 1'}) |
| 71 |
== "(function(n) { return (n == 1) ? 'one' : 'other'; })") |
| 72 |
|
| 73 |
|
| 74 |
def test_to_python(): |
| 75 |
func = plural.to_python({'one': 'n is 1', 'few': 'n in 2..4'}) |
| 76 |
assert func(1) == 'one' |
| 77 |
assert func(3) == 'few' |
| 78 |
|
| 79 |
func = plural.to_python({'one': 'n in 1,11', 'few': 'n in 3..10,13..19'}) |
| 80 |
assert func(11) == 'one' |
| 81 |
assert func(15) == 'few' |
| 82 |
|
| 83 |
|
| 84 |
def test_to_gettext(): |
| 85 |
assert (plural.to_gettext({'one': 'n is 1', 'two': 'n is 2'}) |
| 86 |
== 'nplurals=3; plural=((n == 1) ? 0 : (n == 2) ? 1 : 2);') |
| 87 |
|
| 88 |
|
| 89 |
def test_in_range_list(): |
| 90 |
assert plural.in_range_list(1, [(1, 3)]) |
| 91 |
assert plural.in_range_list(3, [(1, 3)]) |
| 92 |
assert plural.in_range_list(3, [(1, 3), (5, 8)]) |
| 93 |
assert not plural.in_range_list(1.2, [(1, 4)]) |
| 94 |
assert not plural.in_range_list(10, [(1, 4)]) |
| 95 |
assert not plural.in_range_list(10, [(1, 4), (6, 8)]) |
| 96 |
|
| 97 |
|
| 98 |
def test_within_range_list(): |
| 99 |
assert plural.within_range_list(1, [(1, 3)]) |
| 100 |
assert plural.within_range_list(1.0, [(1, 3)]) |
| 101 |
assert plural.within_range_list(1.2, [(1, 4)]) |
| 102 |
assert plural.within_range_list(8.8, [(1, 4), (7, 15)]) |
| 103 |
assert not plural.within_range_list(10, [(1, 4)]) |
| 104 |
assert not plural.within_range_list(10.5, [(1, 4), (20, 30)]) |
| 105 |
|
| 106 |
|
| 107 |
def test_cldr_modulo(): |
| 108 |
assert plural.cldr_modulo(-3, 5) == -3 |
| 109 |
assert plural.cldr_modulo(-3, -5) == -3 |
| 110 |
assert plural.cldr_modulo(3, 5) == 3 |
| 111 |
|
| 112 |
|
| 113 |
def test_plural_within_rules(): |
| 114 |
p = plural.PluralRule({'one': 'n is 1', 'few': 'n within 2,4,7..9'}) |
| 115 |
assert repr(p) == "<PluralRule 'one: n is 1, few: n within 2,4,7..9'>" |
| 116 |
assert plural.to_javascript(p) == ( |
| 117 |
"(function(n) { " |
| 118 |
"return ((n == 2) || (n == 4) || (n >= 7 && n <= 9))" |
| 119 |
" ? 'few' : (n == 1) ? 'one' : 'other'; })") |
| 120 |
assert plural.to_gettext(p) == ( |
| 121 |
'nplurals=3; plural=(((n == 2) || (n == 4) || (n >= 7 && n <= 9))' |
| 122 |
' ? 1 : (n == 1) ? 0 : 2);') |
| 123 |
assert p(0) == 'other' |
| 124 |
assert p(1) == 'one' |
| 125 |
assert p(2) == 'few' |
| 126 |
assert p(3) == 'other' |
| 127 |
assert p(4) == 'few' |
| 128 |
assert p(5) == 'other' |
| 129 |
assert p(6) == 'other' |
| 130 |
assert p(7) == 'few' |
| 131 |
assert p(8) == 'few' |
| 132 |
assert p(9) == 'few' |
| 133 |
|
| 134 |
|
| 135 |
def test_locales_with_no_plural_rules_have_default(): |
| 136 |
from babel import Locale |
| 137 |
pf = Locale.parse('ii').plural_form |
| 138 |
assert pf(1) == 'other' |
| 139 |
assert pf(2) == 'other' |
| 140 |
assert pf(15) == 'other' |
| 141 |
|
| 142 |
|
| 143 |
WELL_FORMED_TOKEN_TESTS = ( |
| 144 |
("", []), |
| 145 |
( |
| 146 |
"n = 1", |
| 147 |
[ |
| 148 |
("value", "1"), |
| 149 |
("symbol", "="), |
| 150 |
("word", "n"), |
| 151 |
], |
| 152 |
), |
| 153 |
( |
| 154 |
"n = 1 @integer 1", |
| 155 |
[ |
| 156 |
("value", "1"), |
| 157 |
("symbol", "="), |
| 158 |
("word", "n"), |
| 159 |
], |
| 160 |
), |
| 161 |
( |
| 162 |
"n is 1", |
| 163 |
[ |
| 164 |
("value", "1"), |
| 165 |
("word", "is"), |
| 166 |
("word", "n"), |
| 167 |
], |
| 168 |
), |
| 169 |
( |
| 170 |
"n % 100 = 3..10", |
| 171 |
[ |
| 172 |
("value", "10"), |
| 173 |
("ellipsis", ".."), |
| 174 |
("value", "3"), |
| 175 |
("symbol", "="), |
| 176 |
("value", "100"), |
| 177 |
("symbol", "%"), |
| 178 |
("word", "n"), |
| 179 |
], |
| 180 |
), |
| 181 |
) |
| 182 |
|
| 183 |
|
| 184 |
@pytest.mark.parametrize('rule_text,tokens', WELL_FORMED_TOKEN_TESTS) |
| 185 |
def test_tokenize_well_formed(rule_text, tokens): |
| 186 |
assert plural.tokenize_rule(rule_text) == tokens |
| 187 |
|
| 188 |
|
| 189 |
MALFORMED_TOKEN_TESTS = ( |
| 190 |
'a = 1', 'n ! 2', |
| 191 |
) |
| 192 |
|
| 193 |
|
| 194 |
@pytest.mark.parametrize('rule_text', MALFORMED_TOKEN_TESTS) |
| 195 |
def test_tokenize_malformed(rule_text): |
| 196 |
with pytest.raises(plural.RuleError): |
| 197 |
plural.tokenize_rule(rule_text) |
| 198 |
|
| 199 |
|
| 200 |
def test_next_token_empty(): |
| 201 |
assert not plural.test_next_token([], '') |
| 202 |
|
| 203 |
|
| 204 |
def test_next_token_type_ok_and_no_value(): |
| 205 |
assert plural.test_next_token([('word', 'and')], 'word') |
| 206 |
|
| 207 |
|
| 208 |
def test_next_token_type_ok_and_not_value(): |
| 209 |
assert not plural.test_next_token([('word', 'and')], 'word', 'or') |
| 210 |
|
| 211 |
|
| 212 |
def test_next_token_type_ok_and_value_ok(): |
| 213 |
assert plural.test_next_token([('word', 'and')], 'word', 'and') |
| 214 |
|
| 215 |
|
| 216 |
def test_next_token_type_not_ok_and_value_ok(): |
| 217 |
assert not plural.test_next_token([('abc', 'and')], 'word', 'and') |
| 218 |
|
| 219 |
|
| 220 |
EXTRACT_OPERANDS_TESTS = ( |
| 221 |
(1, 1, 1, 0, 0, 0, 0), |
| 222 |
(decimal.Decimal('1.0'), '1.0', 1, 1, 0, 0, 0), |
| 223 |
(decimal.Decimal('1.00'), '1.00', 1, 2, 0, 0, 0), |
| 224 |
(decimal.Decimal('1.3'), '1.3', 1, 1, 1, 3, 3), |
| 225 |
(decimal.Decimal('1.30'), '1.30', 1, 2, 1, 30, 3), |
| 226 |
(decimal.Decimal('1.03'), '1.03', 1, 2, 2, 3, 3), |
| 227 |
(decimal.Decimal('1.230'), '1.230', 1, 3, 2, 230, 23), |
| 228 |
(-1, 1, 1, 0, 0, 0, 0), |
| 229 |
(1.3, '1.3', 1, 1, 1, 3, 3), |
| 230 |
) |
| 231 |
|
| 232 |
|
| 233 |
@pytest.mark.parametrize('source,n,i,v,w,f,t', EXTRACT_OPERANDS_TESTS) |
| 234 |
def test_extract_operands(source, n, i, v, w, f, t): |
| 235 |
e_n, e_i, e_v, e_w, e_f, e_t, e_c, e_e = plural.extract_operands(source) |
| 236 |
assert abs(e_n - decimal.Decimal(n)) <= EPSILON # float-decimal conversion inaccuracy |
| 237 |
assert e_i == i |
| 238 |
assert e_v == v |
| 239 |
assert e_w == w |
| 240 |
assert e_f == f |
| 241 |
assert e_t == t |
| 242 |
assert not e_c # Not supported at present |
| 243 |
assert not e_e # Not supported at present |
| 244 |
|
| 245 |
|
| 246 |
@pytest.mark.parametrize('locale', ('ru', 'pl')) |
| 247 |
def test_gettext_compilation(locale): |
| 248 |
# Test that new plural form elements introduced in recent CLDR versions |
| 249 |
# are compiled "down" to `n` when emitting Gettext rules. |
| 250 |
ru_rules = localedata.load(locale)['plural_form'].rules |
| 251 |
chars = 'ivwft' |
| 252 |
# Test that these rules are valid for this test; i.e. that they contain at least one |
| 253 |
# of the gettext-unsupported characters. |
| 254 |
assert any(f" {ch} " in rule for ch in chars for rule in ru_rules.values()) |
| 255 |
# Then test that the generated value indeed does not contain these. |
| 256 |
ru_rules_gettext = plural.to_gettext(ru_rules) |
| 257 |
assert not any(ch in ru_rules_gettext for ch in chars) |