From 461d90d042dc15221fea94fdbd412e77274faac8 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sat, 4 Jul 2020 18:56:48 +0100 Subject: [PATCH] LibJS: Convert Array tests to new testing framework --- .../Tests/builtins/Array/Array.isArray.js | 48 ++-- Libraries/LibJS/Tests/builtins/Array/Array.js | 100 ++++--- .../LibJS/Tests/builtins/Array/Array.of.js | 103 ++++--- .../Array.prototype-generic-functions.js | 268 +++++++++--------- .../builtins/Array/Array.prototype.concat.js | 69 +++-- .../builtins/Array/Array.prototype.every.js | 84 +++--- .../builtins/Array/Array.prototype.fill.js | 35 ++- .../builtins/Array/Array.prototype.filter.js | 130 ++++----- .../builtins/Array/Array.prototype.find.js | 113 ++++---- .../Array/Array.prototype.findIndex.js | 116 ++++---- .../builtins/Array/Array.prototype.forEach.js | 120 ++++---- .../Array/Array.prototype.includes.js | 34 +-- .../builtins/Array/Array.prototype.indexOf.js | 48 ++-- .../builtins/Array/Array.prototype.join.js | 32 +-- .../Array/Array.prototype.lastIndexOf.js | 42 ++- .../builtins/Array/Array.prototype.map.js | 104 ++++--- .../builtins/Array/Array.prototype.pop.js | 38 +-- .../builtins/Array/Array.prototype.push.js | 47 ++- .../builtins/Array/Array.prototype.reduce.js | 203 ++++++------- .../Array/Array.prototype.reduceRight.js | 210 +++++++------- .../builtins/Array/Array.prototype.reverse.js | 36 +-- .../builtins/Array/Array.prototype.shift.js | 38 +-- .../builtins/Array/Array.prototype.slice.js | 72 ++--- .../builtins/Array/Array.prototype.some.js | 62 ++-- .../builtins/Array/Array.prototype.splice.js | 84 ++---- .../Array/Array.prototype.toLocaleString.js | 76 +++-- .../Array/Array.prototype.toString.js | 70 +++-- .../builtins/Array/Array.prototype.unshift.js | 47 ++- .../LibJS/Tests/builtins/Array/array-basic.js | 78 +++-- .../builtins/Array/array-length-setter.js | 82 +++--- .../Array/array-shrink-during-find-crash.js | 14 +- .../Tests/builtins/Array/array-spread.js | 62 ++-- Userland/test-js.cpp | 32 +++ 33 files changed, 1315 insertions(+), 1382 deletions(-) diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.isArray.js b/Libraries/LibJS/Tests/builtins/Array/Array.isArray.js index 714f6e08ea..bbe12a8c44 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.isArray.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.isArray.js @@ -1,28 +1,26 @@ -load("test-common.js"); +test("length is 1", () => { + expect(Array.isArray).toHaveLength(1); +}); -try { - assert(Array.isArray.length === 1); +test("arguments that evaluate to false", () => { + expect(Array.isArray()).toBeFalse(); + expect(Array.isArray("1")).toBeFalse(); + expect(Array.isArray("foo")).toBeFalse(); + expect(Array.isArray(1)).toBeFalse(); + expect(Array.isArray(1, 2, 3)).toBeFalse(); + expect(Array.isArray(undefined)).toBeFalse(); + expect(Array.isArray(null)).toBeFalse(); + expect(Array.isArray(Infinity)).toBeFalse(); + expect(Array.isArray({})).toBeFalse(); +}); - assert(Array.isArray() === false); - assert(Array.isArray("1") === false); - assert(Array.isArray("foo") === false); - assert(Array.isArray(1) === false); - assert(Array.isArray(1, 2, 3) === false); - assert(Array.isArray(undefined) === false); - assert(Array.isArray(null) === false); - assert(Array.isArray(Infinity) === false); - assert(Array.isArray({}) === false); - - assert(Array.isArray([]) === true); - assert(Array.isArray([1]) === true); - assert(Array.isArray([1, 2, 3]) === true); - assert(Array.isArray(new Array()) === true); - assert(Array.isArray(new Array(10)) === true); - assert(Array.isArray(new Array("a", "b", "c")) === true); +test("arguments that evaluate to true", () => { + expect(Array.isArray([])).toBeTrue(); + expect(Array.isArray([1])).toBeTrue(); + expect(Array.isArray([1, 2, 3])).toBeTrue(); + expect(Array.isArray(new Array())).toBeTrue(); + expect(Array.isArray(new Array(10))).toBeTrue(); + expect(Array.isArray(new Array("a", "b", "c"))).toBeTrue(); // FIXME: Array.prototype is supposed to be an array! - // assert(Array.isArray(Array.prototype) === true); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + // expect(Array.isArray(Array.prototype)).toBeTrue(); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.js b/Libraries/LibJS/Tests/builtins/Array/Array.js index 9da2d9de37..1ba2c400b1 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.js @@ -1,55 +1,53 @@ -load("test-common.js"); +test("constructor properties", () => { + expect(Array).toHaveLength(1); + expect(Array.name).toBe("Array"); + expect(Array.prototype.length).toBe(0); +}); -try { - assert(Array.length === 1); - assert(Array.name === "Array"); - assert(Array.prototype.length === 0); - - assert(typeof Array() === "object"); - assert(typeof new Array() === "object"); - - var a; - - a = new Array(5); - assert(a.length === 5); - - a = new Array("5"); - assert(a.length === 1); - assert(a[0] === "5"); - - a = new Array(1, 2, 3); - assert(a.length === 3); - assert(a[0] === 1); - assert(a[1] === 2); - assert(a[2] === 3); - - a = new Array([1, 2, 3]); - assert(a.length === 1); - assert(a[0][0] === 1); - assert(a[0][1] === 2); - assert(a[0][2] === 3); - - a = new Array(1, 2, 3); - Object.defineProperty(a, 3, { - get() { - return 10; - }, - }); - assert(a.toString() === "1,2,3,10"); - - [-1, -100, -0.1, 0.1, 1.23, Infinity, -Infinity, NaN].forEach(value => { - assertThrowsError( - () => { +describe("errors", () => { + test("invalid array length", () => { + [-1, -100, -0.1, 0.1, 1.23, Infinity, -Infinity, NaN].forEach(value => { + expect(() => { new Array(value); - }, - { - error: TypeError, - message: "Invalid array length", - } - ); + }).toThrowWithMessage(TypeError, "Invalid array length"); + }); + }); +}); + +describe("normal behavior", () => { + test("typeof", () => { + expect(typeof Array()).toBe("object"); + expect(typeof new Array()).toBe("object"); }); - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + test("constructor with single numeric argument", () => { + var a = new Array(5); + expect(a instanceof Array).toBeTrue(); + expect(a).toHaveLength(5); + }); + + test("constructor with single non-numeric argument", () => { + var a = new Array("5"); + expect(a instanceof Array).toBeTrue(); + expect(a).toHaveLength(1); + expect(a[0]).toBe("5"); + }); + + test("constructor with multiple numeric arguments", () => { + var a = new Array(1, 2, 3); + expect(a instanceof Array).toBeTrue(); + expect(a).toHaveLength(3); + expect(a[0]).toBe(1); + expect(a[1]).toBe(2); + expect(a[2]).toBe(3); + }); + + test("constructor with single array argument", () => { + var a = new Array([1, 2, 3]); + expect(a instanceof Array).toBeTrue(); + expect(a).toHaveLength(1); + expect(a[0][0]).toBe(1); + expect(a[0][1]).toBe(2); + expect(a[0][2]).toBe(3); + }); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.of.js b/Libraries/LibJS/Tests/builtins/Array/Array.of.js index d44eb78e9f..8e03470263 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.of.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.of.js @@ -1,50 +1,59 @@ -load("test-common.js"); +test("length is 0", () => { + expect(Array.of).toHaveLength(0); +}); -try { - assert(Array.of.length === 0); - - assert(typeof Array.of() === "object"); - - var a; - - a = Array.of(5); - assert(a.length === 1); - assert(a[0] === 5); - - a = Array.of("5"); - assert(a.length === 1); - assert(a[0] === "5"); - - a = Array.of(Infinity); - assert(a.length === 1); - assert(a[0] === Infinity); - - a = Array.of(1, 2, 3); - assert(a.length === 3); - assert(a[0] === 1); - assert(a[1] === 2); - assert(a[2] === 3); - - a = Array.of([1, 2, 3]); - assert(a.length === 1); - assert(a[0][0] === 1); - assert(a[0][1] === 2); - assert(a[0][2] === 3); - - let t = [1, 2, 3]; - Object.defineProperty(t, 3, { - get() { - return 4; - }, +describe("normal behavior", () => { + test("single numeric argument", () => { + var a = Array.of(5); + expect(a instanceof Array).toBeTrue(); + expect(a).toHaveLength(1); + expect(a[0]).toBe(5); }); - a = Array.of(...t); - assert(a.length === 4); - assert(a[0] === 1); - assert(a[1] === 2); - assert(a[2] === 3); - assert(a[3] === 4); - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + test("single non-numeric argument", () => { + var a = Array.of("5"); + expect(a instanceof Array).toBeTrue(); + expect(a).toHaveLength(1); + expect(a[0]).toBe("5"); + }); + + test("single infinite numeric argument", () => { + var a = Array.of(Infinity); + expect(a instanceof Array).toBeTrue(); + expect(a).toHaveLength(1); + expect(a[0]).toBe(Infinity); + }); + + test("multiple numeric arguments", () => { + var a = Array.of(1, 2, 3); + expect(a instanceof Array).toBeTrue(); + expect(a).toHaveLength(3); + expect(a[0]).toBe(1); + expect(a[1]).toBe(2); + expect(a[2]).toBe(3); + }); + + test("single array argument", () => { + var a = Array.of([1, 2, 3]); + expect(a instanceof Array).toBeTrue(); + expect(a).toHaveLength(1); + expect(a[0][0]).toBe(1); + expect(a[0][1]).toBe(2); + expect(a[0][2]).toBe(3); + }); + + test("getter property is included in returned array", () => { + var t = [1, 2, 3]; + Object.defineProperty(t, 3, { + get() { + return 4; + }, + }); + var a = Array.of(...t); + expect(a).toHaveLength(4); + expect(a[0]).toBe(1); + expect(a[1]).toBe(2); + expect(a[2]).toBe(3); + expect(a[3]).toBe(4); + }); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.prototype-generic-functions.js b/Libraries/LibJS/Tests/builtins/Array/Array.prototype-generic-functions.js index 3e0dca3759..a8afde0ea3 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.prototype-generic-functions.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.prototype-generic-functions.js @@ -1,164 +1,150 @@ -load("test-common.js"); +describe("ability to work with generic non-array objects", () => { + test("push, pop", () => { + [undefined, "foo", -42, 0].forEach(length => { + const o = { length }; -try { - [undefined, "foo", -42, 0].forEach(length => { - const o = { length }; + expect(Array.prototype.push.call(o, "foo")).toBe(1); + expect(o).toHaveLength(1); + expect(o[0]).toBe("foo"); + expect(Array.prototype.push.call(o, "bar", "baz")).toBe(3); + expect(o).toHaveLength(3); + expect(o[0]).toBe("foo"); + expect(o[1]).toBe("bar"); + expect(o[2]).toBe("baz"); - assert(Array.prototype.push.call(o, "foo") === 1); - assert(o.length === 1); - assert(o[0] === "foo"); - assert(Array.prototype.push.call(o, "bar", "baz") === 3); - assert(o.length === 3); - assert(o[0] === "foo"); - assert(o[1] === "bar"); - assert(o[2] === "baz"); + expect(Array.prototype.pop.call(o)).toBe("baz"); + expect(o).toHaveLength(2); + expect(Array.prototype.pop.call(o)).toBe("bar"); + expect(o).toHaveLength(1); + expect(Array.prototype.pop.call(o)).toBe("foo"); + expect(o).toHaveLength(0); + expect(Array.prototype.pop.call(o)).toBeUndefined(); + expect(o).toHaveLength(0); - assert(Array.prototype.pop.call(o) === "baz"); - assert(o.length === 2); - assert(Array.prototype.pop.call(o) === "bar"); - assert(o.length === 1); - assert(Array.prototype.pop.call(o) === "foo"); - assert(o.length === 0); - assert(Array.prototype.pop.call(o) === undefined); - assert(o.length === 0); - - o.length = length; - assert(Array.prototype.pop.call(o) === undefined); - assert(o.length === 0); + o.length = length; + expect(Array.prototype.pop.call(o)).toBeUndefined(); + expect(o).toHaveLength(0); + }); }); - { + test("splice", () => { const o = { length: 3, 0: "hello", 2: "serenity" }; const removed = Array.prototype.splice.call(o, 0, 2, "hello", "friends"); - assert(o.length === 3); - assert(o[0] === "hello"); - assert(o[1] === "friends"); - assert(o[2] === "serenity"); - assert(removed.length === 2); - assert(removed[0] === "hello"); - assert(removed[1] === undefined); - } + expect(o).toHaveLength(3); + expect(o[0]).toBe("hello"); + expect(o[1]).toBe("friends"); + expect(o[2]).toBe("serenity"); + expect(removed).toHaveLength(2); + expect(removed[0]).toBe("hello"); + expect(removed[1]).toBeUndefined(); + }); - { - assert(Array.prototype.join.call({}) === ""); - assert(Array.prototype.join.call({ length: "foo" }) === ""); - assert(Array.prototype.join.call({ length: 3 }) === ",,"); - assert(Array.prototype.join.call({ length: 2, 0: "foo", 1: "bar" }) === "foo,bar"); - assert( - Array.prototype.join.call({ length: 2, 0: "foo", 1: "bar", 2: "baz" }) === "foo,bar" + test("join", () => { + expect(Array.prototype.join.call({})).toBe(""); + expect(Array.prototype.join.call({ length: "foo" })).toBe(""); + expect(Array.prototype.join.call({ length: 3 })).toBe(",,"); + expect(Array.prototype.join.call({ length: 2, 0: "foo", 1: "bar" })).toBe("foo,bar"); + expect(Array.prototype.join.call({ length: 2, 0: "foo", 1: "bar", 2: "baz" })).toBe( + "foo,bar" ); - assert(Array.prototype.join.call({ length: 3, 1: "bar" }, "~") === "~bar~"); - assert( - Array.prototype.join.call({ length: 3, 0: "foo", 1: "bar", 2: "baz" }, "~") === - "foo~bar~baz" + expect(Array.prototype.join.call({ length: 3, 1: "bar" }, "~")).toBe("~bar~"); + expect(Array.prototype.join.call({ length: 3, 0: "foo", 1: "bar", 2: "baz" }, "~")).toBe( + "foo~bar~baz" ); - } + }); - { - assert(Array.prototype.toString.call({}) === "[object Object]"); - assert(Array.prototype.toString.call({ join: "foo" }) === "[object Object]"); - assert(Array.prototype.toString.call({ join: () => "foo" }) === "foo"); - } + // FIXME: test-js asserts when this is just called "toString" ಠ_ಠ + test("toString (FIXME)", () => { + expect(Array.prototype.toString.call({})).toBe("[object Object]"); + expect(Array.prototype.toString.call({ join: "foo" })).toBe("[object Object]"); + expect(Array.prototype.toString.call({ join: () => "foo" })).toBe("foo"); + }); - { - assert(Array.prototype.indexOf.call({}) === -1); - assert(Array.prototype.indexOf.call({ 0: undefined }) === -1); - assert(Array.prototype.indexOf.call({ length: 1, 0: undefined }) === 0); - assert(Array.prototype.indexOf.call({ length: 1, 2: "foo" }, "foo") === -1); - assert(Array.prototype.indexOf.call({ length: 5, 2: "foo" }, "foo") === 2); - assert(Array.prototype.indexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo", 3) === 4); - } + test("indexOf", () => { + expect(Array.prototype.indexOf.call({})).toBe(-1); + expect(Array.prototype.indexOf.call({ 0: undefined })).toBe(-1); + expect(Array.prototype.indexOf.call({ length: 1, 0: undefined })).toBe(0); + expect(Array.prototype.indexOf.call({ length: 1, 2: "foo" }, "foo")).toBe(-1); + expect(Array.prototype.indexOf.call({ length: 5, 2: "foo" }, "foo")).toBe(2); + expect(Array.prototype.indexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo", 3)).toBe(4); + }); - { - assert(Array.prototype.lastIndexOf.call({}) === -1); - assert(Array.prototype.lastIndexOf.call({ 0: undefined }) === -1); - assert(Array.prototype.lastIndexOf.call({ length: 1, 0: undefined }) === 0); - assert(Array.prototype.lastIndexOf.call({ length: 1, 2: "foo" }, "foo") === -1); - assert(Array.prototype.lastIndexOf.call({ length: 5, 2: "foo" }, "foo") === 2); - assert(Array.prototype.lastIndexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo") === 4); - assert( - Array.prototype.lastIndexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo", -2) === 2 + test("lastIndexOf", () => { + expect(Array.prototype.lastIndexOf.call({})).toBe(-1); + expect(Array.prototype.lastIndexOf.call({ 0: undefined })).toBe(-1); + expect(Array.prototype.lastIndexOf.call({ length: 1, 0: undefined })).toBe(0); + expect(Array.prototype.lastIndexOf.call({ length: 1, 2: "foo" }, "foo")).toBe(-1); + expect(Array.prototype.lastIndexOf.call({ length: 5, 2: "foo" }, "foo")).toBe(2); + expect(Array.prototype.lastIndexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo")).toBe(4); + expect(Array.prototype.lastIndexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo", -2)).toBe( + 2 ); - } + }); - { - assert(Array.prototype.includes.call({}) === false); - assert(Array.prototype.includes.call({ 0: undefined }) === false); - assert(Array.prototype.includes.call({ length: 1, 0: undefined }) === true); - assert(Array.prototype.includes.call({ length: 1, 2: "foo" }, "foo") === false); - assert(Array.prototype.includes.call({ length: 5, 2: "foo" }, "foo") === true); - } + test("includes", () => { + expect(Array.prototype.includes.call({})).toBeFalse(); + expect(Array.prototype.includes.call({ 0: undefined })).toBeFalse(); + expect(Array.prototype.includes.call({ length: 1, 0: undefined })).toBeTrue(); + expect(Array.prototype.includes.call({ length: 1, 2: "foo" }, "foo")).toBeFalse(); + expect(Array.prototype.includes.call({ length: 5, 2: "foo" }, "foo")).toBeTrue(); + }); const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" }; - { - assertVisitsAll( - visit => { - Array.prototype.every.call(o, function (value) { - visit(value); - return true; - }); - }, - ["foo", "bar", "baz"] - ); - } - - ["find", "findIndex"].forEach(name => { - assertVisitsAll( - visit => { - Array.prototype[name].call(o, function (value) { - visit(value); - return false; - }); - }, - ["foo", "bar", undefined, "baz", undefined] - ); + test("every", () => { + const visited = []; + Array.prototype.every.call(o, value => { + visited.push(value); + return true; + }); + expect(visited).toEqual(["foo", "bar", "baz"]); }); - ["filter", "forEach", "map", "some"].forEach(name => { - assertVisitsAll( - visit => { - Array.prototype[name].call(o, function (value) { - visit(value); - return false; - }); - }, - ["foo", "bar", "baz"] - ); + test("find, findIndex", () => { + ["find", "findIndex"].forEach(name => { + const visited = []; + Array.prototype[name].call(o, value => { + visited.push(value); + return false; + }); + expect(visited).toEqual(["foo", "bar", undefined, "baz", undefined]); + }); }); - { - assertVisitsAll( - visit => { - Array.prototype.reduce.call( - o, - function (_, value) { - visit(value); - return false; - }, - "initial" - ); - }, - ["foo", "bar", "baz"] - ); - } - { - assertVisitsAll( - visit => { - Array.prototype.reduceRight.call( - o, - function (_, value) { - visit(value); - return false; - }, - "initial" - ); - }, - ["baz", "bar", "foo"] - ); - } + test("filter, forEach, map, some", () => { + ["filter", "forEach", "map", "some"].forEach(name => { + const visited = []; + Array.prototype[name].call(o, value => { + visited.push(value); + return false; + }); + expect(visited).toEqual(["foo", "bar", "baz"]); + }); + }); - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + test("reduce", () => { + const visited = []; + Array.prototype.reduce.call( + o, + (_, value) => { + visited.push(value); + return false; + }, + "initial" + ); + expect(visited).toEqual(["foo", "bar", "baz"]); + }); + + test("reduceRight", () => { + const visited = []; + Array.prototype.reduceRight.call( + o, + (_, value) => { + visited.push(value); + return false; + }, + "initial" + ); + expect(visited).toEqual(["baz", "bar", "foo"]); + }); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.concat.js b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.concat.js index 4b44972094..c19b0ec13f 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.concat.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.concat.js @@ -1,36 +1,43 @@ -load("test-common.js"); +test("length is 1", () => { + expect(Array.prototype.concat).toHaveLength(1); +}); -try { - assert(Array.prototype.concat.length === 1); +describe("normal behavior", () => { + var array = ["hello"]; - var array = ["hello", "friends"]; + test("no arguments", () => { + var concatenated = array.concat(); + expect(array).toHaveLength(1); + expect(concatenated).toHaveLength(1); + }); - var array_concat = array.concat(); - assert(array_concat.length === array.length); + test("single argument", () => { + var concatenated = array.concat("friends"); + expect(array).toHaveLength(1); + expect(concatenated).toHaveLength(2); + expect(concatenated[0]).toBe("hello"); + expect(concatenated[1]).toBe("friends"); + }); - array_concat = array.concat(1); - assert(array_concat.length === 3); - assert(array_concat[2] === 1); + test("single array argument", () => { + var concatenated = array.concat([1, 2, 3]); + expect(array).toHaveLength(1); + expect(concatenated).toHaveLength(4); + expect(concatenated[0]).toBe("hello"); + expect(concatenated[1]).toBe(1); + expect(concatenated[2]).toBe(2); + expect(concatenated[3]).toBe(3); + }); - array_concat = array.concat([1, 2, 3]); - assert(array_concat.length === 5); - assert(array_concat[2] === 1); - assert(array_concat[3] === 2); - assert(array_concat[4] === 3); - - array_concat = array.concat(false, "serenity"); - assert(array_concat.length === 4); - assert(array_concat[2] === false); - assert(array_concat[3] === "serenity"); - - array_concat = array.concat({ name: "libjs" }, [1, [2, 3]]); - assert(array_concat.length === 5); - assert(array_concat[2].name === "libjs"); - assert(array_concat[3] === 1); - assert(array_concat[4][0] === 2); - assert(array_concat[4][1] === 3); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + test("multiple arguments", () => { + var concatenated = array.concat(false, "serenity", { name: "libjs" }, [1, [2, 3]]); + expect(array).toHaveLength(1); + expect(concatenated).toHaveLength(6); + expect(concatenated[0]).toBe("hello"); + expect(concatenated[1]).toBeFalse(); + expect(concatenated[2]).toBe("serenity"); + expect(concatenated[3]).toEqual({ name: "libjs" }); + expect(concatenated[4]).toBe(1); + expect(concatenated[5]).toEqual([2, 3]); + }); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.every.js b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.every.js index 5de04d8db0..0215f5b244 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.every.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.every.js @@ -1,49 +1,55 @@ -load("test-common.js"); +test("length is 1", () => { + expect(Array.prototype.every).toHaveLength(1); +}); -try { - assert(Array.prototype.every.length === 1); +describe("errors", () => { + test("requires at least one argument", () => { + expect(() => { + [].every(); + }).toThrowWithMessage(TypeError, "Array.prototype.every() requires at least one argument"); + }); - assertThrowsError( - () => { + test("callback must be a function", () => { + expect(() => { [].every(undefined); - }, - { - error: TypeError, - message: "undefined is not a function", - } - ); + }).toThrowWithMessage(TypeError, "undefined is not a function"); + }); +}); - var arrayOne = ["serenity", { test: "serenity" }]; - var arrayTwo = [true, false, 1, 2, 3, "3"]; +describe("normal behavior", () => { + test("basic functionality", () => { + var arrayOne = ["serenity", { test: "serenity" }]; + var arrayTwo = [true, false, 1, 2, 3, "3"]; - assert(arrayOne.every(value => value === "hello") === false); - assert(arrayOne.every(value => value === "serenity") === false); - assert(arrayOne.every((value, index, arr) => index < 2) === true); - assert(arrayOne.every(value => typeof value === "string") === false); - assert(arrayOne.every(value => arrayOne.pop()) === true); + expect(arrayOne.every(value => value === "hello")).toBeFalse(); + expect(arrayOne.every(value => value === "serenity")).toBeFalse(); + expect(arrayOne.every((value, index, arr) => index < 2)).toBeTrue(); + expect(arrayOne.every(value => typeof value === "string")).toBeFalse(); + expect(arrayOne.every(value => arrayOne.pop())).toBeTrue(); - assert(arrayTwo.every((value, index, arr) => index > 0) === false); - assert(arrayTwo.every((value, index, arr) => index >= 0) === true); - assert(arrayTwo.every(value => typeof value !== "string") === false); - assert(arrayTwo.every(value => typeof value === "number") === false); - assert(arrayTwo.every(value => value > 0) === false); - assert(arrayTwo.every(value => value >= 0 && value < 4) === true); - assert(arrayTwo.every(value => arrayTwo.pop()) === true); + expect(arrayTwo.every((value, index, arr) => index > 0)).toBeFalse(); + expect(arrayTwo.every((value, index, arr) => index >= 0)).toBeTrue(); + expect(arrayTwo.every(value => typeof value !== "string")).toBeFalse(); + expect(arrayTwo.every(value => typeof value === "number")).toBeFalse(); + expect(arrayTwo.every(value => value > 0)).toBeFalse(); + expect(arrayTwo.every(value => value >= 0 && value < 4)).toBeTrue(); + expect(arrayTwo.every(value => arrayTwo.pop())).toBeTrue(); - assert(["", "hello", "friends", "serenity"].every(value => value.length >= 0) === true); - assert([].every(value => value === 1) === true); + expect(["", "hello", "friends", "serenity"].every(value => value.length >= 0)).toBeTrue(); + }); - arrayTwo = [true, false, 1, 2, 3, "3"]; + test("empty array", () => { + expect([].every(value => value === 1)).toBeTrue(); + }); - // Every only goes up to the original length. - assert( - arrayTwo.every((value, index, arr) => { - arr.push("serenity"); - return value < 4; - }) === true - ); + test("elements past the initial array size are ignored", () => { + var array = [1, 2, 3, 4, 5]; - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + expect( + arrayTwo.every((value, index, arr) => { + arr.push(6); + return value <= 5; + }) + ).toBeTrue(); + }); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.fill.js b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.fill.js index 80705bbde3..2773ec42cb 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.fill.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.fill.js @@ -1,23 +1,20 @@ -load("test-common.js"); - -try { - assert(Array.prototype.fill.length === 1); +test("length is 1", () => { + expect(Array.prototype.fill).toHaveLength(1); +}); +test("basic functionality", () => { var array = [1, 2, 3, 4]; - assertArrayEquals(array.fill(0, 2, 4), [1, 2, 0, 0]); - assertArrayEquals(array.fill(5, 1), [1, 5, 5, 5]); - assertArrayEquals(array.fill(6), [6, 6, 6, 6]); - assertArrayEquals([1, 2, 3].fill(4), [4, 4, 4]); - assertArrayEquals([1, 2, 3].fill(4, 1), [1, 4, 4]); - assertArrayEquals([1, 2, 3].fill(4, 1, 2), [1, 4, 3]); - assertArrayEquals([1, 2, 3].fill(4, 3, 3), [1, 2, 3]); - assertArrayEquals([1, 2, 3].fill(4, -3, -2), [4, 2, 3]); - assertArrayEquals([1, 2, 3].fill(4, NaN, NaN), [1, 2, 3]); - assertArrayEquals([1, 2, 3].fill(4, 3, 5), [1, 2, 3]); - assertArrayEquals(Array(3).fill(4), [4, 4, 4]); + expect(array.fill(0, 2, 4)).toEqual([1, 2, 0, 0]); + expect(array.fill(5, 1)).toEqual([1, 5, 5, 5]); + expect(array.fill(6)).toEqual([6, 6, 6, 6]); - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + expect([1, 2, 3].fill(4)).toEqual([4, 4, 4]); + expect([1, 2, 3].fill(4, 1)).toEqual([1, 4, 4]); + expect([1, 2, 3].fill(4, 1, 2)).toEqual([1, 4, 3]); + expect([1, 2, 3].fill(4, 3, 3)).toEqual([1, 2, 3]); + expect([1, 2, 3].fill(4, -3, -2)).toEqual([4, 2, 3]); + expect([1, 2, 3].fill(4, NaN, NaN)).toEqual([1, 2, 3]); + expect([1, 2, 3].fill(4, 3, 5)).toEqual([1, 2, 3]); + expect(Array(3).fill(4)).toEqual([4, 4, 4]); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.filter.js b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.filter.js index 176434b137..6f481cddb8 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.filter.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.filter.js @@ -1,78 +1,68 @@ -load("test-common.js"); +test("length is 1", () => { + expect(Array.prototype.filter).toHaveLength(1); +}); -try { - assert(Array.prototype.filter.length === 1); - - assertThrowsError( - () => { +describe("errors", () => { + test("requires at least one argument", () => { + expect(() => { [].filter(); - }, - { - error: TypeError, - message: "Array.prototype.filter() requires at least one argument", - } - ); + }).toThrowWithMessage(TypeError, "Array.prototype.filter() requires at least one argument"); + }); - assertThrowsError( - () => { + test("callback must be a function", () => { + expect(() => { [].filter(undefined); - }, - { - error: TypeError, - message: "undefined is not a function", - } - ); + }).toThrowWithMessage(TypeError, "undefined is not a function"); + }); +}); - var callbackCalled = 0; - var callback = () => { - callbackCalled++; - }; +describe("normal behavior", () => { + test("never calls callback with empty array", () => { + var callbackCalled = 0; + expect( + [].filter(() => { + callbackCalled++; + }) + ).toEqual([]); + expect(callbackCalled).toBe(0); + }); - assert([].filter(callback).length === 0); - assert(callbackCalled === 0); + test("calls callback once for every item", () => { + var callbackCalled = 0; + expect( + [1, 2, 3].filter(() => { + callbackCalled++; + }) + ).toEqual([]); + expect(callbackCalled).toBe(3); + }); - assert([1, 2, 3].filter(callback).length === 0); - assert(callbackCalled === 3); + test("can filter based on callback return value", () => { + var evenNumbers = [0, 1, 2, 3, 4, 5, 6, 7].filter(x => x % 2 === 0); + expect(evenNumbers).toEqual([0, 2, 4, 6]); - var evenNumbers = [0, 1, 2, 3, 4, 5, 6, 7].filter(x => x % 2 === 0); - assert(evenNumbers.length === 4); - assert(evenNumbers[0] === 0); - assert(evenNumbers[1] === 2); - assert(evenNumbers[2] === 4); - assert(evenNumbers[3] === 6); - - var fruits = [ - "Apple", - "Banana", - "Blueberry", - "Grape", - "Mango", - "Orange", - "Peach", - "Pineapple", - "Raspberry", - "Watermelon", - ]; - const filterItems = (arr, query) => { - return arr.filter(el => el.toLowerCase().indexOf(query.toLowerCase()) !== -1); - }; - - var results; - - results = filterItems(fruits, "Berry"); - assert(results.length === 2); - assert(results[0] === "Blueberry"); - assert(results[1] === "Raspberry"); - - results = filterItems(fruits, "P"); - assert(results.length === 5); - assert(results[0] === "Apple"); - assert(results[1] === "Grape"); - assert(results[2] === "Peach"); - assert(results[3] === "Pineapple"); - assert(results[4] === "Raspberry"); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + var fruits = [ + "Apple", + "Banana", + "Blueberry", + "Grape", + "Mango", + "Orange", + "Peach", + "Pineapple", + "Raspberry", + "Watermelon", + ]; + const filterItems = (arr, query) => { + return arr.filter(el => el.toLowerCase().indexOf(query.toLowerCase()) !== -1); + }; + expect(filterItems(fruits, "Berry")).toEqual(["Blueberry", "Raspberry"]); + expect(filterItems(fruits, "P")).toEqual([ + "Apple", + "Grape", + "Peach", + "Pineapple", + "Raspberry", + ]); + }); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.find.js b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.find.js index 17c46b22b2..4d12b61a11 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.find.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.find.js @@ -1,54 +1,65 @@ -load("test-common.js"); +test("length is 1", () => { + expect(Array.prototype.find).toHaveLength(1); +}); -try { - assert(Array.prototype.find.length === 1); - - assertThrowsError( - () => { - [].find(undefined); - }, - { - error: TypeError, - message: "undefined is not a function", - } - ); - - var array = ["hello", "friends", 1, 2, false]; - - assert(array.find(value => value === "hello") === "hello"); - assert(array.find((value, index, arr) => index === 1) === "friends"); - assert(array.find(value => value == "1") === 1); - assert(array.find(value => value === 1) === 1); - assert(array.find(value => typeof value !== "string") === 1); - assert(array.find(value => typeof value === "boolean") === false); - assert(array.find(value => value > 1) === 2); - assert(array.find(value => value > 1 && value < 3) === 2); - assert(array.find(value => value > 100) === undefined); - assert([].find(value => value === 1) === undefined); - - var callbackCalled = 0; - var callback = () => { - callbackCalled++; - }; - - [].find(callback); - assert(callbackCalled === 0); - - [1, 2, 3].find(callback); - assert(callbackCalled === 3); - - callbackCalled = 0; - [1, , , "foo", , undefined, , ,].find(callback); - assert(callbackCalled === 8); - - callbackCalled = 0; - [1, , , "foo", , undefined, , ,].find(value => { - callbackCalled++; - return value === undefined; +describe("errors", () => { + test("requires at least one argument", () => { + expect(() => { + [].find(); + }).toThrowWithMessage(TypeError, "Array.prototype.find() requires at least one argument"); }); - assert(callbackCalled === 2); - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + test("callback must be a function", () => { + expect(() => { + [].find(undefined); + }).toThrowWithMessage(TypeError, "undefined is not a function"); + }); +}); + +describe("normal behavior", () => { + test("basic functionality", () => { + var array = ["hello", "friends", 1, 2, false]; + + expect(array.find(value => value === "hello")).toBe("hello"); + expect(array.find((value, index, arr) => index === 1)).toBe("friends"); + expect(array.find(value => value == "1")).toBe(1); + expect(array.find(value => value === 1)).toBe(1); + expect(array.find(value => typeof value !== "string")).toBe(1); + expect(array.find(value => typeof value === "boolean")).toBeFalse(); + expect(array.find(value => value > 1)).toBe(2); + expect(array.find(value => value > 1 && value < 3)).toBe(2); + expect(array.find(value => value > 100)).toBeUndefined(); + expect([].find(value => value === 1)).toBeUndefined(); + }); + + test("never calls callback with empty array", () => { + var callbackCalled = 0; + expect( + [].find(() => { + callbackCalled++; + }) + ).toBeUndefined(); + expect(callbackCalled).toBe(0); + }); + + test("calls callback once for every item", () => { + var callbackCalled = 0; + expect( + [1, 2, 3].find(() => { + callbackCalled++; + }) + ).toBeUndefined(); + expect(callbackCalled).toBe(3); + }); + + test("empty slots are treated as undefined", () => { + var callbackCalled = 0; + expect( + [1, , , "foo", , undefined, , ,].find(value => { + callbackCalled++; + return value === undefined; + }) + ).toBeUndefined(); + expect(callbackCalled).toBe(2); + }); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.findIndex.js b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.findIndex.js index 1653edb67b..751caa64be 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.findIndex.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.findIndex.js @@ -1,54 +1,68 @@ -load("test-common.js"); +test("length is 1", () => { + expect(Array.prototype.findIndex).toHaveLength(1); +}); -try { - assert(Array.prototype.findIndex.length === 1); - - assertThrowsError( - () => { - [].findIndex(undefined); - }, - { - error: TypeError, - message: "undefined is not a function", - } - ); - - var array = ["hello", "friends", 1, 2, false]; - - assert(array.findIndex(value => value === "hello") === 0); - assert(array.findIndex((value, index, arr) => index === 1) === 1); - assert(array.findIndex(value => value == "1") === 2); - assert(array.findIndex(value => value === 1) === 2); - assert(array.findIndex(value => typeof value !== "string") === 2); - assert(array.findIndex(value => typeof value === "boolean") === 4); - assert(array.findIndex(value => value > 1) === 3); - assert(array.findIndex(value => value > 1 && value < 3) === 3); - assert(array.findIndex(value => value > 100) === -1); - assert([].findIndex(value => value === 1) === -1); - - var callbackCalled = 0; - var callback = () => { - callbackCalled++; - }; - - [].findIndex(callback); - assert(callbackCalled === 0); - - [1, 2, 3].findIndex(callback); - assert(callbackCalled === 3); - - callbackCalled = 0; - [1, , , "foo", , undefined, , ,].findIndex(callback); - assert(callbackCalled === 8); - - callbackCalled = 0; - [1, , , "foo", , undefined, , ,].findIndex(value => { - callbackCalled++; - return value === undefined; +describe("errors", () => { + test("requires at least one argument", () => { + expect(() => { + [].findIndex(); + }).toThrowWithMessage( + TypeError, + "Array.prototype.findIndex() requires at least one argument" + ); }); - assert(callbackCalled === 2); - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + test("callback must be a function", () => { + expect(() => { + [].findIndex(undefined); + }).toThrowWithMessage(TypeError, "undefined is not a function"); + }); +}); + +describe("normal behavior", () => { + test("basic functionality", () => { + var array = ["hello", "friends", 1, 2, false]; + + expect(array.findIndex(value => value === "hello")).toBe(0); + expect(array.findIndex((value, index, arr) => index === 1)).toBe(1); + expect(array.findIndex(value => value == "1")).toBe(2); + expect(array.findIndex(value => value === 1)).toBe(2); + expect(array.findIndex(value => typeof value !== "string")).toBe(2); + expect(array.findIndex(value => typeof value === "boolean")).toBe(4); + expect(array.findIndex(value => value > 1)).toBe(3); + expect(array.findIndex(value => value > 1 && value < 3)).toBe(3); + expect(array.findIndex(value => value > 100)).toBe(-1); + expect([].findIndex(value => value === 1)).toBe(-1); + }); + + test("never calls callback with empty array", () => { + var callbackCalled = 0; + expect( + [].findIndex(() => { + callbackCalled++; + }) + ).toBe(-1); + expect(callbackCalled).toBe(0); + }); + + test("calls callback once for every item", () => { + var callbackCalled = 0; + expect( + [1, 2, 3].findIndex(() => { + callbackCalled++; + }) + ).toBe(-1); + expect(callbackCalled).toBe(3); + }); + + test("empty slots are treated as undefined", () => { + var callbackCalled = 0; + expect( + [1, , , "foo", , undefined, , ,].findIndex(value => { + callbackCalled++; + return value === undefined; + }) + ).toBe(1); + expect(callbackCalled).toBe(2); + }); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.forEach.js b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.forEach.js index 7d9d605693..bc4b5b9336 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.forEach.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.forEach.js @@ -1,70 +1,70 @@ -load("test-common.js"); +test("length is 1", () => { + expect(Array.prototype.forEach).toHaveLength(1); +}); -try { - assert(Array.prototype.forEach.length === 1); - - assertThrowsError( - () => { +describe("errors", () => { + test("requires at least one argument", () => { + expect(() => { [].forEach(); - }, - { - error: TypeError, - message: "Array.prototype.forEach() requires at least one argument", - } - ); + }).toThrowWithMessage( + TypeError, + "Array.prototype.forEach() requires at least one argument" + ); + }); - assertThrowsError( - () => { + test("callback must be a function", () => { + expect(() => { [].forEach(undefined); - }, - { - error: TypeError, - message: "undefined is not a function", - } - ); + }).toThrowWithMessage(TypeError, "undefined is not a function"); + }); +}); - var a = [1, 2, 3]; - var o = {}; - var callbackCalled = 0; - var callback = () => { - callbackCalled++; - }; - - assert([].forEach(callback) === undefined); - assert(callbackCalled === 0); - - assert(a.forEach(callback) === undefined); - assert(callbackCalled === 3); - - callbackCalled = 0; - a.forEach(function (value, index) { - assert(value === a[index]); - assert(index === a[index] - 1); +describe("normal behavior", () => { + test("never calls callback with empty array", () => { + var callbackCalled = 0; + expect( + [].forEach(() => { + callbackCalled++; + }) + ).toBeUndefined(); + expect(callbackCalled).toBe(0); }); - callbackCalled = 0; - a.forEach(function (_, _, array) { - callbackCalled++; - assert(a.length === array.length); - a.push("test"); + test("calls callback once for every item", () => { + var callbackCalled = 0; + expect( + [1, 2, 3].forEach(() => { + callbackCalled++; + }) + ).toBeUndefined(); + expect(callbackCalled).toBe(3); }); - assert(callbackCalled === 3); - assert(a.length === 6); - callbackCalled = 0; - a.forEach(function (value, index) { - callbackCalled++; - this[index] = value; - }, o); - assert(callbackCalled === 6); - assert(o[0] === 1); - assert(o[1] === 2); - assert(o[2] === 3); - assert(o[3] === "test"); - assert(o[4] === "test"); - assert(o[5] === "test"); + test("callback receives value and index", () => { + var a = [1, 2, 3]; + a.forEach((value, index) => { + expect(value).toBe(a[index]); + expect(index).toBe(a[index] - 1); + }); + }); - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + test("callback receives array", () => { + var callbackCalled = 0; + var a = [1, 2, 3]; + a.forEach((_, _, array) => { + callbackCalled++; + expect(a).toEqual(array); + a.push("test"); + }); + expect(callbackCalled).toBe(3); + expect(a).toEqual([1, 2, 3, "test", "test", "test"]); + }); + + test("this value can be modified", () => { + var t = []; + [1, 2, 3].forEach(function (value) { + this.push(value); + }, t); + expect(t).toEqual([1, 2, 3]); + }); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.includes.js b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.includes.js index 1a98212716..575cd84e1b 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.includes.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.includes.js @@ -1,22 +1,18 @@ -load("test-common.js"); - -try { - assert(Array.prototype.includes.length === 1); +test("length is 1", () => { + expect(Array.prototype.includes).toHaveLength(1); +}); +test("basic functionality", () => { var array = ["hello", "friends", 1, 2, false]; - assert([].includes() === false); - assert([undefined].includes() === true); - assert(array.includes("hello") === true); - assert(array.includes(1) === true); - assert(array.includes(1, -3) === true); - assert(array.includes("serenity") === false); - assert(array.includes(false, -1) === true); - assert(array.includes(2, -1) === false); - assert(array.includes(2, -100) === true); - assert(array.includes("friends", 100) === false); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + expect([].includes()).toBeFalse(); + expect([undefined].includes()).toBeTrue(); + expect(array.includes("hello")).toBeTrue(); + expect(array.includes(1)).toBeTrue(); + expect(array.includes(1, -3)).toBeTrue(); + expect(array.includes("serenity")).toBeFalse(); + expect(array.includes(false, -1)).toBeTrue(); + expect(array.includes(2, -1)).toBeFalse(); + expect(array.includes(2, -100)).toBeTrue(); + expect(array.includes("friends", 100)).toBeFalse(); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.indexOf.js b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.indexOf.js index 51fa66484f..74caab93ee 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.indexOf.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.indexOf.js @@ -1,29 +1,25 @@ -load("test-common.js"); - -try { - assert(Array.prototype.indexOf.length === 1); +test("length is 1", () => { + expect(Array.prototype.indexOf).toHaveLength(1); +}); +test("basic functionality", () => { var array = ["hello", "friends", 1, 2, false]; - assert(array.indexOf("hello") === 0); - assert(array.indexOf("friends") === 1); - assert(array.indexOf(false) === 4); - assert(array.indexOf(false, 2) === 4); - assert(array.indexOf(false, -2) === 4); - assert(array.indexOf(1) === 2); - assert(array.indexOf(1, 1000) === -1); - assert(array.indexOf(1, -1000) === 2); - assert(array.indexOf("serenity") === -1); - assert(array.indexOf(false, -1) === 4); - assert(array.indexOf(2, -1) === -1); - assert(array.indexOf(2, -2) === 3); - assert([].indexOf("serenity") === -1); - assert([].indexOf("serenity", 10) === -1); - assert([].indexOf("serenity", -10) === -1); - assert([].indexOf() === -1); - assert([undefined].indexOf() === 0); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + expect(array.indexOf("hello")).toBe(0); + expect(array.indexOf("friends")).toBe(1); + expect(array.indexOf(false)).toBe(4); + expect(array.indexOf(false, 2)).toBe(4); + expect(array.indexOf(false, -2)).toBe(4); + expect(array.indexOf(1)).toBe(2); + expect(array.indexOf(1, 1000)).toBe(-1); + expect(array.indexOf(1, -1000)).toBe(2); + expect(array.indexOf("serenity")).toBe(-1); + expect(array.indexOf(false, -1)).toBe(4); + expect(array.indexOf(2, -1)).toBe(-1); + expect(array.indexOf(2, -2)).toBe(3); + expect([].indexOf("serenity")).toBe(-1); + expect([].indexOf("serenity", 10)).toBe(-1); + expect([].indexOf("serenity", -10)).toBe(-1); + expect([].indexOf()).toBe(-1); + expect([undefined].indexOf()).toBe(0); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.join.js b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.join.js index fc82deb20c..4bf661ea70 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.join.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.join.js @@ -1,19 +1,15 @@ -load("test-common.js"); +test("length is 1", () => { + expect(Array.prototype.join).toHaveLength(1); +}); -try { - assert(Array.prototype.join.length === 1); - - assert(["hello", "friends"].join() === "hello,friends"); - assert(["hello", "friends"].join(" ") === "hello friends"); - assert(["hello", "friends", "foo"].join("~", "#") === "hello~friends~foo"); - assert([].join() === ""); - assert([null].join() === ""); - assert([undefined].join() === ""); - assert([undefined, null, ""].join() === ",,"); - assert([1, null, 2, undefined, 3].join() === "1,,2,,3"); - assert(Array(3).join() === ",,"); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} +test("basic functionality", () => { + expect(["hello", "friends"].join()).toBe("hello,friends"); + expect(["hello", "friends"].join(" ")).toBe("hello friends"); + expect(["hello", "friends", "foo"].join("~", "#")).toBe("hello~friends~foo"); + expect([].join()).toBe(""); + expect([null].join()).toBe(""); + expect([undefined].join()).toBe(""); + expect([undefined, null, ""].join()).toBe(",,"); + expect([1, null, 2, undefined, 3].join()).toBe("1,,2,,3"); + expect(Array(3).join()).toBe(",,"); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.lastIndexOf.js b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.lastIndexOf.js index f5153b8140..eb0f7c6b37 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.lastIndexOf.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.lastIndexOf.js @@ -1,26 +1,22 @@ -load("test-common.js"); - -try { - assert(Array.prototype.lastIndexOf.length === 1); +test("length is 1", () => { + expect(Array.prototype.lastIndexOf).toHaveLength(1); +}); +test("basic functionality", () => { var array = [1, 2, 3, 1, "hello"]; - assert(array.lastIndexOf("hello") === 4); - assert(array.lastIndexOf("hello", 1000) === 4); - assert(array.lastIndexOf(1) === 3); - assert(array.lastIndexOf(1, -1) === 3); - assert(array.lastIndexOf(1, -2) === 3); - assert(array.lastIndexOf(2) === 1); - assert(array.lastIndexOf(2, -3) === 1); - assert(array.lastIndexOf(2, -4) === 1); - assert([].lastIndexOf("hello") === -1); - assert([].lastIndexOf("hello", 10) === -1); - assert([].lastIndexOf("hello", -10) === -1); - assert([].lastIndexOf() === -1); - assert([undefined].lastIndexOf() === 0); - assert([undefined, undefined, undefined].lastIndexOf() === 2); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + expect(array.lastIndexOf("hello")).toBe(4); + expect(array.lastIndexOf("hello", 1000)).toBe(4); + expect(array.lastIndexOf(1)).toBe(3); + expect(array.lastIndexOf(1, -1)).toBe(3); + expect(array.lastIndexOf(1, -2)).toBe(3); + expect(array.lastIndexOf(2)).toBe(1); + expect(array.lastIndexOf(2, -3)).toBe(1); + expect(array.lastIndexOf(2, -4)).toBe(1); + expect([].lastIndexOf("hello")).toBe(-1); + expect([].lastIndexOf("hello", 10)).toBe(-1); + expect([].lastIndexOf("hello", -10)).toBe(-1); + expect([].lastIndexOf()).toBe(-1); + expect([undefined].lastIndexOf()).toBe(0); + expect([undefined, undefined, undefined].lastIndexOf()).toBe(2); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.map.js b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.map.js index 344ac2ea86..a775fa8c32 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.map.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.map.js @@ -1,63 +1,57 @@ -load("test-common.js"); +test("length is 1", () => { + expect(Array.prototype.map).toHaveLength(1); +}); -try { - assert(Array.prototype.map.length === 1); - - assertThrowsError( - () => { +describe("errors", () => { + test("requires at least one argument", () => { + expect(() => { [].map(); - }, - { - error: TypeError, - message: "Array.prototype.map() requires at least one argument", - } - ); + }).toThrowWithMessage(TypeError, "Array.prototype.map() requires at least one argument"); + }); - assertThrowsError( - () => { + test("callback must be a function", () => { + expect(() => { [].map(undefined); - }, - { - error: TypeError, - message: "undefined is not a function", - } - ); + }).toThrowWithMessage(TypeError, "undefined is not a function"); + }); +}); - var callbackCalled = 0; - var callback = () => { - callbackCalled++; - }; +describe("normal behavior", () => { + test("never calls callback with empty array", () => { + var callbackCalled = 0; + expect( + [].map(() => { + callbackCalled++; + }) + ).toEqual([]); + expect(callbackCalled).toBe(0); + }); - assert([].map(callback).length === 0); - assert(callbackCalled === 0); + test("calls callback once for every item", () => { + var callbackCalled = 0; + expect( + [1, 2, 3].map(() => { + callbackCalled++; + }) + ).toEqual([undefined, undefined, undefined]); + expect(callbackCalled).toBe(3); + }); - assert([1, 2, 3].map(callback).length === 3); - assert(callbackCalled === 3); + test("can map based on callback return value", () => { + expect( + [undefined, null, true, "foo", 42, {}].map( + (value, index) => "" + index + " -> " + value + ) + ).toEqual([ + "0 -> undefined", + "1 -> null", + "2 -> true", + "3 -> foo", + "4 -> 42", + "5 -> [object Object]", + ]); - callbackCalled = 0; - assert([1, , , "foo", , undefined, , ,].map(callback).length === 8); - assert(callbackCalled === 3); - - var results = [undefined, null, true, "foo", 42, {}].map( - (value, index) => "" + index + " -> " + value - ); - assert(results.length === 6); - assert(results[0] === "0 -> undefined"); - assert(results[1] === "1 -> null"); - assert(results[2] === "2 -> true"); - assert(results[3] === "3 -> foo"); - assert(results[4] === "4 -> 42"); - assert(results[5] === "5 -> [object Object]"); - - var squaredNumbers = [0, 1, 2, 3, 4].map(x => x ** 2); - assert(squaredNumbers.length === 5); - assert(squaredNumbers[0] === 0); - assert(squaredNumbers[1] === 1); - assert(squaredNumbers[2] === 4); - assert(squaredNumbers[3] === 9); - assert(squaredNumbers[4] === 16); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + var squaredNumbers = [0, 1, 2, 3, 4].map(x => x ** 2); + expect(squaredNumbers).toEqual([0, 1, 4, 9, 16]); + }); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.pop.js b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.pop.js index a4d09a2311..2763159b3f 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.pop.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.pop.js @@ -1,21 +1,23 @@ -load("test-common.js"); +test("length is 0", () => { + expect(Array.prototype.pop).toHaveLength(0); +}); -try { - var a = [1, 2, 3]; - var value = a.pop(); - assert(value === 3); - assert(a.length === 2); - assert(a[0] === 1); - assert(a[1] === 2); +describe("normal behavior", () => { + test("array with elements", () => { + var a = [1, 2, 3]; + expect(a.pop()).toBe(3); + expect(a).toEqual([1, 2]); + }); - var a = []; - var value = a.pop(); - assert(value === undefined); - assert(a.length === 0); + test("empty array", () => { + var a = []; + expect(a.pop()).toBeUndefined(); + expect(a).toEqual([]); + }); - assert([,].pop() === undefined); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + test("array with empty slot", () => { + var a = [,]; + expect(a.pop()).toBeUndefined(); + expect(a).toEqual([]); + }); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.push.js b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.push.js index f739aec463..209cd7a74d 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.push.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.push.js @@ -1,30 +1,23 @@ -load("test-common.js"); +test("length is 1", () => { + expect(Array.prototype.push).toHaveLength(1); +}); -try { - assert(Array.prototype.push.length === 1); +describe("normal behavior", () => { + test("no argument", () => { + var a = ["hello"]; + expect(a.push()).toBe(1); + expect(a).toEqual(["hello"]); + }); - var a = ["hello"]; - var length = a.push(); - assert(length === 1); - assert(a.length === 1); - assert(a[0] === "hello"); + test("single argument", () => { + var a = ["hello"]; + expect(a.push("friends")).toBe(2); + expect(a).toEqual(["hello", "friends"]); + }); - length = a.push("friends"); - assert(length === 2); - assert(a.length === 2); - assert(a[0] === "hello"); - assert(a[1] === "friends"); - - length = a.push(1, 2, 3); - assert(length === 5); - assert(a.length === 5); - assert(a[0] === "hello"); - assert(a[1] === "friends"); - assert(a[2] === 1); - assert(a[3] === 2); - assert(a[4] === 3); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + test("multiple arguments", () => { + var a = ["hello", "friends"]; + expect(a.push(1, 2, 3)).toBe(5); + expect(a).toEqual(["hello", "friends", 1, 2, 3]); + }); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.reduce.js b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.reduce.js index c7afdbfffb..9b984fea03 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.reduce.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.reduce.js @@ -1,132 +1,113 @@ -load("test-common.js"); +test("length is 1", () => { + expect(Array.prototype.reduce).toHaveLength(1); +}); -try { - assert(Array.prototype.reduce.length === 1); +describe("errors", () => { + test("requires at least one argument", () => { + expect(() => { + [].reduce(); + }).toThrowWithMessage(TypeError, "Array.prototype.reduce() requires at least one argument"); + }); - assertThrowsError( - () => { - [1].reduce(); - }, - { - error: TypeError, - message: "Array.prototype.reduce() requires at least one argument", - } - ); + test("callback must be a function", () => { + expect(() => { + [].reduce(undefined); + }).toThrowWithMessage(TypeError, "undefined is not a function"); + }); - assertThrowsError( - () => { - [1].reduce(undefined); - }, - { - error: TypeError, - message: "undefined is not a function", - } - ); - - assertThrowsError( - () => { + test("reduce of empty array with no initial value", () => { + expect(() => { [].reduce((a, x) => x); - }, - { - error: TypeError, - message: "Reduce of empty array with no initial value", - } - ); + }).toThrowWithMessage(TypeError, "Reduce of empty array with no initial value"); + }); - assertThrowsError( - () => { + test("reduce of array with only empty slots and no initial value", () => { + expect(() => { [, ,].reduce((a, x) => x); - }, - { - error: TypeError, - message: "Reduce of empty array with no initial value", - } - ); - - [1, 2].reduce(function () { - assert(this === undefined); + }).toThrowWithMessage(TypeError, "Reduce of empty array with no initial value"); }); +}); - var callbackCalled = 0; - var callback = () => { - callbackCalled++; - return true; - }; +describe("normal behavior", () => { + test("basic functionality", () => { + [1, 2].reduce(function () { + expect(this).toBeUndefined(); + }); - assert([1].reduce(callback) === 1); - assert(callbackCalled === 0); + var callbackCalled = 0; + var callback = () => { + callbackCalled++; + return true; + }; - assert([, 1].reduce(callback) === 1); - assert(callbackCalled === 0); + expect([1].reduce(callback)).toBe(1); + expect(callbackCalled).toBe(0); - callbackCalled = 0; - assert([1, 2, 3].reduce(callback) === true); - assert(callbackCalled === 2); + expect([, 1].reduce(callback)).toBe(1); + expect(callbackCalled).toBe(0); - callbackCalled = 0; - assert([, , 1, 2, 3].reduce(callback) === true); - assert(callbackCalled === 2); + callbackCalled = 0; + expect([1, 2, 3].reduce(callback)).toBeTrue(); + expect(callbackCalled).toBe(2); - callbackCalled = 0; - assert([1, , , 10, , 100, , ,].reduce(callback) === true); - assert(callbackCalled === 2); + callbackCalled = 0; + expect([, , 1, 2, 3].reduce(callback)).toBeTrue(); + expect(callbackCalled).toBe(2); - var constantlySad = () => ":^("; - var result = [].reduce(constantlySad, ":^)"); - assert(result === ":^)"); + callbackCalled = 0; + expect([1, , , 10, , 100, , ,].reduce(callback)).toBeTrue(); + expect(callbackCalled).toBe(2); - result = [":^0"].reduce(constantlySad, ":^)"); - assert(result === ":^("); + var constantlySad = () => ":^("; + var result = [].reduce(constantlySad, ":^)"); + expect(result).toBe(":^)"); - result = [":^0"].reduce(constantlySad); - assert(result === ":^0"); + result = [":^0"].reduce(constantlySad, ":^)"); + expect(result).toBe(":^("); - result = [5, 4, 3, 2, 1].reduce((accum, elem) => accum + elem); - assert(result === 15); + result = [":^0"].reduce(constantlySad); + expect(result).toBe(":^0"); - result = [1, 2, 3, 4, 5, 6].reduce((accum, elem) => accum + elem, 100); - assert(result === 121); + result = [5, 4, 3, 2, 1].reduce((accum, elem) => accum + elem); + expect(result).toBe(15); - result = [6, 5, 4, 3, 2, 1].reduce((accum, elem) => { - return accum + elem; - }, 100); - assert(result === 121); + result = [1, 2, 3, 4, 5, 6].reduce((accum, elem) => accum + elem, 100); + expect(result).toBe(121); - var indexes = []; - result = ["foo", 1, true].reduce((a, v, i) => { - indexes.push(i); + result = [6, 5, 4, 3, 2, 1].reduce((accum, elem) => { + return accum + elem; + }, 100); + expect(result).toBe(121); + + var indexes = []; + result = ["foo", 1, true].reduce((a, v, i) => { + indexes.push(i); + }); + expect(result).toBeUndefined(); + expect(indexes.length).toBe(2); + expect(indexes[0]).toBe(1); + expect(indexes[1]).toBe(2); + + indexes = []; + result = ["foo", 1, true].reduce((a, v, i) => { + indexes.push(i); + }, "foo"); + expect(result).toBeUndefined(); + expect(indexes).toEqual([0, 1, 2]); + + var mutable = { prop: 0 }; + result = ["foo", 1, true].reduce((a, v) => { + a.prop = v; + return a; + }, mutable); + expect(result).toBe(mutable); + expect(result.prop).toBeTrue(); + + var a1 = [1, 2]; + var a2 = null; + a1.reduce((a, v, i, t) => { + a2 = t; + }); + expect(a1).toBe(a2); }); - assert(result === undefined); - assert(indexes.length === 2); - assert(indexes[0] === 1); - assert(indexes[1] === 2); - - indexes = []; - result = ["foo", 1, true].reduce((a, v, i) => { - indexes.push(i); - }, "foo"); - assert(result === undefined); - assert(indexes.length === 3); - assert(indexes[0] === 0); - assert(indexes[1] === 1); - assert(indexes[2] === 2); - - var mutable = { prop: 0 }; - result = ["foo", 1, true].reduce((a, v) => { - a.prop = v; - return a; - }, mutable); - assert(result === mutable); - assert(result.prop === true); - - var a1 = [1, 2]; - var a2 = null; - a1.reduce((a, v, i, t) => { - a2 = t; - }); - assert(a1 === a2); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.reduceRight.js b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.reduceRight.js index 8bcc083a1b..3d718a0446 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.reduceRight.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.reduceRight.js @@ -1,134 +1,118 @@ -load("test-common.js"); +test("length is 1", () => { + expect(Array.prototype.reduceRight).toHaveLength(1); +}); -try { - assert(Array.prototype.reduceRight.length === 1); +describe("errors", () => { + test("requires at least one argument", () => { + expect(() => { + [].reduceRight(); + }).toThrowWithMessage( + TypeError, + "Array.prototype.reduceRight() requires at least one argument" + ); + }); - assertThrowsError( - () => { - [1].reduceRight(); - }, - { - error: TypeError, - message: "Array.prototype.reduceRight() requires at least one argument", - } - ); + test("callback must be a function", () => { + expect(() => { + [].reduceRight(undefined); + }).toThrowWithMessage(TypeError, "undefined is not a function"); + }); - assertThrowsError( - () => { - [1].reduceRight(undefined); - }, - { - error: TypeError, - message: "undefined is not a function", - } - ); - - assertThrowsError( - () => { + test("reduce of empty array with no initial value", () => { + expect(() => { [].reduceRight((a, x) => x); - }, - { - error: TypeError, - message: "Reduce of empty array with no initial value", - } - ); + }).toThrowWithMessage(TypeError, "Reduce of empty array with no initial value"); + }); - assertThrowsError( - () => { + test("reduce of array with only empty slots and no initial value", () => { + expect(() => { [, ,].reduceRight((a, x) => x); - }, - { - error: TypeError, - message: "Reduce of empty array with no initial value", - } - ); - - [1, 2].reduceRight(function () { - assert(this === undefined); + }).toThrowWithMessage(TypeError, "Reduce of empty array with no initial value"); }); +}); - var callbackCalled = 0; - var callback = () => { - callbackCalled++; - return true; - }; +describe("normal behavior", () => { + test("basic functionality", () => { + [1, 2].reduceRight(function () { + expect(this).toBeUndefined(); + }); - assert([1].reduceRight(callback) === 1); - assert(callbackCalled === 0); + var callbackCalled = 0; + var callback = () => { + callbackCalled++; + return true; + }; - assert([1].reduceRight(callback) === 1); - assert(callbackCalled === 0); + expect([1].reduceRight(callback)).toBe(1); + expect(callbackCalled).toBe(0); - callbackCalled = 0; - assert([1, 2, 3].reduceRight(callback) === true); - assert(callbackCalled === 2); + expect([1].reduceRight(callback)).toBe(1); + expect(callbackCalled).toBe(0); - callbackCalled = 0; - assert([1, 2, 3, ,].reduceRight(callback) === true); - assert(callbackCalled === 2); + callbackCalled = 0; + expect([1, 2, 3].reduceRight(callback)).toBe(true); + expect(callbackCalled).toBe(2); - callbackCalled = 0; - assert([, , , 1, , , 10, , 100, , ,].reduceRight(callback) === true); - assert(callbackCalled === 2); + callbackCalled = 0; + expect([1, 2, 3, ,].reduceRight(callback)).toBe(true); + expect(callbackCalled).toBe(2); - var constantlySad = () => ":^("; - var result = [].reduceRight(constantlySad, ":^)"); - assert(result === ":^)"); + callbackCalled = 0; + expect([, , , 1, , , 10, , 100, , ,].reduceRight(callback)).toBe(true); + expect(callbackCalled).toBe(2); - result = [":^0"].reduceRight(constantlySad, ":^)"); - assert(result === ":^("); + var constantlySad = () => ":^("; + var result = [].reduceRight(constantlySad, ":^)"); + expect(result).toBe(":^)"); - result = [":^0"].reduceRight(constantlySad); - assert(result === ":^0"); + result = [":^0"].reduceRight(constantlySad, ":^)"); + expect(result).toBe(":^("); - result = [5, 4, 3, 2, 1].reduceRight((accum, elem) => "" + accum + elem); - assert(result === "12345"); + result = [":^0"].reduceRight(constantlySad); + expect(result).toBe(":^0"); - result = [1, 2, 3, 4, 5, 6].reduceRight((accum, elem) => { - return "" + accum + elem; - }, 100); - assert(result === "100654321"); + result = [5, 4, 3, 2, 1].reduceRight((accum, elem) => "" + accum + elem); + expect(result).toBe("12345"); - result = [6, 5, 4, 3, 2, 1].reduceRight((accum, elem) => { - return "" + accum + elem; - }, 100); - assert(result === "100123456"); + result = [1, 2, 3, 4, 5, 6].reduceRight((accum, elem) => { + return "" + accum + elem; + }, 100); + expect(result).toBe("100654321"); - var indexes = []; - result = ["foo", 1, true].reduceRight((a, v, i) => { - indexes.push(i); + result = [6, 5, 4, 3, 2, 1].reduceRight((accum, elem) => { + return "" + accum + elem; + }, 100); + expect(result).toBe("100123456"); + + var indexes = []; + result = ["foo", 1, true].reduceRight((a, v, i) => { + indexes.push(i); + }); + expect(result).toBeUndefined(); + expect(indexes.length).toBe(2); + expect(indexes[0]).toBe(1); + expect(indexes[1]).toBe(0); + + indexes = []; + result = ["foo", 1, true].reduceRight((a, v, i) => { + indexes.push(i); + }, "foo"); + expect(result).toBeUndefined(); + expect(indexes).toEqual([2, 1, 0]); + + var mutable = { prop: 0 }; + result = ["foo", 1, true].reduceRight((a, v) => { + a.prop = v; + return a; + }, mutable); + expect(result).toBe(mutable); + expect(result.prop).toBe("foo"); + + var a1 = [1, 2]; + var a2 = null; + a1.reduceRight((a, v, i, t) => { + a2 = t; + }); + expect(a1).toBe(a2); }); - assert(result === undefined); - assert(indexes.length === 2); - assert(indexes[0] === 1); - assert(indexes[1] === 0); - - indexes = []; - result = ["foo", 1, true].reduceRight((a, v, i) => { - indexes.push(i); - }, "foo"); - assert(result === undefined); - assert(indexes.length === 3); - assert(indexes[0] === 2); - assert(indexes[1] === 1); - assert(indexes[2] === 0); - - var mutable = { prop: 0 }; - result = ["foo", 1, true].reduceRight((a, v) => { - a.prop = v; - return a; - }, mutable); - assert(result === mutable); - assert(result.prop === "foo"); - - var a1 = [1, 2]; - var a2 = null; - a1.reduceRight((a, v, i, t) => { - a2 = t; - }); - assert(a1 === a2); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.reverse.js b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.reverse.js index 16cae9147c..b2b71b15d6 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.reverse.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.reverse.js @@ -1,31 +1,9 @@ -load("test-common.js"); - -try { - assert(Array.prototype.reverse.length === 0); +test("length is 0", () => { + expect(Array.prototype.reverse).toHaveLength(0); +}); +test("basic functionality", () => { var array = [1, 2, 3]; - - assert(array[0] === 1); - assert(array[1] === 2); - assert(array[2] === 3); - - array.reverse(); - - assert(array[0] === 3); - assert(array[1] === 2); - assert(array[2] === 1); - - var array_ref = array.reverse(); - - assert(array_ref[0] === 1); - assert(array_ref[1] === 2); - assert(array_ref[2] === 3); - - assert(array[0] === 1); - assert(array[1] === 2); - assert(array[2] === 3); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + expect(array.reverse()).toEqual([3, 2, 1]); + expect(array).toEqual([3, 2, 1]); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.shift.js b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.shift.js index 2b48e7bf20..0e154eeeb6 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.shift.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.shift.js @@ -1,21 +1,23 @@ -load("test-common.js"); +test("length is 0", () => { + expect(Array.prototype.shift).toHaveLength(0); +}); -try { - var a = [1, 2, 3]; - var value = a.shift(); - assert(value === 1); - assert(a.length === 2); - assert(a[0] === 2); - assert(a[1] === 3); +describe("normal behavior", () => { + test("array with elements", () => { + var a = [1, 2, 3]; + expect(a.shift()).toBe(1); + expect(a).toEqual([2, 3]); + }); - var a = []; - var value = a.shift(); - assert(value === undefined); - assert(a.length === 0); + test("empty array", () => { + var a = []; + expect(a.shift()).toBeUndefined(); + expect(a).toEqual([]); + }); - assert([,].shift() === undefined); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + test("array with empty slot", () => { + var a = [,]; + expect(a.shift()).toBeUndefined(); + expect(a).toEqual([]); + }); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.slice.js b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.slice.js index ca7d087245..c1c45be082 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.slice.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.slice.js @@ -1,53 +1,39 @@ -load("test-common.js"); - -try { - assert(Array.prototype.slice.length === 2); +test("length is 0", () => { + expect(Array.prototype.slice).toHaveLength(2); +}); +test("basic functionality", () => { var array = ["hello", "friends", "serenity", 1]; - var array_slice = array.slice(); - assert(array_slice.length === array.length); - assert(array_slice.length === 4); - assert(array_slice[0] === "hello"); - assert(array_slice[1] === "friends"); - assert(array_slice[2] === "serenity"); - assert(array_slice[3] === 1); + var slice = array.slice(); + expect(array).toEqual(["hello", "friends", "serenity", 1]); + expect(slice).toEqual(["hello", "friends", "serenity", 1]); - array_slice = array.slice(1); - assert(array_slice.length === 3); - assert(array_slice[0] === "friends"); - assert(array_slice[1] === "serenity"); - assert(array_slice[2] === 1); + slice = array.slice(1); + expect(array).toEqual(["hello", "friends", "serenity", 1]); + expect(slice).toEqual(["friends", "serenity", 1]); - array_slice = array.slice(0, 2); - assert(array_slice.length === 2); - assert(array_slice[0] === "hello"); - assert(array_slice[1] === "friends"); + slice = array.slice(0, 2); + expect(array).toEqual(["hello", "friends", "serenity", 1]); + expect(slice).toEqual(["hello", "friends"]); - array_slice = array.slice(-1); - assert(array_slice.length === 1); - assert(array_slice[0] === 1); + slice = array.slice(-1); + expect(array).toEqual(["hello", "friends", "serenity", 1]); + expect(slice).toEqual([1]); - array_slice = array.slice(1, 1); - assert(array_slice.length === 0); + slice = array.slice(1, 1); + expect(array).toEqual(["hello", "friends", "serenity", 1]); + expect(slice).toEqual([]); - array_slice = array.slice(1, -1); - assert(array_slice.length === 2); - assert(array_slice[0] === "friends"); - assert(array_slice[1] === "serenity"); + slice = array.slice(1, -1); + expect(array).toEqual(["hello", "friends", "serenity", 1]); + expect(slice).toEqual(["friends", "serenity"]); - array_slice = array.slice(2, -1); - assert(array_slice.length === 1); - assert(array_slice[0] === "serenity"); + slice = array.slice(2, -1); + expect(array).toEqual(["hello", "friends", "serenity", 1]); + expect(slice).toEqual(["serenity"]); - array_slice = array.slice(0, 100); - assert(array_slice.length === 4); - assert(array_slice[0] === "hello"); - assert(array_slice[1] === "friends"); - assert(array_slice[2] === "serenity"); - assert(array_slice[3] === 1); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + slice = array.slice(0, 100); + expect(array).toEqual(["hello", "friends", "serenity", 1]); + expect(slice).toEqual(["hello", "friends", "serenity", 1]); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.some.js b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.some.js index 9c8c095ec9..ff9e62f2d1 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.some.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.some.js @@ -1,37 +1,37 @@ -load("test-common.js"); +test("length is 1", () => { + expect(Array.prototype.some).toHaveLength(1); +}); -try { - assert(Array.prototype.some.length === 1); +describe("errors", () => { + test("requires at least one argument", () => { + expect(() => { + [].some(); + }).toThrowWithMessage(TypeError, "Array.prototype.some() requires at least one argument"); + }); - assertThrowsError( - () => { + test("callback must be a function", () => { + expect(() => { [].some(undefined); - }, - { - error: TypeError, - message: "undefined is not a function", - } - ); + }).toThrowWithMessage(TypeError, "undefined is not a function"); + }); +}); +test("basic functionality", () => { var array = ["hello", "friends", 1, 2, false, -42, { name: "serenityos" }]; - assert(array.some(value => value === "hello") === true); - assert(array.some(value => value === "serenity") === false); - assert(array.some((value, index, arr) => index === 1) === true); - assert(array.some(value => value == "1") === true); - assert(array.some(value => value === 1) === true); - assert(array.some(value => value === 13) === false); - assert(array.some(value => typeof value !== "string") === true); - assert(array.some(value => typeof value === "boolean") === true); - assert(array.some(value => value > 1) === true); - assert(array.some(value => value > 1 && value < 3) === true); - assert(array.some(value => value > 100) === false); - assert(array.some(value => value < 0) === true); - assert(array.some(value => array.pop()) === true); - assert(["", "hello", "friends", "serenity"].some(value => value.length === 0) === true); - assert([].some(value => value === 1) === false); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + expect(array.some(value => value === "hello")).toBeTrue(); + expect(array.some(value => value === "serenity")).toBeFalse(); + expect(array.some((value, index, arr) => index === 1)).toBeTrue(); + expect(array.some(value => value == "1")).toBeTrue(); + expect(array.some(value => value === 1)).toBeTrue(); + expect(array.some(value => value === 13)).toBeFalse(); + expect(array.some(value => typeof value !== "string")).toBeTrue(); + expect(array.some(value => typeof value === "boolean")).toBeTrue(); + expect(array.some(value => value > 1)).toBeTrue(); + expect(array.some(value => value > 1 && value < 3)).toBeTrue(); + expect(array.some(value => value > 100)).toBeFalse(); + expect(array.some(value => value < 0)).toBeTrue(); + expect(array.some(value => array.pop())).toBeTrue(); + expect(["", "hello", "friends", "serenity"].some(value => value.length === 0)).toBeTrue(); + expect([].some(value => value === 1)).toBeFalse(); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.splice.js b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.splice.js index e140631430..245648760a 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.splice.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.splice.js @@ -1,87 +1,49 @@ -load("test-common.js"); - -try { - assert(Array.prototype.splice.length === 2); +test("length is 2", () => { + expect(Array.prototype.splice).toHaveLength(2); +}); +test("basic functionality", () => { var array = ["hello", "friends", "serenity", 1, 2]; var removed = array.splice(3); - assert(array.length === 3); - assert(array[0] === "hello"); - assert(array[1] === "friends"); - assert(array[2] === "serenity"); - assert(removed.length === 2); - assert(removed[0] === 1); - assert(removed[1] === 2); + expect(array).toEqual(["hello", "friends", "serenity"]); + expect(removed).toEqual([1, 2]); array = ["hello", "friends", "serenity", 1, 2]; removed = array.splice(-2); - assert(array.length === 3); - assert(array[0] === "hello"); - assert(array[1] === "friends"); - assert(array[2] === "serenity"); - assert(removed.length === 2); - assert(removed[0] === 1); - assert(removed[1] === 2); + expect(array).toEqual(["hello", "friends", "serenity"]); + expect(removed).toEqual([1, 2]); array = ["hello", "friends", "serenity", 1, 2]; removed = array.splice(-2, 1); - assert(array.length === 4); - assert(array[0] === "hello"); - assert(array[1] === "friends"); - assert(array[2] === "serenity"); - assert(array[3] === 2); - assert(removed.length === 1); - assert(removed[0] === 1); + expect(array).toEqual(["hello", "friends", "serenity", 2]); + expect(removed).toEqual([1]); array = ["serenity"]; removed = array.splice(0, 0, "hello", "friends"); - assert(array.length === 3); - assert(array[0] === "hello"); - assert(array[1] === "friends"); - assert(array[2] === "serenity"); - assert(removed.length === 0); + expect(array).toEqual(["hello", "friends", "serenity"]); + expect(removed).toEqual([]); array = ["goodbye", "friends", "serenity"]; removed = array.splice(0, 1, "hello"); - assert(array.length === 3); - assert(array[0] === "hello"); - assert(array[1] === "friends"); - assert(array[2] === "serenity"); - assert(removed.length === 1); - assert(removed[0] === "goodbye"); + expect(array).toEqual(["hello", "friends", "serenity"]); + expect(removed).toEqual(["goodbye"]); array = ["foo", "bar", "baz"]; removed = array.splice(); - assert(array.length === 3); - assert(array[0] === "foo"); - assert(array[1] === "bar"); - assert(array[2] === "baz"); - assert(removed.length === 0); + expect(array).toEqual(["foo", "bar", "baz"]); + expect(removed).toEqual([]); removed = array.splice(0, 123); - assert(array.length === 0); - assert(removed.length === 3); - assert(removed[0] === "foo"); - assert(removed[1] === "bar"); - assert(removed[2] === "baz"); + expect(array).toEqual([]); + expect(removed).toEqual(["foo", "bar", "baz"]); array = ["foo", "bar", "baz"]; removed = array.splice(123, 123); - assert(array.length === 3); - assert(array[0] === "foo"); - assert(array[1] === "bar"); - assert(array[2] === "baz"); - assert(removed.length === 0); + expect(array).toEqual(["foo", "bar", "baz"]); + expect(removed).toEqual([]); array = ["foo", "bar", "baz"]; removed = array.splice(-123, 123); - assert(array.length === 0); - assert(removed.length === 3); - assert(removed[0] === "foo"); - assert(removed[1] === "bar"); - assert(removed[2] === "baz"); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + expect(array).toEqual([]); + expect(removed).toEqual(["foo", "bar", "baz"]); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.toLocaleString.js b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.toLocaleString.js index fb4e3af0f5..a08a33601d 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.toLocaleString.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.toLocaleString.js @@ -1,24 +1,60 @@ -load("test-common.js"); +test("length is 0", () => { + expect(Array.prototype.toLocaleString).toHaveLength(0); +}); -try { - assert(Array.prototype.toLocaleString.length === 0); +describe("normal behavior", () => { + test("array with no elements", () => { + expect([].toLocaleString()).toBe(""); + }); - assert([].toLocaleString() === ""); - assert(["foo"].toLocaleString() === "foo"); - assert(["foo", "bar"].toLocaleString() === "foo,bar"); - assert(["foo", undefined, "bar", null, "baz"].toLocaleString() === "foo,,bar,,baz"); + test("array with one element", () => { + expect(["foo"].toLocaleString()).toBe("foo"); + }); - var toStringCalled = 0; - var o = { - toString: () => { - toStringCalled++; - return "o"; - }, - }; - assert([o, undefined, o, null, o].toLocaleString() === "o,,o,,o"); - assert(toStringCalled === 3); + test("array with multiple elements", () => { + expect(["foo", "bar", "baz"].toLocaleString()).toBe("foo,bar,baz"); + }); - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + test("number stringification differs from regular toString, for now", () => { + expect([1, 2, 3].toLocaleString()).toBe( + "[object NumberObject],[object NumberObject],[object NumberObject]" + ); + }); + + test("null and undefined result in empty strings", () => { + expect([null].toLocaleString()).toBe(""); + expect([undefined].toLocaleString()).toBe(""); + expect([undefined, null].toLocaleString()).toBe(","); + }); + + test("empty values result in empty strings", () => { + expect(new Array(1).toLocaleString()).toBe(""); + expect(new Array(3).toLocaleString()).toBe(",,"); + var a = new Array(5); + a[2] = "foo"; + a[4] = "bar"; + expect(a.toLocaleString()).toBe(",,foo,,bar"); + }); + + test("getter property is included in returned string", () => { + var a = ["foo"]; + Object.defineProperty(a, 1, { + get() { + return "bar"; + }, + }); + expect(a.toLocaleString()).toBe("foo,bar"); + }); + + test("array with elements that have a custom toString() function", () => { + var toStringCalled = 0; + var o = { + toString() { + toStringCalled++; + return "o"; + }, + }; + expect([o, undefined, o, null, o].toLocaleString()).toBe("o,,o,,o"); + expect(toStringCalled).toBe(3); + }); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.toString.js b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.toString.js index e5d19be997..d07e8af163 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.toString.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.toString.js @@ -1,22 +1,58 @@ -load("test-common.js"); +test("length is 0", () => { + expect(Array.prototype.toString).toHaveLength(0); +}); -try { - var a = [1, 2, 3]; - assert(a.toString() === "1,2,3"); - assert([].toString() === ""); - assert([5].toString() === "5"); +describe("normal behavior", () => { + test("array with no elements", () => { + expect([].toString()).toBe(""); + }); - assert("rgb(" + [10, 11, 12] + ")" === "rgb(10,11,12)"); + test("array with one element", () => { + expect([1].toString()).toBe("1"); + }); - assert([undefined, null].toString() === ","); + test("array with multiple elements", () => { + expect([1, 2, 3].toString()).toBe("1,2,3"); + }); - a = new Array(5); - assert(a.toString() === ",,,,"); - a[2] = "foo"; - a[4] = "bar"; - assert(a.toString() === ",,foo,,bar"); + test("string and array concatenation", () => { + expect("rgb(" + [10, 11, 12] + ")").toBe("rgb(10,11,12)"); + }); - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + test("null and undefined result in empty strings", () => { + expect([null].toString()).toBe(""); + expect([undefined].toString()).toBe(""); + expect([undefined, null].toString()).toBe(","); + }); + + test("empty values result in empty strings", () => { + expect(new Array(1).toString()).toBe(""); + expect(new Array(3).toString()).toBe(",,"); + var a = new Array(5); + a[2] = "foo"; + a[4] = "bar"; + expect(a.toString()).toBe(",,foo,,bar"); + }); + + test("getter property is included in returned string", () => { + var a = [1, 2, 3]; + Object.defineProperty(a, 3, { + get() { + return 10; + }, + }); + expect(a.toString()).toBe("1,2,3,10"); + }); + + test("array with elements that have a custom toString() function", () => { + var toStringCalled = 0; + var o = { + toString() { + toStringCalled++; + return "o"; + }, + }; + expect([o, undefined, o, null, o].toString()).toBe("o,,o,,o"); + expect(toStringCalled).toBe(3); + }); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.unshift.js b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.unshift.js index 3bae2736b1..8e65e89775 100644 --- a/Libraries/LibJS/Tests/builtins/Array/Array.prototype.unshift.js +++ b/Libraries/LibJS/Tests/builtins/Array/Array.prototype.unshift.js @@ -1,30 +1,23 @@ -load("test-common.js"); +test("length is 1", () => { + expect(Array.prototype.unshift).toHaveLength(1); +}); -try { - assert(Array.prototype.unshift.length === 1); +describe("normal behavior", () => { + test("no argument", () => { + var a = ["hello"]; + expect(a.unshift()).toBe(1); + expect(a).toEqual(["hello"]); + }); - var a = ["hello"]; - var length = a.unshift(); - assert(length === 1); - assert(a.length === 1); - assert(a[0] === "hello"); + test("single argument", () => { + var a = ["hello"]; + expect(a.unshift("friends")).toBe(2); + expect(a).toEqual(["friends", "hello"]); + }); - length = a.unshift("friends"); - assert(length === 2); - assert(a.length === 2); - assert(a[0] === "friends"); - assert(a[1] === "hello"); - - length = a.unshift(1, 2, 3); - assert(length === 5); - assert(a.length === 5); - assert(a[0] === 1); - assert(a[1] === 2); - assert(a[2] === 3); - assert(a[3] === "friends"); - assert(a[4] === "hello"); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + test("multiple arguments", () => { + var a = ["friends", "hello"]; + expect(a.unshift(1, 2, 3)).toBe(5); + expect(a).toEqual([1, 2, 3, "friends", "hello"]); + }); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/array-basic.js b/Libraries/LibJS/Tests/builtins/Array/array-basic.js index c13a9de8d5..d6e6976421 100644 --- a/Libraries/LibJS/Tests/builtins/Array/array-basic.js +++ b/Libraries/LibJS/Tests/builtins/Array/array-basic.js @@ -1,63 +1,57 @@ -load("test-common.js"); - -try { +test("basic functionality", () => { var a = [1, 2, 3]; - assert(typeof a === "object"); - assert(a.length === 3); - assert(a[0] === 1); - assert(a[1] === 2); - assert(a[2] === 3); + expect(typeof a).toBe("object"); + expect(a).toHaveLength(3); + expect(a[0]).toBe(1); + expect(a[1]).toBe(2); + expect(a[2]).toBe(3); a[1] = 5; - assert(a[1] === 5); - assert(a.length === 3); + expect(a[1]).toBe(5); + expect(a).toHaveLength(3); a.push(7); - assert(a[3] === 7); - assert(a.length === 4); + expect(a[3]).toBe(7); + expect(a).toHaveLength(4); a = [,]; - assert(a.length === 1); - assert(a.toString() === ""); - assert(a[0] === undefined); + expect(a).toHaveLength(1); + expect(a.toString()).toBe(""); + expect(a[0]).toBeUndefined(); a = [, , , ,]; - assert(a.length === 4); - assert(a.toString() === ",,,"); - assert(a[0] === undefined); - assert(a[1] === undefined); - assert(a[2] === undefined); - assert(a[3] === undefined); + expect(a).toHaveLength(4); + expect(a.toString()).toBe(",,,"); + expect(a[0]).toBeUndefined(); + expect(a[1]).toBeUndefined(); + expect(a[2]).toBeUndefined(); + expect(a[3]).toBeUndefined(); a = [1, , 2, , , 3]; - assert(a.length === 6); - assert(a.toString() === "1,,2,,,3"); - assert(a[0] === 1); - assert(a[1] === undefined); - assert(a[2] === 2); - assert(a[3] === undefined); - assert(a[4] === undefined); - assert(a[5] === 3); + expect(a).toHaveLength(6); + expect(a.toString()).toBe("1,,2,,,3"); + expect(a[0]).toBe(1); + expect(a[1]).toBeUndefined(); + expect(a[2]).toBe(2); + expect(a[3]).toBeUndefined(); + expect(a[4]).toBeUndefined(); + expect(a[5]).toBe(3); a = [1, , 2, , , 3]; Object.defineProperty(a, 1, { get() { - return this.secret_prop; + return this.getterSetterValue; }, set(value) { - this.secret_prop = value; + this.getterSetterValue = value; }, }); - assert(a.length === 6); - assert(a.toString() === "1,,2,,,3"); - assert(a.secret_prop === undefined); + expect(a).toHaveLength(6); + expect(a.toString()).toBe("1,,2,,,3"); + expect(a.getterSetterValue).toBeUndefined(); a[1] = 20; - assert(a.length === 6); - assert(a.toString() === "1,20,2,,,3"); - assert(a.secret_prop === 20); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + expect(a).toHaveLength(6); + expect(a.toString()).toBe("1,20,2,,,3"); + expect(a.getterSetterValue).toBe(20); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/array-length-setter.js b/Libraries/LibJS/Tests/builtins/Array/array-length-setter.js index 1616a4f83d..9a1043f5d4 100644 --- a/Libraries/LibJS/Tests/builtins/Array/array-length-setter.js +++ b/Libraries/LibJS/Tests/builtins/Array/array-length-setter.js @@ -1,51 +1,37 @@ -load("test-common.js"); - -try { - var a = [1, 2, 3]; - - assert(a.length === 3); - assert(a[0] === 1); - assert(a[1] === 2); - assert(a[2] === 3); - - a.length = 5; - assert(a.length === 5); - assert(a[0] === 1); - assert(a[1] === 2); - assert(a[2] === 3); - assert(a[3] === undefined); - assert(a[4] === undefined); - - a.length = 1; - assert(a.length === 1); - assert(a[0] === 1); - - a.length = 0; - assert(a.length === 0); - - a.length = "42"; - assert(a.length === 42); - - a.length = []; - assert(a.length === 0); - - a.length = true; - assert(a.length === 1); - - [undefined, "foo", -1, Infinity, -Infinity, NaN].forEach(value => { - assertThrowsError( - () => { +describe("errors", () => { + test("invalid array length value", () => { + var a = [1, 2, 3]; + [undefined, "foo", -1, Infinity, -Infinity, NaN].forEach(value => { + expect(() => { a.length = value; - }, - { - error: RangeError, - message: "Invalid array length", - } - ); - assert(a.length === 1); + }).toThrowWithMessage(RangeError, "Invalid array length"); + expect(a).toHaveLength(3); + }); + }); +}); + +describe("normal behavior", () => { + test("extend array by setting length", () => { + var a = [1, 2, 3]; + a.length = 5; + expect(a).toEqual([1, 2, 3, undefined, undefined]); }); - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + test("truncate array by setting length", () => { + var a = [1, 2, 3]; + a.length = 2; + expect(a).toEqual([1, 2]); + a.length = 0; + expect(a).toEqual([]); + }); + + test("length value is coerced to number if possible", () => { + var a = [1, 2, 3]; + a.length = "42"; + expect(a).toHaveLength(42); + a.length = []; + expect(a).toHaveLength(0); + a.length = true; + expect(a).toHaveLength(1); + }); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/array-shrink-during-find-crash.js b/Libraries/LibJS/Tests/builtins/Array/array-shrink-during-find-crash.js index 16a041a8e7..d43c5d0d01 100644 --- a/Libraries/LibJS/Tests/builtins/Array/array-shrink-during-find-crash.js +++ b/Libraries/LibJS/Tests/builtins/Array/array-shrink-during-find-crash.js @@ -1,6 +1,4 @@ -load("test-common.js"); - -try { +test("Issue #1992, shrinking array during find() iteration", () => { var a, callbackCalled; callbackCalled = 0; @@ -9,7 +7,7 @@ try { callbackCalled++; a.pop(); }); - assert(callbackCalled === 5); + expect(callbackCalled).toBe(5); callbackCalled = 0; a = [1, 2, 3, 4, 5]; @@ -17,9 +15,5 @@ try { callbackCalled++; a.pop(); }); - assert(callbackCalled === 5); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + expect(callbackCalled).toBe(5); +}); diff --git a/Libraries/LibJS/Tests/builtins/Array/array-spread.js b/Libraries/LibJS/Tests/builtins/Array/array-spread.js index 0e79dc3740..cb570edffe 100644 --- a/Libraries/LibJS/Tests/builtins/Array/array-spread.js +++ b/Libraries/LibJS/Tests/builtins/Array/array-spread.js @@ -1,45 +1,25 @@ -load("test-common.js"); - -function testArray(arr) { - return arr.length === 4 && arr[0] === 0 && arr[1] === 1 && arr[2] === 2 && arr[3] === 3; -} - -try { - let arr = [0, ...[1, 2], 3]; - assert(testArray(arr)); - - let a = [1, 2]; - arr = [0, ...a, 3]; - assert(testArray(arr)); - - let obj = { a: [1, 2] }; - arr = [0, ...obj.a, 3]; - assert(testArray(arr)); - - arr = [...[], ...[...[0, 1, 2]], 3]; - assert(testArray(arr)); - - assertThrowsError( - () => { +describe("errors", () => { + test("cannot spread number in array", () => { + expect(() => { [...1]; - }, - { - error: TypeError, - message: "1 is not iterable", - } - ); + }).toThrowWithMessage(TypeError, "1 is not iterable"); + }); - assertThrowsError( - () => { + test("cannot spread object in array", () => { + expect(() => { [...{}]; - }, - { - error: TypeError, - message: "[object Object] is not iterable", - } - ); + }).toThrowWithMessage(TypeError, "[object Object] is not iterable"); + }); +}); - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} +test("basic functionality", () => { + expect([1, ...[2, 3], 4]).toEqual([1, 2, 3, 4]); + + let a = [2, 3]; + expect([1, ...a, 4]).toEqual([1, 2, 3, 4]); + + let obj = { a: [2, 3] }; + expect([1, ...obj.a, 4]).toEqual([1, 2, 3, 4]); + + expect([...[], ...[...[1, 2, 3]], 4]).toEqual([1, 2, 3, 4]); +}); diff --git a/Userland/test-js.cpp b/Userland/test-js.cpp index 9f329ace8b..2e256c4f53 100644 --- a/Userland/test-js.cpp +++ b/Userland/test-js.cpp @@ -43,6 +43,38 @@ // FIXME: Will eventually not be necessary when all tests are converted Vector tests_to_run = { + "builtins/Array/Array.js", + "builtins/Array/array-basic.js", + "builtins/Array/array-length-setter.js", + "builtins/Array/array-shrink-during-find-crash.js", + "builtins/Array/array-spread.js", + "builtins/Array/Array.isArray.js", + "builtins/Array/Array.of.js", + "builtins/Array/Array.prototype-generic-functions.js", + "builtins/Array/Array.prototype.concat.js", + "builtins/Array/Array.prototype.every.js", + "builtins/Array/Array.prototype.fill.js", + "builtins/Array/Array.prototype.filter.js", + "builtins/Array/Array.prototype.find.js", + "builtins/Array/Array.prototype.findIndex.js", + "builtins/Array/Array.prototype.forEach.js", + "builtins/Array/Array.prototype.includes.js", + "builtins/Array/Array.prototype.indexOf.js", + "builtins/Array/Array.prototype.join.js", + "builtins/Array/Array.prototype.lastIndexOf.js", + "builtins/Array/Array.prototype.map.js", + "builtins/Array/Array.prototype.pop.js", + "builtins/Array/Array.prototype.push.js", + "builtins/Array/Array.prototype.reduce.js", + "builtins/Array/Array.prototype.reduceRight.js", + "builtins/Array/Array.prototype.reverse.js", + "builtins/Array/Array.prototype.shift.js", + "builtins/Array/Array.prototype.slice.js", + "builtins/Array/Array.prototype.some.js", + "builtins/Array/Array.prototype.splice.js", + "builtins/Array/Array.prototype.toLocaleString.js", + "builtins/Array/Array.prototype.toString.js", + "builtins/Array/Array.prototype.unshift.js", "builtins/BigInt/BigInt.js", "builtins/BigInt/bigint-basic.js", "builtins/BigInt/bigint-number-mix-errors.js",