Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Unified Diff: test/intl/assert.js

Issue 1866693002: Allow to pass a user message to assert functions (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Address comments, no semantical changes Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/intl/assert.js
diff --git a/test/intl/assert.js b/test/intl/assert.js
index 3180e6f96e69db62c56f8eb7564bead6e9155a2d..e17615267ae327ea25b60db0a67e99d11fbb6a49 100644
--- a/test/intl/assert.js
+++ b/test/intl/assert.js
@@ -87,14 +87,13 @@ function deepEquals(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.
- var message = 'Failure: expected <' + String(expected) + '>, found <' +
- String(found) + '>.';
+ var message = 'Failure' + (user_message ? ' (' + user_message + ')' : '') +
+ ': expected <' + String(expected) + '>, found <' + String(found) + '>.';
throw new Error(message);
}
@@ -102,9 +101,9 @@ function fail(expected, found) {
/**
* Throws if two variables have different types or values.
*/
-function assertEquals(expected, found) {
+function assertEquals(expected, found, user_message = '') {
if (!deepEquals(expected, found)) {
- fail(expected, found);
+ fail(expected, found, user_message);
}
}
@@ -112,49 +111,49 @@ function assertEquals(expected, found) {
/**
* Throws if value is false.
*/
-function assertTrue(value) {
- assertEquals(true, value)
+function assertTrue(value, user_message = '') {
+ assertEquals(true, value, user_message);
}
/**
* Throws if value is true.
*/
-function assertFalse(value) {
- assertEquals(false, value);
+function assertFalse(value, user_message = '') {
+ 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) {
- var threwException = true;
try {
if (typeof code == 'function') {
code();
} else {
eval(code);
}
- threwException = false;
} catch (e) {
if (typeof type_opt == 'function') {
assertInstanceof(e, type_opt);
}
if (arguments.length >= 3) {
- assertEquals(e.type, cause_opt);
+ assertEquals(cause_opt, e.type, 'thrown exception type mismatch');
}
// Success.
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 {
if (typeof code == 'function') {
code();
@@ -162,7 +161,7 @@ function assertDoesNotThrow(code, name_opt) {
eval(code);
}
} catch (e) {
- fail("threw an exception: ", e.message || e, name_opt);
+ fail("no expection", "exception: " + String(e), user_message);
}
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698