| 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 matcher; | 5 part of matcher; |
| 6 | 6 |
| 7 /** The objects thrown by the default failure handler. */ | 7 /** The objects thrown by the default failure handler. */ |
| 8 class TestFailure { | 8 class TestFailure { |
| 9 String _message; | 9 String _message; |
| 10 | 10 |
| 11 get message => _message; | 11 get message => _message; |
| 12 set message(String value) => _message = value; | 12 set message(String value) => _message = value; |
| 13 | 13 |
| 14 TestFailure(String message) : _message = message; | 14 TestFailure(String message) : _message = message; |
| 15 | 15 |
| 16 String toString() => _message; | 16 String toString() => _message; |
| 17 } | 17 } |
| 18 | 18 |
| 19 /** | 19 /** |
| 20 * Useful utility for nesting match states. |
| 21 */ |
| 22 |
| 23 void addStateInfo(Map matchState, Map values) { |
| 24 var innerState = new Map.from(matchState); |
| 25 matchState.clear(); |
| 26 matchState['state'] = innerState; |
| 27 matchState.addAll(values); |
| 28 } |
| 29 |
| 30 /** |
| 20 * Some matchers, like those for Futures and exception testing, | 31 * Some matchers, like those for Futures and exception testing, |
| 21 * can fail in asynchronous sections, and throw exceptions. | 32 * can fail in asynchronous sections, and throw exceptions. |
| 22 * A user of this library will typically want to catch and handle | 33 * A user of this library will typically want to catch and handle |
| 23 * such exceptions. The [wrapAsync] property is a function that | 34 * such exceptions. The [wrapAsync] property is a function that |
| 24 * can wrap callbacks used by these Matchers so that they can be | 35 * can wrap callbacks used by these Matchers so that they can be |
| 25 * used safely. For example, the unittest library will set this | 36 * used safely. For example, the unittest library will set this |
| 26 * to be expectAsync1. By default this is an identity function. | 37 * to be expectAsync1. By default this is an identity function. |
| 27 */ | 38 */ |
| 28 Function wrapAsync = (f, [id]) => f; | 39 Function wrapAsync = (f, [id]) => f; |
| 29 | 40 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 44 * grained control. | 55 * grained control. |
| 45 * | 56 * |
| 46 * In some cases extra diagnostic info can be produced on failure (for | 57 * In some cases extra diagnostic info can be produced on failure (for |
| 47 * example, stack traces on mismatched exceptions). To enable these, | 58 * example, stack traces on mismatched exceptions). To enable these, |
| 48 * [verbose] should be specified as true; | 59 * [verbose] should be specified as true; |
| 49 */ | 60 */ |
| 50 void expect(actual, matcher, {String reason, FailureHandler failureHandler, | 61 void expect(actual, matcher, {String reason, FailureHandler failureHandler, |
| 51 bool verbose : false}) { | 62 bool verbose : false}) { |
| 52 matcher = wrapMatcher(matcher); | 63 matcher = wrapMatcher(matcher); |
| 53 bool doesMatch; | 64 bool doesMatch; |
| 54 var matchState = new MatchState(); | 65 var matchState = {}; |
| 55 try { | 66 try { |
| 56 doesMatch = matcher.matches(actual, matchState); | 67 doesMatch = matcher.matches(actual, matchState); |
| 57 } catch (e, trace) { | 68 } catch (e, trace) { |
| 58 doesMatch = false; | 69 doesMatch = false; |
| 59 if (reason == null) { | 70 if (reason == null) { |
| 60 reason = '${(e is String) ? e : e.toString()} at $trace'; | 71 reason = '${(e is String) ? e : e.toString()} at $trace'; |
| 61 } | 72 } |
| 62 } | 73 } |
| 63 if (!doesMatch) { | 74 if (!doesMatch) { |
| 64 if (failureHandler == null) { | 75 if (failureHandler == null) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 class DefaultFailureHandler implements FailureHandler { | 109 class DefaultFailureHandler implements FailureHandler { |
| 99 DefaultFailureHandler() { | 110 DefaultFailureHandler() { |
| 100 if (_assertErrorFormatter == null) { | 111 if (_assertErrorFormatter == null) { |
| 101 _assertErrorFormatter = _defaultErrorFormatter; | 112 _assertErrorFormatter = _defaultErrorFormatter; |
| 102 } | 113 } |
| 103 } | 114 } |
| 104 void fail(String reason) { | 115 void fail(String reason) { |
| 105 throw new TestFailure(reason); | 116 throw new TestFailure(reason); |
| 106 } | 117 } |
| 107 void failMatch(actual, Matcher matcher, String reason, | 118 void failMatch(actual, Matcher matcher, String reason, |
| 108 MatchState matchState, bool verbose) { | 119 Map matchState, bool verbose) { |
| 109 fail(_assertErrorFormatter(actual, matcher, reason, matchState, verbose)); | 120 fail(_assertErrorFormatter(actual, matcher, reason, matchState, verbose)); |
| 110 } | 121 } |
| 111 } | 122 } |
| 112 | 123 |
| 113 /** | 124 /** |
| 114 * Changes or resets to the default the failure handler for expect() | 125 * Changes or resets to the default the failure handler for expect() |
| 115 * [handler] is a reference to the new handler; if this is omitted | 126 * [handler] is a reference to the new handler; if this is omitted |
| 116 * or null then the failure handler is reset to the default, which | 127 * or null then the failure handler is reset to the default, which |
| 117 * throws [TestFailure]s on [expect] assertion failures. | 128 * throws [TestFailure]s on [expect] assertion failures. |
| 118 */ | 129 */ |
| 119 void configureExpectFailureHandler([FailureHandler handler = null]) { | 130 void configureExpectFailureHandler([FailureHandler handler = null]) { |
| 120 if (handler == null) { | 131 if (handler == null) { |
| 121 handler = new DefaultFailureHandler(); | 132 handler = new DefaultFailureHandler(); |
| 122 } | 133 } |
| 123 _assertFailureHandler = handler; | 134 _assertFailureHandler = handler; |
| 124 } | 135 } |
| 125 | 136 |
| 126 FailureHandler getOrCreateExpectFailureHandler() { | 137 FailureHandler getOrCreateExpectFailureHandler() { |
| 127 if (_assertFailureHandler == null) { | 138 if (_assertFailureHandler == null) { |
| 128 configureExpectFailureHandler(); | 139 configureExpectFailureHandler(); |
| 129 } | 140 } |
| 130 return _assertFailureHandler; | 141 return _assertFailureHandler; |
| 131 } | 142 } |
| 132 | 143 |
| 133 // The error message formatter for failed asserts. | 144 // The error message formatter for failed asserts. |
| 134 ErrorFormatter _assertErrorFormatter = null; | 145 ErrorFormatter _assertErrorFormatter = null; |
| 135 | 146 |
| 136 // The default error formatter implementation. | 147 // The default error formatter implementation. |
| 137 String _defaultErrorFormatter(actual, Matcher matcher, String reason, | 148 String _defaultErrorFormatter(actual, Matcher matcher, String reason, |
| 138 MatchState matchState, bool verbose) { | 149 Map matchState, bool verbose) { |
| 139 var description = new StringDescription(); | 150 var description = new StringDescription(); |
| 140 description.add('Expected: ').addDescriptionOf(matcher).add('\n'); | 151 description.add('Expected: ').addDescriptionOf(matcher).add('\n'); |
| 152 description.add(' Actual: ').addDescriptionOf(actual); |
| 141 | 153 |
| 142 var mismatchDescription = new StringDescription(); | 154 var mismatchDescription = new StringDescription(); |
| 143 matcher.describeMismatch(actual, mismatchDescription, matchState, verbose); | 155 matcher.describeMismatch(actual, mismatchDescription, matchState, verbose); |
| 144 description.add(' But: ') | |
| 145 .add(mismatchDescription.toString()).add('.\n'); | |
| 146 | 156 |
| 147 description.add('Actual: ').addDescriptionOf(actual); | 157 if (mismatchDescription.length > 0) { |
| 158 description.add(' Which: ${mismatchDescription}\n'); |
| 159 } |
| 148 if (reason != null) { | 160 if (reason != null) { |
| 149 description.add(reason).add('\n'); | 161 description.add(reason).add('\n'); |
| 150 } | 162 } |
| 151 return description.toString(); | 163 return description.toString(); |
| 152 } | 164 } |
| 153 | 165 |
| 154 /** | 166 /** |
| 155 * Changes or resets to default the failure message formatter for expect(). | 167 * Changes or resets to default the failure message formatter for expect(). |
| 156 * [formatter] is a reference to the new formatter; if this is omitted or | 168 * [formatter] is a reference to the new formatter; if this is omitted or |
| 157 * null then the failure formatter is reset to the default. The new | 169 * null then the failure formatter is reset to the default. The new |
| 158 * formatter is returned; this allows custom expect handlers to easily | 170 * formatter is returned; this allows custom expect handlers to easily |
| 159 * get a reference to the default formatter. | 171 * get a reference to the default formatter. |
| 160 */ | 172 */ |
| 161 ErrorFormatter configureExpectFormatter([ErrorFormatter formatter = null]) { | 173 ErrorFormatter configureExpectFormatter([ErrorFormatter formatter = null]) { |
| 162 if (formatter == null) { | 174 if (formatter == null) { |
| 163 formatter = _defaultErrorFormatter; | 175 formatter = _defaultErrorFormatter; |
| 164 } | 176 } |
| 165 return _assertErrorFormatter = formatter; | 177 return _assertErrorFormatter = formatter; |
| 166 } | 178 } |
| 167 | 179 |
| OLD | NEW |