Commit 5704f86c authored by Leszek Swirski's avatar Leszek Swirski Committed by V8 LUCI CQ

[test] Pretty print object properties on assert failure

Because I don't get much out of "Object() != Object()"

Change-Id: I5a765b9cb0a272d30edcd834ec7b60d2fd03190b
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3497352Reviewed-by: 's avatarCamillo Bruni <cbruni@chromium.org>
Auto-Submit: Leszek Swirski <leszeks@chromium.org>
Commit-Queue: Leszek Swirski <leszeks@chromium.org>
Cr-Commit-Position: refs/heads/main@{#79370}
parent d05a38f9
...@@ -3,7 +3,7 @@ test/mjsunit/mjsunit.js:{NUMBER}: Failure: expected <not same as 1> found <1> ...@@ -3,7 +3,7 @@ test/mjsunit/mjsunit.js:{NUMBER}: Failure: expected <not same as 1> found <1>
Stack: MjsUnitAssertionError Stack: MjsUnitAssertionError
at assertNotSame *mjsunit.js {NUMBER}:{NUMBER} at assertNotSame *mjsunit.js {NUMBER}:{NUMBER}
at *%(basename)s 7:1 at *%(basename)s 7:1
throw new MjsUnitAssertionError(message); throw new MjsUnitAssertionError(
^ ^
MjsUnitAssertionError MjsUnitAssertionError
at assertNotSame *mjsunit.js {NUMBER}:{NUMBER} at assertNotSame *mjsunit.js {NUMBER}:{NUMBER}
......
...@@ -3,7 +3,7 @@ test/mjsunit/mjsunit.js:{NUMBER}: Failure: expected <true> found <false> ...@@ -3,7 +3,7 @@ test/mjsunit/mjsunit.js:{NUMBER}: Failure: expected <true> found <false>
Stack: MjsUnitAssertionError Stack: MjsUnitAssertionError
at assertTrue *mjsunit.js {NUMBER}:{NUMBER} at assertTrue *mjsunit.js {NUMBER}:{NUMBER}
at *%(basename)s 7:1 at *%(basename)s 7:1
throw new MjsUnitAssertionError(message); throw new MjsUnitAssertionError(
^ ^
MjsUnitAssertionError MjsUnitAssertionError
at assertTrue *mjsunit.js {NUMBER}:{NUMBER} at assertTrue *mjsunit.js {NUMBER}:{NUMBER}
......
...@@ -25,8 +25,12 @@ ...@@ -25,8 +25,12 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
function MjsUnitAssertionError(message) { var MjsUnitAssertionError = class MjsUnitAssertionError {
this.message = message; #cached_message = undefined;
#message_func = undefined;
constructor(message_func) {
this.#message_func = message_func;
// Temporarily install a custom stack trace formatter and restore the // Temporarily install a custom stack trace formatter and restore the
// previous value. // previous value.
let prevPrepareStackTrace = Error.prepareStackTrace; let prevPrepareStackTrace = Error.prepareStackTrace;
...@@ -37,6 +41,18 @@ function MjsUnitAssertionError(message) { ...@@ -37,6 +41,18 @@ function MjsUnitAssertionError(message) {
} finally { } finally {
Error.prepareStackTrace = prevPrepareStackTrace; Error.prepareStackTrace = prevPrepareStackTrace;
} }
}
get message() {
if (this.#cached_message === undefined) {
this.#cached_message = this.#message_func();
}
return this.#cached_message
}
toString() {
return this.message + "\n\nStack: " + this.stack;
};
} }
/* /*
...@@ -45,11 +61,6 @@ function MjsUnitAssertionError(message) { ...@@ -45,11 +61,6 @@ function MjsUnitAssertionError(message) {
* the f-word and ignore all other lines. * the f-word and ignore all other lines.
*/ */
MjsUnitAssertionError.prototype.toString = function () {
return this.message + "\n\nStack: " + this.stack;
};
// Expected and found values the same objects, or the same primitive // Expected and found values the same objects, or the same primitive
// values. // values.
// For known primitive values, please use assertEquals. // For known primitive values, please use assertEquals.
...@@ -258,6 +269,9 @@ var prettyPrinted; ...@@ -258,6 +269,9 @@ var prettyPrinted;
prettyPrinted = function prettyPrinted(value) { prettyPrinted = function prettyPrinted(value) {
let visited = new Set();
function prettyPrint(value) {
try {
switch (typeof value) { switch (typeof value) {
case "string": case "string":
return JSONStringify(value); return JSONStringify(value);
...@@ -273,6 +287,9 @@ var prettyPrinted; ...@@ -273,6 +287,9 @@ var prettyPrinted;
return String(value); return String(value);
case "object": case "object":
if (value === null) return "null"; if (value === null) return "null";
// Guard against re-visiting.
if (visited.has(value)) return "<...>";
visited.add(value);
var objectClass = classOf(value); var objectClass = classOf(value);
switch (objectClass) { switch (objectClass) {
case "Number": case "Number":
...@@ -280,12 +297,15 @@ var prettyPrinted; ...@@ -280,12 +297,15 @@ var prettyPrinted;
case "String": case "String":
case "Boolean": case "Boolean":
case "Date": case "Date":
return objectClass + "(" + prettyPrinted(ValueOf(value)) + ")"; return objectClass + "(" + prettyPrint(ValueOf(value)) + ")";
case "RegExp": case "RegExp":
return RegExpPrototypeToString.call(value); return RegExpPrototypeToString.call(value);
case "Array": case "Array":
var mapped = ArrayPrototypeMap.call( var mapped = ArrayPrototypeMap.call(
value, prettyPrintedArrayElement); value, (v,i,array)=>{
if (v === undefined && !(i in array)) return "";
return prettyPrint(v, visited);
});
var joined = ArrayPrototypeJoin.call(mapped, ","); var joined = ArrayPrototypeJoin.call(mapped, ",");
return "[" + joined + "]"; return "[" + joined + "]";
case "Uint8Array": case "Uint8Array":
...@@ -304,23 +324,27 @@ var prettyPrinted; ...@@ -304,23 +324,27 @@ var prettyPrinted;
return objectClass + "(" + String(value) + ")"; return objectClass + "(" + String(value) + ")";
} }
// classOf() returned "Object". // classOf() returned "Object".
var name = value.constructor.name; var name = value.constructor?.name ?? "Object";
if (name) return name + "()"; var pretty_properties = [];
return "Object()"; for (let [k,v] of Object.entries(value)) {
ArrayPrototypePush.call(
pretty_properties, `${k}:${prettyPrint(v, visited)}`);
}
var joined = ArrayPrototypeJoin.call(pretty_properties, ",");
return `${name}({${joined}})`;
default: default:
return "-- unknown value --"; return "-- unknown value --";
} }
} catch (e) {
// Guard against general exceptions (especially stack overflows).
return "<error>"
} }
function prettyPrintedArrayElement(value, index, array) {
if (value === undefined && !(index in array)) return "";
return prettyPrinted(value);
} }
return prettyPrint(value);
}
failWithMessage = function failWithMessage(message) { failWithMessage = function failWithMessage(message) {
throw new MjsUnitAssertionError(message); throw new MjsUnitAssertionError(()=>message);
} }
formatFailureText = function(expectedText, found, name_opt) { formatFailureText = function(expectedText, found, name_opt) {
...@@ -340,7 +364,8 @@ var prettyPrinted; ...@@ -340,7 +364,8 @@ var prettyPrinted;
} }
function fail(expectedText, found, name_opt) { function fail(expectedText, found, name_opt) {
return failWithMessage(formatFailureText(expectedText, found, name_opt)); throw new MjsUnitAssertionError(
()=>formatFailureText(expectedText, found, name_opt));
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment