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