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 part of unittest; | 5 part of matcher; |
| 6 |
| 7 /** |
| 8 * Some matchers, like those for Futures and exception testing, |
| 9 * can fail in asynchronous sections, and throw exceptions. |
| 10 * A user of this library will typically want to catch and handle |
| 11 * such exceptions. The [wrapAsync] property is a function that |
| 12 * can wrap callbacks used by these Matchers so that they can be |
| 13 * used safely. For example, the unittest library will set this |
| 14 * to be expectAsync1. By default this is an identity function. |
| 15 */ |
| 16 Function wrapAsync = (f) => f; |
6 | 17 |
7 /** | 18 /** |
8 * This is the main assertion function. It asserts that [actual] | 19 * This is the main assertion function. It asserts that [actual] |
9 * matches the [matcher]. [reason] is optional and is typically not | 20 * matches the [matcher]. [reason] is optional and is typically not |
10 * supplied, as a reason is generated from the matcher; if [reason] | 21 * supplied, as a reason is generated from the matcher; if [reason] |
11 * is included it is appended to the reason generated by the matcher. | 22 * is included it is appended to the reason generated by the matcher. |
12 * | 23 * |
13 * [matcher] can be a value in which case it will be wrapped in an | 24 * [matcher] can be a value in which case it will be wrapped in an |
14 * [equals] matcher. | 25 * [equals] matcher. |
15 * | 26 * |
16 * If the assertion fails, then the default behavior is to throw an | 27 * If the assertion fails, then the default behavior is to throw an |
17 * [ExpectException], but this behavior can be changed by calling | 28 * [ExpectException], but this behavior can be changed by calling |
18 * [configureExpectFailureHandler] and providing an alternative handler that | 29 * [configureExpectFailureHandler] and providing an alternative handler that |
19 * implements the [IFailureHandler] interface. It is also possible to | 30 * implements the [IFailureHandler] interface. It is also possible to |
20 * pass a [failureHandler] to [expect] as a final parameter for fine- | 31 * pass a [failureHandler] to [expect] as a final parameter for fine- |
21 * grained control. | 32 * grained control. |
22 * | 33 * |
23 * In some cases extra diagnostic info can be produced on failure (for | 34 * In some cases extra diagnostic info can be produced on failure (for |
24 * example, stack traces on mismatched exceptions). To enable these, | 35 * example, stack traces on mismatched exceptions). To enable these, |
25 * [verbose] should be specified as true; | 36 * [verbose] should be specified as true; |
26 * | |
27 * expect() is a 3rd generation assertion mechanism, drawing | |
28 * inspiration from [Hamcrest] and Ladislav Thon's [dart-matchers] | |
29 * library. | |
30 * | |
31 * See [Hamcrest] http://en.wikipedia.org/wiki/Hamcrest | |
32 * [Hamcrest] http://code.google.com/p/hamcrest/ | |
33 * [dart-matchers] https://github.com/Ladicek/dart-matchers | |
34 */ | 37 */ |
35 void expect(actual, matcher, {String reason, FailureHandler failureHandler, | 38 void expect(actual, matcher, {String reason, FailureHandler failureHandler, |
36 bool verbose : false}) { | 39 bool verbose : false}) { |
37 matcher = wrapMatcher(matcher); | 40 matcher = wrapMatcher(matcher); |
38 bool doesMatch; | 41 bool doesMatch; |
39 var matchState = new MatchState(); | 42 var matchState = new MatchState(); |
40 try { | 43 try { |
41 doesMatch = matcher.matches(actual, matchState); | 44 doesMatch = matcher.matches(actual, matchState); |
42 } catch (e, trace) { | 45 } catch (e, trace) { |
43 doesMatch = false; | 46 doesMatch = false; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 * formatter is returned; this allows custom expect handlers to easily | 138 * formatter is returned; this allows custom expect handlers to easily |
136 * get a reference to the default formatter. | 139 * get a reference to the default formatter. |
137 */ | 140 */ |
138 ErrorFormatter configureExpectFormatter([ErrorFormatter formatter = null]) { | 141 ErrorFormatter configureExpectFormatter([ErrorFormatter formatter = null]) { |
139 if (formatter == null) { | 142 if (formatter == null) { |
140 formatter = _defaultErrorFormatter; | 143 formatter = _defaultErrorFormatter; |
141 } | 144 } |
142 return _assertErrorFormatter = formatter; | 145 return _assertErrorFormatter = formatter; |
143 } | 146 } |
144 | 147 |
OLD | NEW |