Update group tests

This commit is contained in:
Justin Ridgewell 2022-09-01 16:48:24 -04:00 committed by Philip Chimento
parent 5bd6844df5
commit 22fe1b44b2
21 changed files with 460 additions and 190 deletions

View File

@ -0,0 +1,32 @@
// Copyright (c) 2021 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.group
description: Array.prototype.group iterates array-like up to length
info: |
22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] )
...
2. Let len be ? LengthOfArrayLike(O).
...
4. Let k be 0.
...
6. Repeat, while k < len
...
includes: [compareArray.js]
features: [array-grouping]
---*/
const arrayLike = {0: 1, 1: 2, 2: 3, 3: 4, length: 3 };
let calls = 0;
const obj = Array.prototype.group.call(arrayLike, i => { calls++; return i % 2 === 0 ? 'even' : 'odd'; });
assert.sameValue(calls, 3, 'only calls length times');
assert.compareArray(Object.keys(obj), ['odd', 'even']);
assert.compareArray(obj['even'], [2]);
assert.compareArray(obj['odd'], [1, 3]);

View File

@ -0,0 +1,35 @@
// Copyright (c) 2021 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.group
description: Array.prototype.group calls function with correct arguments
info: |
22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] )
...
6. Repeat, while k < len
a. Let Pk be ! ToString(𝔽(k)).
b. Let kValue be ? Get(O, Pk).
c. Let propertyKey be ? ToPropertyKey(? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »)).
d. Perform ! AddValueToKeyedGroup(groups, propertyKey, kValue).
e. Set k to k + 1.
...
features: [array-grouping]
---*/
const arr = [-0, 0, 1, 2, 3];
let calls = 0;
const map = arr.group((n, i, testArr) => {
calls++;
assert.sameValue(n, arr[i], "selected element aligns with index");
assert.sameValue(testArr, arr, "original array is passed as final argument");
return null;
});
assert.sameValue(calls, 5, 'called for all 5 elements');

View File

@ -2,10 +2,10 @@
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.groupBy
description: Array.prototype.groupBy populates Map with correct keys and values
esid: sec-array.prototype.group
description: Callback is not called and object is not populated if the array is empty
info: |
22.1.3.14 Array.prototype.groupBy ( callbackfn [ , thisArg ] )
22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] )
...
@ -15,9 +15,14 @@ info: |
b. Perform ! CreateDataPropertyOrThrow(obj, g.[[Key]], elements).
...
features: [array-grouping]
---*/
const obj = [].groupBy(i=>i);
const original = [];
const obj = original.group(() => {
throw new Test262Error('callback function should not be called')
});
assert.notSameValue(original, obj, 'group returns a object');
assert.sameValue(Object.keys(obj).length, 0);

View File

@ -0,0 +1,30 @@
// Copyright (c) 2021 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.group
description: Array.prototype.group populates object with correct keys and values
info: |
22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] )
...
8. For each Record { [[Key]], [[Elements]] } g of groups, do
a. Let elements be ! CreateArrayFromList(g.[[Elements]]).
b. Perform ! CreateDataPropertyOrThrow(obj, g.[[Key]], elements).
...
includes: [compareArray.js]
features: [array-grouping]
---*/
const array = [1, 2, 3];
const obj = array.group(i => {
return i % 2 === 0 ? 'even' : 'odd';
});
assert.compareArray(Object.keys(obj), ['odd', 'even']);
assert.compareArray(obj['even'], [2]);
assert.compareArray(obj['odd'], [1, 3]);

View File

@ -0,0 +1,28 @@
// Copyright (c) 2021 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.group
description: Callback can return numbers that are converted to property keys
info: |
22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] )
...
8. For each Record { [[Key]], [[Elements]] } g of groups, do
a. Let elements be ! CreateArrayFromList(g.[[Elements]]).
b. Perform ! CreateDataPropertyOrThrow(obj, g.[[Key]], elements).
...
includes: [compareArray.js]
features: [array-grouping]
---*/
const arr = ['hello', 'test', 'world'];
const obj = arr.group(i => i.length);
assert.compareArray(Object.keys(obj), ['4', '5']);
assert.compareArray(obj['5'], ['hello', 'world']);
assert.compareArray(obj['4'], ['test']);

View File

@ -0,0 +1,29 @@
// Copyright (c) 2021 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.group
description: Array.prototype.group called with non-callable throws TypeError
info: |
22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] )
...
3. If IsCallable(callbackfn) is false, throw a TypeError exception.
...
features: [array-grouping]
---*/
assert.throws(TypeError, function() {
[].group(null)
}, "null callback throws TypeError");
assert.throws(TypeError, function() {
[].group(undefined)
}, "undefined callback throws TypeError");
assert.throws(TypeError, function() {
[].group({})
}, "object callback throws TypeError");

View File

@ -0,0 +1,28 @@
// Copyright (c) 2021 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.group
description: Array.prototype.group applied to null/undefined throws a TypeError
info: |
22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] )
...
1. Let O be ? ToObject(this value).
...
features: [array-grouping]
---*/
const throws = () => {
throw new Test262Error('callback function should not be called')
};
assert.throws(TypeError, function() {
Array.prototype.group.call(undefined, throws);
}, 'undefined this value');
assert.throws(TypeError, function() {
Array.prototype.group.call(undefined, throws);
}, 'null this value');

