Update groupToMap tests

This commit is contained in:
Justin Ridgewell 2022-09-01 21:38:20 -04:00 committed by Philip Chimento
parent ce511219e8
commit 9592077530
28 changed files with 628 additions and 239 deletions

View File

@ -1,35 +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.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

@ -1,25 +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.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

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

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

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

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

@ -1,25 +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.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

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

@ -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.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);
});

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.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]);

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.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');

View File

@ -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');
})
});

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

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.groupToMap
description: Array.prototype.groupToMap populates object with correct keys and values
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).
...
includes: [compareArray.js]
features: [array-grouping]
---*/
const array = [1, 2, 3];
const map = array.groupToMap(i => {
return i % 2 === 0 ? 'even' : 'odd';
});
assert.compareArray(Array.from(map.keys()), ['odd', 'even']);
assert.compareArray(map.get('even'), [2]);
assert.compareArray(map.get('odd'), [1, 3]);

View File

@ -0,0 +1,31 @@
// 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 accessing a getter throws.
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).
...
features: [array-grouping]
---*/
assert.throws(Test262Error, function() {
const arrayLike = Object.defineProperty({
length: 1,
}, '0', {
get: function() {
throw new Test262Error('no element for you');
}
});
Array.prototype.groupToMap.call(arrayLike, function() {
return 'key';
});
});

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.groupToMap
description: Array.prototype.groupToMap populates object with correct keys and values
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).
...
includes: [compareArray.js]
features: [array-grouping]
---*/
const arr = ['hello', 'test', 'world'];
const map = arr.groupToMap(i => i.length);
assert.compareArray(Array.from(map.keys()), [5, 4]);
assert.compareArray(map.get(5), ['hello', 'world']);
assert.compareArray(map.get(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.groupToMap
description: Array.prototype.groupToMap called with non-callable throws TypeError
info: |
22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] )
...
3. If IsCallable(callbackfn) is false, throw a TypeError exception.
...
features: [array-grouping]
---*/
assert.throws(TypeError, function() {
[].groupToMap(null)
}, "null callback throws TypeError");
assert.throws(TypeError, function() {
[].groupToMap(undefined)
}, "undefined callback throws TypeError");
assert.throws(TypeError, function() {
[].groupToMap({})
}, "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.groupToMap
description: Array.prototype.groupToMap applied to null/undefined throws a TypeError
info: |
22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] )
...
1. Let O be ? ToObject(this value).
...
features: [array-grouping]
---*/
const throws = function() {
throw new Test262Error('callback function should not be called')
};
assert.throws(TypeError, function() {
Array.prototype.groupToMap.call(undefined, throws);
}, 'undefined this value');
assert.throws(TypeError, function() {
Array.prototype.groupToMap.call(undefined, throws);
}, 'null this value');

View File

