1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 * Copyright (C) 2014-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *******************************************************************************
8 *
9 * File QUANTITYFORMATTERTEST.CPP
10 *
11 ********************************************************************************
12 */
13 #include "cstring.h"
14 #include "intltest.h"
15 #include "quantityformatter.h"
16 #include "unicode/simpleformatter.h"
17 #include "unicode/numfmt.h"
18 #include "unicode/plurrule.h"
19
20 #define ASSERT_OK(status) UPRV_BLOCK_MACRO_BEGIN { \
21 if(U_FAILURE(status)) { \
22 errcheckln(status, #status " = %s @ %s:%d", u_errorName(status), __FILE__, __LINE__); \
23 return; \
24 } \
25 } UPRV_BLOCK_MACRO_END
26
27 class QuantityFormatterTest : public IntlTest {
28 public:
QuantityFormatterTest()29 QuantityFormatterTest() {
30 }
31 void TestBasic();
32 void runIndexedTest(int32_t index, UBool exec, const char*& name, char* par = nullptr) override;
33
34 private:
35 };
36
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)37 void QuantityFormatterTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/) {
38 TESTCASE_AUTO_BEGIN;
39 TESTCASE_AUTO(TestBasic);
40 TESTCASE_AUTO_END;
41 }
42
TestBasic()43 void QuantityFormatterTest::TestBasic() {
44 UErrorCode status = U_ZERO_ERROR;
45 #if !UCONFIG_NO_FORMATTING
46 QuantityFormatter fmt;
47 assertFalse(
48 "adding bad variant",
49 fmt.addIfAbsent("a bad variant", "{0} pounds", status));
50 assertEquals("adding bad variant status", static_cast<int32_t>(U_ILLEGAL_ARGUMENT_ERROR), status);
51 status = U_ZERO_ERROR;
52 assertFalse(
53 "Adding bad pattern",
54 fmt.addIfAbsent("other", "{0} {1} too many placeholders", status));
55 assertEquals("adding bad pattern status", static_cast<int32_t>(U_ILLEGAL_ARGUMENT_ERROR), status);
56 status = U_ZERO_ERROR;
57 assertFalse("isValid with no patterns", fmt.isValid());
58 assertTrue(
59 "Adding good pattern with no placeholders",
60 fmt.addIfAbsent("zero", "no placeholder", status));
61 assertTrue(
62 "Adding good pattern",
63 fmt.addIfAbsent("other", "{0} pounds", status));
64 assertTrue("isValid with other", fmt.isValid());
65 assertTrue(
66 "Adding good pattern",
67 fmt.addIfAbsent("one", "{0} pound", status));
68
69 assertEquals(
70 "getByVariant",
71 fmt.getByVariant("bad variant")->getTextWithNoArguments(),
72 " pounds");
73 assertEquals(
74 "getByVariant",
75 fmt.getByVariant("other")->getTextWithNoArguments(),
76 " pounds");
77 assertEquals(
78 "getByVariant",
79 fmt.getByVariant("one")->getTextWithNoArguments(),
80 " pound");
81 assertEquals(
82 "getByVariant",
83 fmt.getByVariant("few")->getTextWithNoArguments(),
84 " pounds");
85
86 // Test copy constructor
87 {
88 QuantityFormatter copied(fmt);
89 assertEquals(
90 "copied getByVariant",
91 copied.getByVariant("other")->getTextWithNoArguments(),
92 " pounds");
93 assertEquals(
94 "copied getByVariant",
95 copied.getByVariant("one")->getTextWithNoArguments(),
96 " pound");
97 assertEquals(
98 "copied getByVariant",
99 copied.getByVariant("few")->getTextWithNoArguments(),
100 " pounds");
101 }
102
103 // Test assignment
104 {
105 QuantityFormatter assigned;
106 assigned = fmt;
107 assertEquals(
108 "assigned getByVariant",
109 assigned.getByVariant("other")->getTextWithNoArguments(),
110 " pounds");
111 assertEquals(
112 "assigned getByVariant",
113 assigned.getByVariant("one")->getTextWithNoArguments(),
114 " pound");
115 assertEquals(
116 "assigned getByVariant",
117 assigned.getByVariant("few")->getTextWithNoArguments(),
118 " pounds");
119 }
120
121 // Test format.
122 {
123 LocalPointer<NumberFormat> numfmt(
124 NumberFormat::createInstance(Locale::getEnglish(), status));
125 LocalPointer<PluralRules> plurrule(
126 PluralRules::forLocale("en", status));
127 ASSERT_OK(status);
128 FieldPosition pos(FieldPosition::DONT_CARE);
129 UnicodeString appendTo;
130 assertEquals(
131 "format singular",
132 UnicodeString("1 pound"),
133 fmt.format(
134 1.0,
135 *numfmt,
136 *plurrule,
137 appendTo,
138 pos,
139 status), true);
140 appendTo.remove();
141 assertEquals(
142 "format plural",
143 UnicodeString("2 pounds"),
144 fmt.format(
145 2.0,
146 *numfmt,
147 *plurrule,
148 appendTo,
149 pos,
150 status), true);
151 }
152 fmt.reset();
153 assertFalse("isValid after reset", fmt.isValid());
154 #endif
155 assertSuccess("", status);
156 }
157
createQuantityFormatterTest()158 extern IntlTest *createQuantityFormatterTest() {
159 return new QuantityFormatterTest();
160 }
161