Chromium Code Reviews| Index: test/mjsunit/mjsunit.js |
| diff --git a/test/mjsunit/mjsunit.js b/test/mjsunit/mjsunit.js |
| index fe580f35005a42b4e404c3003092a17118ff3252..50f9cf97d1e7816d58fee521e5bf08d6d9c9f868 100644 |
| --- a/test/mjsunit/mjsunit.js |
| +++ b/test/mjsunit/mjsunit.js |
| @@ -41,6 +41,43 @@ MjsUnitAssertionError.prototype.toString = function () { |
| * the f-word and ignore all other lines. |
| */ |
| +function MJSToString(value) { |
|
Kevin Millikin (Chromium)
2011/03/21 11:25:39
You should adopt the same naming convention as the
Lasse Reichstein
2011/03/21 13:17:58
Renamed to MjsUnitToString.
|
| + switch (typeof value) { |
| + case "string": return JSON.stringify(value); |
| + case "number": |
| + if (value === 0 && 1/value < 0) return "-0"; |
| + case "boolean": |
| + case "null": |
| + case "undefined": |
| + case "function": |
| + return String(value); |
| + case "object": |
| + if (value === null) return "null"; |
| + var clazz = Object.prototype.toString.call(value); |
| + clazz = clazz.substring(8, clazz.length - 1); |
| + switch (clazz) { |
| + case "Number": |
| + case "String": |
| + case "Boolean": |
| + case "Date": |
| + return clazz + "(" + MJSToString(value.valueOf()) + ")"; |
| + case "RegExp": |
| + return value.toString(); |
| + case "Array": |
| + return "[" + value.map(MJSToString).join("\n") + "]"; |
| + case "Object": |
| + break; |
| + default: |
| + return clazz + "()"; |
| + } |
| + // [[Class]] is "Object". |
| + var constructor = value.constructor.name; |
| + if (name) return name + "()"; |
| + return "Object()"; |
| + } |
| +} |
| + |
| + |
| function fail(expected, found, name_opt) { |
| var start; |
|
Kevin Millikin (Chromium)
2011/03/21 11:25:39
I can't figure out why this function is so convolu
Lasse Reichstein
2011/03/21 13:17:58
And it doesn't have to be.
I added the second case
Kevin Millikin (Chromium)
2011/03/21 13:37:36
Here's where we need printf :)
Lasse Reichstein
2011/03/21 14:18:26
Indeed! :)
|
| if (name_opt) { |
| @@ -49,7 +86,18 @@ function fail(expected, found, name_opt) { |
| } else { |
| start = "Fail" + "ure:"; |
| } |
| - throw new MjsUnitAssertionError(start + " expected <" + expected + "> found <" + found + ">"); |
| + var message; |
| + var expected_string = MJSToString(expected); |
| + var found_string = MJSToString(found); |
| + if (typeof expected === typeof found) { |
| + message = |
| + " expected <" + expected_string + "> found <" + found_string + ">"; |
| + } else { |
| + message = " expected <" + expected_string + " (" + |
| + typeof expected + ")> found <" + |
| + found_string + " (" + typeof found + ")>"; |
| + } |
| + throw new MjsUnitAssertionError(start + message); |
| } |
| @@ -73,7 +121,11 @@ function deepObjectEquals(a, b) { |
| function deepEquals(a, b) { |
| - if (a == b) return true; |
| + if (a == b) { |
| + // Check for -0. |
| + if (a === 0 && b === 0) return (1 / a) === (1 / b); |
| + return true; |
| + } |
| if (typeof a == "number" && typeof b == "number" && isNaN(a) && isNaN(b)) { |
| return true; |
| } |