Skip to content

Commit

Permalink
Update groupToMap tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell authored and ptomato committed Sep 14, 2022
1 parent ce51121 commit 9592077
Show file tree
Hide file tree
Showing 28 changed files with 628 additions and 239 deletions.
35 changes: 0 additions & 35 deletions test/built-ins/Array/prototype/groupByToMap/callback-arg.js

This file was deleted.

25 changes: 0 additions & 25 deletions test/built-ins/Array/prototype/groupByToMap/emptyList.js

This file was deleted.

27 changes: 0 additions & 27 deletions test/built-ins/Array/prototype/groupByToMap/evenOdd.js

This file was deleted.

28 changes: 0 additions & 28 deletions test/built-ins/Array/prototype/groupByToMap/groupByLength.js

This file was deleted.

29 changes: 0 additions & 29 deletions test/built-ins/Array/prototype/groupByToMap/length.js

This file was deleted.

28 changes: 0 additions & 28 deletions test/built-ins/Array/prototype/groupByToMap/name.js

This file was deleted.

25 changes: 0 additions & 25 deletions test/built-ins/Array/prototype/groupByToMap/negativeZero.js

This file was deleted.

21 changes: 0 additions & 21 deletions test/built-ins/Array/prototype/groupByToMap/null.js

This file was deleted.

21 changes: 0 additions & 21 deletions test/built-ins/Array/prototype/groupByToMap/undefined.js

This file was deleted.

35 changes: 35 additions & 0 deletions test/built-ins/Array/prototype/groupToMap/array-like.js
Original file line number Diff line number Diff line change
@@ -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.groupToMap
description: Array.prototype.groupToMap iterates array-like up to length
info: |
22.1.3.15 Array.prototype.groupToMap ( 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 map = Array.prototype.groupToMap.call(arrayLike, i => {
calls++;
return i % 2 === 0 ? 'even' : 'odd';
});

assert.sameValue(calls, 3, 'only calls length times');
assert.compareArray(Array.from(map.keys()), ['odd', 'even']);
assert.compareArray(map.get('even'), [2]);
assert.compareArray(map.get('odd'), [1, 3]);
33 changes: 33 additions & 0 deletions test/built-ins/Array/prototype/groupToMap/callback-arg.js
Original file line number Diff line number Diff line change
@@ -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.groupToMap
description: Array.prototype.groupToMap calls function with correct arguments
info: |
22.1.3.15 Array.prototype.groupToMap ( 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 »).
e. Perform ! AddValueToKeyedGroup(groups, key, kValue).
...
features: [array-grouping]
---*/


const arr = [-0, 0, 1, 2, 3];

let calls = 0;

arr.groupToMap((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');
24 changes: 24 additions & 0 deletions test/built-ins/Array/prototype/groupToMap/callback-throws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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.groupToMap
description: Array.prototype.groupToMap errors when return value cannot be converted to a property key.
info: |
22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] )
...
6. Repeat, while k < len
c. Let propertyKey be ? ToPropertyKey(? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »)).
...
features: [array-grouping]
---*/

assert.throws(Test262Error, function() {
const array = [1];
array.groupToMap(function() {
throw new Test262Error('throw in callback');
})
});
28 changes: 28 additions & 0 deletions test/built-ins/Array/prototype/groupToMap/emptyList.js
Original file line number Diff line number Diff line change
@@ -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.groupToMap
description: Callback is not called and object is not populated if the array is empty
info: |
22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] )
...
8. For each Record { [[Key]], [[Elements]] } g of groups, do
a. Let elements be ! CreateArrayFromList(g.[[Elements]]).
b. Perform ! CreateDataPropertyOrThrow(map, g.[[Key]], elements).
...
features: [array-grouping]
---*/

const original = [];

const map = original.groupToMap(() => {
throw new Test262Error('callback function should not be called')
});

assert.notSameValue(original, map, 'groupToMap returns a map');
assert.sameValue(map.size, 0);

0 comments on commit 9592077

Please sign in to comment.