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

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

Issue 10679005: Initial version of mocking support. There is still quite a bit of (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 5 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 | « no previous file | lib/unittest/mock.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 /** 5 /**
6 * This is the main assertion function. It asserts that [actual] 6 * This is the main assertion function. It asserts that [actual]
7 * matches the [matcher]. [matcher] is optional and defaults to isTrue, 7 * matches the [matcher]. [matcher] is optional and defaults to isTrue,
8 * so expect can be used with a single predicate argument. [reason] 8 * so expect can be used with a single predicate argument. [reason]
9 * is optional and is typically not supplied if a reasonable matcher is 9 * is optional and is typically not supplied if a reasonable matcher is
10 * explicitly provided, as a reason can be generated from the matcher. 10 * explicitly provided, as a reason can be generated from the matcher.
11 * If [reason] is included it is appended to the reason generated 11 * If [reason] is included it is appended to the reason generated
12 * by the matcher. 12 * by the matcher.
13 * 13 *
14 * [matcher] can be a value in which case it will be wrapped in an 14 * [matcher] can be a value in which case it will be wrapped in an
15 * [equals] matcher. 15 * [equals] matcher.
16 * 16 *
17 * If the assertion fails, then the default behavior is to throw an 17 * If the assertion fails, then the default behavior is to throw an
18 * [ExpectException], but this behavior can be changed by calling 18 * [ExpectException], but this behavior can be changed by calling
19 * [configureExpectHandler] and providing an alternative handler that 19 * [configureExpectFailureHandler] and providing an alternative handler that
20 * implements the [IFailureHandler] interface. 20 * implements the [IFailureHandler] interface. It is also possible to
21 * pass a [failureHandler] to [expect] as a final parameter for fine-
22 * grained control.
21 * 23 *
22 * expect() is a 3rd generation assertion mechanism, drawing 24 * expect() is a 3rd generation assertion mechanism, drawing
23 * inspiration from [Hamcrest] and Ladislav Thon's [dart-matchers] 25 * inspiration from [Hamcrest] and Ladislav Thon's [dart-matchers]
24 * library. 26 * library.
25 * 27 *
26 * See [Hamcrest] http://en.wikipedia.org/wiki/Hamcrest 28 * See [Hamcrest] http://en.wikipedia.org/wiki/Hamcrest
27 * [Hamcrest] http://http://code.google.com/p/hamcrest/ 29 * [Hamcrest] http://http://code.google.com/p/hamcrest/
28 * [dart-matchers] https://github.com/Ladicek/dart-matchers 30 * [dart-matchers] https://github.com/Ladicek/dart-matchers
29 */ 31 */
30 void expect(actual, [matcher = isTrue, String reason = null]) { 32 void expect(actual, [matcher = isTrue, String reason = null,
33 failureHandler = null]) {
31 matcher = wrapMatcher(matcher); 34 matcher = wrapMatcher(matcher);
32 var doesMatch; 35 var doesMatch;
33 try { 36 try {
34 doesMatch = matcher.matches(actual); 37 doesMatch = matcher.matches(actual);
35 } catch (var e, var trace) { 38 } catch (var e, var trace) {
36 doesMatch = false; 39 doesMatch = false;
37 if (reason == null) { 40 if (reason == null) {
38 reason = '${(e is String) ? e : e.toString()} at $trace'; 41 reason = '${(e is String) ? e : e.toString()} at $trace';
39 } 42 }
40 } 43 }
41 if (!doesMatch) { 44 if (!doesMatch) {
42 // Make sure we have a failure handler configured. 45 if (failureHandler == null) {
43 configureExpectHandler(_assertFailureHandler); 46 failureHandler = getOrCreateExpectFailureHandler();
44 _assertFailureHandler.failMatch(actual, matcher, reason); 47 }
48 failureHandler.failMatch(actual, matcher, reason);
45 } 49 }
46 } 50 }
47 51
48 /** 52 /**
49 * Takes an argument and returns an equivalent matcher. 53 * Takes an argument and returns an equivalent matcher.
50 * If the argument is already a matcher this does nothing, 54 * If the argument is already a matcher this does nothing,
51 * else if the argument is a function, it generates a predicate 55 * else if the argument is a function, it generates a predicate
52 * function matcher, else it generates an equals matcher. 56 * function matcher, else it generates an equals matcher.
53 */ 57 */
54 Matcher wrapMatcher(x) { 58 Matcher wrapMatcher(x) {
(...skipping 23 matching lines...) Expand all
78 fail(_assertErrorFormatter(actual, matcher, reason)); 82 fail(_assertErrorFormatter(actual, matcher, reason));
79 } 83 }
80 } 84 }
81 85
82 /** 86 /**
83 * Changes or resets to the default the failure handler for expect() 87 * Changes or resets to the default the failure handler for expect()
84 * [handler] is a reference to the new handler; if this is omitted 88 * [handler] is a reference to the new handler; if this is omitted
85 * or null then the failure handler is reset to the default, which 89 * or null then the failure handler is reset to the default, which
86 * throws [ExpectExceptions] on [expect] assertion failures. 90 * throws [ExpectExceptions] on [expect] assertion failures.
87 */ 91 */
88 void configureExpectHandler([FailureHandler handler = null]) { 92 void configureExpectFailureHandler([FailureHandler handler = null]) {
89 if (handler == null) { 93 if (handler == null) {
90 handler = new DefaultFailureHandler(); 94 handler = new DefaultFailureHandler();
91 } 95 }
92 _assertFailureHandler = handler; 96 _assertFailureHandler = handler;
93 } 97 }
94 98
99 FailureHandler getOrCreateExpectFailureHandler() {
100 if (_assertFailureHandler == null) {
101 configureExpectFailureHandler();
102 }
103 return _assertFailureHandler;
104 }
105
95 // The error message formatter for failed asserts. 106 // The error message formatter for failed asserts.
96 ErrorFormatter _assertErrorFormatter = null; 107 ErrorFormatter _assertErrorFormatter = null;
97 108
98 // The default error formatter implementation. 109 // The default error formatter implementation.
99 String _defaultErrorFormatter(actual, Matcher matcher, String reason) { 110 String _defaultErrorFormatter(actual, Matcher matcher, String reason) {
100 var description = new StringDescription(); 111 var description = new StringDescription();
101 description.add('Expected: ').addDescriptionOf(matcher). 112 description.add('Expected: ').addDescriptionOf(matcher).
102 add('\n but: '); 113 add('\n but: ');
103 matcher.describeMismatch(actual, description); 114 matcher.describeMismatch(actual, description);
104 if (reason != null) { 115 if (reason != null) {
105 description.add('\n').add(reason).add('\n'); 116 description.add('\n').add(reason).add('\n');
106 } 117 }
107 return description.toString(); 118 return description.toString();
108 } 119 }
109 120
110 /** 121 /**
111 * Changes or resets to default the failure message formatter for expect(). 122 * Changes or resets to default the failure message formatter for expect().
112 * [formatter] is a reference to the new formatter; if this is omitted or 123 * [formatter] is a reference to the new formatter; if this is omitted or
113 * null then the failure formatter is reset to the default. The new 124 * null then the failure formatter is reset to the default. The new
114 * formatter is returned; this allows custom expect handlers to easily 125 * formatter is returned; this allows custom expect handlers to easily
115 * get a reference to the default formatter. 126 * get a reference to the default formatter.
116 */ 127 */
117 ErrorFormatter configureExpectFormatter([ErrorFormatter formatter = null]) { 128 ErrorFormatter configureExpectFormatter([ErrorFormatter formatter = null]) {
118 if (formatter == null) { 129 if (formatter == null) {
119 formatter = _defaultErrorFormatter; 130 formatter = _defaultErrorFormatter;
120 } 131 }
121 return _assertErrorFormatter = formatter; 132 return _assertErrorFormatter = formatter;
122 } 133 }
123 134
OLDNEW
« no previous file with comments | « no previous file | lib/unittest/mock.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698