• 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 
16 #include <gtest/gtest.h>
17 #include <sys/stat.h>
18 #include <cmath>
19 #include "i18n_types.h"
20 #include "relative_time_format.h"
21 #include "format_utils.h"
22 #include "locale_helper.h"
23 #include "relative_time_format_test.h"
24 
25 using namespace OHOS::Global::I18n;
26 using testing::ext::TestSize;
27 using namespace std;
28 using namespace testing;
29 
30 namespace OHOS {
31 namespace Global {
32 namespace I18n {
33 
SetUpTestCase(void)34 void RelativeTimeFormatTest::SetUpTestCase(void)
35 {
36 }
37 
TearDownTestCase(void)38 void RelativeTimeFormatTest::TearDownTestCase(void)
39 {
40 }
41 
SetUp(void)42 void RelativeTimeFormatTest::SetUp(void)
43 {}
44 
TearDown(void)45 void RelativeTimeFormatTest::TearDown(void)
46 {}
47 
48 /**
49  * @tc.name: RelativeTimeFormatTest0001
50  * @tc.desc: Test RelativeTimeFormat
51  * @tc.type: FUNC
52  */
53 HWTEST_F(RelativeTimeFormatTest, RelativeTimeFormatTest0001, TestSize.Level1)
54 {
55     ErrorMessage errorMsg;
56     const std::vector<std::string> localeTags = {"en"};
57     std::vector<std::string> result = RelativeTimeFormat::CanonicalizeLocales(localeTags, errorMsg);
58     EXPECT_EQ(localeTags.size(), 1);
59     std::vector<std::string> locales { "en" };
60     map<string, string> options = {
61         { "style", "narrow" }
62     };
63     std::unique_ptr<RelativeTimeFormat> relativeTimeFmt =
64         std::make_unique<RelativeTimeFormat>(locales, options, true);
65     EXPECT_TRUE(relativeTimeFmt != nullptr);
66 }
67 
68 /**
69  * @tc.name: RelativeTimeFormatTest0002
70  * @tc.desc: Test GetNumberFieldType
71  * @tc.type: FUNC
72  */
73 HWTEST_F(RelativeTimeFormatTest, RelativeTimeFormatTest0002, TestSize.Level1)
74 {
75     const std::string napiType = "number";
76     const int32_t fieldId = UNUM_FRACTION_FIELD;
77     std::string type = FormatUtils::GetNumberFieldType(napiType, fieldId, 12.45);
78     EXPECT_EQ(type, "fraction");
79     const int32_t fieldId2 = UNUM_INTEGER_FIELD;
80     std::string type2 = FormatUtils::GetNumberFieldType("bigint", fieldId2, 100000);
81     EXPECT_EQ(type2, "integer");
82     std::string type3 = FormatUtils::GetNumberFieldType("number", fieldId2, INFINITY);
83     EXPECT_EQ(type3, "infinity");
84     std::string type4 = FormatUtils::GetNumberFieldType("number", fieldId2, NAN);
85     EXPECT_EQ(type4, "nan");
86 }
87 
88 /**
89  * @tc.name: RelativeTimeFormatTest0003
90  * @tc.desc: Test CheckParamLocales
91  * @tc.type: FUNC
92  */
93 HWTEST_F(RelativeTimeFormatTest, RelativeTimeFormatTest0003, TestSize.Level1)
94 {
95     const std::vector<std::string> localeArray = {"@1"};
96     std::string res = LocaleHelper::CheckParamLocales(localeArray);
97     EXPECT_EQ(res, "invalid locale");
98     const std::vector<std::string> localeArr = {};
99     std::string res2 = LocaleHelper::CheckParamLocales(localeArr);
100     EXPECT_EQ(res2, "");
101     const std::vector<std::string> localeArr3 = {"default"};
102     std::string res3 = LocaleHelper::CheckParamLocales(localeArr3);
103     EXPECT_EQ(res3, "");
104 }
105 } // namespace I18n
106 } // namespace Global
107 } // namespace OHOS