aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/i18n/messageformat_test.html
blob: b316c715c19a8b733c10d1ad811c156bd22c1f0b (plain)
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<!DOCTYPE html>
<html>
<!--
Copyright 2010 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.i18n.MessageFormat</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<script src="../base.js"></script>
<script>
  goog.require('goog.i18n.MessageFormat');
  goog.require('goog.i18n.NumberFormatSymbols');
  goog.require('goog.i18n.NumberFormatSymbols_en');
  goog.require('goog.i18n.NumberFormatSymbols_hr');
  goog.require('goog.i18n.ordinalRules');
  goog.require('goog.i18n.pluralRules');
  goog.require('goog.testing.PropertyReplacer');
  goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>

// Testing stubs that autoreset after each test run.
var stubs = new goog.testing.PropertyReplacer();

function tearDown() {
  stubs.reset();
}

function testEmptyPattern() {
  var fmt = new goog.i18n.MessageFormat('');
  assertEquals('', fmt.format({}));
}

function testMissingLeftCurlyBrace() {
  var err = assertThrows(function() {
    new goog.i18n.MessageFormat('\'\'{}}');
  });
  assertEquals('Assertion failed: No matching { for }.', err.message);
}

function testTooManyLeftCurlyBraces() {
  var err = assertThrows(function() {
    new goog.i18n.MessageFormat('{} {');
  });
  assertEquals('Assertion failed: There are mismatched { or } in the pattern.',
      err.message);
}

function testSimpleReplacement() {
  var fmt = new goog.i18n.MessageFormat('New York in {SEASON} is nice.');
  assertEquals('New York in the Summer is nice.',
               fmt.format({'SEASON': 'the Summer'}));
}

function testSimpleSelect() {
  var fmt = new goog.i18n.MessageFormat('{GENDER, select,' +
      'male {His} ' +
      'female {Her} ' +
      'other {Its}}' +
      ' bicycle is {GENDER, select, male {blue} female {red} other {green}}.');

  assertEquals('His bicycle is blue.', fmt.format({'GENDER': 'male'}));
  assertEquals('Her bicycle is red.', fmt.format({'GENDER': 'female'}));
  assertEquals('Its bicycle is green.', fmt.format({'GENDER': 'other'}));
  assertEquals('Its bicycle is green.', fmt.format({'GENDER': 'whatever'}));
}

function testSimplePlural() {
  var fmt = new goog.i18n.MessageFormat('I see {NUM_PEOPLE, plural, offset:1 ' +
      '=0 {no one at all in {PLACE}.} ' +
      '=1 {{PERSON} in {PLACE}.} ' +
      'one {{PERSON} and one other person in {PLACE}.} ' +
      'other {{PERSON} and # other people in {PLACE}.}}');

  assertEquals('I see no one at all in Belgrade.',
               fmt.format({'NUM_PEOPLE': 0,
                           'PLACE': 'Belgrade'}));
  assertEquals('I see Markus in Berlin.',
               fmt.format({'NUM_PEOPLE': 1,
                           'PERSON': 'Markus',
                           'PLACE': 'Berlin'}));
  assertEquals('I see Mark and one other person in Athens.',
               fmt.format({'NUM_PEOPLE': 2,
                           'PERSON': 'Mark',
                           'PLACE': 'Athens'}));
  assertEquals('I see Cibu and 99 other people in the cubes.',
               fmt.format({'NUM_PEOPLE': 100,
                           'PERSON': 'Cibu',
                           'PLACE': 'the cubes'}));
}

function testSimplePluralNoOffset() {
  var fmt = new goog.i18n.MessageFormat('I see {NUM_PEOPLE, plural, ' +
      '=0 {no one at all} ' +
      '=1 {{PERSON}} ' +
      'one {{PERSON} and one other person} ' +
      'other {{PERSON} and # other people}} in {PLACE}.');

  assertEquals('I see no one at all in Belgrade.',
               fmt.format({'NUM_PEOPLE': 0,
                           'PLACE': 'Belgrade'}));
  assertEquals('I see Markus in Berlin.',
               fmt.format({'NUM_PEOPLE': 1,
                           'PERSON': 'Markus',
                           'PLACE': 'Berlin'}));
  assertEquals('I see Mark and 2 other people in Athens.',
               fmt.format({'NUM_PEOPLE': 2,
                           'PERSON': 'Mark',
                           'PLACE': 'Athens'}));
  assertEquals('I see Cibu and 100 other people in the cubes.',
               fmt.format({'NUM_PEOPLE': 100,
                           'PERSON': 'Cibu',
                           'PLACE': 'the cubes'}));
}

