| 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]. [matcher] is optional and defaults to isTrue, |
| 8 * so expect can be used with a single predicate argument. [reason] | 8 * so expect can be used with a single predicate argument. [reason] |
| 9 * is optional and is typically not supplied if a reasonable matcher is | 9 * is optional and is typically not supplied if a reasonable matcher is |
| 10 * explicitly provided, as a reason can be generated from 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 | 11 * If [reason] is included it is appended to the reason generated |
| 12 * by the matcher. | 12 * by the matcher. |
| 13 * | 13 * |
| 14 * [matcher] can be a value in which case it will be wrapped in an | 14 * [matcher] can be a value in which case it will be wrapped in an |
| 15 * [equals] matcher. | 15 * [equals] matcher. |
| 16 * | 16 * |
| 17 * If the assertion fails, then the default behavior is to throw an | 17 * If the assertion fails, then the default behavior is to throw an |
| 18 * [ExpectException], but this behavior can be changed by calling | 18 * [ExpectException], but this behavior can be changed by calling |
| 19 * [configureExpectFailureHandler] and providing an alternative handler that | 19 * [configureExpectFailureHandler] and providing an alternative handler that |
| 20 * implements the [IFailureHandler] interface. It is also possible to | 20 * implements the [IFailureHandler] interface. It is also possible to |
| 21 * pass a [failureHandler] to [expect] as a final parameter for fine- | 21 * pass a [failureHandler] to [expect] as a final parameter for fine- |
| 22 * grained control. | 22 * grained control. |
| 23 * | 23 * |
| 24 * In some cases extra diagnostic info can be produced on failure (for |
| 25 * example, stack traces on mismatched exceptions). To enable these, |
| 26 * [verbose] should be specified as true; |
| 27 * |
| 24 * expect() is a 3rd generation assertion mechanism, drawing | 28 * expect() is a 3rd generation assertion mechanism, drawing |
| 25 * inspiration from [Hamcrest] and Ladislav Thon's [dart-matchers] | 29 * inspiration from [Hamcrest] and Ladislav Thon's [dart-matchers] |
| 26 * library. | 30 * library. |
| 27 * | 31 * |
| 28 * See [Hamcrest] http://en.wikipedia.org/wiki/Hamcrest | 32 * See [Hamcrest] http://en.wikipedia.org/wiki/Hamcrest |
| 29 * [Hamcrest] http://code.google.com/p/hamcrest/ | 33 * [Hamcrest] http://code.google.com/p/hamcrest/ |
| 30 * [dart-matchers] https://github.com/Ladicek/dart-matchers | 34 * [dart-matchers] https://github.com/Ladicek/dart-matchers |
| 31 */ | 35 */ |
| 32 void expect(actual, [matcher = isTrue, String reason = null, | 36 void expect(actual, [matcher = isTrue, String reason = null, |
| 33 failureHandler = null]) { | 37 FailureHandler failureHandler = null, |
| 38 bool verbose = false]) { |
| 34 matcher = wrapMatcher(matcher); | 39 matcher = wrapMatcher(matcher); |
| 35 var doesMatch; | 40 bool doesMatch; |
| 41 var matchState = new MatchState(); |
| 36 try { | 42 try { |
| 37 doesMatch = matcher.matches(actual); | 43 doesMatch = matcher.matches(actual, matchState); |
| 38 } catch (var e, var trace) { | 44 } catch (var e, var trace) { |
| 39 doesMatch = false; | 45 doesMatch = false; |
| 40 if (reason == null) { | 46 if (reason == null) { |
| 41 reason = '${(e is String) ? e : e.toString()} at $trace'; | 47 reason = '${(e is String) ? e : e.toString()} at $trace'; |
| 42 } | 48 } |
| 43 } | 49 } |
| 44 if (!doesMatch) { | 50 if (!doesMatch) { |
| 45 if (failureHandler == null) { | 51 if (failureHandler == null) { |
| 46 failureHandler = getOrCreateExpectFailureHandler(); | 52 failureHandler = getOrCreateExpectFailureHandler(); |
| 47 } | 53 } |
| 48 failureHandler.failMatch(actual, matcher, reason); | 54 failureHandler.failMatch(actual, matcher, reason, matchState, verbose); |
| 49 } | 55 } |
| 50 } | 56 } |
| 51 | 57 |
| 52 /** | 58 /** |
| 53 * Takes an argument and returns an equivalent matcher. | 59 * Takes an argument and returns an equivalent matcher. |
| 54 * If the argument is already a matcher this does nothing, | 60 * If the argument is already a matcher this does nothing, |
| 55 * else if the argument is a function, it generates a predicate | 61 * else if the argument is a function, it generates a predicate |
| 56 * function matcher, else it generates an equals matcher. | 62 * function matcher, else it generates an equals matcher. |
| 57 */ | 63 */ |
| 58 Matcher wrapMatcher(x) { | 64 Matcher wrapMatcher(x) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 71 // The default failure handler that throws ExpectExceptions. | 77 // The default failure handler that throws ExpectExceptions. |
| 72 class DefaultFailureHandler implements FailureHandler { | 78 class DefaultFailureHandler implements FailureHandler { |
| 73 DefaultFailureHandler() { | 79 DefaultFailureHandler() { |
| 74 if (_assertErrorFormatter == null) { | 80 if (_assertErrorFormatter == null) { |
| 75 _assertErrorFormatter = _defaultErrorFormatter; | 81 _assertErrorFormatter = _defaultErrorFormatter; |
| 76 } | 82 } |
| 77 } | 83 } |
| 78 void fail(String reason) { | 84 void fail(String reason) { |
| 79 throw new ExpectException(reason); | 85 throw new ExpectException(reason); |
| 80 } | 86 } |
| 81 void failMatch(actual, Matcher matcher, String reason) { | 87 void failMatch(actual, Matcher matcher, String reason, |
| 82 fail(_assertErrorFormatter(actual, matcher, reason)); | 88 MatchState matchState, bool verbose) { |
| 89 fail(_assertErrorFormatter(actual, matcher, reason, matchState, verbose)); |
| 83 } | 90 } |
| 84 } | 91 } |
| 85 | 92 |
| 86 /** | 93 /** |
| 87 * Changes or resets to the default the failure handler for expect() | 94 * Changes or resets to the default the failure handler for expect() |
| 88 * [handler] is a reference to the new handler; if this is omitted | 95 * [handler] is a reference to the new handler; if this is omitted |
| 89 * or null then the failure handler is reset to the default, which | 96 * or null then the failure handler is reset to the default, which |
| 90 * throws [ExpectExceptions] on [expect] assertion failures. | 97 * throws [ExpectExceptions] on [expect] assertion failures. |
| 91 */ | 98 */ |
| 92 void configureExpectFailureHandler([FailureHandler handler = null]) { | 99 void configureExpectFailureHandler([FailureHandler handler = null]) { |
| 93 if (handler == null) { | 100 if (handler == null) { |
| 94 handler = new DefaultFailureHandler(); | 101 handler = new DefaultFailureHandler(); |
| 95 } | 102 } |
| 96 _assertFailureHandler = handler; | 103 _assertFailureHandler = handler; |
| 97 } | 104 } |
| 98 | 105 |
| 99 FailureHandler getOrCreateExpectFailureHandler() { | 106 FailureHandler getOrCreateExpectFailureHandler() { |
| 100 if (_assertFailureHandler == null) { | 107 if (_assertFailureHandler == null) { |
| 101 configureExpectFailureHandler(); | 108 configureExpectFailureHandler(); |
| 102 } | 109 } |
| 103 return _assertFailureHandler; | 110 return _assertFailureHandler; |
| 104 } | 111 } |
| 105 | 112 |
| 106 // The error message formatter for failed asserts. | 113 // The error message formatter for failed asserts. |
| 107 ErrorFormatter _assertErrorFormatter = null; | 114 ErrorFormatter _assertErrorFormatter = null; |
| 108 | 115 |
| 109 // The default error formatter implementation. | 116 // The default error formatter implementation. |
| 110 String _defaultErrorFormatter(actual, Matcher matcher, String reason) { | 117 String _defaultErrorFormatter(actual, Matcher matcher, String reason, |
| 118 MatchState matchState, bool verbose) { |
| 111 var description = new StringDescription(); | 119 var description = new StringDescription(); |
| 112 description.add('Expected: ').addDescriptionOf(matcher). | 120 description.add('Expected: ').addDescriptionOf(matcher). |
| 113 add('\n but: '); | 121 add('\n but: '); |
| 114 matcher.describeMismatch(actual, description); | 122 matcher.describeMismatch(actual, description, matchState, verbose); |
| 115 description.add('.\n'); | 123 description.add('.\n'); |
| 124 if (verbose && actual is Iterable) { |
| 125 description.add('Actual: ').addDescriptionOf(actual).add('\n'); |
| 126 } |
| 116 if (reason != null) { | 127 if (reason != null) { |
| 117 description.add(reason).add('\n'); | 128 description.add(reason).add('\n'); |
| 118 } | 129 } |
| 119 return description.toString(); | 130 return description.toString(); |
| 120 } | 131 } |
| 121 | 132 |
| 122 /** | 133 /** |
| 123 * Changes or resets to default the failure message formatter for expect(). | 134 * Changes or resets to default the failure message formatter for expect(). |
| 124 * [formatter] is a reference to the new formatter; if this is omitted or | 135 * [formatter] is a reference to the new formatter; if this is omitted or |
| 125 * null then the failure formatter is reset to the default. The new | 136 * null then the failure formatter is reset to the default. The new |
| 126 * formatter is returned; this allows custom expect handlers to easily | 137 * formatter is returned; this allows custom expect handlers to easily |
| 127 * get a reference to the default formatter. | 138 * get a reference to the default formatter. |
| 128 */ | 139 */ |
| 129 ErrorFormatter configureExpectFormatter([ErrorFormatter formatter = null]) { | 140 ErrorFormatter configureExpectFormatter([ErrorFormatter formatter = null]) { |
| 130 if (formatter == null) { | 141 if (formatter == null) { |
| 131 formatter = _defaultErrorFormatter; | 142 formatter = _defaultErrorFormatter; |
| 132 } | 143 } |
| 133 return _assertErrorFormatter = formatter; | 144 return _assertErrorFormatter = formatter; |
| 134 } | 145 } |
| 135 | 146 |
| OLD | NEW |