LibJS: Add additional generic Array.prototype.slice tests

This commit is contained in:
davidot 2021-06-13 19:16:59 +02:00 committed by Linus Groh
parent fc1168a3b3
commit ae8b55a80a
Notes: sideshowbarker 2024-07-18 12:16:05 +09:00

View file

@ -40,15 +40,48 @@ describe("ability to work with generic non-array objects", () => {
});
test("slice", () => {
const o = { length: 3, 0: "hello", 2: "serenity" };
const slice = Array.prototype.slice.call(o, 0, 2);
expect(o).toHaveLength(3);
expect(o[0]).toBe("hello");
expect(o[1]).toBeUndefined();
expect(o[2]).toBe("serenity");
expect(slice).toHaveLength(2);
expect(slice[0]).toBe("hello");
expect(slice[1]).toBeUndefined();
{
const o = { length: 3, 0: "hello", 2: "serenity" };
const slice = Array.prototype.slice.call(o, 0, 2);
expect(o).toHaveLength(3);
expect(o[0]).toBe("hello");
expect(o[1]).toBeUndefined();
expect(o[2]).toBe("serenity");
expect(slice).toHaveLength(2);
expect(slice[0]).toBe("hello");
expect(slice[1]).toBeUndefined();
}
{
const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
expect(Array.prototype.slice.call(o)).toEqual([
"foo",
"bar",
undefined,
"baz",
undefined,
]);
expect(Array.prototype.slice.call(o, 0, 3)).toEqual(["foo", "bar", undefined]);
expect(Array.prototype.slice.call(o, 0, 15)).toEqual([
"foo",
"bar",
undefined,
"baz",
undefined,
]);
expect(Array.prototype.slice.call(o, 1)).toEqual(["bar", undefined, "baz", undefined]);
expect(Array.prototype.slice.call(o, 15)).toEqual([]);
expect(Array.prototype.slice.call(o, -1)).toEqual([undefined]);
expect(Array.prototype.slice.call(o, -2)).toEqual(["baz", undefined]);
expect(Array.prototype.slice.call(o, 1, -1)).toEqual(["bar", undefined, "baz"]);
expect(Array.prototype.slice.call(o, 2, -2)).toEqual([undefined]);
expect(Array.prototype.slice.call(o, 3, -3)).toEqual([]);
expect(Array.prototype.slice.call(o, 0, 0)).toEqual([]);
expect(Array.prototype.slice.call(o, 10, 10)).toEqual([]);
}
});
test("join", () => {