/
pluralForms.php
233 lines (213 loc) · 4.89 KB
/
pluralForms.php
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
<?php
/**
* @group pomo
*/
class PluralFormsTest extends WP_UnitTestCase {
/**
* Parenthesize plural expression.
*
* Legacy workaround for PHP's flipped precedence order for ternary.
*
* @param string $expression the expression without parentheses
* @return string the expression with parentheses added
*/
protected static function parenthesize_plural_expression( $expression ) {
$expression .= ';';
$res = '';
$depth = 0;
for ( $i = 0; $i < strlen( $expression ); ++$i ) {
$char = $expression[ $i ];
switch ( $char ) {
case '?':
$res .= ' ? (';
$depth++;
break;
case ':':
$res .= ') : (';
break;
case ';':
$res .= str_repeat( ')', $depth ) . ';';
$depth = 0;
break;
default:
$res .= $char;
}
}
return rtrim( $res, ';' );
}
/**
* @ticket 41562
* @group external-http
*/
public function test_locales_provider() {
$locales = self::locales_provider();
$this->assertNotEmpty( $locales, 'Unable to retrieve GP_Locales file' );
}
public static function locales_provider() {
if ( ! class_exists( 'GP_Locales' ) ) {
$filename = download_url( 'https://raw.githubusercontent.com/GlotPress/GlotPress-WP/develop/locales/locales.php' );
if ( is_wp_error( $filename ) ) {
return array();
}
require_once $filename;
}
$locales = GP_Locales::locales();
$plural_expressions = array();
foreach ( $locales as $slug => $locale ) {
$plural_expression = $locale->plural_expression;
if ( 'n != 1' !== $plural_expression ) {
$plural_expressions[] = array( $slug, $locale->nplurals, $plural_expression );
}
}
return $plural_expressions;
}
/**
* @ticket 41562
* @dataProvider locales_provider
* @group external-http
*/
public function test_regression( $lang, $nplurals, $expression ) {
require_once dirname( dirname( __DIR__ ) ) . '/includes/plural-form-function.php';
$parenthesized = self::parenthesize_plural_expression( $expression );
$old_style = tests_make_plural_form_function( $nplurals, $parenthesized );
$plural_forms = new Plural_Forms( $expression );
$generated_old = array();
$generated_new = array();
foreach ( range( 0, 200 ) as $i ) {
$generated_old[] = $old_style( $i );
$generated_new[] = $plural_forms->get( $i );
}
$this->assertSame( $generated_old, $generated_new );
}
public static function simple_provider() {
return array(
array(
// Simple equivalence.
'n != 1',
array(
-1 => 1,
0 => 1,
1 => 0,
2 => 1,
5 => 1,
10 => 1,
),
),
array(
// Ternary.
'n ? 1 : 2',
array(
-1 => 1,
0 => 2,
1 => 1,
2 => 1,
),
),
array(
// Comparison.
'n > 1 ? 1 : 2',
array(
-2 => 2,
-1 => 2,
0 => 2,
1 => 2,
2 => 1,
3 => 1,
),
),
array(
'n > 1 ? n > 2 ? 1 : 2 : 3',
array(
-2 => 3,
-1 => 3,
0 => 3,
1 => 3,
2 => 2,
3 => 1,
4 => 1,
),
),
);
}
/**
* @ticket 41562
* @dataProvider simple_provider
*/
public function test_simple( $expression, $expected ) {
$plural_forms = new Plural_Forms( $expression );
$actual = array();
foreach ( array_keys( $expected ) as $num ) {
$actual[ $num ] = $plural_forms->get( $num );
}
$this->assertSame( $expected, $actual );
}
public function data_exceptions() {
return array(
array(
'n # 2', // Invalid expression to parse.
'Unknown symbol "#"', // Expected exception message.
false, // Whether to call the get() method or not.
),
array(
'n & 1',
'Unknown operator "&"',
false,
),
array(
'((n)',
'Mismatched parentheses',
false,
),
array(
'(n))',
'Mismatched parentheses',
false,
),
array(
'n : 2',
'Missing starting "?" ternary operator',
false,
),
array(
'n ? 1',
'Unknown operator "?"',
true,
),
array(
'n n',
'Too many values remaining on the stack',
true,
),
);
}
/**
* Ensures that an exception is thrown when an invalid plural form is encountered.
*
* @ticket 41562
* @dataProvider data_exceptions
*/
public function test_exceptions( $expression, $expected_message, $call_get ) {
$this->expectException( 'Exception' );
$this->expectExceptionMessage( $expected_message );
$plural_forms = new Plural_Forms( $expression );
if ( $call_get ) {
$plural_forms->get( 1 );
}
}
/**
* @ticket 41562
*/
public function test_cache() {
$mock = $this->getMockBuilder( 'Plural_Forms' )
->setMethods( array( 'execute' ) )
->setConstructorArgs( array( 'n != 1' ) )
->getMock();
$mock->expects( $this->once() )
->method( 'execute' )
->with( $this->identicalTo( 2 ) )
->will( $this->returnValue( 1 ) );
$first = $mock->get( 2 );
$second = $mock->get( 2 );
$this->assertSame( $first, $second );
}
}