1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 23:37:36 +00:00

LibJS: Implement Array.prototype.copyWithin generically

This commit is contained in:
davidot 2021-06-13 18:09:34 +02:00 committed by Linus Groh
parent 417f752306
commit 6c13cc67c6
5 changed files with 161 additions and 0 deletions

View file

@ -130,6 +130,35 @@ describe("ability to work with generic non-array objects", () => {
}
});
test("copyWithin", () => {
const initial_o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
{
const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
// returns value and modifies
expect(Array.prototype.copyWithin.call(o, 0, 0)).toEqual(o);
expect(o).toEqual(initial_o);
}
{
const o = {};
expect(Array.prototype.copyWithin.call(o, 1, 16, 32)).toEqual(o);
expect(o).toEqual({});
}
{
const o = { length: 100 };
expect(Array.prototype.copyWithin.call(o, 1, 16, 32)).toEqual(o);
expect(o).toEqual({ length: 100 });
}
{
const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
// returns value and modifies
expect(Array.prototype.copyWithin.call(o, 2, 0)).toEqual(o);
expect(o).toEqual({ length: 5, 0: "foo", 1: "bar", 2: "foo", 3: "bar" });
}
});
const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
test("every", () => {

View file

@ -0,0 +1,45 @@
test("length is 2", () => {
expect(Array.prototype.copyWithin).toHaveLength(2);
});
describe("normal behavior", () => {
test("Noop", () => {
var array = [1, 2];
array.copyWithin(0, 0);
expect(array).toEqual([1, 2]);
});
test("basic behavior", () => {
var array = [1, 2, 3];
var b = array.copyWithin(1, 2);
expect(b).toEqual(array);
expect(array).toEqual([1, 3, 3]);
b = array.copyWithin(2, 0);
expect(b).toEqual(array);
expect(array).toEqual([1, 3, 1]);
});
test("start > target", () => {
var array = [1, 2, 3];
var b = array.copyWithin(0, 1);
expect(b).toEqual(array);
expect(array).toEqual([2, 3, 3]);
});
test("overwriting behavior", () => {
var array = [1, 2, 3];
var b = array.copyWithin(1, 0);
expect(b).toEqual(array);
expect(array).toEqual([1, 1, 2]);
});
test("specify end", () => {
var array = [1, 2, 3];
b = array.copyWithin(2, 0, 1);
expect(b).toEqual(array);
expect(array).toEqual([1, 2, 1]);
});
});