1 /*
2 * Copyright (c) 2022 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/js_api/js_api_lightweightset.h"
17 #include "ecmascript/containers/containers_private.h"
18 #include "ecmascript/ecma_string.h"
19 #include "ecmascript/ecma_vm.h"
20 #include "ecmascript/global_env.h"
21 #include "ecmascript/js_api/js_api_lightweightset_iterator.h"
22 #include "ecmascript/js_function.h"
23 #include "ecmascript/js_handle.h"
24 #include "ecmascript/js_iterator.h"
25 #include "ecmascript/js_object-inl.h"
26 #include "ecmascript/js_tagged_value.h"
27 #include "ecmascript/object_factory.h"
28 #include "ecmascript/tests/test_helper.h"
29
30 using namespace panda;
31 using namespace panda::ecmascript;
32
33 namespace panda::test {
34 class JSAPILightWeightSetIteratorTest : public testing::Test {
35 public:
SetUpTestCase()36 static void SetUpTestCase()
37 {
38 GTEST_LOG_(INFO) << "SetUpTestCase";
39 }
40
TearDownTestCase()41 static void TearDownTestCase()
42 {
43 GTEST_LOG_(INFO) << "TearDownCase";
44 }
45
SetUp()46 void SetUp() override
47 {
48 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
49 }
50
TearDown()51 void TearDown() override
52 {
53 TestHelper::DestroyEcmaVMWithScope(instance, scope);
54 }
55
56 EcmaVM *instance {nullptr};
57 ecmascript::EcmaHandleScope *scope {nullptr};
58 JSThread *thread {nullptr};
59
60 protected:
CreateLightWeightSet()61 JSAPILightWeightSet *CreateLightWeightSet()
62 {
63 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
64 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
65
66 JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject();
67 JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate"));
68 JSHandle<JSTaggedValue> value =
69 JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue();
70
71 auto objCallInfo =
72 TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); // 3 means the value
73 objCallInfo->SetFunction(JSTaggedValue::Undefined());
74 objCallInfo->SetThis(value.GetTaggedValue());
75 objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(containers::ContainerTag::LightWeightSet)));
76
77 auto prev = TestHelper::SetupFrame(thread, objCallInfo);
78 JSHandle<JSTaggedValue> constructor =
79 JSHandle<JSTaggedValue>(thread, containers::ContainersPrivate::Load(objCallInfo));
80 TestHelper::TearDownFrame(thread, prev);
81 JSHandle<JSAPILightWeightSet> lightweightSet =
82 JSHandle<JSAPILightWeightSet>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor),
83 constructor));
84 JSHandle<JSTaggedValue> hashArray = JSHandle<JSTaggedValue>(factory->NewTaggedArray(8)); // 8 means the value
85 JSHandle<JSTaggedValue> valueArray = JSHandle<JSTaggedValue>(factory->NewTaggedArray(8)); // 8 means the value
86 lightweightSet->SetHashes(thread, hashArray);
87 lightweightSet->SetValues(thread, valueArray);
88 lightweightSet->SetLength(0); // 0 means the value
89 return *lightweightSet;
90 }
91 };
92
93 /**
94 * @tc.name: Next
95 * @tc.desc: test special return of Next,
96 * including throw exception and return undefined
97 * @tc.type: FUNC
98 * @tc.require:
99 */
HWTEST_F_L0(JSAPILightWeightSetIteratorTest,SpecailReturnOfNextCreateLightWeightSetIterator)100 HWTEST_F_L0(JSAPILightWeightSetIteratorTest, SpecailReturnOfNextCreateLightWeightSetIterator)
101 {
102 JSHandle<JSAPILightWeightSet> jsLightWeightSet(thread, CreateLightWeightSet());
103 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
104 JSHandle<JSAPILightWeightSetIterator> lightWeightSetIterator = factory->NewJSAPILightWeightSetIterator(
105 jsLightWeightSet, IterationKind::KEY);
106 lightWeightSetIterator->SetIteratedLightWeightSet(thread, JSTaggedValue::Undefined());
107
108 // test Next exception
109 {
110 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
111 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
112 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
113
114 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
115 JSTaggedValue result = JSAPILightWeightSetIterator::Next(ecmaRuntimeCallInfo);
116 TestHelper::TearDownFrame(thread, prev);
117 EXPECT_EQ(result, JSTaggedValue::Exception());
118 EXPECT_EXCEPTION();
119 }
120
121 // test Next return undefined
122 {
123 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
124 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
125 ecmaRuntimeCallInfo->SetThis(lightWeightSetIterator.GetTaggedValue());
126
127 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
128 JSTaggedValue result = JSAPILightWeightSetIterator::Next(ecmaRuntimeCallInfo);
129 TestHelper::TearDownFrame(thread, prev);
130 EXPECT_EQ(result, thread->GlobalConstants()->GetUndefinedIterResult());
131 }
132 }
133 } // namespace panda::test
134