summaryrefslogtreecommitdiff
path: root/icu4c/source/test/intltest/messageformat2test_features.cpp
blob: f4c2ffdb82b7e4293deb187c3485d3a13bb01aed (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
472
473
474
475
476
477
478
// © 2024 and later: Unicode, Inc. and others.

#include "unicode/utypes.h"

#if !UCONFIG_NO_FORMATTING

#if !UCONFIG_NO_MF2

#include "unicode/gregocal.h"
#include "messageformat2test.h"

using namespace icu::message2;
using namespace data_model;

/*
  Tests based on ICU4J's MessageFormat2Test.java
and Mf2FeaturesTest.java
*/

/*
  TODO: Tests need to be unified in a single format that
  both ICU4C and ICU4J can use, rather than being embedded in code.
*/

/*
Tests reflect the syntax specified in

  https://github.com/unicode-org/message-format-wg/commits/main/spec/message.abnf

as of the following commit from 2023-05-09:
  https://github.com/unicode-org/message-format-wg/commit/194f6efcec5bf396df36a19bd6fa78d1fa2e0867

*/

void TestMessageFormat2::testEmptyMessage(TestCase::Builder& testBuilder, IcuTestErrorCode& errorCode) {
    TestUtils::runTestCase(*this, testBuilder.setPattern("")
                           .setExpected("")
                           .build(), errorCode);
}

void TestMessageFormat2::testPlainText(TestCase::Builder& testBuilder, IcuTestErrorCode& errorCode) {
    TestUtils::runTestCase(*this, testBuilder.setPattern("Hello World!")
                           .setExpected("Hello World!")
                           .build(), errorCode);
}

void TestMessageFormat2::testPlaceholders(TestCase::Builder& testBuilder, IcuTestErrorCode& errorCode) {
    TestUtils::runTestCase(*this, testBuilder.setPattern("Hello, {$userName}!")
                                .setExpected("Hello, John!")
                                .setArgument("userName", "John")
                                .build(), errorCode);
}

void TestMessageFormat2::testArgumentMissing(TestCase::Builder& testBuilder, IcuTestErrorCode& errorCode) {
    CHECK_ERROR(errorCode);

    UnicodeString message = "Hello {$name}, today is {$today :date style=long}.";
    LocalPointer<Calendar> cal(Calendar::createInstance(errorCode));
    CHECK_ERROR(errorCode);

    // November 23, 2022 at 7:42:37.123 PM
    cal->set(2022, Calendar::NOVEMBER, 23, 19, 42, 37);
    UDate TEST_DATE = cal->getTime(errorCode);
    CHECK_ERROR(errorCode);

    TestCase test = testBuilder.setPattern(message)
        .clearArguments()
        .setArgument("name", "John")
        .setDateArgument("today", TEST_DATE)
        .setExpected("Hello John, today is November 23, 2022.")
        .build();
    TestUtils::runTestCase(*this, test, errorCode);

    // Missing date argument
    test = testBuilder.setPattern(message)
                                .clearArguments()
                                .setArgument("name", "John")
                                .setExpected("Hello John, today is {$today}.")
                                .setExpectedError(U_MF_UNRESOLVED_VARIABLE_ERROR)
                                .build();
    TestUtils::runTestCase(*this, test, errorCode);
    test = testBuilder.setPattern(message)
                                .clearArguments()
                                .setDateArgument("today", TEST_DATE)
                                .setExpectedError(U_MF_UNRESOLVED_VARIABLE_ERROR)
                                .setExpected("Hello {$name}, today is November 23, 2022.")
                                .build();
    TestUtils::runTestCase(*this, test, errorCode);

    // Both arguments missing
    test = testBuilder.setPattern(message)
                                .clearArguments()
                                .setExpectedError(U_MF_UNRESOLVED_VARIABLE_ERROR)
                                .setExpected("Hello {$name}, today is {$today}.")
                                .build();
    TestUtils::runTestCase(*this, test, errorCode);
}

void TestMessageFormat2::testDefaultLocale(TestCase::Builder& testBuilder, IcuTestErrorCode& errorCode) {
    CHECK_ERROR(errorCode);

    LocalPointer<Calendar> cal(Calendar::createInstance(errorCode));
    CHECK_ERROR(errorCode);
    // November 23, 2022 at 7:42:37.123 PM
    cal->set(2022, Calendar::NOVEMBER, 23, 19, 42, 37);
    UDate TEST_DATE = cal->getTime(errorCode);
    CHECK_ERROR(errorCode);

    UnicodeString message = "Date: {$date :date style=long}.";
    UnicodeString expectedEn = "Date: November 23, 2022.";
    UnicodeString expectedRo = "Date: 23 noiembrie 2022.";

    testBuilder.setPattern(message);

    TestCase test = testBuilder.clearArguments()
        .setDateArgument("date", TEST_DATE)
        .setExpected(expectedEn)
        .setExpectSuccess()
        .build();
    TestUtils::runTestCase(*this, test, errorCode);
    test = testBuilder.setExpected(expectedRo)
                                .setLocale(Locale("ro"))
                                .build();
    TestUtils::runTestCase(*this, test, errorCode);

    Locale originalLocale = Locale::getDefault();
    Locale::setDefault(Locale::forLanguageTag("ro", errorCode), errorCode);
    CHECK_ERROR(errorCode);

    test = testBuilder.setExpected(expectedEn)
                                .setLocale(Locale("en", "US"))
                                .build();
    TestUtils::runTestCase(*this, test, errorCode);
    test = testBuilder.setExpected(expectedRo)
                                .setLocale(Locale::forLanguageTag("ro", errorCode))
                                .build();
    TestUtils::runTestCase(*this, test, errorCode);

    Locale::setDefault(originalLocale, errorCode);
    CHECK_ERROR(errorCode);
}

void TestMessageFormat2::testSpecialPluralWithDecimals(TestCase::Builder& testBuilder, IcuTestErrorCode& errorCode) {
    CHECK_ERROR(errorCode);

    UnicodeString message;

    message = ".local $amount = {$count :number}\n\
                .match {$amount :number}\n\
                  1 {{I have {$amount} dollar.}}\n\
                  * {{I have {$amount} dollars.}}";

    TestCase test = testBuilder.setPattern(message)
        .clearArguments()
        .setArgument("count", (int64_t) 1)
        .setExpected("I have 1 dollar.")
        .setLocale(Locale("en", "US"))
        .build();
    TestUtils::runTestCase(*this, test, errorCode);
}

void TestMessageFormat2::testDefaultFunctionAndOptions(TestCase::Builder& testBuilder, IcuTestErrorCode& errorCode) {
    CHECK_ERROR(errorCode);

    LocalPointer<Calendar> cal(Calendar::createInstance(errorCode));
    CHECK_ERROR(errorCode);
    // November 23, 2022 at 7:42:37.123 PM
    cal->set(2022, Calendar::NOVEMBER, 23, 19, 42, 37);
    UDate TEST_DATE = cal->getTime(errorCode);
    CHECK_ERROR(errorCode);

    TestCase test = testBuilder.setPattern("Testing date formatting: {$date}.")
        .clearArguments()
        .setDateArgument("date", TEST_DATE)
        .setExpected("Testing date formatting: 23.11.2022, 19:42.")
        .setLocale(Locale("ro"))
        .build();
    TestUtils::runTestCase(*this, test, errorCode);
    test = testBuilder.setPattern("Testing date formatting: {$date :datetime}.")
                                .setExpected("Testing date formatting: 23.11.2022, 19:42.")
                                .setLocale(Locale("ro"))
                                .build();
    TestUtils::runTestCase(*this, test, errorCode);
}

void TestMessageFormat2::testSimpleSelection(TestCase::Builder& testBuilder, IcuTestErrorCode& errorCode) {
    (void) testBuilder;
    (void) errorCode;

    /* Covered by testPlural */
}

void TestMessageFormat2::testComplexSelection(TestCase::Builder& testBuilder, IcuTestErrorCode& errorCode) {
    CHECK_ERROR(errorCode);

    UnicodeString message = ".match {$photoCount :number} {$userGender :string}\n\
                  1 masculine {{{$userName} added a new photo to his album.}}\n\
                  1 feminine {{{$userName} added a new photo to her album.}}\n\
                  1 * {{{$userName} added a new photo to their album.}}\n\
                  * masculine {{{$userName} added {$photoCount} photos to his album.}}\n\
                  * feminine {{{$userName} added {$photoCount} photos to her album.}}\n\
                  * * {{{$userName} added {$photoCount} photos to their album.}}";
    testBuilder.setPattern(message);

    int64_t count = 1;
    TestCase test = testBuilder.clearArguments().setArgument("photoCount", count)
        .setArgument("userGender", "masculine")
        .setArgument("userName", "John")
        .setExpected("John added a new photo to his album.")
        .build();
    TestUtils::runTestCase(*this, test, errorCode);
    test = testBuilder.setArgument("userGender", "feminine")
                      .setArgument("userName", "Anna")
                      .setExpected("Anna added a new photo to her album.")
                      .build();
    TestUtils::runTestCase(*this, test, errorCode);
    test = testBuilder.setArgument("userGender", "unknown")
                      .setArgument("userName", "Anonymous")
                      .setExpected("Anonymous added a new photo to their album.")
                      .build();
    TestUtils::runTestCase(*this, test, errorCode);

    count = 13;
    test = testBuilder.clearArguments().setArgument("photoCount", count)
                                .setArgument("userGender", "masculine")
                                .setArgument("userName", "John")
                                .setExpected("John added 13 photos to his album.")
                                .build();
    TestUtils::runTestCase(*this, test, errorCode);
    test = testBuilder.setArgument("userGender", "feminine")
                      .setArgument("userName", "Anna")
                      .setExpected("Anna added 13 photos to her album.")
                      .build();
    TestUtils::runTestCase(*this, test, errorCode);
    test = testBuilder.setArgument("userGender", "unknown")
                      .setArgument("userName", "Anonymous")
                      .setExpected("Anonymous added 13 photos to their album.")
                      .build();
    TestUtils::runTestCase(*this, test, errorCode);
}

void TestMessageFormat2::testSimpleLocalVariable(TestCase::Builder& testBuilder, IcuTestErrorCode& errorCode) {
    CHECK_ERROR(errorCode);

    LocalPointer<Calendar> cal(Calendar::createInstance(errorCode));
    CHECK_ERROR(errorCode);
    // November 23, 2022 at 7:42:37.123 PM
    cal->set(2022, Calendar::NOVEMBER, 23, 19, 42, 37);
    UDate TEST_DATE = cal->getTime(errorCode);
    CHECK_ERROR(errorCode);

    testBuilder.setPattern(".input {$expDate :date style=medium}\n\
                            {{Your tickets expire on {$expDate}.}}");

    int64_t count = 1;
    TestUtils::runTestCase(*this, testBuilder.clearArguments().setArgument("count", count)
                      .setLocale(Locale("en"))
                      .setDateArgument("expDate", TEST_DATE)
                      .setExpected("Your tickets expire on Nov 23, 2022.")
                      .build(), errorCode);
}

void TestMessageFormat2::testLocalVariableWithSelect(TestCase::Builder& testBuilder, IcuTestErrorCode& errorCode) {
    CHECK_ERROR(errorCode);

    LocalPointer<Calendar> cal(Calendar::createInstance(errorCode));
    CHECK_ERROR(errorCode);
    // November 23, 2022 at 7:42:37.123 PM
    cal->set(2022, Calendar::NOVEMBER, 23, 19, 42, 37);
    UDate TEST_DATE = cal->getTime(errorCode);
    CHECK_ERROR(errorCode);

    testBuilder.setPattern(".input {$expDate :date style=medium}\n\
                .match {$count :number}\n\
                 1 {{Your ticket expires on {$expDate}.}}\n\
                 * {{Your {$count} tickets expire on {$expDate}.}}");

    int64_t count = 1;
    TestCase test = testBuilder.clearArguments().setArgument("count", count)
                      .setLocale(Locale("en"))
                      .setDateArgument("expDate", TEST_DATE)
                      .setExpected("Your ticket expires on Nov 23, 2022.")
                      .build();
    TestUtils::runTestCase(*this, test, errorCode);
    count = 3;
    test = testBuilder.setArgument("count", count)
                      .setExpected("Your 3 tickets expire on Nov 23, 2022.")
                      .build();
    TestUtils::runTestCase(*this, test, errorCode);
}

void TestMessageFormat2::testDateFormat(TestCase::Builder& testBuilder, IcuTestErrorCode& errorCode) {
    LocalPointer<Calendar> cal(Calendar::createInstance(errorCode));
    CHECK_ERROR(errorCode);

    cal->set(2022, Calendar::OCTOBER, 27, 0, 0, 0);
    UDate expiration = cal->getTime(errorCode);
    CHECK_ERROR(errorCode);

    TestCase test = testBuilder.clearArguments().setPattern("Your card expires on {$exp :date style=medium}!")
                                .setLocale(Locale("en"))
                                .setExpected("Your card expires on Oct 27, 2022!")
                                .setDateArgument("exp", expiration)
                                .build();
    TestUtils::runTestCase(*this, test, errorCode);

    test = testBuilder.clearArguments().setPattern("Your card expires on {$exp :date style=full}!")
                      .setExpected("Your card expires on Thursday, October 27, 2022!")
                      .setDateArgument("exp", expiration)
                      .build();
    TestUtils::runTestCase(*this, test, errorCode);

    test = testBuilder.clearArguments().setPattern("Your card expires on {$exp :date style=long}!")
                      .setExpected("Your card expires on October 27, 2022!")
                      .setDateArgument("exp", expiration)
                      .build();
    TestUtils::runTestCase(*this, test, errorCode);

    test = testBuilder.clearArguments().setPattern("Your card expires on {$exp :date style=medium}!")
                      .setExpected("Your card expires on Oct 27, 2022!")
                      .setDateArgument("exp", expiration)
                      .build();
    TestUtils::runTestCase(*this, test, errorCode);

    test = testBuilder.clearArguments().setPattern("Your card expires on {$exp :date style=short}!")
                      .setExpected("Your card expires on 10/27/22!")
                      .setDateArgument("exp", expiration)
                      .build();
    TestUtils::runTestCase(*this, test, errorCode);

/*
  This test would require the calendar to be passed as a UObject* with the datetime formatter
  doing an RTTI check -- however, that would be awkward, since it would have to check the tag for each
  possible subclass of `Calendar`. datetime currently has no support for formatting any object argument

    cal.adoptInstead(new GregorianCalendar(2022, Calendar::OCTOBER, 27, errorCode));
    if (cal.isValid()) {
        test = testBuilder.setPattern("Your card expires on {$exp :datetime skeleton=yMMMdE}!")
                          .setExpected("Your card expires on Thu, Oct 27, 2022!")
                          .setArgument("exp", cal.orphan(), errorCode)
                          .build();
        TestUtils::runTestCase(*this, test, errorCode);
    }
*/

    // Implied function based on type of the object to format
    test = testBuilder.clearArguments().setPattern("Your card expires on {$exp}!")
                      .setExpected(CharsToUnicodeString("Your card expires on 10/27/22, 12:00\\u202FAM!"))
                      .setDateArgument("exp", expiration)
                      .build();
    TestUtils::runTestCase(*this, test, errorCode);
}

void TestMessageFormat2::testPlural(TestCase::Builder& testBuilder, IcuTestErrorCode& errorCode) {
    UnicodeString message = ".match {$count :number}\n\
                 1 {{You have one notification.}}\n           \
                 * {{You have {$count} notifications.}}";

    int64_t count = 1;
    TestCase test = testBuilder.clearArguments().setPattern(message)
                                .setExpected("You have one notification.")
                                .setArgument("count", count)
                                .build();
    TestUtils::runTestCase(*this, test, errorCode);

    count = 42;
    test = testBuilder.clearArguments().setExpected("You have 42 notifications.")
                      .setArgument("count", count)
                      .build();
    TestUtils::runTestCase(*this, test, errorCode);

    count = 1;
    test = testBuilder.clearArguments().setPattern(message)
                      .setExpected("You have one notification.")
                      .setArgument("count", "1")
                      .build();
    TestUtils::runTestCase(*this, test, errorCode);

    count = 42;
    test = testBuilder.clearArguments().setExpected("You have 42 notifications.")
                      .setArgument("count", "42")
                      .build();
    TestUtils::runTestCase(*this, test, errorCode);
}

void TestMessageFormat2::testPluralOrdinal(TestCase::Builder& testBuilder, IcuTestErrorCode& errorCode) {
    UnicodeString message =  ".match {$place :number select=ordinal}\n\
                 1 {{You got the gold medal}}\n            \
                 2 {{You got the silver medal}}\n          \
                 3 {{You got the bronze medal}}\n\
                 one {{You got in the {$place}st place}}\n\
                 two {{You got in the {$place}nd place}}\n \
                 few {{You got in the {$place}rd place}}\n \
                 * {{You got in the {$place}th place}}";

    TestCase test = testBuilder.clearArguments().setPattern(message)
                                .setExpected("You got the gold medal")
                                .setArgument("place", "1")
                                .build();
    TestUtils::runTestCase(*this, test, errorCode);

    test = testBuilder.clearArguments().setExpected("You got the silver medal")
                          .setArgument("place", "2")
                          .build();
    TestUtils::runTestCase(*this, test, errorCode);

    test = testBuilder.clearArguments().setExpected("You got the bronze medal")
                          .setArgument("place", "3")
                          .build();
    TestUtils::runTestCase(*this, test, errorCode);

    test = testBuilder.clearArguments().setExpected("You got in the 21st place")
                          .setArgument("place", "21")
                          .build();
    TestUtils::runTestCase(*this, test, errorCode);

    test = testBuilder.clearArguments().setExpected("You got in the 32nd place")
                          .setArgument("place", "32")
                          .build();
    TestUtils::runTestCase(*this, test, errorCode);

    test = testBuilder.clearArguments().setExpected("You got in the 23rd place")
                          .setArgument("place", "23")
                          .build();
    TestUtils::runTestCase(*this, test, errorCode);

    test = testBuilder.clearArguments().setExpected("You got in the 15th place")
                          .setArgument("place", "15")
                          .build();
    TestUtils::runTestCase(*this, test, errorCode);
}

void TestMessageFormat2::testDeclareBeforeUse(TestCase::Builder& testBuilder, IcuTestErrorCode& errorCode) {

    UnicodeString message = ".local $foo = {$baz :number}\n\
                 .local $bar = {$foo}\n                    \
                 .local $baz = {$bar}\n                    \
                 {{The message uses {$baz} and works}}";
    testBuilder.setPattern(message);
    testBuilder.setName("declare-before-use");

    TestCase test = testBuilder.clearArguments().setExpected("The message uses {$baz} and works")
                                .setExpectedError(U_MF_DUPLICATE_DECLARATION_ERROR)
                                .build();
    TestUtils::runTestCase(*this, test, errorCode);
}


void TestMessageFormat2::featureTests() {
    IcuTestErrorCode errorCode(*this, "featureTests");

    TestCase::Builder testBuilder;
    testBuilder.setName("featureTests");

    testEmptyMessage(testBuilder, errorCode);
    testPlainText(testBuilder, errorCode);
    testPlaceholders(testBuilder, errorCode);
    testArgumentMissing(testBuilder, errorCode);
    testDefaultLocale(testBuilder, errorCode);
    testSpecialPluralWithDecimals(testBuilder, errorCode);
    testDefaultFunctionAndOptions(testBuilder, errorCode);
    testSimpleSelection(testBuilder, errorCode);
    testComplexSelection(testBuilder, errorCode);
    testSimpleLocalVariable(testBuilder, errorCode);
    testLocalVariableWithSelect(testBuilder, errorCode);

    testDateFormat(testBuilder, errorCode);
    testPlural(testBuilder, errorCode);
    testPluralOrdinal(testBuilder, errorCode);
    testDeclareBeforeUse(testBuilder, errorCode);
}

TestCase::~TestCase() {}
TestCase::Builder::~Builder() {}

#endif /* #if !UCONFIG_NO_MF2 */

#endif /* #if !UCONFIG_NO_FORMATTING */