diff options
author | Matthew Olsson <matthewcolsson@gmail.com> | 2020-07-05 10:47:40 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-07-06 23:40:35 +0200 |
commit | 918f4affd51cdd285272770465d4d875f4ae5aa2 (patch) | |
tree | 5885d1c4161083454924db9abaaf127c76cf0c99 /Libraries | |
parent | 6d58c48c2fbe2e3ce9b9cfe1dd75a5ccc594f961 (diff) | |
download | serenity-918f4affd51cdd285272770465d4d875f4ae5aa2.zip |
LibJS: Convert remaining top-level tests to new system
Diffstat (limited to 'Libraries')
18 files changed, 470 insertions, 498 deletions
diff --git a/Libraries/LibJS/Tests/const-reassignment.js b/Libraries/LibJS/Tests/const-reassignment.js new file mode 100644 index 0000000000..6638229738 --- /dev/null +++ b/Libraries/LibJS/Tests/const-reassignment.js @@ -0,0 +1,16 @@ +test.skip("reassignment to const", () => { + const constantValue = 1; + expect(() => { + constantValue = 2; + }).toThrowWithMessage(TypeError, "Invalid assignment to const variable"); + expect(constantValue).toBe(1); +}); + +test("const creation in inner scope", () => { + const constantValue = 1; + do { + const constantValue = 2; + expect(constantValue).toBe(2); + } while (false); + expect(constantValue).toBe(1); +}); diff --git a/Libraries/LibJS/Tests/object-basic.js b/Libraries/LibJS/Tests/object-basic.js index d4d0b83e79..d35ff1ed06 100644 --- a/Libraries/LibJS/Tests/object-basic.js +++ b/Libraries/LibJS/Tests/object-basic.js @@ -1,104 +1,149 @@ -load("test-common.js"); - -try { - var foo = "bar"; - var computed = "computed"; - var o = { - 1: 23, - foo, - bar: "baz", - qux: true ? 10 : 20, - hello: "friends", - [1 + 2]: 42, - ["I am a " + computed + " key"]: foo, - duplicate: "hello", - duplicate: "world", - }; - assert(o[1] === 23); - assert(o[1n] === 23); - assert(o["1"] === 23); - assert(o.foo === "bar"); - assert(o["foo"] === "bar"); - assert(o.qux === 10), assert(o.hello === "friends"); - assert(o["hello"] === "friends"); - assert(o[3] === 42); - assert(o["I am a computed key"] === "bar"); - assert(o.duplicate === "world"); - o.baz = "test"; - assert(o.baz === "test"); - assert(o["baz"] === "test"); - o[10] = "123"; - assert(o[10] === "123"); - assert(o["10"] === "123"); - o[10n] = "123"; - assert(o[10] === "123"); - assert(o["10"] === "123"); - o[-1] = "hello friends"; - assert(o[-1] === "hello friends"); - assert(o["-1"] === "hello friends"); - - var math = { 3.14: "pi" }; - assert(math["3.14"] === "pi"); - // Note : this test doesn't pass yet due to floating-point literals being coerced to i32 on access - // assert(math[3.14] === "pi"); - - // This is also allowed! Watch out for syntax errors. - var o2 = { return: 1, yield: 1, for: 1, catch: 1, break: 1 }; - assert(o2.return === 1); - assert(o2.yield === 1); - assert(o2.for === 1); - assert(o2.catch === 1); - assert(o2.break === 1); - - var a; - var append = x => { +describe("correct behavior", () => { + test("numeric indexing", () => { + const o = { 1: 23 }; + + expect(o[1]).toBe(23); + expect(o[1n]).toBe(23); + expect(o["1"]).toBe(23); + + o[10] = "123"; + expect(o[10]).toBe("123"); + expect(o["10"]).toBe("123"); + + o[10n] = "1234"; + expect(o[10]).toBe("1234"); + expect(o["10"]).toBe("1234"); + }); + + test("string indexing", () => { + let foo = "bar"; + + const o = { + foo, + bar: "baz", + qux: true ? 10 : 20, + hello: "friends", + }; + + expect(o.foo).toBe("bar"); + expect(o["foo"]).toBe("bar"); + expect(o.qux).toBe(10), expect(o.hello).toBe("friends"); + expect(o["hello"]).toBe("friends"); + }); + + test("computed properties", () => { + const foo = "bar"; + const computed = "computed"; + const o = { + [1 + 2]: 42, + [`I am a ${computed} key`]: foo, + }; + + expect(o[3]).toBe(42); + expect(o["I am a computed key"]).toBe("bar"); + }); + + test("duplicate keys", () => { + const o = { + duplicate: "hello", + duplicate: "world", + }; + expect(o.duplicate).toBe("world"); + }); + + test("assigning after creation", () => { + const o = {}; + o.baz = "test"; + + expect(o.baz).toBe("test"); + expect(o["baz"]).toBe("test"); + + expect(o[-1]).toBeUndefined(); + o[-1] = "hello friends"; + expect(o[-1]).toBe("hello friends"); + expect(o["-1"]).toBe("hello friends"); + }); + + test("floating point keys", () => { + const math = { 3.14: "pi" }; + expect(math["3.14"]).toBe("pi"); + // FIXME: Floating point literals are coerced to i32 + // expect(math[3.14]).toBe("pi"); + }); + + test("keywords as property keys", () => { + const o2 = { + return: 1, + yield: 1, + for: 1, + catch: 1, + break: 1, + }; + + expect(o2.return).toBe(1); + expect(o2.yield).toBe(1); + expect(o2.for).toBe(1); + expect(o2.catch).toBe(1); + expect(o2.break).toBe(1); + }); + + test("prototypical inheritance", () => { + var base = { + getNumber() { + return 10; + }, + }; + + var derived = { + getNumber() { + return 20 + super.getNumber(); + }, + }; + + Object.setPrototypeOf(derived, base); + expect(derived.getNumber()).toBe(30); + }); +}); + +describe("side effects", () => { + let a; + const append = x => { a.push(x); }; - a = []; - var o3 = { [append(1)]: 1, [append(2)]: 2, [append(3)]: 3 }; - assert(a.length === 3); - assert(a[0] === 1); - assert(a[1] === 2); - assert(a[2] === 3); - assert(o3.undefined === 3); - - a = []; - var o4 = { test: append(1), test: append(2), test: append(3) }; - assert(a.length === 3); - assert(a[0] === 1); - assert(a[1] === 2); - assert(a[2] === 3); - assert(o4.test === undefined); - - var base = { - getNumber() { - return 10; - }, - }; + test("computed key side effects", () => { + a = []; + const o3 = { [append(1)]: 1, [append(2)]: 2, [append(3)]: 3 }; + expect(a).toHaveLength(3); + expect(a[0]).toBe(1); + expect(a[1]).toBe(2); + expect(a[2]).toBe(3); + expect(o3.undefined).toBe(3); + }); - var derived = { - getNumber() { - return 20 + super.getNumber(); - }, - }; + test("value side effects", () => { + a = []; + const o4 = { test: append(1), test: append(2), test: append(3) }; + expect(a).toHaveLength(3); + expect(a[0]).toBe(1); + expect(a[1]).toBe(2); + expect(a[2]).toBe(3); + expect(o4.test).toBeUndefined(); + }); +}); - Object.setPrototypeOf(derived, base); - assert(derived.getNumber() === 30); - - assertIsSyntaxError("({ foo: function() { super.bar; } })"); - assertIsSyntaxError("({ get ...foo })"); - assertIsSyntaxError("({ get... foo })"); - assertIsSyntaxError("({ get foo })"); - assertIsSyntaxError("({ get foo: bar })"); - assertIsSyntaxError("({ get [foo]: bar })"); - assertIsSyntaxError("({ get ...[foo] })"); - assertIsSyntaxError("({ get foo(bar) {} })"); - assertIsSyntaxError("({ set foo() {} })"); - assertIsSyntaxError("({ set foo(bar, baz) {} })"); - assertIsSyntaxError("({ ...foo: bar })"); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} +describe("errors", () => { + test("syntax errors", () => { + expect("({ foo: function() { super.bar; } })").not.toEval(); + expect("({ get ...foo })").not.toEval(); + expect("({ get... foo })").not.toEval(); + expect("({ get foo })").not.toEval(); + expect("({ get foo: bar })").not.toEval(); + expect("({ get [foo]: bar })").not.toEval(); + expect("({ get ...[foo] })").not.toEval(); + expect("({ get foo(bar) {} })").not.toEval(); + expect("({ set foo() {} })").not.toEval(); + expect("({ set foo(bar, baz) {} })").not.toEval(); + expect("({ ...foo: bar })").not.toEval(); + }); +}); diff --git a/Libraries/LibJS/Tests/parser-unary-associativity.js b/Libraries/LibJS/Tests/parser-unary-associativity.js index 4ed4b015ff..cc22b39660 100644 --- a/Libraries/LibJS/Tests/parser-unary-associativity.js +++ b/Libraries/LibJS/Tests/parser-unary-associativity.js @@ -1,20 +1,14 @@ -load("test-common.js"); - -try { - var o = {}; +test("basic functionality", () => { + const o = {}; o.a = 1; - assert(o.a === 1); - assert(!o.a === false); - assert(!o.a === !o.a); - assert(~o.a === ~o.a); - assert(+o.a === +o.a); - assert(-o.a === -o.a); - - assert((typeof "x" === "string") === true); - assert(!(typeof "x" === "string") === false); + expect(o.a === 1).toBeTrue(); + expect(!o.a === false).toBeTrue(); + expect(!o.a === !o.a).toBeTrue(); + expect(~o.a === ~o.a).toBeTrue(); + expect(+o.a === +o.a).toBeTrue(); + expect(-o.a === -o.a).toBeTrue(); - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + expect((typeof "x" === "string") === true).toBeTrue(); + expect(!(typeof "x" === "string") === false).toBeTrue(); +}); diff --git a/Libraries/LibJS/Tests/program-strict-mode.js b/Libraries/LibJS/Tests/program-strict-mode.js index 6d896750b0..87b9da1cbf 100644 --- a/Libraries/LibJS/Tests/program-strict-mode.js +++ b/Libraries/LibJS/Tests/program-strict-mode.js @@ -1,29 +1,18 @@ "use strict"; -load("test-common.js"); - -try { - assert(isStrictMode()); - - (function () { - assert(isStrictMode()); - })(); +test("basic functionality", () => { + expect(isStrictMode()).toBeTrue(); (function () { - "use strict"; - assert(isStrictMode()); + expect(isStrictMode()).toBeTrue(); })(); (() => { - assert(isStrictMode()); + expect(isStrictMode()).toBeTrue(); })(); (() => { "use strict"; - assert(isStrictMode()); + expect(isStrictMode()).toBeTrue(); })(); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} +}); diff --git a/Libraries/LibJS/Tests/strict-mode-errors.js b/Libraries/LibJS/Tests/strict-mode-errors.js index 6557650652..827667b729 100644 --- a/Libraries/LibJS/Tests/strict-mode-errors.js +++ b/Libraries/LibJS/Tests/strict-mode-errors.js @@ -1,21 +1,9 @@ "use strict"; -load("test-common.js"); - -try { +test("basic functionality", () => { [true, false, "foo", 123].forEach(primitive => { - assertThrowsError( - () => { - primitive.foo = "bar"; - }, - { - error: TypeError, - message: "Cannot assign property foo to primitive value", - } - ); + expect(() => { + primitive.foo = "bar"; + }).toThrowWithMessage(TypeError, "Cannot assign property foo to primitive value"); }); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} +}); diff --git a/Libraries/LibJS/Tests/string-escapes.js b/Libraries/LibJS/Tests/string-escapes.js index c3242c7924..e6d9752695 100644 --- a/Libraries/LibJS/Tests/string-escapes.js +++ b/Libraries/LibJS/Tests/string-escapes.js @@ -1,17 +1,13 @@ -load("test-common.js"); +test("hex escapes", () => { + expect("\x55").toBe("U"); + expect("X55").toBe("X55"); + expect(`\x55`).toBe("U"); + expect(`\X55`).toBe("X55"); +}); -try { - assert("\x55" === "U"); - assert("X55" === "X55"); - assert(`\x55` === "U"); - assert(`\X55` === "X55"); - - assert("\u26a0" === "⚠"); - assert(`\u26a0` === "⚠"); - assert("\u{1f41e}" === "🐞"); - assert(`\u{1f41e}` === "🐞"); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} +test("unicode escapes", () => { + expect("\u26a0").toBe("⚠"); + expect(`\u26a0`).toBe("⚠"); + expect("\u{1f41e}").toBe("🐞"); + expect(`\u{1f41e}`).toBe("🐞"); +}); diff --git a/Libraries/LibJS/Tests/string-spread.js b/Libraries/LibJS/Tests/string-spread.js index cca1ab60bf..5443e997eb 100644 --- a/Libraries/LibJS/Tests/string-spread.js +++ b/Libraries/LibJS/Tests/string-spread.js @@ -1,27 +1,25 @@ -load("test-common.js"); - function testArray(arr) { return arr.length === 4 && arr[0] === "a" && arr[1] === "b" && arr[2] === "c" && arr[3] === "d"; } -try { - var arr; - - arr = ["a", ..."bc", "d"]; - assert(testArray(arr)); +test("spreading string literal", () => { + expect(["a", ..."bc", "d"]).toEqual(["a", "b", "c", "d"]); +}); - let s = "bc"; - arr = ["a", ...s, "d"]; - assert(testArray(arr)); +test("spreading string variable", () => { + const s = "bc"; + expect(["a", ...s, "d"]).toEqual(["a", "b", "c", "d"]); +}); - let obj = { a: "bc" }; - arr = ["a", ...obj.a, "d"]; - assert(testArray(arr)); +test("spreading string in object", () => { + const obj = { a: "bc" }; + expect(["a", ...obj.a, "d"]).toEqual(["a", "b", "c", "d"]); +}); - arr = [..."", ...[...new String("abc")], "d"]; - assert(testArray(arr)); +test("spreading empty string", () => { + expect([..."", "a", ..."bc", ..."", "d", ...""]).toEqual(["a", "b", "c", "d"]); +}); - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} +test("spreading string objects", () => { + expect([..."", ...[...new String("abc")], "d"]).toEqual(["a", "b", "c", "d"]); +}); diff --git a/Libraries/LibJS/Tests/switch-break.js b/Libraries/LibJS/Tests/switch-break.js index ea0ceaef97..3eb8946d35 100644 --- a/Libraries/LibJS/Tests/switch-break.js +++ b/Libraries/LibJS/Tests/switch-break.js @@ -1,9 +1,7 @@ -load("test-common.js"); - -try { - var i = 0; - var three; - var five; +test("basic functionality", () => { + let i = 0; + let three; + let five; for (; i < 9; ) { switch (i) { @@ -16,8 +14,7 @@ try { } ++i; } - assert(three === 3); - assert(five === 5); - console.log("PASS"); -} catch {} + expect(three).toBe(3); + expect(five).toBe(5); +}); diff --git a/Libraries/LibJS/Tests/template-literals.js b/Libraries/LibJS/Tests/template-literals.js index aac01b5025..bf089f8e26 100644 --- a/Libraries/LibJS/Tests/template-literals.js +++ b/Libraries/LibJS/Tests/template-literals.js @@ -1,50 +1,58 @@ -load("test-common.js"); - -try { - assert(`foo` === "foo"); - assert(`foo{` === "foo{"); - assert(`foo}` === "foo}"); - assert(`foo$` === "foo$"); - assert(`foo\`` === "foo`"); - assert(`foo\$` === "foo$"); - - assert(`foo ${undefined}` === "foo undefined"); - assert(`foo ${null}` === "foo null"); - assert(`foo ${5}` === "foo 5"); - assert(`foo ${true}` === "foo true"); - assert(`foo ${"bar"}` === "foo bar"); - assert(`foo \${"bar"}` === 'foo ${"bar"}'); - - assert(`foo ${{}}` === "foo [object Object]"); - assert(`foo ${{ bar: { baz: "qux" } }}` === "foo [object Object]"); - assert(`foo ${"bar"} ${"baz"}` === "foo bar baz"); - assert(`${"foo"} bar baz` === "foo bar baz"); - assert(`${"foo bar baz"}` === "foo bar baz"); +test("plain literals with expression-like characters", () => { + expect(`foo`).toBe("foo"); + expect(`foo{`).toBe("foo{"); + expect(`foo}`).toBe("foo}"); + expect(`foo$`).toBe("foo$"); +}); +test("plain literals with escaped special characters", () => { + expect(`foo\``).toBe("foo`"); + expect(`foo\$`).toBe("foo$"); + expect(`foo \${"bar"}`).toBe('foo ${"bar"}'); +}); + +test("literals in expressions", () => { + expect(`foo ${undefined}`).toBe("foo undefined"); + expect(`foo ${null}`).toBe("foo null"); + expect(`foo ${5}`).toBe("foo 5"); + expect(`foo ${true}`).toBe("foo true"); + expect(`foo ${"bar"}`).toBe("foo bar"); +}); + +test("objects in expressions", () => { + expect(`foo ${{}}`).toBe("foo [object Object]"); + expect(`foo ${{ bar: { baz: "qux" } }}`).toBe("foo [object Object]"); +}); + +test("expressions at beginning of template literal", () => { + expect(`${"foo"} bar baz`).toBe("foo bar baz"); + expect(`${"foo bar baz"}`).toBe("foo bar baz"); +}); + +test("multiple template literals", () => { + expect(`foo ${"bar"} ${"baz"}`).toBe("foo bar baz"); +}); + +test("variables in expressions", () => { let a = 27; - assert(`${a}` === "27"); - assert(`foo ${a}` === "foo 27"); - assert(`foo ${a ? "bar" : "baz"}` === "foo bar"); - assert(`foo ${(() => a)()}` === "foo 27"); - - assert(`foo ${`bar`}` === "foo bar"); - assert(`${`${`${`${"foo"}`} bar`}`}` === "foo bar"); - assert( + expect(`${a}`).toBe("27"); + expect(`foo ${a}`).toBe("foo 27"); + expect(`foo ${a ? "bar" : "baz"}`).toBe("foo bar"); + expect(`foo ${(() => a)()}`).toBe("foo 27"); +}); + +test("template literals in expressions", () => { + expect(`foo ${`bar`}`).toBe("foo bar"); + expect(`${`${`${`${"foo"}`} bar`}`}`).toBe("foo bar"); +}); + +test("newline literals (not characters)", () => { + expect( `foo - bar` === "foo\n bar" - ); - - assertThrowsError( - () => { - `${b}`; - }, - { - error: ReferenceError, - message: "'b' is not defined", - } - ); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + bar` + ).toBe("foo\n bar"); +}); + +test("reference error from expressions", () => { + expect(() => `${b}`).toThrowWithMessage(ReferenceError, "'b' is not defined"); +}); diff --git a/Libraries/LibJS/Tests/throw-basic.js b/Libraries/LibJS/Tests/throw-basic.js index 733b14f80a..e359a24aa1 100644 --- a/Libraries/LibJS/Tests/throw-basic.js +++ b/Libraries/LibJS/Tests/throw-basic.js @@ -1,30 +1,34 @@ -load("test-common.js"); +test("throw literal", () => { + try { + throw 1; + expect().fail(); + } catch (e) { + if (e.name === "ExpectationError") throw e; + expect(e).toBe(1); + } +}); -try { - throw 1; - assertNotReached(); -} catch (e) { - assert(e === 1); -} +test("throw array", () => { + try { + throw [99]; + expect().fail(); + } catch (e) { + if (e.name === "ExpectationError") throw e; + expect(e).toEqual([99]); + } +}); -try { - throw [99]; - assertNotReached(); -} catch (e) { - assert(typeof e === "object"); - assert(e.length === 1); -} +test("call function that throws", () => { + function foo() { + throw "hello"; + expect().fail(); + } -function foo() { - throw "hello"; - assertNotReached(); -} - -try { - foo(); - assertNotReached(); -} catch (e) { - assert(e === "hello"); -} - -console.log("PASS"); + try { + foo(); + expect().fail(); + } catch (e) { + if (e.name === "ExpectationError") throw e; + expect(e).toBe("hello"); + } +}); diff --git a/Libraries/LibJS/Tests/to-number-basic.js b/Libraries/LibJS/Tests/to-number-basic.js index 91a1f00b10..5b80f24d53 100644 --- a/Libraries/LibJS/Tests/to-number-basic.js +++ b/Libraries/LibJS/Tests/to-number-basic.js @@ -1,62 +1,75 @@ -load("test-common.js"); - -try { - assert(+false === 0); - assert(-false === 0); - assert(+true === 1); - assert(-true === -1); - assert(+null === 0); - assert(-null === 0); - assert(+[] === 0); - assert(-[] === 0); - assert(+[,] === 0); - assert(-[,] === 0); - assert(+[null] === 0); - assert(-[null] === 0); - assert(+[undefined] === 0); - assert(-[undefined] === 0); - assert(+[[[[[]]]]] === 0); - assert(-[[[[[]]]]] === 0); - assert(+[[[[[42]]]]] === 42); - assert(-[[[[[42]]]]] === -42); - assert(+"" === 0); - assert(-"" === 0); - assert(+"42" === 42); - assert(-"42" === -42); - assert(+42 === 42); - assert(-42 === -42); - assert(+1.23 === 1.23); - assert(-1.23 === -1.23); - assert(+"1.23" === 1.23); - assert(-"1.23" === -1.23); - assert(+"Infinity" === Infinity); - assert(+"+Infinity" === Infinity); - assert(+"-Infinity" === -Infinity); - assert(-"Infinity" === -Infinity); - assert(-"+Infinity" === -Infinity); - assert(-"-Infinity" === Infinity); - assert(+" \r \t \n " === 0); - assert(+" \n \t Infinity \r " === Infinity); - assert(+"\r \n1.23 \t\t\t \n" === 1.23); - - assert(isNaN(+undefined)); - assert(isNaN(-undefined)); - assert(isNaN(+{})); - assert(isNaN(-{})); - assert(isNaN(+{ a: 1 })); - assert(isNaN(-{ a: 1 })); - assert(isNaN(+[, , ,])); - assert(isNaN(-[, , ,])); - assert(isNaN(+[undefined, undefined])); - assert(isNaN(-[undefined, undefined])); - assert(isNaN(+[1, 2, 3])); - assert(isNaN(-[1, 2, 3])); - assert(isNaN(+[[[["foo"]]]])); - assert(isNaN(-[[[["foo"]]]])); - assert(isNaN(+"foo")); - assert(isNaN(-"foo")); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} +test("non-numeric primitives", () => { + expect(+false).toBe(0); + expect(-false).toBe(-0); + expect(+true).toBe(1); + expect(-true).toBe(-1); + expect(+null).toBe(0); + expect(-null).toBe(-0); + expect(+undefined).toBeNaN(); + expect(-undefined).toBeNaN(); +}); + +test("arrays", () => { + expect(+[]).toBe(0); + expect(-[]).toBe(-0); + expect(+[,]).toBe(0); + expect(-[,]).toBe(-0); + expect(+[null]).toBe(0); + expect(-[null]).toBe(-0); + expect(+[undefined]).toBe(0); + expect(-[undefined]).toBe(-0); + expect(+[[[[[]]]]]).toBe(0); + expect(-[[[[[]]]]]).toBe(-0); + expect(+[[[[[42]]]]]).toBe(42); + expect(-[[[[[42]]]]]).toBe(-42); + + expect(+[, , ,]).toBeNaN(); + expect(-[, , ,]).toBeNaN(); + expect(+[undefined, undefined]).toBeNaN(); + expect(-[undefined, undefined]).toBeNaN(); + expect(+[1, 2, 3]).toBeNaN(); + expect(-[1, 2, 3]).toBeNaN(); + expect(+[[[["foo"]]]]).toBeNaN(); + expect(-[[[["foo"]]]]).toBeNaN(); +}); + +test("strings", () => { + expect(+"").toBe(0); + expect(-"").toBe(-0); + expect(+"42").toBe(42); + expect(-"42").toBe(-42); + expect(+"1.23").toBe(1.23); + expect(-"1.23").toBe(-1.23); + + expect(+"foo").toBeNaN(); + expect(-"foo").toBeNaN(); +}); + +test("numbers", () => { + expect(+42).toBe(42); + expect(-42).toBe(-42); + expect(+1.23).toBe(1.23); + expect(-1.23).toBe(-1.23); +}); + +test("infinity", () => { + expect(+"Infinity").toBe(Infinity); + expect(+"+Infinity").toBe(Infinity); + expect(+"-Infinity").toBe(-Infinity); + expect(-"Infinity").toBe(-Infinity); + expect(-"+Infinity").toBe(-Infinity); + expect(-"-Infinity").toBe(Infinity); +}); + +test("space and space-like escapes", () => { + expect(+" \r \t \n ").toBe(0); + expect(+" \n \t Infinity \r ").toBe(Infinity); + expect(+"\r \n1.23 \t\t\t \n").toBe(1.23); +}); + +test("object literals", () => { + expect(+{}).toBeNaN(); + expect(-{}).toBeNaN(); + expect(+{ a: 1 }).toBeNaN(); + expect(-{ a: 1 }).toBeNaN(); +}); diff --git a/Libraries/LibJS/Tests/to-number-exception.js b/Libraries/LibJS/Tests/to-number-exception.js index 3eed7253f6..1174ed195d 100644 --- a/Libraries/LibJS/Tests/to-number-exception.js +++ b/Libraries/LibJS/Tests/to-number-exception.js @@ -1,54 +1,25 @@ -load("test-common.js"); +const message = "oops, Value::to_number() failed"; -try { - const message = "oops, Value::to_number() failed"; - const o = { - toString() { - throw new Error(message); - }, - }; +const o = { + toString() { + throw new Error(message); + }, +}; - assertThrowsError( - () => { - +o; - }, - { - error: Error, - message, - } - ); +test("basic functionality", () => { + expect(() => { + +o; + }).toThrowWithMessage(Error, message); - assertThrowsError( - () => { - o - 1; - }, - { - error: Error, - message, - } - ); + expect(() => { + o - 1; + }).toThrowWithMessage(Error, message); - assertThrowsError( - () => { - "foo".charAt(o); - }, - { - error: Error, - message, - } - ); + expect(() => { + "foo".charAt(o); + }).toThrowWithMessage(Error, message); - assertThrowsError( - () => { - "bar".repeat(o); - }, - { - error: Error, - message, - } - ); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + expect(() => { + "bar".repeat(o); + }).toThrowWithMessage(Error, message); +}); diff --git a/Libraries/LibJS/Tests/update-expression-on-member-expression.js b/Libraries/LibJS/Tests/update-expression-on-member-expression.js index 3ddb8a1869..f24e76ee51 100644 --- a/Libraries/LibJS/Tests/update-expression-on-member-expression.js +++ b/Libraries/LibJS/Tests/update-expression-on-member-expression.js @@ -1,5 +1,5 @@ test("basic update expression", () => { - var o = {}; + const o = {}; o.f = 1; expect(o.f++).toBe(1); diff --git a/Libraries/LibJS/Tests/update-expressions-basic.js b/Libraries/LibJS/Tests/update-expressions-basic.js index 32add75d2e..160819b260 100644 --- a/Libraries/LibJS/Tests/update-expressions-basic.js +++ b/Libraries/LibJS/Tests/update-expressions-basic.js @@ -1,57 +1,53 @@ -load("test-common.js"); - -try { - assertThrowsError( - () => { +describe("correct behavior", () => { + test("basic functionality", () => { + let n = 0; + expect(++n).toBe(1); + expect(n).toBe(1); + + n = 0; + expect(n++).toBe(0); + expect(n).toBe(1); + + n = 0; + expect(--n).toBe(-1); + expect(n).toBe(-1); + + n = 0; + expect(n--).toBe(0); + expect(n).toBe(-1); + + let a = []; + expect(a++).toBe(0); + expect(a).toBe(1); + + let b = true; + expect(b--).toBe(1); + expect(b).toBe(0); + }); + + test("updates that produce NaN", () => { + let s = "foo"; + expect(++s).toBeNaN(); + expect(s).toBeNaN(); + + s = "foo"; + expect(s++).toBeNaN(); + expect(s).toBeNaN(); + + s = "foo"; + expect(--s).toBeNaN(); + expect(s).toBeNaN(); + + s = "foo"; + expect(s--).toBeNaN(); + expect(s).toBeNaN(); + }); +}); + +describe("errors", () => { + test("update expression throws reference error", () => { + expect(() => { ++x; - }, - { - error: ReferenceError, - message: "'x' is not defined", - } - ); - - var n = 0; - assert(++n === 1); - assert(n === 1); - - var n = 0; - assert(n++ === 0); - assert(n === 1); - - var n = 0; - assert(--n === -1); - assert(n === -1); - - var n = 0; - assert(n-- === 0); - assert(n === -1); - - var a = []; - assert(a++ === 0); - assert(a === 1); - - var b = true; - assert(b-- === 1); - assert(b === 0); - - var s = "foo"; - assert(isNaN(++s)); - assert(isNaN(s)); - - var s = "foo"; - assert(isNaN(s++)); - assert(isNaN(s)); - - var s = "foo"; - assert(isNaN(--s)); - assert(isNaN(s)); - - var s = "foo"; - assert(isNaN(s--)); - assert(isNaN(s)); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + }).toThrowWithMessage(ReferenceError, "'x' is not defined"); + }); +}); diff --git a/Libraries/LibJS/Tests/var-multiple-declarator.js b/Libraries/LibJS/Tests/var-multiple-declarator.js index 0596f4057c..089e67313c 100644 --- a/Libraries/LibJS/Tests/var-multiple-declarator.js +++ b/Libraries/LibJS/Tests/var-multiple-declarator.js @@ -1,13 +1,8 @@ -load("test-common.js"); - -try { +test("basic functionality", () => { var a = 1, b = 2, c = a + b; - assert(a === 1); - assert(b === 2); - assert(c === 3); - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + expect(a).toBe(1); + expect(b).toBe(2); + expect(c).toBe(3); +}); diff --git a/Libraries/LibJS/Tests/var-scoping.js b/Libraries/LibJS/Tests/var-scoping.js index 44cb2af741..478836a43e 100644 --- a/Libraries/LibJS/Tests/var-scoping.js +++ b/Libraries/LibJS/Tests/var-scoping.js @@ -1,9 +1,7 @@ -load("test-common.js"); - -try { +test("basic functionality", () => { function foo() { i = 3; - assert(i === 3); + expect(i).toBe(3); var i; } @@ -15,9 +13,5 @@ try { } catch (e) { caught_exception = e; } - assert(caught_exception !== undefined); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + expect(caught_exception).not.toBeUndefined(); +}); diff --git a/Libraries/LibJS/Tests/variable-declaration.js b/Libraries/LibJS/Tests/variable-declaration.js deleted file mode 100644 index 7a6c0b85b3..0000000000 --- a/Libraries/LibJS/Tests/variable-declaration.js +++ /dev/null @@ -1,27 +0,0 @@ -load("test-common.js"); - -try { - const constantValue = 1; - assertThrowsError( - () => { - constantValue = 2; - }, - { - error: TypeError, - message: "Invalid assignment to const variable", - } - ); - assert(constantValue === 1); - - // Make sure we can define new constants in inner scopes. - const constantValue2 = 1; - do { - const constantValue2 = 2; - assert(constantValue2 === 2); - } while (false); - assert(constantValue2 === 1); - - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} diff --git a/Libraries/LibJS/Tests/variable-undefined.js b/Libraries/LibJS/Tests/variable-undefined.js index c853a3e8c2..d9963feaee 100644 --- a/Libraries/LibJS/Tests/variable-undefined.js +++ b/Libraries/LibJS/Tests/variable-undefined.js @@ -1,19 +1,14 @@ -load("test-common.js"); +test("basic functionality", () => { + function foo(a) { + return a; + } -function foo(a) { - return a; -} - -try { var x = undefined; - assert(x === undefined); - assert(foo(x) === undefined); + expect(x).toBeUndefined(); + expect(foo(x)).toBeUndefined(); var o = {}; o.x = x; - assert(o.x === undefined); - assert(o.x === x); - console.log("PASS"); -} catch (e) { - console.log("FAIL: " + e); -} + expect(o.x).toBeUndefined(); + expect(o.x).toBe(x); +}); |