1 /*
2 * Copyright (c) 2022 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 "builtin_test_util.h"
17 #include "ecmascript/builtins/builtins_list_format.h"
18
19 #include "ecmascript/global_env.h"
20 #include "ecmascript/js_list_format.h"
21 #include "ecmascript/js_array.h"
22 #include "ecmascript/tests/test_helper.h"
23
24 using namespace panda::ecmascript;
25 using namespace panda::ecmascript::builtins;
26
27 namespace panda::test {
28 class BuiltinsListFormatTest : public BaseTestWithScope<true> {
29 };
30
31 // new Intl.ListFormat(locales)
HWTEST_F_L0(BuiltinsListFormatTest,ListFormatConstructor)32 HWTEST_F_L0(BuiltinsListFormatTest, ListFormatConstructor)
33 {
34 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
35 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
36 JSHandle<JSFunction> newTarget(env->GetListFormatFunction());
37
38 JSHandle<JSTaggedValue> localesString(factory->NewFromASCII("en-GB"));
39 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*newTarget), 8);
40 ecmaRuntimeCallInfo->SetFunction(newTarget.GetTaggedValue());
41 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
42 ecmaRuntimeCallInfo->SetCallArg(0, localesString.GetTaggedValue());
43 // option tag is default value
44 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue::Undefined());
45
46 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
47 JSTaggedValue result = BuiltinsListFormat::ListFormatConstructor(ecmaRuntimeCallInfo);
48 TestHelper::TearDownFrame(thread, prev);
49 EXPECT_TRUE(result.IsJSListFormat());
50 }
51
FormatCommon(JSThread * thread,JSHandle<JSListFormat> & jsFormat,JSTaggedValue value)52 JSTaggedValue FormatCommon(JSThread* thread, JSHandle<JSListFormat>& jsFormat, JSTaggedValue value)
53 {
54 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
55 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
56 ecmaRuntimeCallInfo->SetThis(jsFormat.GetTaggedValue());
57 ecmaRuntimeCallInfo->SetCallArg(0, value);
58
59 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
60 JSTaggedValue result = BuiltinsListFormat::Format(ecmaRuntimeCallInfo);
61 TestHelper::TearDownFrame(thread, prev);
62 return result;
63 }
64
65 // Format("Motorcycle" ,type(conjunction))
HWTEST_F_L0(BuiltinsListFormatTest,Format_001)66 HWTEST_F_L0(BuiltinsListFormatTest, Format_001)
67 {
68 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
69 JSHandle<JSTaggedValue> locale(factory->NewFromASCII("en-GB"));
70 JSHandle<JSTaggedValue> typeValue(factory->NewFromASCII("conjunction")); // the default value
71 JSHandle<JSListFormat> jSListFormat =
72 JSHandle<JSListFormat>(thread, BuiltTestUtil::JSListFormatCreateWithOptionTest(thread, locale, typeValue));
73
74 JSHandle<JSTaggedValue> listValue(factory->NewFromASCII("Motorcycle"));
75 auto result = FormatCommon(thread, jSListFormat, listValue.GetTaggedValue());
76
77 JSHandle<EcmaString> handleEcmaStr(thread, result);
78 EXPECT_STREQ("M, o, t, o, r, c, y, c, l and e", EcmaStringAccessor(handleEcmaStr).ToCString(thread).c_str());
79 }
80
DefineOwnPropertyCommonTest(JSThread * thread,JSHandle<JSObject> & obj,std::vector<JSHandle<JSTaggedValue>> & vals)81 void DefineOwnPropertyCommonTest(JSThread* thread, JSHandle<JSObject>& obj, std::vector<JSHandle<JSTaggedValue>>& vals)
82 {
83 for (size_t i = 0; i < vals.size(); i++) {
84 JSHandle<JSTaggedValue> key(thread, JSTaggedValue(static_cast<int>(i)));
85 PropertyDescriptor desc(thread, vals[i], true, true, true);
86 JSArray::DefineOwnProperty(thread, obj, key, desc);
87 }
88 }
89
CommonTest(JSThread * thread,JSHandle<JSTaggedValue> & locale,JSHandle<JSTaggedValue> & typeValue,std::vector<std::string> & strValues)90 JSHandle<EcmaString> CommonTest(JSThread* thread, JSHandle<JSTaggedValue>& locale, JSHandle<JSTaggedValue>& typeValue,
91 std::vector<std::string>& strValues)
92 {
93 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
94 JSHandle<JSListFormat> jSListFormat =
95 JSHandle<JSListFormat>(thread, BuiltTestUtil::JSListFormatCreateWithOptionTest(thread, locale, typeValue));
96 JSArray *arr = JSArray::ArrayCreate(thread, JSTaggedNumber(0)).GetObject<JSArray>();
97 EXPECT_TRUE(arr != nullptr);
98 JSHandle<JSObject> value(thread, arr);
99 std::vector<JSHandle<JSTaggedValue>> vals;
100 for (size_t i = 0; i < strValues.size(); i++) {
101 JSHandle<JSTaggedValue> listValue(factory->NewFromStdString(strValues[i]));
102 vals.push_back(listValue);
103 }
104
105 DefineOwnPropertyCommonTest(thread, value, vals);
106
107 auto result = FormatCommon(thread, jSListFormat, value.GetTaggedValue());
108 JSHandle<EcmaString> handleEcmaStr(thread, result);
109 return handleEcmaStr;
110 }
111
112 // Format(["Motorcycle", "Bus", "Car" ], type(conjunction))
HWTEST_F_L0(BuiltinsListFormatTest,Format_002)113 HWTEST_F_L0(BuiltinsListFormatTest, Format_002)
114 {
115 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
116 JSHandle<JSTaggedValue> locale(factory->NewFromASCII("en-GB"));
117 JSHandle<JSTaggedValue> typeValue(factory->NewFromASCII("conjunction")); // the default value
118 std::vector<std::string> listVal{"Motorcycle", "Bus", "Car"};
119 JSHandle<EcmaString> handleEcmaStr = CommonTest(thread, locale, typeValue, listVal);
120 EXPECT_STREQ("Motorcycle, Bus and Car", EcmaStringAccessor(handleEcmaStr).ToCString(thread).c_str());
121 }
122
123 // Format(["Motorcycle", "Bus", "Car" ], type(disjunction))
HWTEST_F_L0(BuiltinsListFormatTest,Format_003)124 HWTEST_F_L0(BuiltinsListFormatTest, Format_003)
125 {
126 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
127 JSHandle<JSTaggedValue> locale(factory->NewFromASCII("en-GB"));
128 JSHandle<JSTaggedValue> typeValue(factory->NewFromASCII("disjunction")); // the default value
129 std::vector<std::string> listVal{"Motorcycle", "Bus", "Car"};
130 JSHandle<EcmaString> handleEcmaStr = CommonTest(thread, locale, typeValue, listVal);
131 EXPECT_STREQ("Motorcycle, Bus or Car", EcmaStringAccessor(handleEcmaStr).ToCString(thread).c_str());
132 }
133
134 // Format(["中文英文" ], type(disjunction))
HWTEST_F_L0(BuiltinsListFormatTest,Format_004)135 HWTEST_F_L0(BuiltinsListFormatTest, Format_004)
136 {
137 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
138 JSHandle<JSTaggedValue> locale(factory->NewFromASCII("zh-cn"));
139 JSHandle<JSTaggedValue> typeValue(factory->NewFromASCII("disjunction")); // the default value
140
141 std::vector<std::string> listVal{"中文英文"};
142 JSHandle<EcmaString> handleEcmaStr = CommonTest(thread, locale, typeValue, listVal);
143 EXPECT_STREQ("中文英文", EcmaStringAccessor(handleEcmaStr).ToCString(thread).c_str());
144 }
145
146 // Format(["中文", "英文", "韩文" ], type(conjunction))
HWTEST_F_L0(BuiltinsListFormatTest,Format_005)147 HWTEST_F_L0(BuiltinsListFormatTest, Format_005)
148 {
149 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
150 JSHandle<JSTaggedValue> locale(factory->NewFromASCII("zh-cn"));
151 JSHandle<JSTaggedValue> typeValue(factory->NewFromASCII("conjunction")); // the default value
152
153 std::vector<std::string> listVal{"中文", "英文", "韩文" };
154 JSHandle<EcmaString> handleEcmaStr = CommonTest(thread, locale, typeValue, listVal);
155 EXPECT_STREQ("中文、英文和韩文", EcmaStringAccessor(handleEcmaStr).ToCString(thread).c_str());
156 }
157 } // namespace panda::test
158