| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 library matcher.expect; | 5 library matcher.expect; |
| 6 | 6 |
| 7 import 'core_matchers.dart'; | 7 import 'core_matchers.dart'; |
| 8 import 'description.dart'; | 8 import 'description.dart'; |
| 9 import 'interfaces.dart'; | 9 import 'interfaces.dart'; |
| 10 import 'util.dart'; | 10 import 'util.dart'; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 | 38 |
| 39 /// The ErrorFormatter type is used for functions that | 39 /// The ErrorFormatter type is used for functions that |
| 40 /// can be used to build up error reports upon [expect] failures. | 40 /// can be used to build up error reports upon [expect] failures. |
| 41 /// There is one built-in implementation ([defaultErrorFormatter]) | 41 /// There is one built-in implementation ([defaultErrorFormatter]) |
| 42 /// which is used by the default failure handler. If the failure handler | 42 /// which is used by the default failure handler. If the failure handler |
| 43 /// is replaced it may be desirable to replace the [stringDescription] | 43 /// is replaced it may be desirable to replace the [stringDescription] |
| 44 /// error formatter with another. | 44 /// error formatter with another. |
| 45 typedef String ErrorFormatter(actual, Matcher matcher, String reason, | 45 typedef String ErrorFormatter(actual, Matcher matcher, String reason, |
| 46 Map matchState, bool verbose); | 46 Map matchState, bool verbose); |
| 47 | 47 |
| 48 /// This Function is used by certain Matchers to catch certain exceptions. |
| 49 /// |
| 48 /// Some matchers, like those for Futures and exception testing, | 50 /// Some matchers, like those for Futures and exception testing, |
| 49 /// can fail in asynchronous sections, and throw exceptions. | 51 /// can fail in asynchronous sections, and throw exceptions. |
| 50 /// A user of this library will typically want to catch and handle | 52 /// A user of this library will typically want to catch and handle |
| 51 /// such exceptions. The [wrapAsync] property is a function that | 53 /// such exceptions. The [wrapAsync] property is a function that |
| 52 /// can wrap callbacks used by these Matchers so that they can be | 54 /// can wrap callbacks used by these Matchers so that they can be |
| 53 /// used safely. For example, the unittest library will set this | 55 /// used safely. For example, the unittest library will set this |
| 54 /// to be `expectAsync`. By default this is an identity function. | 56 /// to be `expectAsync`. By default this is an identity function. |
| 55 Function wrapAsync = (Function f, [id]) => f; | 57 Function wrapAsync = (Function f, [id]) => f; |
| 56 | 58 |
| 57 /// This is the main assertion function. It asserts that [actual] | 59 /// Assert that [actual] matches [matcher]. |
| 58 /// matches the [matcher]. [reason] is optional and is typically not | 60 /// |
| 59 /// supplied, as a reason is generated from the matcher; if [reason] | 61 /// This is the main assertion function. [reason] is optional and is typically |
| 62 /// not supplied, as a reason is generated from the matcher; if [reason] |
| 60 /// is included it is appended to the reason generated by the matcher. | 63 /// is included it is appended to the reason generated by the matcher. |
| 61 /// | 64 /// |
| 62 /// [matcher] can be a value in which case it will be wrapped in an | 65 /// [matcher] can be a value in which case it will be wrapped in an |
| 63 /// [equals] matcher. | 66 /// [equals] matcher. |
| 64 /// | 67 /// |
| 65 /// If the assertion fails, then the default behavior is to throw a | 68 /// If the assertion fails, then the default behavior is to throw a |
| 66 /// [TestFailure], but this behavior can be changed by calling | 69 /// [TestFailure], but this behavior can be changed by calling |
| 67 /// [configureExpectFailureHandler] and providing an alternative handler that | 70 /// [configureExpectFailureHandler] and providing an alternative handler that |
| 68 /// implements the [IFailureHandler] interface. It is also possible to | 71 /// implements the [IFailureHandler] interface. It is also possible to |
| 69 /// pass a [failureHandler] to [expect] as a final parameter for fine- | 72 /// pass a [failureHandler] to [expect] as a final parameter for fine- |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 } | 115 } |
| 113 void fail(String reason) { | 116 void fail(String reason) { |
| 114 throw new TestFailure(reason); | 117 throw new TestFailure(reason); |
| 115 } | 118 } |
| 116 void failMatch(actual, Matcher matcher, String reason, | 119 void failMatch(actual, Matcher matcher, String reason, |
| 117 Map matchState, bool verbose) { | 120 Map matchState, bool verbose) { |
| 118 fail(_assertErrorFormatter(actual, matcher, reason, matchState, verbose)); | 121 fail(_assertErrorFormatter(actual, matcher, reason, matchState, verbose)); |
| 119 } | 122 } |
| 120 } | 123 } |
| 121 | 124 |
| 122 /// Changes or resets to the default the failure handler for expect() | 125 /// Changes the default failure handler for [expect]. |
| 126 /// |
| 123 /// [handler] is a reference to the new handler; if this is omitted | 127 /// [handler] is a reference to the new handler; if this is omitted |
| 124 /// or null then the failure handler is reset to the default, which | 128 /// or null then the failure handler is reset to the default, which |
| 125 /// throws [TestFailure]s on [expect] assertion failures. | 129 /// throws [TestFailure]s on [expect] assertion failures. |
| 126 void configureExpectFailureHandler([FailureHandler handler = null]) { | 130 void configureExpectFailureHandler([FailureHandler handler = null]) { |
| 127 if (handler == null) { | 131 if (handler == null) { |
| 128 handler = new DefaultFailureHandler(); | 132 handler = new DefaultFailureHandler(); |
| 129 } | 133 } |
| 130 _assertFailureHandler = handler; | 134 _assertFailureHandler = handler; |
| 131 } | 135 } |
| 132 | 136 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 152 | 156 |
| 153 if (mismatchDescription.length > 0) { | 157 if (mismatchDescription.length > 0) { |
| 154 description.add(' Which: ${mismatchDescription}\n'); | 158 description.add(' Which: ${mismatchDescription}\n'); |
| 155 } | 159 } |
| 156 if (reason != null) { | 160 if (reason != null) { |
| 157 description.add(reason).add('\n'); | 161 description.add(reason).add('\n'); |
| 158 } | 162 } |
| 159 return description.toString(); | 163 return description.toString(); |
| 160 } | 164 } |
| 161 | 165 |
| 162 /// Changes or resets to default the failure message formatter for expect(). | 166 /// Changes the failure message formatter for expect(). |
| 167 /// |
| 163 /// [formatter] is a reference to the new formatter; if this is omitted or | 168 /// [formatter] is a reference to the new formatter; if this is omitted or |
| 164 /// null then the failure formatter is reset to the default. The new | 169 /// null then the failure formatter is reset to the default. The new |
| 165 /// formatter is returned; this allows custom expect handlers to easily | 170 /// formatter is returned; this allows custom expect handlers to easily |
| 166 /// get a reference to the default formatter. | 171 /// get a reference to the default formatter. |
| 167 ErrorFormatter configureExpectFormatter([ErrorFormatter formatter = null]) { | 172 ErrorFormatter configureExpectFormatter([ErrorFormatter formatter = null]) { |
| 168 if (formatter == null) { | 173 if (formatter == null) { |
| 169 formatter = _defaultErrorFormatter; | 174 formatter = _defaultErrorFormatter; |
| 170 } | 175 } |
| 171 return _assertErrorFormatter = formatter; | 176 return _assertErrorFormatter = formatter; |
| 172 } | 177 } |
| OLD | NEW |