From e77202fe0f03b7825514bfb769ce5e36c8e9ad2b Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 11 Nov 2020 22:16:37 +0000 Subject: [PATCH] LibJS/Tests: Use canParseSource() for toEval() We can now enable the "new.target is syntax error outside of function" test :^) --- .../LibJS/Tests/automatic-semicolon-insertion.js | 12 ++++++------ .../LibJS/Tests/functions/function-new-target.js | 3 +-- Libraries/LibJS/Tests/test-common-tests.js | 4 +--- Libraries/LibJS/Tests/test-common.js | 10 ++-------- 4 files changed, 10 insertions(+), 19 deletions(-) diff --git a/Libraries/LibJS/Tests/automatic-semicolon-insertion.js b/Libraries/LibJS/Tests/automatic-semicolon-insertion.js index 924ff20b91..998b0b6622 100644 --- a/Libraries/LibJS/Tests/automatic-semicolon-insertion.js +++ b/Libraries/LibJS/Tests/automatic-semicolon-insertion.js @@ -1,18 +1,18 @@ test("Issue #1829, if-else without braces or semicolons", () => { const source = `if (1) - return 1; + foo; else - return 0; + bar; if (1) - return 1 + foo else - return 0 + bar if (1) - return 1 + foo else - return 0;`; + bar;`; expect(source).toEval(); }); diff --git a/Libraries/LibJS/Tests/functions/function-new-target.js b/Libraries/LibJS/Tests/functions/function-new-target.js index 8b5667d3f0..9eb7659391 100644 --- a/Libraries/LibJS/Tests/functions/function-new-target.js +++ b/Libraries/LibJS/Tests/functions/function-new-target.js @@ -20,7 +20,6 @@ test("basic functionality", () => { expect(new baz().newTarget).toEqual(baz); }); -// FIXME: This does not work as expected as toEval() places the code inside a function :| -test.skip("syntax error outside of function", () => { +test("syntax error outside of function", () => { expect("new.target").not.toEval(); }); diff --git a/Libraries/LibJS/Tests/test-common-tests.js b/Libraries/LibJS/Tests/test-common-tests.js index 8dec6e1fb5..a857d327bb 100644 --- a/Libraries/LibJS/Tests/test-common-tests.js +++ b/Libraries/LibJS/Tests/test-common-tests.js @@ -326,13 +326,11 @@ test("toThrowWithMessage", () => { expect(thrower).not.toThrowWithMessage(TypeError, "foo baz"); }); -// FIXME: Will have to change when this matcher changes to use the -// "eval" function test("toEval", () => { expect("let a = 1").toEval(); expect("a < 1").toEval(); expect("&&*^%#%@").not.toEval(); - expect("function foo() { return 1; }; return foo();").toEval(); + expect("function foo() { return 1; }; foo();").toEval(); }); // FIXME: Will have to change when this matcher changes to use the diff --git a/Libraries/LibJS/Tests/test-common.js b/Libraries/LibJS/Tests/test-common.js index f7c8c83539..709d270867 100644 --- a/Libraries/LibJS/Tests/test-common.js +++ b/Libraries/LibJS/Tests/test-common.js @@ -302,14 +302,8 @@ class ExpectationError extends Error { // Test for syntax errors; target must be a string toEval() { this.__expect(typeof this.target === "string"); - - let threw = false; - try { - new Function(this.target); - } catch (e) { - threw = true; - } - this.__expect(this.inverted ? threw : !threw); + const success = canParseSource(this.target) + this.__expect(this.inverted ? !success : success); } // Must compile regardless of inverted-ness