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_string.h"
17
18 #include "ecmascript/base/builtins_base.h"
19 #include "ecmascript/builtins/builtins_regexp.h"
20 #include "ecmascript/ecma_runtime_call_info.h"
21 #include "ecmascript/ecma_string.h"
22 #include "ecmascript/ecma_vm.h"
23 #include "ecmascript/global_env.h"
24 #include "ecmascript/js_array.h"
25 #include "ecmascript/js_primitive_ref.h"
26 #include "ecmascript/js_regexp.h"
27 #include "ecmascript/js_tagged_value.h"
28 #include "ecmascript/object_factory.h"
29 #include "ecmascript/tests/test_helper.h"
30
31 using namespace panda::ecmascript;
32 using namespace panda::ecmascript::builtins;
33
34 namespace panda::test {
35 class BuiltinsStringTest : public testing::Test {
36 public:
SetUpTestCase()37 static void SetUpTestCase()
38 {
39 GTEST_LOG_(INFO) << "SetUpTestCase";
40 }
41
TearDownTestCase()42 static void TearDownTestCase()
43 {
44 GTEST_LOG_(INFO) << "TearDownCase";
45 }
46
SetUp()47 void SetUp() override
48 {
49 JSRuntimeOptions options;
50 #if PANDA_TARGET_LINUX
51 // for consistency requirement, use ohos_icu4j/data as icu-data-path
52 options.SetIcuDataPath(ICU_PATH);
53 #endif
54 options.SetEnableForceGC(true);
55 instance = JSNApi::CreateEcmaVM(options);
56 instance->SetEnableForceGC(true);
57 ASSERT_TRUE(instance != nullptr) << "Cannot create EcmaVM";
58 thread = instance->GetJSThread();
59 scope = new EcmaHandleScope(thread);
60 }
61
TearDown()62 void TearDown() override
63 {
64 TestHelper::DestroyEcmaVMWithScope(instance, scope);
65 }
66
67 EcmaVM *instance {nullptr};
68 EcmaHandleScope *scope {nullptr};
69 JSThread *thread {nullptr};
70 };
71
CreateBuiltinsStringRegExpObjByPatternAndFlags(JSThread * thread,const JSHandle<EcmaString> & pattern,const JSHandle<EcmaString> & flags)72 JSTaggedValue CreateBuiltinsStringRegExpObjByPatternAndFlags(JSThread *thread, const JSHandle<EcmaString> &pattern,
73 const JSHandle<EcmaString> &flags)
74 {
75 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
76 JSHandle<JSFunction> regexp(env->GetRegExpFunction());
77 JSHandle<JSObject> globalObject(thread, env->GetGlobalObject());
78
79 // 8 : test case
80 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, regexp.GetTaggedValue(), 8);
81 ecmaRuntimeCallInfo->SetFunction(regexp.GetTaggedValue());
82 ecmaRuntimeCallInfo->SetThis(globalObject.GetTaggedValue());
83 ecmaRuntimeCallInfo->SetCallArg(0, pattern.GetTaggedValue());
84 ecmaRuntimeCallInfo->SetCallArg(1, flags.GetTaggedValue());
85
86 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
87 JSTaggedValue result = BuiltinsRegExp::RegExpConstructor(ecmaRuntimeCallInfo);
88 TestHelper::TearDownFrame(thread, prev);
89 return result;
90 }
91
HWTEST_F_L0(BuiltinsStringTest,StringConstructor1)92 HWTEST_F_L0(BuiltinsStringTest, StringConstructor1)
93 {
94 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
95
96 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
97 JSHandle<JSFunction> string(env->GetStringFunction());
98 JSHandle<JSObject> globalObject(thread, env->GetGlobalObject());
99 JSHandle<EcmaString> string2 = factory->NewFromASCII("ABC");
100
101 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, string.GetTaggedValue(), 6);
102 ecmaRuntimeCallInfo->SetFunction(string.GetTaggedValue());
103 ecmaRuntimeCallInfo->SetThis(globalObject.GetTaggedValue());
104 ecmaRuntimeCallInfo->SetCallArg(0, string2.GetTaggedValue());
105
106 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
107 JSTaggedValue result = BuiltinsString::StringConstructor(ecmaRuntimeCallInfo);
108 JSTaggedValue value(static_cast<JSTaggedType>(result.GetRawData()));
109 ASSERT_TRUE(value.IsECMAObject());
110 JSHandle<JSPrimitiveRef> ref(thread, JSPrimitiveRef::Cast(value.GetTaggedObject()));
111 JSTaggedValue test = factory->NewFromASCII("ABC").GetTaggedValue();
112 ASSERT_EQ(EcmaStringAccessor::Compare(EcmaString::Cast(ref->GetValue().GetTaggedObject()),
113 reinterpret_cast<EcmaString *>(test.GetRawData())),
114 0);
115 }
116
117 // String.fromCharCode(65, 66, 67)
HWTEST_F_L0(BuiltinsStringTest,fromCharCode1)118 HWTEST_F_L0(BuiltinsStringTest, fromCharCode1)
119 {
120 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
121 const double arg1 = 65;
122 const double arg2 = 66;
123 const double arg3 = 67;
124
125 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10);
126 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
127 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
128 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(arg1));
129 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(arg2));
130 ecmaRuntimeCallInfo->SetCallArg(2, JSTaggedValue(arg3));
131
132 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
133 JSTaggedValue result = BuiltinsString::FromCharCode(ecmaRuntimeCallInfo);
134 ASSERT_TRUE(result.IsString());
135 JSTaggedValue value(static_cast<JSTaggedType>(result.GetRawData()));
136 JSHandle<JSTaggedValue> valueHandle(thread, JSTaggedValue(value.GetTaggedObject()));
137 JSTaggedValue test = factory->NewFromASCII("ABC").GetTaggedValue();
138 ASSERT_EQ(EcmaStringAccessor::Compare(EcmaString::Cast(valueHandle->GetTaggedObject()),
139 reinterpret_cast<EcmaString *>(test.GetRawData())),
140 0);
141 }
142
143 // String.fromCodePoint(65, 66, 67)
HWTEST_F_L0(BuiltinsStringTest,fromCodePoint1)144 HWTEST_F_L0(BuiltinsStringTest, fromCodePoint1)
145 {
146 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
147 const double arg1 = 65;
148 const double arg2 = 66;
149 const double arg3 = 67;
150
151 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10);
152 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
153 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
154 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(arg1));
155 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(arg2));
156 ecmaRuntimeCallInfo->SetCallArg(2, JSTaggedValue(arg3));
157
158 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
159 JSTaggedValue result = BuiltinsString::FromCodePoint(ecmaRuntimeCallInfo);
160 ASSERT_TRUE(result.IsString());
161 JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
162 JSTaggedValue test = factory->NewFromASCII("ABC").GetTaggedValue();
163 ASSERT_EQ(EcmaStringAccessor::Compare(*resultHandle, reinterpret_cast<EcmaString *>(test.GetRawData())), 0);
164 }
165
166 // "abcabcabc".charAt(5)
HWTEST_F_L0(BuiltinsStringTest,charAt1)167 HWTEST_F_L0(BuiltinsStringTest, charAt1)
168 {
169 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
170 JSHandle<EcmaString> thisVal = factory->NewFromASCII("abcabcabc");
171
172 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
173 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
174 ecmaRuntimeCallInfo->SetThis(thisVal.GetTaggedValue());
175 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(5)));
176
177 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
178 JSTaggedValue result = BuiltinsString::CharAt(ecmaRuntimeCallInfo);
179 ASSERT_TRUE(result.IsString());
180 JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
181 JSTaggedValue test = factory->NewFromASCII("c").GetTaggedValue();
182 ASSERT_EQ(EcmaStringAccessor::Compare(*resultHandle, reinterpret_cast<EcmaString *>(test.GetRawData())), 0);
183 }
184
185 // "一二三四".charAt(2)
HWTEST_F_L0(BuiltinsStringTest,charAt2)186 HWTEST_F_L0(BuiltinsStringTest, charAt2)
187 {
188 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
189 JSHandle<EcmaString> thisVal = factory->NewFromUtf8("一二三四");
190
191 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
192 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
193 ecmaRuntimeCallInfo->SetThis(thisVal.GetTaggedValue());
194 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(2)));
195
196 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
197 JSTaggedValue result = BuiltinsString::CharAt(ecmaRuntimeCallInfo);
198 ASSERT_TRUE(result.IsString());
199 JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
200 JSTaggedValue test = factory->NewFromUtf8("三").GetTaggedValue();
201 ASSERT_EQ(EcmaStringAccessor::Compare(*resultHandle, reinterpret_cast<EcmaString *>(test.GetRawData())), 0);
202 }
203
204 // "abcabcabc".charAt(-1)
HWTEST_F_L0(BuiltinsStringTest,charAt3)205 HWTEST_F_L0(BuiltinsStringTest, charAt3)
206 {
207 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
208 JSHandle<EcmaString> thisVal = factory->NewFromASCII("abcabcabc");
209
210 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
211 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
212 ecmaRuntimeCallInfo->SetThis(thisVal.GetTaggedValue());
213 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(-1)));
214
215 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
216 JSTaggedValue result = BuiltinsString::CharAt(ecmaRuntimeCallInfo);
217 ASSERT_TRUE(result.IsString());
218 JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
219 JSTaggedValue test = factory->GetEmptyString().GetTaggedValue();
220 ASSERT_EQ(EcmaStringAccessor::Compare(*resultHandle, reinterpret_cast<EcmaString *>(test.GetRawData())), 0);
221 }
222
223 // "ABC".charCodeAt(0)
HWTEST_F_L0(BuiltinsStringTest,charCodeAt1)224 HWTEST_F_L0(BuiltinsStringTest, charCodeAt1)
225 {
226 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
227 JSHandle<EcmaString> thisVal = factory->NewFromASCII("ABC");
228
229 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
230 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
231 ecmaRuntimeCallInfo->SetThis(thisVal.GetTaggedValue());
232 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(0)));
233
234 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
235 JSTaggedValue result = BuiltinsString::CharCodeAt(ecmaRuntimeCallInfo);
236
237 ASSERT_EQ(result.GetRawData(), JSTaggedValue(65).GetRawData());
238 }
239
240 // "ABC".charCodeAt(-1)
HWTEST_F_L0(BuiltinsStringTest,charCodeAt2)241 HWTEST_F_L0(BuiltinsStringTest, charCodeAt2)
242 {
243 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
244 JSHandle<EcmaString> thisVal = factory->NewFromASCII("ABC");
245
246 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
247 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
248 ecmaRuntimeCallInfo->SetThis(thisVal.GetTaggedValue());
249 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(-1)));
250
251 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
252 JSTaggedValue result = BuiltinsString::CharCodeAt(ecmaRuntimeCallInfo);
253
254 JSTaggedValue test = BuiltinsString::GetTaggedDouble(base::NAN_VALUE);
255 ASSERT_EQ(result.GetRawData(), test.GetRawData());
256 }
257
258 // "ABC".codePointAt(1)
HWTEST_F_L0(BuiltinsStringTest,codePointAt1)259 HWTEST_F_L0(BuiltinsStringTest, codePointAt1)
260 {
261 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
262 JSHandle<EcmaString> thisVal = factory->NewFromASCII("ABC");
263
264 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
265 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
266 ecmaRuntimeCallInfo->SetThis(thisVal.GetTaggedValue());
267 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(1)));
268
269 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
270 JSTaggedValue result = BuiltinsString::CodePointAt(ecmaRuntimeCallInfo);
271
272 ASSERT_EQ(result.GetRawData(), JSTaggedValue(66).GetRawData());
273 }
274
275 // 'a'.concat('b', 'c', 'd')
HWTEST_F_L0(BuiltinsStringTest,concat1)276 HWTEST_F_L0(BuiltinsStringTest, concat1)
277 {
278 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
279 JSHandle<EcmaString> thisStr = factory->NewFromASCII("a");
280 JSHandle<EcmaString> val1 = factory->NewFromASCII("b");
281 JSHandle<EcmaString> val2 = factory->NewFromASCII("c");
282 JSHandle<EcmaString> val3 = factory->NewFromASCII("d");
283
284 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10);
285 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
286 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
287 ecmaRuntimeCallInfo->SetCallArg(0, val1.GetTaggedValue());
288 ecmaRuntimeCallInfo->SetCallArg(1, val2.GetTaggedValue());
289 ecmaRuntimeCallInfo->SetCallArg(2, val3.GetTaggedValue());
290
291 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
292 JSTaggedValue result = BuiltinsString::Concat(ecmaRuntimeCallInfo);
293 ASSERT_TRUE(result.IsString());
294 JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
295 JSTaggedValue test = factory->NewFromASCII("abcd").GetTaggedValue();
296 ASSERT_EQ(EcmaStringAccessor::Compare(*resultHandle, reinterpret_cast<EcmaString *>(test.GetRawData())), 0);
297 }
298
299 // "abcabcabc".indexof('b')
HWTEST_F_L0(BuiltinsStringTest,indexof1)300 HWTEST_F_L0(BuiltinsStringTest, indexof1)
301 {
302 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
303 JSHandle<EcmaString> thisStr = factory->NewFromASCII("abcabcabc");
304 JSHandle<EcmaString> val = factory->NewFromASCII("b");
305
306 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
307 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
308 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
309 ecmaRuntimeCallInfo->SetCallArg(0, val.GetTaggedValue());
310
311 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
312 JSTaggedValue result = BuiltinsString::IndexOf(ecmaRuntimeCallInfo);
313
314 ASSERT_EQ(result.GetRawData(), JSTaggedValue(1).GetRawData());
315 }
316
317 // "abcabcabc".indexof('b', 2)
HWTEST_F_L0(BuiltinsStringTest,indexof2)318 HWTEST_F_L0(BuiltinsStringTest, indexof2)
319 {
320 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
321 JSHandle<EcmaString> thisStr = factory->NewFromASCII("abcabcabc");
322 JSHandle<EcmaString> val = factory->NewFromASCII("b");
323
324 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
325 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
326 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
327 ecmaRuntimeCallInfo->SetCallArg(0, val.GetTaggedValue());
328 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(static_cast<double>(2)));
329
330 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
331 JSTaggedValue result = BuiltinsString::IndexOf(ecmaRuntimeCallInfo);
332
333 ASSERT_EQ(result.GetRawData(), JSTaggedValue(4).GetRawData());
334 }
335
336 // "abcabcabc".indexof('d')
HWTEST_F_L0(BuiltinsStringTest,indexof3)337 HWTEST_F_L0(BuiltinsStringTest, indexof3)
338 {
339 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
340 JSHandle<EcmaString> thisStr = factory->NewFromASCII("abcabcabc");
341 JSHandle<EcmaString> val = factory->NewFromASCII("d");
342
343 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
344 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
345 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
346 ecmaRuntimeCallInfo->SetCallArg(0, val.GetTaggedValue());
347
348 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
349 JSTaggedValue result = BuiltinsString::IndexOf(ecmaRuntimeCallInfo);
350
351 ASSERT_EQ(result.GetRawData(), JSTaggedValue(-1).GetRawData());
352 }
353
354 // "abcabcabc".lastIndexOf('b')
HWTEST_F_L0(BuiltinsStringTest,lastIndexOf1)355 HWTEST_F_L0(BuiltinsStringTest, lastIndexOf1)
356 {
357 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
358 JSHandle<EcmaString> thisStr = factory->NewFromASCII("abcabcabc");
359 JSHandle<EcmaString> val = factory->NewFromASCII("b");
360
361 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
362 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
363 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
364 ecmaRuntimeCallInfo->SetCallArg(0, val.GetTaggedValue());
365
366 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
367 JSTaggedValue result = BuiltinsString::LastIndexOf(ecmaRuntimeCallInfo);
368
369 ASSERT_EQ(result.GetRawData(), JSTaggedValue(7).GetRawData());
370 }
371 // "abcabcabc".lastIndexOf('b', 2)
HWTEST_F_L0(BuiltinsStringTest,lastIndexOf2)372 HWTEST_F_L0(BuiltinsStringTest, lastIndexOf2)
373 {
374 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
375 JSHandle<EcmaString> thisStr = factory->NewFromASCII("abcabcabc");
376 JSHandle<EcmaString> val = factory->NewFromASCII("b");
377
378 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
379 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
380 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
381 ecmaRuntimeCallInfo->SetCallArg(0, val.GetTaggedValue());
382 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(static_cast<double>(2)));
383
384 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
385 JSTaggedValue result = BuiltinsString::LastIndexOf(ecmaRuntimeCallInfo);
386
387 ASSERT_EQ(result.GetRawData(), JSTaggedValue(1).GetRawData());
388 }
389
390 // "abcabcabc".lastIndexOf('d')
HWTEST_F_L0(BuiltinsStringTest,lastIndexOf3)391 HWTEST_F_L0(BuiltinsStringTest, lastIndexOf3)
392 {
393 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
394 JSHandle<EcmaString> thisStr = factory->NewFromASCII("abcabcabc");
395 JSHandle<EcmaString> val = factory->NewFromASCII("d");
396
397 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
398 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
399 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
400 ecmaRuntimeCallInfo->SetCallArg(0, val.GetTaggedValue());
401
402 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
403 JSTaggedValue result = BuiltinsString::LastIndexOf(ecmaRuntimeCallInfo);
404
405 ASSERT_EQ(result.GetRawData(), JSTaggedValue(-1).GetRawData());
406 }
407
408 // "abcabcabc".includes('b')
HWTEST_F_L0(BuiltinsStringTest,Includes2)409 HWTEST_F_L0(BuiltinsStringTest, Includes2)
410 {
411 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
412 JSHandle<EcmaString> thisStr = factory->NewFromASCII("abcabcabc");
413 JSHandle<EcmaString> val = factory->NewFromASCII("b");
414
415 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
416 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
417 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
418 ecmaRuntimeCallInfo->SetCallArg(0, val.GetTaggedValue());
419
420 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
421 JSTaggedValue result = BuiltinsString::Includes(ecmaRuntimeCallInfo);
422
423 ASSERT_EQ(result.GetRawData(), JSTaggedValue::True().GetRawData());
424 }
425
426 // "abccccccc".includes('b',2)
HWTEST_F_L0(BuiltinsStringTest,Includes3)427 HWTEST_F_L0(BuiltinsStringTest, Includes3)
428 {
429 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
430 JSHandle<EcmaString> thisStr = factory->NewFromASCII("abccccccc");
431 JSHandle<EcmaString> val = factory->NewFromASCII("b");
432
433 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
434 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
435 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
436 ecmaRuntimeCallInfo->SetCallArg(0, val.GetTaggedValue());
437 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(static_cast<double>(2)));
438
439 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
440 JSTaggedValue result = BuiltinsString::Includes(ecmaRuntimeCallInfo);
441
442 ASSERT_EQ(result.GetRawData(), JSTaggedValue::False().GetRawData());
443 }
444
445 // "一二三四".includes('二')
HWTEST_F_L0(BuiltinsStringTest,Includes4)446 HWTEST_F_L0(BuiltinsStringTest, Includes4)
447 {
448 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
449 JSHandle<EcmaString> thisStr = factory->NewFromUtf8("一二三四");
450 JSHandle<EcmaString> val = factory->NewFromUtf8("二");
451
452 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
453 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
454 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
455 ecmaRuntimeCallInfo->SetCallArg(0, val.GetTaggedValue());
456
457 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
458 JSTaggedValue result = BuiltinsString::Includes(ecmaRuntimeCallInfo);
459
460 ASSERT_EQ(result.GetRawData(), JSTaggedValue::True().GetRawData());
461 }
462
463 // "To be, or not to be, that is the question.".startsWith('To be')
HWTEST_F_L0(BuiltinsStringTest,startsWith1)464 HWTEST_F_L0(BuiltinsStringTest, startsWith1)
465 {
466 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
467 JSHandle<EcmaString> thisStr = factory->NewFromASCII("To be, or not to be, that is the question.");
468 JSHandle<EcmaString> val = factory->NewFromASCII("To be");
469
470 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
471 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
472 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
473 ecmaRuntimeCallInfo->SetCallArg(0, val.GetTaggedValue());
474
475 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
476 JSTaggedValue result = BuiltinsString::StartsWith(ecmaRuntimeCallInfo);
477
478 ASSERT_EQ(result.GetRawData(), JSTaggedValue::True().GetRawData());
479 }
480
481 // "To be, or not to be, that is the question.".startsWith('not to be')
HWTEST_F_L0(BuiltinsStringTest,startsWith2)482 HWTEST_F_L0(BuiltinsStringTest, startsWith2)
483 {
484 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
485 JSHandle<EcmaString> thisStr = factory->NewFromASCII("To be, or not to be, that is the question.");
486 JSHandle<EcmaString> val = factory->NewFromASCII("not to be");
487
488 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
489 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
490 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
491 ecmaRuntimeCallInfo->SetCallArg(0, val.GetTaggedValue());
492
493 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
494 JSTaggedValue result = BuiltinsString::StartsWith(ecmaRuntimeCallInfo);
495
496 ASSERT_EQ(result.GetRawData(), JSTaggedValue::False().GetRawData());
497 }
498
499 // "To be, or not to be, that is the question.".startsWith('not to be', 10)
HWTEST_F_L0(BuiltinsStringTest,startsWith3)500 HWTEST_F_L0(BuiltinsStringTest, startsWith3)
501 {
502 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
503 JSHandle<EcmaString> thisStr = factory->NewFromASCII("To be, or not to be, that is the question.");
504 JSHandle<EcmaString> val = factory->NewFromASCII("not to be");
505
506 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
507 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
508 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
509 ecmaRuntimeCallInfo->SetCallArg(0, val.GetTaggedValue());
510 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(static_cast<double>(10)));
511
512 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
513 JSTaggedValue result = BuiltinsString::StartsWith(ecmaRuntimeCallInfo);
514
515 ASSERT_EQ(result.GetRawData(), JSTaggedValue::True().GetRawData());
516 }
517
518 // "To be, or not to be, that is the question.".endsWith('question.')
HWTEST_F_L0(BuiltinsStringTest,endsWith1)519 HWTEST_F_L0(BuiltinsStringTest, endsWith1)
520 {
521 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
522 JSHandle<EcmaString> thisStr = factory->NewFromASCII("To be, or not to be, that is the question.");
523 JSHandle<EcmaString> val = factory->NewFromASCII("question.");
524
525 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
526 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
527 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
528 ecmaRuntimeCallInfo->SetCallArg(0, val.GetTaggedValue());
529
530 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
531 JSTaggedValue result = BuiltinsString::EndsWith(ecmaRuntimeCallInfo);
532
533 ASSERT_EQ(result.GetRawData(), JSTaggedValue::True().GetRawData());
534 }
535
536 // "To be, or not to be, that is the question.".endsWith('to be')
HWTEST_F_L0(BuiltinsStringTest,endsWith2)537 HWTEST_F_L0(BuiltinsStringTest, endsWith2)
538 {
539 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
540 JSHandle<EcmaString> thisStr = factory->NewFromASCII("To be, or not to be, that is the question.");
541 JSHandle<EcmaString> val = factory->NewFromASCII("to be");
542
543 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
544 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
545 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
546 ecmaRuntimeCallInfo->SetCallArg(0, val.GetTaggedValue());
547
548 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
549 JSTaggedValue result = BuiltinsString::EndsWith(ecmaRuntimeCallInfo);
550
551 ASSERT_EQ(result.GetRawData(), JSTaggedValue::False().GetRawData());
552 }
553
554 // "To be, or not to be, that is the question.".endsWith('to be', 19)
HWTEST_F_L0(BuiltinsStringTest,endsWith3)555 HWTEST_F_L0(BuiltinsStringTest, endsWith3)
556 {
557 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
558 JSHandle<EcmaString> thisStr = factory->NewFromASCII("To be, or not to be, that is the question.");
559 JSHandle<EcmaString> val = factory->NewFromASCII("to be");
560
561 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
562 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
563 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
564 ecmaRuntimeCallInfo->SetCallArg(0, val.GetTaggedValue());
565 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(static_cast<double>(19)));
566
567 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
568 JSTaggedValue result = BuiltinsString::EndsWith(ecmaRuntimeCallInfo);
569
570 ASSERT_EQ(result.GetRawData(), JSTaggedValue::True().GetRawData());
571 }
572
573 // "有ABC".toLocaleLowerCase()
HWTEST_F_L0(BuiltinsStringTest,toLocaleLowerCase2)574 HWTEST_F_L0(BuiltinsStringTest, toLocaleLowerCase2)
575 {
576 ASSERT_NE(thread, nullptr);
577 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
578 JSHandle<EcmaString> this_str = factory->NewFromUtf8("有ABC");
579
580 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
581 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
582 ecmaRuntimeCallInfo->SetThis(this_str.GetTaggedValue());
583
584 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
585 JSTaggedValue result = BuiltinsString::ToLocaleLowerCase(ecmaRuntimeCallInfo);
586 ASSERT_TRUE(result.IsString());
587 JSHandle<EcmaString> result_handle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
588 JSTaggedValue test = factory->NewFromUtf8("有abc").GetTaggedValue();
589 ASSERT_EQ(EcmaStringAccessor::Compare(*result_handle, reinterpret_cast<EcmaString *>(test.GetRawData())), 0);
590 }
591
592 // "ABC".toLowerCase()
HWTEST_F_L0(BuiltinsStringTest,toLowerCase1)593 HWTEST_F_L0(BuiltinsStringTest, toLowerCase1)
594 {
595 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
596 JSHandle<EcmaString> thisStr = factory->NewFromASCII("ABC");
597
598 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
599 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
600 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
601
602 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
603 JSTaggedValue result = BuiltinsString::ToLowerCase(ecmaRuntimeCallInfo);
604 ASSERT_TRUE(result.IsString());
605 JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
606 JSHandle<EcmaString> test = factory->NewFromASCII("abc");
607 ASSERT_TRUE(JSTaggedValue::SameValue(resultHandle.GetTaggedValue(), test.GetTaggedValue()));
608 }
609
610 // "abc".toUpperCase()
HWTEST_F_L0(BuiltinsStringTest,toUpperCase1)611 HWTEST_F_L0(BuiltinsStringTest, toUpperCase1)
612 {
613 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
614 JSHandle<EcmaString> thisStr = factory->NewFromASCII("abc");
615
616 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
617 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
618 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
619
620 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
621 JSTaggedValue result = BuiltinsString::ToUpperCase(ecmaRuntimeCallInfo);
622 ASSERT_TRUE(result.IsString());
623 JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
624 JSTaggedValue test = factory->NewFromASCII("ABC").GetTaggedValue();
625 ASSERT_EQ(EcmaStringAccessor::Compare(*resultHandle, reinterpret_cast<EcmaString *>(test.GetRawData())), 0);
626 }
627
628 // "abc".localecompare('b')
HWTEST_F_L0(BuiltinsStringTest,localecompare1)629 HWTEST_F_L0(BuiltinsStringTest, localecompare1)
630 {
631 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
632 JSHandle<EcmaString> thisStr = factory->NewFromASCII("abc");
633 JSHandle<EcmaString> val = factory->NewFromASCII("b");
634
635 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
636 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
637 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
638 ecmaRuntimeCallInfo->SetCallArg(0, val.GetTaggedValue());
639
640 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
641 JSTaggedValue result = BuiltinsString::LocaleCompare(ecmaRuntimeCallInfo);
642
643 ASSERT_EQ(result.GetRawData(), JSTaggedValue(-1).GetRawData());
644 }
645
646 // "abc".localecompare('abc')
HWTEST_F_L0(BuiltinsStringTest,localecompare2)647 HWTEST_F_L0(BuiltinsStringTest, localecompare2)
648 {
649 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
650 JSHandle<EcmaString> thisStr = factory->NewFromASCII("abc");
651 JSHandle<EcmaString> val = factory->NewFromASCII("abc");
652
653 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
654 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
655 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
656 ecmaRuntimeCallInfo->SetCallArg(0, val.GetTaggedValue());
657
658 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
659 JSTaggedValue result = BuiltinsString::LocaleCompare(ecmaRuntimeCallInfo);
660
661 ASSERT_EQ(result.GetRawData(), JSTaggedValue(0).GetRawData());
662 }
663
664 // "abc".localecompare('aa')
HWTEST_F_L0(BuiltinsStringTest,localecompare3)665 HWTEST_F_L0(BuiltinsStringTest, localecompare3)
666 {
667 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
668 JSHandle<EcmaString> thisStr = factory->NewFromASCII("abc");
669 JSHandle<EcmaString> val = factory->NewFromASCII("aa");
670
671 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
672 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
673 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
674 ecmaRuntimeCallInfo->SetCallArg(0, val.GetTaggedValue());
675
676 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
677 JSTaggedValue result = BuiltinsString::LocaleCompare(ecmaRuntimeCallInfo);
678
679 ASSERT_EQ(result.GetRawData(), JSTaggedValue(1).GetRawData());
680 }
681
682 // "你好".localecompare('辅助')
HWTEST_F_L0(BuiltinsStringTest,localecompare4)683 HWTEST_F_L0(BuiltinsStringTest, localecompare4)
684 {
685 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
686 std::string referenceStr = "你好";
687 std::string compareStr = "辅助";
688 JSHandle<EcmaString> thisStr = factory->NewFromStdString(referenceStr);
689 JSHandle<EcmaString> val = factory->NewFromStdString(compareStr);
690 JSHandle<EcmaString> locale = factory->NewFromASCII("zh-Hans");
691
692 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
693 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
694 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
695 ecmaRuntimeCallInfo->SetCallArg(0, val.GetTaggedValue());
696 ecmaRuntimeCallInfo->SetCallArg(1, locale.GetTaggedValue());
697
698 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
699 JSTaggedValue result = BuiltinsString::LocaleCompare(ecmaRuntimeCallInfo);
700 TestHelper::TearDownFrame(thread, prev);
701
702 ASSERT_GT(result.GetRawData(), JSTaggedValue(0).GetRawData());
703 }
704
705 // "abc".normalize('NFC')
HWTEST_F_L0(BuiltinsStringTest,normalize1)706 HWTEST_F_L0(BuiltinsStringTest, normalize1)
707 {
708 ASSERT_NE(thread, nullptr);
709 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
710 JSHandle<EcmaString> this_str = factory->NewFromASCII("abc");
711 JSHandle<EcmaString> val = factory->NewFromASCII("NFC");
712
713 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
714 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
715 ecmaRuntimeCallInfo->SetThis(this_str.GetTaggedValue());
716 ecmaRuntimeCallInfo->SetCallArg(0, val.GetTaggedValue());
717
718 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
719 JSTaggedValue result = BuiltinsString::Normalize(ecmaRuntimeCallInfo);
720 ASSERT_TRUE(result.IsString());
721 JSHandle<EcmaString> result_handle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
722 JSTaggedValue test = factory->NewFromASCII("abc").GetTaggedValue();
723 ASSERT_EQ(EcmaStringAccessor::Compare(*result_handle, reinterpret_cast<EcmaString *>(test.GetRawData())), 0);
724 }
725
726 // "abc".repeat(5)
HWTEST_F_L0(BuiltinsStringTest,repeat1)727 HWTEST_F_L0(BuiltinsStringTest, repeat1)
728 {
729 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
730 JSHandle<EcmaString> thisStr = factory->NewFromASCII("abc");
731
732 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
733 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
734 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
735 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(5)));
736
737 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
738 JSTaggedValue result = BuiltinsString::Repeat(ecmaRuntimeCallInfo);
739 ASSERT_TRUE(result.IsString());
740 JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
741 JSTaggedValue test = factory->NewFromASCII("abcabcabcabcabc").GetTaggedValue();
742 ASSERT_EQ(EcmaStringAccessor::Compare(*resultHandle, reinterpret_cast<EcmaString *>(test.GetRawData())), 0);
743 }
744
745 // 'The morning is upon us.'.slice(4, -2)
HWTEST_F_L0(BuiltinsStringTest,slice1)746 HWTEST_F_L0(BuiltinsStringTest, slice1)
747 {
748 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
749 JSHandle<EcmaString> thisStr = factory->NewFromASCII("The morning is upon us.");
750
751 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
752 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
753 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
754 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(4)));
755 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(static_cast<double>(-2)));
756
757 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
758 JSTaggedValue result = BuiltinsString::Slice(ecmaRuntimeCallInfo);
759 ASSERT_TRUE(result.IsString());
760 JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
761 JSTaggedValue test = factory->NewFromASCII("morning is upon u").GetTaggedValue();
762 ASSERT_EQ(EcmaStringAccessor::Compare(*resultHandle, reinterpret_cast<EcmaString *>(test.GetRawData())), 0);
763 }
764
765 // 'The morning is upon us.'.slice(12)
HWTEST_F_L0(BuiltinsStringTest,slice2)766 HWTEST_F_L0(BuiltinsStringTest, slice2)
767 {
768 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
769 JSHandle<EcmaString> thisStr = factory->NewFromASCII("The morning is upon us.");
770
771 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
772 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
773 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
774 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(12)));
775
776 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
777 JSTaggedValue result = BuiltinsString::Slice(ecmaRuntimeCallInfo);
778 ASSERT_TRUE(result.IsString());
779 JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
780 JSTaggedValue test = factory->NewFromASCII("is upon us.").GetTaggedValue();
781 ASSERT_EQ(EcmaStringAccessor::Compare(*resultHandle, reinterpret_cast<EcmaString *>(test.GetRawData())), 0);
782 }
783
784 // 'Mozilla'.substring(3, -3)
HWTEST_F_L0(BuiltinsStringTest,substring1)785 HWTEST_F_L0(BuiltinsStringTest, substring1)
786 {
787 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
788 JSHandle<EcmaString> thisStr = factory->NewFromASCII("Mozilla");
789
790 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
791 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
792 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
793 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(3)));
794 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(static_cast<double>(-3)));
795
796 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
797 JSTaggedValue result = BuiltinsString::Substring(ecmaRuntimeCallInfo);
798 ASSERT_TRUE(result.IsString());
799 JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
800 JSTaggedValue test = factory->NewFromASCII("Moz").GetTaggedValue();
801 ASSERT_EQ(EcmaStringAccessor::Compare(*resultHandle, reinterpret_cast<EcmaString *>(test.GetRawData())), 0);
802 }
803
804 // 'Mozilla'.substring(7, 4)
HWTEST_F_L0(BuiltinsStringTest,substring2)805 HWTEST_F_L0(BuiltinsStringTest, substring2)
806 {
807 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
808 JSHandle<EcmaString> thisStr = factory->NewFromASCII("Mozilla");
809
810 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
811 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
812 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
813 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(7)));
814 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(static_cast<double>(4)));
815
816 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
817 JSTaggedValue result = BuiltinsString::Substring(ecmaRuntimeCallInfo);
818 ASSERT_TRUE(result.IsString());
819 JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
820 JSTaggedValue test = factory->NewFromASCII("lla").GetTaggedValue();
821 ASSERT_EQ(EcmaStringAccessor::Compare(*resultHandle, reinterpret_cast<EcmaString *>(test.GetRawData())), 0);
822 }
823
824 // " Hello world! ".trim()
HWTEST_F_L0(BuiltinsStringTest,trim1)825 HWTEST_F_L0(BuiltinsStringTest, trim1)
826 {
827 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
828 JSHandle<EcmaString> thisStr = factory->NewFromASCII(" Hello world! ");
829
830 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
831 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
832 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
833
834 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
835 JSTaggedValue result = BuiltinsString::Trim(ecmaRuntimeCallInfo);
836 ASSERT_TRUE(result.IsString());
837 JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
838 JSTaggedValue test = factory->NewFromASCII("Hello world!").GetTaggedValue();
839 ASSERT_EQ(EcmaStringAccessor::Compare(*resultHandle, reinterpret_cast<EcmaString *>(test.GetRawData())), 0);
840 }
841
HWTEST_F_L0(BuiltinsStringTest,trim2)842 HWTEST_F_L0(BuiltinsStringTest, trim2)
843 {
844 auto ecmaVM = thread->GetEcmaVM();
845 JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
846
847 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
848 JSHandle<EcmaString> thisStr = factory->NewFromASCII(" Hello world! ");
849 JSHandle<JSFunction> stringObject(env->GetStringFunction());
850 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(thisStr.GetTaggedValue().GetTaggedObject()));
851 JSHandle<JSPrimitiveRef> str = factory->NewJSPrimitiveRef(stringObject, value);
852
853 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
854 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
855 ecmaRuntimeCallInfo->SetThis(str.GetTaggedValue());
856
857 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
858 JSTaggedValue result = BuiltinsString::Trim(ecmaRuntimeCallInfo);
859 ASSERT_TRUE(result.IsString());
860 JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
861 JSTaggedValue test = factory->NewFromASCII("Hello world!").GetTaggedValue();
862 ASSERT_EQ(EcmaStringAccessor::Compare(*resultHandle, reinterpret_cast<EcmaString *>(test.GetRawData())), 0);
863 }
864
HWTEST_F_L0(BuiltinsStringTest,ToString)865 HWTEST_F_L0(BuiltinsStringTest, ToString)
866 {
867 auto ecmaVM = thread->GetEcmaVM();
868 JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
869
870 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
871 JSHandle<EcmaString> thisStr = factory->NewFromASCII("abcabcabc");
872 JSHandle<JSFunction> stringObject(env->GetStringFunction());
873 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(thisStr.GetTaggedValue().GetTaggedObject()));
874 JSHandle<JSPrimitiveRef> str = factory->NewJSPrimitiveRef(stringObject, value);
875
876 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
877 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
878 ecmaRuntimeCallInfo->SetThis(str.GetTaggedValue());
879
880 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
881 JSTaggedValue result = BuiltinsString::ToString(ecmaRuntimeCallInfo);
882 ASSERT_TRUE(result.IsString());
883 JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
884 JSTaggedValue test = JSTaggedValue(*thisStr);
885 ASSERT_EQ(result.GetRawData(), test.GetRawData());
886 }
887
HWTEST_F_L0(BuiltinsStringTest,ValueOf)888 HWTEST_F_L0(BuiltinsStringTest, ValueOf)
889 {
890 auto ecmaVM = thread->GetEcmaVM();
891 JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
892
893 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
894 JSHandle<EcmaString> thisStr = factory->NewFromASCII("abcabcabc");
895 JSHandle<JSFunction> stringObject(env->GetStringFunction());
896 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(thisStr.GetTaggedValue().GetTaggedObject()));
897 JSHandle<JSPrimitiveRef> str = factory->NewJSPrimitiveRef(stringObject, value);
898
899 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
900 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
901 ecmaRuntimeCallInfo->SetThis(str.GetTaggedValue());
902
903 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
904 JSTaggedValue result = BuiltinsString::ValueOf(ecmaRuntimeCallInfo);
905 ASSERT_TRUE(result.IsString());
906 JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
907 JSTaggedValue test = JSTaggedValue(*thisStr);
908 ASSERT_EQ(result.GetRawData(), test.GetRawData());
909 }
910
BuiltinsStringTestCreate(JSThread * thread)911 static inline JSFunction *BuiltinsStringTestCreate(JSThread *thread)
912 {
913 EcmaVM *ecmaVM = thread->GetEcmaVM();
914 JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
915 return globalEnv->GetObjectFunction().GetObject<JSFunction>();
916 }
917
HWTEST_F_L0(BuiltinsStringTest,Raw)918 HWTEST_F_L0(BuiltinsStringTest, Raw)
919 {
920 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
921 JSHandle<JSTaggedValue> foo(factory->NewFromASCII("foo"));
922 JSHandle<JSTaggedValue> bar(factory->NewFromASCII("bar"));
923 JSHandle<JSTaggedValue> baz(factory->NewFromASCII("baz"));
924 JSHandle<JSTaggedValue> rawArray = JSHandle<JSTaggedValue>::Cast(JSArray::ArrayCreate(thread, JSTaggedNumber(0)));
925 JSHandle<JSObject> obj(rawArray);
926 JSHandle<JSTaggedValue> key0(thread, JSTaggedValue(0));
927 PropertyDescriptor desc0(thread, foo);
928 JSArray::DefineOwnProperty(thread, obj, key0, desc0);
929 JSHandle<JSTaggedValue> key1(thread, JSTaggedValue(1));
930 PropertyDescriptor desc1(thread, bar);
931 JSArray::DefineOwnProperty(thread, obj, key1, desc1);
932 JSHandle<JSTaggedValue> key2(thread, JSTaggedValue(2));
933 PropertyDescriptor desc2(thread, baz);
934 JSArray::DefineOwnProperty(thread, obj, key2, desc2);
935
936 JSHandle<JSTaggedValue> constructor(thread, BuiltinsStringTestCreate(thread));
937 JSHandle<JSTaggedValue> templateString(
938 factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
939 JSHandle<JSTaggedValue> rawKey(factory->NewFromASCII("raw"));
940 JSObject::SetProperty(thread, templateString, rawKey, rawArray);
941 JSHandle<EcmaString> test = factory->NewFromASCII("foo5barJavaScriptbaz");
942
943 JSHandle<EcmaString> javascript = factory->NewFromASCII("JavaScript");
944
945 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10);
946 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
947 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
948 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(templateString.GetObject<EcmaString>()));
949 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(static_cast<int32_t>(5)));
950 ecmaRuntimeCallInfo->SetCallArg(2, javascript.GetTaggedValue());
951
952 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
953 JSTaggedValue result = BuiltinsString::Raw(ecmaRuntimeCallInfo);
954 ASSERT_TRUE(result.IsString());
955 ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(reinterpret_cast<EcmaString *>(result.GetRawData()), *test));
956 }
957
HWTEST_F_L0(BuiltinsStringTest,Replace)958 HWTEST_F_L0(BuiltinsStringTest, Replace)
959 {
960 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
961 JSHandle<EcmaString> thisStr = factory->NewFromASCII("Twas the night before Xmas...");
962 JSHandle<EcmaString> searchStr = factory->NewFromASCII("Xmas");
963 JSHandle<EcmaString> replaceStr = factory->NewFromASCII("Christmas");
964 JSHandle<EcmaString> expected = factory->NewFromASCII("Twas the night before Christmas...");
965
966 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
967 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
968 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
969 ecmaRuntimeCallInfo->SetCallArg(0, searchStr.GetTaggedValue());
970 ecmaRuntimeCallInfo->SetCallArg(1, replaceStr.GetTaggedValue());
971
972 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
973 JSTaggedValue result = BuiltinsString::Replace(ecmaRuntimeCallInfo);
974 TestHelper::TearDownFrame(thread, prev);
975
976 ASSERT_TRUE(result.IsString());
977 ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(reinterpret_cast<EcmaString *>(result.GetRawData()), *expected));
978
979 JSHandle<EcmaString> replaceStr1 = factory->NewFromASCII("abc$$");
980 JSHandle<EcmaString> expected1 = factory->NewFromASCII("Twas the night before abc$...");
981
982 auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
983 ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
984 ecmaRuntimeCallInfo1->SetThis(thisStr.GetTaggedValue());
985 ecmaRuntimeCallInfo1->SetCallArg(0, searchStr.GetTaggedValue());
986 ecmaRuntimeCallInfo1->SetCallArg(1, replaceStr1.GetTaggedValue());
987
988 prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
989 JSTaggedValue result1 = BuiltinsString::Replace(ecmaRuntimeCallInfo1);
990 TestHelper::TearDownFrame(thread, prev);
991
992 JSHandle<EcmaString> resultString1(thread, result1);
993 ASSERT_TRUE(result1.IsString());
994 ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*resultString1, *expected1));
995
996 JSHandle<EcmaString> replaceStr2 = factory->NewFromASCII("abc$$dd");
997 JSHandle<EcmaString> expected2 = factory->NewFromASCII("Twas the night before abc$dd...");
998
999 auto ecmaRuntimeCallInfo2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
1000 ecmaRuntimeCallInfo2->SetFunction(JSTaggedValue::Undefined());
1001 ecmaRuntimeCallInfo2->SetThis(thisStr.GetTaggedValue());
1002 ecmaRuntimeCallInfo2->SetCallArg(0, searchStr.GetTaggedValue());
1003 ecmaRuntimeCallInfo2->SetCallArg(1, replaceStr2.GetTaggedValue());
1004
1005 prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo2);
1006 JSTaggedValue result2 = BuiltinsString::Replace(ecmaRuntimeCallInfo2);
1007 TestHelper::TearDownFrame(thread, prev);
1008
1009 JSHandle<EcmaString> resultString2(thread, result2);
1010 ASSERT_TRUE(result2.IsString());
1011 ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*resultString2, *expected2));
1012
1013 JSHandle<EcmaString> replaceStr3 = factory->NewFromASCII("abc$&dd");
1014 JSHandle<EcmaString> expected3 = factory->NewFromASCII("Twas the night before abcXmasdd...");
1015
1016 auto ecmaRuntimeCallInfo3 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
1017 ecmaRuntimeCallInfo3->SetFunction(JSTaggedValue::Undefined());
1018 ecmaRuntimeCallInfo3->SetThis(thisStr.GetTaggedValue());
1019 ecmaRuntimeCallInfo3->SetCallArg(0, searchStr.GetTaggedValue());
1020 ecmaRuntimeCallInfo3->SetCallArg(1, replaceStr3.GetTaggedValue());
1021
1022 prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo3);
1023 JSTaggedValue result3 = BuiltinsString::Replace(ecmaRuntimeCallInfo3);
1024 TestHelper::TearDownFrame(thread, prev);
1025
1026 JSHandle<EcmaString> resultString3(thread, result3);
1027 ASSERT_TRUE(result3.IsString());
1028 ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*resultString3, *expected3));
1029
1030 JSHandle<EcmaString> replaceStr4 = factory->NewFromASCII("abc$`dd");
1031 JSHandle<EcmaString> expected4 =
1032 factory->NewFromASCII("Twas the night before abcTwas the night before dd...");
1033
1034 auto ecmaRuntimeCallInfo4 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
1035 ecmaRuntimeCallInfo4->SetFunction(JSTaggedValue::Undefined());
1036 ecmaRuntimeCallInfo4->SetThis(thisStr.GetTaggedValue());
1037 ecmaRuntimeCallInfo4->SetCallArg(0, searchStr.GetTaggedValue());
1038 ecmaRuntimeCallInfo4->SetCallArg(1, replaceStr4.GetTaggedValue());
1039
1040 prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo4);
1041 JSTaggedValue result4 = BuiltinsString::Replace(ecmaRuntimeCallInfo4);
1042 TestHelper::TearDownFrame(thread, prev);
1043
1044 JSHandle<EcmaString> resultString4(thread, result4);
1045 ASSERT_TRUE(result4.IsString());
1046 ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*resultString4, *expected4));
1047 }
1048
HWTEST_F_L0(BuiltinsStringTest,Replace2)1049 HWTEST_F_L0(BuiltinsStringTest, Replace2)
1050 {
1051 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1052 JSHandle<EcmaString> thisStr = factory->NewFromASCII("Twas the night before Xmas...");
1053 JSHandle<EcmaString> searchStr = factory->NewFromASCII("Xmas");
1054 JSHandle<EcmaString> replaceStr = factory->NewFromASCII("abc$\'dd");
1055 JSHandle<EcmaString> expected = factory->NewFromASCII("Twas the night before abc...dd...");
1056
1057 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
1058 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
1059 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
1060 ecmaRuntimeCallInfo->SetCallArg(0, searchStr.GetTaggedValue());
1061 ecmaRuntimeCallInfo->SetCallArg(1, replaceStr.GetTaggedValue());
1062
1063 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
1064 JSTaggedValue result = BuiltinsString::Replace(ecmaRuntimeCallInfo);
1065 TestHelper::TearDownFrame(thread, prev);
1066
1067 ASSERT_TRUE(result.IsString());
1068 ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(reinterpret_cast<EcmaString *>(result.GetRawData()), *expected));
1069
1070 JSHandle<EcmaString> replaceStr2 = factory->NewFromASCII("abc$`dd$\'$ff");
1071 JSHandle<EcmaString> expected2 =
1072 factory->NewFromASCII("Twas the night before abcTwas the night before dd...$ff...");
1073
1074 auto ecmaRuntimeCallInfo2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
1075 ecmaRuntimeCallInfo2->SetFunction(JSTaggedValue::Undefined());
1076 ecmaRuntimeCallInfo2->SetThis(thisStr.GetTaggedValue());
1077 ecmaRuntimeCallInfo2->SetCallArg(0, searchStr.GetTaggedValue());
1078 ecmaRuntimeCallInfo2->SetCallArg(1, replaceStr2.GetTaggedValue());
1079
1080 prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo2);
1081 JSTaggedValue result2 = BuiltinsString::Replace(ecmaRuntimeCallInfo2);
1082 TestHelper::TearDownFrame(thread, prev);
1083
1084 JSHandle<EcmaString> resultString2(thread, result2);
1085 ASSERT_TRUE(result2.IsString());
1086 ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*resultString2, *expected2));
1087
1088 JSHandle<EcmaString> replaceStr3 = factory->NewFromASCII("abc$`dd$\'$");
1089 JSHandle<EcmaString> expected3 =
1090 factory->NewFromASCII("Twas the night before abcTwas the night before dd...$...");
1091
1092 auto ecmaRuntimeCallInfo3 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
1093 ecmaRuntimeCallInfo3->SetFunction(JSTaggedValue::Undefined());
1094 ecmaRuntimeCallInfo3->SetThis(thisStr.GetTaggedValue());
1095 ecmaRuntimeCallInfo3->SetCallArg(0, searchStr.GetTaggedValue());
1096 ecmaRuntimeCallInfo3->SetCallArg(1, replaceStr3.GetTaggedValue());
1097
1098 prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo3);
1099 JSTaggedValue result3 = BuiltinsString::Replace(ecmaRuntimeCallInfo3);
1100 TestHelper::TearDownFrame(thread, prev);
1101
1102 JSHandle<EcmaString> resultString3(thread, result3);
1103 ASSERT_TRUE(result3.IsString());
1104 ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*resultString3, *expected3));
1105
1106 JSHandle<EcmaString> replaceStr4 = factory->NewFromASCII("abc$`dd$$");
1107 JSHandle<EcmaString> expected4 =
1108 factory->NewFromASCII("Twas the night before abcTwas the night before dd$...");
1109
1110 auto ecmaRuntimeCallInfo4 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
1111 ecmaRuntimeCallInfo4->SetFunction(JSTaggedValue::Undefined());
1112 ecmaRuntimeCallInfo4->SetThis(thisStr.GetTaggedValue());
1113 ecmaRuntimeCallInfo4->SetCallArg(0, searchStr.GetTaggedValue());
1114 ecmaRuntimeCallInfo4->SetCallArg(1, replaceStr4.GetTaggedValue());
1115
1116 prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo4);
1117 JSTaggedValue result4 = BuiltinsString::Replace(ecmaRuntimeCallInfo4);
1118 TestHelper::TearDownFrame(thread, prev);
1119
1120 ASSERT_TRUE(result4.IsString());
1121 JSHandle<EcmaString> resultString4(thread, result4);
1122 ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*resultString4, *expected4));
1123 }
1124
HWTEST_F_L0(BuiltinsStringTest,Replace3)1125 HWTEST_F_L0(BuiltinsStringTest, Replace3)
1126 {
1127 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1128 JSHandle<EcmaString> thisStr = factory->NewFromASCII("Twas the night before Xmas...");
1129 JSHandle<EcmaString> searchStr = factory->NewFromASCII("Xmas");
1130 JSHandle<EcmaString> replaceStr = factory->NewFromASCII("$&a $` $\' $2 $01 $$1 $21 $32 a");
1131 JSHandle<EcmaString> expected = factory->NewFromASCII(
1132 "Twas the night before Xmasa Twas the night before ... $2 $01 $1 $21 $32 a...");
1133
1134 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
1135 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
1136 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
1137 ecmaRuntimeCallInfo->SetCallArg(0, searchStr.GetTaggedValue());
1138 ecmaRuntimeCallInfo->SetCallArg(1, replaceStr.GetTaggedValue());
1139
1140 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
1141 JSTaggedValue result = BuiltinsString::Replace(ecmaRuntimeCallInfo);
1142
1143 ASSERT_TRUE(result.IsString());
1144 ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(reinterpret_cast<EcmaString *>(result.GetRawData()), *expected));
1145 }
1146
HWTEST_F_L0(BuiltinsStringTest,Replace4)1147 HWTEST_F_L0(BuiltinsStringTest, Replace4)
1148 {
1149 // invoke RegExpConstructor method
1150 JSHandle<EcmaString> pattern1 =
1151 thread->GetEcmaVM()->GetFactory()->NewFromASCII("quick\\s(brown).+?(jumps)");
1152 JSHandle<EcmaString> flags1 = thread->GetEcmaVM()->GetFactory()->NewFromASCII("iug");
1153 JSTaggedValue result1 = CreateBuiltinsStringRegExpObjByPatternAndFlags(thread, pattern1, flags1);
1154 JSHandle<JSRegExp> searchStr(thread, reinterpret_cast<JSRegExp *>(result1.GetRawData()));
1155 JSHandle<EcmaString> expected = thread->GetEcmaVM()->GetFactory()->NewFromASCII(
1156 "The Quick Brown Fox Jumpsa The Over The Lazy Dog Jumps Brown $1 Jumps1 $32 a Over The Lazy Dog");
1157
1158 // make ecma_runtime_call_info2
1159 JSHandle<EcmaString> thisStr =
1160 thread->GetEcmaVM()->GetFactory()->NewFromASCII("The Quick Brown Fox Jumps Over The Lazy Dog");
1161 JSHandle<EcmaString> replaceStr =
1162 thread->GetEcmaVM()->GetFactory()->NewFromASCII("$&a $` $\' $2 $01 $$1 $21 $32 a");
1163
1164 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
1165 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
1166 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
1167 ecmaRuntimeCallInfo->SetCallArg(0, searchStr.GetTaggedValue());
1168 ecmaRuntimeCallInfo->SetCallArg(1, replaceStr.GetTaggedValue());
1169
1170 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
1171 JSTaggedValue result = BuiltinsString::Replace(ecmaRuntimeCallInfo);
1172
1173 ASSERT_TRUE(result.IsString());
1174 ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(reinterpret_cast<EcmaString *>(result.GetRawData()), *expected));
1175 }
1176
HWTEST_F_L0(BuiltinsStringTest,Split)1177 HWTEST_F_L0(BuiltinsStringTest, Split)
1178 {
1179 // invoke RegExpConstructor method
1180 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1181 JSHandle<EcmaString> thisStr = factory->NewFromASCII("Hello World. How are you doing?");
1182 JSHandle<EcmaString> separatorStr = factory->NewFromASCII(" ");
1183 JSHandle<JSTaggedValue> limit(thread, JSTaggedValue(3));
1184 JSHandle<EcmaString> expected1 = factory->NewFromASCII("Hello");
1185 JSHandle<EcmaString> expected2 = factory->NewFromASCII("World.");
1186 JSHandle<EcmaString> expected3 = factory->NewFromASCII("How");
1187
1188 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
1189 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
1190 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
1191 ecmaRuntimeCallInfo->SetCallArg(0, separatorStr.GetTaggedValue());
1192 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(static_cast<int32_t>(3)));
1193
1194 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
1195 JSTaggedValue result = BuiltinsString::Split(ecmaRuntimeCallInfo);
1196
1197 ASSERT_TRUE(result.IsECMAObject());
1198 JSHandle<JSArray> resultArray(thread, reinterpret_cast<JSArray *>(result.GetRawData()));
1199 ASSERT_TRUE(resultArray->IsJSArray());
1200 JSHandle<JSTaggedValue> resultObj(resultArray);
1201 JSHandle<EcmaString> string1(
1202 JSObject::GetProperty(thread, resultObj, JSHandle<JSTaggedValue>(thread, JSTaggedValue(0))).GetValue());
1203 JSHandle<EcmaString> string2(
1204 JSObject::GetProperty(thread, resultObj, JSHandle<JSTaggedValue>(thread, JSTaggedValue(1))).GetValue());
1205 JSHandle<EcmaString> string3(
1206 JSObject::GetProperty(thread, resultObj, JSHandle<JSTaggedValue>(thread, JSTaggedValue(2))).GetValue());
1207 ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*string1, *expected1));
1208 ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*string2, *expected2));
1209 ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*string3, *expected3));
1210 }
1211
HWTEST_F_L0(BuiltinsStringTest,Split2)1212 HWTEST_F_L0(BuiltinsStringTest, Split2)
1213 {
1214 // invoke RegExpConstructor method
1215 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1216 JSHandle<EcmaString> thisStr = factory->NewFromASCII("a-b-c");
1217 JSHandle<EcmaString> pattern1 = factory->NewFromASCII("-");
1218 JSHandle<EcmaString> flags1 = factory->NewFromASCII("iug");
1219 JSTaggedValue result1 = CreateBuiltinsStringRegExpObjByPatternAndFlags(thread, pattern1, flags1);
1220 JSHandle<JSRegExp> separatorObj(thread, result1);
1221
1222 JSHandle<JSTaggedValue> limit(thread, JSTaggedValue(3));
1223 JSHandle<EcmaString> expected1 = factory->NewFromASCII("a");
1224 JSHandle<EcmaString> expected2 = factory->NewFromASCII("b");
1225 JSHandle<EcmaString> expected3 = factory->NewFromASCII("c");
1226
1227 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
1228 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
1229 ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
1230 ecmaRuntimeCallInfo->SetCallArg(0, separatorObj.GetTaggedValue());
1231 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(static_cast<int32_t>(3)));
1232
1233 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
1234 JSTaggedValue result = BuiltinsString::Split(ecmaRuntimeCallInfo);
1235
1236 ASSERT_TRUE(result.IsECMAObject());
1237 JSHandle<JSArray> resultArray(thread, result);
1238 ASSERT_TRUE(resultArray->IsJSArray());
1239 JSHandle<JSTaggedValue> resultObj(resultArray);
1240 JSHandle<EcmaString> string1(
1241 JSObject::GetProperty(thread, resultObj, JSHandle<JSTaggedValue>(thread, JSTaggedValue(0))).GetValue());
1242 JSHandle<EcmaString> string2(
1243 JSObject::GetProperty(thread, resultObj, JSHandle<JSTaggedValue>(thread, JSTaggedValue(1))).GetValue());
1244 JSHandle<EcmaString> string3(
1245 JSObject::GetProperty(thread, resultObj, JSHandle<JSTaggedValue>(thread, JSTaggedValue(2))).GetValue());
1246 ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*string1, *expected1));
1247 ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*string2, *expected2));
1248 ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*string3, *expected3));
1249 }
1250 } // namespace panda::test
1251