added tests for groupBy

This commit is contained in:
polsevev 2021-12-15 12:18:33 +01:00 committed by Philip Chimento
parent 0bc0ca189d
commit 5bd6844df5
8 changed files with 208 additions and 0 deletions

View File

@ -0,0 +1,33 @@
// 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

@ -0,0 +1,23 @@
// 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 obj = [].groupBy(i=>i);
assert.sameValue(Object.keys(obj).length, 0);

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.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

@ -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.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

@ -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.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

@ -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.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

@ -0,0 +1,21 @@
// 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

@ -0,0 +1,21 @@
// 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);
});