Chromium Code Reviews| Index: pkg/unittest/lib/src/test_case.dart |
| =================================================================== |
| --- pkg/unittest/lib/src/test_case.dart (revision 18238) |
| +++ pkg/unittest/lib/src/test_case.dart (working copy) |
| @@ -68,18 +68,39 @@ |
| bool get isComplete => !enabled || result != null; |
| + void _prepTest() { |
| + _config.onTestStart(this); |
| + startTime = new DateTime.now(); |
| + runningTime = null; |
| + } |
| + |
| void run() { |
| if (enabled) { |
| result = stackTrace = null; |
| message = ''; |
| _doneTeardown = false; |
| if (_setUp != null) { |
| - _setUp(); |
| + var rtn = _setUp(); |
| + if (rtn is Future) { |
| + rtn.then(expectAsync1((_) { |
| + _prepTest(); |
| + test(); |
| + }, id: '[Async setUp completion handler]')) |
| + .catchError((e) { |
|
justinfagnani
2013/02/08 01:36:48
Make sure you always call catchError() before then
gram
2013/02/08 17:52:24
Why?
Siggi Cherem (dart-lang)
2013/02/08 18:02:22
to clarify: as long as the futures are chained, th
justinfagnani
2013/02/08 18:36:57
Good catch. I was mentally using the old pattern,
|
| + _prepTest(); |
|
justinfagnani
2013/02/08 01:36:48
why call _prepTest() when there's an error?
gram
2013/02/08 17:52:24
We are going to treat this like a regular test err
|
| + // Calling error() will result in the tearDown being done. |
| + // One could debate whether tearDown should be done after |
| + // a failed setUp. There is no right answer, but doing it |
| + // seems to be the more conservative approach. |
| + error("$description: Test setup failed: ${e.error}"); |
| + }); |
| + return; |
| + } |
| } |
| - _config.onTestStart(this); |
| - startTime = new DateTime.now(); |
| - runningTime = null; |
| + _prepTest(); |
|
justinfagnani
2013/02/08 01:36:48
Looks like for async setup _prepTest() and test()
gram
2013/02/08 17:52:24
No, there is a return statement in the block.
justinfagnani
2013/02/08 18:36:57
Oops
|
| test(); |
| + } else { |
| + _nextTestCase(); |
| } |
| } |
| @@ -90,13 +111,42 @@ |
| // runningTime = new DateTime.now().difference(startTime); |
| runningTime = new Duration(milliseconds: 0); |
| } |
| + bool mustRunNextTest = false; |
| if (!_doneTeardown) { |
| + _doneTeardown = true; |
| + mustRunNextTest = true; |
| if (_tearDown != null) { |
| - _tearDown(); |
| + var rtn = _tearDown(); |
| + if (rtn is Future) { |
| + rtn.then(expectAsync1((_) { |
| + if (result == null) { |
| + // The test passed. In some cases we will already |
| + // have set this result (e.g. if the test was async |
| + // and all callbacks completed). If not, we do it here. |
| + pass(); |
| + } else { |
| + // The test has already been marked as pass/fail. |
| + // Just report the updated result. |
| + _config.onTestResult(this); |
| + } |
| + _nextTestCase(); |
| + }, id: '[Async tearDown completion handler]')) |
| + .catchError((e) { |
|
justinfagnani
2013/02/08 01:36:48
Make sure you always call catchError() before then
|
| + // We don't call fail() as that will potentially result in |
| + // spurious messages like 'test failed more than once'. |
| + result = ERROR; |
| + message = "$description: Test teardown failed: ${e.error}"; |
| + _config.onTestResult(this); |
| + _nextTestCase(); |
| + }); |
| + return; |
| + } |
| } |
| - _doneTeardown = true; |
| } |
| _config.onTestResult(this); |
| + if (mustRunNextTest) { |
| + _nextTestCase(); |
| + } |
| } |
| void pass() { |