1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 21:18:14 +00:00

LibJS: Add messages to the toEval and toEvalTo tests

This makes it a lot easier to understand what is going wrong when an
eval test fails. As an example instead of just getting:
`ExpectationError`
You would now get:
`ExpectationError: Expected _1_2E+0_1_ to eval to _12_ but got _120_`.
This commit is contained in:
davidot 2021-11-30 01:00:06 +01:00 committed by Linus Groh
parent 7e6ad172a4
commit 450dedabd1

View file

@ -353,7 +353,12 @@ class ExpectationError extends Error {
toEval() {
this.__expect(typeof this.target === "string");
const success = canParseSource(this.target);
this.__expect(this.inverted ? !success : success);
this.__expect(
this.inverted ? !success : success,
() =>
`Expected _${valueToString(this.target)}_` +
(this.inverted ? "not to eval but it did" : "to eval but it didn't")
);
}
// Must compile regardless of inverted-ness
@ -365,11 +370,18 @@ class ExpectationError extends Error {
try {
result = eval(this.target);
} catch (e) {
throw new ExpectationError();
throw new ExpectationError(
`Expected _${valueToString(this.target)}_ to eval but it failed with ${e}`
);
}
this.__doMatcher(() => {
this.__expect(deepEquals(value, result));
this.__expect(
deepEquals(value, result),
() =>
`Expected _${valueToString(this.target)}_ to eval to ` +
`_${valueToString(value)}_ but got _${valueToString(result)}_`
);
});
}