function testSelectNestedInPlural() {
  var fmt = new goog.i18n.MessageFormat('{CIRCLES, plural, ' +
      'one {{GENDER, select, ' +
      '  female {{WHO} added you to her circle} ' +
      '  other  {{WHO} added you to his circle}}} ' +
      'other {{GENDER, select, ' +
      '  female {{WHO} added you to her # circles} ' +
      '  other  {{WHO} added you to his # circles}}}}');

  assertEquals('Jelena added you to her circle',
               fmt.format({'GENDER': 'female',
                           'WHO': 'Jelena',
                           'CIRCLES': 1}));
  assertEquals('Milan added you to his 1,234 circles',
               fmt.format({'GENDER': 'male',
                           'WHO': 'Milan',
                           'CIRCLES': 1234}));
}

function testPluralNestedInSelect() {
  // Added offset just for testing purposes. It doesn't make sense
  // to have it otherwise.
  var fmt = new goog.i18n.MessageFormat('{GENDER, select, ' +
      'female {{NUM_GROUPS, plural, ' +
      '  one {{WHO} added you to her group} ' +
      '  other {{WHO} added you to her # groups}}} ' +
      'other {{NUM_GROUPS, plural, offset:1' +
      '  one {{WHO} added you to his group} ' +
      '  other {{WHO} added you to his # groups}}}}');

  assertEquals('Jelena added you to her group',
               fmt.format({'GENDER': 'female',
                           'WHO': 'Jelena',
                           'NUM_GROUPS': 1}));
  assertEquals('Milan added you to his 1,233 groups',
               fmt.format({'GENDER': 'male',
                           'WHO': 'Milan',
                           'NUM_GROUPS': 1234}));
}

function testLiteralOpenCurlyBrace() {
  var fmt = new goog.i18n.MessageFormat("Anna's house" +
      " has '{0} and # in the roof' and {NUM_COWS} cows.");
  assertEquals("Anna's house has {0} and # in the roof and 5 cows.",
               fmt.format({'NUM_COWS': '5'}));
}

function testLiteralClosedCurlyBrace() {
  var fmt = new goog.i18n.MessageFormat("Anna's house" +
      " has '{'0'} and # in the roof' and {NUM_COWS} cows.");
  assertEquals("Anna's house has {0} and # in the roof and 5 cows.",
               fmt.format({'NUM_COWS': '5'}));
}

function testLiteralPoundSign() {
  var fmt = new goog.i18n.MessageFormat("Anna's house" +
      " has '{0}' and '# in the roof' and {NUM_COWS} cows.");
  assertEquals("Anna's house has {0} and # in the roof and 5 cows.",
               fmt.format({'NUM_COWS': '5'}));
}

function testNoLiteralsForSingleQuotes() {
  var fmt = new goog.i18n.MessageFormat("Anna's house" +
      " 'has {NUM_COWS} cows'.");
  assertEquals("Anna's house 'has 5 cows'.", fmt.format({'NUM_COWS': '5'}));
}

function testConsecutiveSingleQuotesAreReplacedWithOneSingleQuote() {
  var fmt = new goog.i18n.MessageFormat("Anna''s house a'{''''b'");
  assertEquals("Anna's house a{''b", fmt.format({}));
}

function testConsecutiveSingleQuotesBeforeSpecialCharDontCreateLiteral() {
  var fmt = new goog.i18n.MessageFormat("a''{NUM_COWS}'b");
  assertEquals("a'5'b", fmt.format({'NUM_COWS': '5'}));
}

function testSerbianSimpleSelect() {
  stubs.set(goog.i18n.pluralRules, 'select', goog.i18n.pluralRules.beSelect_);

  var fmt = new goog.i18n.MessageFormat('{GENDER, select, ' +
      'female {Njen} other {Njegov}} bicikl je ' +
      '{GENDER, select, female {crven} other {plav}}.');

  assertEquals('Njegov bicikl je plav.', fmt.format({'GENDER': 'male'}));
  assertEquals('Njen bicikl je crven.', fmt.format({'GENDER': 'female'}));
}