@ -0,0 +1,31 @@
// 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 does not error 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 »)).
...
includes: [compareArray.js]
features: [array-grouping]
---*/
const key = {
toString() {
throw new Test262Error('not a property key');
}
};
const map = [1].groupToMap(function() {
return key;
});
assert.compareArray(Array.from(map.keys()), [key]);
assert.compareArray(map.get(key), [1]);

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.groupToMap
description: Array.prototype.groupToMap errors when array-like's length can't be coerced.
info: |
22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] )
...
2. Let len be ? LengthOfArrayLike(O).
...
features: [array-grouping]
---*/
assert.throws(Test262Error, function() {
const arrayLike = Object.defineProperty({}, 'length', {
get: function() {
throw new Test262Error('no length for you');
}
});
Array.prototype.groupToMap.call(arrayLike, function() {
return 'key';
});
});
assert.throws(TypeError, function() {
const arrayLike = {
length: 1n,
};
Array.prototype.groupToMap.call(arrayLike, function() {
return 'key';
});
});

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.groupToMap
description: Array.prototype.groupToMap property length descriptor
info: |
22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] )
...
17 ECMAScript Standard Built-in Objects
...
includes: [propertyHelper.js]
features: [array-grouping]
---*/
verifyProperty(Array.prototype.groupToMap, "length", {
value: 1,
enumerable: false,
writable: false,
configurable: true
});

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.groupToMap
description: Array.prototype.groupToMap returns a Map instance
info: |
22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] )
...
7. Let map be ! Construct(%Map%).
...
9. Return map.
...
features: [array-grouping]
---*/
const array = [1, 2, 3];
const map = array.groupToMap(i => {
return i % 2 === 0 ? 'even' : 'odd';
});
assert.sameValue(map instanceof Map, 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.groupToMap
description: Array.prototype.groupToMap property name descriptor
info: |
22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] )
...
17 ECMAScript Standard Built-in Objects
...
includes: [propertyHelper.js]
features: [array-grouping]
---*/
verifyProperty(Array.prototype.groupToMap, "name", {
value: "groupToMap",
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.groupToMap
description: Array.prototype.groupToMap normalize 0 for Map key
info: |
22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] )
...
6.d. If key is -0𝔽, set key to +0𝔽.
...
includes: [compareArray.js]
features: [array-grouping]
---*/
const arr = [-0, +0];
const map = arr.groupToMap(i => i);
assert.sameValue(map.size, 1);
assert.compareArray(map.get(0), [-0, 0]);

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.groupToMap
description: Array.prototype.groupToMap adds undefined to the group for sparse arrays
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).
...
includes: [compareArray.js]
features: [array-grouping]
---*/
let calls = 0;
const array = [, , ,];
const map = array.groupToMap(() => {
calls++;
return 'key';
});
assert.sameValue(calls, 3);
const key = map.get('key');
assert(0 in key, 'group has a first element');
assert(1 in key, 'group has a second element');
assert(2 in key, 'group has a third element');
assert.compareArray(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.groupToMap
description: Array.prototype.groupToMap calls with thisArg
info: |
22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] )
...
6. Repeat, while k < len
c. Let key be ? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »).
...
flags: [onlyStrict]
features: [array-grouping]
---*/
let sentinel = {};
let result;
[1].groupToMap(function() {
result = this;
}, "TestString");
assert.sameValue(result, "TestString");
[1].groupToMap(function() {
result = this;
}, 1);
assert.sameValue(result, 1);
[1].groupToMap(function() {
result = this;
}, true);
assert.sameValue(result, true);
[1].groupToMap(function() {
result = this;
}, null);
assert.sameValue(result, null);
[1].groupToMap(function() {
result = this;
}, sentinel);
assert.sameValue(result, sentinel);
[1].groupToMap(function() {
result = this;
}, void 0);
assert.sameValue(result, void 0);
[1].groupToMap(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.groupToMap
description: Array.prototype.groupToMap calls with thisArg
info: |
22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] )
...
6. Repeat, while k < len
c. Let key be ? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »).
...
flags: [noStrict]
features: [array-grouping]
---*/
let sentinel = {};
let result;
[1].groupToMap(function() {
result = this;
}, "TestString");
assert.sameValue(result instanceof String, true);
assert.sameValue(result.valueOf(), "TestString");
[1].groupToMap(function() {
result = this;
}, 1);
assert.sameValue(result instanceof Number, true);
assert.sameValue(result.valueOf(), 1);
[1].groupToMap(function() {
result = this;
}, true);
assert.sameValue(result instanceof Boolean, true);
assert.sameValue(result.valueOf(), true);
[1].groupToMap(function() {
result = this;
}, null);
assert.sameValue(result, globalThis);
[1].groupToMap(function() {
result = this;
}, sentinel);
assert.sameValue(result, sentinel);
[1].groupToMap(function() {
result = this;
}, void 0);
assert.sameValue(result, globalThis);
[1].groupToMap(function() {
result = this;
});
assert.sameValue(result, globalThis);

View File

@ -0,0 +1,39 @@
// 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 does not coerce return value with ToPropertyKey
info: |
22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] )
...
6. Repeat, while k < len
c. Let key be ? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »).
e. Perform AddValueToKeyedGroup(groups, key, kValue).
...
8. For each Record { [[Key]], [[Elements]] } g of groups, do
a. Let elements be ! CreateArrayFromList(g.[[Elements]]).
b. Perform ! CreateDataPropertyOrThrow(map, g.[[Key]], elements).
...
includes: [compareArray.js]
features: [array-grouping]
---*/
let calls = 0;
const stringable = {
toString() {
return 1;
}
}
const array = [1, '1', stringable];
const map = array.groupToMap(v => v);
assert.compareArray(Array.from(map.keys()), [1, '1', stringable]);
assert.compareArray(map.get('1'), ['1']);
assert.compareArray(map.get(1), [1]);
assert.compareArray(map.get(stringable), [stringable]);