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 /// Represents the state for an individual unit test. |
8 * Represents the state for an individual unit test. | 8 /// |
9 * | 9 /// Create by calling [test] or [solo_test]. |
10 * Create by calling [test] or [solo_test]. | |
11 */ | |
12 class TestCase { | 10 class TestCase { |
13 /** Identifier for this test. */ | 11 /// Identifier for this test. |
14 final int id; | 12 final int id; |
15 | 13 |
16 /** A description of what the test is specifying. */ | 14 /// A description of what the test is specifying. |
17 final String description; | 15 final String description; |
18 | 16 |
19 /** The setup function to call before the test, if any. */ | 17 /// The setup function to call before the test, if any. |
20 final Function _setUp; | 18 final Function _setUp; |
21 | 19 |
22 /** The teardown function to call after the test, if any. */ | 20 /// The teardown function to call after the test, if any. |
23 final Function _tearDown; | 21 final Function _tearDown; |
24 | 22 |
25 /** The body of the test case. */ | 23 /// The body of the test case. |
26 final TestFunction _testFunction; | 24 final TestFunction _testFunction; |
27 | 25 |
28 /** | 26 /// Remaining number of callbacks functions that must reach a 'done' state |
29 * Remaining number of callbacks functions that must reach a 'done' state | 27 /// to wait for before the test completes. |
30 * to wait for before the test completes. | |
31 */ | |
32 int _callbackFunctionsOutstanding = 0; | 28 int _callbackFunctionsOutstanding = 0; |
33 | 29 |
34 String _message = ''; | 30 String _message = ''; |
35 /** Error or failure message. */ | 31 /// Error or failure message. |
36 String get message => _message; | 32 String get message => _message; |
37 | 33 |
38 String _result; | 34 String _result; |
39 /** | 35 /// 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 */ | |
42 String get result => _result; | 36 String get result => _result; |
43 | 37 |
44 /** Returns whether this test case passed. */ | 38 /// Returns whether this test case passed. |
45 bool get passed => _result == PASS; | 39 bool get passed => _result == PASS; |
46 | 40 |
47 StackTrace _stackTrace; | 41 StackTrace _stackTrace; |
48 /** Stack trace associated with this test, or [:null:] if it succeeded. */ | 42 /// Stack trace associated with this test, or [:null:] if it succeeded. |
49 StackTrace get stackTrace => _stackTrace; | 43 StackTrace get stackTrace => _stackTrace; |
50 | 44 |
51 /** The group (or groups) under which this test is running. */ | 45 /// The group (or groups) under which this test is running. |
52 final String currentGroup; | 46 final String currentGroup; |
53 | 47 |
54 DateTime _startTime; | 48 DateTime _startTime; |
55 DateTime get startTime => _startTime; | 49 DateTime get startTime => _startTime; |
56 | 50 |
57 Duration _runningTime; | 51 Duration _runningTime; |
58 Duration get runningTime => _runningTime; | 52 Duration get runningTime => _runningTime; |
59 | 53 |
60 bool _enabled = true; | 54 bool _enabled = true; |
61 | 55 |
(...skipping 16 matching lines...) Expand all Loading... |
78 } | 72 } |
79 if (result == null || result == PASS) { | 73 if (result == null || result == PASS) { |
80 if (e is TestFailure) { | 74 if (e is TestFailure) { |
81 _fail("$e", stack); | 75 _fail("$e", stack); |
82 } else { | 76 } else { |
83 _error("$stage failed: Caught $e", stack); | 77 _error("$stage failed: Caught $e", stack); |
84 } | 78 } |
85 } | 79 } |
86 }; | 80 }; |
87 | 81 |
88 /** | 82 /// Perform any associated [_setUp] function and run the test. Returns |
89 * Perform any associated [_setUp] function and run the test. Returns | 83 /// a [Future] that can be used to schedule the next test. If the test runs |
90 * a [Future] that can be used to schedule the next test. If the test runs | 84 /// to completion synchronously, or is disabled, null is returned, to |
91 * to completion synchronously, or is disabled, null is returned, to | 85 /// tell unittest to schedule the next test immediately. |
92 * tell unittest to schedule the next test immediately. | |
93 */ | |
94 Future _run() { | 86 Future _run() { |
95 if (!enabled) return new Future.value(); | 87 if (!enabled) return new Future.value(); |
96 | 88 |
97 _result = _stackTrace = null; | 89 _result = _stackTrace = null; |
98 _message = ''; | 90 _message = ''; |
99 | 91 |
100 // Avoid calling [new Future] to avoid issue 11911. | 92 // Avoid calling [new Future] to avoid issue 11911. |
101 return new Future.value().then((_) { | 93 return new Future.value().then((_) { |
102 if (_setUp != null) return _setUp(); | 94 if (_setUp != null) return _setUp(); |
103 }).catchError(_errorHandler('Setup')) | 95 }).catchError(_errorHandler('Setup')).then((_) { |
104 .then((_) { | 96 // Skip the test if setup failed. |
105 // Skip the test if setup failed. | 97 if (result != null) return new Future.value(); |
106 if (result != null) return new Future.value(); | 98 _config.onTestStart(this); |
107 _config.onTestStart(this); | 99 _startTime = new DateTime.now(); |
108 _startTime = new DateTime.now(); | 100 _runningTime = null; |
109 _runningTime = null; | 101 ++_callbackFunctionsOutstanding; |
110 ++_callbackFunctionsOutstanding; | 102 return _testFunction(); |
111 return _testFunction(); | 103 }).catchError(_errorHandler('Test')).then((_) { |
112 }) | 104 _markCallbackComplete(); |
113 .catchError(_errorHandler('Test')) | 105 if (result == null) { |
114 .then((_) { | 106 // Outstanding callbacks exist; we need to return a Future. |
115 _markCallbackComplete(); | 107 _testComplete = new Completer(); |
116 if (result == null) { | 108 return _testComplete.future.whenComplete(() { |
117 // Outstanding callbacks exist; we need to return a Future. | 109 if (_tearDown != null) { |
118 _testComplete = new Completer(); | |
119 return _testComplete.future.whenComplete(() { | |
120 if (_tearDown != null) { | |
121 return _tearDown(); | |
122 } | |
123 }).catchError(_errorHandler('Teardown')); | |
124 } else if (_tearDown != null) { | |
125 return _tearDown(); | 110 return _tearDown(); |
126 } | 111 } |
127 }) | 112 }).catchError(_errorHandler('Teardown')); |
128 .catchError(_errorHandler('Teardown')); | 113 } else if (_tearDown != null) { |
| 114 return _tearDown(); |
| 115 } |
| 116 }).catchError(_errorHandler('Teardown')); |
129 } | 117 } |
130 | 118 |
131 // Set the results, notify the config, and return true if this | 119 // Set the results, notify the config, and return true if this |
132 // is the first time the result is being set. | 120 // is the first time the result is being set. |
133 void _setResult(String testResult, String messageText, StackTrace stack) { | 121 void _setResult(String testResult, String messageText, StackTrace stack) { |
134 _message = messageText; | 122 _message = messageText; |
135 _stackTrace = _getTrace(stack); | 123 _stackTrace = _getTrace(stack); |
136 if (_stackTrace == null) _stackTrace = stack; | 124 if (_stackTrace == null) _stackTrace = stack; |
137 if (result == null) { | 125 if (result == null) { |
138 _result = testResult; | 126 _result = testResult; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 } | 171 } |
184 | 172 |
185 void _markCallbackComplete() { | 173 void _markCallbackComplete() { |
186 if (--_callbackFunctionsOutstanding == 0 && !isComplete) { | 174 if (--_callbackFunctionsOutstanding == 0 && !isComplete) { |
187 _pass(); | 175 _pass(); |
188 } | 176 } |
189 } | 177 } |
190 | 178 |
191 String toString() => _result != null ? "$description: $result" : description; | 179 String toString() => _result != null ? "$description: $result" : description; |
192 } | 180 } |
OLD | NEW |