summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Tests
diff options
context:
space:
mode:
authorLuke <luke.wilde@live.co.uk>2021-06-13 15:49:52 +0100
committerLinus Groh <mail@linusgroh.de>2021-06-13 16:21:34 +0100
commitd72aeb2e1a6970ed56ed28abd88ea8225027ff8c (patch)
tree61d7cd2c222dbbb53bbd9b6ba8784e8e41a5d793 /Userland/Libraries/LibJS/Tests
parent2e1a01a499d0fd0294922c4d77b19745ed0a6960 (diff)
downloadserenity-d72aeb2e1a6970ed56ed28abd88ea8225027ff8c.zip
LibJS: Rewrite Array.prototype.slice to be spec compliant
This makes it generic in the process (which is required by jQuery) This fixes 19 test262 test cases :^)
Diffstat (limited to 'Userland/Libraries/LibJS/Tests')
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype-generic-functions.js12
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype.slice.js15
2 files changed, 27 insertions, 0 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 7121d6c18d..85ac3bf9e5 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
@@ -39,6 +39,18 @@ describe("ability to work with generic non-array objects", () => {
expect(removed[1]).toBeUndefined();
});
+ 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();
+ });
+
test("join", () => {
expect(Array.prototype.join.call({})).toBe("");
expect(Array.prototype.join.call({ length: "foo" })).toBe("");
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype.slice.js b/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype.slice.js
index c1c45be082..62f495b917 100644
--- a/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype.slice.js
+++ b/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype.slice.js
@@ -37,3 +37,18 @@ test("basic functionality", () => {
expect(array).toEqual(["hello", "friends", "serenity", 1]);
expect(slice).toEqual(["hello", "friends", "serenity", 1]);
});
+
+// FIXME: These tests are currently skipped because an invalid array length in this case is 2**32 or above.
+// The codebase currently uses size_t for lengths, which is currently the same as u32 when building for Serenity.
+// This means these lengths wrap around to 0, making the test not work correctly.
+test.skip("Invalid lengths", () => {
+ var length = Math.pow(2, 32);
+
+ var obj = {
+ length: length,
+ };
+
+ expect(() => {
+ Array.prototype.slice.call(obj, 0);
+ }).toThrowWithMessage(RangeError, "Invalid array length");
+});