1 /*
2 * Copyright (c) 2021 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 "ecmascript/builtins/builtins_intl.h"
17 #include "ecmascript/js_array.h"
18 #include "ecmascript/global_env.h"
19 #include "ecmascript/tests/test_helper.h"
20
21 using namespace panda::ecmascript;
22 using namespace panda::ecmascript::builtins;
23
24 namespace panda::test {
25 class BuiltinsIntlTest : public testing::Test {
26 public:
SetUpTestCase()27 static void SetUpTestCase()
28 {
29 GTEST_LOG_(INFO) << "SetUpTestCase";
30 }
31
TearDownTestCase()32 static void TearDownTestCase()
33 {
34 GTEST_LOG_(INFO) << "TearDownCase";
35 }
36
SetUp()37 void SetUp() override
38 {
39 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
40 }
41
TearDown()42 void TearDown() override
43 {
44 TestHelper::DestroyEcmaVMWithScope(instance, scope);
45 }
46
47 PandaVM *instance {nullptr};
48 EcmaHandleScope *scope {nullptr};
49 JSThread *thread {nullptr};
50 };
51
HWTEST_F_L0(BuiltinsIntlTest,GetCanonicalLocales_001)52 HWTEST_F_L0(BuiltinsIntlTest, GetCanonicalLocales_001)
53 {
54 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
55 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
56 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
57 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue::Undefined());
58
59 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo.get());
60 JSTaggedValue resultObj = BuiltinsIntl::GetCanonicalLocales(ecmaRuntimeCallInfo.get());
61 EXPECT_TRUE(resultObj.IsJSArray());
62 JSHandle<JSArray> resultHandle(thread, resultObj);
63 EXPECT_EQ(resultHandle->GetArrayLength(), 0);
64 }
65
HWTEST_F_L0(BuiltinsIntlTest,GetCanonicalLocales_002)66 HWTEST_F_L0(BuiltinsIntlTest, GetCanonicalLocales_002)
67 {
68 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
69 JSHandle<EcmaString> handleStr = factory->NewFromCanBeCompressString("ko-kore-kr");
70
71 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
72 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
73 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
74 ecmaRuntimeCallInfo->SetCallArg(0, handleStr.GetTaggedValue());
75
76 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo.get());
77 JSTaggedValue resultObj = BuiltinsIntl::GetCanonicalLocales(ecmaRuntimeCallInfo.get());
78 EXPECT_TRUE(resultObj.IsJSArray());
79 JSHandle<JSArray> resultHandle(thread, resultObj);
80
81 JSHandle<TaggedArray> elements(thread, resultHandle->GetElements());
82 EXPECT_EQ(elements->GetLength(), 1);
83 JSHandle<EcmaString> handleEcmaStr(thread, elements->Get(0));
84 EXPECT_STREQ("ko-Kore-KR", CString(handleEcmaStr->GetCString().get()).c_str());
85 }
86
HWTEST_F_L0(BuiltinsIntlTest,GetCanonicalLocales_003)87 HWTEST_F_L0(BuiltinsIntlTest, GetCanonicalLocales_003)
88 {
89 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
90
91 JSArray *arr = JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetObject<JSArray>();
92 JSHandle<JSTaggedValue> obj(thread, arr);
93
94 JSHandle<JSTaggedValue> handleStr(factory->NewFromCanBeCompressString("zh-Hans-Cn"));
95 PropertyDescriptor desc(thread, handleStr, true, true, true);
96 JSHandle<JSTaggedValue> key(factory->NewFromCanBeCompressString("1"));
97 JSArray::DefineOwnProperty(thread, JSHandle<JSObject>(obj), key, desc);
98
99 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
100 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
101 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
102 ecmaRuntimeCallInfo->SetCallArg(0, obj.GetTaggedValue());
103
104 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo.get());
105 JSTaggedValue resultObj = BuiltinsIntl::GetCanonicalLocales(ecmaRuntimeCallInfo.get());
106 EXPECT_TRUE(resultObj.IsJSArray());
107 JSHandle<JSArray> resultHandle(thread, resultObj);
108
109 JSHandle<TaggedArray> elements(thread, resultHandle->GetElements());
110 EXPECT_EQ(elements->GetLength(), 1);
111 JSHandle<EcmaString> handleEcmaStr(thread, elements->Get(0));
112 EXPECT_STREQ("zh-Hans-CN", CString(handleEcmaStr->GetCString().get()).c_str());
113 }
114 } // namespace panda::test
115