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_hashmap.h"
24 #include "ecmascript/js_api/js_api_hashmap_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/ecma_test_common.h"
29
30 using namespace panda;
31 using namespace panda::ecmascript;
32
33 namespace panda::test {
34 class JSAPIHashMapIteratorTest : public BaseTestWithScope<false> {
35 protected:
CreateHashMap()36 JSAPIHashMap *CreateHashMap()
37 {
38 return EcmaContainerCommon::CreateHashMap(thread);
39 }
40 };
41
42 /**
43 * @tc.name: Next and CreateHashMapIterator
44 * @tc.desc: test special return of Next and CreateHashMapIterator,
45 * including throw exception and return undefined
46 * @tc.type: FUNC
47 * @tc.require:
48 */
HWTEST_F_L0(JSAPIHashMapIteratorTest,SpecailReturnOfNextCreateHashMapIterator)49 HWTEST_F_L0(JSAPIHashMapIteratorTest, SpecailReturnOfNextCreateHashMapIterator)
50 {
51 JSHandle<JSAPIHashMap> jsHashMap(thread, CreateHashMap());
52 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
53 JSHandle<JSAPIHashMapIterator> hashMapIterator = factory->NewJSAPIHashMapIterator(
54 jsHashMap, IterationKind::KEY_AND_VALUE);
55 hashMapIterator->SetIteratedHashMap(thread, JSTaggedValue::Undefined());
56
57 // test Next exception
58 {
59 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
60 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
61 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
62
63 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
64 JSTaggedValue result = JSAPIHashMapIterator::Next(ecmaRuntimeCallInfo);
65 TestHelper::TearDownFrame(thread, prev);
66 EXPECT_EQ(result, JSTaggedValue::Exception());
67 EXPECT_EXCEPTION();
68 }
69
70 // test Next return undefined
71 {
72 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
73 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
74 ecmaRuntimeCallInfo->SetThis(hashMapIterator.GetTaggedValue());
75
76 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
77 JSTaggedValue result = JSAPIHashMapIterator::Next(ecmaRuntimeCallInfo);
78 TestHelper::TearDownFrame(thread, prev);
79 EXPECT_EQ(result, thread->GetEcmaVM()->GetGlobalEnv()->GetUndefinedIteratorResult().GetTaggedValue());
80 }
81
82 // test CreateHashMapIterator exception
83 {
84 JSHandle<JSTaggedValue> undefined(thread, JSTaggedValue::Undefined());
85 JSHandle<JSTaggedValue> result =
86 JSAPIHashMapIterator::CreateHashMapIterator(thread, undefined, IterationKind::KEY_AND_VALUE);
87 EXPECT_EQ(result.GetTaggedValue(), JSTaggedValue::Exception());
88 EXPECT_EXCEPTION();
89 }
90 }
91 } // namespace panda::test
92