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 String _stackTrace; | 44 Trace _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 String get stackTrace => _stackTrace; | 46 Trace 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'; | |
73 if (result == null || result == PASS) { | 72 if (result == null || result == PASS) { |
74 if (e is TestFailure) { | 73 if (e is TestFailure) { |
75 fail("$e", stack); | 74 fail("$e", stack); |
76 } else { | 75 } else { |
77 error("$stage failed: Caught $e", stack); | 76 error("$stage failed: Caught $e", stack); |
78 } | 77 } |
79 } | 78 } |
80 }; | 79 }; |
81 | 80 |
82 /** | 81 /** |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 }).catchError(_errorHandler('Teardown')); | 114 }).catchError(_errorHandler('Teardown')); |
116 } else if (tearDown != null) { | 115 } else if (tearDown != null) { |
117 return tearDown(); | 116 return tearDown(); |
118 } | 117 } |
119 }) | 118 }) |
120 .catchError(_errorHandler('Teardown')); | 119 .catchError(_errorHandler('Teardown')); |
121 } | 120 } |
122 | 121 |
123 // Set the results, notify the config, and return true if this | 122 // Set the results, notify the config, and return true if this |
124 // is the first time the result is being set. | 123 // is the first time the result is being set. |
125 void _setResult(String testResult, String messageText, String stack) { | 124 void _setResult(String testResult, String messageText, stack) { |
126 _message = messageText; | 125 _message = messageText; |
127 _stackTrace = _formatStack(stack); | 126 _stackTrace = _getTrace(stack); |
128 if (result == null) { | 127 if (result == null) { |
129 _result = testResult; | 128 _result = testResult; |
130 _config.onTestResult(this); | 129 _config.onTestResult(this); |
131 } else { | 130 } else { |
132 _result = testResult; | 131 _result = testResult; |
133 _config.onTestResultChanged(this); | 132 _config.onTestResultChanged(this); |
134 } | 133 } |
135 } | 134 } |
136 | 135 |
137 void _complete(String testResult, | 136 void _complete(String testResult, [String messageText = '', stack]) { |
138 [String messageText = '', | |
139 String stack = '']) { | |
140 if (runningTime == null) { | 137 if (runningTime == null) { |
141 // The startTime can be `null` if an error happened during setup. In this | 138 // The startTime can be `null` if an error happened during setup. In this |
142 // case we simply report a running time of 0. | 139 // case we simply report a running time of 0. |
143 if (startTime != null) { | 140 if (startTime != null) { |
144 _runningTime = new DateTime.now().difference(startTime); | 141 _runningTime = new DateTime.now().difference(startTime); |
145 } else { | 142 } else { |
146 _runningTime = const Duration(seconds: 0); | 143 _runningTime = const Duration(seconds: 0); |
147 } | 144 } |
148 } | 145 } |
149 _setResult(testResult, messageText, stack); | 146 _setResult(testResult, messageText, stack); |
150 if (_testComplete != null) { | 147 if (_testComplete != null) { |
151 var t = _testComplete; | 148 var t = _testComplete; |
152 _testComplete = null; | 149 _testComplete = null; |
153 t.complete(this); | 150 t.complete(this); |
154 } | 151 } |
155 } | 152 } |
156 | 153 |
157 void pass() { | 154 void pass() { |
158 _complete(PASS); | 155 _complete(PASS); |
159 } | 156 } |
160 | 157 |
161 void fail(String messageText, [String stack = '']) { | 158 void fail(String messageText, [stack]) { |
162 assert(stack != null); | |
163 if (result != null) { | 159 if (result != null) { |
164 String newMessage = (result == PASS) | 160 String newMessage = (result == PASS) |
165 ? 'Test failed after initially passing: $messageText' | 161 ? 'Test failed after initially passing: $messageText' |
166 : 'Test failed more than once: $messageText'; | 162 : 'Test failed more than once: $messageText'; |
167 // TODO(gram): Should we combine the stack with the old one? | 163 // TODO(gram): Should we combine the stack with the old one? |
168 _complete(ERROR, newMessage, stack); | 164 _complete(ERROR, newMessage, stack); |
169 } else { | 165 } else { |
170 _complete(FAIL, messageText, stack); | 166 _complete(FAIL, messageText, stack); |
171 } | 167 } |
172 } | 168 } |
173 | 169 |
174 void error(String messageText, [String stack = '']) { | 170 void error(String messageText, [stack]) { |
175 assert(stack != null); | |
176 _complete(ERROR, messageText, stack); | 171 _complete(ERROR, messageText, stack); |
177 } | 172 } |
178 | 173 |
179 void _markCallbackComplete() { | 174 void _markCallbackComplete() { |
180 if (--_callbackFunctionsOutstanding == 0 && !isComplete) { | 175 if (--_callbackFunctionsOutstanding == 0 && !isComplete) { |
181 pass(); | 176 pass(); |
182 } | 177 } |
183 } | 178 } |
184 } | 179 } |
OLD | NEW |