1 /*
2 * Copyright (c) 2021 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/tests/test_helper.h"
17
18 #include "ecmascript/ecma_vm.h"
19 #include "ecmascript/js_for_in_iterator.h"
20 #include "ecmascript/js_handle.h"
21
22 using namespace panda::ecmascript;
23
24 namespace panda::test {
25 class JSForinIteratorTest : public testing::Test {
26 public:
SetUpTestCase()27 static void SetUpTestCase()
28 {
29 GTEST_LOG_(INFO) << "SetUpTestCase";
30 }
31
TearDownTestCase()32 static void TearDownTestCase()
33 {
34 GTEST_LOG_(INFO) << "TearDownCase";
35 }
36
SetUp()37 void SetUp() override
38 {
39 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
40 }
41
TearDown()42 void TearDown() override
43 {
44 TestHelper::DestroyEcmaVMWithScope(instance, scope);
45 }
46
47 EcmaVM *instance {nullptr};
48 ecmascript::EcmaHandleScope *scope {nullptr};
49 JSThread *thread {nullptr};
50 };
51
HWTEST_F_L0(JSForinIteratorTest,Create)52 HWTEST_F_L0(JSForinIteratorTest, Create)
53 {
54 JSHandle<JSObject> nullHandle(thread, JSTaggedValue::Null());
55 JSHandle<JSObject> grandfather = JSObject::ObjectCreate(thread, nullHandle);
56 EXPECT_TRUE(JSTaggedValue::GetPrototype(thread, JSHandle<JSTaggedValue>(grandfather)).IsNull());
57
58 JSHandle<JSObject> father = JSObject::ObjectCreate(thread, grandfather);
59
60 JSHandle<JSObject> son = JSObject::ObjectCreate(thread, father);
61
62 JSHandle<JSTaggedValue> key1(thread->GetEcmaVM()->GetFactory()->NewFromASCII("key1"));
63 JSHandle<JSTaggedValue> key2(thread->GetEcmaVM()->GetFactory()->NewFromASCII("key2"));
64 JSHandle<JSTaggedValue> key3(thread->GetEcmaVM()->GetFactory()->NewFromASCII("key3"));
65 JSHandle<JSTaggedValue> key1Value(thread, JSTaggedValue(1));
66 JSHandle<JSTaggedValue> key2Value(thread, JSTaggedValue(2));
67 JSHandle<JSTaggedValue> key3Value(thread, JSTaggedValue(3));
68
69 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(grandfather), key3, key3Value);
70 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(father), key2, key2Value);
71
72 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(son), key1, key1Value);
73 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(son), key2, key1Value);
74 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(son), key3, key1Value);
75
76 JSHandle<JSForInIterator> it = thread->GetEcmaVM()->GetFactory()->NewJSForinIterator(JSHandle<JSTaggedValue>(son));
77 std::pair<JSTaggedValue, bool> n1 = JSForInIterator::NextInternal(thread, it);
78 EXPECT_EQ(n1.first, key1.GetTaggedValue());
79 EXPECT_FALSE(n1.second);
80
81 std::pair<JSTaggedValue, bool> n2 = JSForInIterator::NextInternal(thread, it);
82 EXPECT_EQ(n2.first, key2.GetTaggedValue());
83 EXPECT_FALSE(n2.second);
84
85 std::pair<JSTaggedValue, bool> n3 = JSForInIterator::NextInternal(thread, it);
86 EXPECT_EQ(n3.first, key3.GetTaggedValue());
87 EXPECT_FALSE(n3.second);
88
89 std::pair<JSTaggedValue, bool> n4 = JSForInIterator::NextInternal(thread, it);
90 EXPECT_EQ(n4.first, JSTaggedValue::Undefined());
91 EXPECT_TRUE(n4.second);
92 }
93 } // namespace panda::test
94