Index: test/mjsunit/mjsunit.js |
diff --git a/test/mjsunit/mjsunit.js b/test/mjsunit/mjsunit.js |
index 6a7c2da9e47d1ca56b52c98b3afbd2a2b6d72b6c..385d01484e74583bdcdf87dffe2d8a56ec1d8502 100644 |
--- a/test/mjsunit/mjsunit.js |
+++ b/test/mjsunit/mjsunit.js |
@@ -54,6 +54,7 @@ var assertSame; |
// and the properties of non-Array objects). |
var assertEquals; |
+var assertEqualsAsync; |
// The difference between expected and found value is within certain tolerance. |
var assertEqualsDelta; |
@@ -93,6 +94,8 @@ var assertNotNull; |
// to the type property on the thrown exception. |
var assertThrows; |
+var assertThrowsAsync; |
+ |
// Assert that the passed function throws an exception. |
// The exception is checked against the second argument using assertEquals. |
var assertThrowsEquals; |
@@ -286,6 +289,33 @@ var assertContains; |
}; |
+ assertEqualsAsync = function assertEqualsAsync(expected, run, msg) { |
+ var actual; |
+ var hadValue = false; |
+ var hadError = false; |
+ var promise = run(); |
+ |
+ if (typeof promise !== "object" || typeof promise.then !== "function") { |
+ throw new MjsUnitAssertionError( |
+ "Expected " + run.toString() + |
+ " to return a Promise, but it returned " + PrettyPrint(promise)); |
+ } |
+ |
+ promise.then(function(value) { hadValue = true; actual = value; }, |
+ function(error) { hadError = true; actual = error; }); |
+ |
+ assertFalse(hadValue || hadError); |
+ |
+ flushMicrotasks(); |
+ |
+ if (hadError) throw actual; |
+ |
+ assertTrue( |
+ hadValue, "Expected '" + run.toString() + "' to produce a value"); |
+ |
+ assertEquals(expected, actual, msg); |
+ }; |
+ |
assertEqualsDelta = |
function assertEqualsDelta(expected, found, delta, name_opt) { |
assertTrue(Math.abs(expected - found) <= delta, name_opt); |
@@ -372,6 +402,40 @@ var assertContains; |
throw new MjsUnitAssertionError("Did not throw exception"); |
}; |
+ assertThrowsAsync = function assertThrowsAsync(run, errorType, message) { |
+ var actual; |
+ var hadValue = false; |
+ var hadError = false; |
+ var promise = run(); |
+ |
+ if (typeof promise !== "object" || typeof promise.then !== "function") { |
+ throw new MjsUnitAssertionError( |
+ "Expected " + run.toString() + |
+ " to return a Promise, but it returned " + PrettyPrint(promise)); |
+ } |
+ |
+ promise.then(function(value) { hadValue = true; actual = value; }, |
+ function(error) { hadError = true; actual = error; }); |
+ |
+ assertFalse(hadValue || hadError); |
+ |
+ flushMicrotasks(); |
+ |
+ if (!hadError) { |
+ throw new MjsUnitAssertionError( |
+ "Expected " + run + "() to throw " + errorType.name + |
+ ", but did not throw."); |
+ } |
+ if (!(actual instanceof errorType)) |
+ throw new MjsUnitAssertionError( |
+ "Expected " + run + "() to throw " + errorType.name + |
+ ", but threw '" + actual + "'"); |
+ if (message !== void 0 && actual.message !== message) |
+ throw new MjsUnitAssertionError( |
+ "Expected " + run + "() to throw '" + message + "', but threw '" + |
+ actual.message + "'"); |
+ }; |
+ |
assertThrowsEquals = function assertThrowsEquals(fun, val) { |
try { |