| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of unittest; | 5 part of unittest; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Represents the state for an individual unit test. | 8 * Represents the state for an individual unit test. |
| 9 * | 9 * |
| 10 * Create by calling [test] or [solo_test]. | 10 * Create by calling [test] or [solo_test]. |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 Future _runTest() { | 76 Future _runTest() { |
| 77 _prepTest(); | 77 _prepTest(); |
| 78 // Increment/decrement callbackFunctionsOutstanding to prevent | 78 // Increment/decrement callbackFunctionsOutstanding to prevent |
| 79 // synchronous 'async' callbacks from causing the test to be | 79 // synchronous 'async' callbacks from causing the test to be |
| 80 // marked as complete before the body is completely executed. | 80 // marked as complete before the body is completely executed. |
| 81 ++_callbackFunctionsOutstanding; | 81 ++_callbackFunctionsOutstanding; |
| 82 var f = testFunction(); | 82 var f = testFunction(); |
| 83 --_callbackFunctionsOutstanding; | 83 --_callbackFunctionsOutstanding; |
| 84 if (f is Future) { | 84 if (f is Future) { |
| 85 return f.then((_) => _finishTest()) | 85 return f.then((_) => _finishTest()) |
| 86 .catchError((e) => fail("${e.error}")); | 86 .catchError((error) => fail("${error}")); |
| 87 } else { | 87 } else { |
| 88 _finishTest(); | 88 _finishTest(); |
| 89 return null; | 89 return null; |
| 90 } | 90 } |
| 91 } | 91 } |
| 92 | 92 |
| 93 void _finishTest() { | 93 void _finishTest() { |
| 94 if (result == null && _callbackFunctionsOutstanding == 0) { | 94 if (result == null && _callbackFunctionsOutstanding == 0) { |
| 95 pass(); | 95 pass(); |
| 96 } | 96 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 111 var rtn = setUp == null ? null : setUp(); | 111 var rtn = setUp == null ? null : setUp(); |
| 112 if (rtn is Future) { | 112 if (rtn is Future) { |
| 113 rtn.then((_) => _runTest()) | 113 rtn.then((_) => _runTest()) |
| 114 .catchError((e) { | 114 .catchError((e) { |
| 115 _prepTest(); | 115 _prepTest(); |
| 116 // Calling error() will result in the tearDown being done. | 116 // Calling error() will result in the tearDown being done. |
| 117 // One could debate whether tearDown should be done after | 117 // One could debate whether tearDown should be done after |
| 118 // a failed setUp. There is no right answer, but doing it | 118 // a failed setUp. There is no right answer, but doing it |
| 119 // seems to be the more conservative approach, because | 119 // seems to be the more conservative approach, because |
| 120 // unittest will not stop at a test failure. | 120 // unittest will not stop at a test failure. |
| 121 error("$description: Test setup failed: ${e.error}"); | 121 error("$description: Test setup failed: $e"); |
| 122 }); | 122 }); |
| 123 } else { | 123 } else { |
| 124 var f = _runTest(); | 124 var f = _runTest(); |
| 125 if (f != null) { | 125 if (f != null) { |
| 126 return f; | 126 return f; |
| 127 } | 127 } |
| 128 } | 128 } |
| 129 if (result == null) { // Not complete. | 129 if (result == null) { // Not complete. |
| 130 _testComplete = new Completer(); | 130 _testComplete = new Completer(); |
| 131 return _testComplete.future; | 131 return _testComplete.future; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 } | 168 } |
| 169 _setResult(testResult, messageText, stack); | 169 _setResult(testResult, messageText, stack); |
| 170 if (!_doneTeardown) { | 170 if (!_doneTeardown) { |
| 171 _doneTeardown = true; | 171 _doneTeardown = true; |
| 172 if (tearDown != null) { | 172 if (tearDown != null) { |
| 173 var rtn = tearDown(); | 173 var rtn = tearDown(); |
| 174 if (rtn is Future) { | 174 if (rtn is Future) { |
| 175 rtn.then((_) { | 175 rtn.then((_) { |
| 176 _notifyComplete(); | 176 _notifyComplete(); |
| 177 }) | 177 }) |
| 178 .catchError((e) { | 178 .catchError((error) { |
| 179 var trace = getAttachedStackTrace(error); |
| 179 // We don't call fail() as that will potentially result in | 180 // We don't call fail() as that will potentially result in |
| 180 // spurious messages like 'test failed more than once'. | 181 // spurious messages like 'test failed more than once'. |
| 181 _setResult(ERROR, "$description: Test teardown failed: ${e.error}", | 182 _setResult(ERROR, "$description: Test teardown failed: ${error}", |
| 182 e.stackTrace.toString()); | 183 trace == null ? "" : trace.toString()); |
| 183 _notifyComplete(); | 184 _notifyComplete(); |
| 184 }); | 185 }); |
| 185 return; | 186 return; |
| 186 } | 187 } |
| 187 } | 188 } |
| 188 } | 189 } |
| 189 _notifyComplete(); | 190 _notifyComplete(); |
| 190 } | 191 } |
| 191 | 192 |
| 192 void pass() { | 193 void pass() { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 208 void error(String messageText, [String stack = '']) { | 209 void error(String messageText, [String stack = '']) { |
| 209 _complete(ERROR, messageText, stack); | 210 _complete(ERROR, messageText, stack); |
| 210 } | 211 } |
| 211 | 212 |
| 212 void _markCallbackComplete() { | 213 void _markCallbackComplete() { |
| 213 if (--_callbackFunctionsOutstanding == 0 && !isComplete) { | 214 if (--_callbackFunctionsOutstanding == 0 && !isComplete) { |
| 214 pass(); | 215 pass(); |
| 215 } | 216 } |
| 216 } | 217 } |
| 217 } | 218 } |
| OLD | NEW |