function testSerbianSimplePlural() {
  stubs.set(goog.i18n.pluralRules, 'select', goog.i18n.pluralRules.beSelect_);

  var fmt = new goog.i18n.MessageFormat('Ja {NUM_PEOPLE, plural, offset:1 ' +
      '=0 {ne vidim nikoga} ' +
      '=1 {vidim {PERSON}} ' +
      'one {vidim {PERSON} i jos # osobu} ' +
      'few {vidim {PERSON} i jos # osobe} ' +
      'many {vidim {PERSON} i jos # osoba} ' +
      'other {{PERSON} i jos # osoba}} ' +
      'u {PLACE}.');

  assertEquals('Ja ne vidim nikoga u Beogradu.',
               fmt.format({'NUM_PEOPLE': 0,
                           'PLACE': 'Beogradu'}));
  assertEquals('Ja vidim Markusa u Berlinu.',
               fmt.format({'NUM_PEOPLE': 1,
                           'PERSON': 'Markusa',
                           'PLACE': 'Berlinu'}));
  assertEquals('Ja vidim Marka i jos 1 osobu u Atini.',
               fmt.format({'NUM_PEOPLE': 2,
                           'PERSON': 'Marka',
                           'PLACE': 'Atini'}));
  assertEquals('Ja vidim Petra i jos 3 osobe u muzeju.',
               fmt.format({'NUM_PEOPLE': 4,
                           'PERSON': 'Petra',
                           'PLACE': 'muzeju'}));
  assertEquals('Ja vidim Cibua i jos 99 osoba u bazenu.',
               fmt.format({'NUM_PEOPLE': 100,
                           'PERSON': 'Cibua',
                           'PLACE': 'bazenu'}));
}

function testSerbianSimplePluralNoOffset() {
  stubs.set(goog.i18n.pluralRules, 'select', goog.i18n.pluralRules.beSelect_);

  var fmt = new goog.i18n.MessageFormat('Ja {NUM_PEOPLE, plural, ' +
      '=0 {ne vidim nikoga} ' +
      '=1 {vidim {PERSON}} ' +
      'one {vidim {PERSON} i jos # osobu} ' +
      'few {vidim {PERSON} i jos # osobe} ' +
      'many {vidim {PERSON} i jos # osoba} ' +
      'other {{PERSON} i jos # osoba}} ' +
      'u {PLACE}.');

  assertEquals('Ja ne vidim nikoga u Beogradu.',
               fmt.format({'NUM_PEOPLE': 0,
                           'PLACE': 'Beogradu'}));
  assertEquals('Ja vidim Markusa u Berlinu.',
               fmt.format({'NUM_PEOPLE': 1,
                           'PERSON': 'Markusa',
                           'PLACE': 'Berlinu'}));
  assertEquals('Ja vidim Marka i jos 21 osobu u Atini.',
               fmt.format({'NUM_PEOPLE': 21,
                           'PERSON': 'Marka',
                           'PLACE': 'Atini'}));
  assertEquals('Ja vidim Petra i jos 3 osobe u muzeju.',
               fmt.format({'NUM_PEOPLE': 3,
                           'PERSON': 'Petra',
                           'PLACE': 'muzeju'}));
  assertEquals('Ja vidim Cibua i jos 100 osoba u bazenu.',
               fmt.format({'NUM_PEOPLE': 100,
                           'PERSON': 'Cibua',
                           'PLACE': 'bazenu'}));
}

