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/builtins/builtins_boolean.h"
17
18 #include "ecmascript/ecma_runtime_call_info.h"
19 #include "ecmascript/ecma_string.h"
20 #include "ecmascript/ecma_vm.h"
21 #include "ecmascript/global_env.h"
22 #include "ecmascript/js_primitive_ref.h"
23 #include "ecmascript/js_tagged_value-inl.h"
24 #include "ecmascript/js_thread.h"
25 #include "ecmascript/object_factory.h"
26 #include "ecmascript/tests/test_helper.h"
27
28 using namespace panda::ecmascript;
29 using namespace panda::ecmascript::builtins;
30 using BuiltinsBase = panda::ecmascript::base::BuiltinsBase;
31
32 namespace panda::test {
33 class BuiltinsBooleanTest : public testing::Test {
34 public:
SetUpTestCase()35 static void SetUpTestCase()
36 {
37 GTEST_LOG_(INFO) << "SetUpTestCase";
38 }
39
TearDownTestCase()40 static void TearDownTestCase()
41 {
42 GTEST_LOG_(INFO) << "TearDownCase";
43 }
44
SetUp()45 void SetUp() override
46 {
47 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
48 }
49
TearDown()50 void TearDown() override
51 {
52 TestHelper::DestroyEcmaVMWithScope(instance, scope);
53 }
54
55 EcmaVM *instance {nullptr};
56 EcmaHandleScope *scope {nullptr};
57 JSThread *thread {nullptr};
58 };
59
60 // new Boolean(123)
HWTEST_F_L0(BuiltinsBooleanTest,BooleanConstructor)61 HWTEST_F_L0(BuiltinsBooleanTest, BooleanConstructor)
62 {
63 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
64
65 JSHandle<JSFunction> boolean(env->GetBooleanFunction());
66 JSHandle<JSObject> globalObject(thread, env->GetGlobalObject());
67
68 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*boolean), 6);
69 ecmaRuntimeCallInfo->SetFunction(boolean.GetTaggedValue());
70 ecmaRuntimeCallInfo->SetThis(globalObject.GetTaggedValue());
71 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int32_t>(123)));
72
73 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
74 JSTaggedValue result = BuiltinsBoolean::BooleanConstructor(ecmaRuntimeCallInfo);
75
76 ASSERT_TRUE(result.IsECMAObject());
77 ASSERT_EQ(JSPrimitiveRef::Cast(result.GetTaggedObject())->GetValue().IsTrue(), 1);
78 }
79
80 // new Boolean(undefined)
HWTEST_F_L0(BuiltinsBooleanTest,BooleanConstructor1)81 HWTEST_F_L0(BuiltinsBooleanTest, BooleanConstructor1)
82 {
83 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
84
85 JSHandle<JSFunction> boolean(env->GetBooleanFunction());
86 JSHandle<JSObject> globalObject(thread, env->GetGlobalObject());
87
88 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*boolean), 6);
89 ecmaRuntimeCallInfo->SetFunction(boolean.GetTaggedValue());
90 ecmaRuntimeCallInfo->SetThis(globalObject.GetTaggedValue());
91 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue::Undefined());
92
93 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
94 JSTaggedValue result = BuiltinsBoolean::BooleanConstructor(ecmaRuntimeCallInfo);
95
96 ASSERT_TRUE(result.IsECMAObject());
97 ASSERT_EQ(JSPrimitiveRef::Cast(result.GetTaggedObject())->GetValue().IsFalse(), 1);
98 }
99
100 // Boolean("helloworld")
HWTEST_F_L0(BuiltinsBooleanTest,BooleanConstructor2)101 HWTEST_F_L0(BuiltinsBooleanTest, BooleanConstructor2)
102 {
103 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
104
105 JSHandle<JSFunction> boolean(env->GetBooleanFunction());
106 JSHandle<JSObject> globalObject(thread, env->GetGlobalObject());
107 JSHandle<EcmaString> str = thread->GetEcmaVM()->GetFactory()->NewFromASCII("helloworld");
108
109 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
110 ecmaRuntimeCallInfo->SetFunction(boolean.GetTaggedValue());
111 ecmaRuntimeCallInfo->SetThis(globalObject.GetTaggedValue());
112 ecmaRuntimeCallInfo->SetCallArg(0, str.GetTaggedValue());
113
114 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
115 JSTaggedValue result = BuiltinsBoolean::BooleanConstructor(ecmaRuntimeCallInfo);
116
117 JSTaggedValue ruler = BuiltinsBase::GetTaggedBoolean(true);
118 ASSERT_EQ(result.GetRawData(), ruler.GetRawData());
119 }
120
121 // false.toString()
HWTEST_F_L0(BuiltinsBooleanTest,BooleanPrototypeToString)122 HWTEST_F_L0(BuiltinsBooleanTest, BooleanPrototypeToString)
123 {
124 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
125 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
126 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::False());
127
128 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
129 JSTaggedValue result = BuiltinsBoolean::BooleanPrototypeToString(ecmaRuntimeCallInfo);
130 ASSERT_TRUE(result.IsString());
131 JSHandle<EcmaString> res(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
132 auto ruler = thread->GetEcmaVM()->GetFactory()->NewFromASCII("false");
133 ASSERT_EQ(EcmaStringAccessor::Compare(*res, *ruler), 0);
134 }
135
136 // (new Boolean(true)).toString()
HWTEST_F_L0(BuiltinsBooleanTest,BooleanPrototypeToString1)137 HWTEST_F_L0(BuiltinsBooleanTest, BooleanPrototypeToString1)
138 {
139 auto ecmaVM = thread->GetEcmaVM();
140 JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
141
142 JSHandle<JSFunction> booleanObject(env->GetBooleanFunction());
143 JSHandle<JSTaggedValue> value(thread, JSTaggedValue::True());
144 JSHandle<JSPrimitiveRef> boolean = thread->GetEcmaVM()->GetFactory()->NewJSPrimitiveRef(booleanObject, value);
145
146 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
147 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
148 ecmaRuntimeCallInfo->SetThis(boolean.GetTaggedValue());
149
150 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
151 JSTaggedValue result = BuiltinsBoolean::BooleanPrototypeToString(ecmaRuntimeCallInfo);
152 ASSERT_TRUE(result.IsString());
153 JSHandle<EcmaString> res(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
154 auto ruler = thread->GetEcmaVM()->GetFactory()->NewFromASCII("true");
155 ASSERT_EQ(EcmaStringAccessor::Compare(*res, *ruler), 0);
156 }
157
158 // true.valueOf()
HWTEST_F_L0(BuiltinsBooleanTest,BooleanPrototypeValueOf)159 HWTEST_F_L0(BuiltinsBooleanTest, BooleanPrototypeValueOf)
160 {
161 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
162 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
163 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::True());
164
165 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
166 JSTaggedValue result = BuiltinsBoolean::BooleanPrototypeValueOf(ecmaRuntimeCallInfo);
167
168 JSTaggedValue ruler = BuiltinsBase::GetTaggedBoolean(true);
169 ASSERT_EQ(result.GetRawData(), ruler.GetRawData());
170 }
171
172 // (new Boolean(false)).valueOf()
HWTEST_F_L0(BuiltinsBooleanTest,BooleanPrototypeValueOf1)173 HWTEST_F_L0(BuiltinsBooleanTest, BooleanPrototypeValueOf1)
174 {
175 auto ecmaVM = thread->GetEcmaVM();
176 JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
177
178 JSHandle<JSFunction> booleanObject(env->GetBooleanFunction());
179 JSHandle<JSTaggedValue> value(thread, JSTaggedValue::False());
180 JSHandle<JSPrimitiveRef> boolean = thread->GetEcmaVM()->GetFactory()->NewJSPrimitiveRef(booleanObject, value);
181
182 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
183 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
184 ecmaRuntimeCallInfo->SetThis(boolean.GetTaggedValue());
185
186 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
187 JSTaggedValue result = BuiltinsBoolean::BooleanPrototypeValueOf(ecmaRuntimeCallInfo);
188
189 JSTaggedValue ruler = BuiltinsBase::GetTaggedBoolean(false);
190 ASSERT_EQ(result.GetRawData(), ruler.GetRawData());
191 }
192 } // namespace panda::test
193