Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: pkg/matcher/lib/src/expect.dart

Issue 438473002: pkg/matcher: refactored code around data-structure vs execution-based matchers (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/matcher/lib/src/error_matchers.dart ('k') | pkg/matcher/lib/src/future_matchers.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.expect; 5 library matcher.expect;
6 6
7 import 'core_matchers.dart'; 7 import 'core_matchers.dart';
8 import 'description.dart'; 8 import 'description.dart';
9 import 'interfaces.dart'; 9 import 'interfaces.dart';
10 import 'util.dart';
10 11
11 /// The objects thrown by the default failure handler. 12 /// The objects thrown by the default failure handler.
12 class TestFailure extends Error { 13 class TestFailure extends Error {
13 final String message; 14 final String message;
14 15
15 TestFailure(this.message); 16 TestFailure(this.message);
16 17
17 String toString() => message; 18 String toString() => message;
18 } 19 }
19 20
20 /// Useful utility for nesting match states. 21 /// Failed matches are reported using a default IFailureHandler.
22 /// The default implementation simply throws [TestFailure]s;
23 /// this can be replaced by some other implementation of
24 /// IFailureHandler by calling configureExpectHandler.
25 abstract class FailureHandler {
26 /// This handles failures given a textual decription
27 void fail(String reason);
21 28
22 void addStateInfo(Map matchState, Map values) { 29 /// This handles failures given the actual [value], the [matcher]
23 var innerState = new Map.from(matchState); 30 /// the [reason] (argument from [expect]), some additonal [matchState]
24 matchState.clear(); 31 /// generated by the [matcher], and a verbose flag which controls in
25 matchState['state'] = innerState; 32 /// some cases how much [matchState] information is used. It will use
26 matchState.addAll(values); 33 /// these to create a detailed error message (typically by calling
34 /// an [ErrorFormatter]) and then call [fail] with this message.
35 void failMatch(actual, Matcher matcher, String reason,
36 Map matchState, bool verbose);
27 } 37 }
28 38
39 /// The ErrorFormatter type is used for functions that
40 /// can be used to build up error reports upon [expect] failures.
41 /// There is one built-in implementation ([defaultErrorFormatter])
42 /// which is used by the default failure handler. If the failure handler
43 /// is replaced it may be desirable to replace the [stringDescription]
44 /// error formatter with another.
45 typedef String ErrorFormatter(actual, Matcher matcher, String reason,
46 Map matchState, bool verbose);
47
29 /// Some matchers, like those for Futures and exception testing, 48 /// Some matchers, like those for Futures and exception testing,
30 /// can fail in asynchronous sections, and throw exceptions. 49 /// can fail in asynchronous sections, and throw exceptions.
31 /// A user of this library will typically want to catch and handle 50 /// A user of this library will typically want to catch and handle
32 /// such exceptions. The [wrapAsync] property is a function that 51 /// such exceptions. The [wrapAsync] property is a function that
33 /// can wrap callbacks used by these Matchers so that they can be 52 /// can wrap callbacks used by these Matchers so that they can be
34 /// used safely. For example, the unittest library will set this 53 /// used safely. For example, the unittest library will set this
35 /// to be `expectAsync`. By default this is an identity function. 54 /// to be `expectAsync`. By default this is an identity function.
36 Function wrapAsync = (Function f, [id]) => f; 55 Function wrapAsync = (Function f, [id]) => f;
37 56
38 /// This is the main assertion function. It asserts that [actual] 57 /// This is the main assertion function. It asserts that [actual]
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 } 93 }
75 } 94 }
76 95
77 void fail(String message, {FailureHandler failureHandler}) { 96 void fail(String message, {FailureHandler failureHandler}) {
78 if (failureHandler == null) { 97 if (failureHandler == null) {
79 failureHandler = getOrCreateExpectFailureHandler(); 98 failureHandler = getOrCreateExpectFailureHandler();
80 } 99 }
81 failureHandler.fail(message); 100 failureHandler.fail(message);
82 } 101 }
83 102
84 /// Takes an argument and returns an equivalent matcher.
85 /// If the argument is already a matcher this does nothing,
86 /// else if the argument is a function, it generates a predicate
87 /// function matcher, else it generates an equals matcher.
88 Matcher wrapMatcher(x) {
89 if (x is Matcher) {
90 return x;
91 } else if (x is Function) {
92 return predicate(x);
93 } else {
94 return equals(x);
95 }
96 }
97
98 // The handler for failed asserts. 103 // The handler for failed asserts.
99 FailureHandler _assertFailureHandler = null; 104 FailureHandler _assertFailureHandler = null;
100 105
101 // The default failure handler that throws [TestFailure]s. 106 // The default failure handler that throws [TestFailure]s.
102 class DefaultFailureHandler implements FailureHandler { 107 class DefaultFailureHandler implements FailureHandler {
103 DefaultFailureHandler() { 108 DefaultFailureHandler() {
104 if (_assertErrorFormatter == null) { 109 if (_assertErrorFormatter == null) {
105 _assertErrorFormatter = _defaultErrorFormatter; 110 _assertErrorFormatter = _defaultErrorFormatter;
106 } 111 }
107 } 112 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 /// [formatter] is a reference to the new formatter; if this is omitted or 163 /// [formatter] is a reference to the new formatter; if this is omitted or
159 /// null then the failure formatter is reset to the default. The new 164 /// null then the failure formatter is reset to the default. The new
160 /// formatter is returned; this allows custom expect handlers to easily 165 /// formatter is returned; this allows custom expect handlers to easily
161 /// get a reference to the default formatter. 166 /// get a reference to the default formatter.
162 ErrorFormatter configureExpectFormatter([ErrorFormatter formatter = null]) { 167 ErrorFormatter configureExpectFormatter([ErrorFormatter formatter = null]) {
163 if (formatter == null) { 168 if (formatter == null) {
164 formatter = _defaultErrorFormatter; 169 formatter = _defaultErrorFormatter;
165 } 170 }
166 return _assertErrorFormatter = formatter; 171 return _assertErrorFormatter = formatter;
167 } 172 }
OLDNEW
« no previous file with comments | « pkg/matcher/lib/src/error_matchers.dart ('k') | pkg/matcher/lib/src/future_matchers.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698