Plural rules: updates for UTS #35 Rev 33
[lhc/web/wiklou.git] / tests / phpunit / languages / utils / CLDRPluralRuleEvaluatorTest.php
1 <?php
2 /**
3 * @author Niklas Laxström
4 * @file
5 */
6
7 /**
8 * @covers CLDRPluralRuleEvaluator
9 */
10 class CLDRPluralRuleEvaluatorTest extends MediaWikiTestCase {
11 /**
12 * @dataProvider validTestCases
13 */
14 function testValidRules( $expected, $rules, $number, $comment ) {
15 $result = CLDRPluralRuleEvaluator::evaluate( $number, (array)$rules );
16 $this->assertEquals( $expected, $result, $comment );
17 }
18
19 /**
20 * @dataProvider invalidTestCases
21 * @expectedException CLDRPluralRuleError
22 */
23 function testInvalidRules( $rules, $comment ) {
24 CLDRPluralRuleEvaluator::evaluate( 1, (array)$rules );
25 }
26
27 function validTestCases() {
28 $tests = array(
29 # expected, rule, number, comment
30 array( 0, 'n is 1', 1, 'integer number and is' ),
31 array( 0, 'n is 1', "1", 'string integer number and is' ),
32 array( 0, 'n is 1', 1.0, 'float number and is' ),
33 array( 0, 'n is 1', "1.0", 'string float number and is' ),
34 array( 1, 'n is 1', 1.1, 'float number and is' ),
35 array( 1, 'n is 1', 2, 'float number and is' ),
36
37 array( 0, 'n in 1,3,5', 3, '' ),
38 array( 1, 'n not in 1,3,5', 5, '' ),
39
40 array( 1, 'n in 1,3,5', 2, '' ),
41 array( 0, 'n not in 1,3,5', 4, '' ),
42
43 array( 0, 'n in 1..3', 2, '' ),
44 array( 0, 'n in 1..3', 3, 'in is inclusive' ),
45 array( 1, 'n in 1..3', 0, '' ),
46
47 array( 1, 'n not in 1..3', 2, '' ),
48 array( 1, 'n not in 1..3', 3, 'in is inclusive' ),
49 array( 0, 'n not in 1..3', 0, '' ),
50
51 array( 1, 'n is not 1 and n is not 2 and n is not 3', 1, 'and relation' ),
52 array( 0, 'n is not 1 and n is not 2 and n is not 4', 3, 'and relation' ),
53
54 array( 0, 'n is not 1 or n is 1', 1, 'or relation' ),
55 array( 1, 'n is 1 or n is 2', 3, 'or relation' ),
56
57 array( 0, 'n is 1', 1, 'extra whitespace' ),
58
59 array( 0, 'n mod 3 is 1', 7, 'mod' ),
60 array( 0, 'n mod 3 is not 1', 4.3, 'mod with floats' ),
61
62 array( 0, 'n within 1..3', 2, 'within with integer' ),
63 array( 0, 'n within 1..3', 2.5, 'within with float' ),
64 array( 0, 'n in 1..3', 2, 'in with integer' ),
65 array( 1, 'n in 1..3', 2.5, 'in with float' ),
66
67 array( 0, 'n in 3 or n is 4 and n is 5', 3, 'and binds more tightly than or' ),
68 array( 1, 'n is 3 or n is 4 and n is 5', 4, 'and binds more tightly than or' ),
69
70 array( 0, 'n mod 10 in 3..4,9 and n mod 100 not in 10..19,70..79,90..99', 24, 'breton rule' ),
71 array( 1, 'n mod 10 in 3..4,9 and n mod 100 not in 10..19,70..79,90..99', 25, 'breton rule' ),
72
73 array( 0, 'n within 0..2 and n is not 2', 0, 'french rule' ),
74 array( 0, 'n within 0..2 and n is not 2', 1, 'french rule' ),
75 array( 0, 'n within 0..2 and n is not 2', 1.2, 'french rule' ),
76 array( 1, 'n within 0..2 and n is not 2', 2, 'french rule' ),
77
78 array( 1, 'n in 3..10,13..19', 2, 'scottish rule - ranges with comma' ),
79 array( 0, 'n in 3..10,13..19', 4, 'scottish rule - ranges with comma' ),
80 array( 1, 'n in 3..10,13..19', 12.999, 'scottish rule - ranges with comma' ),
81 array( 0, 'n in 3..10,13..19', 13, 'scottish rule - ranges with comma' ),
82
83 array( 0, '5 mod 3 is n', 2, 'n as result of mod - no need to pass' ),
84
85 # Revision 33 new operand examples
86 # expected, rule, number, comment
87 array( 0, 'i is 1', '1.00', 'new operand i' ),
88 array( 0, 'v is 2', '1.00', 'new operand v' ),
89 array( 0, 'w is 0', '1.00', 'new operand w' ),
90 array( 0, 'f is 0', '1.00', 'new operand f' ),
91 array( 0, 't is 0', '1.00', 'new operand t' ),
92
93 array( 0, 'i is 1', '1.30', 'new operand i' ),
94 array( 0, 'v is 2', '1.30', 'new operand v' ),
95 array( 0, 'w is 1', '1.30', 'new operand w' ),
96 array( 0, 'f is 30', '1.30', 'new operand f' ),
97 array( 0, 't is 3', '1.30', 'new operand t' ),
98
99 array( 0, 'i is 1', '1.03', 'new operand i' ),
100 array( 0, 'v is 2', '1.03', 'new operand v' ),
101 array( 0, 'w is 2', '1.03', 'new operand w' ),
102 array( 0, 'f is 3', '1.03', 'new operand f' ),
103 array( 0, 't is 3', '1.03', 'new operand t' ),
104
105 # Revision 33 new operator aliases
106 # expected, rule, number, comment
107 array( 0, 'n % 3 is 1', 7, 'new % operator' ),
108 array( 0, 'n = 1,3,5', 3, 'new = operator' ),
109 array( 1, 'n != 1,3,5', 5, 'new != operator' ),
110
111 # Revision 33 samples
112 # expected, rule, number, comment
113 array( 0, 'n in 1,3,5@integer 3~10, 103~110, 1003, … @decimal 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 103.0, 1003.0, …', 3, 'samples' ),
114
115 # Revision 33 some test cases from CLDR
116 array( 0, 'i = 1 and v = 0 or i = 0 and t = 1', '0.1', 'pt one' ),
117 array( 0, 'i = 1 and v = 0 or i = 0 and t = 1', '0.01', 'pt one' ),
118 array( 0, 'i = 1 and v = 0 or i = 0 and t = 1', '0.10', 'pt one' ),
119 array( 0, 'i = 1 and v = 0 or i = 0 and t = 1', '0.010', 'pt one' ),
120 array( 0, 'i = 1 and v = 0 or i = 0 and t = 1', '0.100', 'pt one' ),
121 array( 1, 'i = 1 and v = 0 or i = 0 and t = 1', '0.0', 'pt other' ),
122 array( 1, 'i = 1 and v = 0 or i = 0 and t = 1', '0.2', 'pt other' ),
123 array( 1, 'i = 1 and v = 0 or i = 0 and t = 1', '10.0', 'pt other' ),
124 array( 1, 'i = 1 and v = 0 or i = 0 and t = 1', '100.0', 'pt other' ),
125 array( 0, 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14', '2', 'bs few' ),
126 array( 0, 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14', '4', 'bs few' ),
127 array( 0, 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14', '22', 'bs few' ),
128 array( 0, 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14', '102', 'bs few' ),
129 array( 0, 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14', '0.2', 'bs few' ),
130 array( 0, 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14', '0.4', 'bs few' ),
131 array( 0, 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14', '10.2', 'bs few' ),
132 array( 1, 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14', '10.0', 'bs other' ),
133
134 );
135
136 return $tests;
137 }
138
139 function invalidTestCases() {
140 $tests = array(
141 array( 'n mod mod 5 is 1', 'mod mod' ),
142 array( 'n', 'just n' ),
143 array( 'n is in 5', 'is in' ),
144 );
145
146 return $tests;
147 }
148 }