| 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]. [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 or equal to bar"); |
| 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 = null]) { |
| 36 if (matcherOrReason is Matcher) { | 40 if (matcher == null) { |
| 41 // Treat this as an assert(predicate, [reason]). |
| 42 if (!actual) { |
| 43 if (reason == null) { |
| 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 matcher = wrapMatcher(matcher); |
| 37 var doesMatch; | 53 var doesMatch; |
| 38 try { | 54 try { |
| 39 doesMatch = matcherOrReason.matches(actual); | 55 doesMatch = matcher.matches(actual); |
| 40 } catch (var e, var trace) { | 56 } catch (var e, var trace) { |
| 41 doesMatch = false; | 57 doesMatch = false; |
| 42 if (reason == '') { | 58 if (reason == null) { |
| 43 reason = '${(e is String) ? e : e.toString()} at $trace'; | 59 reason = '${(e is String) ? e : e.toString()} at $trace'; |
| 44 } | 60 } |
| 45 } | 61 } |
| 46 if (!doesMatch) { | 62 if (!doesMatch) { |
| 47 // Make sure we have a failure handler configured. | 63 // Make sure we have a failure handler configured. |
| 48 configureExpectHandler(_assertFailureHandler); | 64 configureExpectHandler(_assertFailureHandler); |
| 49 _assertFailureHandler.failMatch(actual, matcherOrReason, reason); | 65 _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 } | 66 } |
| 58 } | 67 } |
| 59 } | 68 } |
| 60 | 69 |
| 61 /** | 70 /** |
| 62 * Takes an argument and returns an equivalent matcher. | 71 * Takes an argument and returns an equivalent matcher. |
| 63 * If the argument is already a matcher this does nothing, else it | 72 * If the argument is already a matcher this does nothing, else it |
| 64 * generates an equals matcher for the argument. | 73 * generates an equals matcher for the argument. |
| 65 */ | 74 */ |
| 66 Matcher wrapMatcher(x) => ((x is Matcher) ? x : equals(x)); | 75 Matcher wrapMatcher(x) => ((x is Matcher) ? x : equals(x)); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 | 107 |
| 99 // The error message formatter for failed asserts. | 108 // The error message formatter for failed asserts. |
| 100 ErrorFormatter _assertErrorFormatter = null; | 109 ErrorFormatter _assertErrorFormatter = null; |
| 101 | 110 |
| 102 // The default error formatter implementation. | 111 // The default error formatter implementation. |
| 103 String _defaultErrorFormatter(actual, Matcher matcher, String reason) { | 112 String _defaultErrorFormatter(actual, Matcher matcher, String reason) { |
| 104 var description = new StringDescription(); | 113 var description = new StringDescription(); |
| 105 description.add('Expected: ').addDescriptionOf(matcher). | 114 description.add('Expected: ').addDescriptionOf(matcher). |
| 106 add('\n but: '); | 115 add('\n but: '); |
| 107 matcher.describeMismatch(actual, description); | 116 matcher.describeMismatch(actual, description); |
| 108 description.add('\n').add(reason).add('\n'); | 117 if (reason != null) { |
| 118 description.add('\n').add(reason).add('\n'); |
| 119 } |
| 109 return description.toString(); | 120 return description.toString(); |
| 110 } | 121 } |
| 111 | 122 |
| 112 /** | 123 /** |
| 113 * Changes or resets to default the failure message formatter for expect(). | 124 * Changes or resets to default the failure message formatter for expect(). |
| 114 * [formatter] is a reference to the new formatter; if this is omitted or | 125 * [formatter] is a reference to the new formatter; if this is omitted or |
| 115 * null then the failure formatter is reset to the default. The new | 126 * null then the failure formatter is reset to the default. The new |
| 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 |