• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "collator_test.h"
16 
17 #include <map>
18 #include <vector>
19 
20 #include "collator.h"
21 
22 using namespace OHOS::Global::I18n;
23 using testing::ext::TestSize;
24 using namespace std;
25 
26 namespace OHOS {
27 namespace Global {
28 namespace I18n {
29 
SetUpTestCase(void)30 void CollatorTest::SetUpTestCase(void)
31 {}
32 
TearDownTestCase(void)33 void CollatorTest::TearDownTestCase(void)
34 {}
35 
SetUp(void)36 void CollatorTest::SetUp(void)
37 {}
38 
TearDown(void)39 void CollatorTest::TearDown(void)
40 {}
41 
42 /**
43  * @tc.name: CollatorFuncTest001
44  * @tc.desc: Test Intl Collator.supportedLocalesOf
45  * @tc.type: FUNC
46  */
47 HWTEST_F(CollatorTest, CollatorFuncTest001, TestSize.Level1)
48 {
49     std::vector<std::string> localeTags = {};
50     std::map<std::string, std::string> options = {};
51     Collator collator(localeTags, options);
52     std::map<std::string, std::string> resolvedOptions;
53     collator.ResolvedOptions(resolvedOptions);
54     auto it = resolvedOptions.find("locale");
55     ASSERT_TRUE(it != resolvedOptions.end());
56     std::string defaultLocale = it->second;
57 
58     std::vector<std::string> requestLocales = {"zxx"};
59     requestLocales.push_back(defaultLocale);
60     I18nErrorCode status = I18nErrorCode::SUCCESS;
61     std::vector<std::string> resultLocales = Collator::SupportedLocalesOf(requestLocales, options, status);
62     EXPECT_EQ(status, I18nErrorCode::SUCCESS);
63     EXPECT_EQ(resultLocales.size(), 1);
64     EXPECT_EQ(resultLocales[0], defaultLocale);
65 }
66 
67 /**
68  * @tc.name: CollatorFuncTest002
69  * @tc.desc: Test Intl Collator.supportedLocalesOf
70  * @tc.type: FUNC
71  */
72 HWTEST_F(CollatorTest, CollatorFuncTest002, TestSize.Level1)
73 {
74     std::vector<std::string> localeTags = {"de", "en", "es", "fr", "it"};
75     std::map<std::string, std::string> options = {{"localeMatcher", "lookup"}};
76     I18nErrorCode status = I18nErrorCode::SUCCESS;
77     std::vector<std::string> resultLocales = Collator::SupportedLocalesOf(localeTags, options, status);
78     EXPECT_EQ(status, I18nErrorCode::SUCCESS);
79     for (auto locale : resultLocales) {
80         std::vector<std::string> locales;
81         locales.push_back(locale);
82         std::map<std::string, std::string> configs = {{"sensitivity", "variant"}, {"localeMatcher", "lookup"}};
83         Collator collator(locales, configs);
84         CompareResult result = collator.Compare("A", "b");
85         EXPECT_EQ(result, CompareResult::SMALLER);
86         result = collator.Compare("b", "C");
87         EXPECT_EQ(result, CompareResult::SMALLER);
88     }
89 }
90 
91 /**
92  * @tc.name: CollatorFuncTest003
93  * @tc.desc: Test Intl Collator.supportedLocalesOf
94  * @tc.type: FUNC
95  */
96 HWTEST_F(CollatorTest, CollatorFuncTest003, TestSize.Level1)
97 {
98     std::vector<std::string> localeTags = {"ban", "id-u-co-pinyin", "de-ID"};
99     std::map<std::string, std::string> options = {{"localeMatcher", "lookup"}};
100     I18nErrorCode status = I18nErrorCode::SUCCESS;
101     std::vector<std::string> resultLocales = Collator::SupportedLocalesOf(localeTags, options, status);
102     EXPECT_EQ(status, I18nErrorCode::SUCCESS);
103     EXPECT_EQ(resultLocales.size(), 2);
104     EXPECT_EQ(resultLocales[0], "id-u-co-pinyin");
105     EXPECT_EQ(resultLocales[1], "de-ID");
106     std::map<std::string, std::string> optionsBogus = {{"localeMatcher", "bogus"}};
107     std::vector<std::string> resultLocalesBogus = Collator::SupportedLocalesOf(localeTags, optionsBogus, status);
108     EXPECT_EQ(status, I18nErrorCode::INVALID_PARAM);
109     EXPECT_EQ(resultLocalesBogus.size(), 0);
110 }
111 
112 /**
113  * @tc.name: CollatorFuncTest004
114  * @tc.desc: Test Intl Collator.supportedLocalesOf
115  * @tc.type: FUNC
116  */
117 HWTEST_F(CollatorTest, CollatorFuncTest004, TestSize.Level1)
118 {
119     std::vector<std::string> localeTags = {"ban", "id-u-co-pinyin", "de-ID"};
120     std::map<std::string, std::string> options = {{"localeMatcher", "bogus"}};
121     Collator collator(localeTags, options);
122     I18nErrorCode status = collator.GetError();
123     EXPECT_EQ(status, I18nErrorCode::INVALID_PARAM);
124 }
125 
126 /**
127  * @tc.name: CollatorFuncTest005
128  * @tc.desc: Test Intl Collator.supportedLocalesOf
129  * @tc.type: FUNC
130  */
131 HWTEST_F(CollatorTest, CollatorFuncTest005, TestSize.Level1)
132 {
133     std::vector<std::string> localeTags = {"ban", "id-u-co-pinyin", "de-ID"};
134     std::map<std::string, std::string> options = {{"usage", "bogus"}};
135     Collator collator(localeTags, options);
136     I18nErrorCode status = collator.GetError();
137     EXPECT_EQ(status, I18nErrorCode::INVALID_PARAM);
138 }
139 
140 /**
141  * @tc.name: CollatorFuncTest006
142  * @tc.desc: Test Intl Collator.supportedLocalesOf
143  * @tc.type: FUNC
144  */
145 HWTEST_F(CollatorTest, CollatorFuncTest006, TestSize.Level1)
146 {
147     std::vector<std::string> localeTags = {"ban", "id-u-co-pinyin", "de-ID"};
148     std::map<std::string, std::string> options = {{"sensitivity", "bogus"}};
149     Collator collator(localeTags, options);
150     I18nErrorCode status = collator.GetError();
151     EXPECT_EQ(status, I18nErrorCode::INVALID_PARAM);
152 }
153 
154 /**
155  * @tc.name: CollatorFuncTest007
156  * @tc.desc: Test Intl Collator.supportedLocalesOf
157  * @tc.type: FUNC
158  */
159 HWTEST_F(CollatorTest, CollatorFuncTest007, TestSize.Level1)
160 {
161     std::vector<std::string> localeTags = {"ban", "id-u-co-pinyin", "de-ID"};
162     std::map<std::string, std::string> options = {{"ignorePunctuation", "bogus"}};
163     Collator collator(localeTags, options);
164     I18nErrorCode status = collator.GetError();
165     EXPECT_EQ(status, I18nErrorCode::INVALID_PARAM);
166 }
167 
168 /**
169  * @tc.name: CollatorFuncTest008
170  * @tc.desc: Test Intl Collator.supportedLocalesOf
171  * @tc.type: FUNC
172  */
173 HWTEST_F(CollatorTest, CollatorFuncTest008, TestSize.Level1)
174 {
175     std::vector<std::string> localeTags = {"ban", "id-u-co-pinyin", "de-ID"};
176     std::map<std::string, std::string> options = {{"numeric", "bogus"}};
177     Collator collator(localeTags, options);
178     I18nErrorCode status = collator.GetError();
179     EXPECT_EQ(status, I18nErrorCode::INVALID_PARAM);
180 }
181 
182 /**
183  * @tc.name: CollatorFuncTest009
184  * @tc.desc: Test Intl Collator.supportedLocalesOf
185  * @tc.type: FUNC
186  */
187 HWTEST_F(CollatorTest, CollatorFuncTest009, TestSize.Level1)
188 {
189     std::vector<std::string> localeTags = {"ban", "id-u-co-pinyin", "de-ID"};
190     std::map<std::string, std::string> options = {{"caseFirst", "bogus"}};
191     Collator collator(localeTags, options);
192     I18nErrorCode status = collator.GetError();
193     EXPECT_EQ(status, I18nErrorCode::INVALID_PARAM);
194 }
195 } // namespace I18n
196 } // namespace Global
197 } // namespace OHOS