| 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]. |
| 11 */ | 11 */ |
| 12 class TestCase { | 12 class TestCase { |
| 13 /** Identifier for this test. */ | 13 /** Identifier for this test. */ |
| 14 final int id; | 14 final int id; |
| 15 | 15 |
| 16 /** A description of what the test is specifying. */ | 16 /** A description of what the test is specifying. */ |
| 17 final String description; | 17 final String description; |
| 18 | 18 |
| 19 /** The setup function to call before the test, if any. */ | 19 /** The setup function to call before the test, if any. */ |
| 20 Function setUp; | 20 final Function _setUp; |
| 21 | 21 |
| 22 /** The teardown function to call after the test, if any. */ | 22 /** The teardown function to call after the test, if any. */ |
| 23 Function tearDown; | 23 final Function _tearDown; |
| 24 | 24 |
| 25 /** The body of the test case. */ | 25 /** The body of the test case. */ |
| 26 TestFunction testFunction; | 26 final TestFunction _testFunction; |
| 27 | 27 |
| 28 /** | 28 /** |
| 29 * Remaining number of callbacks functions that must reach a 'done' state | 29 * Remaining number of callbacks functions that must reach a 'done' state |
| 30 * to wait for before the test completes. | 30 * to wait for before the test completes. |
| 31 */ | 31 */ |
| 32 int _callbackFunctionsOutstanding = 0; | 32 int _callbackFunctionsOutstanding = 0; |
| 33 | 33 |
| 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; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 56 | 56 |
| 57 Duration _runningTime; | 57 Duration _runningTime; |
| 58 Duration get runningTime => _runningTime; | 58 Duration get runningTime => _runningTime; |
| 59 | 59 |
| 60 bool enabled = true; | 60 bool enabled = true; |
| 61 | 61 |
| 62 bool _doneTeardown = false; | 62 bool _doneTeardown = false; |
| 63 | 63 |
| 64 Completer _testComplete; | 64 Completer _testComplete; |
| 65 | 65 |
| 66 TestCase._internal(this.id, this.description, this.testFunction) | 66 TestCase._internal(this.id, this.description, this._testFunction) |
| 67 : currentGroup = _currentContext.fullName, | 67 : currentGroup = _currentContext.fullName, |
| 68 setUp = _currentContext.testSetup, | 68 _setUp = _currentContext.testSetup, |
| 69 tearDown = _currentContext.testTeardown; | 69 _tearDown = _currentContext.testTeardown; |
| 70 | 70 |
| 71 bool get isComplete => !enabled || result != null; | 71 bool get isComplete => !enabled || result != null; |
| 72 | 72 |
| 73 Function _errorHandler(String stage) => (e, stack) { | 73 Function _errorHandler(String stage) => (e, stack) { |
| 74 if (stack == null && e is Error) { | 74 if (stack == null && e is Error) { |
| 75 stack = e.stackTrace; | 75 stack = e.stackTrace; |
| 76 } | 76 } |
| 77 if (result == null || result == PASS) { | 77 if (result == null || result == PASS) { |
| 78 if (e is TestFailure) { | 78 if (e is TestFailure) { |
| 79 fail("$e", stack); | 79 fail("$e", stack); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 90 * tell unittest to schedule the next test immediately. | 90 * tell unittest to schedule the next test immediately. |
| 91 */ | 91 */ |
| 92 Future _run() { | 92 Future _run() { |
| 93 if (!enabled) return new Future.value(); | 93 if (!enabled) return new Future.value(); |
| 94 | 94 |
| 95 _result = _stackTrace = null; | 95 _result = _stackTrace = null; |
| 96 _message = ''; | 96 _message = ''; |
| 97 | 97 |
| 98 // Avoid calling [new Future] to avoid issue 11911. | 98 // Avoid calling [new Future] to avoid issue 11911. |
| 99 return new Future.value().then((_) { | 99 return new Future.value().then((_) { |
| 100 if (setUp != null) return setUp(); | 100 if (_setUp != null) return _setUp(); |
| 101 }).catchError(_errorHandler('Setup')) | 101 }).catchError(_errorHandler('Setup')) |
| 102 .then((_) { | 102 .then((_) { |
| 103 // Skip the test if setup failed. | 103 // Skip the test if setup failed. |
| 104 if (result != null) return new Future.value(); | 104 if (result != null) return new Future.value(); |
| 105 _config.onTestStart(this); | 105 _config.onTestStart(this); |
| 106 _startTime = new DateTime.now(); | 106 _startTime = new DateTime.now(); |
| 107 _runningTime = null; | 107 _runningTime = null; |
| 108 ++_callbackFunctionsOutstanding; | 108 ++_callbackFunctionsOutstanding; |
| 109 return testFunction(); | 109 return _testFunction(); |
| 110 }) | 110 }) |
| 111 .catchError(_errorHandler('Test')) | 111 .catchError(_errorHandler('Test')) |
| 112 .then((_) { | 112 .then((_) { |
| 113 _markCallbackComplete(); | 113 _markCallbackComplete(); |
| 114 if (result == null) { | 114 if (result == null) { |
| 115 // Outstanding callbacks exist; we need to return a Future. | 115 // Outstanding callbacks exist; we need to return a Future. |
| 116 _testComplete = new Completer(); | 116 _testComplete = new Completer(); |
| 117 return _testComplete.future.whenComplete(() { | 117 return _testComplete.future.whenComplete(() { |
| 118 if (tearDown != null) { | 118 if (_tearDown != null) { |
| 119 return tearDown(); | 119 return _tearDown(); |
| 120 } | 120 } |
| 121 }).catchError(_errorHandler('Teardown')); | 121 }).catchError(_errorHandler('Teardown')); |
| 122 } else if (tearDown != null) { | 122 } else if (_tearDown != null) { |
| 123 return tearDown(); | 123 return _tearDown(); |
| 124 } | 124 } |
| 125 }) | 125 }) |
| 126 .catchError(_errorHandler('Teardown')); | 126 .catchError(_errorHandler('Teardown')); |
| 127 } | 127 } |
| 128 | 128 |
| 129 // Set the results, notify the config, and return true if this | 129 // Set the results, notify the config, and return true if this |
| 130 // is the first time the result is being set. | 130 // is the first time the result is being set. |
| 131 void _setResult(String testResult, String messageText, StackTrace stack) { | 131 void _setResult(String testResult, String messageText, StackTrace stack) { |
| 132 _message = messageText; | 132 _message = messageText; |
| 133 _stackTrace = _getTrace(stack); | 133 _stackTrace = _getTrace(stack); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 } | 181 } |
| 182 | 182 |
| 183 void _markCallbackComplete() { | 183 void _markCallbackComplete() { |
| 184 if (--_callbackFunctionsOutstanding == 0 && !isComplete) { | 184 if (--_callbackFunctionsOutstanding == 0 && !isComplete) { |
| 185 pass(); | 185 pass(); |
| 186 } | 186 } |
| 187 } | 187 } |
| 188 | 188 |
| 189 String toString() => _result != null ? "$description: $result" : description; | 189 String toString() => _result != null ? "$description: $result" : description; |
| 190 } | 190 } |
| OLD | NEW |