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

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

Issue 12335113: Unit test changes: (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 9 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/unittest/lib/src/config.dart ('k') | pkg/unittest/lib/src/interfaces.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 part of matcher; 5 part of matcher;
6 6
7 /** The objects thrown by the default failure handler. */
8 class TestFailure {
9 String _message;
10
11 get message => _message;
12 set message(String value) => _message = value;
13
14 TestFailure(String message) : _message = message;
15
16 String toString() => _message;
17 }
18
7 /** 19 /**
8 * Some matchers, like those for Futures and exception testing, 20 * Some matchers, like those for Futures and exception testing,
9 * can fail in asynchronous sections, and throw exceptions. 21 * can fail in asynchronous sections, and throw exceptions.
10 * A user of this library will typically want to catch and handle 22 * A user of this library will typically want to catch and handle
11 * such exceptions. The [wrapAsync] property is a function that 23 * such exceptions. The [wrapAsync] property is a function that
12 * can wrap callbacks used by these Matchers so that they can be 24 * can wrap callbacks used by these Matchers so that they can be
13 * used safely. For example, the unittest library will set this 25 * used safely. For example, the unittest library will set this
14 * to be expectAsync1. By default this is an identity function. 26 * to be expectAsync1. By default this is an identity function.
15 */ 27 */
16 Function wrapAsync = (f) => f; 28 Function wrapAsync = (f) => f;
17 29
18 /** 30 /**
19 * This is the main assertion function. It asserts that [actual] 31 * This is the main assertion function. It asserts that [actual]
20 * matches the [matcher]. [reason] is optional and is typically not 32 * matches the [matcher]. [reason] is optional and is typically not
21 * supplied, as a reason is generated from the matcher; if [reason] 33 * supplied, as a reason is generated from the matcher; if [reason]
22 * is included it is appended to the reason generated by the matcher. 34 * is included it is appended to the reason generated by the matcher.
23 * 35 *
24 * [matcher] can be a value in which case it will be wrapped in an 36 * [matcher] can be a value in which case it will be wrapped in an
25 * [equals] matcher. 37 * [equals] matcher.
26 * 38 *
27 * If the assertion fails, then the default behavior is to throw an 39 * If the assertion fails, then the default behavior is to throw a
28 * [ExpectException], but this behavior can be changed by calling 40 * [TestFailure], but this behavior can be changed by calling
29 * [configureExpectFailureHandler] and providing an alternative handler that 41 * [configureExpectFailureHandler] and providing an alternative handler that
30 * implements the [IFailureHandler] interface. It is also possible to 42 * implements the [IFailureHandler] interface. It is also possible to
31 * pass a [failureHandler] to [expect] as a final parameter for fine- 43 * pass a [failureHandler] to [expect] as a final parameter for fine-
32 * grained control. 44 * grained control.
33 * 45 *
34 * In some cases extra diagnostic info can be produced on failure (for 46 * In some cases extra diagnostic info can be produced on failure (for
35 * example, stack traces on mismatched exceptions). To enable these, 47 * example, stack traces on mismatched exceptions). To enable these,
36 * [verbose] should be specified as true; 48 * [verbose] should be specified as true;
37 */ 49 */
38 void expect(actual, matcher, {String reason, FailureHandler failureHandler, 50 void expect(actual, matcher, {String reason, FailureHandler failureHandler,
(...skipping 29 matching lines...) Expand all
68 } else if (x is Function) { 80 } else if (x is Function) {
69 return predicate(x); 81 return predicate(x);
70 } else { 82 } else {
71 return equals(x); 83 return equals(x);
72 } 84 }
73 } 85 }
74 86
75 // The handler for failed asserts. 87 // The handler for failed asserts.
76 FailureHandler _assertFailureHandler = null; 88 FailureHandler _assertFailureHandler = null;
77 89
78 // The default failure handler that throws ExpectExceptions. 90 // The default failure handler that throws [TestFailure]s.
79 class DefaultFailureHandler implements FailureHandler { 91 class DefaultFailureHandler implements FailureHandler {
80 DefaultFailureHandler() { 92 DefaultFailureHandler() {
81 if (_assertErrorFormatter == null) { 93 if (_assertErrorFormatter == null) {
82 _assertErrorFormatter = _defaultErrorFormatter; 94 _assertErrorFormatter = _defaultErrorFormatter;
83 } 95 }
84 } 96 }
85 void fail(String reason) { 97 void fail(String reason) {
86 throw new ExpectException(reason); 98 throw new TestFailure(reason);
87 } 99 }
88 void failMatch(actual, Matcher matcher, String reason, 100 void failMatch(actual, Matcher matcher, String reason,
89 MatchState matchState, bool verbose) { 101 MatchState matchState, bool verbose) {
90 fail(_assertErrorFormatter(actual, matcher, reason, matchState, verbose)); 102 fail(_assertErrorFormatter(actual, matcher, reason, matchState, verbose));
91 } 103 }
92 } 104 }
93 105
94 /** 106 /**
95 * Changes or resets to the default the failure handler for expect() 107 * Changes or resets to the default the failure handler for expect()
96 * [handler] is a reference to the new handler; if this is omitted 108 * [handler] is a reference to the new handler; if this is omitted
97 * or null then the failure handler is reset to the default, which 109 * or null then the failure handler is reset to the default, which
98 * throws [ExpectExceptions] on [expect] assertion failures. 110 * throws [TestFailure]s on [expect] assertion failures.
99 */ 111 */
100 void configureExpectFailureHandler([FailureHandler handler = null]) { 112 void configureExpectFailureHandler([FailureHandler handler = null]) {
101 if (handler == null) { 113 if (handler == null) {
102 handler = new DefaultFailureHandler(); 114 handler = new DefaultFailureHandler();
103 } 115 }
104 _assertFailureHandler = handler; 116 _assertFailureHandler = handler;
105 } 117 }
106 118
107 FailureHandler getOrCreateExpectFailureHandler() { 119 FailureHandler getOrCreateExpectFailureHandler() {
108 if (_assertFailureHandler == null) { 120 if (_assertFailureHandler == null) {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 * formatter is returned; this allows custom expect handlers to easily 166 * formatter is returned; this allows custom expect handlers to easily
155 * get a reference to the default formatter. 167 * get a reference to the default formatter.
156 */ 168 */
157 ErrorFormatter configureExpectFormatter([ErrorFormatter formatter = null]) { 169 ErrorFormatter configureExpectFormatter([ErrorFormatter formatter = null]) {
158 if (formatter == null) { 170 if (formatter == null) {
159 formatter = _defaultErrorFormatter; 171 formatter = _defaultErrorFormatter;
160 } 172 }
161 return _assertErrorFormatter = formatter; 173 return _assertErrorFormatter = formatter;
162 } 174 }
163 175
OLDNEW
« no previous file with comments | « pkg/unittest/lib/src/config.dart ('k') | pkg/unittest/lib/src/interfaces.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698