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