|
10 | 10 | #include "unicode/locid.h" |
11 | 11 | #include "unicode/messageformat2.h" |
12 | 12 | #include "unicode/messageformat2_function_registry.h" |
13 | | - |
| 13 | +#include "unicode/messageformat2_formattable.h" |
14 | 14 |
|
15 | 15 | using icu::Locale; |
| 16 | +using icu::UnicodeString; |
| 17 | +using icu::message2::Formattable; |
16 | 18 | using icu::message2::data_model::FunctionName; |
17 | 19 | using icu::message2::MFFunctionRegistry; |
| 20 | +using icu::message2::MessageArguments; |
18 | 21 | using icu::message2::MessageFormatter; |
19 | 22 |
|
20 | 23 |
|
21 | | -MFFunctionRegistry SetupCustomBuilder(UErrorCode& errorCode) { |
| 24 | +TEST_CASE("MF2Factory#testBasic") |
| 25 | +{ |
| 26 | + UErrorCode errorCode = U_ZERO_ERROR; |
22 | 27 | auto functionName = FunctionName("inflection"); |
23 | | - return MFFunctionRegistry::Builder(errorCode) |
| 28 | + MFFunctionRegistry::Builder(errorCode) |
24 | 29 | .adoptFormatter(functionName, inflection::message2::createFormatterFactory(), errorCode) |
25 | 30 | .adoptSelector(functionName, inflection::message2::createSelectorFactory(), errorCode) |
26 | 31 | .build(); |
| 32 | + REQUIRE(U_SUCCESS(errorCode)); |
27 | 33 | } |
28 | 34 |
|
29 | | -TEST_CASE("MF2Factory#testBasic") |
| 35 | +TEST_CASE("MF2Factory#testCreateFormatter") |
30 | 36 | { |
31 | 37 | UErrorCode errorCode = U_ZERO_ERROR; |
32 | | - auto customRegistry = SetupCustomBuilder(errorCode); |
| 38 | + auto functionName = FunctionName("inflection"); |
| 39 | + auto customRegistry = MFFunctionRegistry::Builder(errorCode) |
| 40 | + .adoptFormatter(functionName, inflection::message2::createFormatterFactory(), errorCode) |
| 41 | + .build(); |
| 42 | + REQUIRE(U_SUCCESS(errorCode)); |
| 43 | + |
| 44 | + UParseError pe; |
| 45 | + MessageFormatter mf = MessageFormatter::Builder(errorCode) |
| 46 | + .setFunctionRegistry(customRegistry) |
| 47 | + .setLocale(Locale::forLanguageTag("es-MX", errorCode)) |
| 48 | + .setPattern( |
| 49 | + "Foo {$name :inflection hello=world definiteness=definite number=plural gender=feminine} Bar", |
| 50 | + pe, errorCode) |
| 51 | + .build(errorCode); |
33 | 52 | REQUIRE(U_SUCCESS(errorCode)); |
| 53 | + |
| 54 | + std::map<UnicodeString, Formattable> arguments; |
| 55 | + arguments["name"]= Formattable("gato"); |
| 56 | + |
| 57 | + UnicodeString ret = mf.formatToString(MessageArguments(arguments, errorCode), errorCode); |
| 58 | + REQUIRE(U_SUCCESS(errorCode)); |
| 59 | + REQUIRE(ret == u"Foo las gatas Bar"); |
34 | 60 | } |
35 | 61 |
|
36 | | -TEST_CASE("MF2Factory#testCreateFormatter") |
| 62 | +TEST_CASE("MF2Factory#testCreateSelector") |
37 | 63 | { |
38 | 64 | UErrorCode errorCode = U_ZERO_ERROR; |
| 65 | + auto functionName = FunctionName("inflection"); |
| 66 | + auto customRegistry = MFFunctionRegistry::Builder(errorCode) |
| 67 | + .adoptSelector(functionName, inflection::message2::createSelectorFactory(), errorCode) |
| 68 | + .build(); |
| 69 | + REQUIRE(U_SUCCESS(errorCode)); |
| 70 | + |
39 | 71 | UParseError pe; |
40 | 72 | MessageFormatter mf = MessageFormatter::Builder(errorCode) |
41 | | - .setFunctionRegistry(SetupCustomBuilder(errorCode)) |
| 73 | + .setFunctionRegistry(customRegistry) |
42 | 74 | .setLocale(Locale::forLanguageTag("es-MX", errorCode)) |
43 | | - .setPattern("Foo {$name :inflection hello=world definiteness=definite number=plural gender=feminine} Bar", pe, errorCode) |
| 75 | + .setPattern( |
| 76 | + ".local $var1 = {$name :inflection feature=gender} \ |
| 77 | + .local $var2 = {$name :inflection feature=number} \ |
| 78 | + .match $var1 $var2\ |
| 79 | + masculine 2 {{{$name} is Masculine & 2}} \ |
| 80 | + feminine singular {{{$name} is Feminine & Singular}} \ |
| 81 | + foo 4 {{{$name} is Foo & 4}} \ |
| 82 | + masculine singular {{{$name} is Masculine & Singular}} \ |
| 83 | + hello singular {{{$name} is Hello & Singular}} \ |
| 84 | + * * {{{$name} is other}}\n", |
| 85 | + pe, errorCode) |
44 | 86 | .build(errorCode); |
45 | 87 | REQUIRE(U_SUCCESS(errorCode)); |
| 88 | + |
| 89 | + std::map<UnicodeString, Formattable> arguments; |
| 90 | + arguments["name"]= Formattable("gato"); |
| 91 | + |
| 92 | + UnicodeString ret = mf.formatToString(MessageArguments(arguments, errorCode), errorCode); |
| 93 | + REQUIRE(U_SUCCESS(errorCode)); |
| 94 | + REQUIRE(ret == u"gato is Masculine & Singular"); |
46 | 95 | } |
0 commit comments