• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/base/builtins_base.h"
17 #include "ecmascript/ecma_runtime_call_info.h"
18 #include "ecmascript/global_env.h"
19 #include "ecmascript/tests/test_helper.h"
20 
21 using namespace panda::ecmascript;
22 using namespace panda::ecmascript::base;
23 
24 namespace panda::test {
25 using ArgsPosition = BuiltinsBase::ArgsPosition;
26 class BuiltinsBaseTest : public testing::Test {
27 public:
SetUpTestCase()28     static void SetUpTestCase()
29     {
30         GTEST_LOG_(INFO) << "SetUpTestCase";
31     }
32 
TearDownTestCase()33     static void TearDownTestCase()
34     {
35         GTEST_LOG_(INFO) << "TearDownCase";
36     }
37 
SetUp()38     void SetUp() override
39     {
40         TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
41     }
42 
TearDown()43     void TearDown() override
44     {
45         TestHelper::DestroyEcmaVMWithScope(instance, scope);
46     }
47 
48     EcmaVM *instance {nullptr};
49     EcmaHandleScope *scope {nullptr};
50     JSThread *thread {nullptr};
51 };
52 
53 /**
54  * @tc.name: GetArgsArray
55  * @tc.desc: Create msgs through "CreateEcmaRuntimeCallInfo" function,Set ArgsNumber and CallArg ,then call
56  *           the "GetArgsArray" function to get an array value is within expectations.
57  * @tc.type: FUNC
58  * @tc.require:
59  */
HWTEST_F_L0(BuiltinsBaseTest,GetArgsArray)60 HWTEST_F_L0(BuiltinsBaseTest, GetArgsArray)
61 {
62     uint32_t argvLength = 10;
63     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), argvLength);
64     ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(1));
65     ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(2));
66     ecmaRuntimeCallInfo->SetCallArg(2, JSTaggedValue(3));
67 
68     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
69     JSHandle<TaggedArray> resultArray = BuiltinsBase::GetArgsArray(ecmaRuntimeCallInfo);
70     TestHelper::TearDownFrame(thread, prev);
71 
72     EXPECT_EQ(resultArray->GetLength(), 3U);
73     EXPECT_EQ(resultArray->Get(0).GetInt(), 1);
74     EXPECT_EQ(resultArray->Get(1).GetInt(), 2);
75     EXPECT_EQ(resultArray->Get(2).GetInt(), 3);
76 }
77 
78 /**
79  * @tc.name: BuiltinsBase_info_Get
80  * @tc.desc: Create msgs through "CreateEcmaRuntimeCallInfo" function,then through "SetFunction","SetThis","SetCallArg"
81  *           function set msgs,check result returned through "GetConstructor","GetFunction","GetThis","GetCallArg"
82  *           function from BuiltinsBase is within expectations.
83  * @tc.type: FUNC
84  * @tc.require:
85  */
HWTEST_F_L0(BuiltinsBaseTest,BuiltinsBase_info_Get)86 HWTEST_F_L0(BuiltinsBaseTest, BuiltinsBase_info_Get)
87 {
88     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
89     JSHandle<JSFunction> handleFunction(env->GetProxyFunction());
90     JSHandle<JSObject> handleNewTarget(thread, env->GetGlobalObject());
91 
92     auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10);
93     ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
94     ecmaRuntimeCallInfo1->SetThis(JSTaggedValue::Undefined());
95     ecmaRuntimeCallInfo1->SetCallArg(0, JSTaggedValue(ArgsPosition::FIRST));
96     ecmaRuntimeCallInfo1->SetCallArg(1, JSTaggedValue(ArgsPosition::SECOND));
97     ecmaRuntimeCallInfo1->SetCallArg(2, JSTaggedValue(ArgsPosition::FOURTH));
98     [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
99 
100     EXPECT_TRUE(BuiltinsBase::GetConstructor(ecmaRuntimeCallInfo1)->IsUndefined());
101     EXPECT_TRUE(BuiltinsBase::GetNewTarget(ecmaRuntimeCallInfo1)->IsUndefined());
102     EXPECT_EQ(BuiltinsBase::GetCallArg(ecmaRuntimeCallInfo1, 0)->GetInt(), 0);
103     EXPECT_EQ(BuiltinsBase::GetCallArg(ecmaRuntimeCallInfo1, 1)->GetInt(), 1);
104     EXPECT_EQ(BuiltinsBase::GetCallArg(ecmaRuntimeCallInfo1, 2)->GetInt(), 3);
105     TestHelper::TearDownFrame(thread, prev1);
106 
107     auto ecmaRuntimeCallInfo2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*handleNewTarget), 6);
108     ecmaRuntimeCallInfo2->SetFunction(handleFunction.GetTaggedValue());
109     ecmaRuntimeCallInfo2->SetThis(handleNewTarget.GetTaggedValue());
110     ecmaRuntimeCallInfo2->SetCallArg(0, JSTaggedValue::Undefined());
111     [[maybe_unused]] auto prev2 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo2);
112 
113     EXPECT_TRUE(BuiltinsBase::GetConstructor(ecmaRuntimeCallInfo2)->IsJSFunction());
114     EXPECT_TRUE(BuiltinsBase::GetNewTarget(ecmaRuntimeCallInfo2)->IsJSGlobalObject());
115     EXPECT_TRUE(BuiltinsBase::GetCallArg(ecmaRuntimeCallInfo2, 0)->IsUndefined());
116     TestHelper::TearDownFrame(thread, prev2);
117 }
118 
119 /**
120  * @tc.name: GetTaggedString
121  * @tc.desc: Check whether the result returned through "GetTaggedString" function is within expectations.
122  * @tc.type: FUNC
123  * @tc.require:
124  */
HWTEST_F_L0(BuiltinsBaseTest,GetTaggedString)125 HWTEST_F_L0(BuiltinsBaseTest, GetTaggedString)
126 {
127     char BuiltinsBaseStr1[] = "BuiltinsBase";
128     JSTaggedValue resultStr1 = BuiltinsBase::GetTaggedString(thread, BuiltinsBaseStr1);
129     EXPECT_TRUE(resultStr1.IsString());
130     JSHandle<EcmaString> handleEcmaStr1(thread, resultStr1);
131     EXPECT_STREQ(EcmaStringAccessor(handleEcmaStr1).ToCString().c_str(), "BuiltinsBase");
132 
133     char BuiltinsBaseStr2[] = ""; // Empty String
134     JSTaggedValue resultStr2 = BuiltinsBase::GetTaggedString(thread, BuiltinsBaseStr2);
135     EXPECT_TRUE(resultStr2.IsString());
136     JSHandle<EcmaString> handleEcmaStr2(thread, resultStr2);
137     EXPECT_STREQ(EcmaStringAccessor(handleEcmaStr2).ToCString().c_str(), "");
138 }
139 
140 /**
141  * @tc.name: GetTaggedInt
142  * @tc.desc: Check whether the result returned through "GetTaggedInt" function is within expectations.
143  * @tc.type: FUNC
144  * @tc.require:
145  */
HWTEST_F_L0(BuiltinsBaseTest,GetTaggedInt)146 HWTEST_F_L0(BuiltinsBaseTest, GetTaggedInt)
147 {
148     EXPECT_EQ(BuiltinsBase::GetTaggedInt(1).GetInt(), 1);
149     EXPECT_EQ(BuiltinsBase::GetTaggedInt(9).GetInt(), 9);
150 }
151 
152 /**
153  * @tc.name: GetTaggedDouble
154  * @tc.desc: Check whether the result returned through "GetTaggedDouble" function is within expectations.
155  * @tc.type: FUNC
156  * @tc.require:
157  */
HWTEST_F_L0(BuiltinsBaseTest,GetTaggedDouble)158 HWTEST_F_L0(BuiltinsBaseTest, GetTaggedDouble)
159 {
160     EXPECT_EQ(BuiltinsBase::GetTaggedDouble(1.1100).GetNumber(), 1.1100);
161     EXPECT_EQ(BuiltinsBase::GetTaggedDouble(9.1200).GetNumber(), 9.1200);
162 }
163 
164 /**
165  * @tc.name: GetTaggedBoolean
166  * @tc.desc: Check whether the result returned through "GetTaggedBoolean" function is within expectations.
167  * @tc.type: FUNC
168  * @tc.require:
169  */
HWTEST_F_L0(BuiltinsBaseTest,GetTaggedBoolean)170 HWTEST_F_L0(BuiltinsBaseTest, GetTaggedBoolean)
171 {
172     EXPECT_EQ(BuiltinsBase::GetTaggedBoolean(false).GetRawData(), JSTaggedValue::False().GetRawData());
173     EXPECT_EQ(BuiltinsBase::GetTaggedBoolean(true).GetRawData(),  JSTaggedValue::True().GetRawData());
174 }
175 }  // namespace panda::test