blob: 1aa60b6dcb4754cdc9f0cd2793b0dca8771fe1fa [file] [log] [blame]
alanknighteda105f2016-05-03 12:17:54 -07001// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5/// Test plurals without translation.
6///
7/// This exercises the plural selection rules. We aren't worried about the text,
8/// just that it picks the right phrase for the right number.
9
10library plural_test;
11
12import 'package:intl/intl.dart';
alanknight810432e2017-06-15 13:40:56 -070013import 'package:test/test.dart';
alanknighteda105f2016-05-03 12:17:54 -070014
15/// Hard-coded expected values for a Russian plural rule.
16///
17/// Note that the way I'm interpreting this is that if there's a case for zero,
18/// one or two and the number is exactly that, then use that value. Otherwise we
19/// use One for the singular, Few for the genitive singular, and Many for the
20/// genitive plural. Other would be used for fractional values if we supported
21/// those.
22var expectedRu = '''
230:Zero
241:One
252:Few
263:Few
274:Few
285:Many
296:Many
307:Many
318:Many
329:Many
3310:Many
3411:Many
3512:Many
3613:Many
3714:Many
3815:Many
3916:Many
4017:Many
4118:Many
4219:Many
4320:Many
4421:One
4522:Few
4623:Few
4724:Few
4825:Many
4926:Many
5027:Many
5128:Many
5229:Many
5330:Many
5431:One
5532:Few
5659:Many
5760:Many
5861:One
5962:Few
6063:Few
6164:Few
6265:Many
6366:Many
6467:Many
6568:Many
6669:Many
6770:Many
6871:One
6972:Few
70100:Many
71101:One
72102:Few
73103:Few
74104:Few
75105:Many
76106:Many
77107:Many
78108:Many
79109:Many
80110:Many
81111:Many
82112:Many
83113:Many
84114:Many
85115:Many
86116:Many
87117:Many
88118:Many
89119:Many
90120:Many
91121:One
92122:Few
93129:Many
94130:Many
95131:One
96132:Few
97139:Many
98140:Many
99141:One
100142:Few
101143:Few
102144:Few
103145:Many
104''';
105
106var expectedEn = '''
1070:Zero
1081:One
1092:Other
1103:Other
1114:Other
1125:Other
1136:Other
1147:Other
1158:Other
1169:Other
11710:Other
11811:Other
11912:Other
12013:Other
12114:Other
12215:Other
12316:Other
12417:Other
12518:Other
12619:Other
12720:Other
12821:Other
12922:Other
130145:Other
131''';
132
133var expectedRo = '''
1340:Few
1351:One
1362:Few
13712:Few
13823:Other
1391212:Few
1401223:Other
141''';
142
143var expectedSr = '''
1440:Other
1451:One
14631:One
1473:Few
14833:Few
1495:Other
15010:Other
15135:Other
15237:Other
15340:Other
1542:Few
15520:Other
15621:One
15722:Few
15823:Few
15924:Few
16025:Other
161''';
162
Dart Team795c4902019-08-29 10:58:28 -0700163String plural(n, locale) => Intl.plural(n,
alanknighteda105f2016-05-03 12:17:54 -0700164 locale: locale,
165 name: 'plural',
166 desc: 'A simple plural test case',
167 examples: {'n': 1},
168 args: [n],
169 zero: '$n:Zero',
170 one: '$n:One',
171 few: '$n:Few',
172 many: '$n:Many',
173 other: '$n:Other');
174
Dart Team795c4902019-08-29 10:58:28 -0700175String pluralNoZero(n, locale) => Intl.plural(n,
alanknighteda105f2016-05-03 12:17:54 -0700176 locale: locale,
177 name: 'plural',
178 desc: 'A simple plural test case',
179 examples: {'n': 1},
180 args: [n],
181 one: '$n:One',
182 few: '$n:Few',
183 many: '$n:Many',
184 other: '$n:Other');
185
Dart Team795c4902019-08-29 10:58:28 -0700186void main() {
alanknighteda105f2016-05-03 12:17:54 -0700187 verify(expectedRu, 'ru', plural);
188 verify(expectedRu, 'ru_RU', plural);
189 verify(expectedEn, 'en', plural);
190 verify(expectedRo, 'ro', pluralNoZero);
191 verify(expectedSr, 'sr', pluralNoZero);
alanknight090fb4d2016-09-02 16:34:43 -0700192
Dart Team795c4902019-08-29 10:58:28 -0700193 test('Check null howMany', () {
194 expect(plural(0, null), '0:Zero');
davidmorgan4d5c96c2020-08-26 15:58:57 +0000195 expect(() => plural(null, null), throwsA(isA<Error>()));
196 expect(() => plural(null, 'ru'), throwsA(isA<Error>()));
alanknight090fb4d2016-09-02 16:34:43 -0700197 });
Dart Team5644b152019-07-16 00:54:18 -0700198
Dart Team795c4902019-08-29 10:58:28 -0700199 verifyWithPrecision('1 dollar', 'en', 1, 0);
Dart Team5644b152019-07-16 00:54:18 -0700200 // This would not work in back-compatibility for one vs. =1 in plurals,
201 // because of this test in intl.dart:
202 // if (howMany == 1 && one != null) return one;
203 // That one will ignore the precision and always return one, while the
204 // test below requires the result to be 'other'
205 // verify_with_precision('1.00 dollars', 'en', 1, 2);
206
Dart Team795c4902019-08-29 10:58:28 -0700207 verifyWithPrecision('1 dollar', 'en', 1.2, 0);
208 verifyWithPrecision('1.20 dollars', 'en', 1.2, 2);
Dart Team5644b152019-07-16 00:54:18 -0700209
Dart Team795c4902019-08-29 10:58:28 -0700210 verifyWithPrecision('3 dollars', 'en', 3.14, 0);
211 verifyWithPrecision('3.14 dollars', 'en', 3.14, 2);
alanknighteda105f2016-05-03 12:17:54 -0700212}
213
Dart Team795c4902019-08-29 10:58:28 -0700214void verify(String expectedValues, String locale, pluralFunction) {
alanknighteda105f2016-05-03 12:17:54 -0700215 var lines = expectedValues.split('\n').where((x) => x.isNotEmpty).toList();
216 for (var i = 0; i < lines.length; i++) {
217 test(lines[i], () {
218 var number = int.parse(lines[i].split(':').first);
219 expect(pluralFunction(number, locale), lines[i]);
Dart Team469e3402019-11-18 15:50:11 -0800220 var float = number.toDouble();
221 var lineWithFloat = lines[i].replaceFirst('$number', '$float');
222 expect(pluralFunction(float, locale), lineWithFloat);
alanknighteda105f2016-05-03 12:17:54 -0700223 });
224 }
225}
Dart Team5644b152019-07-16 00:54:18 -0700226
Dart Team795c4902019-08-29 10:58:28 -0700227void verifyWithPrecision(String expected, String locale, num n, int precision) {
Dart Team5644b152019-07-16 00:54:18 -0700228 test('verify_with_precision(howMany: $n, precision: $precision)', () {
229 var nString = n.toStringAsFixed(precision);
230 var actual = Intl.plural(n,
231 locale: locale,
232 precision: precision,
233 one: '$nString dollar',
234 other: '$nString dollars');
235 expect(actual, expected);
236 });
237}