Commit b4edd2f6 authored by clemensh's avatar clemensh Committed by Commit bot

Allow to pass a user message to assert functions

Nothing too important, but it helps localizing the cause of an error
much faster.
By the way, I also changed the output for assertThrows and
assertDoesNotThrow a bit.
All new arguments are optional, so everything is backwards compatible.

R=jfb@chromium.org, titzer@chromium.org
BUG=

Review URL: https://codereview.chromium.org/1866693002

Cr-Commit-Position: refs/heads/master@{#35322}
parent ecb45844
...@@ -87,14 +87,13 @@ function deepEquals(a, b) { ...@@ -87,14 +87,13 @@ function deepEquals(a, b) {
return deepObjectEquals(a, b); return deepObjectEquals(a, b);
} }
/** /**
* Throws an exception, and prints the values in case of error. * Throws an exception containing the user_message (if any) and the values.
*/ */
function fail(expected, found) { function fail(expected, found, user_message = '') {
// TODO(cira): Replace String with PrettyPrint for objects and arrays. // TODO(cira): Replace String with PrettyPrint for objects and arrays.
var message = 'Failure: expected <' + String(expected) + '>, found <' + var message = 'Failure' + (user_message ? ' (' + user_message + ')' : '') +
String(found) + '>.'; ': expected <' + String(expected) + '>, found <' + String(found) + '>.';
throw new Error(message); throw new Error(message);
} }
...@@ -102,9 +101,9 @@ function fail(expected, found) { ...@@ -102,9 +101,9 @@ function fail(expected, found) {
/** /**
* Throws if two variables have different types or values. * Throws if two variables have different types or values.
*/ */
function assertEquals(expected, found) { function assertEquals(expected, found, user_message = '') {
if (!deepEquals(expected, found)) { if (!deepEquals(expected, found)) {
fail(expected, found); fail(expected, found, user_message);
} }
} }
...@@ -112,49 +111,49 @@ function assertEquals(expected, found) { ...@@ -112,49 +111,49 @@ function assertEquals(expected, found) {
/** /**
* Throws if value is false. * Throws if value is false.
*/ */
function assertTrue(value) { function assertTrue(value, user_message = '') {
assertEquals(true, value) assertEquals(true, value, user_message);
} }
/** /**
* Throws if value is true. * Throws if value is true.
*/ */
function assertFalse(value) { function assertFalse(value, user_message = '') {
assertEquals(false, value); assertEquals(false, value, user_message);
} }
/** /**
* Returns true if code throws specified exception. * Runs code() and asserts that it throws the specified exception.
*/ */
function assertThrows(code, type_opt, cause_opt) { function assertThrows(code, type_opt, cause_opt) {
var threwException = true;
try { try {
if (typeof code == 'function') { if (typeof code == 'function') {
code(); code();
} else { } else {
eval(code); eval(code);
} }
threwException = false;
} catch (e) { } catch (e) {
if (typeof type_opt == 'function') { if (typeof type_opt == 'function') {
assertInstanceof(e, type_opt); assertInstanceof(e, type_opt);
} }
if (arguments.length >= 3) { if (arguments.length >= 3) {
assertEquals(e.type, cause_opt); assertEquals(cause_opt, e.type, 'thrown exception type mismatch');
} }
// Success. // Success.
return; return;
} }
throw new Error("Did not throw exception"); var expected = arguments.length >= 3 ? cause_opt :
typeof type_opt == 'function' ? type_opt : 'any exception';
fail(expected, 'no exception', 'expected thrown exception');
} }
/** /**
* Throws an exception if code throws. * Runs code() and asserts that it does now throw any exception.
*/ */
function assertDoesNotThrow(code, name_opt) { function assertDoesNotThrow(code, user_message = '') {
try { try {
if (typeof code == 'function') { if (typeof code == 'function') {
code(); code();
...@@ -162,7 +161,7 @@ function assertDoesNotThrow(code, name_opt) { ...@@ -162,7 +161,7 @@ function assertDoesNotThrow(code, name_opt) {
eval(code); eval(code);
} }
} catch (e) { } catch (e) {
fail("threw an exception: ", e.message || e, name_opt); fail("no expection", "exception: " + String(e), user_message);
} }
} }
......
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