Chromium Code Reviews| Index: test/mjsunit/mjsunit.js |
| diff --git a/test/mjsunit/mjsunit.js b/test/mjsunit/mjsunit.js |
| index 6a7c2da9e47d1ca56b52c98b3afbd2a2b6d72b6c..bcb11c024449f8f13fe25c61570256174b0c69df 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); |
| + |
| + %RunMicrotasks(); |
|
Dan Ehrenberg
2016/05/16 22:36:47
Won't this throw without --allow-natives-syntax? M
caitp (gmail)
2016/05/16 22:39:59
Well it will throw, but I don't think anything els
Dan Ehrenberg
2016/05/16 22:49:20
This will probably throw at parse time. % function
|
| + |
| + 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); |
| + |
| + %RunMicrotasks(); |
| + |
| + 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 { |