• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/*
17 * @tc.name:spreadoperator
18 * @tc.desc:test spread (...)
19 * @tc.type: FUNC
20 * @tc.require: issueI5NO8G
21 */
22// test spread Array
23var arr1 = [...Array(16).keys()];
24print(arr1.length);
25print(arr1);
26
27var arr2 = [1, 2, 4, 6, 7, 8, 9, 54];
28var arr3 = [...arr2];
29print(arr3.length);
30print(arr3);
31
32// test spread Set
33const set1 = new Set();
34set1.add(42);
35set1.add(42);
36set1.add(13);
37set1.add(23);
38
39var arr4 = [...set1.keys()];
40print(arr4.length);
41print(arr4);
42
43var arr5 = [...set1];
44print(arr5.length);
45print(arr5);
46
47// test spread map
48const map1 = new Map();
49map1.set('a', 1);
50map1.set('b', 2);
51map1.set('c', 3);
52
53var arr6 = [...map1.keys()];
54print(arr6.length);
55print(arr6);
56
57var arr7 = [...map1.values()];
58print(arr7.length);
59print(arr7);
60
61// test change Symbol.iterator
62let iterFunc = function *() {
63    yield 1;
64    yield 2;
65    yield 3;
66}
67print(...map1);
68Map.prototype[Symbol.iterator] = iterFunc;
69print(...map1);
70
71let set  = new Set()
72set.add('a');
73set.add('b');
74set.add('c');
75print(...set);
76Set.prototype[Symbol.iterator] = iterFunc;
77print(...set);
78
79let uint8 = new Uint8Array(2);
80print(...uint8);
81Uint8Array.prototype[Symbol.iterator] = iterFunc;
82print(...uint8);
83
84let arr8 = ['foo'];
85let warn1 = print.bind(print);
86function show1(message, ...args) {
87    return warn1(message, ...args);
88}
89show1(...arr8);
90
91let arr9 = ['foo'];
92let warn2 = print.bind(print);
93function show2(message, ...args) {
94    warn2(message, ...args);
95}
96const handler = {
97    apply: function (target, thisArg, argumentsList) {
98        return target(...argumentsList);;
99    }
100};
101let proxy = new Proxy(show2, handler);
102proxy(...arr9);
103
104let fruits1 = ['Apple']
105let fruits2 = ['Apple', 'Banana']
106print(...fruits2)
107Array.prototype[Symbol.iterator] = function* () {
108    yield 1;
109    yield 2;
110    yield 3;
111}
112print(...fruits1)
113print(...fruits2)
114
115// test spread array when encounter situations like [...arr, elem1, elem2] with arr be StableJSArray
116function appendChild(newNode) {
117    this.childNodes = [...this.childNodes, newNode];
118}
119const app = { tageName: 'VIEW', childNodes: [], appendChild };
120for (let i = 0; i < 5; ++i) {
121    const el = { tageName: 'VIEW', childNodes: [], appendChild };
122    const text = { tageName: 'VIEW', childNodes: [], appendChild };
123    const content = { tageName: '#text', content: i };
124    text.appendChild(content);
125    el.appendChild(text);
126    app.appendChild(el);
127}
128for (let i = 0; i < 5; ++i) {
129    print(app.childNodes[i].childNodes[0].childNodes[0].content);
130}
131let result = []
132try {
133    class C29 {};
134    const v66 = undefined;
135    new C29(...v66);
136} catch (err) {
137    result.push(err.name == "TypeError");
138}
139print(result)
140
141let arr=[];
142print([,...arr].length)
143print([,...''].length)