| 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 /** | 7 /** |
| 8 * MatchState is a simple wrapper around an arbitrary object. | |
| 9 * [Matcher] [matches] methods can use this to store useful | |
| 10 * information upon match failures, and this information will | |
| 11 * be passed to [describeMismatch]. Each [Matcher] is responsible | |
| 12 * for its own use of this state, so the state created by [matches] | |
| 13 * should be consistent with that expected by [describeMismatch] in | |
| 14 * the same [Matcher] class, but can vary between classes. The inner | |
| 15 * state, if set, will typically be a [Map] with a number of key-value | |
| 16 * pairs containing relevant state information. | |
| 17 */ | |
| 18 class MatchState { | |
| 19 var state = null; | |
| 20 | |
| 21 MatchState([this.state]); | |
| 22 } | |
| 23 | |
| 24 /** | |
| 25 * BaseMatcher is the base class for all matchers. To implement a new | 8 * BaseMatcher is the base class for all matchers. To implement a new |
| 26 * matcher, either add a class that implements Matcher or a class that | 9 * matcher, either add a class that implements Matcher or a class that |
| 27 * extends BaseMatcher. Extending BaseMatcher has the benefit that a | 10 * extends BaseMatcher. Extending BaseMatcher has the benefit that a |
| 28 * default implementation of describeMismatch will be provided. | 11 * default implementation of describeMismatch will be provided. |
| 29 */ | 12 */ |
| 30 abstract class BaseMatcher implements Matcher { | 13 abstract class BaseMatcher implements Matcher { |
| 31 const BaseMatcher(); | 14 const BaseMatcher(); |
| 32 | 15 |
| 33 /** | 16 /** |
| 34 * Tests the matcher against a given [item] | 17 * Tests the matcher against a given [item] |
| 35 * and return true if the match succeeds; false otherwise. | 18 * and return true if the match succeeds; false otherwise. |
| 36 * [matchState] may be used to return additional info for | 19 * [matchState] may be used to return additional info for |
| 37 * the use of [describeMismatch]. | 20 * the use of [describeMismatch]. |
| 38 */ | 21 */ |
| 39 bool matches(item, MatchState matchState); | 22 bool matches(item, Map matchState); |
| 40 | 23 |
| 41 /** | 24 /** |
| 42 * Creates a textual description of a matcher, | 25 * Creates a textual description of a matcher, |
| 43 * by appending to [mismatchDescription]. | 26 * by appending to [mismatchDescription]. |
| 44 */ | 27 */ |
| 45 Description describe(Description mismatchDescription); | 28 Description describe(Description mismatchDescription); |
| 46 | 29 |
| 47 /** | 30 /** |
| 48 * Generates a description of the matcher failed for a particular | 31 * Generates a description of the matcher failed for a particular |
| 49 * [item], by appending the description to [mismatchDescription]. | 32 * [item], by appending the description to [mismatchDescription]. |
| 50 * It does not check whether the [item] fails the match, as it is | 33 * It does not check whether the [item] fails the match, as it is |
| 51 * only called after a failed match. There may be additional info | 34 * only called after a failed match. There may be additional info |
| 52 * about the mismatch in [matchState]. | 35 * about the mismatch in [matchState]. |
| 36 * The base matcher does not add anything as the actual value is |
| 37 * typically sufficient, but matchers that can add valuable info |
| 38 * should override this. |
| 53 */ | 39 */ |
| 54 Description describeMismatch(item, Description mismatchDescription, | 40 Description describeMismatch(item, Description mismatchDescription, |
| 55 MatchState matchState, bool verbose) => | 41 Map matchState, bool verbose) => |
| 56 mismatchDescription.add('was ').addDescriptionOf(item); | 42 mismatchDescription; |
| 57 } | 43 } |
| OLD | NEW |