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/builtins/builtins_boolean.h"
174514f5e3Sopenharmony_ci
184514f5e3Sopenharmony_ci#include "ecmascript/ecma_runtime_call_info.h"
194514f5e3Sopenharmony_ci#include "ecmascript/ecma_string.h"
204514f5e3Sopenharmony_ci#include "ecmascript/ecma_vm.h"
214514f5e3Sopenharmony_ci#include "ecmascript/global_env.h"
224514f5e3Sopenharmony_ci#include "ecmascript/js_primitive_ref.h"
234514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value-inl.h"
244514f5e3Sopenharmony_ci#include "ecmascript/js_thread.h"
254514f5e3Sopenharmony_ci#include "ecmascript/object_factory.h"
264514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h"
274514f5e3Sopenharmony_ci
284514f5e3Sopenharmony_ciusing namespace panda::ecmascript;
294514f5e3Sopenharmony_ciusing namespace panda::ecmascript::builtins;
304514f5e3Sopenharmony_ciusing BuiltinsBase = panda::ecmascript::base::BuiltinsBase;
314514f5e3Sopenharmony_ci
324514f5e3Sopenharmony_cinamespace panda::test {
334514f5e3Sopenharmony_ciclass BuiltinsBooleanTest : public BaseTestWithScope<false> {
344514f5e3Sopenharmony_ci};
354514f5e3Sopenharmony_ci
364514f5e3Sopenharmony_ci// new Boolean(123)
374514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsBooleanTest, BooleanConstructor)
384514f5e3Sopenharmony_ci{
394514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
404514f5e3Sopenharmony_ci
414514f5e3Sopenharmony_ci    JSHandle<JSFunction> boolean(env->GetBooleanFunction());
424514f5e3Sopenharmony_ci    JSHandle<JSObject> globalObject(thread, env->GetGlobalObject());
434514f5e3Sopenharmony_ci
444514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*boolean), 6);
454514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(boolean.GetTaggedValue());
464514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(globalObject.GetTaggedValue());
474514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int32_t>(123)));
484514f5e3Sopenharmony_ci
494514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
504514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsBoolean::BooleanConstructor(ecmaRuntimeCallInfo);
514514f5e3Sopenharmony_ci
524514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsECMAObject());
534514f5e3Sopenharmony_ci    ASSERT_EQ(JSPrimitiveRef::Cast(result.GetTaggedObject())->GetValue().IsTrue(), 1);
544514f5e3Sopenharmony_ci}
554514f5e3Sopenharmony_ci
564514f5e3Sopenharmony_ci// new Boolean(undefined)
574514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsBooleanTest, BooleanConstructor1)
584514f5e3Sopenharmony_ci{
594514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
604514f5e3Sopenharmony_ci
614514f5e3Sopenharmony_ci    JSHandle<JSFunction> boolean(env->GetBooleanFunction());
624514f5e3Sopenharmony_ci    JSHandle<JSObject> globalObject(thread, env->GetGlobalObject());
634514f5e3Sopenharmony_ci
644514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*boolean), 6);
654514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(boolean.GetTaggedValue());
664514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(globalObject.GetTaggedValue());
674514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue::Undefined());
684514f5e3Sopenharmony_ci
694514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
704514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsBoolean::BooleanConstructor(ecmaRuntimeCallInfo);
714514f5e3Sopenharmony_ci
724514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsECMAObject());
734514f5e3Sopenharmony_ci    ASSERT_EQ(JSPrimitiveRef::Cast(result.GetTaggedObject())->GetValue().IsFalse(), 1);
744514f5e3Sopenharmony_ci}
754514f5e3Sopenharmony_ci
764514f5e3Sopenharmony_ci// Boolean("helloworld")
774514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsBooleanTest, BooleanConstructor2)
784514f5e3Sopenharmony_ci{
794514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
804514f5e3Sopenharmony_ci
814514f5e3Sopenharmony_ci    JSHandle<JSFunction> boolean(env->GetBooleanFunction());
824514f5e3Sopenharmony_ci    JSHandle<JSObject> globalObject(thread, env->GetGlobalObject());
834514f5e3Sopenharmony_ci    JSHandle<EcmaString> str = thread->GetEcmaVM()->GetFactory()->NewFromASCII("helloworld");
844514f5e3Sopenharmony_ci
854514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
864514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(boolean.GetTaggedValue());
874514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(globalObject.GetTaggedValue());
884514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, str.GetTaggedValue());
894514f5e3Sopenharmony_ci
904514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
914514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsBoolean::BooleanConstructor(ecmaRuntimeCallInfo);
924514f5e3Sopenharmony_ci
934514f5e3Sopenharmony_ci    JSTaggedValue ruler = BuiltinsBase::GetTaggedBoolean(true);
944514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), ruler.GetRawData());
954514f5e3Sopenharmony_ci}
964514f5e3Sopenharmony_ci
974514f5e3Sopenharmony_ci// false.toString()
984514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsBooleanTest, BooleanPrototypeToString)
994514f5e3Sopenharmony_ci{
1004514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
1014514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
1024514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(JSTaggedValue::False());
1034514f5e3Sopenharmony_ci
1044514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
1054514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsBoolean::BooleanPrototypeToString(ecmaRuntimeCallInfo);
1064514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
1074514f5e3Sopenharmony_ci    JSHandle<EcmaString> res(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
1084514f5e3Sopenharmony_ci    auto ruler = thread->GetEcmaVM()->GetFactory()->NewFromASCII("false");
1094514f5e3Sopenharmony_ci    ASSERT_EQ(EcmaStringAccessor::Compare(instance, res, ruler), 0);
1104514f5e3Sopenharmony_ci}
1114514f5e3Sopenharmony_ci
1124514f5e3Sopenharmony_ci// (new Boolean(true)).toString()
1134514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsBooleanTest, BooleanPrototypeToString1)
1144514f5e3Sopenharmony_ci{
1154514f5e3Sopenharmony_ci    auto ecmaVM = thread->GetEcmaVM();
1164514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
1174514f5e3Sopenharmony_ci
1184514f5e3Sopenharmony_ci    JSHandle<JSFunction> booleanObject(env->GetBooleanFunction());
1194514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> value(thread, JSTaggedValue::True());
1204514f5e3Sopenharmony_ci    JSHandle<JSPrimitiveRef> boolean = thread->GetEcmaVM()->GetFactory()->NewJSPrimitiveRef(booleanObject, value);
1214514f5e3Sopenharmony_ci
1224514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
1234514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
1244514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(boolean.GetTaggedValue());
1254514f5e3Sopenharmony_ci
1264514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
1274514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsBoolean::BooleanPrototypeToString(ecmaRuntimeCallInfo);
1284514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
1294514f5e3Sopenharmony_ci    JSHandle<EcmaString> res(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
1304514f5e3Sopenharmony_ci    auto ruler = thread->GetEcmaVM()->GetFactory()->NewFromASCII("true");
1314514f5e3Sopenharmony_ci    ASSERT_EQ(EcmaStringAccessor::Compare(instance, res, ruler), 0);
1324514f5e3Sopenharmony_ci}
1334514f5e3Sopenharmony_ci
1344514f5e3Sopenharmony_ci// true.valueOf()
1354514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsBooleanTest, BooleanPrototypeValueOf)
1364514f5e3Sopenharmony_ci{
1374514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
1384514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
1394514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(JSTaggedValue::True());
1404514f5e3Sopenharmony_ci
1414514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
1424514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsBoolean::BooleanPrototypeValueOf(ecmaRuntimeCallInfo);
1434514f5e3Sopenharmony_ci
1444514f5e3Sopenharmony_ci    JSTaggedValue ruler = BuiltinsBase::GetTaggedBoolean(true);
1454514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), ruler.GetRawData());
1464514f5e3Sopenharmony_ci}
1474514f5e3Sopenharmony_ci
1484514f5e3Sopenharmony_ci// (new Boolean(false)).valueOf()
1494514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsBooleanTest, BooleanPrototypeValueOf1)
1504514f5e3Sopenharmony_ci{
1514514f5e3Sopenharmony_ci    auto ecmaVM = thread->GetEcmaVM();
1524514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
1534514f5e3Sopenharmony_ci
1544514f5e3Sopenharmony_ci    JSHandle<JSFunction> booleanObject(env->GetBooleanFunction());
1554514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> value(thread, JSTaggedValue::False());
1564514f5e3Sopenharmony_ci    JSHandle<JSPrimitiveRef> boolean = thread->GetEcmaVM()->GetFactory()->NewJSPrimitiveRef(booleanObject, value);
1574514f5e3Sopenharmony_ci
1584514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
1594514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
1604514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(boolean.GetTaggedValue());
1614514f5e3Sopenharmony_ci
1624514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
1634514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsBoolean::BooleanPrototypeValueOf(ecmaRuntimeCallInfo);
1644514f5e3Sopenharmony_ci
1654514f5e3Sopenharmony_ci    JSTaggedValue ruler = BuiltinsBase::GetTaggedBoolean(false);
1664514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), ruler.GetRawData());
1674514f5e3Sopenharmony_ci}
1684514f5e3Sopenharmony_ci}  // namespace panda::test
169