summaryrefslogtreecommitdiff
path: root/tests/test_plural.py
blob: 1a9fbe5076b75f5b414c1df11a48a3758031e3e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007-2011 Edgewall Software, 2013-2020 the Babel team
# All rights reserved.
#
# This software is licensed as described in the file LICENSE, which
# you should have received as part of this distribution. The terms
# are also available at http://babel.edgewall.org/wiki/License.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://babel.edgewall.org/log/.
import unittest
import pytest

from babel import plural, localedata
from babel._compat import decimal

EPSILON = decimal.Decimal("0.0001")


def test_plural_rule():
    rule = plural.PluralRule({'one': 'n is 1'})
    assert rule(1) == 'one'
    assert rule(2) == 'other'

    rule = plural.PluralRule({'one': 'n is 1'})
    assert rule.rules == {'one': 'n is 1'}


def test_plural_rule_operands_i():
    rule = plural.PluralRule({'one': 'i is 1'})
    assert rule(1.2) == 'one'
    assert rule(2) == 'other'


def test_plural_rule_operands_v():
    rule = plural.PluralRule({'one': 'v is 2'})
    assert rule(decimal.Decimal('1.20')) == 'one'
    assert rule(decimal.Decimal('1.2')) == 'other'
    assert rule(2) == 'other'


def test_plural_rule_operands_w():
    rule = plural.PluralRule({'one': 'w is 2'})
    assert rule(decimal.Decimal('1.23')) == 'one'
    assert rule(decimal.Decimal('1.20')) == 'other'
    assert rule(1.2) == 'other'


def test_plural_rule_operands_f():
    rule = plural.PluralRule({'one': 'f is 20'})
    assert rule(decimal.Decimal('1.23')) == 'other'
    assert rule(decimal.Decimal('1.20')) == 'one'
    assert rule(1.2) == 'other'


def test_plural_rule_operands_t():
    rule = plural.PluralRule({'one': 't = 5'})
    assert rule(decimal.Decimal('1.53')) == 'other'
    assert rule(decimal.Decimal('1.50')) == 'one'
    assert rule(1.5) == 'one'


def test_plural_other_is_ignored():
    rule = plural.PluralRule({'one': 'n is 1', 'other': '@integer 2'})
    assert rule(1) == 'one'


def test_to_javascript():
    assert (plural.to_javascript({'one': 'n is 1'})
            == "(function(n) { return (n == 1) ? 'one' : 'other'; })")


def test_to_python():
    func = plural.to_python({'one': 'n is 1', 'few': 'n in 2..4'})
    assert func(1) == 'one'
    assert func(3) == 'few'

    func = plural.to_python({'one': 'n in 1,11', 'few': 'n in 3..10,13..19'})
    assert func(11) == 'one'
    assert func(15) == 'few'


def test_to_gettext():
    assert (plural.to_gettext({'one': 'n is 1', 'two': 'n is 2'})
            == 'nplurals=3; plural=((n == 1) ? 0 : (n == 2) ? 1 : 2)')


def test_in_range_list():
    assert plural.in_range_list(1, [(1, 3)])
    assert plural.in_range_list(3, [(1, 3)])
    assert plural.in_range_list(3, [(1, 3), (5, 8)])
    assert not plural.in_range_list(1.2, [(1, 4)])
    assert not plural.in_range_list(10, [(1, 4)])
    assert not plural.in_range_list(10, [(1, 4), (6, 8)])


def test_within_range_list():
    assert plural.within_range_list(1, [(1, 3)])
    assert plural.within_range_list(1.0, [(1, 3)])
    assert plural.within_range_list(1.2, [(1, 4)])
    assert plural.within_range_list(8.8, [(1, 4), (7, 15)])
    assert not plural.within_range_list(10, [(1, 4)])
    assert not plural.within_range_list(10.5, [(1, 4), (20, 30)])


def test_cldr_modulo():
    assert plural.cldr_modulo(-3, 5) == -3
    assert plural.cldr_modulo(-3, -5) == -3
    assert plural.cldr_modulo(3, 5) == 3


def test_plural_within_rules():
    p = plural.PluralRule({'one': 'n is 1', 'few': 'n within 2,4,7..9'})
    assert repr(p) == "<PluralRule 'one: n is 1, few: n within 2,4,7..9'>"
    assert plural.to_javascript(p) == (
        "(function(n) { "
        "return ((n == 2) || (n == 4) || (n >= 7 && n <= 9))"
        " ? 'few' : (n == 1) ? 'one' : 'other'; })")
    assert plural.to_gettext(p) == (
        'nplurals=3; plural=(((n == 2) || (n == 4) || (n >= 7 && n <= 9))'
        ' ? 1 : (n == 1) ? 0 : 2)')
    assert p(0) == 'other'
    assert p(1) == 'one'
    assert p(2) == 'few'
    assert p(3) == 'other'
    assert p(4) == 'few'
    assert p(5) == 'other'
    assert p(6) == 'other'
    assert p(7) == 'few'
    assert p(8) == 'few'
    assert p(9) == 'few'


def test_locales_with_no_plural_rules_have_default():
    from babel import Locale
    pf = Locale.parse('ii').plural_form
    assert pf(1) == 'other'
    assert pf(2) == 'other'
    assert pf(15) == 'other'


