From ae8b55a80a02bd52223e55c271b5a1ff5982f58f Mon Sep 17 00:00:00 2001 From: davidot Date: Sun, 13 Jun 2021 19:16:59 +0200 Subject: [PATCH] LibJS: Add additional generic Array.prototype.slice tests --- .../Array.prototype-generic-functions.js | 51 +++++++++++++++---- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype-generic-functions.js b/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype-generic-functions.js index c509ea3056..07b0bf610b 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype-generic-functions.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype-generic-functions.js @@ -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", () => {