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
18#include "ecmascript/js_array.h"
19#include "ecmascript/global_env.h"
20#include "ecmascript/tests/test_helper.h"
21
22using namespace panda::ecmascript;
23using namespace panda::ecmascript::builtins;
24
25namespace panda::test {
26class BuiltinsIntlTest : public BaseTestWithScope<true> {
27};
28
29HWTEST_F_L0(BuiltinsIntlTest, GetCanonicalLocales_001)
30{
31    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
32    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
33    ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
34    ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue::Undefined());
35
36    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
37    JSTaggedValue resultObj = BuiltinsIntl::GetCanonicalLocales(ecmaRuntimeCallInfo);
38    JSHandle<JSArray> resultHandle(thread, resultObj);
39    EXPECT_EQ(resultHandle->GetArrayLength(), 0U);
40}
41
42HWTEST_F_L0(BuiltinsIntlTest, GetCanonicalLocales_002)
43{
44    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
45    JSHandle<EcmaString> handleStr = factory->NewFromASCII("ko-kore-kr");
46
47    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
48    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
49    ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
50    ecmaRuntimeCallInfo->SetCallArg(0, handleStr.GetTaggedValue());
51
52    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
53    JSTaggedValue resultObj = BuiltinsIntl::GetCanonicalLocales(ecmaRuntimeCallInfo);
54    JSHandle<JSArray> resultHandle(thread, resultObj);
55
56    JSHandle<TaggedArray> elements(thread, resultHandle->GetElements());
57    EXPECT_EQ(elements->GetLength(), 1U);
58    JSHandle<EcmaString> handleEcmaStr(thread, elements->Get(0));
59    EXPECT_STREQ("ko-Kore-KR", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
60}
61
62HWTEST_F_L0(BuiltinsIntlTest, GetCanonicalLocales_003)
63{
64    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
65
66    JSArray *arr = JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetObject<JSArray>();
67    JSHandle<JSTaggedValue> obj(thread, arr);
68
69    JSHandle<JSTaggedValue> handleStr(factory->NewFromASCII("zh-Hans-Cn"));
70    PropertyDescriptor desc(thread, handleStr, true, true, true);
71    JSHandle<JSTaggedValue> key(factory->NewFromASCII("1"));
72    JSArray::DefineOwnProperty(thread, JSHandle<JSObject>(obj), key, desc);
73
74    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
75    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
76    ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
77    ecmaRuntimeCallInfo->SetCallArg(0, obj.GetTaggedValue());
78
79    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
80    JSTaggedValue resultObj = BuiltinsIntl::GetCanonicalLocales(ecmaRuntimeCallInfo);
81    JSHandle<JSArray> resultHandle(thread, resultObj);
82
83    JSHandle<TaggedArray> elements(thread, resultHandle->GetElements());
84    EXPECT_EQ(elements->GetLength(), 1U);
85    JSHandle<EcmaString> handleEcmaStr(thread, elements->Get(0));
86    EXPECT_STREQ("zh-Hans-CN", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
87}
88} // namespace panda::test
89