2 * Copyright 2014 Haiku, Inc.
3 * Distributed under the terms of the MIT License.
7 #include "MessageFormatTest.h"
10 #include <MessageFormat.h>
12 #include <cppunit/TestCaller.h>
13 #include <cppunit/TestSuite.h>
16 MessageFormatTest::MessageFormatTest()
21 MessageFormatTest::~MessageFormatTest()
27 MessageFormatTest::TestFormat()
38 static const char* polishTemplate
= "{0, plural, one{Wybrano # obiekt} "
39 "few{Wybrano # obiekty} many{Wybrano # obiektów} "
40 "other{Wybrano # obyektu}}";
42 // There are 4 rules in russian: one (1, 21, ...), few (2-4, 22-24, ...),
43 // many (anything else), and other (non-integer numbers). When formatting
44 // integers only, either both many and other must be there (with other
45 // not being used), or one/few/other must be used.
46 static const char* russianTemplate
= "{0, plural, one{# объект} "
47 "few{# объекта} other{# объектов}}";
49 static const Test tests
[] = {
50 {"en_US", "A QA engineer walks into a bar.", 0,
51 "A QA engineer walks into a bar."},
52 {"en_US", "Orders {0, plural, one{# beer} other{# beers}}.", 1,
54 {"en_US", "Orders {0, plural, one{# beer} other{# beers}}.", 0,
56 {"en_US", "Orders {0, plural, one{# beer} other{# beers}}.", 99999999,
57 "Orders 99,999,999 beers."},
58 {"en_US", "Orders {0, plural, one{# beer} other{# beers}}.", -INT_MAX
,
59 "Orders -2,147,483,647 beers."},
60 {"en_US", "Orders {0, plural, one{# beer} other{# beers}}.", -1,
62 {"en_US", "Orders {0, plural, one{a lizard} other{more lizards}}.", 1,
64 {"en_US", "Orders {0, plural, one{a lizard} other{more lizards}}.", 2,
65 "Orders more lizards."},
66 {"en_US", "Orders {0, plural, one{# \x8A} other{# \x02}}.", 2,
68 {"fr_FR", "Commande {0, plural, one{# bière} other{# bières}}.",
69 99999999, "Commande 99 999 999 bières."},
70 {"pl_PL", polishTemplate
, 1, "Wybrano 1 obiekt"},
71 {"pl_PL", polishTemplate
, 3, "Wybrano 3 obiekty"},
72 {"pl_PL", polishTemplate
, 5, "Wybrano 5 obiektów"},
73 {"pl_PL", polishTemplate
, 23, "Wybrano 23 obiekty"},
74 {"ru_RU", russianTemplate
, 1, "1 объект"},
75 {"ru_RU", russianTemplate
, 2, "2 объекта"},
76 {"ru_RU", russianTemplate
, 5, "5 объектов"},
80 for (int i
= 0; tests
[i
].pattern
!= NULL
; i
++) {
84 BLanguage
language(tests
[i
].locale
);
85 BMessageFormat
formatter(language
, tests
[i
].pattern
);
87 result
= formatter
.Format(output
, tests
[i
].number
);
88 CPPUNIT_ASSERT_EQUAL(B_OK
, result
);
89 CPPUNIT_ASSERT_EQUAL(BString(tests
[i
].expected
), output
);
95 MessageFormatTest::TestBogus()
101 static const Test tests
[] = {
102 { "{0, plural, one{# dog} other{# dogs}" }, // Missing closing brace
103 { "{0, plural, one{# dog}, other{# dogs}}" }, // Extra comma
104 { "{0, plural, one{# dog}" }, // Missing "other"
105 //{ "{4099, plural, one{# dog} other{# dogs}}" }, // Out of bounds arg
106 { "{0, invalid, one{# dog} other{# dogs}}" }, // Invalid rule
110 for (int i
= 0; tests
[i
].pattern
!= NULL
; i
++) {
116 BMessageFormat
formatter(tests
[i
].pattern
);
117 CPPUNIT_ASSERT(formatter
.InitCheck() != B_OK
);
119 result
= formatter
.Format(output
, 1);
120 CPPUNIT_ASSERT(result
!= B_OK
);
122 result
= formatter
.Format(output
, 2);
123 CPPUNIT_ASSERT(result
!= B_OK
);
129 MessageFormatTest::AddTests(BTestSuite
& parent
)
131 CppUnit::TestSuite
& suite
= *new CppUnit::TestSuite("MessageFormatTest");
133 suite
.addTest(new CppUnit::TestCaller
<MessageFormatTest
>(
134 "MessageFormatTest::TestFormat", &MessageFormatTest::TestFormat
));
135 suite
.addTest(new CppUnit::TestCaller
<MessageFormatTest
>(
136 "MessageFormatTest::TestBogus", &MessageFormatTest::TestBogus
));
138 parent
.addTest("MessageFormatTest", &suite
);