Debian Codegraph @

tests/test_plural_rule_parser.py

raw
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
13 import pytest
14
15 from babel import plural
16
17 N_NODE = plural.ident_node('n')
18
19
20 def make_range_list(*values):
21 ranges = []
22 for v in values:
23 if isinstance(v, int):
24 val_node = plural.value_node(v)
25 ranges.append((val_node, val_node))
26 else:
27 assert isinstance(v, tuple)
28 ranges.append((plural.value_node(v[0]), plural.value_node(v[1])))
29 return plural.range_list_node(ranges)
30
31
32 def n_eq(v):
33 return 'relation', ('in', N_NODE, make_range_list(v))
34
35
36 def test_error_when_unexpected_end():
37 with pytest.raises(plural.RuleError):
38 plural._Parser('n =')
39
40
41 def test_eq_relation():
42 assert plural._Parser('n = 1').ast == n_eq(1)
43
44
45 def test_in_range_relation():
46 assert plural._Parser('n = 2..4').ast == (
47 'relation',
48 ('in', N_NODE, make_range_list((2, 4))),
49 )
50
51
52 def test_negate():
53 assert plural._Parser('n != 1').ast == plural.negate(n_eq(1))
54
55
56 def test_or():
57 assert plural._Parser('n = 1 or n = 2').ast == ('or', (n_eq(1), n_eq(2)))
58
59
60 def test_and():
61 assert plural._Parser('n = 1 and n = 2').ast == ('and', (n_eq(1), n_eq(2)))
62
63
64 def test_or_and():
65 assert plural._Parser('n = 0 or n != 1 and n % 100 = 1..19').ast == (
66 'or',
67 (
68 n_eq(0),
69 (
70 'and',
71 (
72 plural.negate(n_eq(1)),
73 (
74 'relation',
75 (
76 'in',
77 ('mod', (N_NODE, plural.value_node(100))),
78 (make_range_list((1, 19))),
79 ),
80 ),
81 ),
82 ),
83 ),
84 )