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