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

Unified Diff: lib/unittest/unittest.dart

Issue 10830128: Ability to guard callbacks that may never get called. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 5 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 | tests/lib/unittest/unittest_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/unittest/unittest.dart
===================================================================
--- lib/unittest/unittest.dart (revision 10122)
+++ lib/unittest/unittest.dart (working copy)
@@ -323,6 +323,7 @@
Function _callback;
int _expectedCalls;
int _calls = 0;
+ int _testNum;
TestCase _testCase;
Function _shouldCallBack;
Function _isDone;
@@ -335,6 +336,7 @@
_shouldCallBack = shouldCallBack;
_isDone = isDone;
_expectedCalls = expectedCalls;
+ _testNum = _currentTest;
_testCase = _tests[_currentTest];
if (expectedCalls > 0) {
_testCase.callbacks++;
@@ -353,6 +355,10 @@
_init(callback, _always, isDone, 1);
}
+ _SpreadArgsHelper.optionalCalls(callback) {
+ _init(callback, _always, _never, 0);
Siggi Cherem (dart-lang) 2012/08/02 00:19:07 seems that _never will never be used anywhere else
gram 2012/08/02 00:41:32 Done.
+ }
+
_after() {
if (_isDone()) {
_handleAllCallbacksDone();
@@ -361,6 +367,8 @@
_allCallsDone() => _calls == _expectedCalls;
+ _never() => false;
+
_always() {
// Always run except if the test is done.
if (_testCase.isComplete) {
@@ -398,7 +406,7 @@
_state = _UNCAUGHT_ERROR;
}
},
- _after);
+ _after, _testNum);
}
invoke0() {
@@ -409,7 +417,7 @@
return _callback();
}
},
- _after);
+ _after, _testNum);
}
invoke1(arg1) {
@@ -420,7 +428,7 @@
return _callback(arg1);
}
},
- _after);
+ _after, _testNum);
}
invoke2(arg1, arg2) {
@@ -431,7 +439,7 @@
return _callback(arg1, arg2);
}
},
- _after);
+ _after, _testNum);
}
/** Returns false if we exceded the number of expected calls. */
@@ -527,6 +535,47 @@
}
/**
+ * Wraps the [callback] in a new function and returns that function. The new
+ * function will be able to handle exceptions by directing them to the correct
+ * test. This is thus similar to expectAsync0. Use it to wrap any callbacks that
+ * might optionally be called but may never be called during the test.
+ * [callback] should take between 0 and 4 positional arguments (named arguments
+ * are not supported).
+ */
+Function _protectAsync(Function callback) {
+ return new _SpreadArgsHelper.optionalCalls(callback).invoke;
+}
+
+/**
+ * Wraps the [callback] in a new function and returns that function. The new
+ * function will be able to handle exceptions by directing them to the correct
+ * test. This is thus similar to expectAsync0. Use it to wrap any callbacks that
+ * might optionally be called but may never be called during the test.
+ * [callback] should take 0 positional arguments (named arguments are not
+ * supported).
+ */
+// TODO(sigmund): deprecate this API when issue 2706 is fixed.
+Function protectAsync0(Function callback) {
+ return new _SpreadArgsHelper.optionalCalls(callback).invoke0;
+}
+
+/**
+ * Like [protectAsync0] but [callback] should take 1 positional argument.
+ */
+// TODO(sigmund): deprecate this API when issue 2706 is fixed.
+Function protectAsync1(Function callback) {
+ return new _SpreadArgsHelper.optionalCalls(callback).invoke1;
+}
+
+/**
+ * Like [protectAsync0] but [callback] should take 2 positional arguments.
+ */
+// TODO(sigmund): deprecate this API when issue 2706 is fixed.
+Function protectAsync2(Function callback) {
+ return new _SpreadArgsHelper.optionalCalls(callback).invoke2;
+}
+
+/**
* Creates a new named group of tests. Calls to group() or test() within the
* body of the function passed to this will inherit this group's description.
*/
@@ -689,11 +738,12 @@
* Run [tryBody] guarded in a try-catch block. If an exception is thrown, update
* the [_currentTest] status accordingly.
*/
-guardAsync(tryBody, [finallyBody]) {
+guardAsync(tryBody, [finallyBody, testNum = -1]) {
+ if (testNum < 0) testNum = _currentTest;
try {
return tryBody();
} catch (var e, var trace) {
- registerException(e, trace);
+ registerException(testNum, e, trace);
} finally {
_state = _READY;
if (finallyBody != null) finallyBody();
@@ -703,26 +753,19 @@
/**
* Registers that an exception was caught for the current test.
*/
-registerException(e, [trace]) {
- if (e is ExpectException) {
- Expect.isTrue(_currentTest < _tests.length);
- if (_state != _UNCAUGHT_ERROR) {
- _tests[_currentTest].fail(e.message,
- trace == null ? '' : trace.toString());
+registerException(testNum, e, [trace]) {
+ if (_tests[testNum].result == null) {
+ if (e is ExpectException) {
+ _tests[testNum].fail(e.message, trace == null ? '' : trace.toString());
+ } else {
+ _tests[testNum].fail('Caught $e', trace == null ? '' : trace.toString());
}
} else {
- if (_state == _RUNNING_TEST) {
- // If a random exception is thrown from within a test, we consider that
- // a test failure too. A test case implicitly has an expectation that it
- // will run to completion without an uncaught exception being thrown.
- _tests[_currentTest].fail('Caught $e',
- trace == null ? '' : trace.toString());
- } else if (_state != _UNCAUGHT_ERROR) {
- _tests[_currentTest].error('Caught $e',
- trace == null ? '' : trace.toString());
- }
+ _tests[testNum].error('Caught $e', trace == null ? '' : trace.toString());
}
- _nextTestCase();
+ if (testNum == _currentTest) {
Siggi Cherem (dart-lang) 2012/08/02 00:19:07 yay!
gram 2012/08/02 00:41:32 Done.
+ _nextTestCase();
+ }
}
/**
« no previous file with comments | « no previous file | tests/lib/unittest/unittest_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698