| 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 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 if (rtn is Future) { | 115 if (rtn is Future) { |
| 116 rtn.then((_) => _runTest()) | 116 rtn.then((_) => _runTest()) |
| 117 .catchError((e) { | 117 .catchError((e) { |
| 118 _prepTest(); | 118 _prepTest(); |
| 119 // Calling error() will result in the tearDown being done. | 119 // Calling error() will result in the tearDown being done. |
| 120 // One could debate whether tearDown should be done after | 120 // One could debate whether tearDown should be done after |
| 121 // a failed setUp. There is no right answer, but doing it | 121 // a failed setUp. There is no right answer, but doing it |
| 122 // seems to be the more conservative approach, because | 122 // seems to be the more conservative approach, because |
| 123 // unittest will not stop at a test failure. | 123 // unittest will not stop at a test failure. |
| 124 var stack = getAttachedStackTrace(e); | 124 var stack = getAttachedStackTrace(e); |
| 125 if (stack == null) stack = ''; |
| 125 error("$description: Test setup failed: $e", "$stack"); | 126 error("$description: Test setup failed: $e", "$stack"); |
| 126 }); | 127 }); |
| 127 } else { | 128 } else { |
| 128 var f = _runTest(); | 129 var f = _runTest(); |
| 129 if (f != null) { | 130 if (f != null) { |
| 130 return f; | 131 return f; |
| 131 } | 132 } |
| 132 } | 133 } |
| 133 if (result == null) { // Not complete. | 134 if (result == null) { // Not complete. |
| 134 _testComplete = new Completer(); | 135 _testComplete = new Completer(); |
| 135 return _testComplete.future; | 136 return _testComplete.future; |
| 136 } | 137 } |
| 137 return null; | 138 return null; |
| 138 } | 139 } |
| 139 | 140 |
| 140 void _notifyComplete() { | 141 void _notifyComplete() { |
| 141 if (_testComplete != null) { | 142 if (_testComplete != null) { |
| 142 _testComplete.complete(this); | 143 _testComplete.complete(this); |
| 143 _testComplete = null; | 144 _testComplete = null; |
| 144 } | 145 } |
| 145 } | 146 } |
| 146 | 147 |
| 147 // Set the results, notify the config, and return true if this | 148 // Set the results, notify the config, and return true if this |
| 148 // is the first time the result is being set. | 149 // is the first time the result is being set. |
| 149 void _setResult(String testResult, String messageText, String stack) { | 150 void _setResult(String testResult, String messageText, String stack) { |
| 150 _message = messageText; | 151 _message = messageText; |
| 151 _stackTrace = stack; | 152 _stackTrace = _formatStack(stack); |
| 152 if (result == null) { | 153 if (result == null) { |
| 153 _result = testResult; | 154 _result = testResult; |
| 154 _config.onTestResult(this); | 155 _config.onTestResult(this); |
| 155 } else { | 156 } else { |
| 156 _result = testResult; | 157 _result = testResult; |
| 157 _config.onTestResultChanged(this); | 158 _config.onTestResultChanged(this); |
| 158 } | 159 } |
| 159 } | 160 } |
| 160 | 161 |
| 161 void _complete(String testResult, | 162 void _complete(String testResult, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 192 } | 193 } |
| 193 } | 194 } |
| 194 _notifyComplete(); | 195 _notifyComplete(); |
| 195 } | 196 } |
| 196 | 197 |
| 197 void pass() { | 198 void pass() { |
| 198 _complete(PASS); | 199 _complete(PASS); |
| 199 } | 200 } |
| 200 | 201 |
| 201 void fail(String messageText, [String stack = '']) { | 202 void fail(String messageText, [String stack = '']) { |
| 203 assert(stack != null); |
| 202 if (result != null) { | 204 if (result != null) { |
| 203 String newMessage = (result == PASS) | 205 String newMessage = (result == PASS) |
| 204 ? 'Test failed after initially passing: $messageText' | 206 ? 'Test failed after initially passing: $messageText' |
| 205 : 'Test failed more than once: $messageText'; | 207 : 'Test failed more than once: $messageText'; |
| 206 // TODO(gram): Should we combine the stack with the old one? | 208 // TODO(gram): Should we combine the stack with the old one? |
| 207 _complete(ERROR, newMessage, stack); | 209 _complete(ERROR, newMessage, stack); |
| 208 } else { | 210 } else { |
| 209 _complete(FAIL, messageText, stack); | 211 _complete(FAIL, messageText, stack); |
| 210 } | 212 } |
| 211 } | 213 } |
| 212 | 214 |
| 213 void error(String messageText, [String stack = '']) { | 215 void error(String messageText, [String stack = '']) { |
| 216 assert(stack != null); |
| 214 _complete(ERROR, messageText, stack); | 217 _complete(ERROR, messageText, stack); |
| 215 } | 218 } |
| 216 | 219 |
| 217 void _markCallbackComplete() { | 220 void _markCallbackComplete() { |
| 218 if (--_callbackFunctionsOutstanding == 0 && !isComplete) { | 221 if (--_callbackFunctionsOutstanding == 0 && !isComplete) { |
| 219 pass(); | 222 pass(); |
| 220 } | 223 } |
| 221 } | 224 } |
| 222 } | 225 } |
| OLD | NEW |