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 /** |
|
eub
2012/06/15 22:30:43
Document that "matcher" may be a value to match fo
| |
| 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]. [reason] is optional and is typically | 7 * matches the [matcher]. [reason] is optional and is typically |
| 8 * not supplied, as a reason can be generated from the matcher. | 8 * not supplied, as a reason can be generated from the matcher. |
| 9 * If [reason] is included it is appended to the reason generated | 9 * If [reason] is included it is appended to the reason generated |
| 10 * by the matcher. | 10 * by the matcher. |
| 11 * | 11 * |
| 12 * If the assertion fails, then the default behavior is to throw an | 12 * If the assertion fails, then the default behavior is to throw an |
| 13 * [ExpectException], but this behavior can be changed by calling | 13 * [ExpectException], but this behavior can be changed by calling |
| 14 * [configureExpectHandler] and providing an alternative handler that | 14 * [configureExpectHandler] and providing an alternative handler that |
| 15 * implements the [IFailureHandler] interface. | 15 * implements the [IFailureHandler] interface. |
| 16 * | 16 * |
| 17 * [expect] allows an alternative call format, providing a Boolean | 17 * [expect] allows an alternative call format, providing a Boolean |
| 18 * predicate as the first argument and an optional reason as the | 18 * predicate as the first argument and an optional reason as a named |
| 19 * second argument. This supports brevity at the expense of detailed | 19 * second argument. This supports brevity at the expense of detailed |
| 20 * error messages. For example, these are equivalent, but the first | 20 * error messages. For example, these are equivalent, but the first |
| 21 * form will give a detailed error message, while the second form will | 21 * form will give a detailed error message, while the second form will |
| 22 * just give a generic assertion failed message: | 22 * just give a generic assertion failed message: |
| 23 * | 23 * |
| 24 * expect(foo, isLessThanOrEqual(bar)); | 24 * expect(foo, isLessThanOrEqual(bar)); |
| 25 * expect(foo <= bar); | 25 * expect(foo <= bar); |
| 26 * | 26 * |
| 27 * A better way of doing the second form is: | |
| 28 * | |
| 29 * expect(foo <= bar, reason: "foo not less than bar"); | |
|
eub
2012/06/15 22:30:43
Utter nitpick: "foo not less than or equal to bar"
gram
2012/06/15 22:40:42
Done.
| |
| 30 * | |
| 27 * expect() is a 3rd generation assertion mechanism, drawing | 31 * expect() is a 3rd generation assertion mechanism, drawing |
| 28 * inspiration from [Hamcrest] and Ladislav Thon's [dart-matchers] | 32 * inspiration from [Hamcrest] and Ladislav Thon's [dart-matchers] |
| 29 * library. | 33 * library. |
| 30 * | 34 * |
| 31 * See [Hamcrest] http://en.wikipedia.org/wiki/Hamcrest | 35 * See [Hamcrest] http://en.wikipedia.org/wiki/Hamcrest |
| 32 * [Hamcrest] http://http://code.google.com/p/hamcrest/ | 36 * [Hamcrest] http://http://code.google.com/p/hamcrest/ |
| 33 * [dart-matchers] https://github.com/Ladicek/dart-matchers | 37 * [dart-matchers] https://github.com/Ladicek/dart-matchers |
| 34 */ | 38 */ |
| 35 void expect(actual, [matcherOrReason = null, String reason = '']) { | 39 void expect(actual, [matcher = null, String reason = '']) { |
|
eub
2012/06/15 22:30:43
How do you like null instead of ''?
gram
2012/06/15 22:40:42
Done.
gram
2012/06/15 22:40:42
Done.
| |
| 36 if (matcherOrReason is Matcher) { | 40 if (matcher == null) { |
| 41 // Treat this as an assert(predicate, reason). | |
|
eub
2012/06/15 22:30:43
[reason]
gram
2012/06/15 22:40:42
Done.
| |
| 42 if (!actual) { | |
| 43 if (reason == '') { | |
| 44 reason = 'Assertion failed'; | |
| 45 } | |
| 46 // Make sure we have a failure handler configured. | |
| 47 configureExpectHandler(_assertFailureHandler); | |
| 48 _assertFailureHandler.fail(reason); | |
| 49 } | |
| 50 } else { | |
| 51 // Treat this as an expect(value, matcher, [reason]). | |
| 52 if (matcher is! Matcher) { | |
|
eub
2012/06/15 22:30:43
This is a duplicate "is" check w/ the one in wrapM
gram
2012/06/15 22:40:42
Done.
| |
| 53 matcher = wrapMatcher(matcher); | |
| 54 } | |
| 37 var doesMatch; | 55 var doesMatch; |
| 38 try { | 56 try { |
| 39 doesMatch = matcherOrReason.matches(actual); | 57 doesMatch = matcher.matches(actual); |
| 40 } catch (var e, var trace) { | 58 } catch (var e, var trace) { |
| 41 doesMatch = false; | 59 doesMatch = false; |
| 42 if (reason == '') { | 60 if (reason == '') { |
| 43 reason = '${(e is String) ? e : e.toString()} at $trace'; | 61 reason = '${(e is String) ? e : e.toString()} at $trace'; |
|
eub
2012/06/15 22:30:43
It would be nice if String had a no-op toString, e
gram
2012/06/15 22:40:42
:-)
| |
| 44 } | 62 } |
| 45 } | 63 } |
| 46 if (!doesMatch) { | 64 if (!doesMatch) { |
| 47 // Make sure we have a failure handler configured. | 65 // Make sure we have a failure handler configured. |
| 48 configureExpectHandler(_assertFailureHandler); | 66 configureExpectHandler(_assertFailureHandler); |
| 49 _assertFailureHandler.failMatch(actual, matcherOrReason, reason); | 67 _assertFailureHandler.failMatch(actual, matcher, reason); |
| 50 } | |
| 51 } else { | |
| 52 if (!actual) { | |
| 53 reason = (matcherOrReason == null) ? 'Assertion failed' : matcherOrReason; | |
| 54 // Make sure we have a failure handler configured. | |
| 55 configureExpectHandler(_assertFailureHandler); | |
| 56 _assertFailureHandler.fail(reason); | |
| 57 } | 68 } |
| 58 } | 69 } |
| 59 } | 70 } |
| 60 | 71 |
| 61 /** | 72 /** |
| 62 * Takes an argument and returns an equivalent matcher. | 73 * Takes an argument and returns an equivalent matcher. |
| 63 * If the argument is already a matcher this does nothing, else it | 74 * If the argument is already a matcher this does nothing, else it |
| 64 * generates an equals matcher for the argument. | 75 * generates an equals matcher for the argument. |
| 65 */ | 76 */ |
| 66 Matcher wrapMatcher(x) => ((x is Matcher) ? x : equals(x)); | 77 Matcher wrapMatcher(x) => ((x is Matcher) ? x : equals(x)); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 * formatter is returned; this allows custom expect handlers to easily | 127 * formatter is returned; this allows custom expect handlers to easily |
| 117 * get a reference to the default formatter. | 128 * get a reference to the default formatter. |
| 118 */ | 129 */ |
| 119 ErrorFormatter configureExpectFormatter([ErrorFormatter formatter = null]) { | 130 ErrorFormatter configureExpectFormatter([ErrorFormatter formatter = null]) { |
| 120 if (formatter == null) { | 131 if (formatter == null) { |
| 121 formatter = _defaultErrorFormatter; | 132 formatter = _defaultErrorFormatter; |
| 122 } | 133 } |
| 123 return _assertErrorFormatter = formatter; | 134 return _assertErrorFormatter = formatter; |
| 124 } | 135 } |
| 125 | 136 |
| OLD | NEW |