• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ecmascript/js_regexp_iterator.h"
17 #include "ecmascript/builtins/builtins_regexp.h"
18 #include "ecmascript/global_env.h"
19 #include "ecmascript/js_regexp.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 using BuiltinsRegExp = builtins::BuiltinsRegExp;
27 class JSRegexpIteratorTest : public testing::Test {
28 public:
SetUpTestCase()29     static void SetUpTestCase()
30     {
31         GTEST_LOG_(INFO) << "SetUpTestCase";
32     }
33 
TearDownTestCase()34     static void TearDownTestCase()
35     {
36         GTEST_LOG_(INFO) << "TearDownCase";
37     }
38 
SetUp()39     void SetUp() override
40     {
41         TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
42     }
43 
TearDown()44     void TearDown() override
45     {
46         TestHelper::DestroyEcmaVMWithScope(instance, scope);
47     }
48 
49     EcmaVM *instance {nullptr};
50     ecmascript::EcmaHandleScope *scope {nullptr};
51     JSThread *thread {nullptr};
52 };
53 
CreateJSRegexpByPatternAndFlags(JSThread * thread,const JSHandle<EcmaString> & pattern,const JSHandle<EcmaString> & flags)54 static JSTaggedValue CreateJSRegexpByPatternAndFlags(JSThread *thread, const JSHandle<EcmaString> &pattern,
55                                                      const JSHandle<EcmaString> &flags)
56 {
57     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
58     JSHandle<JSFunction> regexp(env->GetRegExpFunction());
59     JSHandle<JSObject> globalObject(thread, env->GetGlobalObject());
60     // 8 : test case
61     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*regexp), 8);
62     ecmaRuntimeCallInfo->SetFunction(regexp.GetTaggedValue());
63     ecmaRuntimeCallInfo->SetThis(globalObject.GetTaggedValue());
64     ecmaRuntimeCallInfo->SetCallArg(0, pattern.GetTaggedValue());
65     ecmaRuntimeCallInfo->SetCallArg(1, flags.GetTaggedValue());
66 
67     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
68     // call RegExpConstructor method
69     JSTaggedValue result = BuiltinsRegExp::RegExpConstructor(ecmaRuntimeCallInfo);
70     TestHelper::TearDownFrame(thread, prev);
71     return result;
72 }
73 
HWTEST_F_L0(JSRegexpIteratorTest,CreateRegExpStringIterator)74 HWTEST_F_L0(JSRegexpIteratorTest, CreateRegExpStringIterator)
75 {
76     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
77     JSHandle<EcmaString> pattern = factory->NewFromASCII("\\w+");
78     JSHandle<EcmaString> flags = factory->NewFromASCII("gim");
79 
80     JSHandle<JSTaggedValue> matchHandle(thread, CreateJSRegexpByPatternAndFlags(thread, pattern, flags));
81     JSHandle<EcmaString> inputStr = factory->NewFromASCII("g");
82     JSHandle<JSTaggedValue> regExpIterator =
83         JSRegExpIterator::CreateRegExpStringIterator(thread, matchHandle, inputStr, true, false);
84     EXPECT_TRUE(regExpIterator->IsJSRegExpIterator());
85     regExpIterator =
86         JSRegExpIterator::CreateRegExpStringIterator(thread, matchHandle, inputStr, false, false);
87     EXPECT_TRUE(regExpIterator->IsJSRegExpIterator());
88 }
89 
HWTEST_F_L0(JSRegexpIteratorTest,Next)90 HWTEST_F_L0(JSRegexpIteratorTest, Next)
91 {
92     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
93     JSHandle<EcmaString> pattern = factory->NewFromASCII("-[0-9]+");
94     JSHandle<EcmaString> flags = factory->NewFromASCII("g");
95     JSHandle<EcmaString> inputStr = factory->NewFromASCII("2016-01-02|2019-03-04");
96     JSHandle<JSTaggedValue> zero(factory->NewFromASCII("0"));
97     JSHandle<JSTaggedValue> barZero(factory->NewFromASCII("-0"));
98 
99     JSTaggedValue jsRegExp = CreateJSRegexpByPatternAndFlags(thread, pattern, flags);
100     JSHandle<JSRegExp> ObjValue(thread, reinterpret_cast<JSRegExp *>(jsRegExp.GetRawData()));
101     // create regExp iterator
102     auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
103     ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
104     ecmaRuntimeCallInfo1->SetThis(ObjValue.GetTaggedValue());
105     ecmaRuntimeCallInfo1->SetCallArg(0, inputStr.GetTaggedValue());
106     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
107     JSTaggedValue iteratorValue = BuiltinsRegExp::MatchAll(ecmaRuntimeCallInfo1);
108     TestHelper::TearDownFrame(thread, prev);
109 
110     JSHandle<JSRegExpIterator> regExpIterator(thread, reinterpret_cast<JSRegExpIterator *>(iteratorValue.GetRawData()));
111     uint32_t matchLength = 4; // 4 : 4 Number of matches
112     // traversal regExp iterator
113     for (uint32_t i = 0; i <= matchLength; i++) {
114         auto ecmaRuntimeCallInfo2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
115         ecmaRuntimeCallInfo2->SetFunction(JSTaggedValue::Undefined());
116         ecmaRuntimeCallInfo2->SetThis(regExpIterator.GetTaggedValue());
117 
118         prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo2);
119         JSTaggedValue result = JSRegExpIterator::Next(ecmaRuntimeCallInfo2);
120         TestHelper::TearDownFrame(thread, prev);
121 
122         JSHandle<JSTaggedValue> matchObj(thread, result);
123         if (i <= matchLength - 1) {
124             JSHandle<JSTaggedValue> resultValue(thread, JSTaggedValue(i + 1));
125             JSHandle<EcmaString> compareVal =
126                 factory->ConcatFromString(JSHandle<EcmaString>(barZero), JSTaggedValue::ToString(thread, resultValue));
127             JSHandle<JSTaggedValue> matchResult = JSIterator::IteratorValue(thread, matchObj);
128             JSHandle<JSTaggedValue> zeroHandle(JSObject::GetProperty(thread, matchResult, zero).GetValue());
129             JSHandle<EcmaString> outputZero = JSTaggedValue::ToString(thread, zeroHandle);
130             EXPECT_EQ(EcmaStringAccessor::Compare(instance, outputZero, compareVal), 0);
131             EXPECT_FALSE(regExpIterator->GetDone());
132         }
133         else {
134             EXPECT_TRUE(regExpIterator->GetDone());
135             EXPECT_EQ(JSIterator::IteratorValue(thread, matchObj).GetTaggedValue(), JSTaggedValue::Undefined());
136         }
137     }
138 }
139 } // namespace panda::test