| 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 23 matching lines...) Expand all Loading... |
| 34 String _message = ''; | 34 String _message = ''; |
| 35 /** Error or failure message. */ | 35 /** Error or failure message. */ |
| 36 String get message => _message; | 36 String get message => _message; |
| 37 | 37 |
| 38 String _result; | 38 String _result; |
| 39 /** | 39 /** |
| 40 * One of [PASS], [FAIL], [ERROR], or [null] if the test hasn't run yet. | 40 * One of [PASS], [FAIL], [ERROR], or [null] if the test hasn't run yet. |
| 41 */ | 41 */ |
| 42 String get result => _result; | 42 String get result => _result; |
| 43 | 43 |
| 44 Trace _stackTrace; | 44 String _stackTrace; |
| 45 /** Stack trace associated with this test, or [null] if it succeeded. */ | 45 /** Stack trace associated with this test, or [null] if it succeeded. */ |
| 46 Trace get stackTrace => _stackTrace; | 46 String get stackTrace => _stackTrace; |
| 47 | 47 |
| 48 /** The group (or groups) under which this test is running. */ | 48 /** The group (or groups) under which this test is running. */ |
| 49 final String currentGroup; | 49 final String currentGroup; |
| 50 | 50 |
| 51 DateTime _startTime; | 51 DateTime _startTime; |
| 52 DateTime get startTime => _startTime; | 52 DateTime get startTime => _startTime; |
| 53 | 53 |
| 54 Duration _runningTime; | 54 Duration _runningTime; |
| 55 Duration get runningTime => _runningTime; | 55 Duration get runningTime => _runningTime; |
| 56 | 56 |
| 57 bool enabled = true; | 57 bool enabled = true; |
| 58 | 58 |
| 59 bool _doneTeardown = false; | 59 bool _doneTeardown = false; |
| 60 | 60 |
| 61 Completer _testComplete; | 61 Completer _testComplete; |
| 62 | 62 |
| 63 TestCase._internal(this.id, this.description, this.testFunction) | 63 TestCase._internal(this.id, this.description, this.testFunction) |
| 64 : currentGroup = _currentContext.fullName, | 64 : currentGroup = _currentContext.fullName, |
| 65 setUp = _currentContext.testSetup, | 65 setUp = _currentContext.testSetup, |
| 66 tearDown = _currentContext.testTeardown; | 66 tearDown = _currentContext.testTeardown; |
| 67 | 67 |
| 68 bool get isComplete => !enabled || result != null; | 68 bool get isComplete => !enabled || result != null; |
| 69 | 69 |
| 70 Function _errorHandler(String stage) => (e) { | 70 Function _errorHandler(String stage) => (e) { |
| 71 var stack = getAttachedStackTrace(e); | 71 var stack = getAttachedStackTrace(e); |
| 72 stack = (stack == null) ? '' : '$stack'; |
| 72 if (result == null || result == PASS) { | 73 if (result == null || result == PASS) { |
| 73 if (e is TestFailure) { | 74 if (e is TestFailure) { |
| 74 fail("$e", stack); | 75 fail("$e", stack); |
| 75 } else { | 76 } else { |
| 76 error("$stage failed: Caught $e", stack); | 77 error("$stage failed: Caught $e", stack); |
| 77 } | 78 } |
| 78 } | 79 } |
| 79 }; | 80 }; |
| 80 | 81 |
| 81 /** | 82 /** |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 }).catchError(_errorHandler('Teardown')); | 115 }).catchError(_errorHandler('Teardown')); |
| 115 } else if (tearDown != null) { | 116 } else if (tearDown != null) { |
| 116 return tearDown(); | 117 return tearDown(); |
| 117 } | 118 } |
| 118 }) | 119 }) |
| 119 .catchError(_errorHandler('Teardown')); | 120 .catchError(_errorHandler('Teardown')); |
| 120 } | 121 } |
| 121 | 122 |
| 122 // Set the results, notify the config, and return true if this | 123 // Set the results, notify the config, and return true if this |
| 123 // is the first time the result is being set. | 124 // is the first time the result is being set. |
| 124 void _setResult(String testResult, String messageText, stack) { | 125 void _setResult(String testResult, String messageText, String stack) { |
| 125 _message = messageText; | 126 _message = messageText; |
| 126 _stackTrace = _getTrace(stack); | 127 _stackTrace = _formatStack(stack); |
| 127 if (result == null) { | 128 if (result == null) { |
| 128 _result = testResult; | 129 _result = testResult; |
| 129 _config.onTestResult(this); | 130 _config.onTestResult(this); |
| 130 } else { | 131 } else { |
| 131 _result = testResult; | 132 _result = testResult; |
| 132 _config.onTestResultChanged(this); | 133 _config.onTestResultChanged(this); |
| 133 } | 134 } |
| 134 } | 135 } |
| 135 | 136 |
| 136 void _complete(String testResult, [String messageText = '', stack]) { | 137 void _complete(String testResult, |
| 138 [String messageText = '', |
| 139 String stack = '']) { |
| 137 if (runningTime == null) { | 140 if (runningTime == null) { |
| 138 // The startTime can be `null` if an error happened during setup. In this | 141 // The startTime can be `null` if an error happened during setup. In this |
| 139 // case we simply report a running time of 0. | 142 // case we simply report a running time of 0. |
| 140 if (startTime != null) { | 143 if (startTime != null) { |
| 141 _runningTime = new DateTime.now().difference(startTime); | 144 _runningTime = new DateTime.now().difference(startTime); |
| 142 } else { | 145 } else { |
| 143 _runningTime = const Duration(seconds: 0); | 146 _runningTime = const Duration(seconds: 0); |
| 144 } | 147 } |
| 145 } | 148 } |
| 146 _setResult(testResult, messageText, stack); | 149 _setResult(testResult, messageText, stack); |
| 147 if (_testComplete != null) { | 150 if (_testComplete != null) { |
| 148 var t = _testComplete; | 151 var t = _testComplete; |
| 149 _testComplete = null; | 152 _testComplete = null; |
| 150 t.complete(this); | 153 t.complete(this); |
| 151 } | 154 } |
| 152 } | 155 } |
| 153 | 156 |
| 154 void pass() { | 157 void pass() { |
| 155 _complete(PASS); | 158 _complete(PASS); |
| 156 } | 159 } |
| 157 | 160 |
| 158 void fail(String messageText, [stack]) { | 161 void fail(String messageText, [String stack = '']) { |
| 162 assert(stack != null); |
| 159 if (result != null) { | 163 if (result != null) { |
| 160 String newMessage = (result == PASS) | 164 String newMessage = (result == PASS) |
| 161 ? 'Test failed after initially passing: $messageText' | 165 ? 'Test failed after initially passing: $messageText' |
| 162 : 'Test failed more than once: $messageText'; | 166 : 'Test failed more than once: $messageText'; |
| 163 // TODO(gram): Should we combine the stack with the old one? | 167 // TODO(gram): Should we combine the stack with the old one? |
| 164 _complete(ERROR, newMessage, stack); | 168 _complete(ERROR, newMessage, stack); |
| 165 } else { | 169 } else { |
| 166 _complete(FAIL, messageText, stack); | 170 _complete(FAIL, messageText, stack); |
| 167 } | 171 } |
| 168 } | 172 } |
| 169 | 173 |
| 170 void error(String messageText, [stack]) { | 174 void error(String messageText, [String stack = '']) { |
| 175 assert(stack != null); |
| 171 _complete(ERROR, messageText, stack); | 176 _complete(ERROR, messageText, stack); |
| 172 } | 177 } |
| 173 | 178 |
| 174 void _markCallbackComplete() { | 179 void _markCallbackComplete() { |
| 175 if (--_callbackFunctionsOutstanding == 0 && !isComplete) { | 180 if (--_callbackFunctionsOutstanding == 0 && !isComplete) { |
| 176 pass(); | 181 pass(); |
| 177 } | 182 } |
| 178 } | 183 } |
| 179 } | 184 } |
| OLD | NEW |