Debian Codegraph @

closure-library/closure/goog/i18n/messageformat_test.js

raw
1 // Copyright 2010 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.i18n.MessageFormatTest');
16 goog.setTestOnly('goog.i18n.MessageFormatTest');
17
18 goog.require('goog.i18n.MessageFormat');
19 goog.require('goog.i18n.NumberFormatSymbols_hr');
20 goog.require('goog.i18n.pluralRules');
21 goog.require('goog.testing.PropertyReplacer');
22 goog.require('goog.testing.jsunit');
23
24 // Testing stubs that autoreset after each test run.
25 var stubs = new goog.testing.PropertyReplacer();
26
27 function tearDown() {
28 stubs.reset();
29 }
30
31 function testEmptyPattern() {
32 var fmt = new goog.i18n.MessageFormat('');
33 assertEquals('', fmt.format({}));
34 }
35
36 function testMissingLeftCurlyBrace() {
37 var err = assertThrows(function() {
38 var fmt = new goog.i18n.MessageFormat('\'\'{}}');
39 fmt.format({});
40 });
41 assertEquals('Assertion failed: No matching { for }.', err.message);
42 }
43
44 function testTooManyLeftCurlyBraces() {
45 var err = assertThrows(function() {
46 var fmt = new goog.i18n.MessageFormat('{} {');
47 fmt.format({});
48 });
49 assertEquals(
50 'Assertion failed: There are mismatched { or } in the pattern.',
51 err.message);
52 }
53
54 function testSimpleReplacement() {
55 var fmt = new goog.i18n.MessageFormat('New York in {SEASON} is nice.');
56 assertEquals(
57 'New York in the Summer is nice.', fmt.format({'SEASON': 'the Summer'}));
58 }
59
60 function testSimpleSelect() {
61 var fmt = new goog.i18n.MessageFormat(
62 '{GENDER, select,' +
63 'male {His} ' +
64 'female {Her} ' +
65 'other {Its}}' +
66 ' bicycle is {GENDER, select, male {blue} female {red} other {green}}.');
67
68 assertEquals('His bicycle is blue.', fmt.format({'GENDER': 'male'}));
69 assertEquals('Her bicycle is red.', fmt.format({'GENDER': 'female'}));
70 assertEquals('Its bicycle is green.', fmt.format({'GENDER': 'other'}));
71 assertEquals('Its bicycle is green.', fmt.format({'GENDER': 'whatever'}));
72 }
73
74 function testSimplePlural() {
75 var fmt = new goog.i18n.MessageFormat(
76 'I see {NUM_PEOPLE, plural, offset:1 ' +
77 '=0 {no one at all in {PLACE}.} ' +
78 '=1 {{PERSON} in {PLACE}.} ' +
79 'one {{PERSON} and one other person in {PLACE}.} ' +
80 'other {{PERSON} and # other people in {PLACE}.}}');
81
82 assertEquals(
83 'I see no one at all in Belgrade.',
84 fmt.format({'NUM_PEOPLE': 0, 'PLACE': 'Belgrade'}));
85 assertEquals(
86 'I see Markus in Berlin.',
87 fmt.format({'NUM_PEOPLE': 1, 'PERSON': 'Markus', 'PLACE': 'Berlin'}));
88 assertEquals(
89 'I see Mark and one other person in Athens.',
90 fmt.format({'NUM_PEOPLE': 2, 'PERSON': 'Mark', 'PLACE': 'Athens'}));
91 assertEquals(
92 'I see Cibu and 99 other people in the cubes.',
93 fmt.format({'NUM_PEOPLE': 100, 'PERSON': 'Cibu', 'PLACE': 'the cubes'}));
94 }
95
96 function testSimplePluralNoOffset() {
97 var fmt = new goog.i18n.MessageFormat(
98 'I see {NUM_PEOPLE, plural, ' +
99 '=0 {no one at all} ' +
100 '=1 {{PERSON}} ' +
101 'one {{PERSON} and one other person} ' +
102 'other {{PERSON} and # other people}} in {PLACE}.');
103
104 assertEquals(
105 'I see no one at all in Belgrade.',
106 fmt.format({'NUM_PEOPLE': 0, 'PLACE': 'Belgrade'}));
107 assertEquals(
108 'I see Markus in Berlin.',
109 fmt.format({'NUM_PEOPLE': 1, 'PERSON': 'Markus', 'PLACE': 'Berlin'}));
110 assertEquals(
111 'I see Mark and 2 other people in Athens.',
112 fmt.format({'NUM_PEOPLE': 2, 'PERSON': 'Mark', 'PLACE': 'Athens'}));
113 assertEquals(
114 'I see Cibu and 100 other people in the cubes.',
115 fmt.format({'NUM_PEOPLE': 100, 'PERSON': 'Cibu', 'PLACE': 'the cubes'}));
116 }
117
118 function testSelectNestedInPlural() {
119 var fmt = new goog.i18n.MessageFormat(
120 '{CIRCLES, plural, ' +
121 'one {{GENDER, select, ' +
122 ' female {{WHO} added you to her circle} ' +
123 ' other {{WHO} added you to his circle}}} ' +
124 'other {{GENDER, select, ' +
125 ' female {{WHO} added you to her # circles} ' +
126 ' other {{WHO} added you to his # circles}}}}');
127
128 assertEquals(
129 'Jelena added you to her circle',
130 fmt.format({'GENDER': 'female', 'WHO': 'Jelena', 'CIRCLES': 1}));
131 assertEquals(
132 'Milan added you to his 1,234 circles',
133 fmt.format({'GENDER': 'male', 'WHO': 'Milan', 'CIRCLES': 1234}));
134 }
135
136 function testPluralNestedInSelect() {
137 // Added offset just for testing purposes. It doesn't make sense
138 // to have it otherwise.
139 var fmt = new goog.i18n.MessageFormat(
140 '{GENDER, select, ' +
141 'female {{NUM_GROUPS, plural, ' +
142 ' one {{WHO} added you to her group} ' +
143 ' other {{WHO} added you to her # groups}}} ' +
144 'other {{NUM_GROUPS, plural, offset:1' +
145 ' one {{WHO} added you to his group} ' +
146 ' other {{WHO} added you to his # groups}}}}');
147
148 assertEquals(
149 'Jelena added you to her group',
150 fmt.format({'GENDER': 'female', 'WHO': 'Jelena', 'NUM_GROUPS': 1}));
151 assertEquals(
152 'Milan added you to his 1,233 groups',
153 fmt.format({'GENDER': 'male', 'WHO': 'Milan', 'NUM_GROUPS': 1234}));
154 }
155
156 function testLiteralOpenCurlyBrace() {
157 var fmt = new goog.i18n.MessageFormat(
158 "Anna's house" + " has '{0} and # in the roof' and {NUM_COWS} cows.");
159 assertEquals(
160 "Anna's house has {0} and # in the roof and 5 cows.",
161 fmt.format({'NUM_COWS': '5'}));
162 }
163
164 function testLiteralClosedCurlyBrace() {
165 var fmt = new goog.i18n.MessageFormat(
166 "Anna's house" + " has '{'0'} and # in the roof' and {NUM_COWS} cows.");
167 assertEquals(
168 "Anna's house has {0} and # in the roof and 5 cows.",
169 fmt.format({'NUM_COWS': '5'}));
170 // Regression for: b/34764827
171 assertEquals(
172 'Anna\'s house has {0} and # in the roof and 8 cows.',
173 fmt.format({'NUM_COWS': '8'}));
174 }
175
176 function testLiteralPoundSign() {
177 var fmt = new goog.i18n.MessageFormat(
178 "Anna's house" + " has '{0}' and '# in the roof' and {NUM_COWS} cows.");
179 assertEquals(
180 "Anna's house has {0} and # in the roof and 5 cows.",
181 fmt.format({'NUM_COWS': '5'}));
182 // Regression for: b/34764827
183 assertEquals(
184 'Anna\'s house has {0} and # in the roof and 10 cows.',
185 fmt.format({'NUM_COWS': '10'}));
186 }
187
188 function testNoLiteralsForSingleQuotes() {
189 var fmt =
190 new goog.i18n.MessageFormat("Anna's house" + " 'has {NUM_COWS} cows'.");
191 assertEquals("Anna's house 'has 5 cows'.", fmt.format({'NUM_COWS': '5'}));
192 }
193
194 function testConsecutiveSingleQuotesAreReplacedWithOneSingleQuote() {
195 var fmt = new goog.i18n.MessageFormat("Anna''s house a'{''''b'");
196 assertEquals("Anna's house a{''b", fmt.format({}));
197 }
198
199 function testConsecutiveSingleQuotesBeforeSpecialCharDontCreateLiteral() {
200 var fmt = new goog.i18n.MessageFormat("a''{NUM_COWS}'b");
201 assertEquals("a'5'b", fmt.format({'NUM_COWS': '5'}));
202 }
203
204 function testSerbianSimpleSelect() {
205 stubs.set(goog.i18n.pluralRules, 'select', goog.i18n.pluralRules.beSelect_);
206
207 var fmt = new goog.i18n.MessageFormat(
208 '{GENDER, select, ' +
209 'female {Njen} other {Njegov}} bicikl je ' +
210 '{GENDER, select, female {crven} other {plav}}.');
211
212 assertEquals('Njegov bicikl je plav.', fmt.format({'GENDER': 'male'}));
213 assertEquals('Njen bicikl je crven.', fmt.format({'GENDER': 'female'}));
214 }
215
216 function testSerbianSimplePlural() {
217 stubs.set(goog.i18n.pluralRules, 'select', goog.i18n.pluralRules.beSelect_);
218
219 var fmt = new goog.i18n.MessageFormat(
220 'Ja {NUM_PEOPLE, plural, offset:1 ' +
221 '=0 {ne vidim nikoga} ' +
222 '=1 {vidim {PERSON}} ' +
223 'one {vidim {PERSON} i jos # osobu} ' +
224 'few {vidim {PERSON} i jos # osobe} ' +
225 'many {vidim {PERSON} i jos # osoba} ' +
226 'other {{PERSON} i jos # osoba}} ' +
227 'u {PLACE}.');
228
229 assertEquals(
230 'Ja ne vidim nikoga u Beogradu.',
231 fmt.format({'NUM_PEOPLE': 0, 'PLACE': 'Beogradu'}));
232 assertEquals(
233 'Ja vidim Markusa u Berlinu.',
234 fmt.format({'NUM_PEOPLE': 1, 'PERSON': 'Markusa', 'PLACE': 'Berlinu'}));
235 assertEquals(
236 'Ja vidim Marka i jos 1 osobu u Atini.',
237 fmt.format({'NUM_PEOPLE': 2, 'PERSON': 'Marka', 'PLACE': 'Atini'}));
238 assertEquals(
239 'Ja vidim Petra i jos 3 osobe u muzeju.',
240 fmt.format({'NUM_PEOPLE': 4, 'PERSON': 'Petra', 'PLACE': 'muzeju'}));
241 assertEquals(
242 'Ja vidim Cibua i jos 99 osoba u bazenu.',
243 fmt.format({'NUM_PEOPLE': 100, 'PERSON': 'Cibua', 'PLACE': 'bazenu'}));
244 }
245
246 function testSerbianSimplePluralNoOffset() {
247 stubs.set(goog.i18n.pluralRules, 'select', goog.i18n.pluralRules.beSelect_);
248
249 var fmt = new goog.i18n.MessageFormat(
250 'Ja {NUM_PEOPLE, plural, ' +
251 '=0 {ne vidim nikoga} ' +
252 '=1 {vidim {PERSON}} ' +
253 'one {vidim {PERSON} i jos # osobu} ' +
254 'few {vidim {PERSON} i jos # osobe} ' +
255 'many {vidim {PERSON} i jos # osoba} ' +
256 'other {{PERSON} i jos # osoba}} ' +
257 'u {PLACE}.');
258
259 assertEquals(
260 'Ja ne vidim nikoga u Beogradu.',
261 fmt.format({'NUM_PEOPLE': 0, 'PLACE': 'Beogradu'}));
262 assertEquals(
263 'Ja vidim Markusa u Berlinu.',
264 fmt.format({'NUM_PEOPLE': 1, 'PERSON': 'Markusa', 'PLACE': 'Berlinu'}));
265 assertEquals(
266 'Ja vidim Marka i jos 21 osobu u Atini.',
267 fmt.format({'NUM_PEOPLE': 21, 'PERSON': 'Marka', 'PLACE': 'Atini'}));
268 assertEquals(
269 'Ja vidim Petra i jos 3 osobe u muzeju.',
270 fmt.format({'NUM_PEOPLE': 3, 'PERSON': 'Petra', 'PLACE': 'muzeju'}));
271 assertEquals(
272 'Ja vidim Cibua i jos 100 osoba u bazenu.',
273 fmt.format({'NUM_PEOPLE': 100, 'PERSON': 'Cibua', 'PLACE': 'bazenu'}));
274 }
275
276 function testSerbianSelectNestedInPlural() {
277 stubs.set(goog.i18n.pluralRules, 'select', goog.i18n.pluralRules.beSelect_);
278 stubs.set(goog.i18n, 'NumberFormatSymbols', goog.i18n.NumberFormatSymbols_hr);
279
280 var fmt = new goog.i18n.MessageFormat(
281 '{CIRCLES, plural, ' +
282 'one {{GENDER, select, ' +
283 ' female {{WHO} vas je dodala u njen # kruzok} ' +
284 ' other {{WHO} vas je dodao u njegov # kruzok}}} ' +
285 'few {{GENDER, select, ' +
286 ' female {{WHO} vas je dodala u njena # kruzoka} ' +
287 ' other {{WHO} vas je dodao u njegova # kruzoka}}} ' +
288 'many {{GENDER, select, ' +
289 ' female {{WHO} vas je dodala u njenih # kruzoka} ' +
290 ' other {{WHO} vas je dodao u njegovih # kruzoka}}} ' +
291 'other {{GENDER, select, ' +
292 ' female {{WHO} vas je dodala u njenih # kruzoka} ' +
293 ' other {{WHO} vas je dodao u njegovih # kruzoka}}}}');
294
295 assertEquals(
296 'Jelena vas je dodala u njen 21 kruzok',
297 fmt.format({'GENDER': 'female', 'WHO': 'Jelena', 'CIRCLES': 21}));
298 assertEquals(
299 'Jelena vas je dodala u njena 3 kruzoka',
300 fmt.format({'GENDER': 'female', 'WHO': 'Jelena', 'CIRCLES': 3}));
301 assertEquals(
302 'Jelena vas je dodala u njenih 5 kruzoka',
303 fmt.format({'GENDER': 'female', 'WHO': 'Jelena', 'CIRCLES': 5}));
304 assertEquals(
305 'Milan vas je dodao u njegovih 1.235 kruzoka',
306 fmt.format({'GENDER': 'male', 'WHO': 'Milan', 'CIRCLES': 1235}));
307 }
308
309 function testFallbackToOtherOptionInPlurals() {
310 // Use Arabic plural rules since they have all six cases.
311 // Only locale and numbers matter, the actual language of the message
312 // does not.
313 stubs.set(goog.i18n.pluralRules, 'select', goog.i18n.pluralRules.arSelect_);
314
315 var fmt = new goog.i18n.MessageFormat(
316 '{NUM_MINUTES, plural, ' +
317 'other {# minutes}}');
318
319 // These numbers exercise all cases for the arabic plural rules.
320 assertEquals('0 minutes', fmt.format({'NUM_MINUTES': 0}));
321 assertEquals('1 minutes', fmt.format({'NUM_MINUTES': 1}));
322 assertEquals('2 minutes', fmt.format({'NUM_MINUTES': 2}));
323 assertEquals('3 minutes', fmt.format({'NUM_MINUTES': 3}));
324 assertEquals('11 minutes', fmt.format({'NUM_MINUTES': 11}));
325 assertEquals('1.5 minutes', fmt.format({'NUM_MINUTES': 1.5}));
326 }
327
328 function testPoundShowsNumberMinusOffsetInAllCases() {
329 var fmt = new goog.i18n.MessageFormat(
330 '{SOME_NUM, plural, offset:1 ' +
331 '=0 {#} =1 {#} =2 {#}one {#} other {#}}');
332
333 assertEquals('-1', fmt.format({'SOME_NUM': '0'}));
334 assertEquals('0', fmt.format({'SOME_NUM': '1'}));
335 assertEquals('1', fmt.format({'SOME_NUM': '2'}));
336 assertEquals('20', fmt.format({'SOME_NUM': '21'}));
337 }
338
339 function testSpecialCharactersInParamaterDontChangeFormat() {
340 var fmt = new goog.i18n.MessageFormat(
341 '{SOME_NUM, plural,' +
342 'other {# {GROUP}}}');
343
344 // Test pound sign.
345 assertEquals(
346 '10 group#1', fmt.format({'SOME_NUM': '10', 'GROUP': 'group#1'}));
347 // Test other special characters in parameters, like { and }.
348 assertEquals('10 } {', fmt.format({'SOME_NUM': '10', 'GROUP': '} {'}));
349 }
350
351 function testMissingOrInvalidPluralParameter() {
352 var fmt = new goog.i18n.MessageFormat(
353 '{SOME_NUM, plural,' +
354 'other {result}}');
355
356 // Key name doesn't match A != SOME_NUM.
357 assertEquals(
358 'Undefined or invalid parameter - SOME_NUM', fmt.format({A: '10'}));
359
360 // Value is not a number.
361 assertEquals(
362 'Undefined or invalid parameter - SOME_NUM',
363 fmt.format({'SOME_NUM': 'Value'}));
364 }
365
366 function testMissingSelectParameter() {
367 var fmt = new goog.i18n.MessageFormat(
368 '{GENDER, select,' +
369 'other {result}}');
370
371 // Key name doesn't match A != GENDER.
372 assertEquals('Undefined parameter - GENDER', fmt.format({A: 'female'}));
373 }
374
375 function testMissingSimplePlaceholder() {
376 var fmt = new goog.i18n.MessageFormat('{result}');
377
378 // Key name doesn't match A != result.
379 assertEquals('Undefined parameter - result', fmt.format({A: 'none'}));
380 }
381
382 function testPluralWithIgnorePound() {
383 var fmt = new goog.i18n.MessageFormat(
384 '{SOME_NUM, plural,' +
385 'other {# {GROUP}}}');
386
387 // Test pound sign.
388 assertEquals(
389 '# group#1',
390 fmt.formatIgnoringPound({'SOME_NUM': '10', 'GROUP': 'group#1'}));
391 // Test other special characters in parameters, like { and }.
392 assertEquals(
393 '# } {', fmt.formatIgnoringPound({'SOME_NUM': '10', 'GROUP': '} {'}));
394 }
395
396 function testSimplePluralWithIgnorePound() {
397 var fmt = new goog.i18n.MessageFormat(
398 'I see {NUM_PEOPLE, plural, offset:1 ' +
399 '=0 {no one at all in {PLACE}.} ' +
400 '=1 {{PERSON} in {PLACE}.} ' +
401 'one {{PERSON} and one other person in {PLACE}.} ' +
402 'other {{PERSON} and # other people in {PLACE}.}}');
403
404 assertEquals(
405 'I see Cibu and # other people in the cubes.',
406 fmt.formatIgnoringPound(
407 {'NUM_PEOPLE': 100, 'PERSON': 'Cibu', 'PLACE': 'the cubes'}));
408 }
409
410 function testSimpleOrdinal() {
411 var fmt = new goog.i18n.MessageFormat(
412 '{NUM_FLOOR, selectordinal, ' +
413 'one {Take the elevator to the #st floor.}' +
414 'two {Take the elevator to the #nd floor.}' +
415 'few {Take the elevator to the #rd floor.}' +
416 'other {Take the elevator to the #th floor.}}');
417
418 assertEquals(
419 'Take the elevator to the 1st floor.', fmt.format({'NUM_FLOOR': 1}));
420 assertEquals(
421 'Take the elevator to the 2nd floor.', fmt.format({'NUM_FLOOR': 2}));
422 assertEquals(
423 'Take the elevator to the 3rd floor.', fmt.format({'NUM_FLOOR': 3}));
424 assertEquals(
425 'Take the elevator to the 4th floor.', fmt.format({'NUM_FLOOR': 4}));
426 assertEquals(
427 'Take the elevator to the 23rd floor.', fmt.format({'NUM_FLOOR': 23}));
428 // Esoteric example.
429 assertEquals(
430 'Take the elevator to the 0th floor.', fmt.format({'NUM_FLOOR': 0}));
431 }
432
433 function testOrdinalWithNegativeValue() {
434 var fmt = new goog.i18n.MessageFormat(
435 '{NUM_FLOOR, selectordinal, ' +
436 'one {Take the elevator to the #st floor.}' +
437 'two {Take the elevator to the #nd floor.}' +
438 'few {Take the elevator to the #rd floor.}' +
439 'other {Take the elevator to the #th floor.}}');
440
441 try {
442 fmt.format({'NUM_FLOOR': -2});
443 } catch (e) {
444 assertEquals(
445 'Assertion failed: Argument index smaller than offset.', e.message);
446 return;
447 }
448 fail('Expected an error to be thrown');
449 }
450
451 function testSimpleOrdinalWithIgnorePound() {
452 var fmt = new goog.i18n.MessageFormat(
453 '{NUM_FLOOR, selectordinal, ' +
454 'one {Take the elevator to the #st floor.}' +
455 'two {Take the elevator to the #nd floor.}' +
456 'few {Take the elevator to the #rd floor.}' +
457 'other {Take the elevator to the #th floor.}}');
458
459 assertEquals(
460 'Take the elevator to the #th floor.',
461 fmt.formatIgnoringPound({'NUM_FLOOR': 100}));
462 }
463
464 function testMissingOrInvalidOrdinalParameter() {
465 var fmt = new goog.i18n.MessageFormat(
466 '{SOME_NUM, selectordinal,' +
467 'other {result}}');
468
469 // Key name doesn't match A != SOME_NUM.
470 assertEquals(
471 'Undefined or invalid parameter - SOME_NUM', fmt.format({A: '10'}));
472
473 // Value is not a number.
474 assertEquals(
475 'Undefined or invalid parameter - SOME_NUM',
476 fmt.format({'SOME_NUM': 'Value'}));
477 }