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/ecma_string.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_lightweightmap.h"
22 #include "ecmascript/js_api/js_api_lightweightmap_iterator.h"
23 #include "ecmascript/js_function.h"
24 #include "ecmascript/js_handle.h"
25 #include "ecmascript/js_iterator.h"
26 #include "ecmascript/js_object-inl.h"
27 #include "ecmascript/js_tagged_value.h"
28 #include "ecmascript/object_factory.h"
29 #include "ecmascript/tests/ecma_test_common.h"
30
31 using namespace panda;
32
33 using namespace panda::ecmascript;
34
35 using namespace panda::ecmascript::containers;
36
37 namespace panda::test {
38 class JSAPILightWeightMapIteratorTest : public BaseTestWithScope<false> {
39 public:
40 const static int DEFAULT_SIZE = 8;
41
42 protected:
CreateLightWeightMap()43 JSAPILightWeightMap *CreateLightWeightMap()
44 {
45 return EcmaContainerCommon::CreateLightWeightMap(thread);
46 }
47 };
48
49 /**
50 * @tc.name: Next and CreateLightWeightMapIterator
51 * @tc.desc: test special return of Next and CreateLightWeightMapIterator,
52 * including throw exception and return undefined
53 * @tc.type: FUNC
54 * @tc.require:
55 */
HWTEST_F_L0(JSAPILightWeightMapIteratorTest,SpecailReturnOfNextCreateLightWeightMapIterator)56 HWTEST_F_L0(JSAPILightWeightMapIteratorTest, SpecailReturnOfNextCreateLightWeightMapIterator)
57 {
58 JSHandle<JSAPILightWeightMap> jsLightWeightMap(thread, CreateLightWeightMap());
59 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
60 JSHandle<JSAPILightWeightMapIterator> lightWeightMapIterator = factory->NewJSAPILightWeightMapIterator(
61 jsLightWeightMap, IterationKind::KEY_AND_VALUE);
62 lightWeightMapIterator->SetIteratedLightWeightMap(thread, JSTaggedValue::Undefined());
63
64 // test Next exception
65 {
66 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
67 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
68 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
69
70 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
71 JSTaggedValue result = JSAPILightWeightMapIterator::Next(ecmaRuntimeCallInfo);
72 TestHelper::TearDownFrame(thread, prev);
73 EXPECT_EQ(result, JSTaggedValue::Exception());
74 EXPECT_EXCEPTION();
75 }
76
77 // test Next return undefined
78 {
79 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
80 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
81 ecmaRuntimeCallInfo->SetThis(lightWeightMapIterator.GetTaggedValue());
82
83 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
84 JSTaggedValue result = JSAPILightWeightMapIterator::Next(ecmaRuntimeCallInfo);
85 TestHelper::TearDownFrame(thread, prev);
86 EXPECT_EQ(result, thread->GetEcmaVM()->GetGlobalEnv()->GetUndefinedIteratorResult().GetTaggedValue());
87 }
88
89 // test CreateLightWeightMapIterator exception
90 {
91 JSHandle<JSTaggedValue> undefined(thread, JSTaggedValue::Undefined());
92 JSHandle<JSTaggedValue> result =
93 JSAPILightWeightMapIterator::CreateLightWeightMapIterator(thread, undefined, IterationKind::KEY_AND_VALUE);
94 EXPECT_EQ(result.GetTaggedValue(), JSTaggedValue::Exception());
95 EXPECT_EXCEPTION();
96 }
97 }
98 } // namespace panda::test
99