1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "intl_plural_rules_test.h"
17
18 #include "intl_plural_rules.h"
19
20 using testing::ext::TestSize;
21
22 namespace OHOS {
23 namespace Global {
24 namespace I18n {
SetUp(void)25 void IntlPluralRulesTest::SetUp(void) {}
26
TearDown(void)27 void IntlPluralRulesTest::TearDown(void) {}
28
29 /**
30 * @tc.name: IntlPluralRulesFuncTest001
31 * @tc.desc: Test Intl Plural constructor
32 * @tc.type: FUNC
33 */
34 HWTEST_F(IntlPluralRulesTest, IntlPluralRulesFuncTest001, TestSize.Level1)
35 {
36 std::vector<std::string> localeTags = { "en-GB", "ar-EG" };
37 std::unordered_map<std::string, std::string> options = {
38 { "localeMatcher", "lookup" },
39 { "type", "cardinal"},
40 { "minimumIntegerDigits", "1"},
41 { "minimumSignificantDigits", "1"},
42 { "maximumSignificantDigits", "21"}
43 };
44 ErrorMessage errorMessage;
45 IntlPluralRules intlPulral(localeTags, options, errorMessage);
46 EXPECT_EQ(errorMessage.type, ErrorType::NO_ERROR);
47 EXPECT_EQ(intlPulral.Select(0, errorMessage), "other");
48 EXPECT_EQ(intlPulral.Select(1, errorMessage), "one");
49 EXPECT_EQ(intlPulral.Select(2, errorMessage), "other");
50 IntlPluralRules::ResolvedValue resolvedValue = intlPulral.ResolvedOptions();
51 EXPECT_EQ(resolvedValue.roundingType, RoundingType::SIGNIFICANTDIGITS);
52 EXPECT_EQ(resolvedValue.locale, "en");
53 EXPECT_EQ(resolvedValue.type, "cardinal");
54 EXPECT_EQ(resolvedValue.minimumIntegerDigits, 1);
55 EXPECT_EQ(resolvedValue.minimumDigits, 1);
56 EXPECT_EQ(resolvedValue.maximumDigits, 21);
57 EXPECT_EQ(resolvedValue.pluralCategories[0], "one");
58 EXPECT_EQ(resolvedValue.pluralCategories[1], "other");
59 }
60
61 /**
62 * @tc.name: IntlPluralRulesFuncTest002
63 * @tc.desc: Test Intl Plural constructor
64 * @tc.type: FUNC
65 */
66 HWTEST_F(IntlPluralRulesTest, IntlPluralRulesFuncTest002, TestSize.Level1)
67 {
68 std::vector<std::string> localeTags = { "en-GB", "ar-EG" };
69 std::unordered_map<std::string, std::string> options = {
70 { "localeMatcher", "lookup" },
71 { "type", "ordinal"},
72 { "minimumIntegerDigits", "2"},
73 { "minimumFractionDigits", "10"},
74 { "maximumFractionDigits", "20"}
75 };
76 ErrorMessage errorMessage;
77 IntlPluralRules intlPulral(localeTags, options, errorMessage);
78 EXPECT_EQ(errorMessage.type, ErrorType::NO_ERROR);
79 EXPECT_EQ(intlPulral.Select(0, errorMessage), "other");
80 EXPECT_EQ(intlPulral.Select(1, errorMessage), "one");
81 EXPECT_EQ(intlPulral.Select(2, errorMessage), "two");
82 EXPECT_EQ(intlPulral.Select(3, errorMessage), "few");
83 IntlPluralRules::ResolvedValue resolvedValue = intlPulral.ResolvedOptions();
84 EXPECT_EQ(resolvedValue.roundingType, RoundingType::FRACTIONDIGITS);
85 EXPECT_EQ(resolvedValue.locale, "en");
86 EXPECT_EQ(resolvedValue.type, "ordinal");
87 EXPECT_EQ(resolvedValue.minimumIntegerDigits, 2);
88 EXPECT_EQ(resolvedValue.minimumDigits, 10);
89 EXPECT_EQ(resolvedValue.maximumDigits, 20);
90 EXPECT_EQ(resolvedValue.pluralCategories[0], "few");
91 EXPECT_EQ(resolvedValue.pluralCategories[1], "one");
92 EXPECT_EQ(resolvedValue.pluralCategories[2], "two");
93 EXPECT_EQ(resolvedValue.pluralCategories[3], "other");
94 }
95
96 /**
97 * @tc.name: IntlPluralRulesFuncTest003
98 * @tc.desc: Test Intl Plural default constructor
99 * @tc.type: FUNC
100 */
101 HWTEST_F(IntlPluralRulesTest, IntlPluralRulesFuncTest003, TestSize.Level1)
102 {
103 std::vector<std::string> localeTags = {};
104 std::unordered_map<std::string, std::string> options = {};
105 ErrorMessage errorMessage;
106 IntlPluralRules intlPulral(localeTags, options, errorMessage);
107 EXPECT_EQ(errorMessage.type, ErrorType::NO_ERROR);
108 EXPECT_EQ(intlPulral.Select(0, errorMessage), "other");
109 EXPECT_EQ(intlPulral.Select(1, errorMessage), "one");
110 EXPECT_EQ(intlPulral.Select(2, errorMessage), "other");
111 IntlPluralRules::ResolvedValue resolvedValue = intlPulral.ResolvedOptions();
112 EXPECT_EQ(resolvedValue.roundingType, RoundingType::FRACTIONDIGITS);
113 EXPECT_EQ(resolvedValue.locale, "en-US");
114 EXPECT_EQ(resolvedValue.type, "cardinal");
115 EXPECT_EQ(resolvedValue.minimumIntegerDigits, 1);
116 EXPECT_EQ(resolvedValue.minimumDigits, 0);
117 EXPECT_EQ(resolvedValue.maximumDigits, 3);
118 EXPECT_EQ(resolvedValue.pluralCategories[0], "one");
119 EXPECT_EQ(resolvedValue.pluralCategories[1], "other");
120 }
121
122 /**
123 * @tc.name: IntlPluralRulesFuncTest004
124 * @tc.desc: Test Intl Plural constructor bogus param
125 * @tc.type: FUNC
126 */
127 HWTEST_F(IntlPluralRulesTest, IntlPluralRulesFuncTest004, TestSize.Level1)
128 {
129 std::vector<std::string> invalidLocaleTags = { "en-GB", "en_GB" };
130 std::unordered_map<std::string, std::string> options = {};
131 ErrorMessage invalidLocaleError;
132 IntlPluralRules invalidPulral(invalidLocaleTags, options, invalidLocaleError);
133 EXPECT_EQ(invalidLocaleError.type, ErrorType::RANGE_ERROR);
134 EXPECT_EQ(invalidLocaleError.message, "invalid locale");
135
136 std::vector<std::string> localeTags = {};
137 std::unordered_map<std::string, std::string> bogusOptions = { {"localeMatcher", "bogus"} };
138 ErrorMessage localeMatcherError;
139 invalidPulral = IntlPluralRules(localeTags, bogusOptions, localeMatcherError);
140 EXPECT_EQ(localeMatcherError.type, ErrorType::RANGE_ERROR);
141 EXPECT_EQ(localeMatcherError.message, "getStringOption failed");
142
143 ErrorMessage typeError;
144 bogusOptions = { {"type", "bogus"} };
145 invalidPulral = IntlPluralRules(localeTags, bogusOptions, typeError);
146 EXPECT_EQ(typeError.type, ErrorType::RANGE_ERROR);
147 EXPECT_EQ(typeError.message, "getStringOption failed");
148
149 ErrorMessage minMinFraction;
150 bogusOptions = { {"minimumFractionDigits", "-1"} };
151 invalidPulral = IntlPluralRules(localeTags, bogusOptions, minMinFraction);
152 EXPECT_EQ(minMinFraction.type, ErrorType::RANGE_ERROR);
153 EXPECT_EQ(minMinFraction.message, "");
154
155 ErrorMessage maxMinFraction;
156 bogusOptions = { {"minimumFractionDigits", "21"} };
157 invalidPulral = IntlPluralRules(localeTags, bogusOptions, maxMinFraction);
158 EXPECT_EQ(maxMinFraction.type, ErrorType::RANGE_ERROR);
159 EXPECT_EQ(maxMinFraction.message, "");
160
161 ErrorMessage minMaxFraction;
162 bogusOptions = { {"maximumFractionDigits", "-1"} };
163 invalidPulral = IntlPluralRules(localeTags, bogusOptions, minMaxFraction);
164 EXPECT_EQ(minMaxFraction.type, ErrorType::RANGE_ERROR);
165 EXPECT_EQ(minMaxFraction.message, "");
166
167 ErrorMessage maxMaxFraction;
168 bogusOptions = { {"maximumFractionDigits", "21"} };
169 invalidPulral = IntlPluralRules(localeTags, bogusOptions, maxMaxFraction);
170 EXPECT_EQ(maxMaxFraction.type, ErrorType::RANGE_ERROR);
171 EXPECT_EQ(maxMaxFraction.message, "");
172 }
173
174 /**
175 * @tc.name: IntlPluralRulesFuncTest005
176 * @tc.desc: Test Intl Plural constructor bogus param
177 * @tc.type: FUNC
178 */
179 HWTEST_F(IntlPluralRulesTest, IntlPluralRulesFuncTest005, TestSize.Level1)
180 {
181 std::vector<std::string> localeTags = {};
182 ErrorMessage minInteger;
183 std::unordered_map<std::string, std::string> bogusOptions = { {"minimumIntegerDigits", "0"} };
184 IntlPluralRules invalidPulral(localeTags, bogusOptions, minInteger);
185 EXPECT_EQ(minInteger.type, ErrorType::RANGE_ERROR);
186 EXPECT_EQ(minInteger.message, "");
187
188 ErrorMessage maxInteger;
189 bogusOptions = { {"minimumIntegerDigits", "22"} };
190 invalidPulral = IntlPluralRules(localeTags, bogusOptions, maxInteger);
191 EXPECT_EQ(maxInteger.type, ErrorType::RANGE_ERROR);
192 EXPECT_EQ(maxInteger.message, "");
193
194 ErrorMessage minMinSignificant;
195 bogusOptions = { {"minimumSignificantDigits", "0"} };
196 invalidPulral = IntlPluralRules(localeTags, bogusOptions, minMinSignificant);
197 EXPECT_EQ(minMinSignificant.type, ErrorType::RANGE_ERROR);
198 EXPECT_EQ(minMinSignificant.message, "");
199
200 ErrorMessage maxMinSignificant;
201 bogusOptions = { {"minimumSignificantDigits", "22"} };
202 invalidPulral = IntlPluralRules(localeTags, bogusOptions, maxMinSignificant);
203 EXPECT_EQ(maxMinSignificant.type, ErrorType::RANGE_ERROR);
204 EXPECT_EQ(maxMinSignificant.message, "");
205
206 ErrorMessage minMaxSignificant;
207 bogusOptions = { {"maximumSignificantDigits", "0"} };
208 invalidPulral = IntlPluralRules(localeTags, bogusOptions, minMaxSignificant);
209 EXPECT_EQ(minMaxSignificant.type, ErrorType::RANGE_ERROR);
210 EXPECT_EQ(minMaxSignificant.message, "");
211
212 ErrorMessage maxMaxSignificant;
213 bogusOptions = { {"maximumSignificantDigits", "22"} };
214 invalidPulral = IntlPluralRules(localeTags, bogusOptions, maxMaxSignificant);
215 EXPECT_EQ(maxMaxSignificant.type, ErrorType::RANGE_ERROR);
216 EXPECT_EQ(maxMaxSignificant.message, "");
217 }
218
219 /**
220 * @tc.name: IntlPluralRulesFuncTest006
221 * @tc.desc: Test Intl Plural SupportedLocalesOf
222 * @tc.type: FUNC
223 */
224 HWTEST_F(IntlPluralRulesTest, IntlPluralRulesFuncTest006, TestSize.Level1)
225 {
226 std::vector<std::string> requestLocales = { "en-US", "ban", "ar-OM", "de-DE" };
227 std::unordered_map<std::string, std::string> options = { {"localeMatcher", "lookup"} };
228 ErrorMessage errorMessage;
229 std::vector<std::string> supportedLocales =
230 IntlPluralRules::SupportedLocalesOf(requestLocales, options, errorMessage);
231 EXPECT_EQ(errorMessage.type, ErrorType::NO_ERROR);
232 EXPECT_EQ(supportedLocales.size(), 3);
233 EXPECT_EQ(supportedLocales[0], "en-US");
234 EXPECT_EQ(supportedLocales[1], "ar-OM");
235 EXPECT_EQ(supportedLocales[2], "de-DE");
236 }
237
238 /**
239 * @tc.name: IntlPluralRulesFuncTest007
240 * @tc.desc: Test Intl Plural SupportedLocalesOf bogus locale
241 * @tc.type: FUNC
242 */
243 HWTEST_F(IntlPluralRulesTest, IntlPluralRulesFuncTest007, TestSize.Level1)
244 {
245 std::vector<std::string> requestLocales = { "zh-CN", "test", "ar-OM", "de-DE" };
246 std::unordered_map<std::string, std::string> options = { {"localeMatcher", "best fit"} };
247 ErrorMessage errorMessage;
248 std::vector<std::string> supportedLocales =
249 IntlPluralRules::SupportedLocalesOf(requestLocales, options, errorMessage);
250 EXPECT_EQ(errorMessage.type, ErrorType::RANGE_ERROR);
251 EXPECT_EQ(errorMessage.message, "invalid locale");
252 }
253
254 /**
255 * @tc.name: IntlPluralRulesFuncTest008
256 * @tc.desc: Test Intl Plural SupportedLocalesOf bogus options
257 * @tc.type: FUNC
258 */
259 HWTEST_F(IntlPluralRulesTest, IntlPluralRulesFuncTest008, TestSize.Level1)
260 {
261 std::vector<std::string> requestLocales = { "zh-CN", "en-US", "ar-OM", "de-DE" };
262 std::unordered_map<std::string, std::string> options = { {"localeMatcher", "bogus"} };
263 ErrorMessage errorMessage;
264 std::vector<std::string> supportedLocales =
265 IntlPluralRules::SupportedLocalesOf(requestLocales, options, errorMessage);
266 EXPECT_EQ(errorMessage.type, ErrorType::RANGE_ERROR);
267 EXPECT_EQ(errorMessage.message, "getStringOption failed");
268 }
269 } // namespace I18n
270 } // namespace Global
271 } // namespace OHOS
272