function testSerbianSelectNestedInPlural() {
  stubs.set(goog.i18n.pluralRules, 'select', goog.i18n.pluralRules.beSelect_);
  stubs.set(goog.i18n, 'NumberFormatSymbols', goog.i18n.NumberFormatSymbols_hr);

  var fmt = new goog.i18n.MessageFormat('{CIRCLES, plural, ' +
      'one {{GENDER, select, ' +
      '  female {{WHO} vas je dodala u njen # kruzok} ' +
      '  other  {{WHO} vas je dodao u njegov # kruzok}}} ' +
      'few {{GENDER, select, ' +
      '  female {{WHO} vas je dodala u njena # kruzoka} ' +
      '  other  {{WHO} vas je dodao u njegova # kruzoka}}} ' +
      'many {{GENDER, select, ' +
      '  female {{WHO} vas je dodala u njenih # kruzoka} ' +
      '  other  {{WHO} vas je dodao u njegovih # kruzoka}}} ' +
      'other {{GENDER, select, ' +
      '  female {{WHO} vas je dodala u njenih # kruzoka} ' +
      '  other  {{WHO} vas je dodao u njegovih # kruzoka}}}}');

  assertEquals('Jelena vas je dodala u njen 21 kruzok',
               fmt.format({'GENDER': 'female',
                           'WHO': 'Jelena',
                           'CIRCLES': 21}));
  assertEquals('Jelena vas je dodala u njena 3 kruzoka',
               fmt.format({'GENDER': 'female',
                           'WHO': 'Jelena',
                           'CIRCLES': 3}));
  assertEquals('Jelena vas je dodala u njenih 5 kruzoka',
               fmt.format({'GENDER': 'female',
                           'WHO': 'Jelena',
                           'CIRCLES': 5}));
  assertEquals('Milan vas je dodao u njegovih 1.235 kruzoka',
               fmt.format({'GENDER': 'male',
                           'WHO': 'Milan',
                           'CIRCLES': 1235}));
}

function testFallbackToOtherOptionInPlurals() {
  // Use Arabic plural rules since they have all six cases.
  // Only locale and numbers matter, the actual language of the message
  // does not.
  stubs.set(goog.i18n.pluralRules, 'select', goog.i18n.pluralRules.arSelect_);

  var fmt = new goog.i18n.MessageFormat('{NUM_MINUTES, plural, ' +
      'other {# minutes}}');

  // These numbers exercise all cases for the arabic plural rules.
  assertEquals('0 minutes', fmt.format({'NUM_MINUTES': 0}));
  assertEquals('1 minutes', fmt.format({'NUM_MINUTES': 1}));
  assertEquals('2 minutes', fmt.format({'NUM_MINUTES': 2}));
  assertEquals('3 minutes', fmt.format({'NUM_MINUTES': 3}));
  assertEquals('11 minutes', fmt.format({'NUM_MINUTES': 11}));
  assertEquals('1.5 minutes', fmt.format({'NUM_MINUTES': 1.5}));
}

function testPoundShowsNumberMinusOffsetInAllCases() {
  var fmt = new goog.i18n.MessageFormat('{SOME_NUM, plural, offset:1 ' +
      '=0 {#} =1 {#} =2 {#}one {#} other {#}}');

  assertEquals('-1', fmt.format({'SOME_NUM': '0'}));
  assertEquals('0', fmt.format({'SOME_NUM': '1'}));
  assertEquals('1', fmt.format({'SOME_NUM': '2'}));
  assertEquals('20', fmt.format({'SOME_NUM': '21'}));
}

function testSpecialCharactersInParamaterDontChangeFormat() {
  var fmt = new goog.i18n.MessageFormat('{SOME_NUM, plural,' +
      'other {# {GROUP}}}');

  // Test pound sign.
  assertEquals('10 group#1',
               fmt.format({'SOME_NUM': '10', 'GROUP': 'group#1'}));
  // Test other special characters in parameters, like { and }.
  assertEquals('10 } {', fmt.format({'SOME_NUM': '10', 'GROUP': '} {'}));
}

function testMissingOrInvalidPluralParameter() {
  var fmt = new goog.i18n.MessageFormat('{SOME_NUM, plural,' +
      'other {result}}');

  // Key name doesn't match A != SOME_NUM.
  assertEquals('Undefined or invalid parameter - SOME_NUM',
               fmt.format({A: '10'}));

  // Value is not a number.
  assertEquals('Undefined or invalid parameter - SOME_NUM',
               fmt.format({'SOME_NUM': 'Value'}));
}

function testMissingSelectParameter() {
  var fmt = new goog.i18n.MessageFormat('{GENDER, select,' +
      'other {result}}');

  // Key name doesn't match A != GENDER.
  assertEquals('Undefined parameter - GENDER',
               fmt.format({A: 'female'}));
}

