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_iterator.h"
17
18 #include "ecmascript/js_iterator.h"
19 #include "ecmascript/js_string_iterator.h"
20 #include "ecmascript/tests/test_helper.h"
21
22 using namespace panda::ecmascript;
23 using namespace panda::ecmascript::builtins;
24
25 namespace panda::test {
26 class BuiltinsStringIteratorTest : public BaseTestWithScope<false> {
27 };
28
CreateBuiltinsJSStringIterator(JSThread * thread,const CString keyCStr)29 static JSTaggedValue CreateBuiltinsJSStringIterator(JSThread *thread, const CString keyCStr)
30 {
31 EcmaVM *ecmaVM = thread->GetEcmaVM();
32 ObjectFactory *factory = ecmaVM->GetFactory();
33
34 JSHandle<EcmaString> string = factory->NewFromUtf8(&keyCStr[0]);
35 JSHandle<JSStringIterator> stringIterator = JSStringIterator::CreateStringIterator(thread, string);
36 EXPECT_TRUE(*stringIterator != nullptr);
37
38 return stringIterator.GetTaggedValue();
39 }
40
NextCommon(JSThread * thread,JSHandle<JSStringIterator> & strIter)41 JSTaggedValue NextCommon(JSThread* thread, JSHandle<JSStringIterator>& strIter)
42 {
43 auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
44 ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
45 ecmaRuntimeCallInfo1->SetThis(strIter.GetTaggedValue());
46
47 auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
48 auto result = BuiltinsStringIterator::Next(ecmaRuntimeCallInfo1);
49 TestHelper::TearDownFrame(thread, prev);
50 return result;
51 }
52
53 // Single char16_t Basic Multilingual plane character
HWTEST_F_L0(BuiltinsStringIteratorTest,Next_001)54 HWTEST_F_L0(BuiltinsStringIteratorTest, Next_001)
55 {
56 auto globalConst = thread->GlobalConstants();
57 CString string = "没有";
58 JSHandle<JSStringIterator> stringIterator =
59 JSHandle<JSStringIterator>(thread, CreateBuiltinsJSStringIterator(thread, string));
60 JSHandle<JSTaggedValue> valueStr = globalConst->GetHandledValueString();
61
62 NextCommon(thread, stringIterator);
63 EXPECT_EQ(stringIterator->GetStringIteratorNextIndex(), 1U);
64
65 NextCommon(thread, stringIterator);
66 EXPECT_EQ(stringIterator->GetStringIteratorNextIndex(), 2U);
67
68 JSTaggedValue result = NextCommon(thread, stringIterator);
69 JSHandle<JSTaggedValue> resultObj(thread, result);
70 EXPECT_TRUE(JSObject::GetProperty(thread, JSHandle<JSObject>(thread, result), valueStr).GetValue()->IsUndefined());
71 }
72
73 // character with lead surrogate and trail surrogate character
HWTEST_F_L0(BuiltinsStringIteratorTest,Next_002)74 HWTEST_F_L0(BuiltinsStringIteratorTest, Next_002)
75 {
76 auto globalConst = thread->GlobalConstants();
77 CString string = "没";
78 JSHandle<JSStringIterator> stringIterator =
79 JSHandle<JSStringIterator>(thread, CreateBuiltinsJSStringIterator(thread, string));
80 JSHandle<JSTaggedValue> valueStr = globalConst->GetHandledValueString();
81
82 NextCommon(thread, stringIterator);
83 EXPECT_EQ(stringIterator->GetStringIteratorNextIndex(), 1U);
84
85 NextCommon(thread, stringIterator);
86 EXPECT_EQ(stringIterator->GetStringIteratorNextIndex(), 3U);
87
88 auto result = NextCommon(thread, stringIterator);
89 JSHandle<JSTaggedValue> resultObj(thread, result);
90 EXPECT_TRUE(JSObject::GetProperty(thread, JSHandle<JSObject>(thread, result), valueStr).GetValue()->IsUndefined());
91 }
92 } // namespace panda::test
93