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()]; 24assert_equal(arr1.length,16); 25assert_equal(arr1,[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]); 26 27var arr2 = [1, 2, 4, 6, 7, 8, 9, 54]; 28var arr3 = [...arr2]; 29assert_equal(arr3.length,8); 30assert_equal(arr3,[1, 2, 4, 6, 7, 8, 9, 54]); 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()]; 40assert_equal(arr4.length,3); 41assert_equal(arr4,[42, 13, 23]); 42 43var arr5 = [...set1]; 44assert_equal(arr5.length,3); 45assert_equal(arr5,[42, 13, 23]); 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()]; 54assert_equal(arr6.length,3); 55assert_equal(arr6,["a", "b", "c"]); 56 57var arr7 = [...map1.values()]; 58assert_equal(arr7.length,3); 59assert_equal(arr7,[1, 2, 3]); 60 61// test change Symbol.iterator 62let iterFunc = function *() { 63 yield 1; 64 yield 2; 65 yield 3; 66} 67let expandedMap = [...map1]; 68assert_equal(JSON.stringify(expandedMap),JSON.stringify([["a", 1], ["b", 2], ["c", 3]])); 69Map.prototype[Symbol.iterator] = iterFunc; 70let objkey1 = [...map1]; 71assert_equal(objkey1,[1, 2, 3]); 72 73let set = new Set() 74set.add('a'); 75set.add('b'); 76set.add('c'); 77let objkey2 = [...set]; 78assert_equal(objkey2,["a", "b", "c"]); 79Set.prototype[Symbol.iterator] = iterFunc; 80let objkey3 = [...set]; 81assert_equal(objkey3,[1, 2, 3]); 82 83let uint8 = new Uint8Array(2); 84let objkey4 = [...uint8]; 85assert_equal(objkey4,[0, 0]); 86Uint8Array.prototype[Symbol.iterator] = iterFunc; 87let objkey5 = [...uint8]; 88assert_equal(objkey5,[1, 2, 3]); 89 90let arr8 = ['foo']; 91let warn1 = print.bind(print); 92function show1(message, ...args) { 93 return warn1(message, ...args); 94} 95assert_equal(...arr8,'foo'); 96 97let arr9 = ['foo']; 98let warn2 = print.bind(print); 99function show2(message, ...args) { 100 warn2(message, ...args); 101} 102const handler = { 103 apply: function (target, thisArg, argumentsList) { 104 return target(...argumentsList); 105 } 106}; 107let proxy = new Proxy(show2, handler); 108assert_equal(...arr9,"foo"); 109 110let fruits1 = ['Apple'] 111let fruits2 = ['Apple', 'Banana'] 112let objkey6 = [...fruits2]; 113assert_equal(objkey6,["Apple", "Banana"]); 114Array.prototype[Symbol.iterator] = function* () { 115 yield 1; 116 yield 2; 117 yield 3; 118} 119let objkey7 = [...fruits1]; 120assert_equal(objkey7,['Apple']); 121let objkey8 = [...fruits2]; 122assert_equal(objkey8,['Apple', 'Banana']); 123 124// test spread array when encounter situations like [...arr, elem1, elem2] with arr be StableJSArray 125function appendChild(newNode) { 126 this.childNodes = [...this.childNodes, newNode]; 127} 128const app = { tageName: 'VIEW', childNodes: [], appendChild }; 129for (let i = 0; i < 5; ++i) { 130 const el = { tageName: 'VIEW', childNodes: [], appendChild }; 131 const text = { tageName: 'VIEW', childNodes: [], appendChild }; 132 const content = { tageName: '#text', content: i }; 133 text.appendChild(content); 134 el.appendChild(text); 135 app.appendChild(el); 136} 137let objkey9 = []; 138for (let i = 0; i < 5; ++i) { 139 objkey9.push(app.childNodes[i].childNodes[0].childNodes[0].content); 140} 141assert_equal(objkey9,[0, 1, 2, 3, 4]); 142 143let result = [] 144try { 145 class C29 {}; 146 const v66 = undefined; 147 new C29(...v66); 148} catch (err) { 149 result.push(err.name == "TypeError"); 150} 151assert_equal(result,[true]); 152 153let arr=[]; 154assert_equal([,...arr].length,1); 155assert_equal([,...''].length,1); 156 157test_end();