mirror of https://github.com/tc39/test262.git
flatten and flatMap tests
This commit is contained in:
parent
f68488ffad
commit
1547e49c95
|
@ -0,0 +1,33 @@
|
|||
// Copyright (C) 2018 Shilpi Jain. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
esid: sec-array.prototype.flatMap
|
||||
description: >
|
||||
array-like objects can be flattened
|
||||
includes: [compareArray.js]
|
||||
---*/
|
||||
|
||||
function takesTwoParams(a, b) {
|
||||
return Array.prototype.flatMap.call(arguments, function(ele) { return ele * 2});
|
||||
}
|
||||
|
||||
var actual = takesTwoParams(1,[2]);
|
||||
var expected = [2, 4];
|
||||
|
||||
assert(compareArray(actual, expected), 'arguments array like object');
|
||||
|
||||
var a = {
|
||||
"length": 1,
|
||||
"0": 1
|
||||
};
|
||||
|
||||
actual = Array.prototype.flatMap.call(a, function(ele) { return ele * 2});
|
||||
assert.sameValue(JSON.stringify(actual), JSON.stringify(['2']), 'array like objects');
|
||||
|
||||
var a = {
|
||||
"length": undefined,
|
||||
"0": 1
|
||||
};
|
||||
|
||||
actual = Array.prototype.flatMap.call(a, function(ele) { return ele * 2});
|
||||
assert.sameValue(JSON.stringify(actual), JSON.stringify([]), 'array like objects');
|
|
@ -0,0 +1,13 @@
|
|||
// Copyright (C) 2018 Shilpi Jain. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
esid: sec-array.prototype.flatMap
|
||||
description: >
|
||||
Behavior when array is depth more than 1
|
||||
includes: [compareArray.js]
|
||||
---*/
|
||||
|
||||
var a = [void 0,[void 0]];
|
||||
var flattenMap = [].flatMap.bind(a, function() {});
|
||||
|
||||
assert.compareArray(a.flatMap(flattenMap), [undefined, undefined, undefined, undefined]);
|
|
@ -0,0 +1,24 @@
|
|||
// Copyright (C) 2018 Shilpi Jain. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
esid: sec-array.prototype.flatMap
|
||||
description: >
|
||||
Behavior when array is depth more than 1
|
||||
includes: [compareArray.js]
|
||||
---*/
|
||||
|
||||
assert(compareArray([1, [2]].flatMap(function(ele) {
|
||||
return ele * 2;
|
||||
}), [2, 4]), 'array depth is 1');
|
||||
|
||||
assert(compareArray([1, [2], [[3]]].flatMap(function(ele) {
|
||||
return ele * 2;
|
||||
}), [2, 4, 6]), 'array depth is more than 1');
|
||||
|
||||
var actual = [1, [2], [3, [3]]].flatMap(function(ele) {
|
||||
return ele * 2;
|
||||
});
|
||||
|
||||
assert.sameValue(actual[0], 2);
|
||||
assert.sameValue(actual[1], 4);
|
||||
assert(isNaN(actual[2]));
|
|
@ -0,0 +1,18 @@
|
|||
// Copyright (C) 2018 Shilpi Jain. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
es6id: sec-array.prototype.flatMap
|
||||
description: Array.prototype.flatMap.length value and descriptor.
|
||||
info: >
|
||||
17 ECMAScript Standard Built-in Objects
|
||||
includes: [propertyHelper.js]
|
||||
---*/
|
||||
|
||||
assert.sameValue(
|
||||
Array.prototype.flatMap.length, 1,
|
||||
'The value of `Array.prototype.flatmap.length` is `1`'
|
||||
);
|
||||
|
||||
verifyNotEnumerable(Array.prototype.flatMap, 'length');
|
||||
verifyNotWritable(Array.prototype.flatMap, 'length');
|
||||
verifyConfigurable(Array.prototype.flatMap, 'length');
|
|
@ -0,0 +1,18 @@
|
|||
// Copyright (C) 2018 Shilpi Jain. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
es6id: sec-array.prototype.flatmap
|
||||
description: Array.prototype.flatmap name value and descriptor.
|
||||
info: >
|
||||
17 ECMAScript Standard Built-in Objects
|
||||
includes: [propertyHelper.js]
|
||||
---*/
|
||||
|
||||
assert.sameValue(
|
||||
Array.prototype.flatMap.name, 'flatMap',
|
||||
'The value of `Array.prototype.flatMap.name` is `"flatMap"`'
|
||||
);
|
||||
|
||||
verifyNotEnumerable(Array.prototype.flatMap, 'name');
|
||||
verifyNotWritable(Array.prototype.flatMap, 'name');
|
||||
verifyConfigurable(Array.prototype.flatMap, 'name');
|
|
@ -0,0 +1,11 @@
|
|||
// Copyright (C) 2018 Shilpi Jain. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
esid: sec-array.prototype.flatMap
|
||||
description: >
|
||||
non callable argument should throw TypeError Exception
|
||||
---*/
|
||||
|
||||
assert.throws(TypeError, function () {
|
||||
[].flatMap({});
|
||||
}, 'non callable argument');
|
|
@ -0,0 +1,34 @@
|
|||
// Copyright (C) 2018 Shilpi Jain. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
esid: sec-array.prototype.flatMap
|
||||
description: >
|
||||
Behavior when `constructor` property is neither an Object nor undefined
|
||||
- if IsConstructor(C) is false, throw a TypeError exception.
|
||||
---*/
|
||||
|
||||
var a = [];
|
||||
|
||||
a.constructor = null;
|
||||
assert.throws(TypeError, function() {
|
||||
a.flatMap();
|
||||
}, 'null value');
|
||||
|
||||
a = [];
|
||||
a.constructor = 1;
|
||||
assert.throws(TypeError, function() {
|
||||
a.flatMap();
|
||||
}, 'number value');
|
||||
|
||||
a = [];
|
||||
a.constructor = 'string';
|
||||
assert.throws(TypeError, function() {
|
||||
a.flatMap();
|
||||
}, 'string value');
|
||||
|
||||
a = [];
|
||||
a.constructor = true;
|
||||
assert.throws(TypeError, function() {
|
||||
a.flatMap();
|
||||
}, 'boolean value');
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
// Copyright (C) 2018 Shilpi Jain. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
esid: sec-array.prototype.flatMap
|
||||
description: >
|
||||
null or undefined should throw TypeError Exception
|
||||
---*/
|
||||
|
||||
assert.throws(TypeError, function() {
|
||||
[].flatMap.call(null);
|
||||
}, 'null value');
|
||||
|
||||
assert.throws(TypeError, function() {
|
||||
[].flatMap.call();
|
||||
}, 'missing');
|
||||
|
||||
assert.throws(TypeError, function() {
|
||||
[].flatMap.call(void 0);
|
||||
}, 'undefined');
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
// Copyright (C) 2018 Shilpi Jain. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
esid: sec-array.prototype.flatMap
|
||||
description: >
|
||||
Behavior when thisArg is provided
|
||||
Array.prototype.flatMap ( mapperFunction [ , thisArg ] )
|
||||
includes: [compareArray.js]
|
||||
---*/
|
||||
|
||||
"use strict";
|
||||
|
||||
var a;
|
||||
|
||||
assert(compareArray([1].flatMap(function() { return this}, "TestString"), ["TestString"]));
|
||||
assert(compareArray([1].flatMap(function() { return this}, 1), [1]));
|
||||
assert(compareArray([1].flatMap(function() { return this}, null), [null]));
|
||||
assert(compareArray([1].flatMap(function() { return this}, true), [true]));
|
||||
assert(compareArray([1].flatMap(function() { return this}, a = {}), [a]));
|
||||
assert(compareArray([1].flatMap(function() { return this}, void 0), [undefined]));
|
|
@ -0,0 +1,31 @@
|
|||
// Copyright (C) 2018 Shilpi Jain. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
esid: sec-array.prototype.flatten
|
||||
description: >
|
||||
array-like objects can be flattened
|
||||
includes: [compareArray.js]
|
||||
---*/
|
||||
|
||||
function takesTwoParams(a, b) {
|
||||
return Array.prototype.flatten.call(arguments);
|
||||
}
|
||||
|
||||
var actual = takesTwoParams(1,[2]);
|
||||
var expected = [1, 2];
|
||||
|
||||
assert(compareArray(actual, expected), 'arguments array like object');
|
||||
|
||||
var a = {
|
||||
"length": 1,
|
||||
"0": 'a'
|
||||
};
|
||||
|
||||
actual = Array.prototype.flatten.call(a);
|
||||
assert.sameValue(JSON.stringify(actual), JSON.stringify(['a']), 'array like objects');
|
||||
|
||||
a = {
|
||||
"length": undefined,
|
||||
"0": 'a'
|
||||
};
|
||||
assert.sameValue(JSON.stringify(actual), JSON.stringify([]), 'array like objects undefined length');
|
|
@ -0,0 +1,13 @@
|
|||
// Copyright (C) 2018 Shilpi Jain. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
esid: sec-array.prototype.flatten
|
||||
description: >
|
||||
using bound functions
|
||||
includes: [compareArray.js]
|
||||
---*/
|
||||
|
||||
var a = [1,[1]];
|
||||
var flattenIt = [].flatten.bind(a);
|
||||
|
||||
assert(compareArray(flattenIt(), [1, 1]), 'bound functions');
|
|
@ -0,0 +1,15 @@
|
|||
// Copyright (C) 2018 Shilpi Jain. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
esid: sec-array.prototype.flatten
|
||||
description: >
|
||||
arrays with empty arrays elements
|
||||
includes: [compareArray.js]
|
||||
---*/
|
||||
|
||||
var a;
|
||||
assert(compareArray([].flatten([[]]), []));
|
||||
assert(compareArray(Array.prototype.flatten.call([[], []]), []));
|
||||
assert(compareArray(Array.prototype.flatten.call([[], [1]]), [1]));
|
||||
assert(compareArray(Array.prototype.flatten.call([[], [1, a = []]]), [1, a]));
|
||||
assert.sameValue(JSON.stringify(Array.prototype.flatten.call([{}, []])), JSON.stringify([{}]));
|
|
@ -0,0 +1,12 @@
|
|||
// Copyright (C) 2018 Shilpi Jain. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
esid: sec-array.prototype.flatten
|
||||
description: >
|
||||
arrays with empty object elements
|
||||
---*/
|
||||
|
||||
assert.sameValue(JSON.stringify(Array.prototype.flatten.call([{}])), JSON.stringify([{}]));
|
||||
assert.sameValue(JSON.stringify(Array.prototype.flatten.call([{}, [{}]])), JSON.stringify([{}, {}]));
|
||||
assert.sameValue(JSON.stringify(Array.prototype.flatten.call([[{null: {}}], [{}]])), JSON.stringify([{null: {}}, {}]));
|
||||
assert.sameValue(JSON.stringify(Array.prototype.flatten.call([[{null: null}], [{}]])), JSON.stringify([{null: null}, {}]));
|
|
@ -0,0 +1,18 @@
|
|||
// Copyright (C) 2018 Shilpi Jain. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
es6id: sec-array.prototype.flatten
|
||||
description: Array.prototype.flatten.length value and descriptor.
|
||||
info: >
|
||||
17 ECMAScript Standard Built-in Objects
|
||||
includes: [propertyHelper.js]
|
||||
---*/
|
||||
|
||||
assert.sameValue(
|
||||
Array.prototype.flatten.length, 0,
|
||||
'The value of `Array.prototype.flatten.length` is `0`'
|
||||
);
|
||||
|
||||
verifyNotEnumerable(Array.prototype.flatten, 'length');
|
||||
verifyNotWritable(Array.prototype.flatten, 'length');
|
||||
verifyConfigurable(Array.prototype.flatten, 'length');
|
|
@ -0,0 +1,20 @@
|
|||
// Copyright (C) 2018 Shilpi Jain. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
esid: sec-array.prototype.flatten
|
||||
description: >
|
||||
Array.prototype.flatten.name value and descriptor.
|
||||
info: >
|
||||
17 ECMAScript Standard Built-in Objects
|
||||
includes: [propertyHelper.js]
|
||||
---*/
|
||||
|
||||
assert.sameValue(
|
||||
Array.prototype.flatten.name, 'flatten',
|
||||
'The value of `Array.prototype.flatten.name` is `"flatten"`'
|
||||
);
|
||||
|
||||
verifyNotEnumerable(Array.prototype.flatten, 'name');
|
||||
verifyNotWritable(Array.prototype.flatten, 'name');
|
||||
verifyConfigurable(Array.prototype.flatten, 'name');
|
||||
|
42
test/built-ins/Array/prototype/flatten/non-numeric-depth-should-not-throw.js
vendored
Normal file
42
test/built-ins/Array/prototype/flatten/non-numeric-depth-should-not-throw.js
vendored
Normal file
|
@ -0,0 +1,42 @@
|
|||
// Copyright (C) 2018 Shilpi Jain. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
esid: sec-array.prototype.flatten
|
||||
description: >
|
||||
if the argument is a string or object, the depthNum is 0
|
||||
includes: [compareArray.js]
|
||||
---*/
|
||||
|
||||
var a = [1, [2]];
|
||||
var expected = a;
|
||||
|
||||
// non integral string depthNum is converted to 0
|
||||
var depthNum = 'TestString';
|
||||
var actual = a.flatten(depthNum);
|
||||
assert(compareArray(actual, expected), 'non integral string depthNum');
|
||||
|
||||
// object type depthNum is converted to 0
|
||||
depthNum = {};
|
||||
var actual = a.flatten(depthNum);
|
||||
assert(compareArray(actual, expected), 'object type depthNum');
|
||||
|
||||
// negative infinity depthNum is converted to 0
|
||||
depthNum = Number.NEGATIVE_INFINITY;
|
||||
var actual = a.flatten(depthNum);
|
||||
assert(compareArray(actual, expected), 'negative infinity depthNum');
|
||||
|
||||
// positive zero depthNum is converted to 0
|
||||
depthNum = +0;
|
||||
var actual = a.flatten(depthNum);
|
||||
assert(compareArray(actual, expected), 'positive zero depthNum');
|
||||
|
||||
// negative zero depthNum is converted to 0
|
||||
depthNum = -0;
|
||||
var actual = a.flatten(depthNum);
|
||||
assert(compareArray(actual, expected), 'negative zero depthNum');
|
||||
|
||||
// integral string depthNum is converted to an integer
|
||||
depthNum = '1';
|
||||
var actual = a.flatten(depthNum);
|
||||
expected = [1, 2]
|
||||
assert(compareArray(actual, expected), 'integral string depthNum');
|
|
@ -0,0 +1,33 @@
|
|||
// Copyright (C) 2018 Shilpi Jain. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
esid: sec-array.prototype.flatten
|
||||
description: >
|
||||
Behavior when `constructor` property is neither an Object nor undefined
|
||||
- if IsConstructor(C) is false, throw a TypeError exception.
|
||||
---*/
|
||||
|
||||
var a = [];
|
||||
|
||||
a.constructor = null;
|
||||
assert.throws(TypeError, function() {
|
||||
a.flatten();
|
||||
}, 'null value');
|
||||
|
||||
a = [];
|
||||
a.constructor = 1;
|
||||
assert.throws(TypeError, function() {
|
||||
a.flatten();
|
||||
}, 'number value');
|
||||
|
||||
a = [];
|
||||
a.constructor = 'string';
|
||||
assert.throws(TypeError, function() {
|
||||
a.flatten();
|
||||
}, 'string value');
|
||||
|
||||
a = [];
|
||||
a.constructor = true;
|
||||
assert.throws(TypeError, function() {
|
||||
a.flatten();
|
||||
}, 'boolean value');
|
|
@ -0,0 +1,15 @@
|
|||
// Copyright (C) 2018 Shilpi Jain. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
esid: sec-array.prototype.flatten
|
||||
description: >
|
||||
arrays with null, and undefined
|
||||
includes: [compareArray.js]
|
||||
---*/
|
||||
var a;
|
||||
|
||||
assert(compareArray(Array.prototype.flatten.call([1, null, void 0]), [1, null, undefined]));
|
||||
assert(compareArray(Array.prototype.flatten.call([1,[null, void 0]]), [1, null, undefined]));
|
||||
assert(compareArray(Array.prototype.flatten.call([[null, void 0], [null, void 0]]), [null, undefined, null, undefined]));
|
||||
assert(compareArray(Array.prototype.flatten.call([1,[null, a = [void 0]]], 1), [1, null, a]));
|
||||
assert(compareArray(Array.prototype.flatten.call([1,[null, [void 0]]], 2), [1, null, undefined]));
|
|
@ -0,0 +1,20 @@
|
|||
// Copyright (C) 2018 Shilpi Jain. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
esid: sec-array.prototype.flatten
|
||||
description: >
|
||||
null or undefined should throw TypeError Exception
|
||||
---*/
|
||||
|
||||
assert.throws(TypeError, function() {
|
||||
[].flatten.call(null);
|
||||
}, 'null value');
|
||||
|
||||
assert.throws(TypeError, function() {
|
||||
[].flatten.call();
|
||||
}, 'missing');
|
||||
|
||||
assert.throws(TypeError, function() {
|
||||
[].flatten.call(void 0);
|
||||
}, 'undefined');
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
// Copyright (C) 2018 Shilpi Jain. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
esid: sec-array.prototype.flatten
|
||||
description: >
|
||||
if the argument is a positive infinity, the depthNum is max depth of the array
|
||||
includes: [compareArray.js]
|
||||
---*/
|
||||
|
||||
var a = [1, [2, [3, [4]]]]
|
||||
assert(compareArray(a.flatten(Number.POSITIVE_INFINITY), [1, 2, 3, 4]), 'positive infinity depthNum');
|
|
@ -0,0 +1,20 @@
|
|||
// Copyright (C) 2018 Shilpi Jain. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
esid: sec-array.prototype.flatten
|
||||
es6id: 22.1.3
|
||||
description: Property type and descriptor.
|
||||
info: >
|
||||
17 ECMAScript Standard Built-in Objects
|
||||
includes: [propertyHelper.js]
|
||||
---*/
|
||||
|
||||
assert.sameValue(
|
||||
typeof Array.prototype.flatten,
|
||||
'function',
|
||||
'`typeof Array.prototype.flatten` is `function`'
|
||||
);
|
||||
|
||||
verifyNotEnumerable(Array.prototype, 'flatten');
|
||||
verifyWritable(Array.prototype, 'flatten');
|
||||
verifyConfigurable(Array.prototype, 'flatten');
|
21
test/built-ins/Array/prototype/flatten/symbol-object-create-null-depth-throws.js
vendored
Normal file
21
test/built-ins/Array/prototype/flatten/symbol-object-create-null-depth-throws.js
vendored
Normal file
|
@ -0,0 +1,21 @@
|
|||
// Copyright (C) 2018 Shilpi Jain. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
esid: sec-array.prototype.flatten
|
||||
description: >
|
||||
if the argument is a Symbol or Object null, it throws exception
|
||||
---*/
|
||||
|
||||
var a = [];
|
||||
var depthNum = Symbol();
|
||||
|
||||
assert.throws(TypeError, function() {
|
||||
a.flatten(depthNum);
|
||||
}, 'symbol value');
|
||||
|
||||
depthNum = Object.create(null);
|
||||
|
||||
assert.throws(TypeError, function() {
|
||||
a.flatten(depthNum);
|
||||
}, 'object create null');
|
||||
|
Loading…
Reference in New Issue