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