• 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/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/test_helper.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 testing::Test {
39 public:
40     const static int DEFAULT_SIZE = 8;
SetUpTestCase()41     static void SetUpTestCase()
42     {
43         GTEST_LOG_(INFO) << "SetUpTestCase";
44     }
45 
TearDownTestCase()46     static void TearDownTestCase()
47     {
48         GTEST_LOG_(INFO) << "TearDownCase";
49     }
50 
SetUp()51     void SetUp() override
52     {
53         TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
54     }
55 
TearDown()56     void TearDown() override
57     {
58         TestHelper::DestroyEcmaVMWithScope(instance, scope);
59     }
60 
61     EcmaVM *instance {nullptr};
62     ecmascript::EcmaHandleScope *scope {nullptr};
63     JSThread *thread {nullptr};
64 
65 protected:
CreateLightWeightMap()66     JSAPILightWeightMap *CreateLightWeightMap()
67     {
68         JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
69         ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
70 
71         JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject();
72         JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate"));
73         JSHandle<JSTaggedValue> value = JSObject::GetProperty(thread,
74             JSHandle<JSTaggedValue>(globalObject), key).GetValue();
75 
76         auto objCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
77         objCallInfo->SetFunction(JSTaggedValue::Undefined());
78         objCallInfo->SetThis(value.GetTaggedValue());
79         objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(containers::ContainerTag::LightWeightMap)));
80 
81         auto prev = TestHelper::SetupFrame(thread, objCallInfo);
82         JSHandle<JSTaggedValue> constructor =
83             JSHandle<JSTaggedValue>(thread, containers::ContainersPrivate::Load(objCallInfo));
84         TestHelper::TearDownFrame(thread, prev);
85         JSHandle<JSAPILightWeightMap> lightWeightMap = JSHandle<JSAPILightWeightMap>::
86             Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
87         JSHandle<JSTaggedValue> hashArray = JSHandle<JSTaggedValue>(factory->NewTaggedArray(DEFAULT_SIZE));
88         JSHandle<JSTaggedValue> keyArray = JSHandle<JSTaggedValue>(factory->NewTaggedArray(DEFAULT_SIZE));
89         JSHandle<JSTaggedValue> valueArray = JSHandle<JSTaggedValue>(factory->NewTaggedArray(DEFAULT_SIZE));
90         lightWeightMap->SetHashes(thread, hashArray);
91         lightWeightMap->SetKeys(thread, keyArray);
92         lightWeightMap->SetValues(thread, valueArray);
93         lightWeightMap->SetLength(0);
94         return *lightWeightMap;
95     }
96 };
97 
98 /**
99  * @tc.name: Next and CreateLightWeightMapIterator
100  * @tc.desc: test special return of Next and CreateLightWeightMapIterator,
101  *           including throw exception and return undefined
102  * @tc.type: FUNC
103  * @tc.require:
104  */
HWTEST_F_L0(JSAPILightWeightMapIteratorTest,SpecailReturnOfNextCreateLightWeightMapIterator)105 HWTEST_F_L0(JSAPILightWeightMapIteratorTest, SpecailReturnOfNextCreateLightWeightMapIterator)
106 {
107     JSHandle<JSAPILightWeightMap> jsLightWeightMap(thread, CreateLightWeightMap());
108     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
109     JSHandle<JSAPILightWeightMapIterator> lightWeightMapIterator = factory->NewJSAPILightWeightMapIterator(
110         jsLightWeightMap, IterationKind::KEY_AND_VALUE);
111     lightWeightMapIterator->SetIteratedLightWeightMap(thread, JSTaggedValue::Undefined());
112 
113     // test Next exception
114     {
115         auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
116         ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
117         ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
118 
119         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
120         JSTaggedValue result = JSAPILightWeightMapIterator::Next(ecmaRuntimeCallInfo);
121         TestHelper::TearDownFrame(thread, prev);
122         EXPECT_EQ(result, JSTaggedValue::Exception());
123         EXPECT_EXCEPTION();
124     }
125 
126     // test Next return undefined
127     {
128         auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
129         ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
130         ecmaRuntimeCallInfo->SetThis(lightWeightMapIterator.GetTaggedValue());
131 
132         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
133         JSTaggedValue result = JSAPILightWeightMapIterator::Next(ecmaRuntimeCallInfo);
134         TestHelper::TearDownFrame(thread, prev);
135         EXPECT_EQ(result, thread->GlobalConstants()->GetUndefinedIterResult());
136     }
137 
138     // test CreateLightWeightMapIterator exception
139     {
140         JSHandle<JSTaggedValue> undefined(thread, JSTaggedValue::Undefined());
141         JSHandle<JSTaggedValue> result =
142             JSAPILightWeightMapIterator::CreateLightWeightMapIterator(thread, undefined, IterationKind::KEY_AND_VALUE);
143         EXPECT_EQ(result.GetTaggedValue(), JSTaggedValue::Exception());
144         EXPECT_EXCEPTION();
145     }
146 }
147 }  // namespace panda::test
148