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

Unified Diff: lib/unittest/unittest.dart

Issue 10545167: Some unit test fixes: (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 6 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
Index: lib/unittest/unittest.dart
===================================================================
--- lib/unittest/unittest.dart (revision 8626)
+++ lib/unittest/unittest.dart (working copy)
@@ -154,7 +154,7 @@
/** [Configuration] used by the unittest library. */
Configuration _config = null;
-/** Set the [Configuration] used by the unittest library. */
+/** Set the [Configuration] used by the unittest library.*/
Bob Nystrom 2012/06/13 23:52:29 ?
gram 2012/06/14 00:50:58 Done.
void configure(Configuration config) {
_config = config;
}
@@ -296,64 +296,107 @@
// arguments inside [_expectAsync], once bug 282 is fixed or frog is replaced by
// dart2js.
class _SpreadArgsHelper {
- Function callback;
- int expectedCalls;
- int calls = 0;
- TestCase testCase;
- _SpreadArgsHelper(this.callback, this.expectedCalls) {
+ Function _callback;
+ int _expectedCalls;
+ int _calls = 0;
+ TestCase _testCase;
+ Function _shouldCallBack;
+ Function _isDone;
+
+ _Init(Function callback, Function shouldCallBack, Function isDone,
Siggi Cherem (dart-lang) 2012/06/13 23:43:53 -> lower case.
Bob Nystrom 2012/06/13 23:52:29 Can you use a delegating constructor for this? Som
gram 2012/06/14 00:50:58 Done.
gram 2012/06/14 00:50:58 I tried this originally. It didn't work because se
+ [expectedCalls = 0]) {
Expect.isTrue(_currentTest < _tests.length);
Bob Nystrom 2012/06/13 23:52:29 This Expect call is weird. Make it an assert inste
gram 2012/06/14 00:50:58 Made assert.
- testCase = _tests[_currentTest];
- testCase.callbacks++;
+ _callback = callback;
+ _shouldCallBack = shouldCallBack;
+ _isDone = isDone;
+ _expectedCalls = expectedCalls;
+ _testCase = _tests[_currentTest];
+ _testCase.callbacks++;
}
+ _SpreadArgsHelper( callback, shouldCallBack, isDone) {
Siggi Cherem (dart-lang) 2012/06/13 23:43:53 remove extra space
Bob Nystrom 2012/06/13 23:52:29 No space after '('.
gram 2012/06/14 00:50:58 Done.
gram 2012/06/14 00:50:58 Done.
+ _Init(callback, shouldCallBack, isDone);
+ }
+
+ _SpreadArgsHelper.fixedCallCount(callback, expectedCalls) {
Siggi Cherem (dart-lang) 2012/06/13 23:43:53 let's make _callback final and change all 3 constr
gram 2012/06/14 00:50:58 Tried this; it doesn't work for the same reason de
+ _Init(callback, _checkCallCount, _allCallsDone, expectedCalls);
+ }
+
+ _SpreadArgsHelper.variableCallCount(callback, isDone) {
+ _Init(callback, _always, isDone);
+ }
+
+ _after() {
+ if (_isDone())
Siggi Cherem (dart-lang) 2012/06/13 23:43:53 style: add { } or merge this and the next line in
gram 2012/06/14 00:50:58 Done.
+ _handleAllCallbacksDone();
Bob Nystrom 2012/06/13 23:52:29 Make this one line or use {}.
gram 2012/06/14 00:50:58 Done.
+ }
+
+ _allCallsDone() {
Bob Nystrom 2012/06/13 23:52:29 Make this an =>?
gram 2012/06/14 00:50:58 Done.
+ return (_calls == _expectedCalls);
+ }
+
+ _always() {
+ // always run except if the test is done
Bob Nystrom 2012/06/13 23:52:29 Full sentence: "Always... done."
gram 2012/06/14 00:50:58 Done.
+ if (_testCase.isComplete) {
+ _testCase.error(
+ 'Callback called after already being marked as done ($_calls)',
Bob Nystrom 2012/06/13 23:52:29 Indent another 2.
gram 2012/06/14 00:50:58 Done.
+ '');
+ _state = _UNCAUGHT_ERROR;
Bob Nystrom 2012/06/13 23:52:29 Indent +1.
gram 2012/06/14 00:50:58 Done.
+ return false;
+ } else {
+ return true;
+ }
+ }
+
invoke([arg0 = _sentinel, arg1 = _sentinel, arg2 = _sentinel,
arg3 = _sentinel, arg4 = _sentinel]) {
return guardAsync(() {
- if (!_incrementCall()) {
+ ++_calls;
+ if (!_shouldCallBack()) {
return;
} else if (arg0 == _sentinel) {
- return callback();
+ return _callback();
} else if (arg1 == _sentinel) {
- return callback(arg0);
+ return _callback(arg0);
} else if (arg2 == _sentinel) {
- return callback(arg0, arg1);
+ return _callback(arg0, arg1);
} else if (arg3 == _sentinel) {
- return callback(arg0, arg1, arg2);
+ return _callback(arg0, arg1, arg2);
} else if (arg4 == _sentinel) {
- return callback(arg0, arg1, arg2, arg3);
+ return _callback(arg0, arg1, arg2, arg3);
} else {
- testCase.error(
+ _testCase.error(
'unittest lib does not support callbacks with more than 4 arguments',
'');
_state = _UNCAUGHT_ERROR;
}
- }, () { if (calls == expectedCalls) callbackDone(); });
+ },
+ _after);
}
invoke0() {
return guardAsync(
- () => _incrementCall() ? callback() : null,
- () { if (calls == expectedCalls) callbackDone(); });
+ () { if (_shouldCallBack()) _callback(); },
+ _after);
}
invoke1(arg1) {
return guardAsync(
- () => _incrementCall() ? callback(arg1) : null,
- () { if (calls == expectedCalls) callbackDone(); });
+ () { if (_shouldCallBack()) _callback(arg1); },
+ _after);
}
invoke2(arg1, arg2) {
return guardAsync(
- () => _incrementCall() ? callback(arg1, arg2) : null,
- () { if (calls == expectedCalls) callbackDone(); });
+ () { if (_shouldCallBack()) _callback(arg1, arg2); },
+ _after);
}
/** Returns false if we exceded the number of expected calls. */
- bool _incrementCall() {
- calls++;
- if (calls > expectedCalls) {
- testCase.error(
- 'Callback called more times than expected ($calls > $expectedCalls)',
+ bool _checkCallCount() {
+ if (_calls > _expectedCalls) {
+ _testCase.error(
+ 'Callback called more times than expected ($_calls > $_expectedCalls)',
Bob Nystrom 2012/06/13 23:52:29 Indent another 1.
gram 2012/06/14 00:50:58 Done.
'');
_state = _UNCAUGHT_ERROR;
return false;
@@ -371,7 +414,7 @@
* arguments (named arguments are not supported here).
*/
Function _expectAsync(Function callback, [int count = 1]) {
- return new _SpreadArgsHelper(callback, count).invoke;
+ return new _SpreadArgsHelper.fixedCallCount(callback, count).invoke;
}
/**
@@ -384,22 +427,63 @@
*/
// TODO(sigmund): deprecate this API when issue 2706 is fixed.
Function expectAsync0(Function callback, [int count = 1]) {
- return new _SpreadArgsHelper(callback, count).invoke0;
+ return new _SpreadArgsHelper.fixedCallCount(callback, count).invoke0;
}
/** Like [expectAsync0] but [callback] should take 1 positional argument. */
// TODO(sigmund): deprecate this API when issue 2706 is fixed.
Function expectAsync1(Function callback, [int count = 1]) {
- return new _SpreadArgsHelper(callback, count).invoke1;
+ return new _SpreadArgsHelper.fixedCallCount(callback, count).invoke1;
}
/** Like [expectAsync0] but [callback] should take 2 positional arguments. */
// TODO(sigmund): deprecate this API when issue 2706 is fixed.
Function expectAsync2(Function callback, [int count = 1]) {
- return new _SpreadArgsHelper(callback, count).invoke2;
+ return new _SpreadArgsHelper.fixedCallCount(callback, count).invoke2;
}
/**
+ * Indicate that [callback] is expected to be called until [isDone] returns
+ * true. The unittest framework check [isDone] after each callback and only
Bob Nystrom 2012/06/13 23:52:29 "check" -> "checks"
gram 2012/06/14 00:50:58 Done.
+ * when it returns true will it continue with the following test. Using
+ * [expectAsyncUntil] will also ensure that errors that occur within
+ * [callback] are tracked and reported. [callback] should take between 0 and
+ * 4 positional arguments (named arguments are not supported).
+ */
+Function expectAsyncUntil(Function callback, Function isDone) {
Siggi Cherem (dart-lang) 2012/06/13 23:43:53 make this private until issue 2706 gets fixed
gram 2012/06/14 00:50:58 Done.
+ return new _SpreadArgsHelper.variableCallCount(callback, isDone).invoke;
+}
+
+/**
+ * Indicate that [callback] is expected to be called until [isDone] returns
+ * true. The unittest framework check [isDone] after each callback and only
+ * when it returns true will it continue with the following test. Using
+ * [expectAsyncUntil0] will also ensure that errors that occur within
+ * [callback] are tracked and reported. [callback] should take 0 positional
+ * arguments (named arguments are not supported).
+ */
+// TODO(sigmund): deprecate this API when issue 2706 is fixed.
+Function expectAsyncUntil0(Function callback, Function isDone) {
+ return new _SpreadArgsHelper.variableCallCount(callback, isDone).invoke0;
+}
+
+/**
+ * Like [expectAsyncUntil0] but [callback] should take 1 positional argument.
+ */
+// TODO(sigmund): deprecate this API when issue 2706 is fixed.
+Function expectAsyncUntil1(Function callback, Function isDone) {
+ return new _SpreadArgsHelper.variableCallCount(callback, isDone).invoke1;
+}
+
+/**
+ * Like [expectAsyncUntil0] but [callback] should take 2 positional arguments.
+ */
+// TODO(sigmund): deprecate this API when issue 2706 is fixed.
+Function expectAsyncUntil2(Function callback, Function isDone) {
+ return new _SpreadArgsHelper.variableCallCount(callback, isDone).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.
*/
@@ -425,7 +509,7 @@
}
/** Called by subclasses to indicate that an asynchronous test completed. */
-void callbackDone() {
+void _handleAllCallbacksDone() {
// TODO (gram): we defer this to give the nextBatch recursive
// stack a chance to unwind. This is a temporary hack but
// really a bunch of code here needs to be fixed. We have a
@@ -541,7 +625,6 @@
guardAsync(() {
_callbacksCalled = 0;
_state = _RUNNING_TEST;
-
testCase.test();
if (_state != _UNCAUGHT_ERROR) {
@@ -595,6 +678,8 @@
if (_state != _UNINITIALIZED) return;
_tests = <TestCase>[];
+ _uncaughtErrorMessage = null;
+ _currentTest = 0;
_currentGroup = '';
_state = _READY;
_testRunner = _nextBatch;

Powered by Google App Engine
This is Rietveld 408576698