1// Copyright JS Foundation and other contributors, http://js.foundation 2// 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// Test with regular arrays 16var alpha = ['a', 'b', 'c']; 17var numeric = [1, 2, 3]; 18 19var alphaNumeric = alpha.concat(numeric); 20assert(JSON.stringify(alphaNumeric) === '["a","b","c",1,2,3]'); 21assert(alphaNumeric.length === 6); 22 23numeric[Symbol.isConcatSpreadable] = false; 24alphaNumeric = alpha.concat(numeric); 25assert(JSON.stringify(alphaNumeric) === '["a","b","c",[1,2,3]]'); 26assert(alphaNumeric.length === 4); 27 28numeric[Symbol.isConcatSpreadable] = true; 29 30// Test with array-like object 31var fakeArray = { 32 [Symbol.isConcatSpreadable]: true, 33 length: 2, 34 0: 4, 35 1: 5 36} 37 38var numericArray = numeric.concat(fakeArray); 39assert(JSON.stringify(numericArray) === '[1,2,3,4,5]'); 40assert(numericArray.length === 5); 41 42fakeArray[Symbol.isConcatSpreadable] = false; 43numericArray = numeric.concat(fakeArray); 44assert(JSON.stringify(numericArray) === '[1,2,3,{"0":4,"1":5,"length":2}]'); 45assert(numericArray.length === 4); 46 47// Test with object 48var obj = { 0: 'd' }; 49 50var alphaObj = alpha.concat(obj); 51assert(JSON.stringify(alphaObj) === '["a","b","c",{"0":"d"}]'); 52assert(alphaObj.length === 4); 53 54obj[Symbol.isConcatSpreadable] = true; 55alphaObj = alpha.concat(obj); 56assert(JSON.stringify(alphaObj) === '["a","b","c"]'); 57assert(alphaObj.length === 3); 58 59// Test with boolean 60var bool = true; 61var numericBool = numeric.concat(bool); 62assert(JSON.stringify(numericBool) === '[1,2,3,true]'); 63assert(numericBool.length === 4); 64 65bool[Symbol.isConcatSpreadable] = false; 66numericBool = numeric.concat(bool); 67assert(JSON.stringify(numericBool) === '[1,2,3,true]'); 68assert(numericBool.length === 4); 69 70// Test when unable to concat 71var array1 = []; 72var array2 = []; 73Object.defineProperty(array2, '0', { 'get' : function () {throw new ReferenceError ("foo"); } }); 74 75try { 76 array1.concat(array2); 77 assert(false); 78} catch (e) { 79 assert(e.message === "foo"); 80 assert(e instanceof ReferenceError); 81} 82