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/ic/property_box.h"
17 #include "ecmascript/tests/test_helper.h"
18
19 using namespace panda::ecmascript;
20
21 namespace panda::test {
22 class PropertyBoxTest : public testing::Test {
23 public:
SetUpTestCase()24 static void SetUpTestCase()
25 {
26 GTEST_LOG_(INFO) << "SetUpTestCase";
27 }
28
TearDownTestCase()29 static void TearDownTestCase()
30 {
31 GTEST_LOG_(INFO) << "TearDownCase";
32 }
33
SetUp()34 void SetUp() override
35 {
36 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
37 }
38
TearDown()39 void TearDown() override
40 {
41 TestHelper::DestroyEcmaVMWithScope(instance, scope);
42 }
43
44 EcmaVM *instance {nullptr};
45 EcmaHandleScope *scope {nullptr};
46 JSThread *thread {nullptr};
47 };
48
49 /**
50 * @tc.name: Clear
51 * @tc.desc: Creating PropertyBox object through "NewPropertyBox" function,the Property value is exist,then calling
52 * "Clear" function make the value be hole.
53 * @tc.type: FUNC
54 * @tc.requre:
55 */
HWTEST_F_L0(PropertyBoxTest,Clear)56 HWTEST_F_L0(PropertyBoxTest, Clear)
57 {
58 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
59
60 JSHandle<JSTaggedValue> handleValue(thread, JSTaggedValue(1));
61 JSHandle<PropertyBox> handlePropertyBox = factory->NewPropertyBox(handleValue);
62 EXPECT_FALSE(handlePropertyBox->GetValue().IsHole());
63
64 handlePropertyBox->Clear(thread);
65 EXPECT_TRUE(handlePropertyBox->GetValue().IsHole());
66 EXPECT_TRUE(handlePropertyBox->IsInvalid());
67 }
68
69 /**
70 * @tc.name: SetValue
71 * @tc.desc: Creating PropertyBox object through "NewPropertyBox" function,this object call "SetValue" function
72 * check wether the result returned through "GetValue" function is within expectations.
73 * @tc.type: FUNC
74 * @tc.requre:
75 */
HWTEST_F_L0(PropertyBoxTest,SetValue)76 HWTEST_F_L0(PropertyBoxTest, SetValue)
77 {
78 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
79 JSHandle<JSTaggedValue> handleValue(thread, JSTaggedValue::Undefined());
80 JSHandle<JSTaggedValue> handlePropertyBoxVal(factory->NewPropertyBox(handleValue));
81
82 JSHandle<PropertyBox> handlePropertyBox = JSHandle<PropertyBox>::Cast(handlePropertyBoxVal);
83 handlePropertyBox->SetValue(thread, JSTaggedValue(2));
84 EXPECT_EQ(handlePropertyBox->GetValue().GetInt(), 2);
85 EXPECT_FALSE(handlePropertyBox->IsInvalid());
86 }
87 } // namespace panda::test