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 library matcher.interfaces; | 5 library matcher.interfaces; |
6 | 6 |
7 // To decouple the reporting of errors, and allow for extensibility of | 7 // To decouple the reporting of errors, and allow for extensibility of |
8 // matchers, we make use of some interfaces. | 8 // matchers, we make use of some interfaces. |
9 | 9 |
10 /// The ErrorFormatter type is used for functions that | 10 /** |
11 /// can be used to build up error reports upon [expect] failures. | 11 * The ErrorFormatter type is used for functions that |
12 /// There is one built-in implementation ([defaultErrorFormatter]) | 12 * can be used to build up error reports upon [expect] failures. |
13 /// which is used by the default failure handler. If the failure handler | 13 * There is one built-in implementation ([defaultErrorFormatter]) |
14 /// is replaced it may be desirable to replace the [stringDescription] | 14 * which is used by the default failure handler. If the failure handler |
15 /// error formatter with another. | 15 * is replaced it may be desirable to replace the [stringDescription] |
| 16 * error formatter with another. |
| 17 */ |
16 typedef String ErrorFormatter(actual, Matcher matcher, String reason, | 18 typedef String ErrorFormatter(actual, Matcher matcher, String reason, |
17 Map matchState, bool verbose); | 19 Map matchState, bool verbose); |
18 | 20 |
19 /// Matchers build up their error messages by appending to | 21 /** |
20 /// Description objects. This interface is implemented by | 22 * Matchers build up their error messages by appending to |
21 /// StringDescription. This interface is unlikely to need | 23 * Description objects. This interface is implemented by |
22 /// other implementations, but could be useful to replace in | 24 * StringDescription. This interface is unlikely to need |
23 /// some cases - e.g. language conversion. | 25 * other implementations, but could be useful to replace in |
| 26 * some cases - e.g. language conversion. |
| 27 */ |
24 abstract class Description { | 28 abstract class Description { |
25 int get length; | 29 int get length; |
26 | 30 |
27 /// Change the value of the description. | 31 /** Change the value of the description. */ |
28 Description replace(String text); | 32 Description replace(String text); |
29 | 33 |
30 /// This is used to add arbitrary text to the description. | 34 /** This is used to add arbitrary text to the description. */ |
31 Description add(String text); | 35 Description add(String text); |
32 | 36 |
33 /// This is used to add a meaningful description of a value. | 37 /** This is used to add a meaningful description of a value. */ |
34 Description addDescriptionOf(value); | 38 Description addDescriptionOf(value); |
35 | 39 |
36 /// This is used to add a description of an [Iterable] [list], | 40 /** |
37 /// with appropriate [start] and [end] markers and inter-element [separator]. | 41 * This is used to add a description of an [Iterable] [list], |
| 42 * with appropriate [start] and [end] markers and inter-element [separator]. |
| 43 */ |
38 Description addAll(String start, String separator, String end, Iterable list); | 44 Description addAll(String start, String separator, String end, Iterable list); |
39 } | 45 } |
40 | 46 |
41 /// [expect] Matchers must implement/extend the Matcher class. | 47 /** |
42 /// The base Matcher class has a generic implementation of [describeMismatch] | 48 * [expect] Matchers must implement/extend the Matcher class. |
43 /// so this does not need to be provided unless a more clear description is | 49 * The base Matcher class has a generic implementation of [describeMismatch] |
44 /// required. The other two methods ([matches] and [describe]) | 50 * so this does not need to be provided unless a more clear description is |
45 /// must always be provided as they are highly matcher-specific. | 51 * required. The other two methods ([matches] and [describe]) |
| 52 * must always be provided as they are highly matcher-specific. |
| 53 */ |
46 abstract class Matcher { | 54 abstract class Matcher { |
47 const Matcher(); | 55 const Matcher(); |
48 | 56 |
49 /// This does the matching of the actual vs expected values. | 57 /** |
50 /// [item] is the actual value. [matchState] can be supplied | 58 * This does the matching of the actual vs expected values. |
51 /// and may be used to add details about the mismatch that are too | 59 * [item] is the actual value. [matchState] can be supplied |
52 /// costly to determine in [describeMismatch]. | 60 * and may be used to add details about the mismatch that are too |
| 61 * costly to determine in [describeMismatch]. |
| 62 */ |
53 bool matches(item, Map matchState); | 63 bool matches(item, Map matchState); |
54 | 64 |
55 /// This builds a textual description of the matcher. | 65 /** This builds a textual description of the matcher. */ |
56 Description describe(Description description); | 66 Description describe(Description description); |
57 | 67 |
58 /// This builds a textual description of a specific mismatch. [item] | 68 /** |
59 /// is the value that was tested by [matches]; [matchState] is | 69 * This builds a textual description of a specific mismatch. [item] |
60 /// the [Map] that was passed to and supplemented by [matches] | 70 * is the value that was tested by [matches]; [matchState] is |
61 /// with additional information about the mismact, and [mismatchDescription] | 71 * the [Map] that was passed to and supplemented by [matches] |
62 /// is the [Description] that is being built to decribe the mismatch. | 72 * with additional information about the mismact, and [mismatchDescription] |
63 /// A few matchers make use of the [verbose] flag to provide detailed | 73 * is the [Description] that is being built to decribe the mismatch. |
64 /// information that is not typically included but can be of help in | 74 * A few matchers make use of the [verbose] flag to provide detailed |
65 /// diagnosing failures, such as stack traces. | 75 * information that is not typically included but can be of help in |
| 76 * diagnosing failures, such as stack traces. |
| 77 */ |
66 Description describeMismatch(item, Description mismatchDescription, | 78 Description describeMismatch(item, Description mismatchDescription, |
67 Map matchState, bool verbose) => mismatchDescription; | 79 Map matchState, bool verbose) => mismatchDescription; |
68 } | 80 } |
69 | 81 |
70 /// Failed matches are reported using a default IFailureHandler. | 82 /** |
71 /// The default implementation simply throws [TestFailure]s; | 83 * Failed matches are reported using a default IFailureHandler. |
72 /// this can be replaced by some other implementation of | 84 * The default implementation simply throws [TestFailure]s; |
73 /// IFailureHandler by calling configureExpectHandler. | 85 * this can be replaced by some other implementation of |
| 86 * IFailureHandler by calling configureExpectHandler. |
| 87 */ |
74 abstract class FailureHandler { | 88 abstract class FailureHandler { |
75 /// This handles failures given a textual decription | 89 /** This handles failures given a textual decription */ |
76 void fail(String reason); | 90 void fail(String reason); |
77 | 91 |
78 /// This handles failures given the actual [value], the [matcher] | 92 /** |
79 /// the [reason] (argument from [expect]), some additonal [matchState] | 93 * This handles failures given the actual [value], the [matcher] |
80 /// generated by the [matcher], and a verbose flag which controls in | 94 * the [reason] (argument from [expect]), some additonal [matchState] |
81 /// some cases how much [matchState] information is used. It will use | 95 * generated by the [matcher], and a verbose flag which controls in |
82 /// these to create a detailed error message (typically by calling | 96 * some cases how much [matchState] information is used. It will use |
83 /// an [ErrorFormatter]) and then call [fail] with this message. | 97 * these to create a detailed error message (typically by calling |
| 98 * an [ErrorFormatter]) and then call [fail] with this message. |
| 99 */ |
84 void failMatch(actual, Matcher matcher, String reason, | 100 void failMatch(actual, Matcher matcher, String reason, |
85 Map matchState, bool verbose); | 101 Map matchState, bool verbose); |
86 } | 102 } |
| 103 |
OLD | NEW |