function testMissingSimplePlaceholder() {
  var fmt = new goog.i18n.MessageFormat('{result}');

  // Key name doesn't match A != result.
  assertEquals('Undefined parameter - result',
               fmt.format({A: 'none'}));
}

function testPluralWithIgnorePound() {
  var fmt = new goog.i18n.MessageFormat('{SOME_NUM, plural,' +
      'other {# {GROUP}}}');

  // Test pound sign.
  assertEquals('# group#1',
               fmt.formatIgnoringPound({'SOME_NUM': '10', 'GROUP': 'group#1'}));
  // Test other special characters in parameters, like { and }.
  assertEquals('# } {',
      fmt.formatIgnoringPound({'SOME_NUM': '10', 'GROUP': '} {'}));
}

function testSimplePluralWithIgnorePound() {
  var fmt = new goog.i18n.MessageFormat(
      'I see {NUM_PEOPLE, plural, offset:1 ' +
      '=0 {no one at all in {PLACE}.} ' +
      '=1 {{PERSON} in {PLACE}.} ' +
      'one {{PERSON} and one other person in {PLACE}.} ' +
      'other {{PERSON} and # other people in {PLACE}.}}');

  assertEquals('I see Cibu and # other people in the cubes.',
               fmt.formatIgnoringPound({'NUM_PEOPLE': 100,
                           'PERSON': 'Cibu',
                           'PLACE': 'the cubes'}));
}

function testSimpleOrdinal() {
  var fmt = new goog.i18n.MessageFormat('{NUM_FLOOR, selectordinal, ' +
      'one {Take the elevator to the #st floor.}' +
      'two {Take the elevator to the #nd floor.}' +
      'few {Take the elevator to the #rd floor.}' +
      'other {Take the elevator to the #th floor.}}');

  assertEquals('Take the elevator to the 1st floor.',
               fmt.format({'NUM_FLOOR': 1}));
  assertEquals('Take the elevator to the 2nd floor.',
               fmt.format({'NUM_FLOOR': 2}));
  assertEquals('Take the elevator to the 3rd floor.',
               fmt.format({'NUM_FLOOR': 3}));
  assertEquals('Take the elevator to the 4th floor.',
               fmt.format({'NUM_FLOOR': 4}));
  assertEquals('Take the elevator to the 23rd floor.',
               fmt.format({'NUM_FLOOR': 23}));
  // Esoteric example.
  assertEquals('Take the elevator to the 0th floor.',
               fmt.format({'NUM_FLOOR': 0}));
}

function testOrdinalWithNegativeValue() {
  var fmt = new goog.i18n.MessageFormat('{NUM_FLOOR, selectordinal, ' +
      'one {Take the elevator to the #st floor.}' +
      'two {Take the elevator to the #nd floor.}' +
      'few {Take the elevator to the #rd floor.}' +
      'other {Take the elevator to the #th floor.}}');

  try {
    fmt.format({'NUM_FLOOR': -2});
  } catch (e) {
    assertEquals('Assertion failed: Argument index smaller than offset.',
                 e.message);
    return;
  }
  fail('Expected an error to be thrown');
}

function testSimpleOrdinalWithIgnorePound() {
  var fmt = new goog.i18n.MessageFormat('{NUM_FLOOR, selectordinal, ' +
      'one {Take the elevator to the #st floor.}' +
      'two {Take the elevator to the #nd floor.}' +
      'few {Take the elevator to the #rd floor.}' +
      'other {Take the elevator to the #th floor.}}');

  assertEquals('Take the elevator to the #th floor.',
               fmt.formatIgnoringPound({'NUM_FLOOR': 100}));
}

function testMissingOrInvalidOrdinalParameter() {
  var fmt = new goog.i18n.MessageFormat('{SOME_NUM, selectordinal,' +
      'other {result}}');

  // Key name doesn't match A != SOME_NUM.
  assertEquals('Undefined or invalid parameter - SOME_NUM',
               fmt.format({A: '10'}));

  // Value is not a number.
  assertEquals('Undefined or invalid parameter - SOME_NUM',
               fmt.format({'SOME_NUM': 'Value'}));
}
</script>
</body>
</html>