LibJS/Tests: Use canParseSource() for toEval()

We can now enable the "new.target is syntax error outside of function"
test :^)
This commit is contained in:
Linus Groh 2020-11-11 22:16:37 +00:00 committed by Andreas Kling
parent 7fc98a96a9
commit e77202fe0f
Notes: sideshowbarker 2024-07-19 01:26:02 +09:00
4 changed files with 10 additions and 19 deletions

View file

@ -1,18 +1,18 @@
test("Issue #1829, if-else without braces or semicolons", () => { test("Issue #1829, if-else without braces or semicolons", () => {
const source = `if (1) const source = `if (1)
return 1; foo;
else else
return 0; bar;
if (1) if (1)
return 1 foo
else else
return 0 bar
if (1) if (1)
return 1 foo
else else
return 0;`; bar;`;
expect(source).toEval(); expect(source).toEval();
}); });

View file

@ -20,7 +20,6 @@ test("basic functionality", () => {
expect(new baz().newTarget).toEqual(baz); expect(new baz().newTarget).toEqual(baz);
}); });
// FIXME: This does not work as expected as toEval() places the code inside a function :| test("syntax error outside of function", () => {
test.skip("syntax error outside of function", () => {
expect("new.target").not.toEval(); expect("new.target").not.toEval();
}); });

View file

@ -326,13 +326,11 @@ test("toThrowWithMessage", () => {
expect(thrower).not.toThrowWithMessage(TypeError, "foo baz"); expect(thrower).not.toThrowWithMessage(TypeError, "foo baz");
}); });
// FIXME: Will have to change when this matcher changes to use the
// "eval" function
test("toEval", () => { test("toEval", () => {
expect("let a = 1").toEval(); expect("let a = 1").toEval();
expect("a < 1").toEval(); expect("a < 1").toEval();
expect("&&*^%#%@").not.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 // FIXME: Will have to change when this matcher changes to use the

View file

@ -302,14 +302,8 @@ class ExpectationError extends Error {
// Test for syntax errors; target must be a string // Test for syntax errors; target must be a string
toEval() { toEval() {
this.__expect(typeof this.target === "string"); this.__expect(typeof this.target === "string");
const success = canParseSource(this.target)
let threw = false; this.__expect(this.inverted ? !success : success);
try {
new Function(this.target);
} catch (e) {
threw = true;
}
this.__expect(this.inverted ? threw : !threw);
} }
// Must compile regardless of inverted-ness // Must compile regardless of inverted-ness