add tests for array.prototype.groupByToMap

This commit is contained in:
polsevev 2021-12-15 11:42:58 +01:00 committed by Philip Chimento
parent e7af14502e
commit ce511219e8
9 changed files with 239 additions and 0 deletions

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.groupByToMap
description: Array.prototype.groupByToMap populates Map with correct keys and values
info: |
22.1.3.15 Array.prototype.groupByToMap ( callbackfn [ , thisArg ] )
...
6. Repeat, while k < len
a. Let Pk be ! ToString(𝔽(k)).
b. Let kValue be ? Get(O, Pk).
c. Let key be ? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »).
d. If key is -0𝔽, set key to +0𝔽.
e. Perform ! AddValueToKeyedGroup(groups, key, kValue).
f. Set k to k + 1.
...
---*/
const arr = [-0,0,1,2,3];
const sentinel = {};
const map = arr.groupByToMap.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,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.groupByToMap
description: Array.prototype.groupByToMap populates Map with correct keys and values
info: |
22.1.3.15 Array.prototype.groupByToMap ( callbackfn [ , thisArg ] )
...
8. For each Record { [[Key]], [[Elements]] } g of groups, do
a. Let elements be ! CreateArrayFromList(g.[[Elements]]).
b. Let entry be the Record { [[Key]]: g.[[Key]], [[Value]]: elements }.
c. Append entry as the last element of map.[[MapData]].
...
---*/
const map = [].groupByToMap(i=>i);
assert.sameValue(map.size, 0);
assert.sameValue(typeof map, typeof new Map());

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.groupByToMap
description: Array.prototype.groupByToMap populates Map with correct keys and values
info: |
22.1.3.15 Array.prototype.groupByToMap ( callbackfn [ , thisArg ] )
...
8. For each Record { [[Key]], [[Elements]] } g of groups, do
a. Let elements be ! CreateArrayFromList(g.[[Elements]]).
b. Let entry be the Record { [[Key]]: g.[[Key]], [[Value]]: elements }.
c. Append entry as the last element of map.[[MapData]].
...
---*/
const array = [1,2,3];
const map = array.groupByToMap(i=>{return i%2===0?'even':'odd';});
assert.sameValue(map.get('even')[0], 2);
assert.sameValue(map.get('odd')[0], 1);
assert.sameValue(map.get('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.groupByToMap
description: Array.prototype.groupByToMap populates Map with correct keys and values
info: |
22.1.3.15 Array.prototype.groupByToMap ( callbackfn [ , thisArg ] )
...
8. For each Record { [[Key]], [[Elements]] } g of groups, do
a. Let elements be ! CreateArrayFromList(g.[[Elements]]).
b. Let entry be the Record { [[Key]]: g.[[Key]], [[Value]]: elements }.
c. Append entry as the last element of map.[[MapData]].
...
---*/
const arr = ['test', 'hello', 'world'];
const map = arr.groupByToMap(i=>i.length);
assert.sameValue(map.get(4)[0], 'test');
assert.sameValue(map.get(5)[0], 'hello');
assert.sameValue(map.get(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.groupByToMap
description: Array.prototype.groupByToMap property length descriptor
info: |
22.1.3.15 Array.prototype.groupByToMap ( callbackfn [ , thisArg ] )
...
17 ECMAScript Standard Built-in Objects
...
includes: [propertyHelper.js]
---*/
assert.sameValue(
Array.prototype.groupByToMap.length, 1,
'The value of `Array.prototype.groupByMap.length` is `1`'
);
verifyProperty(Array.prototype.groupByToMap, "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.groupByToMap
description: Array.prototype.groupByToMap property name descriptor
info: |
22.1.3.15 Array.prototype.groupByToMap ( callbackfn [ , thisArg ] )
...
17 ECMAScript Standard Built-in Objects
...
includes: [propertyHelper.js]
---*/
assert.sameValue(
Array.prototype.groupByToMap.name, 'groupByToMap',
'The value of `Array.prototype.groupByMap.name` is `"groupByMap"`'
);
verifyProperty(Array.prototype.groupByToMap, "name", {
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.groupByToMap
description: Array.prototype.groupByToMap normalize 0 for Map key
info: |
22.1.3.15 Array.prototype.groupByToMap ( callbackfn [ , thisArg ] )
...
6.d. If key is -0𝔽, set key to +0𝔽.
...
---*/
const arr = [-0, +0];
const map = arr.groupByToMap(i=>i);
assert.sameValue(map.size, 1);
assert.sameValue(map.has(0), true);
assert.sameValue(map.get(0).length, 2);

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.groupByToMap
description: Array.prototype.groupByToMap applied to null throws a TypeError
info: |
22.1.3.15 Array.prototype.groupByToMap ( callbackfn [ , thisArg ] )
...
3. If IsCallable(callbackfn) is false, throw a TypeError exception.
...
---*/
assert.throws(TypeError, function() {
Array.prototype.groupByToMap.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.groupByToMap
description: Array.prototype.groupByToMap applied to undefined throws a TypeError
info: |
22.1.3.15 Array.prototype.groupByToMap ( callbackfn [ , thisArg ] )
...
3. If IsCallable(callbackfn) is false, throw a TypeError exception.
...
---*/
assert.throws(TypeError, function() {
Array.prototype.groupByToMap.call(undefined, undefined);
});