14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#include "ecmascript/base/builtins_base.h" 174514f5e3Sopenharmony_ci#include "ecmascript/ecma_runtime_call_info.h" 184514f5e3Sopenharmony_ci#include "ecmascript/global_env.h" 194514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h" 204514f5e3Sopenharmony_ci 214514f5e3Sopenharmony_ciusing namespace panda::ecmascript; 224514f5e3Sopenharmony_ciusing namespace panda::ecmascript::base; 234514f5e3Sopenharmony_ci 244514f5e3Sopenharmony_cinamespace panda::test { 254514f5e3Sopenharmony_ciusing ArgsPosition = BuiltinsBase::ArgsPosition; 264514f5e3Sopenharmony_ciclass BuiltinsBaseTest : public BaseTestWithScope<false> { 274514f5e3Sopenharmony_ci}; 284514f5e3Sopenharmony_ci 294514f5e3Sopenharmony_ci/** 304514f5e3Sopenharmony_ci * @tc.name: GetArgsArray 314514f5e3Sopenharmony_ci * @tc.desc: Create msgs through "CreateEcmaRuntimeCallInfo" function,Set ArgsNumber and CallArg ,then call 324514f5e3Sopenharmony_ci * the "GetArgsArray" function to get an array value is within expectations. 334514f5e3Sopenharmony_ci * @tc.type: FUNC 344514f5e3Sopenharmony_ci * @tc.require: 354514f5e3Sopenharmony_ci */ 364514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsBaseTest, GetArgsArray) 374514f5e3Sopenharmony_ci{ 384514f5e3Sopenharmony_ci uint32_t argvLength = 10; 394514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), argvLength); 404514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(1)); 414514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(2)); 424514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(2, JSTaggedValue(3)); 434514f5e3Sopenharmony_ci 444514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 454514f5e3Sopenharmony_ci JSHandle<TaggedArray> resultArray = BuiltinsBase::GetArgsArray(ecmaRuntimeCallInfo); 464514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 474514f5e3Sopenharmony_ci 484514f5e3Sopenharmony_ci EXPECT_EQ(resultArray->GetLength(), 3U); 494514f5e3Sopenharmony_ci EXPECT_EQ(resultArray->Get(0).GetInt(), 1); 504514f5e3Sopenharmony_ci EXPECT_EQ(resultArray->Get(1).GetInt(), 2); 514514f5e3Sopenharmony_ci EXPECT_EQ(resultArray->Get(2).GetInt(), 3); 524514f5e3Sopenharmony_ci} 534514f5e3Sopenharmony_ci 544514f5e3Sopenharmony_ci/** 554514f5e3Sopenharmony_ci * @tc.name: BuiltinsBase_info_Get 564514f5e3Sopenharmony_ci * @tc.desc: Create msgs through "CreateEcmaRuntimeCallInfo" function,then through "SetFunction","SetThis","SetCallArg" 574514f5e3Sopenharmony_ci * function set msgs,check result returned through "GetConstructor","GetFunction","GetThis","GetCallArg" 584514f5e3Sopenharmony_ci * function from BuiltinsBase is within expectations. 594514f5e3Sopenharmony_ci * @tc.type: FUNC 604514f5e3Sopenharmony_ci * @tc.require: 614514f5e3Sopenharmony_ci */ 624514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsBaseTest, BuiltinsBase_info_Get) 634514f5e3Sopenharmony_ci{ 644514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 654514f5e3Sopenharmony_ci JSHandle<JSFunction> handleFunction(env->GetProxyFunction()); 664514f5e3Sopenharmony_ci JSHandle<JSObject> handleNewTarget(thread, env->GetGlobalObject()); 674514f5e3Sopenharmony_ci 684514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10); 694514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined()); 704514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetThis(JSTaggedValue::Undefined()); 714514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetCallArg(0, JSTaggedValue(ArgsPosition::FIRST)); 724514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetCallArg(1, JSTaggedValue(ArgsPosition::SECOND)); 734514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetCallArg(2, JSTaggedValue(ArgsPosition::FOURTH)); 744514f5e3Sopenharmony_ci [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1); 754514f5e3Sopenharmony_ci 764514f5e3Sopenharmony_ci EXPECT_TRUE(BuiltinsBase::GetConstructor(ecmaRuntimeCallInfo1)->IsUndefined()); 774514f5e3Sopenharmony_ci EXPECT_TRUE(BuiltinsBase::GetNewTarget(ecmaRuntimeCallInfo1)->IsUndefined()); 784514f5e3Sopenharmony_ci EXPECT_EQ(BuiltinsBase::GetCallArg(ecmaRuntimeCallInfo1, 0)->GetInt(), 0); 794514f5e3Sopenharmony_ci EXPECT_EQ(BuiltinsBase::GetCallArg(ecmaRuntimeCallInfo1, 1)->GetInt(), 1); 804514f5e3Sopenharmony_ci EXPECT_EQ(BuiltinsBase::GetCallArg(ecmaRuntimeCallInfo1, 2)->GetInt(), 3); 814514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev1); 824514f5e3Sopenharmony_ci 834514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*handleNewTarget), 6); 844514f5e3Sopenharmony_ci ecmaRuntimeCallInfo2->SetFunction(handleFunction.GetTaggedValue()); 854514f5e3Sopenharmony_ci ecmaRuntimeCallInfo2->SetThis(handleNewTarget.GetTaggedValue()); 864514f5e3Sopenharmony_ci ecmaRuntimeCallInfo2->SetCallArg(0, JSTaggedValue::Undefined()); 874514f5e3Sopenharmony_ci [[maybe_unused]] auto prev2 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo2); 884514f5e3Sopenharmony_ci 894514f5e3Sopenharmony_ci EXPECT_TRUE(BuiltinsBase::GetConstructor(ecmaRuntimeCallInfo2)->IsJSFunction()); 904514f5e3Sopenharmony_ci EXPECT_TRUE(BuiltinsBase::GetNewTarget(ecmaRuntimeCallInfo2)->IsJSGlobalObject()); 914514f5e3Sopenharmony_ci EXPECT_TRUE(BuiltinsBase::GetCallArg(ecmaRuntimeCallInfo2, 0)->IsUndefined()); 924514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev2); 934514f5e3Sopenharmony_ci} 944514f5e3Sopenharmony_ci 954514f5e3Sopenharmony_ci/** 964514f5e3Sopenharmony_ci * @tc.name: GetTaggedString 974514f5e3Sopenharmony_ci * @tc.desc: Check whether the result returned through "GetTaggedString" function is within expectations. 984514f5e3Sopenharmony_ci * @tc.type: FUNC 994514f5e3Sopenharmony_ci * @tc.require: 1004514f5e3Sopenharmony_ci */ 1014514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsBaseTest, GetTaggedString) 1024514f5e3Sopenharmony_ci{ 1034514f5e3Sopenharmony_ci char BuiltinsBaseStr1[] = "BuiltinsBase"; 1044514f5e3Sopenharmony_ci JSTaggedValue resultStr1 = BuiltinsBase::GetTaggedString(thread, BuiltinsBaseStr1); 1054514f5e3Sopenharmony_ci EXPECT_TRUE(resultStr1.IsString()); 1064514f5e3Sopenharmony_ci JSHandle<EcmaString> handleEcmaStr1(thread, resultStr1); 1074514f5e3Sopenharmony_ci EXPECT_STREQ(EcmaStringAccessor(handleEcmaStr1).ToCString().c_str(), "BuiltinsBase"); 1084514f5e3Sopenharmony_ci 1094514f5e3Sopenharmony_ci char BuiltinsBaseStr2[] = ""; // Empty String 1104514f5e3Sopenharmony_ci JSTaggedValue resultStr2 = BuiltinsBase::GetTaggedString(thread, BuiltinsBaseStr2); 1114514f5e3Sopenharmony_ci EXPECT_TRUE(resultStr2.IsString()); 1124514f5e3Sopenharmony_ci JSHandle<EcmaString> handleEcmaStr2(thread, resultStr2); 1134514f5e3Sopenharmony_ci EXPECT_STREQ(EcmaStringAccessor(handleEcmaStr2).ToCString().c_str(), ""); 1144514f5e3Sopenharmony_ci} 1154514f5e3Sopenharmony_ci 1164514f5e3Sopenharmony_ci/** 1174514f5e3Sopenharmony_ci * @tc.name: GetTaggedInt 1184514f5e3Sopenharmony_ci * @tc.desc: Check whether the result returned through "GetTaggedInt" function is within expectations. 1194514f5e3Sopenharmony_ci * @tc.type: FUNC 1204514f5e3Sopenharmony_ci * @tc.require: 1214514f5e3Sopenharmony_ci */ 1224514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsBaseTest, GetTaggedInt) 1234514f5e3Sopenharmony_ci{ 1244514f5e3Sopenharmony_ci EXPECT_EQ(BuiltinsBase::GetTaggedInt(1).GetInt(), 1); 1254514f5e3Sopenharmony_ci EXPECT_EQ(BuiltinsBase::GetTaggedInt(9).GetInt(), 9); 1264514f5e3Sopenharmony_ci} 1274514f5e3Sopenharmony_ci 1284514f5e3Sopenharmony_ci/** 1294514f5e3Sopenharmony_ci * @tc.name: GetTaggedDouble 1304514f5e3Sopenharmony_ci * @tc.desc: Check whether the result returned through "GetTaggedDouble" function is within expectations. 1314514f5e3Sopenharmony_ci * @tc.type: FUNC 1324514f5e3Sopenharmony_ci * @tc.require: 1334514f5e3Sopenharmony_ci */ 1344514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsBaseTest, GetTaggedDouble) 1354514f5e3Sopenharmony_ci{ 1364514f5e3Sopenharmony_ci EXPECT_EQ(BuiltinsBase::GetTaggedDouble(1.1100).GetNumber(), 1.1100); 1374514f5e3Sopenharmony_ci EXPECT_EQ(BuiltinsBase::GetTaggedDouble(9.1200).GetNumber(), 9.1200); 1384514f5e3Sopenharmony_ci} 1394514f5e3Sopenharmony_ci 1404514f5e3Sopenharmony_ci/** 1414514f5e3Sopenharmony_ci * @tc.name: GetTaggedBoolean 1424514f5e3Sopenharmony_ci * @tc.desc: Check whether the result returned through "GetTaggedBoolean" function is within expectations. 1434514f5e3Sopenharmony_ci * @tc.type: FUNC 1444514f5e3Sopenharmony_ci * @tc.require: 1454514f5e3Sopenharmony_ci */ 1464514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsBaseTest, GetTaggedBoolean) 1474514f5e3Sopenharmony_ci{ 1484514f5e3Sopenharmony_ci EXPECT_EQ(BuiltinsBase::GetTaggedBoolean(false).GetRawData(), JSTaggedValue::False().GetRawData()); 1494514f5e3Sopenharmony_ci EXPECT_EQ(BuiltinsBase::GetTaggedBoolean(true).GetRawData(), JSTaggedValue::True().GetRawData()); 1504514f5e3Sopenharmony_ci} 1514514f5e3Sopenharmony_ci} // namespace panda::test