mirror of
https://github.com/tc39/test262.git
synced 2025-04-08 19:35:28 +02:00
add tests for array.prototype.groupByToMap
This commit is contained in:
parent
e7af14502e
commit
ce511219e8
35
test/built-ins/Array/prototype/groupByToMap/callback-arg.js
vendored
Normal file
35
test/built-ins/Array/prototype/groupByToMap/callback-arg.js
vendored
Normal 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;
|
||||
});
|
25
test/built-ins/Array/prototype/groupByToMap/emptyList.js
vendored
Normal file
25
test/built-ins/Array/prototype/groupByToMap/emptyList.js
vendored
Normal 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());
|
27
test/built-ins/Array/prototype/groupByToMap/evenOdd.js
vendored
Normal file
27
test/built-ins/Array/prototype/groupByToMap/evenOdd.js
vendored
Normal 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);
|
28
test/built-ins/Array/prototype/groupByToMap/groupByLength.js
vendored
Normal file
28
test/built-ins/Array/prototype/groupByToMap/groupByLength.js
vendored
Normal 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')
|
29
test/built-ins/Array/prototype/groupByToMap/length.js
vendored
Normal file
29
test/built-ins/Array/prototype/groupByToMap/length.js
vendored
Normal 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
|
||||
});
|
28
test/built-ins/Array/prototype/groupByToMap/name.js
vendored
Normal file
28
test/built-ins/Array/prototype/groupByToMap/name.js
vendored
Normal 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
|
||||
});
|
25
test/built-ins/Array/prototype/groupByToMap/negativeZero.js
vendored
Normal file
25
test/built-ins/Array/prototype/groupByToMap/negativeZero.js
vendored
Normal 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);
|
21
test/built-ins/Array/prototype/groupByToMap/null.js
vendored
Normal file
21
test/built-ins/Array/prototype/groupByToMap/null.js
vendored
Normal 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);
|
||||
});
|
21
test/built-ins/Array/prototype/groupByToMap/undefined.js
vendored
Normal file
21
test/built-ins/Array/prototype/groupByToMap/undefined.js
vendored
Normal 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);
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user