OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 * testcase.dart: this file is sourced by unittest.dart. It defines [TestCase] | 8 * testcase.dart: this file is sourced by unittest.dart. It defines [TestCase] |
9 * and assumes unittest defines the type [TestFunction]. | 9 * and assumes unittest defines the type [TestFunction]. |
10 */ | 10 */ |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 _tearDown = _testTeardown; | 69 _tearDown = _testTeardown; |
70 | 70 |
71 bool get isComplete => !enabled || result != null; | 71 bool get isComplete => !enabled || result != null; |
72 | 72 |
73 void _prepTest() { | 73 void _prepTest() { |
74 _config.onTestStart(this); | 74 _config.onTestStart(this); |
75 startTime = new DateTime.now(); | 75 startTime = new DateTime.now(); |
76 runningTime = null; | 76 runningTime = null; |
77 } | 77 } |
78 | 78 |
79 void _runTest() { | 79 Future _runTest() { |
80 _prepTest(); | 80 _prepTest(); |
81 test(); | 81 var f = test(); |
| 82 if (f is Future) { |
| 83 f.then((_) => _finishTest()) |
| 84 .catchError((e) => fail("${e.error}")); |
| 85 return f; |
| 86 } else { |
| 87 _finishTest(); |
| 88 return null; |
| 89 } |
| 90 } |
| 91 |
| 92 void _finishTest() { |
82 if (result == null && callbackFunctionsOutstanding == 0) { | 93 if (result == null && callbackFunctionsOutstanding == 0) { |
83 pass(); | 94 pass(); |
84 } | 95 } |
85 } | 96 } |
86 | 97 |
87 /** | 98 /** |
88 * Perform any associated [setUp] function and run the test. Returns | 99 * Perform any associated [setUp] function and run the test. Returns |
89 * a [Future] that can be used to schedule the next test. If the test runs | 100 * a [Future] that can be used to schedule the next test. If the test runs |
90 * to completion synchronously, or is disabled, we return null, to | 101 * to completion synchronously, or is disabled, we return null, to |
91 * tell unittest to schedule the next test immediately. | 102 * tell unittest to schedule the next test immediately. |
92 */ | 103 */ |
93 Future run() { | 104 Future run() { |
94 if (!enabled) return null; | 105 if (!enabled) return null; |
95 | 106 |
96 result = stackTrace = null; | 107 result = stackTrace = null; |
97 message = ''; | 108 message = ''; |
98 _doneTeardown = false; | 109 _doneTeardown = false; |
99 var rtn = _setUp == null ? null : _setUp(); | 110 var rtn = _setUp == null ? null : _setUp(); |
100 if (rtn is Future) { | 111 if (rtn is Future) { |
101 rtn.then(expectAsync1((_) =>_runTest(), | 112 rtn.then((_) => _runTest()) |
102 id: '[Async setUp completion handler]')) | 113 .catchError((e) { |
103 .catchError((e) { | 114 _prepTest(); |
104 _prepTest(); | 115 // Calling error() will result in the tearDown being done. |
105 // Calling error() will result in the tearDown being done. | 116 // One could debate whether tearDown should be done after |
106 // One could debate whether tearDown should be done after | 117 // a failed setUp. There is no right answer, but doing it |
107 // a failed setUp. There is no right answer, but doing it | 118 // seems to be the more conservative approach, because |
108 // seems to be the more conservative approach, because | 119 // unittest will not stop at a test failure. |
109 // unittest will not stop at a test failure. | 120 error("$description: Test setup failed: ${e.error}"); |
110 error("$description: Test setup failed: ${e.error}"); | 121 }); |
111 }); | |
112 } else { | 122 } else { |
113 _runTest(); | 123 var f = _runTest(); |
| 124 if (f != null) { |
| 125 return f; |
| 126 } |
114 } | 127 } |
115 if (result == null) { // Not complete. | 128 if (result == null) { // Not complete. |
116 _testComplete = new Completer(); | 129 _testComplete = new Completer(); |
117 return _testComplete.future; | 130 return _testComplete.future; |
118 } | 131 } |
119 return null; | 132 return null; |
120 } | 133 } |
121 | 134 |
122 void _notifyComplete() { | 135 void _notifyComplete() { |
123 if (_testComplete != null) { | 136 if (_testComplete != null) { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 } | 200 } |
188 } | 201 } |
189 | 202 |
190 void error(String messageText, [String stack = '']) { | 203 void error(String messageText, [String stack = '']) { |
191 result = ERROR; | 204 result = ERROR; |
192 message = messageText; | 205 message = messageText; |
193 stackTrace = stack; | 206 stackTrace = stack; |
194 _complete(); | 207 _complete(); |
195 } | 208 } |
196 } | 209 } |
OLD | NEW |