Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, 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 * testcase.dart: this file is sourced by unittest.dart. It defines [TestCase] | 8 * Represents the state for an individual unit test. |
| 9 * and assumes unittest defines the type [TestFunction]. | 9 * |
| 10 * Create by calling [test] or [solo_test]. | |
| 10 */ | 11 */ |
| 11 | |
| 12 /** Summarizes information about a single test case. */ | |
| 13 class TestCase { | 12 class TestCase { |
| 14 /** Identifier for this test. */ | 13 /** Identifier for this test. */ |
| 15 final int id; | 14 final int id; |
| 16 | 15 |
| 17 /** A description of what the test is specifying. */ | 16 /** A description of what the test is specifying. */ |
| 18 final String description; | 17 final String description; |
| 19 | 18 |
| 20 /** The setup function to call before the test, if any. */ | 19 /** The setup function to call before the test, if any. */ |
| 21 Function _setUp; | 20 Function _setUp; |
| 22 | 21 |
| 23 Function get setUp => _setUp; | 22 Function get setUp => _setUp; |
| 24 set setUp(Function value) => _setUp = value; | 23 set setUp(Function value) => _setUp = value; |
| 25 | 24 |
| 26 /** The teardown function to call after the test, if any. */ | 25 /** The teardown function to call after the test, if any. */ |
| 27 Function _tearDown; | 26 Function _tearDown; |
| 28 | 27 |
| 29 Function get tearDown => _tearDown; | 28 Function get tearDown => _tearDown; |
| 30 set tearDown(Function value) => _tearDown = value; | 29 set tearDown(Function value) => _tearDown = value; |
|
gram
2013/03/13 16:35:58
I think I have been guilty of this myself, but acc
Siggi Cherem (dart-lang)
2013/03/13 16:46:04
yep - if you expose both read and write access, it
| |
| 31 | 30 |
| 32 /** The body of the test case. */ | 31 /** The body of the test case. */ |
| 33 TestFunction test; | 32 TestFunction testFunction; |
| 34 | 33 |
| 35 /** | 34 /** |
| 36 * Remaining number of callbacks functions that must reach a 'done' state | 35 * Remaining number of callbacks functions that must reach a 'done' state |
| 37 * to wait for before the test completes. | 36 * to wait for before the test completes. |
| 38 */ | 37 */ |
| 39 int callbackFunctionsOutstanding; | 38 int _callbackFunctionsOutstanding = 0; |
| 40 | 39 |
| 40 String _message = ''; | |
| 41 /** Error or failure message. */ | 41 /** Error or failure message. */ |
| 42 String message = ''; | 42 String get message => _message; |
| 43 | 43 |
| 44 String _result; | |
| 44 /** | 45 /** |
| 45 * One of [PASS], [FAIL], [ERROR], or [null] if the test hasn't run yet. | 46 * One of [PASS], [FAIL], [ERROR], or [null] if the test hasn't run yet. |
| 46 */ | 47 */ |
| 47 String result; | 48 String get result => _result; |
| 48 | 49 |
| 49 /** Stack trace associated with this test, or null if it succeeded. */ | 50 String _stackTrace; |
| 50 String stackTrace; | 51 /** Stack trace associated with this test, or [null] if it succeeded. */ |
| 52 String get stackTrace => _stackTrace; | |
| 51 | 53 |
| 52 /** The group (or groups) under which this test is running. */ | 54 /** The group (or groups) under which this test is running. */ |
| 53 final String currentGroup; | 55 final String currentGroup; |
| 54 | 56 |
| 55 DateTime startTime; | 57 DateTime _startTime; |
| 58 DateTime get startTime => _startTime; | |
| 56 | 59 |
| 57 Duration runningTime; | 60 Duration _runningTime; |
| 61 Duration get runningTime => _runningTime; | |
| 58 | 62 |
| 59 bool enabled = true; | 63 bool enabled = true; |
| 60 | 64 |
| 61 bool _doneTeardown = false; | 65 bool _doneTeardown = false; |
| 62 | 66 |
| 63 Completer _testComplete; | 67 Completer _testComplete; |
| 64 | 68 |
| 65 TestCase(this.id, this.description, this.test, | 69 TestCase._internal(this.id, this.description, this.testFunction) |
| 66 this.callbackFunctionsOutstanding) | |
| 67 : currentGroup = _currentGroup, | 70 : currentGroup = _currentGroup, |
| 68 _setUp = _testSetup, | 71 _setUp = _testSetup, |
| 69 _tearDown = _testTeardown; | 72 _tearDown = _testTeardown; |
| 70 | 73 |
| 71 bool get isComplete => !enabled || result != null; | 74 bool get isComplete => !enabled || result != null; |
| 72 | 75 |
| 73 void _prepTest() { | 76 void _prepTest() { |
| 74 _config.onTestStart(this); | 77 _config.onTestStart(this); |
| 75 startTime = new DateTime.now(); | 78 _startTime = new DateTime.now(); |
| 76 runningTime = null; | 79 _runningTime = null; |
| 77 } | 80 } |
| 78 | 81 |
| 79 Future _runTest() { | 82 Future _runTest() { |
| 80 _prepTest(); | 83 _prepTest(); |
| 81 // Increment/decrement callbackFunctionsOutstanding to prevent | 84 // Increment/decrement callbackFunctionsOutstanding to prevent |
| 82 // synchronous 'async' callbacks from causing the test to be | 85 // synchronous 'async' callbacks from causing the test to be |
| 83 // marked as complete before the body is completely executed. | 86 // marked as complete before the body is completely executed. |
| 84 ++callbackFunctionsOutstanding; | 87 ++_callbackFunctionsOutstanding; |
| 85 var f = test(); | 88 var f = testFunction(); |
| 86 --callbackFunctionsOutstanding; | 89 --_callbackFunctionsOutstanding; |
| 87 if (f is Future) { | 90 if (f is Future) { |
| 88 f.then((_) => _finishTest()) | 91 f.then((_) => _finishTest()) |
| 89 .catchError((e) => fail("${e.error}")); | 92 .catchError((e) => fail("${e.error}")); |
| 90 return f; | 93 return f; |
| 91 } else { | 94 } else { |
| 92 _finishTest(); | 95 _finishTest(); |
| 93 return null; | 96 return null; |
| 94 } | 97 } |
| 95 } | 98 } |
| 96 | 99 |
| 97 void _finishTest() { | 100 void _finishTest() { |
| 98 if (result == null && callbackFunctionsOutstanding == 0) { | 101 if (result == null && _callbackFunctionsOutstanding == 0) { |
| 99 pass(); | 102 pass(); |
| 100 } | 103 } |
| 101 } | 104 } |
| 102 | 105 |
| 103 /** | 106 /** |
| 104 * Perform any associated [setUp] function and run the test. Returns | 107 * Perform any associated [_setUp] function and run the test. Returns |
| 105 * a [Future] that can be used to schedule the next test. If the test runs | 108 * a [Future] that can be used to schedule the next test. If the test runs |
| 106 * to completion synchronously, or is disabled, we return null, to | 109 * to completion synchronously, or is disabled, null is returned, to |
| 107 * tell unittest to schedule the next test immediately. | 110 * tell unittest to schedule the next test immediately. |
| 108 */ | 111 */ |
| 109 Future run() { | 112 Future _run() { |
| 110 if (!enabled) return null; | 113 if (!enabled) return null; |
| 111 | 114 |
| 112 result = stackTrace = null; | 115 _result = _stackTrace = null; |
| 113 message = ''; | 116 _message = ''; |
| 114 _doneTeardown = false; | 117 _doneTeardown = false; |
| 115 var rtn = _setUp == null ? null : _setUp(); | 118 var rtn = _setUp == null ? null : _setUp(); |
| 116 if (rtn is Future) { | 119 if (rtn is Future) { |
| 117 rtn.then((_) => _runTest()) | 120 rtn.then((_) => _runTest()) |
| 118 .catchError((e) { | 121 .catchError((e) { |
| 119 _prepTest(); | 122 _prepTest(); |
| 120 // Calling error() will result in the tearDown being done. | 123 // Calling error() will result in the tearDown being done. |
| 121 // One could debate whether tearDown should be done after | 124 // One could debate whether tearDown should be done after |
| 122 // a failed setUp. There is no right answer, but doing it | 125 // a failed setUp. There is no right answer, but doing it |
| 123 // seems to be the more conservative approach, because | 126 // seems to be the more conservative approach, because |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 140 void _notifyComplete() { | 143 void _notifyComplete() { |
| 141 if (_testComplete != null) { | 144 if (_testComplete != null) { |
| 142 _testComplete.complete(this); | 145 _testComplete.complete(this); |
| 143 _testComplete = null; | 146 _testComplete = null; |
| 144 } | 147 } |
| 145 } | 148 } |
| 146 | 149 |
| 147 // Set the results, notify the config, and return true if this | 150 // Set the results, notify the config, and return true if this |
| 148 // is the first time the result is being set. | 151 // is the first time the result is being set. |
| 149 void _setResult(String testResult, String messageText, String stack) { | 152 void _setResult(String testResult, String messageText, String stack) { |
| 150 message = messageText; | 153 _message = messageText; |
| 151 stackTrace = stack; | 154 _stackTrace = stack; |
| 152 if (result == null) { | 155 if (result == null) { |
| 153 result = testResult; | 156 _result = testResult; |
| 154 _config.onTestResult(this); | 157 _config.onTestResult(this); |
| 155 } else { | 158 } else { |
| 156 result = testResult; | 159 _result = testResult; |
| 157 _config.onTestResultChanged(this); | 160 _config.onTestResultChanged(this); |
| 158 } | 161 } |
| 159 } | 162 } |
| 160 | 163 |
| 161 void _complete(String testResult, | 164 void _complete(String testResult, |
| 162 [String messageText = '', | 165 [String messageText = '', |
| 163 String stack = '']) { | 166 String stack = '']) { |
| 164 if (runningTime == null) { | 167 if (runningTime == null) { |
| 165 runningTime = new DateTime.now().difference(startTime); | 168 _runningTime = new DateTime.now().difference(startTime); |
| 166 } | 169 } |
| 167 _setResult(testResult, messageText, stack); | 170 _setResult(testResult, messageText, stack); |
| 168 if (!_doneTeardown) { | 171 if (!_doneTeardown) { |
| 169 _doneTeardown = true; | 172 _doneTeardown = true; |
| 170 if (_tearDown != null) { | 173 if (_tearDown != null) { |
| 171 var rtn = _tearDown(); | 174 var rtn = _tearDown(); |
| 172 if (rtn is Future) { | 175 if (rtn is Future) { |
| 173 rtn.then((_) { | 176 rtn.then((_) { |
| 174 _notifyComplete(); | 177 _notifyComplete(); |
| 175 }) | 178 }) |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 200 _complete(ERROR, newMessage, stack); | 203 _complete(ERROR, newMessage, stack); |
| 201 } else { | 204 } else { |
| 202 _complete(FAIL, messageText, stack); | 205 _complete(FAIL, messageText, stack); |
| 203 } | 206 } |
| 204 } | 207 } |
| 205 | 208 |
| 206 void error(String messageText, [String stack = '']) { | 209 void error(String messageText, [String stack = '']) { |
| 207 _complete(ERROR, messageText, stack); | 210 _complete(ERROR, messageText, stack); |
| 208 } | 211 } |
| 209 | 212 |
| 210 void markCallbackComplete() { | 213 void _markCallbackComplete() { |
| 211 if (--callbackFunctionsOutstanding == 0 && !isComplete) { | 214 if (--_callbackFunctionsOutstanding == 0 && !isComplete) { |
| 212 pass(); | 215 pass(); |
| 213 } | 216 } |
| 214 } | 217 } |
| 215 } | 218 } |
| OLD | NEW |