View File

@ -0,0 +1,26 @@
// Copyright (c) 2021 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.group
description: Array.prototype.group property length descriptor
info: |
22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] )
...
17 ECMAScript Standard Built-in Objects
...
includes: [propertyHelper.js]
features: [array-grouping]
---*/
verifyProperty(Array.prototype.group, "length", {
value: 1,
enumerable: false,
writable: false,
configurable: true
});

View File

@ -0,0 +1,25 @@
// Copyright (c) 2021 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.group
description: Array.prototype.group property name descriptor
info: |
22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] )
...
17 ECMAScript Standard Built-in Objects
...
includes: [propertyHelper.js]
features: [array-grouping]
---*/
verifyProperty(Array.prototype.group, "name", {
value: "group",
enumerable: false,
writable: false,
configurable: true
});

View File

@ -0,0 +1,27 @@
// Copyright (c) 2021 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.group
description: Array.prototype.group returns a null prototype object
info: |
22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] )
...
7. Let obj be OrdinaryObjectCreate(null).
...
9. Return obj.
...
features: [array-grouping]
---*/
const array = [1, 2, 3];
const obj = array.group(i => {
return i % 2 === 0 ? 'even' : 'odd';
});
assert.sameValue(Object.getPrototypeOf(obj), null);
assert.sameValue(obj.hasOwnProperty, undefined);

View File

@ -0,0 +1,36 @@
// Copyright (c) 2021 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.group
description: Array.prototype.group adds undefined to the group for sparse arrays
info: |
22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] )
...
6. Repeat, while k < len
a. Let Pk be ! ToString(𝔽(k)).
b. Let kValue be ? Get(O, Pk).
c. Let propertyKey be ? ToPropertyKey(? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »)).
d. Perform AddValueToKeyedGroup(groups, propertyKey, kValue).
...
includes: [compareArray.js]
features: [array-grouping]
---*/
let calls = 0;
const array = [, , ,];
const obj = array.group(() => {
calls++;
return 'key';
});
assert.sameValue(calls, 3);
assert(0 in obj['key'], 'group has a first element');
assert(1 in obj['key'], 'group has a second element');
assert(2 in obj['key'], 'group has a third element');
assert.compareArray(obj['key'], [void 0, void 0, void 0]);

View File

@ -0,0 +1,57 @@
// Copyright (c) 2021 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.group
description: Array.prototype.group calls with thisArg
info: |
22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] )
...
6. Repeat, while k < len
c. Let propertyKey be ? ToPropertyKey(? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »)).
...
flags: [onlyStrict]
features: [array-grouping]
---*/
let sentinel = {};
let result;
[1].group(function() {
result = this;
}, "TestString");
assert.sameValue(result, "TestString");
[1].group(function() {
result = this;
}, 1);
assert.sameValue(result, 1);
[1].group(function() {
result = this;
}, true);
assert.sameValue(result, true);
[1].group(function() {
result = this;
}, null);
assert.sameValue(result, null);
[1].group(function() {
result = this;
}, sentinel);
assert.sameValue(result, sentinel);
[1].group(function() {
result = this;
}, void 0);
assert.sameValue(result, void 0);
[1].group(function() {
result = this;
});
assert.sameValue(result, void 0);

View File

@ -0,0 +1,60 @@
// Copyright (c) 2021 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.group
description: Array.prototype.group calls with thisArg
info: |
22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] )
...
6. Repeat, while k < len
c. Let propertyKey be ? ToPropertyKey(? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »)).
...
flags: [noStrict]
features: [array-grouping]
---*/
let sentinel = {};
let result;
[1].group(function() {
result = this;
}, "TestString");
assert.sameValue(result instanceof String, true);
assert.sameValue(result.valueOf(), "TestString");
[1].group(function() {
result = this;
}, 1);
assert.sameValue(result instanceof Number, true);
assert.sameValue(result.valueOf(), 1);
[1].group(function() {
result = this;
}, true);
assert.sameValue(result instanceof Boolean, true);
assert.sameValue(result.valueOf(), true);
[1].group(function() {
result = this;
}, null);
assert.sameValue(result, globalThis);
[1].group(function() {
result = this;
}, sentinel);
assert.sameValue(result, sentinel);
[1].group(function() {
result = this;
}, void 0);
assert.sameValue(result, globalThis);
[1].group(function() {
result = this;
});
assert.sameValue(result, globalThis);

View File