WELL_FORMED_TOKEN_TESTS = (
    ('', []),
    ('n = 1', [('value', '1'), ('symbol', '='), ('word', 'n'), ]),
    ('n = 1 @integer 1', [('value', '1'), ('symbol', '='), ('word', 'n'), ]),
    ('n is 1', [('value', '1'), ('word', 'is'), ('word', 'n'), ]),
    ('n % 100 = 3..10', [('value', '10'), ('ellipsis', '..'), ('value', '3'),
                         ('symbol', '='), ('value', '100'), ('symbol', '%'),
                         ('word', 'n'), ]),
)


@pytest.mark.parametrize('rule_text,tokens', WELL_FORMED_TOKEN_TESTS)
def test_tokenize_well_formed(rule_text, tokens):
    assert plural.tokenize_rule(rule_text) == tokens


MALFORMED_TOKEN_TESTS = (
    'a = 1', 'n ! 2',
)


@pytest.mark.parametrize('rule_text', MALFORMED_TOKEN_TESTS)
def test_tokenize_malformed(rule_text):
    with pytest.raises(plural.RuleError):
        plural.tokenize_rule(rule_text)


class TestNextTokenTestCase(unittest.TestCase):

    def test_empty(self):
        assert not plural.test_next_token([], '')

    def test_type_ok_and_no_value(self):
        assert plural.test_next_token([('word', 'and')], 'word')

    def test_type_ok_and_not_value(self):
        assert not plural.test_next_token([('word', 'and')], 'word', 'or')

    def test_type_ok_and_value_ok(self):
        assert plural.test_next_token([('word', 'and')], 'word', 'and')

    def test_type_not_ok_and_value_ok(self):
        assert not plural.test_next_token([('abc', 'and')], 'word', 'and')


def make_range_list(*values):
    ranges = []
    for v in values:
        if isinstance(v, int):
            val_node = plural.value_node(v)
            ranges.append((val_node, val_node))
        else:
            assert isinstance(v, tuple)
            ranges.append((plural.value_node(v[0]),
                           plural.value_node(v[1])))
    return plural.range_list_node(ranges)


class PluralRuleParserTestCase(unittest.TestCase):

    def setUp(self):
        self.n = plural.ident_node('n')

    def n_eq(self, v):
        return 'relation', ('in', self.n, make_range_list(v))

    def test_error_when_unexpected_end(self):
        with pytest.raises(plural.RuleError):
            plural._Parser('n =')

    def test_eq_relation(self):
        assert plural._Parser('n = 1').ast == self.n_eq(1)

    def test_in_range_relation(self):
        assert plural._Parser('n = 2..4').ast == \
            ('relation', ('in', self.n, make_range_list((2, 4))))

    def test_negate(self):
        assert plural._Parser('n != 1').ast == plural.negate(self.n_eq(1))

    def test_or(self):
        assert plural._Parser('n = 1 or n = 2').ast ==\
            ('or', (self.n_eq(1), self.n_eq(2)))

    def test_and(self):
        assert plural._Parser('n = 1 and n = 2').ast ==\
            ('and', (self.n_eq(1), self.n_eq(2)))

    def test_or_and(self):
        assert plural._Parser('n = 0 or n != 1 and n % 100 = 1..19').ast == \
            ('or', (self.n_eq(0),
                    ('and', (plural.negate(self.n_eq(1)),
                             ('relation', ('in',
                                           ('mod', (self.n,
                                                    plural.value_node(100))),
                                           (make_range_list((1, 19)))))))
                    ))


EXTRACT_OPERANDS_TESTS = (
    (1, 1, 1, 0, 0, 0, 0),
    (decimal.Decimal('1.0'), '1.0', 1, 1, 0, 0, 0),
    (decimal.Decimal('1.00'), '1.00', 1, 2, 0, 0, 0),
    (decimal.Decimal('1.3'), '1.3', 1, 1, 1, 3, 3),
    (decimal.Decimal('1.30'), '1.30', 1, 2, 1, 30, 3),
    (decimal.Decimal('1.03'), '1.03', 1, 2, 2, 3, 3),
    (decimal.Decimal('1.230'), '1.230', 1, 3, 2, 230, 23),
    (-1, 1, 1, 0, 0, 0, 0),
    (1.3, '1.3', 1, 1, 1, 3, 3),
)


@pytest.mark.parametrize('source,n,i,v,w,f,t', EXTRACT_OPERANDS_TESTS)
def test_extract_operands(source, n, i, v, w, f, t):
    e_n, e_i, e_v, e_w, e_f, e_t = plural.extract_operands(source)
    assert abs(e_n - decimal.Decimal(n)) <= EPSILON  # float-decimal conversion inaccuracy
    assert e_i == i
    assert e_v == v
    assert e_w == w
    assert e_f == f
    assert e_t == t


@pytest.mark.parametrize('locale', ('ru', 'pl'))
def test_gettext_compilation(locale):
    # Test that new plural form elements introduced in recent CLDR versions
    # are compiled "down" to `n` when emitting Gettext rules.
    ru_rules = localedata.load(locale)['plural_form'].rules
    chars = 'ivwft'
    # Test that these rules are valid for this test; i.e. that they contain at least one
    # of the gettext-unsupported characters.
    assert any((" " + ch + " ") in rule for ch in chars for rule in ru_rules.values())
    # Then test that the generated value indeed does not contain these.
    ru_rules_gettext = plural.to_gettext(ru_rules)
    assert not any(ch in ru_rules_gettext for ch in chars)