alanknight | eda105f | 2016-05-03 12:17:54 -0700 | [diff] [blame] | 1 | // 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 | |
| 10 | library plural_test; |
| 11 | |
| 12 | import 'package:intl/intl.dart'; |
alanknight | 810432e | 2017-06-15 13:40:56 -0700 | [diff] [blame] | 13 | import 'package:test/test.dart'; |
alanknight | eda105f | 2016-05-03 12:17:54 -0700 | [diff] [blame] | 14 | |
| 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. |
| 22 | var expectedRu = ''' |
| 23 | 0:Zero |
| 24 | 1:One |
| 25 | 2:Few |
| 26 | 3:Few |
| 27 | 4:Few |
| 28 | 5:Many |
| 29 | 6:Many |
| 30 | 7:Many |
| 31 | 8:Many |
| 32 | 9:Many |
| 33 | 10:Many |
| 34 | 11:Many |
| 35 | 12:Many |
| 36 | 13:Many |
| 37 | 14:Many |
| 38 | 15:Many |
| 39 | 16:Many |
| 40 | 17:Many |
| 41 | 18:Many |
| 42 | 19:Many |
| 43 | 20:Many |
| 44 | 21:One |
| 45 | 22:Few |
| 46 | 23:Few |
| 47 | 24:Few |
| 48 | 25:Many |
| 49 | 26:Many |
| 50 | 27:Many |
| 51 | 28:Many |
| 52 | 29:Many |
| 53 | 30:Many |
| 54 | 31:One |
| 55 | 32:Few |
| 56 | 59:Many |
| 57 | 60:Many |
| 58 | 61:One |
| 59 | 62:Few |
| 60 | 63:Few |
| 61 | 64:Few |
| 62 | 65:Many |
| 63 | 66:Many |
| 64 | 67:Many |
| 65 | 68:Many |
| 66 | 69:Many |
| 67 | 70:Many |
| 68 | 71:One |
| 69 | 72:Few |
| 70 | 100:Many |
| 71 | 101:One |
| 72 | 102:Few |
| 73 | 103:Few |
| 74 | 104:Few |
| 75 | 105:Many |
| 76 | 106:Many |
| 77 | 107:Many |
| 78 | 108:Many |
| 79 | 109:Many |
| 80 | 110:Many |
| 81 | 111:Many |
| 82 | 112:Many |
| 83 | 113:Many |
| 84 | 114:Many |
| 85 | 115:Many |
| 86 | 116:Many |
| 87 | 117:Many |
| 88 | 118:Many |
| 89 | 119:Many |
| 90 | 120:Many |
| 91 | 121:One |
| 92 | 122:Few |
| 93 | 129:Many |
| 94 | 130:Many |
| 95 | 131:One |
| 96 | 132:Few |
| 97 | 139:Many |
| 98 | 140:Many |
| 99 | 141:One |
| 100 | 142:Few |
| 101 | 143:Few |
| 102 | 144:Few |
| 103 | 145:Many |
| 104 | '''; |
| 105 | |
| 106 | var expectedEn = ''' |
| 107 | 0:Zero |
| 108 | 1:One |
| 109 | 2:Other |
| 110 | 3:Other |
| 111 | 4:Other |
| 112 | 5:Other |
| 113 | 6:Other |
| 114 | 7:Other |
| 115 | 8:Other |
| 116 | 9:Other |
| 117 | 10:Other |
| 118 | 11:Other |
| 119 | 12:Other |
| 120 | 13:Other |
| 121 | 14:Other |
| 122 | 15:Other |
| 123 | 16:Other |
| 124 | 17:Other |
| 125 | 18:Other |
| 126 | 19:Other |
| 127 | 20:Other |
| 128 | 21:Other |
| 129 | 22:Other |
| 130 | 145:Other |
| 131 | '''; |
| 132 | |
| 133 | var expectedRo = ''' |
| 134 | 0:Few |
| 135 | 1:One |
| 136 | 2:Few |
| 137 | 12:Few |
| 138 | 23:Other |
| 139 | 1212:Few |
| 140 | 1223:Other |
| 141 | '''; |
| 142 | |
| 143 | var expectedSr = ''' |
| 144 | 0:Other |
| 145 | 1:One |
| 146 | 31:One |
| 147 | 3:Few |
| 148 | 33:Few |
| 149 | 5:Other |
| 150 | 10:Other |
| 151 | 35:Other |
| 152 | 37:Other |
| 153 | 40:Other |
| 154 | 2:Few |
| 155 | 20:Other |
| 156 | 21:One |
| 157 | 22:Few |
| 158 | 23:Few |
| 159 | 24:Few |
| 160 | 25:Other |
| 161 | '''; |
| 162 | |
Dart Team | 795c490 | 2019-08-29 10:58:28 -0700 | [diff] [blame] | 163 | String plural(n, locale) => Intl.plural(n, |
alanknight | eda105f | 2016-05-03 12:17:54 -0700 | [diff] [blame] | 164 | 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 Team | 795c490 | 2019-08-29 10:58:28 -0700 | [diff] [blame] | 175 | String pluralNoZero(n, locale) => Intl.plural(n, |
alanknight | eda105f | 2016-05-03 12:17:54 -0700 | [diff] [blame] | 176 | 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 Team | 795c490 | 2019-08-29 10:58:28 -0700 | [diff] [blame] | 186 | void main() { |
alanknight | eda105f | 2016-05-03 12:17:54 -0700 | [diff] [blame] | 187 | 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); |
alanknight | 090fb4d | 2016-09-02 16:34:43 -0700 | [diff] [blame] | 192 | |
Dart Team | 795c490 | 2019-08-29 10:58:28 -0700 | [diff] [blame] | 193 | test('Check null howMany', () { |
| 194 | expect(plural(0, null), '0:Zero'); |
davidmorgan | 4d5c96c | 2020-08-26 15:58:57 +0000 | [diff] [blame] | 195 | expect(() => plural(null, null), throwsA(isA<Error>())); |
| 196 | expect(() => plural(null, 'ru'), throwsA(isA<Error>())); |
alanknight | 090fb4d | 2016-09-02 16:34:43 -0700 | [diff] [blame] | 197 | }); |
Dart Team | 5644b15 | 2019-07-16 00:54:18 -0700 | [diff] [blame] | 198 | |
Dart Team | 795c490 | 2019-08-29 10:58:28 -0700 | [diff] [blame] | 199 | verifyWithPrecision('1 dollar', 'en', 1, 0); |
Dart Team | 5644b15 | 2019-07-16 00:54:18 -0700 | [diff] [blame] | 200 | // 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 Team | 795c490 | 2019-08-29 10:58:28 -0700 | [diff] [blame] | 207 | verifyWithPrecision('1 dollar', 'en', 1.2, 0); |
| 208 | verifyWithPrecision('1.20 dollars', 'en', 1.2, 2); |
Dart Team | 5644b15 | 2019-07-16 00:54:18 -0700 | [diff] [blame] | 209 | |
Dart Team | 795c490 | 2019-08-29 10:58:28 -0700 | [diff] [blame] | 210 | verifyWithPrecision('3 dollars', 'en', 3.14, 0); |
| 211 | verifyWithPrecision('3.14 dollars', 'en', 3.14, 2); |
alanknight | eda105f | 2016-05-03 12:17:54 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Dart Team | 795c490 | 2019-08-29 10:58:28 -0700 | [diff] [blame] | 214 | void verify(String expectedValues, String locale, pluralFunction) { |
alanknight | eda105f | 2016-05-03 12:17:54 -0700 | [diff] [blame] | 215 | 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 Team | 469e340 | 2019-11-18 15:50:11 -0800 | [diff] [blame] | 220 | var float = number.toDouble(); |
| 221 | var lineWithFloat = lines[i].replaceFirst('$number', '$float'); |
| 222 | expect(pluralFunction(float, locale), lineWithFloat); |
alanknight | eda105f | 2016-05-03 12:17:54 -0700 | [diff] [blame] | 223 | }); |
| 224 | } |
| 225 | } |
Dart Team | 5644b15 | 2019-07-16 00:54:18 -0700 | [diff] [blame] | 226 | |
Dart Team | 795c490 | 2019-08-29 10:58:28 -0700 | [diff] [blame] | 227 | void verifyWithPrecision(String expected, String locale, num n, int precision) { |
Dart Team | 5644b15 | 2019-07-16 00:54:18 -0700 | [diff] [blame] | 228 | 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 | } |