• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ecma_test_common.h"
29 
30 using namespace panda;
31 using namespace panda::ecmascript;
32 
33 namespace panda::test {
34 class JSAPILightWeightSetIteratorTest : public BaseTestWithScope<false> {
35 protected:
CreateLightWeightSet()36     JSAPILightWeightSet *CreateLightWeightSet()
37     {
38         return EcmaContainerCommon::CreateLightWeightSet(thread);
39     }
40 };
41 
42 /**
43  * @tc.name: Next
44  * @tc.desc: test special return of Next,
45  *           including throw exception and return undefined
46  * @tc.type: FUNC
47  * @tc.require:
48  */
HWTEST_F_L0(JSAPILightWeightSetIteratorTest,SpecailReturnOfNextCreateLightWeightSetIterator)49 HWTEST_F_L0(JSAPILightWeightSetIteratorTest, SpecailReturnOfNextCreateLightWeightSetIterator)
50 {
51     JSHandle<JSAPILightWeightSet> jsLightWeightSet(thread, CreateLightWeightSet());
52     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
53     JSHandle<JSAPILightWeightSetIterator> lightWeightSetIterator = factory->NewJSAPILightWeightSetIterator(
54         jsLightWeightSet, IterationKind::KEY);
55     lightWeightSetIterator->SetIteratedLightWeightSet(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 = JSAPILightWeightSetIterator::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(lightWeightSetIterator.GetTaggedValue());
75 
76         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
77         JSTaggedValue result = JSAPILightWeightSetIterator::Next(ecmaRuntimeCallInfo);
78         TestHelper::TearDownFrame(thread, prev);
79         EXPECT_EQ(result, thread->GlobalConstants()->GetUndefinedIterResult());
80     }
81 }
82 }  // namespace panda::test
83