• 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_plain_array.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_plain_array_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/test_helper.h"
29 
30 using namespace panda;
31 
32 using namespace panda::ecmascript;
33 
34 namespace panda::test {
35 class JSAPIPlainArrayIteratorTest : public testing::Test {
36 public:
SetUpTestCase()37     static void SetUpTestCase()
38     {
39         GTEST_LOG_(INFO) << "SetUpTestCase";
40     }
41 
TearDownTestCase()42     static void TearDownTestCase()
43     {
44         GTEST_LOG_(INFO) << "TearDownCase";
45     }
46 
SetUp()47     void SetUp() override
48     {
49         TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
50     }
51 
TearDown()52     void TearDown() override
53     {
54         TestHelper::DestroyEcmaVMWithScope(instance, scope);
55     }
56 
57     EcmaVM *instance {nullptr};
58     ecmascript::EcmaHandleScope *scope {nullptr};
59     JSThread *thread {nullptr};
60 
61 protected:
CreatePlainArray()62     JSAPIPlainArray *CreatePlainArray()
63     {
64         ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
65         JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
66 
67         JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject();
68         JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate"));
69         JSHandle<JSTaggedValue> value =
70             JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue();
71 
72         auto objCallInfo =
73             TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); // 6 means the value
74         objCallInfo->SetFunction(JSTaggedValue::Undefined());
75         objCallInfo->SetThis(value.GetTaggedValue());
76         objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(containers::ContainerTag::PlainArray)));
77 
78         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, objCallInfo);
79         JSTaggedValue result = containers::ContainersPrivate::Load(objCallInfo);
80         TestHelper::TearDownFrame(thread, prev);
81 
82         JSHandle<JSTaggedValue> constructor(thread, result);
83         JSHandle<JSAPIPlainArray> plainArray(
84             factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
85         JSHandle<JSTaggedValue> keyArray = JSHandle<JSTaggedValue>(factory->NewTaggedArray(8)); // 8 means the value
86         JSHandle<JSTaggedValue> valueArray = JSHandle<JSTaggedValue>(factory->NewTaggedArray(8)); // 8 means the value
87         plainArray->SetKeys(thread, keyArray);
88         plainArray->SetValues(thread, valueArray);
89         return *plainArray;
90     }
91 };
92 
93 /**
94  * @tc.name: Next
95  * @tc.desc: test special return of Next, including throw exception
96  * @tc.type: FUNC
97  * @tc.require:
98  */
HWTEST_F_L0(JSAPIPlainArrayIteratorTest,SpecailReturnOfNextCreatePlainArrayIterator)99 HWTEST_F_L0(JSAPIPlainArrayIteratorTest, SpecailReturnOfNextCreatePlainArrayIterator)
100 {
101     JSHandle<JSAPIPlainArray> jsPlainArray(thread, CreatePlainArray());
102     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
103     JSHandle<JSAPIPlainArrayIterator> plainArrayIterator = factory->NewJSAPIPlainArrayIterator(
104         jsPlainArray, IterationKind::KEY);
105     plainArrayIterator->SetIteratedPlainArray(thread, JSTaggedValue::Undefined());
106 
107     // test Next exception
108     {
109         auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
110         ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
111         ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
112 
113         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
114         JSTaggedValue result = JSAPIPlainArrayIterator::Next(ecmaRuntimeCallInfo);
115         TestHelper::TearDownFrame(thread, prev);
116         EXPECT_EQ(result, JSTaggedValue::Exception());
117         EXPECT_EXCEPTION();
118     }
119 }
120 } // namespace panda::test
121