File: messageformat2test_icu.cpp

package info (click to toggle)
icu 78.2-1
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 123,992 kB
  • sloc: cpp: 527,891; ansic: 112,789; sh: 4,983; makefile: 4,657; perl: 3,199; python: 2,933; xml: 749; sed: 36; lisp: 12
file content (164 lines) | stat: -rw-r--r-- 6,885 bytes parent folder | download
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
// © 2024 and later: Unicode, Inc. and others.
// License & terms of use: https://www.unicode.org/copyright.html

#include "unicode/utypes.h"

#if !UCONFIG_NO_NORMALIZATION

#if !UCONFIG_NO_FORMATTING

#if !UCONFIG_NO_MF2

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

using namespace icu::message2;

/*
  Tests based on ICU4J's Mf2IcuTest.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::testSample(TestCase::Builder& testBuilder, IcuTestErrorCode& errorCode) {
    TestUtils::runTestCase(*this, testBuilder.setPattern("There are {$count} files on {$where}")
                                .setArgument("count", "abc")
                                .setArgument("where", "def")
                                .setExpected("There are abc files on def")
                                .build(), errorCode);
}

void TestMessageFormat2::testStaticFormat(TestCase::Builder& testBuilder, IcuTestErrorCode& errorCode) {
    TestUtils::runTestCase(*this, testBuilder.setPattern("At {$when :time style=medium} on {$when :date style=medium}, \
there was {$what} on planet {$planet :integer}.")
                                .setArgument("planet", (int64_t) 7)
                                .setDateArgument("when", (UDate) 871068000000)
                                .setArgument("what", "a disturbance in the Force")
                                .setExpected(CharsToUnicodeString("At 12:20:00\\u202FPM on Aug 8, 1997, there was a disturbance in the Force on planet 7."))
                                .build(), errorCode);
}

void TestMessageFormat2::testSimpleFormat(TestCase::Builder& testBuilder, IcuTestErrorCode& errorCode) {
    testBuilder.setPattern("The disk \"{$diskName}\" contains {$fileCount} file(s).");
    testBuilder.setArgument("diskName", "MyDisk");


    TestCase test = testBuilder.setArgument("fileCount", (int64_t) 0)
        .setExpected("The disk \"MyDisk\" contains 0 file(s).")
        .build();
    TestUtils::runTestCase(*this, test, errorCode);

    test = testBuilder.setArgument("fileCount", (int64_t) 1)
                      .setExpected("The disk \"MyDisk\" contains 1 file(s).")
                      .build();
    TestUtils::runTestCase(*this, test, errorCode);

    test = testBuilder.setArgument("fileCount", (int64_t) 12)
                      .setExpected("The disk \"MyDisk\" contains 12 file(s).")
                      .build();
    TestUtils::runTestCase(*this, test, errorCode);
}

void TestMessageFormat2::testSelectFormatToPattern(TestCase::Builder& testBuilder, IcuTestErrorCode& errorCode) {
    UnicodeString pattern = CharsToUnicodeString(".match {$userGender :string}\n\
                 female {{{$userName} est all\\u00E9e \\u00E0 Paris.}}\n\
                 *     {{{$userName} est all\\u00E9 \\u00E0 Paris.}}");

    testBuilder.setPattern(pattern);

    TestCase test = testBuilder.setArgument("userName", "Charlotte")
                                .setArgument("userGender", "female")
                                .setExpected(CharsToUnicodeString("Charlotte est all\\u00e9e \\u00e0 Paris."))
                                .build();
    TestUtils::runTestCase(*this, test, errorCode);

    test = testBuilder.setArgument("userName", "Guillaume")
                                .setArgument("userGender", "male")
                                .setExpected(CharsToUnicodeString("Guillaume est all\\u00e9 \\u00e0 Paris."))
                                .build();
    TestUtils::runTestCase(*this, test, errorCode);

    test = testBuilder.setArgument("userName", "Dominique")
                                .setArgument("userGender", "unknown")
                                .setExpected(CharsToUnicodeString("Dominique est all\\u00e9 \\u00e0 Paris."))
                                .build();
    TestUtils::runTestCase(*this, test, errorCode);
}

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

    UDate testDate = UDate(1671782400000); // 2022-12-23
    UnicodeString user = "John";
    UnicodeString badArgumentsNames[] = {
        "userX", "todayX"
    };
    UnicodeString goodArgumentsNames[] = {
        "user", "today"
    };
    icu::Formattable oldArgumentsValues[] = {
        icu::Formattable(user), icu::Formattable(testDate, icu::Formattable::kIsDate)
    };
    UnicodeString expectedGood = "Hello John, today is December 23, 2022.";

    LocalPointer<MessageFormat> mf1(new MessageFormat("Hello {user}, today is {today,date,long}.", errorCode));
    CHECK_ERROR(errorCode);

    UnicodeString result;
    mf1->format(badArgumentsNames, oldArgumentsValues, 2, result, errorCode);
    assertEquals("testMf1Behavior", (UBool) true, U_SUCCESS(errorCode));
    assertEquals("old icu test", "Hello {user}, today is {today}.", result);
    result.remove();
    mf1->format(goodArgumentsNames, oldArgumentsValues, 2, result, errorCode);
    assertEquals("testMf1Behavior", (UBool) true, U_SUCCESS(errorCode));
    assertEquals("old icu test", expectedGood, result);

    TestCase test = testBuilder.setPattern("Hello {$user}, today is {$today :date style=long}.")
                                .setArgument(badArgumentsNames[0], user)
                                .setDateArgument(badArgumentsNames[1], testDate)
                                .setExpected("Hello {$user}, today is {$today}.")
                                .setExpectedError(U_MF_UNRESOLVED_VARIABLE_ERROR)
                                .build();
    TestUtils::runTestCase(*this, test, errorCode);

    test = testBuilder.clearArguments()
                      .setExpectSuccess()
                      .setArgument(goodArgumentsNames[0], user)
                      .setDateArgument(goodArgumentsNames[1], testDate)
                      .setExpected(expectedGood)
                      .build();
    TestUtils::runTestCase(*this, test, errorCode);
}

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

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

    testSample(testBuilder, errorCode);
    testStaticFormat(testBuilder, errorCode);
    testSimpleFormat(testBuilder, errorCode);
    testSelectFormatToPattern(testBuilder, errorCode);
    testMf1Behavior(testBuilder, errorCode);
}

#endif /* #if !UCONFIG_NO_MF2 */

#endif /* #if !UCONFIG_NO_FORMATTING */

#endif /* #if !UCONFIG_NO_NORMALIZATION */