diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /js/src/tests/test262/built-ins/Array/prototype/flatMap | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'js/src/tests/test262/built-ins/Array/prototype/flatMap')
25 files changed, 1316 insertions, 0 deletions
diff --git a/js/src/tests/test262/built-ins/Array/prototype/flatMap/array-like-objects-nested.js b/js/src/tests/test262/built-ins/Array/prototype/flatMap/array-like-objects-nested.js new file mode 100644 index 0000000000..58152e6c67 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/flatMap/array-like-objects-nested.js @@ -0,0 +1,74 @@ +// Copyright (C) 2018 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-array.prototype.flatMap +description: > + Does not flatten array-like objects nested into the main array +info: | + FlattenIntoArray(target, source, sourceLen, start, depth [ , mapperFunction, thisArg ]) + + 1. Let targetIndex be start. + 2. Let sourceIndex be 0. + 3. Repeat, while sourceIndex < sourceLen + a. Let P be ! ToString(sourceIndex). + b. Let exists be ? HasProperty(source, P). + c. If exists is true, then + i. Let element be ? Get(source, P). + ii. If mapperFunction is present, then + 1. Assert: thisArg is present. + 2. Set element to ? Call(mapperFunction, thisArg , « element, sourceIndex, source »). + iii. Let shouldFlatten be false. + iv. If depth > 0, then + 1. Set shouldFlatten to ? IsArray(element). + v. If shouldFlatten is true, then + 1. Let elementLen be ? ToLength(? Get(element, "length")). + 2. Set targetIndex to ? FlattenIntoArray(target, element, elementLen, targetIndex, depth - 1). + vi. Else, + 1. If targetIndex ≥ 253-1, throw a TypeError exception. + 2. Perform ? CreateDataPropertyOrThrow(target, ! ToString(targetIndex), element). + 3. Increase targetIndex by 1. +includes: [compareArray.js] +features: [Array.prototype.flatMap, Int32Array] +---*/ + +function fn(e) { + return e; +} + +var obj1 = { + length: 1, + 0: 'a', + toString() { return 'obj1'; } +}; + +var obj2 = new Int32Array(2); + +var obj3 = { + get length() { throw "should not even consider the length property" }, + toString() { return 'obj3'; } +}; + +var arr = [obj1, obj2, obj3]; +var actual = arr.flatMap(fn); +assert.compareArray(actual, arr, 'The value of actual is expected to equal the value of arr'); +assert.notSameValue(actual, arr, 'The value of actual is expected to not equal the value of `arr`'); + +var arrLike = { + length: 4, + 0: obj1, + 1: obj2, + 2: obj3, + get 3() { return arrLike }, + toString() { return 'obj4'; } +}; + +actual = [].flatMap.call(arrLike, fn); +assert.compareArray(actual, [obj1, obj2, obj3, arrLike], 'The value of actual is expected to be [obj1, obj2, obj3, arrLike]'); +assert.notSameValue(actual, arrLike, 'The value of actual is expected to not equal the value of `arrLike`'); +assert.sameValue( + Object.getPrototypeOf(actual), + Array.prototype, + 'Object.getPrototypeOf([].flatMap.call(arrLike, fn)") returns Array.prototype' +); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/flatMap/array-like-objects-poisoned-length.js b/js/src/tests/test262/built-ins/Array/prototype/flatMap/array-like-objects-poisoned-length.js new file mode 100644 index 0000000000..b7a8d20ef4 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/flatMap/array-like-objects-poisoned-length.js @@ -0,0 +1,62 @@ +// Copyright (C) 2018 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-array.prototype.flatMap +description: > + Observe abrupt completion in poisoned lengths of array-like objects +info: | + Array.prototype.flatMap ( mapperFunction [ , thisArg ] ) + + 1. Let O be ? ToObject(this value). + 2. Let sourceLen be ? ToLength(? Get(O, "length")). +features: [Array.prototype.flatMap, Symbol.toPrimitive] +---*/ + +assert.sameValue(typeof Array.prototype.flatMap, 'function'); + +function fn(e) { + return e; +} + +var arr = { + length: Symbol(), +}; +assert.throws(TypeError, function() { + [].flatMap.call(arr, fn); +}, 'length is a symbol'); + +arr = { + get length() { throw new Test262Error() } +}; +assert.throws(Test262Error, function() { + [].flatMap.call(arr, fn); +}, 'custom get error'); + +arr = { + length: { + valueOf() { throw new Test262Error() } + } +}; +assert.throws(Test262Error, function() { + [].flatMap.call(arr, fn); +}, 'custom valueOf error'); + +arr = { + length: { + toString() { throw new Test262Error() } + } +}; +assert.throws(Test262Error, function() { + [].flatMap.call(arr, fn); +}, 'custom toString error'); + +arr = { + length: { + [Symbol.toPrimitive]() { throw new Test262Error() } + } +}; +assert.throws(Test262Error, function() { + [].flatMap.call(arr, fn); +}, 'custom Symbol.toPrimitive error'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/flatMap/array-like-objects-typedarrays.js b/js/src/tests/test262/built-ins/Array/prototype/flatMap/array-like-objects-typedarrays.js new file mode 100644 index 0000000000..6215e4d2d9 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/flatMap/array-like-objects-typedarrays.js @@ -0,0 +1,52 @@ +// Copyright (C) 2018 Leo Balter. 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 (typedArrays) +info: | + Array.prototype.flatMap ( mapperFunction [ , thisArg ] ) + + 1. Let O be ? ToObject(this value). + 2. Let sourceLen be ? ToLength(? Get(O, "length")). + ... + 5. Let A be ? ArraySpeciesCreate(O, 0). + ... + + ArraySpeciesCreate ( originalArray, length ) + + 3. Let isArray be ? IsArray(originalArray). + 4. If isArray is false, return ? ArrayCreate(length). + 5. Let C be ? Get(originalArray, "constructor"). +includes: [compareArray.js] +features: [Array.prototype.flatMap, Int32Array] +---*/ + +function same(e) { + return e; +} + +var ta; +var actual; + +ta = new Int32Array([1, 0, 42]); + +Object.defineProperty(ta, 'constructor', { + get() { throw "it should not object the typedarray ctor"; } +}); +actual = [].flatMap.call(ta, same); +assert.compareArray(actual, [1, 0, 42], 'The value of actual is expected to be [1, 0, 42]'); +assert.sameValue(Object.getPrototypeOf(actual), Array.prototype, 'Object.getPrototypeOf([].flatMap.call(ta, same)") returns Array.prototype'); +assert.sameValue(actual instanceof Int32Array, false, 'The result of evaluating (actual instanceof Int32Array) is expected to be false'); + +ta = new Int32Array(0); + +Object.defineProperty(ta, 'constructor', { + get() { throw "it should not object the typedarray ctor"; } +}); +actual = [].flatMap.call(ta, same); +assert.compareArray(actual, [], 'The value of actual is expected to be []'); +assert.sameValue(Object.getPrototypeOf(actual), Array.prototype, 'Object.getPrototypeOf([].flatMap.call(ta, same)") returns Array.prototype'); +assert.sameValue(actual instanceof Int32Array, false, 'The result of evaluating (actual instanceof Int32Array) is expected to be false'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/flatMap/array-like-objects.js b/js/src/tests/test262/built-ins/Array/prototype/flatMap/array-like-objects.js new file mode 100644 index 0000000000..6c5fa28717 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/flatMap/array-like-objects.js @@ -0,0 +1,87 @@ +// Copyright (C) 2018 Shilpi Jain and Michael Ficarra. 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 +info: | + Array.prototype.flatMap ( mapperFunction [ , thisArg ] ) + + 1. Let O be ? ToObject(this value). + 2. Let sourceLen be ? ToLength(? Get(O, "length")). + ... + 5. Let A be ? ArraySpeciesCreate(O, 0). + ... + + ArraySpeciesCreate ( originalArray, length ) + + 3. Let isArray be ? IsArray(originalArray). + 4. If isArray is false, return ? ArrayCreate(length). + + FlattenIntoArray(target, source, sourceLen, start, depth [ , mapperFunction, thisArg ]) + + 1. Let targetIndex be start. + 2. Let sourceIndex be 0. + 3. Repeat, while sourceIndex < sourceLen + a. Let P be ! ToString(sourceIndex). + b. Let exists be ? HasProperty(source, P). + c. If exists is true, then + ... + ** Skip if property does not exist ** +includes: [compareArray.js] +features: [Array.prototype.flatMap] +---*/ + +function fn(e) { + return [39, e * 2]; // returns an array to observe it being flattened after +} + +var a; +var actual; + +a = { + length: 3, + 0: 1, + // property 1 will be fully skipped + 2: 21, + get 3() { throw 'it should not get this property'; } +}; +actual = [].flatMap.call(a, fn); +assert.compareArray(actual, [39, 2, 39, 42], 'The value of actual is expected to be [39, 2, 39, 42]'); +assert.sameValue(Object.getPrototypeOf(actual), Array.prototype, 'Object.getPrototypeOf([].flatMap.call(a, fn)") returns Array.prototype'); + +a = { + length: undefined, + get 0() { throw 'it should not get this property'; }, +}; +actual = [].flatMap.call(a, fn); +assert.compareArray(actual, [], 'The value of actual is expected to be []'); +assert.sameValue(Object.getPrototypeOf(actual), Array.prototype, 'Object.getPrototypeOf([].flatMap.call(a, fn)") returns Array.prototype'); + +var called = false; +a = { + get length() { + if (!called) { + called = true; + return 2; + } else { + throw 'is should get the length only once'; + } + }, + 0: 21, + 1: 19.5, + get 2() { throw 'it should not get this property'; }, +}; +actual = [].flatMap.call(a, fn); +assert.compareArray(actual, [39, 42, 39, 39], 'The value of actual is expected to be [39, 42, 39, 39]'); +assert.sameValue(Object.getPrototypeOf(actual), Array.prototype, 'Object.getPrototypeOf([].flatMap.call(a, fn)") returns Array.prototype'); + +a = { + length: 10001, + [10000]: 7, +}; +actual = [].flatMap.call(a, fn); +assert.compareArray(actual, [39, 14], 'The value of actual is expected to be [39, 14]'); +assert.sameValue(Object.getPrototypeOf(actual), Array.prototype, 'Object.getPrototypeOf([].flatMap.call(a, fn)") returns Array.prototype'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/flatMap/bound-function-argument.js b/js/src/tests/test262/built-ins/Array/prototype/flatMap/bound-function-argument.js new file mode 100644 index 0000000000..e9d9dfb39e --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/flatMap/bound-function-argument.js @@ -0,0 +1,16 @@ +// Copyright (C) 2018 Shilpi Jain and Michael Ficarra. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-array.prototype.flatMap +description: > + Behavior when given a bound function +includes: [compareArray.js] +features: [Array.prototype.flatMap] +---*/ + +var a = [0, 0]; +assert.compareArray(a.flatMap(function() { + return this; +}.bind([1, 2])), [1, 2, 1, 2], 'a.flatMap(function() {return this;}.bind([1, 2])) must return [1, 2, 1, 2]'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/flatMap/browser.js b/js/src/tests/test262/built-ins/Array/prototype/flatMap/browser.js new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/flatMap/browser.js diff --git a/js/src/tests/test262/built-ins/Array/prototype/flatMap/call-with-boolean.js b/js/src/tests/test262/built-ins/Array/prototype/flatMap/call-with-boolean.js new file mode 100644 index 0000000000..8ebd2c8e20 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/flatMap/call-with-boolean.js @@ -0,0 +1,21 @@ +// Copyright (c) 2020 Rick Waldron. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-array.prototype.flatMap +description: Array.prototype.flatMap applied to boolean primitive +includes: [compareArray.js] +---*/ + +assert.compareArray( + Array.prototype.flatMap.call(true, () => {}), + [], + 'Array.prototype.flatMap.call(true, () => {}) must return []' +); +assert.compareArray( + Array.prototype.flatMap.call(false, () => {}), + [], + 'Array.prototype.flatMap.call(false, () => {}) must return []' +); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/flatMap/depth-always-one.js b/js/src/tests/test262/built-ins/Array/prototype/flatMap/depth-always-one.js new file mode 100644 index 0000000000..b61c26bae4 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/flatMap/depth-always-one.js @@ -0,0 +1,25 @@ +// Copyright (C) 2018 Shilpi Jain and Michael Ficarra. 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] +features: [Array.prototype.flatMap] +---*/ + +assert.compareArray([1, 2].flatMap(function(e) { + return [e, e * 2]; +}), [1, 2, 2, 4], '[1, 2].flatMap(function(e) {return [e, e * 2];}) must return [1, 2, 2, 4]'); + +var result = [1, 2, 3].flatMap(function(ele) { + return [ + [ele * 2] + ]; +}); +assert.sameValue(result.length, 3, 'The value of result.length is expected to be 3'); +assert.compareArray(result[0], [2], 'The value of result[0] is expected to be [2]'); +assert.compareArray(result[1], [4], 'The value of result[1] is expected to be [4]'); +assert.compareArray(result[2], [6], 'The value of result[2] is expected to be [6]'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/flatMap/length.js b/js/src/tests/test262/built-ins/Array/prototype/flatMap/length.js new file mode 100644 index 0000000000..c0767be8cc --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/flatMap/length.js @@ -0,0 +1,23 @@ +// Copyright (C) 2018 Shilpi Jain and Michael Ficarra. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-array.prototype.flatMap +description: Array.prototype.flatMap.length value and descriptor. +info: > + 17 ECMAScript Standard Built-in Objects +includes: [propertyHelper.js] +features: [Array.prototype.flatMap] +---*/ + +assert.sameValue( + Array.prototype.flatMap.length, 1, + 'The value of `Array.prototype.flatmap.length` is `1`' +); + +verifyProperty(Array.prototype.flatMap, 'length', { + enumerable: false, + writable: false, + configurable: true, +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/flatMap/name.js b/js/src/tests/test262/built-ins/Array/prototype/flatMap/name.js new file mode 100644 index 0000000000..991c171e78 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/flatMap/name.js @@ -0,0 +1,23 @@ +// Copyright (C) 2018 Shilpi Jain and Michael Ficarra. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-array.prototype.flatmap +description: Array.prototype.flatmap name value and descriptor. +info: > + 17 ECMAScript Standard Built-in Objects +includes: [propertyHelper.js] +features: [Array.prototype.flatMap] +---*/ + +assert.sameValue( + Array.prototype.flatMap.name, 'flatMap', + 'The value of `Array.prototype.flatMap.name` is `"flatMap"`' +); + +verifyProperty(Array.prototype.flatMap, 'name', { + enumerable: false, + writable: false, + configurable: true, +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/flatMap/non-callable-argument-throws.js b/js/src/tests/test262/built-ins/Array/prototype/flatMap/non-callable-argument-throws.js new file mode 100644 index 0000000000..99fa7e30eb --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/flatMap/non-callable-argument-throws.js @@ -0,0 +1,52 @@ +// Copyright (C) 2018 Shilpi Jain and Michael Ficarra. 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 +info: | + Array.prototype.flatMap ( mapperFunction [ , thisArg ] ) + + 1. Let O be ? ToObject(this value). + 2. Let sourceLen be ? ToLength(? Get(O, "length")). + 3. If IsCallable(mapperFunction) is false, throw a TypeError exception. + ... +features: [Array.prototype.flatMap, Symbol] +---*/ + +assert.sameValue(typeof Array.prototype.flatMap, "function"); + +assert.throws(TypeError, function() { + [].flatMap({}); +}, 'non callable argument, object'); + +assert.throws(TypeError, function() { + [].flatMap(0); +}, 'non callable argument, number'); + +assert.throws(TypeError, function() { + [].flatMap(); +}, 'non callable argument, implict undefined'); + +assert.throws(TypeError, function() { + [].flatMap(undefined); +}, 'non callable argument, undefined'); + +assert.throws(TypeError, function() { + [].flatMap(null); +}, 'non callable argument, null'); + +assert.throws(TypeError, function() { + [].flatMap(false); +}, 'non callable argument, boolean'); + +assert.throws(TypeError, function() { + [].flatMap(''); +}, 'non callable argument, string'); + +var s = Symbol(); +assert.throws(TypeError, function() { + [].flatMap(s); +}, 'non callable argument, symbol'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/flatMap/not-a-constructor.js b/js/src/tests/test262/built-ins/Array/prototype/flatMap/not-a-constructor.js new file mode 100644 index 0000000000..8facbadc1c --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/flatMap/not-a-constructor.js @@ -0,0 +1,35 @@ +// Copyright (C) 2020 Rick Waldron. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-ecmascript-standard-built-in-objects +description: > + Array.prototype.flatMap does not implement [[Construct]], is not new-able +info: | + ECMAScript Function Objects + + Built-in function objects that are not identified as constructors do not + implement the [[Construct]] internal method unless otherwise specified in + the description of a particular function. + + sec-evaluatenew + + ... + 7. If IsConstructor(constructor) is false, throw a TypeError exception. + ... +includes: [isConstructor.js] +features: [Reflect.construct, Array.prototype.flatMap, arrow-function] +---*/ + +assert.sameValue( + isConstructor(Array.prototype.flatMap), + false, + 'isConstructor(Array.prototype.flatMap) must return false' +); + +assert.throws(TypeError, () => { + new Array.prototype.flatMap(() => {}); +}, '`new Array.prototype.flatMap(() => {})` throws TypeError'); + + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/flatMap/prop-desc.js b/js/src/tests/test262/built-ins/Array/prototype/flatMap/prop-desc.js new file mode 100644 index 0000000000..59e449ee48 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/flatMap/prop-desc.js @@ -0,0 +1,29 @@ +// Copyright (C) 2018 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-array.prototype.flatMap +description: Property type and descriptor. +info: > + 17 ECMAScript Standard Built-in Objects + + Every other data property described in clauses 18 through 26 and in Annex B.2 + has the attributes { [[Writable]]: true, [[Enumerable]]: false, + [[Configurable]]: true } unless otherwise specified. +includes: [propertyHelper.js] +features: [Array.prototype.flatMap] +---*/ + +assert.sameValue( + typeof Array.prototype.flatMap, + 'function', + '`typeof Array.prototype.flatMap` is `function`' +); + +verifyProperty(Array.prototype, 'flatMap', { + enumerable: false, + writable: true, + configurable: true, +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/flatMap/proxy-access-count.js b/js/src/tests/test262/built-ins/Array/prototype/flatMap/proxy-access-count.js new file mode 100644 index 0000000000..e1693d4ea8 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/flatMap/proxy-access-count.js @@ -0,0 +1,44 @@ +// Copyright (C) 2018 Richard Lawrence. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-array.prototype.flatMap +description: > + properties are accessed correct number of times by .flatMap +info: | + Array.prototype.flatMap ( mapperFunction [ , thisArg ] ) + + ... + 6. Perform ? FlattenIntoArray(A, O, sourceLen, 0, 1, mapperFunction, T). + + FlattenIntoArray (target, source, sourceLen, start, depth [ , mapperFunction, thisArg ]) + + 3. Repeat, while sourceIndex < sourceLen + a. Let P be ! ToString(sourceIndex). + b. Let exists be ? HasProperty(source, P). + c. If exists is true, then + i. Let element be ? Get(source, P). +features: [Array.prototype.flatMap] +includes: [compareArray.js] +---*/ + +assert.sameValue( + typeof Array.prototype.flatMap, + 'function', + 'The value of `typeof Array.prototype.flatMap` is expected to be "function"' +); + +const getCalls = [], hasCalls = []; +const handler = { + get : function (t, p, r) { getCalls.push(p); return Reflect.get(t, p, r); }, + has : function (t, p, r) { hasCalls.push(p); return Reflect.has(t, p, r); } +} + +const tier2 = new Proxy([4, 3], handler); +const tier1 = new Proxy([2, [3, 4, 2, 2], 5, tier2, 6], handler); + +Array.prototype.flatMap.call(tier1, function(a){ return a; }); + +assert.compareArray(getCalls, ["length", "constructor", "0", "1", "2", "3", "length", "0", "1", "4"], 'The value of getCalls is expected to be ["length", "constructor", "0", "1", "2", "3", "length", "0", "1", "4"]'); +assert.compareArray(hasCalls, ["0", "1", "2", "3", "0", "1", "4"], 'The value of hasCalls is expected to be ["0", "1", "2", "3", "0", "1", "4"]'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/flatMap/shell.js b/js/src/tests/test262/built-ins/Array/prototype/flatMap/shell.js new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/flatMap/shell.js diff --git a/js/src/tests/test262/built-ins/Array/prototype/flatMap/target-array-non-extensible.js b/js/src/tests/test262/built-ins/Array/prototype/flatMap/target-array-non-extensible.js new file mode 100644 index 0000000000..b5b17dfe6f --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/flatMap/target-array-non-extensible.js @@ -0,0 +1,50 @@ +// Copyright (C) 2020 Alexey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-array.prototype.flatmap +description: > + TypeError is thrown if CreateDataProperty fails. + (result object is non-extensible, source array gets flattened) +info: | + Array.prototype.flatMap ( mapperFunction [ , thisArg ] ) + + [...] + 6. Perform ? FlattenIntoArray(A, O, sourceLen, 0, depthNum). + + FlattenIntoArray ( target, source, sourceLen, start, depth [ , mapperFunction, thisArg ] ) + + [...] + 9. Repeat, while sourceIndex < sourceLen + [...] + c. If exists is true, then + [...] + v. If shouldFlatten is true, then + [...] + 2. Set targetIndex to ? FlattenIntoArray(target, element, elementLen, targetIndex, depth - 1). + [...] + + CreateDataPropertyOrThrow ( O, P, V ) + + [...] + 3. Let success be ? CreateDataProperty(O, P, V). + 4. If success is false, throw a TypeError exception. +features: [Symbol.species] +---*/ + +var A = function(_length) { + this.length = 0; + Object.preventExtensions(this); +}; + +var arr = [[1]]; +arr.constructor = {}; +arr.constructor[Symbol.species] = A; + +assert.throws(TypeError, function() { + arr.flatMap(function(item) { + return item; + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/flatMap/target-array-with-non-configurable-property.js b/js/src/tests/test262/built-ins/Array/prototype/flatMap/target-array-with-non-configurable-property.js new file mode 100644 index 0000000000..4df2aacac0 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/flatMap/target-array-with-non-configurable-property.js @@ -0,0 +1,54 @@ +// Copyright (C) 2020 Alexey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-array.prototype.flatmap +description: > + TypeError is thrown if CreateDataProperty fails. + (result object's "0" is non-configurable, source array is not flattened) +info: | + Array.prototype.flatMap ( mapperFunction [ , thisArg ] ) + + [...] + 6. Perform ? FlattenIntoArray(A, O, sourceLen, 0, depthNum). + + FlattenIntoArray ( target, source, sourceLen, start, depth [ , mapperFunction, thisArg ] ) + + [...] + 9. Repeat, while sourceIndex < sourceLen + [...] + c. If exists is true, then + [...] + v. If shouldFlatten is true, then + [...] + vi. Else, + [...] + 2. Perform ? CreateDataPropertyOrThrow(target, ! ToString(targetIndex), element). + [...] + + CreateDataPropertyOrThrow ( O, P, V ) + + [...] + 3. Let success be ? CreateDataProperty(O, P, V). + 4. If success is false, throw a TypeError exception. +features: [Symbol.species] +---*/ + +var A = function(_length) { + Object.defineProperty(this, "0", { + writable: true, + configurable: false, + }); +}; + +var arr = [1]; +arr.constructor = {}; +arr.constructor[Symbol.species] = A; + +assert.throws(TypeError, function() { + arr.flatMap(function(item) { + return item; + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/flatMap/target-array-with-non-writable-property.js b/js/src/tests/test262/built-ins/Array/prototype/flatMap/target-array-with-non-writable-property.js new file mode 100644 index 0000000000..1ed7aef093 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/flatMap/target-array-with-non-writable-property.js @@ -0,0 +1,56 @@ +// Copyright (C) 2020 Alexey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-array.prototype.flatmap +description: > + Non-writable properties are overwritten by CreateDataProperty. + (result object's "0" is non-writable, source array is not flattened) +info: | + Array.prototype.flatMap ( mapperFunction [ , thisArg ] ) + + [...] + 6. Perform ? FlattenIntoArray(A, O, sourceLen, 0, depthNum). + + FlattenIntoArray ( target, source, sourceLen, start, depth [ , mapperFunction, thisArg ] ) + + [...] + 9. Repeat, while sourceIndex < sourceLen + [...] + c. If exists is true, then + [...] + v. If shouldFlatten is true, then + [...] + vi. Else, + [...] + 2. Perform ? CreateDataPropertyOrThrow(target, ! ToString(targetIndex), element). + [...] +features: [Symbol.species] +includes: [propertyHelper.js] +---*/ + +var A = function(_length) { + Object.defineProperty(this, "0", { + value: 1, + writable: false, + enumerable: false, + configurable: true, + }); +}; + +var arr = [2]; +arr.constructor = {}; +arr.constructor[Symbol.species] = A; + +var res = arr.flatMap(function(item) { + return item; +}); + +verifyProperty(res, "0", { + value: 2, + writable: true, + enumerable: true, + configurable: true, +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/flatMap/this-value-ctor-non-object.js b/js/src/tests/test262/built-ins/Array/prototype/flatMap/this-value-ctor-non-object.js new file mode 100644 index 0000000000..896f1054ea --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/flatMap/this-value-ctor-non-object.js @@ -0,0 +1,84 @@ +// Copyright (C) 2018 Shilpi Jain and Michael Ficarra. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-array.prototype.flatMap +description: > + Assert behavior if this value has a custom non-object constructor property +info: | + Array.prototype.flatMap ( mapperFunction [ , thisArg ] ) + + 1. Let O be ? ToObject(this value). + 2. Let sourceLen be ? ToLength(? Get(O, "length")). + ... + 5. Let A be ? ArraySpeciesCreate(O, 0). + ... + + ArraySpeciesCreate ( originalArray, length ) + + 3. Let isArray be ? IsArray(originalArray). + 4. If isArray is false, return ? ArrayCreate(length). + 5. Let C be ? Get(originalArray, "constructor"). + 6. If IsConstructor(C) is true, then + a. Let thisRealm be the current Realm Record. + b. Let realmC be ? GetFunctionRealm(C). + c. If thisRealm and realmC are not the same Realm Record, then + i. If SameValue(C, realmC.[[Intrinsics]].[[%Array%]]) is true, set C to undefined. + 7. If Type(C) is Object, then + a. Set C to ? Get(C, @@species). + b. If C is null, set C to undefined. + 8. If C is undefined, return ? ArrayCreate(length). + 9. If IsConstructor(C) is false, throw a TypeError exception. +features: [Array.prototype.flatMap, Symbol] +includes: [compareArray.js] +---*/ + +assert.sameValue( + typeof Array.prototype.flatMap, + 'function', + 'The value of `typeof Array.prototype.flatMap` is expected to be "function"' +); + +var a = []; +var mapperFn = function() {}; + +a.constructor = null; +assert.throws(TypeError, function() { + a.flatMap(mapperFn); +}, 'a.flatMap(mapperFn) throws a TypeError exception'); + +a = []; +a.constructor = 1; +assert.throws(TypeError, function() { + a.flatMap(mapperFn); +}, 'a.flatMap(mapperFn) throws a TypeError exception'); + +a = []; +a.constructor = 'string'; +assert.throws(TypeError, function() { + a.flatMap(mapperFn); +}, 'a.flatMap(mapperFn) throws a TypeError exception'); + +a = []; +a.constructor = true; +assert.throws(TypeError, function() { + a.flatMap(mapperFn); +}, 'a.flatMap(mapperFn) throws a TypeError exception'); + +a = []; +a.constructor = Symbol(); +assert.throws(TypeError, function() { + a.flatMap(mapperFn); +}, 'a.flatMap(mapperFn) throws a TypeError exception'); + +a = []; +a.constructor = undefined; +var actual = a.flatMap(mapperFn); +assert.compareArray(actual, [], 'The value of actual is expected to be []'); +assert.sameValue( + Object.getPrototypeOf(actual), + Array.prototype, + 'Object.getPrototypeOf(a.flatMap(mapperFn)) returns Array.prototype' +); +assert.notSameValue(actual, a, 'The value of actual is expected to not equal the value of `a`'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/flatMap/this-value-ctor-object-species-bad-throws.js b/js/src/tests/test262/built-ins/Array/prototype/flatMap/this-value-ctor-object-species-bad-throws.js new file mode 100644 index 0000000000..e7ebabfce3 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/flatMap/this-value-ctor-object-species-bad-throws.js @@ -0,0 +1,139 @@ +// Copyright (C) 2018 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-array.prototype.flatMap +description: > + Assert behavior if this value has a custom object constructor property species +info: | + Array.prototype.flatMap ( mapperFunction [ , thisArg ] ) + + 1. Let O be ? ToObject(this value). + 2. Let sourceLen be ? ToLength(? Get(O, "length")). + ... + 5. Let A be ? ArraySpeciesCreate(O, 0). + ... + + ArraySpeciesCreate ( originalArray, length ) + + 3. Let isArray be ? IsArray(originalArray). + 4. If isArray is false, return ? ArrayCreate(length). + 5. Let C be ? Get(originalArray, "constructor"). + 6. If IsConstructor(C) is true, then + a. Let thisRealm be the current Realm Record. + b. Let realmC be ? GetFunctionRealm(C). + c. If thisRealm and realmC are not the same Realm Record, then + i. If SameValue(C, realmC.[[Intrinsics]].[[%Array%]]) is true, set C to undefined. + 7. If Type(C) is Object, then + a. Set C to ? Get(C, @@species). + b. If C is null, set C to undefined. + 8. If C is undefined, return ? ArrayCreate(length). + 9. If IsConstructor(C) is false, throw a TypeError exception. + 10. Return ? Construct(C, « length »). +features: [Array.prototype.flatMap, Symbol, Symbol.species] +includes: [compareArray.js] +---*/ + +assert.sameValue( + typeof Array.prototype.flatMap, + 'function', + 'The value of `typeof Array.prototype.flatMap` is expected to be "function"' +); + +var arr = [[42, 1], [42, 2]]; +var mapperFn = function(e) { return e; }; + +arr.constructor = {}; +var actual = arr.flatMap(mapperFn); +assert.compareArray(actual, [42, 1, 42, 2], 'The value of actual is expected to be [42, 1, 42, 2]'); +assert.sameValue( + Object.getPrototypeOf(actual), + Array.prototype, + 'Object.getPrototypeOf(arr.flatMap(mapperFn)) returns Array.prototype' +); + +var called = 0; +arr.constructor = { + get [Symbol.species]() { + called++; + return 0; + } +}; +assert.throws(TypeError, function() { + arr.flatMap(mapperFn); +}, 'arr.flatMap(mapperFn) throws a TypeError exception'); +assert.sameValue(called, 1, 'The value of called is expected to be 1'); + +called = 0; +arr.constructor = { + get [Symbol.species]() { + called++; + return ''; + } +}; +assert.throws(TypeError, function() { + arr.flatMap(mapperFn); +}, 'arr.flatMap(mapperFn) throws a TypeError exception'); +assert.sameValue(called, 1, 'The value of called is expected to be 1'); + +called = 0; +arr.constructor = { + get [Symbol.species]() { + called++; + return false; + } +}; +assert.throws(TypeError, function() { + arr.flatMap(mapperFn); +}, 'arr.flatMap(mapperFn) throws a TypeError exception'); +assert.sameValue(called, 1, 'The value of called is expected to be 1'); + +called = 0; +arr.constructor = { + get [Symbol.species]() { + called++; + return {}; + } +}; +assert.throws(TypeError, function() { + arr.flatMap(mapperFn); +}, 'arr.flatMap(mapperFn) throws a TypeError exception'); +assert.sameValue(called, 1, 'The value of called is expected to be 1'); + +called = 0; +arr.constructor = { + get [Symbol.species]() { + called++; + return []; + } +}; +assert.throws(TypeError, function() { + arr.flatMap(mapperFn); +}, 'arr.flatMap(mapperFn) throws a TypeError exception'); +assert.sameValue(called, 1, 'The value of called is expected to be 1'); + +called = 0; +arr.constructor = { + get [Symbol.species]() { + called++; + return Symbol(); + } +}; +assert.throws(TypeError, function() { + arr.flatMap(mapperFn); +}, 'arr.flatMap(mapperFn) throws a TypeError exception'); +assert.sameValue(called, 1, 'The value of called is expected to be 1'); + +called = 0; +arr.constructor = { + get [Symbol.species]() { + called++; + throw new Test262Error + } +}; +assert.throws(Test262Error, function() { + arr.flatMap(mapperFn); +}, 'arr.flatMap(mapperFn) throws a Test262Error exception'); +assert.sameValue(called, 1, 'The value of called is expected to be 1'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/flatMap/this-value-ctor-object-species-custom-ctor-poisoned-throws.js b/js/src/tests/test262/built-ins/Array/prototype/flatMap/this-value-ctor-object-species-custom-ctor-poisoned-throws.js new file mode 100644 index 0000000000..e3d6cd262e --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/flatMap/this-value-ctor-object-species-custom-ctor-poisoned-throws.js @@ -0,0 +1,60 @@ +// Copyright (C) 2018 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-array.prototype.flatMap +description: > + Assert behavior if this value has a poisoned custom species constructor +info: | + Array.prototype.flatMap ( mapperFunction [ , thisArg ] ) + + 1. Let O be ? ToObject(this value). + 2. Let sourceLen be ? ToLength(? Get(O, "length")). + ... + 5. Let A be ? ArraySpeciesCreate(O, 0). + 6. Perform ? FlattenIntoArray(A, O, sourceLen, 0, 1, mapperFunction, T). + 7. Return A. + + ArraySpeciesCreate ( originalArray, length ) + + 3. Let isArray be ? IsArray(originalArray). + 4. If isArray is false, return ? ArrayCreate(length). + 5. Let C be ? Get(originalArray, "constructor"). + 6. If IsConstructor(C) is true, then + ... + 7. If Type(C) is Object, then + a. Set C to ? Get(C, @@species). + b. If C is null, set C to undefined. + 8. If C is undefined, return ? ArrayCreate(length). + 9. If IsConstructor(C) is false, throw a TypeError exception. + 10. Return ? Construct(C, « length »). +features: [Array.prototype.flatMap, Symbol, Symbol.species] +---*/ + +assert.sameValue(typeof Array.prototype.flatMap, 'function'); + +var arr = []; +var mapperFn = function(e) { return e; }; + +var called = 0; +var ctorCalled = 0; +function ctor(len) { + assert.sameValue(new.target, ctor, 'new target is defined'); + assert.sameValue(len, 0, 'first argument is always 0'); + ctorCalled++; + throw new Test262Error(); +} + +arr.constructor = { + get [Symbol.species]() { + called++; + return ctor; + } +}; +assert.throws(Test262Error, function() { + arr.flatMap(mapperFn); +}, 'Return abrupt completion from species custom ctor'); +assert.sameValue(called, 1, 'got species once'); +assert.sameValue(ctorCalled, 1, 'called custom ctor once'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/flatMap/this-value-ctor-object-species-custom-ctor.js b/js/src/tests/test262/built-ins/Array/prototype/flatMap/this-value-ctor-object-species-custom-ctor.js new file mode 100644 index 0000000000..1424e57d24 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/flatMap/this-value-ctor-object-species-custom-ctor.js @@ -0,0 +1,96 @@ +// Copyright (C) 2018 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-array.prototype.flatMap +description: > + Assert behavior if this value has a custom species constructor +info: | + Array.prototype.flatMap ( mapperFunction [ , thisArg ] ) + + 1. Let O be ? ToObject(this value). + 2. Let sourceLen be ? ToLength(? Get(O, "length")). + ... + 5. Let A be ? ArraySpeciesCreate(O, 0). + 6. Perform ? FlattenIntoArray(A, O, sourceLen, 0, 1, mapperFunction, T). + 7. Return A. + + ArraySpeciesCreate ( originalArray, length ) + + 3. Let isArray be ? IsArray(originalArray). + 4. If isArray is false, return ? ArrayCreate(length). + 5. Let C be ? Get(originalArray, "constructor"). + 6. If IsConstructor(C) is true, then + ... + 7. If Type(C) is Object, then + a. Set C to ? Get(C, @@species). + b. If C is null, set C to undefined. + 8. If C is undefined, return ? ArrayCreate(length). + 9. If IsConstructor(C) is false, throw a TypeError exception. + 10. Return ? Construct(C, « length »). + + FlattenIntoArray(target, source, sourceLen, start, depth [ , mapperFunction, thisArg ]) + + 3. Repeat, while sourceIndex < sourceLen + a. Let P be ! ToString(sourceIndex). + b. Let exists be ? HasProperty(source, P). + c. If exists is true, then + ... + vi. Else, + ... + 2. Perform ? CreateDataPropertyOrThrow(target, ! ToString(targetIndex), element). +features: [Array.prototype.flatMap, Symbol, Symbol.species] +includes: [propertyHelper.js] +---*/ + +assert.sameValue(typeof Array.prototype.flatMap, 'function'); + +var arr = [[42, 1], [42, 2]]; +var mapperFn = function(e) { return e; }; + +var called = 0; +var ctorCalled = 0; +function ctor(len) { + assert.sameValue(new.target, ctor, 'new target is defined'); + assert.sameValue(len, 0, 'first argument is always 0'); + ctorCalled++; +} + +arr.constructor = { + get [Symbol.species]() { + called++; + return ctor; + } +}; +var actual = arr.flatMap(mapperFn); +assert(actual instanceof ctor, 'returned value is an instance of custom ctor'); +assert.sameValue(called, 1, 'got species once'); +assert.sameValue(ctorCalled, 1, 'called custom ctor once'); + +assert.sameValue(Object.prototype.hasOwnProperty.call(actual, 'length'), false, 'it does not define an own length property'); +verifyProperty(actual, '0', { + configurable: true, + writable: true, + enumerable: true, + value: 42 +}); +verifyProperty(actual, '1', { + configurable: true, + writable: true, + enumerable: true, + value: 1 +}); +verifyProperty(actual, '2', { + configurable: true, + writable: true, + enumerable: true, + value: 42 +}); +verifyProperty(actual, '3', { + configurable: true, + writable: true, + enumerable: true, + value: 2 +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/flatMap/this-value-ctor-object-species.js b/js/src/tests/test262/built-ins/Array/prototype/flatMap/this-value-ctor-object-species.js new file mode 100644 index 0000000000..25106bdbe2 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/flatMap/this-value-ctor-object-species.js @@ -0,0 +1,159 @@ +// Copyright (C) 2018 Leo Balter. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-array.prototype.flatMap +description: > + Assert behavior if this value has a custom object constructor property +info: | + Array.prototype.flatMap ( mapperFunction [ , thisArg ] ) + + 1. Let O be ? ToObject(this value). + 2. Let sourceLen be ? ToLength(? Get(O, "length")). + ... + 5. Let A be ? ArraySpeciesCreate(O, 0). + ... + + ArraySpeciesCreate ( originalArray, length ) + + 3. Let isArray be ? IsArray(originalArray). + 4. If isArray is false, return ? ArrayCreate(length). + 5. Let C be ? Get(originalArray, "constructor"). + 6. If IsConstructor(C) is true, then + a. Let thisRealm be the current Realm Record. + b. Let realmC be ? GetFunctionRealm(C). + c. If thisRealm and realmC are not the same Realm Record, then + i. If SameValue(C, realmC.[[Intrinsics]].[[%Array%]]) is true, set C to undefined. + 7. If Type(C) is Object, then + a. Set C to ? Get(C, @@species). + b. If C is null, set C to undefined. + 8. If C is undefined, return ? ArrayCreate(length). + 9. If IsConstructor(C) is false, throw a TypeError exception. + 10. Return ? Construct(C, « length »). +features: [Array.prototype.flatMap, Symbol, Symbol.species] +includes: [compareArray.js] +---*/ + +assert.sameValue( + typeof Array.prototype.flatMap, + 'function', + 'The value of `typeof Array.prototype.flatMap` is expected to be "function"' +); + +var arr = [[42, 1], [42, 2]]; +var mapperFn = function(e) { return e; }; + +arr.constructor = {}; +var actual = arr.flatMap(mapperFn); +assert.compareArray(actual, [42, 1, 42, 2], 'The value of actual is expected to be [42, 1, 42, 2]'); +assert.sameValue( + Object.getPrototypeOf(actual), + Array.prototype, + 'Object.getPrototypeOf(arr.flatMap(mapperFn)) returns Array.prototype' +); + +var called = 0; +arr.constructor = { + get [Symbol.species]() { + called++; + return null; + } +}; +actual = arr.flatMap(mapperFn); +assert.compareArray(actual, [42, 1, 42, 2], 'The value of actual is expected to be [42, 1, 42, 2]'); +assert.sameValue( + Object.getPrototypeOf(actual), + Array.prototype, + 'Object.getPrototypeOf(arr.flatMap(mapperFn)) returns Array.prototype' +); +assert.sameValue(called, 1, 'The value of called is expected to be 1'); + +called = 0; +arr.constructor = { + get [Symbol.species]() { + called++; + return undefined; + } +}; +actual = arr.flatMap(mapperFn); +assert.compareArray(actual, [42, 1, 42, 2], 'The value of actual is expected to be [42, 1, 42, 2]'); +assert.sameValue( + Object.getPrototypeOf(actual), + Array.prototype, + 'Object.getPrototypeOf(arr.flatMap(mapperFn)) returns Array.prototype' +); +assert.sameValue(called, 1, 'The value of called is expected to be 1'); + +called = 0; +arr.constructor = { + get [Symbol.species]() { + called++; + return 0; + } +}; +assert.throws(TypeError, function() { + arr.flatMap(mapperFn); +}, 'arr.flatMap(mapperFn) throws a TypeError exception'); +assert.sameValue(called, 1, 'The value of called is expected to be 1'); + +called = 0; +arr.constructor = { + get [Symbol.species]() { + called++; + return ''; + } +}; +assert.throws(TypeError, function() { + arr.flatMap(mapperFn); +}, 'arr.flatMap(mapperFn) throws a TypeError exception'); +assert.sameValue(called, 1, 'The value of called is expected to be 1'); + +called = 0; +arr.constructor = { + get [Symbol.species]() { + called++; + return false; + } +}; +assert.throws(TypeError, function() { + arr.flatMap(mapperFn); +}, 'arr.flatMap(mapperFn) throws a TypeError exception'); +assert.sameValue(called, 1, 'The value of called is expected to be 1'); + +called = 0; +arr.constructor = { + get [Symbol.species]() { + called++; + return {}; + } +}; +assert.throws(TypeError, function() { + arr.flatMap(mapperFn); +}, 'arr.flatMap(mapperFn) throws a TypeError exception'); +assert.sameValue(called, 1, 'The value of called is expected to be 1'); + +called = 0; +arr.constructor = { + get [Symbol.species]() { + called++; + return []; + } +}; +assert.throws(TypeError, function() { + arr.flatMap(mapperFn); +}, 'arr.flatMap(mapperFn) throws a TypeError exception'); +assert.sameValue(called, 1, 'The value of called is expected to be 1'); + +called = 0; +arr.constructor = { + get [Symbol.species]() { + called++; + return Symbol(); + } +}; +assert.throws(TypeError, function() { + arr.flatMap(mapperFn); +}, 'arr.flatMap(mapperFn) throws a TypeError exception'); +assert.sameValue(called, 1, 'The value of called is expected to be 1'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/flatMap/this-value-null-undefined-throws.js b/js/src/tests/test262/built-ins/Array/prototype/flatMap/this-value-null-undefined-throws.js new file mode 100644 index 0000000000..f2b52aeb34 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/flatMap/this-value-null-undefined-throws.js @@ -0,0 +1,27 @@ +// Copyright (C) 2018 Shilpi Jain and Michael Ficarra. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-array.prototype.flatMap +description: > + Throw a TypeError if this value is null or undefined +info: | + Array.prototype.flatMap ( mapperFunction [ , thisArg ] ) + + 1. Let O be ? ToObject(this value). + ... +features: [Array.prototype.flatMap] +---*/ + +assert.sameValue(typeof Array.prototype.flatMap, 'function'); + +var mapperFn = function() {}; + +assert.throws(TypeError, function() { + [].flatMap.call(null, mapperFn); +}, 'null value'); + +assert.throws(TypeError, function() { + [].flatMap.call(undefined, mapperFn); +}, 'undefined'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/flatMap/thisArg-argument-strict.js b/js/src/tests/test262/built-ins/Array/prototype/flatMap/thisArg-argument-strict.js new file mode 100644 index 0000000000..b2b1ad9347 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/flatMap/thisArg-argument-strict.js @@ -0,0 +1,48 @@ +'use strict'; +// Copyright (C) 2018 Shilpi Jain and Michael Ficarra. 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 ] ) +flags: [onlyStrict] +includes: [compareArray.js] +features: [Array.prototype.flatMap] +---*/ + +var a = {}; +var actual; + +actual = [1].flatMap(function() { + return [this]; +}, "TestString"); +assert.compareArray(actual, ["TestString"], 'The value of actual is expected to be ["TestString"]'); + +actual = [1].flatMap(function() { + return [this]; +}, 1); +assert.compareArray(actual, [1], 'The value of actual is expected to be [1]'); + +actual = [1].flatMap(function() { + return [this]; +}, null); +assert.compareArray(actual, [null], 'The value of actual is expected to be [null]'); + +actual = [1].flatMap(function() { + return [this]; +}, true); +assert.compareArray(actual, [true], 'The value of actual is expected to be [true]'); + +actual = [1].flatMap(function() { + return [this]; +}, a); +assert.compareArray(actual, [a], 'The value of actual is expected to be [a]'); + +actual = [1].flatMap(function() { + return [this]; +}, void 0); +assert.compareArray(actual, [undefined], 'The value of actual is expected to be [undefined]'); + + +reportCompare(0, 0); |