1 /*
2  * Copyright (c) 2024 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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 #include <map>
19 #include <vector>
20 #include "number_format.h"
21 #include "intl_test.h"
22 #include "generate_ics_file.h"
23 #include "signature_verifier.h"
24 #include <unistd.h>
25 #include "unicode/utypes.h"
26 
27 using namespace OHOS::Global::I18n;
28 using testing::ext::TestSize;
29 using namespace std;
30 using namespace testing;
31 
32 namespace OHOS {
33 namespace Global {
34 namespace I18n {
35 /**
36  * @tc.name: IntlFuncTest0099
37  * @tc.desc: Test Intl NumberFormat.format
38  * @tc.type: FUNC
39  */
40 HWTEST_F(IntlTest, IntlFuncTest0099, TestSize.Level1)
41 {
42     string locale = "en-IN";
43     string expects = "+1,23,456.79 euros";
44     vector<string> locales{locale};
45     string useGrouping = "true";
46     string minimumIntegerDigits = "7";
47     string maximumFractionDigits = "2";
48     string style = "currency";
49     string currency = "978";
50     map<string, string> options = { { "useGrouping", useGrouping },
51                                     { "style", style },
52                                     { "currency", currency },
53                                     { "currencyDisplay", "name" },
54                                     { "currencySign", "accounting" },
55                                     { "signDisplay", "always" } };
56     std::unique_ptr<NumberFormat> numFmt = std::make_unique<NumberFormat>(locales, options);
57     ASSERT_TRUE(numFmt != nullptr);
58     string out = numFmt->Format(123456.789);
59     EXPECT_EQ(out, expects);
60     EXPECT_EQ(numFmt->GetUseGrouping(), useGrouping);
61     EXPECT_EQ(numFmt->GetStyle(), style);
62     EXPECT_EQ(numFmt->GetCurrency(), "EUR");
63 }
64 
65 /**
66  * @tc.name: IntlFuncTest00100
67  * @tc.desc: Test Intl NumberFormat.format
68  * @tc.type: FUNC
69  */
70 HWTEST_F(IntlTest, IntlFuncTest00100, TestSize.Level1)
71 {
72     string locale = "en-IN";
73     string expects = "+1,23,456.789";
74     vector<string> locales{locale};
75     string useGrouping = "true";
76     string minimumIntegerDigits = "7";
77     string maximumFractionDigits = "2";
78     string style = "currency";
79     string currency = "111";
80     map<string, string> options = { { "useGrouping", useGrouping },
81                                     { "style", style },
82                                     { "currency", currency },
83                                     { "currencyDisplay", "name" },
84                                     { "currencySign", "accounting" },
85                                     { "signDisplay", "always" } };
86     std::unique_ptr<NumberFormat> numFmt = std::make_unique<NumberFormat>(locales, options);
87     ASSERT_TRUE(numFmt != nullptr);
88     string out = numFmt->Format(123456.789);
89     EXPECT_EQ(out, expects);
90     EXPECT_EQ(numFmt->GetUseGrouping(), useGrouping);
91     EXPECT_EQ(numFmt->GetStyle(), style);
92     EXPECT_EQ(numFmt->GetCurrency(), "");
93 }
94 
95 /**
96  * @tc.name: IntlFuncTest00101
97  * @tc.desc: Test Intl NumberFormat.format
98  * @tc.type: FUNC
99  */
100 HWTEST_F(IntlTest, IntlFuncTest00101, TestSize.Level1)
101 {
102     string locale = "en-IN";
103     string expects = "+1,23,456.789";
104     vector<string> locales{locale};
105     string useGrouping = "true";
106     string minimumIntegerDigits = "7";
107     string maximumFractionDigits = "2";
108     string style = "currency";
109     string currency = "a1b";
110     map<string, string> options = { { "useGrouping", useGrouping },
111                                     { "style", style },
112                                     { "currency", currency },
113                                     { "currencyDisplay", "name" },
114                                     { "currencySign", "accounting" },
115                                     { "signDisplay", "always" } };
116     std::unique_ptr<NumberFormat> numFmt = std::make_unique<NumberFormat>(locales, options);
117     ASSERT_TRUE(numFmt != nullptr);
118     string out = numFmt->Format(123456.789);
119     EXPECT_EQ(out, expects);
120     EXPECT_EQ(numFmt->GetUseGrouping(), useGrouping);
121     EXPECT_EQ(numFmt->GetStyle(), style);
122     EXPECT_EQ(numFmt->GetCurrency(), "");
123 }
124 
125 /**
126  * @tc.name: IntlFuncTest00102
127  * @tc.desc: Test Intl NumberFormat.format
128  * @tc.type: FUNC
129  */
130 HWTEST_F(IntlTest, IntlFuncTest00102, TestSize.Level1)
131 {
132     string locale = "en-IN";
133     string expects = "+1,23,456.789";
134     vector<string> locales{locale};
135     string useGrouping = "true";
136     string minimumIntegerDigits = "7";
137     string maximumFractionDigits = "2";
138     string style = "currency";
139     string currency = "a#b";
140     map<string, string> options = { { "useGrouping", useGrouping },
141                                     { "style", style },
142                                     { "currency", currency },
143                                     { "currencyDisplay", "name" },
144                                     { "currencySign", "accounting" },
145                                     { "signDisplay", "always" } };
146     std::unique_ptr<NumberFormat> numFmt = std::make_unique<NumberFormat>(locales, options);
147     ASSERT_TRUE(numFmt != nullptr);
148     string out = numFmt->Format(123456.789);
149     EXPECT_EQ(out, expects);
150     EXPECT_EQ(numFmt->GetUseGrouping(), useGrouping);
151     EXPECT_EQ(numFmt->GetStyle(), style);
152     EXPECT_EQ(numFmt->GetCurrency(), "");
153 }
154 
155 /**
156  * @tc.name: IntlFuncTest00108
157  * @tc.desc: Test Intl NumberFormat.format
158  * @tc.type: FUNC
159  */
160 HWTEST_F(IntlTest, IntlFuncTest00108, TestSize.Level1)
161 {
162     string locale = "en-IN";
163     string expects = "+1,23,456.789";
164     vector<string> locales{locale};
165     string useGrouping = "true";
166     string minimumIntegerDigits = "7";
167     string maximumFractionDigits = "2";
168     string style = "currency";
169     string currency = "ab";
170     map<string, string> options = { { "useGrouping", useGrouping },
171                                     { "style", style },
172                                     { "currency", currency },
173                                     { "currencyDisplay", "name" },
174                                     { "currencySign", "accounting" },
175                                     { "signDisplay", "always" } };
176     std::unique_ptr<NumberFormat> numFmt = std::make_unique<NumberFormat>(locales, options);
177     ASSERT_TRUE(numFmt != nullptr);
178     string out = numFmt->Format(123456.789);
179     EXPECT_EQ(out, expects);
180     EXPECT_EQ(numFmt->GetUseGrouping(), useGrouping);
181     EXPECT_EQ(numFmt->GetStyle(), style);
182     EXPECT_EQ(numFmt->GetCurrency(), "");
183 }
184 
185 /**
186  * @tc.name: IntlFuncTest00109
187  * @tc.desc: Test Intl NumberFormat.format
188  * @tc.type: FUNC
189  */
190 HWTEST_F(IntlTest, IntlFuncTest00109, TestSize.Level1)
191 {
192     string locale = "en-IN";
193     string expects = "+1,23,456.789";
194     vector<string> locales{locale};
195     string useGrouping = "true";
196     string minimumIntegerDigits = "7";
197     string maximumFractionDigits = "2";
198     string style = "currency";
199     string currency = "abcd";
200     map<string, string> options = { { "useGrouping", useGrouping },
201                                     { "style", style },
202                                     { "currency", currency },
203                                     { "currencyDisplay", "name" },
204                                     { "currencySign", "accounting" },
205                                     { "signDisplay", "always" } };
206     std::unique_ptr<NumberFormat> numFmt = std::make_unique<NumberFormat>(locales, options);
207     ASSERT_TRUE(numFmt != nullptr);
208     string out = numFmt->Format(123456.789);
209     EXPECT_EQ(out, expects);
210     EXPECT_EQ(numFmt->GetUseGrouping(), useGrouping);
211     EXPECT_EQ(numFmt->GetStyle(), style);
212     EXPECT_EQ(numFmt->GetCurrency(), "");
213 }
214 } // namespace I18n
215 } // namespace Global
216 } // namespace OHOS