@ -0,0 +1,37 @@
// Copyright (c) 2021 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.group
description: Array.prototype.group coerces return value with ToPropertyKey
info: |
22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] )
...
6. Repeat, while k < len
c. Let propertyKey be ? ToPropertyKey(? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »)).
d. Perform AddValueToKeyedGroup(groups, propertyKey, kValue).
...
8. For each Record { [[Key]], [[Elements]] } g of groups, do
a. Let elements be ! CreateArrayFromList(g.[[Elements]]).
b. Perform ! CreateDataPropertyOrThrow(obj, g.[[Key]], elements).
...
includes: [compareArray.js]
features: [array-grouping]
---*/
let calls = 0;
const stringable = {
toString() {
return 1;
}
}
const array = [1, '1', stringable];
const obj = array.group(v => v);
assert.compareArray(Object.keys(obj), ['1']);
assert.compareArray(obj['1'], [1, '1', stringable]);

View File

@ -1,33 +0,0 @@
// Copyright (c) 2021 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.groupBy
description: Array.prototype.groupBy populates Map with correct keys and values
info: |
22.1.3.14 Array.prototype.groupBy ( callbackfn [ , thisArg ] )
...
6. Repeat, while k < len
a. Let Pk be ! ToString(𝔽(k)).
b. Let kValue be ? Get(O, Pk).
c. Let propertyKey be ? ToPropertyKey(? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »)).
d. Perform ! AddValueToKeyedGroup(groups, propertyKey, kValue).
e. Set k to k + 1.
...
---*/
const arr = [-0,0,1,2,3];
const sentinel = {};
const map = arr.groupBy.call(sentinel, (n,i,testArr) => {
assert.sameValue(this, sentinel, "this value is correctly bound");
assert.sameValue(n, arr[i], "selected element aligns with index");
assert.sameValue(testArr, arr, "original array is passed as final argument");
return null;
});

View File

@ -1,26 +0,0 @@
// Copyright (c) 2021 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.groupBy
description: Array.prototype.groupBy populates Map with correct keys and values
info: |
22.1.3.14 Array.prototype.groupBy ( callbackfn [ , thisArg ] )
...
8. For each Record { [[Key]], [[Elements]] } g of groups, do
a. Let elements be ! CreateArrayFromList(g.[[Elements]]).
b. Perform ! CreateDataPropertyOrThrow(obj, g.[[Key]], elements).
...
---*/
const array = [1,2,3];
const obj = array.groupBy(i=>{return i%2===0?'even':'odd';});
assert.sameValue(obj['even'][0], 2);
assert.sameValue(obj['odd'][0], 1);
assert.sameValue(obj['odd'][1], 3);

View File

@ -1,27 +0,0 @@
// Copyright (c) 2021 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.groupBy
description: Array.prototype.groupBy populates Map with correct keys and values
info: |
22.1.3.14 Array.prototype.groupBy ( callbackfn [ , thisArg ] )
...
8. For each Record { [[Key]], [[Elements]] } g of groups, do
a. Let elements be ! CreateArrayFromList(g.[[Elements]]).
b. Perform ! CreateDataPropertyOrThrow(obj, g.[[Key]], elements).
...
---*/
const arr = ['test', 'hello', 'world'];
const obj = arr.groupBy(i=>i.length);
assert.sameValue(obj["4"][0], 'test');
assert.sameValue(obj["5"][0], 'hello');
assert.sameValue(obj["5"][1], 'world')

View File

@ -1,29 +0,0 @@
// Copyright (c) 2021 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.groupBy
description: Array.prototype.groupBy property length descriptor
info: |
22.1.3.14 Array.prototype.groupBy ( callbackfn [ , thisArg ] )
...
17 ECMAScript Standard Built-in Objects
...
includes: [propertyHelper.js]
---*/
assert.sameValue(
Array.prototype.groupBy.length, 1,
'The value of `Array.prototype.groupBy.length` is `1`'
);
verifyProperty(Array.prototype.groupBy, "length", {
enumerable: false,
writable: false,
configurable: true
});

View File

@ -1,28 +0,0 @@
// Copyright (c) 2021 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.groupBy
description: Array.prototype.groupBy property name descriptor
info: |
22.1.3.14 Array.prototype.groupBy ( callbackfn [ , thisArg ] )
...
17 ECMAScript Standard Built-in Objects
...
includes: [propertyHelper.js]
---*/
assert.sameValue(
Array.prototype.groupBy.name, 'groupBy',
'The value of `Array.prototype.groupBy.name` is `"groupBy"`'
);
verifyProperty(Array.prototype.groupBy, "name", {
enumerable: false,
writable: false,
configurable: true
});

View File

@ -1,21 +0,0 @@
// Copyright (c) 2021 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.groupBy
description: Array.prototype.groupBy applied to null throws a TypeError
info: |
22.1.3.14 Array.prototype.groupBy ( callbackfn [ , thisArg ] )
...
3. If IsCallable(callbackfn) is false, throw a TypeError exception.
...
---*/
assert.throws(TypeError, function() {
Array.prototype.groupBy.call(null,null);
});

View File

@ -1,21 +0,0 @@
// Copyright (c) 2021 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.groupBy
description: Array.prototype.groupBy applied to undefined throws a TypeError
info: |
22.1.3.14 Array.prototype.groupBy ( callbackfn [ , thisArg ] )
...
3. If IsCallable(callbackfn) is false, throw a TypeError exception.
...
---*/
assert.throws(TypeError, function() {
Array.prototype.groupBy.call(undefined);
});