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