Skip to content

Commit 44d6405

Browse files
committed
Add unit test
1 parent a9302a9 commit 44d6405

File tree

1 file changed

+57
-8
lines changed

1 file changed

+57
-8
lines changed

inflection/test/src/inflection/message2/MF2FactoryTest.cpp

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,86 @@
1010
#include "unicode/locid.h"
1111
#include "unicode/messageformat2.h"
1212
#include "unicode/messageformat2_function_registry.h"
13-
13+
#include "unicode/messageformat2_formattable.h"
1414

1515
using icu::Locale;
16+
using icu::UnicodeString;
17+
using icu::message2::Formattable;
1618
using icu::message2::data_model::FunctionName;
1719
using icu::message2::MFFunctionRegistry;
20+
using icu::message2::MessageArguments;
1821
using icu::message2::MessageFormatter;
1922

2023

21-
MFFunctionRegistry SetupCustomBuilder(UErrorCode& errorCode) {
24+
TEST_CASE("MF2Factory#testBasic")
25+
{
26+
UErrorCode errorCode = U_ZERO_ERROR;
2227
auto functionName = FunctionName("inflection");
23-
return MFFunctionRegistry::Builder(errorCode)
28+
MFFunctionRegistry::Builder(errorCode)
2429
.adoptFormatter(functionName, inflection::message2::createFormatterFactory(), errorCode)
2530
.adoptSelector(functionName, inflection::message2::createSelectorFactory(), errorCode)
2631
.build();
32+
REQUIRE(U_SUCCESS(errorCode));
2733
}
2834

29-
TEST_CASE("MF2Factory#testBasic")
35+
TEST_CASE("MF2Factory#testCreateFormatter")
3036
{
3137
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);
3352
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");
3460
}
3561

36-
TEST_CASE("MF2Factory#testCreateFormatter")
62+
TEST_CASE("MF2Factory#testCreateSelector")
3763
{
3864
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+
3971
UParseError pe;
4072
MessageFormatter mf = MessageFormatter::Builder(errorCode)
41-
.setFunctionRegistry(SetupCustomBuilder(errorCode))
73+
.setFunctionRegistry(customRegistry)
4274
.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)
4486
.build(errorCode);
4587
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");
4695
}

0 commit comments

Comments
 (0)