Skip to content
Snippets Groups Projects
Commit 1182adb9 authored by Rick Waldron's avatar Rick Waldron
Browse files

Import tests from Google V8 (Array isConcatSpreadable)

These tests are derived from the following files within the Google V8 project:

    	test/mjsunit/harmony/array-concat.js
parent 2eca2c71
No related branches found
No related tags found
No related merge requests found
Showing
with 466 additions and 0 deletions
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.1_3
description: Array.prototype.concat arity
---*/
assert.sameValue(Array.prototype.concat.length, 1);
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.1_3
description: Array.prototype.concat array like length to string throws
---*/
function MyError() {}
var obj = {
"length": { toString: function() {
throw new MyError();
}, valueOf: null
},
"1": "A",
"3": "B",
"5": "C"
};
obj[Symbol.isConcatSpreadable] = true;
assert.throws(MyError, function() {
[].concat(obj);
});
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.1_3
description: Array.prototype.concat array like length valueOf throws
---*/
function MyError() {}
var obj = {
"length": {
valueOf: function() {
throw new MyError();
},
toString: null
},
"1": "A",
"3": "B",
"5": "C"
};
obj[Symbol.isConcatSpreadable] = true;
assert.throws(MyError, function() {
[].concat(obj);
});
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.1_3
description: Array.prototype.concat array like negative length
includes: [compareArray.js]
---*/
var obj = {
"length": -6,
"1": "A",
"3": "B",
"5": "C"
};
obj[Symbol.isConcatSpreadable] = true;
assert(compareArray([].concat(obj), []));
obj.length = -6.7;
assert(compareArray([].concat(obj), []));
obj.length = "-6";
assert(compareArray([].concat(obj), []));
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.1_3
description: Array.prototype.concat array like primitive non number length
includes: [compareArray.js]
---*/
var obj = {
"1": "A",
"3": "B",
"5": "C"
};
obj[Symbol.isConcatSpreadable] = true;
obj.length = {toString: function() { return "SIX"; }, valueOf: null };
assert(compareArray([].concat(obj), []));
obj.length = {toString: null, valueOf: function() { return "SIX"; } };
assert(compareArray([].concat(obj), []));
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.1_3
description: Array.prototype.concat array like string length
includes: [compareArray.js]
---*/
var obj = {
"length": "6",
"1": "A",
"3": "B",
"5": "C"
};
obj[Symbol.isConcatSpreadable] = true;
var obj2 = { length: 3, "0": "0", "1": "1", "2": "2" };
var arr = ["X", "Y", "Z"];
var expected = [
void 0, "A", void 0, "B", void 0, "C",
obj2,
"X", "Y", "Z"
];
var actual = Array.prototype.concat.call(obj, obj2, arr);
assert(compareArray(actual, expected));
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.1_3
description: Array.prototype.concat array like to length throws
---*/
var obj = {
"length": {valueOf: null, toString: null},
"1": "A",
"3": "B",
"5": "C"
};
obj[Symbol.isConcatSpreadable] = true;
var obj2 = { length: 3, "0": "0", "1": "1", "2": "2" };
var arr = ["X", "Y", "Z"];
assert.throws(TypeError, function() {
Array.prototype.concat.call(obj, obj2, arr);
});
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.1_3
description: Array.prototype.concat array like
includes: [compareArray.js]
---*/
var obj = {
"length": 6,
"1": "A",
"3": "B",
"5": "C"
};
obj[Symbol.isConcatSpreadable] = true;
var obj2 = { length: 3, "0": "0", "1": "1", "2": "2" };
var arr = ["X", "Y", "Z"];
var expected = [
void 0, "A", void 0, "B", void 0, "C",
obj2,
"X", "Y", "Z"
];
var actual = Array.prototype.concat.call(obj, obj2, arr);
assert(compareArray(actual, expected));
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.1_3
description: Array.prototype.concat descriptor
---*/
var desc = Object.getOwnPropertyDescriptor(Array.prototype, "concat");
assert.sameValue(desc.enumerable, false);
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.1_3
description: Array.prototype.concat holey sloppy arguments
includes: [compareArray.js]
---*/
var args = (function(a) { return arguments; })(1,2,3);
delete args[1];
args[Symbol.isConcatSpreadable] = true;
assert(compareArray([1, void 0, 3, 1, void 0, 3], [].concat(args, args)));
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.1_3
description: Array.prototype.concat large typed array
includes: [compareArray.js]
---*/
function concatTypedArray(type, elems, modulo) {
var items = new Array(elems);
var ta_by_len = new type(elems);
for (var i = 0; i < elems; ++i) {
ta_by_len[i] = items[i] = modulo === false ? i : elems % modulo;
}
var ta = new type(items);
assert(compareArray([].concat(ta, ta), [ta, ta]));
ta[Symbol.isConcatSpreadable] = true;
assert(compareArray([].concat(ta), items));
assert(compareArray([].concat(ta_by_len, ta_by_len), [ta_by_len, ta_by_len]));
ta_by_len[Symbol.isConcatSpreadable] = true;
assert(compareArray([].concat(ta_by_len), items));
// TypedArray with fake `length`.
ta = new type(1);
var defValue = ta[0];
var expected = new Array(4000);
expected[0] = defValue;
Object.defineProperty(ta, "length", { value: 4000 });
ta[Symbol.isConcatSpreadable] = true;
assert(compareArray([].concat(ta), expected));
}
var max = [Math.pow(2, 8), Math.pow(2, 16), Math.pow(2, 32), false, false];
[
Uint8Array,
Uint16Array,
Uint32Array,
Float32Array,
Float64Array
].forEach(function(ctor, i) {
concatTypedArray(ctor, 4000, max[i]);
});
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.1_3
description: Array.prototype.concat length throws
---*/
function MyError() {}
var obj = {};
obj[Symbol.isConcatSpreadable] = true;
Object.defineProperty(obj, "length", {
get: function() { throw new MyError(); }
});
assert.throws(MyError, function() {
[].concat(obj);
});
assert.throws(MyError, function() {
Array.prototype.concat.call(obj, 1, 2, 3);
});
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.1_3
description: Array.prototype.concat no prototype
---*/
assert.sameValue(Array.prototype.concat.prototype, void 0);
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.1_3
description: array-concat-non-array
includes: [compareArray.js]
---*/
//
// test262 --command "v8 --harmony-classes --harmony_object_literals" array-concat-non-array
//
class NonArray {
constructor() {
Array.apply(this, arguments);
}
}
var obj = new NonArray(1,2,3);
var result = Array.prototype.concat.call(obj, 4, 5, 6);
assert.sameValue(Array, result.constructor);
assert.sameValue(result instanceof NonArray, false);
assert(compareArray(result, [obj,4,5,6]));
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.1_3
description: Array.prototype.concat sloppy arguments throws
---*/
function MyError() {}
var args = (function(a) { return arguments; })(1,2,3);
Object.defineProperty(args, 0, {
get: function() { throw new MyError(); }
});
args[Symbol.isConcatSpreadable] = true;
assert.throws(MyError, function() {
return [].concat(args, args);
});
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.1_3
description: Array.prototype.concat sloppy arguments with dupes
includes: [compareArray.js]
---*/
var args = (function(a, a, a) { return arguments; })(1,2,3);
args[Symbol.isConcatSpreadable] = true;
assert(compareArray([].concat(args, args), [1, 2, 3, 1, 2, 3]));
Object.defineProperty(args, "length", { value: 6 });
assert(compareArray([].concat(args), [1, 2, 3, void 0, void 0, void 0]));
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.1_3
description: Array.prototype.concat sloppy arguments
includes: [compareArray.js]
---*/
var args = (function(a, b, c) { return arguments; })(1,2,3);
args[Symbol.isConcatSpreadable] = true;
assert(compareArray([].concat(args, args), [1, 2, 3, 1, 2, 3]));
Object.defineProperty(args, "length", { value: 6 });
assert(compareArray([].concat(args), [1, 2, 3, void 0, void 0, void 0]));
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.1_3
description: Array.prototype.concat small typed array
includes: [compareArray.js]
---*/
function concatTypedArray(type, elems, modulo) {
var items = new Array(elems);
var ta_by_len = new type(elems);
for (var i = 0; i < elems; ++i) {
ta_by_len[i] = items[i] = modulo === false ? i : elems % modulo;
}
var ta = new type(items);
assert(compareArray([].concat(ta, ta), [ta, ta]));
ta[Symbol.isConcatSpreadable] = true;
assert(compareArray([].concat(ta), items));
assert(compareArray([].concat(ta_by_len, ta_by_len), [ta_by_len, ta_by_len]));
ta_by_len[Symbol.isConcatSpreadable] = true;
assert(compareArray([].concat(ta_by_len), items));
// TypedArray with fake `length`.
ta = new type(1);
var defValue = ta[0];
var expected = new Array(4000);
expected[0] = defValue;
Object.defineProperty(ta, "length", { value: 4000 });
ta[Symbol.isConcatSpreadable] = true;
assert(compareArray([].concat(ta), expected));
}
var max = [Math.pow(2, 8), Math.pow(2, 16), Math.pow(2, 32), false, false];
[
Uint8Array,
Uint16Array,
Uint32Array,
Float32Array,
Float64Array
].forEach(function(ctor, i) {
concatTypedArray(ctor, 1, max[i]);
});
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.1_3
description: Array.prototype.concat Symbol.isConcatSpreadable boolean wrapper
includes: [compareArray.js]
---*/
var bool = new Boolean(true)
// Boolean wrapper objects are not concat-spreadable by default
assert(compareArray([bool], [].concat(bool)));
// Boolean wrapper objects may be individually concat-spreadable
bool[Symbol.isConcatSpreadable] = true;
bool.length = 3;
bool[0] = 1, bool[1] = 2, bool[2] = 3;
assert(compareArray([1, 2, 3], [].concat(bool)));
Boolean.prototype[Symbol.isConcatSpreadable] = true;
// Boolean wrapper objects may be concat-spreadable
assert(compareArray([], [].concat(new Boolean(true))));
Boolean.prototype[0] = 1;
Boolean.prototype[1] = 2;
Boolean.prototype[2] = 3;
Boolean.prototype.length = 3;
assert(compareArray([1,2,3], [].concat(new Boolean(true))));
// Boolean values are never concat-spreadable
assert(compareArray([true], [].concat(true)));
delete Boolean.prototype[Symbol.isConcatSpreadable];
delete Boolean.prototype[0];
delete Boolean.prototype[1];
delete Boolean.prototype[2];
delete Boolean.prototype.length;
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.1_3
description: Array.prototype.concat Symbol.isConcatSpreadable function
includes: [compareArray.js]
---*/
var fn = function(a, b, c) {}
// Functions are not concat-spreadable by default
assert(compareArray([fn], [].concat(fn)));
// Functions may be individually concat-spreadable
fn[Symbol.isConcatSpreadable] = true;
fn[0] = 1, fn[1] = 2, fn[2] = 3;
assert(compareArray([1, 2, 3], [].concat(fn)));
Function.prototype[Symbol.isConcatSpreadable] = true;
// Functions may be concat-spreadable
assert(compareArray([void 0, void 0, void 0], [].concat(function(a,b,c) {})));
Function.prototype[0] = 1;
Function.prototype[1] = 2;
Function.prototype[2] = 3;
assert(compareArray([1,2,3], [].concat(function(a, b, c) {})));
delete Function.prototype[Symbol.isConcatSpreadable];
delete Function.prototype[0];
delete Function.prototype[1];
delete Function.prototype[2];
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment