Skip to content

Commit

Permalink
Add "Change Array by Copy" tests (stage 3) (#3464)
Browse files Browse the repository at this point in the history
Co-authored-by: Ashley Claymore <acutmore@users.noreply.github.com>
Co-authored-by: Tim Chevalier <tjc@igalia.com>
Co-authored-by: Ms2ger <Ms2ger@igalia.com>
  • Loading branch information
4 people committed Oct 18, 2022
1 parent 77b559c commit 6f4601d
Show file tree
Hide file tree
Showing 119 changed files with 3,626 additions and 0 deletions.
5 changes: 5 additions & 0 deletions features.txt
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,11 @@ regexp-duplicate-named-groups
# https://github.com/tc39/proposal-symbols-as-weakmap-keys
symbols-as-weakmap-keys

# Array.prototype.toReversed, Array.prototype.toSorted, Array.prototype.toSpliced,
# Array.prototype.with and the equivalent TypedArray methods.
# https://github.com/tc39/proposal-change-array-by-copy/
change-array-by-copy

## Standard language features
#
# Language features that have been included in a published version of the
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (C) 2021 Igalia. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype-@@unscopables
description: >
Initial value of `Symbol.unscopables` property
info: |
22.1.3.32 Array.prototype [ @@unscopables ]
...
12. Perform ! CreateDataPropertyOrThrow(unscopableList, "toReversed", true).
13. Perform ! CreateDataPropertyOrThrow(unscopableList, "toSorted", true).
14. Perform ! CreateDataPropertyOrThrow(unscopableList, "toSpliced", true).
...
includes: [propertyHelper.js]
features: [Symbol.unscopables, change-array-by-copy]
---*/

var unscopables = Array.prototype[Symbol.unscopables];

for (const unscopable of ["toReversed", "toSorted", "toSpliced"]) {
verifyProperty(unscopables, unscopable, {
value: true,
writable: true,
configurable: true
})
};

assert.sameValue(Object.hasOwnProperty.call(unscopables, "with"), false, "does not have `with`");
18 changes: 18 additions & 0 deletions test/built-ins/Array/prototype/toReversed/frozen-this-value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-array.prototype.toReversed
description: >
Array.prototype.toReversed works on frozen objects
features: [change-array-by-copy]
includes: [compareArray.js]
---*/

var arr = Object.freeze([0, 1, 2]);
var result = arr.toReversed();
assert.compareArray(result, [2, 1, 0]);

var arrayLike = Object.freeze({ length: 3, 0: 0, 1: 1, 2: 2 });
result = Array.prototype.toReversed.call(arrayLike);
assert.compareArray(result, [2, 1, 0]);
49 changes: 49 additions & 0 deletions test/built-ins/Array/prototype/toReversed/get-descending-order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-array.prototype.toReversed
description: >
Array.prototype.toReversed gets the array elements from the last one to the first one.
info: |
Array.prototype.toReversed ( )
...
2. Let len be ? LengthOfArrayLike(O).
...
5. Repeat, while k < len
a. Let from be ! ToString(𝔽(len - k - 1)).
...
c. Let fromValue be ? Get(O, from).
...
features: [change-array-by-copy]
includes: [compareArray.js]
---*/

var order = [];
var arrayLike = {
length: 3,
get 0() {
order.push(0);
},
get 1() {
order.push(1);
},
get 2() {
order.push(2);
},
};

Array.prototype.toReversed.call(arrayLike);

assert.compareArray(order, [2, 1, 0]);

order = [];
var arr = [0, 1, 2];
Object.defineProperty(arr, 0, { get: function() { order.push(0); } });
Object.defineProperty(arr, 1, { get: function() { order.push(1); } });
Object.defineProperty(arr, 2, { get: function() { order.push(2); } });

Array.prototype.toReversed.call(arr);

assert.compareArray(order, [2, 1, 0]);
29 changes: 29 additions & 0 deletions test/built-ins/Array/prototype/toReversed/holes-not-preserved.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-array.prototype.toReversed
description: >
Array.prototype.toReversed does not preserve holes in the array
info: |
Array.prototype.toReversed ( )
...
2. Let len be ? LengthOfArrayLike(O).
...
5. Repeat, while k < len
a. Let from be ! ToString(𝔽(len - k - 1)).
...
c. Let fromValue be ? Get(O, from).
d. Perform ? CreateDataPropertyOrThrow(A, Pk, fromValue).
...
features: [change-array-by-copy]
includes: [compareArray.js]
---*/

var arr = [0, /* hole */, 2, /* hole */, 4];
Array.prototype[3] = 3;

var reversed = arr.toReversed();
assert.compareArray(reversed, [4, 3, 2, undefined, 0]);
assert(reversed.hasOwnProperty(3));
31 changes: 31 additions & 0 deletions test/built-ins/Array/prototype/toReversed/ignores-species.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-array.prototype.toReversed
description: >
Array.prototype.toReversed ignores @@species
info: |
Array.prototype.toReversed ( )
...
3. Let A be ? ArrayCreate(𝔽(len)).
...
features: [change-array-by-copy]
---*/

var a = [];
a.constructor = {};
a.constructor[Symbol.species] = function () {}

assert.sameValue(Object.getPrototypeOf(a.toReversed()), Array.prototype);

var b = [];

Object.defineProperty(b, "constructor", {
get() {
throw new Test262Error("Should not get .constructor");
}
});

b.toReversed();
16 changes: 16 additions & 0 deletions test/built-ins/Array/prototype/toReversed/immutable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-array.prototype.toReversed
description: >
Array.prototype.toReversed does not mutate its this value
features: [change-array-by-copy]
includes: [compareArray.js]
---*/

var arr = [0, 1, 2];
arr.toReversed();

assert.compareArray(arr, [0, 1, 2]);
assert.notSameValue(arr.toReversed(), arr);
20 changes: 20 additions & 0 deletions test/built-ins/Array/prototype/toReversed/length-casted-to-zero.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-array.prototype.toReversed
description: >
Array.prototype.toReversed creates an empty array if the this value .length is not a positive integer.
info: |
Array.prototype.toReversed ( )
...
2. Let len be ? LengthOfArrayLike(O).
...
features: [change-array-by-copy]
includes: [compareArray.js]
---*/

assert.compareArray(Array.prototype.toReversed.call({ length: -2 }), []);
assert.compareArray(Array.prototype.toReversed.call({ length: "dog" }), []);
assert.compareArray(Array.prototype.toReversed.call({ length: NaN }), []);
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-array.prototype.toReversed
description: >
Array.prototype.toReversed caches the length getting the array elements.
info: |
Array.prototype.toReversed ( )
...
2. Let len be ? LengthOfArrayLike(O).
...
5. Repeat, while k < len
...
c. Let fromValue be ? Get(O, from).
...
features: [change-array-by-copy]
includes: [compareArray.js]
---*/

var arr = [0, 1, 2, 3, 4];
Array.prototype[1] = 5;

Object.defineProperty(arr, "3", {
get() {
arr.length = 1;
return 3;
}
});

assert.compareArray(arr.toReversed(), [4, 3, undefined, 5, 0]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-array.prototype.toReversed
description: >
Array.prototype.toReversed limits the length to 2 ** 32 - 1
info: |
Array.prototype.toReversed ( )
...
2. Let len be ? LengthOfArrayLike(O).
3. Let A be ? ArrayCreate(𝔽(len)).
...
ArrayCreate ( length [, proto ] )
1. If length > 2 ** 32 - 1, throw a RangeError exception.
features: [change-array-by-copy]
---*/

// Object with large "length" property
var arrayLike = {
get "0"() {
throw new Test262Error("Get 0");
},
get "4294967295" () { // 2 ** 32 - 1
throw new Test262Error("Get 2147483648");
},
get "4294967296" () { // 2 ** 32
throw new Test262Error("Get 2147483648");
},
length: 2 ** 32
};

assert.throws(RangeError, function() {
Array.prototype.toReversed.call(arrayLike);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-array.prototype.toReversed
description: >
Array.prototype.toReversed caches the length getting the array elements.
info: |
Array.prototype.toReversed ( )
...
2. Let len be ? LengthOfArrayLike(O).
...
5. Repeat, while k < len
...
c. Let fromValue be ? Get(O, from).
...
features: [change-array-by-copy]
includes: [compareArray.js]
---*/

var arr = [0, 1, 2];
Object.defineProperty(arr, "0", {
get() {
arr.push(4);
return 0;
}
});

assert.compareArray(arr.toReversed(), [2, 1, 0]);
29 changes: 29 additions & 0 deletions test/built-ins/Array/prototype/toReversed/length-tolength.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-array.prototype.toReversed
description: >
Array.prototype.toReversed converts the this value length to a number.
info: |
Array.prototype.toReversed ( )
...
2. Let len be ? LengthOfArrayLike(O).
...
features: [change-array-by-copy]
includes: [compareArray.js]
---*/

assert.compareArray(Array.prototype.toReversed.call({ length: "2", 0: 1, 1: 2, 2: 3 }), [2, 1]);

var arrayLike = {
length: {
valueOf: () => 2
},
0: 1,
1: 2,
2: 3,
};

assert.compareArray(Array.prototype.toReversed.call(arrayLike), [2, 1]);
30 changes: 30 additions & 0 deletions test/built-ins/Array/prototype/toReversed/metadata/length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-array.prototype.toReversed
description: >
The "length" property of Array.prototype.toReversed
info: |
17 ECMAScript Standard Built-in Objects
Every built-in function object, including constructors, has a length property
whose value is an integer. Unless otherwise specified, this value is equal to
the largest number of named arguments shown in the subclause headings for the
function description. Optional parameters (which are indicated with brackets:
[ ]) or rest parameters (which are shown using the form «...name») are not
included in the default argument count.
Unless otherwise specified, the length property of a built-in function object
has the attributes { [[Writable]]: false, [[Enumerable]]: false,
[[Configurable]]: true }.
includes: [propertyHelper.js]
features: [change-array-by-copy]
---*/

verifyProperty(Array.prototype.toReversed, "length", {
value: 0,
writable: false,
enumerable: false,
configurable: true,
});

0 comments on commit 6f4601d

Please sign in to comment.