• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 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/*
17 * @tc.name: isin
18 * @tc.desc: test isin. Test whether the return value of IsIn is exception while input para is not a ECMA obj.
19 * @tc.type: FUNC
20 */
21{
22    try {
23        1 in 0;
24        let tmpArr = [0];
25        assert_unreachable();
26    } catch (e) {
27        assert_equal(e instanceof TypeError, true);
28    }
29}
30
31{
32    const v5 = [-3];
33    const a8 = new Uint8Array(2);
34    v5.__proto__ = a8;
35    assert_equal(-3 in v5, false);
36}
37
38{
39    const obj = { a: 1 };
40    assert_equal('a' in obj, true);
41    assert_equal('b' in obj, false);
42}
43
44{
45    const arr = ['x'];
46    assert_equal(0 in arr, true);
47    assert_equal(1 in arr, false);
48    arr[100] = 'y';
49    assert_equal(100 in arr, true);
50    assert_equal('length' in arr, true);
51}
52
53{
54    const base = { foo: 1 };
55    const derived = Object.create(base);
56    assert_equal('foo' in derived, true);
57    derived.foo = 2;
58    assert_equal('foo' in derived, true);
59}
60
61{
62    try {
63        'x' in undefined;
64        assert_unreachable();
65    } catch (e) {
66        assert_equal(e instanceof TypeError, true);
67    }
68
69    try {
70        'y' in null;
71    } catch (e) {
72        assert_equal(e instanceof TypeError, true);
73    }
74}
75
76{
77    const sym = Symbol('s');
78    const obj = { [sym]: 42 };
79    assert_equal(sym in obj, true);
80    assert_equal('s' in obj, false);
81}
82
83{
84    const target = {};
85    const proxy = new Proxy(target, {
86        has(target, prop) {
87            return prop === 'custom';
88        }
89    });
90    assert_equal('custom' in proxy, true);
91    assert_equal('another' in proxy, false);
92}
93
94{
95    try {
96        'x' in 42;
97        assert_unreachable();
98    } catch (e) {
99        assert_equal(e instanceof TypeError, true);
100    }
101
102    try {
103        'length' in 'abc';
104        assert_unreachable();
105    } catch (e) {
106        assert_equal(e instanceof TypeError, true);
107    }
108}
109
110test_end();