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 BaseTestWithScope<false> {
26 };
27
HWTEST_F_L0(JSForinIteratorTest,Create)28 HWTEST_F_L0(JSForinIteratorTest, Create)
29 {
30 JSHandle<JSObject> nullHandle(thread, JSTaggedValue::Null());
31 JSHandle<JSObject> grandfather = JSObject::ObjectCreate(thread, nullHandle);
32 EXPECT_TRUE(JSTaggedValue::GetPrototype(thread, JSHandle<JSTaggedValue>(grandfather)).IsNull());
33
34 JSHandle<JSObject> father = JSObject::ObjectCreate(thread, grandfather);
35
36 JSHandle<JSObject> son = JSObject::ObjectCreate(thread, father);
37
38 JSHandle<JSTaggedValue> key1(thread->GetEcmaVM()->GetFactory()->NewFromASCII("key1"));
39 JSHandle<JSTaggedValue> key2(thread->GetEcmaVM()->GetFactory()->NewFromASCII("key2"));
40 JSHandle<JSTaggedValue> key3(thread->GetEcmaVM()->GetFactory()->NewFromASCII("key3"));
41 JSHandle<JSTaggedValue> key1Value(thread, JSTaggedValue(1));
42 JSHandle<JSTaggedValue> key2Value(thread, JSTaggedValue(2));
43 JSHandle<JSTaggedValue> key3Value(thread, JSTaggedValue(3));
44
45 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(grandfather), key3, key3Value);
46 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(father), key2, key2Value);
47
48 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(son), key1, key1Value);
49 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(son), key2, key1Value);
50 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(son), key3, key1Value);
51
52 JSHandle<JSForInIterator> it = JSObject::EnumerateObjectProperties(thread, JSHandle<JSTaggedValue>(son));
53 JSTaggedValue n1 = JSForInIterator::NextInternal(thread, it);
54 EXPECT_EQ(n1, key1.GetTaggedValue());
55
56 JSTaggedValue n2 = JSForInIterator::NextInternal(thread, it);
57 EXPECT_EQ(n2, key2.GetTaggedValue());
58
59 JSTaggedValue n3 = JSForInIterator::NextInternal(thread, it);
60 EXPECT_EQ(n3, key3.GetTaggedValue());
61
62 JSTaggedValue n4 = JSForInIterator::NextInternal(thread, it);
63 EXPECT_EQ(n4, JSTaggedValue::Undefined());
64 }
65
HWTEST_F_L0(JSForinIteratorTest,ForinNativePointer)66 HWTEST_F_L0(JSForinIteratorTest, ForinNativePointer)
67 {
68 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
69 int pointArr[10];
70 auto *nativePointer = pointArr;
71 JSHandle<JSNativePointer> pointer = factory->NewJSNativePointer(nativePointer);
72 JSHandle<JSForInIterator> it = JSObject::EnumerateObjectProperties(thread, JSHandle<JSTaggedValue>(pointer));
73 EXPECT_EQ(it->GetLength(), 0);
74 }
75 } // namespace panda::test
76