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/containers/containers_private.h"
17 #include "ecmascript/ecma_string.h"
18 #include "ecmascript/ecma_vm.h"
19 #include "ecmascript/global_env.h"
20 #include "ecmascript/js_function.h"
21 #include "ecmascript/js_handle.h"
22 #include "ecmascript/js_iterator.h"
23 #include "ecmascript/js_api/js_api_hashset.h"
24 #include "ecmascript/js_api/js_api_hashset_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 JSAPIHashSetIteratorTest : 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:
CreateHashSet()61 JSAPIHashSet *CreateHashSet()
62 {
63 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
64 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
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 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
72 objCallInfo->SetFunction(JSTaggedValue::Undefined());
73 objCallInfo->SetThis(value.GetTaggedValue());
74 objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(containers::ContainerTag::HashSet)));
75
76 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, objCallInfo);
77 JSTaggedValue result = containers::ContainersPrivate::Load(objCallInfo);
78 TestHelper::TearDownFrame(thread, prev);
79
80 JSHandle<JSTaggedValue> constructor(thread, result);
81 JSHandle<JSAPIHashSet> set(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
82 JSTaggedValue hashSetArray = TaggedHashArray::Create(thread);
83 set->SetTable(thread, hashSetArray);
84 set->SetSize(0);
85 return *set;
86 }
87 };
88
89 /**
90 * @tc.name: Next and CreateHashSetIterator
91 * @tc.desc: test special return of Next and CreateHashSetIterator,
92 * including throw exception and return undefined
93 * @tc.type: FUNC
94 * @tc.require:
95 */
HWTEST_F_L0(JSAPIHashSetIteratorTest,SpecailReturnOfNextCreateHashSetIterator)96 HWTEST_F_L0(JSAPIHashSetIteratorTest, SpecailReturnOfNextCreateHashSetIterator)
97 {
98 JSHandle<JSAPIHashSet> jsHashSet(thread, CreateHashSet());
99 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
100 JSHandle<JSAPIHashSetIterator> hashSetIterator = factory->NewJSAPIHashSetIterator(
101 jsHashSet, IterationKind::KEY);
102 hashSetIterator->SetIteratedHashSet(thread, JSTaggedValue::Undefined());
103
104 // test Next exception
105 {
106 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
107 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
108 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
109
110 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
111 JSTaggedValue result = JSAPIHashSetIterator::Next(ecmaRuntimeCallInfo);
112 TestHelper::TearDownFrame(thread, prev);
113 EXPECT_EQ(result, JSTaggedValue::Exception());
114 EXPECT_EXCEPTION();
115 }
116
117 // test Next return undefined
118 {
119 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
120 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
121 ecmaRuntimeCallInfo->SetThis(hashSetIterator.GetTaggedValue());
122
123 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
124 JSTaggedValue result = JSAPIHashSetIterator::Next(ecmaRuntimeCallInfo);
125 TestHelper::TearDownFrame(thread, prev);
126 EXPECT_EQ(result, thread->GlobalConstants()->GetUndefinedIterResult());
127 }
128
129 // test CreateHashSetIterator exception
130 {
131 JSHandle<JSTaggedValue> undefined(thread, JSTaggedValue::Undefined());
132 JSHandle<JSTaggedValue> result =
133 JSAPIHashSetIterator::CreateHashSetIterator(thread, undefined, IterationKind::KEY);
134 EXPECT_EQ(result.GetTaggedValue(), JSTaggedValue::Exception());
135 EXPECT_EXCEPTION();
136 }
137 }
138 } // namespace panda::test
139