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

Side by Side Diff: pkg/unittest/mock.dart

Issue 10948005: Reverting 12485 and 12480 just for the sake of mocking, (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 3 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 | « lib/isolate/base.dart ('k') | pkg/unittest/unittest.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 * The error formatter for mocking is a bit different from the default one 6 * The error formatter for mocking is a bit different from the default one
7 * for unit testing; instead of the third argument being a 'reason' 7 * for unit testing; instead of the third argument being a 'reason'
8 * it is instead a [signature] describing the method signature filter 8 * it is instead a [signature] describing the method signature filter
9 * that was used to select the logs that were verified. 9 * that was used to select the logs that were verified.
10 */ 10 */
(...skipping 20 matching lines...) Expand all
31 } 31 }
32 void failMatch(actual, Matcher matcher, String reason, 32 void failMatch(actual, Matcher matcher, String reason,
33 MatchState matchState, bool verbose) { 33 MatchState matchState, bool verbose) {
34 proxy.fail(_mockingErrorFormatter(actual, matcher, reason, 34 proxy.fail(_mockingErrorFormatter(actual, matcher, reason,
35 matchState, verbose)); 35 matchState, verbose));
36 } 36 }
37 } 37 }
38 38
39 _MockFailureHandler _mockFailureHandler = null; 39 _MockFailureHandler _mockFailureHandler = null;
40 40
41 /**
42 * [_noArg] is a sentinel value representing no argument.
43 */
44 const _noArg = const _Sentinel();
45
41 /** The ways in which a call to a mock method can be handled. */ 46 /** The ways in which a call to a mock method can be handled. */
42 class Action { 47 class Action {
43 /** Do nothing (void method) */ 48 /** Do nothing (void method) */
44 static const IGNORE = const Action._('IGNORE'); 49 static const IGNORE = const Action._('IGNORE');
45 50
46 /** Return a supplied value. */ 51 /** Return a supplied value. */
47 static const RETURN = const Action._('RETURN'); 52 static const RETURN = const Action._('RETURN');
48 53
49 /** Throw a supplied value. */ 54 /** Throw a supplied value. */
50 static const THROW = const Action._('THROW'); 55 static const THROW = const Action._('THROW');
(...skipping 30 matching lines...) Expand all
81 class CallMatcher { 86 class CallMatcher {
82 Matcher nameFilter; 87 Matcher nameFilter;
83 List<Matcher> argMatchers; 88 List<Matcher> argMatchers;
84 89
85 /** 90 /**
86 * Constructor for [CallMatcher]. [name] can be null to 91 * Constructor for [CallMatcher]. [name] can be null to
87 * match anything, or a literal [String], a predicate [Function], 92 * match anything, or a literal [String], a predicate [Function],
88 * or a [Matcher]. The various arguments can be scalar values or 93 * or a [Matcher]. The various arguments can be scalar values or
89 * [Matcher]s. 94 * [Matcher]s.
90 */ 95 */
91 CallMatcher([name, arg0, arg1, arg2, arg3, arg4, 96 CallMatcher([name,
92 arg5, arg6, arg7, arg8, arg9]) { 97 arg0 = _noArg,
93 nameFilter = name != null ? wrapMatcher(name) : anything; 98 arg1 = _noArg,
99 arg2 = _noArg,
100 arg3 = _noArg,
101 arg4 = _noArg,
102 arg5 = _noArg,
103 arg6 = _noArg,
104 arg7 = _noArg,
105 arg8 = _noArg,
106 arg9 = _noArg]) {
107 if (name == null) {
108 nameFilter = anything;
109 } else {
110 nameFilter = wrapMatcher(name);
111 }
94 argMatchers = new List<Matcher>(); 112 argMatchers = new List<Matcher>();
95 if (!?arg0) return; 113 if (arg0 === _noArg) return;
96 argMatchers.add(wrapMatcher(arg0)); 114 argMatchers.add(wrapMatcher(arg0));
97 if (!?arg1) return; 115 if (arg1 === _noArg) return;
98 argMatchers.add(wrapMatcher(arg1)); 116 argMatchers.add(wrapMatcher(arg1));
99 if (!?arg2) return; 117 if (arg2 === _noArg) return;
100 argMatchers.add(wrapMatcher(arg2)); 118 argMatchers.add(wrapMatcher(arg2));
101 if (!?arg3) return; 119 if (arg3 === _noArg) return;
102 argMatchers.add(wrapMatcher(arg3)); 120 argMatchers.add(wrapMatcher(arg3));
103 if (!?arg4) return; 121 if (arg4 === _noArg) return;
104 argMatchers.add(wrapMatcher(arg4)); 122 argMatchers.add(wrapMatcher(arg4));
105 if (!?arg5) return; 123 if (arg5 === _noArg) return;
106 argMatchers.add(wrapMatcher(arg5)); 124 argMatchers.add(wrapMatcher(arg5));
107 if (!?arg6) return; 125 if (arg6 === _noArg) return;
108 argMatchers.add(wrapMatcher(arg6)); 126 argMatchers.add(wrapMatcher(arg6));
109 if (!?arg7) return; 127 if (arg7 === _noArg) return;
110 argMatchers.add(wrapMatcher(arg7)); 128 argMatchers.add(wrapMatcher(arg7));
111 if (!?arg8) return; 129 if (arg8 === _noArg) return;
112 argMatchers.add(wrapMatcher(arg8)); 130 argMatchers.add(wrapMatcher(arg8));
113 if (!?arg9) return; 131 if (arg9 === _noArg) return;
114 argMatchers.add(wrapMatcher(arg9)); 132 argMatchers.add(wrapMatcher(arg9));
115 } 133 }
116 134
117 /** 135 /**
118 * We keep our behavior specifications in a Map, which is keyed 136 * We keep our behavior specifications in a Map, which is keyed
119 * by the [CallMatcher]. To make the keys unique and to get a 137 * by the [CallMatcher]. To make the keys unique and to get a
120 * descriptive value for the [CallMatcher] we have this override 138 * descriptive value for the [CallMatcher] we have this override
121 * of [toString()]. 139 * of [toString()].
122 */ 140 */
123 String toString() { 141 String toString() {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 } 175 }
158 return true; 176 return true;
159 } 177 }
160 } 178 }
161 179
162 /** 180 /**
163 * Returns a [CallMatcher] for the specified signature. [method] can be 181 * Returns a [CallMatcher] for the specified signature. [method] can be
164 * null to match anything, or a literal [String], a predicate [Function], 182 * null to match anything, or a literal [String], a predicate [Function],
165 * or a [Matcher]. The various arguments can be scalar values or [Matcher]s. 183 * or a [Matcher]. The various arguments can be scalar values or [Matcher]s.
166 */ 184 */
167 CallMatcher callsTo([method, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]) { 185 CallMatcher callsTo([method,
168 if (!?a0) return new CallMatcher(method); 186 arg0 = _noArg,
169 if (!?a1) return new CallMatcher(method, a0); 187 arg1 = _noArg,
170 if (!?a2) return new CallMatcher(method, a0, a1); 188 arg2 = _noArg,
171 if (!?a3) return new CallMatcher(method, a0, a1, a2); 189 arg3 = _noArg,
172 if (!?a4) return new CallMatcher(method, a0, a1, a2, a3); 190 arg4 = _noArg,
173 if (!?a5) return new CallMatcher(method, a0, a1, a2, a3, a4); 191 arg5 = _noArg,
174 if (!?a6) return new CallMatcher(method, a0, a1, a2, a3, a4, a5); 192 arg6 = _noArg,
175 if (!?a7) return new CallMatcher(method, a0, a1, a2, a3, a4, a5, a6); 193 arg7 = _noArg,
176 if (!?a8) return new CallMatcher(method, a0, a1, a2, a3, a4, a5, a6, a7); 194 arg8 = _noArg,
177 if (!?a9) return new CallMatcher(method, a0, a1, a2, a3, a4, a5, a6, a7, a8); 195 arg9 = _noArg]) {
178 return new CallMatcher(method, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); 196 return new CallMatcher(method, arg0, arg1, arg2, arg3, arg4,
197 arg5, arg6, arg7, arg8, arg9);
179 } 198 }
180 199
181 /** 200 /**
182 * A [Behavior] represents how a [Mock] will respond to one particular 201 * A [Behavior] represents how a [Mock] will respond to one particular
183 * type of method call. 202 * type of method call.
184 */ 203 */
185 class Behavior { 204 class Behavior {
186 CallMatcher matcher; // The method call matcher. 205 CallMatcher matcher; // The method call matcher.
187 List<Responder> actions; // The values to return/throw or proxies to call. 206 List<Responder> actions; // The values to return/throw or proxies to call.
188 bool logging = true; 207 bool logging = true;
(...skipping 1208 matching lines...) Expand 10 before | Expand all | Expand 10 after
1397 Exception("Can't retrieve logs when logging was never enabled."); 1416 Exception("Can't retrieve logs when logging was never enabled.");
1398 } else { 1417 } else {
1399 return log.getMatches(name, logFilter, actionMatcher, destructive); 1418 return log.getMatches(name, logFilter, actionMatcher, destructive);
1400 } 1419 }
1401 } 1420 }
1402 1421
1403 /** 1422 /**
1404 * Useful shorthand method that creates a [CallMatcher] from its arguments 1423 * Useful shorthand method that creates a [CallMatcher] from its arguments
1405 * and then calls [getLogs]. 1424 * and then calls [getLogs].
1406 */ 1425 */
1407 LogEntryList calls(method, [a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]) { 1426 LogEntryList calls(method,
1408 if (!?a0) return getLogs(callsTo(method)); 1427 [arg0 = _noArg,
1409 if (!?a1) return getLogs(callsTo(method, a0)); 1428 arg1 = _noArg,
1410 if (!?a2) return getLogs(callsTo(method, a0, a1)); 1429 arg2 = _noArg,
1411 if (!?a3) return getLogs(callsTo(method, a0, a1, a2)); 1430 arg3 = _noArg,
1412 if (!?a4) return getLogs(callsTo(method, a0, a1, a2, a3)); 1431 arg4 = _noArg,
1413 if (!?a5) return getLogs(callsTo(method, a0, a1, a2, a3, a4)); 1432 arg5 = _noArg,
1414 if (!?a6) return getLogs(callsTo(method, a0, a1, a2, a3, a4, a5)); 1433 arg6 = _noArg,
1415 if (!?a7) return getLogs(callsTo(method, a0, a1, a2, a3, a4, a5, a6)); 1434 arg7 = _noArg,
1416 if (!?a8) return getLogs(callsTo(method, a0, a1, a2, a3, a4, a5, a6, a7)); 1435 arg8 = _noArg,
1417 if (!?a9) { 1436 arg9 = _noArg]) =>
1418 return getLogs(callsTo(method, a0, a1, a2, a3, a4, a5, a6, a7, a8)); 1437 getLogs(callsTo(method, arg0, arg1, arg2, arg3, arg4,
1419 } 1438 arg5, arg6, arg7, arg8, arg9));
1420 return getLogs(callsTo(method, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9));
1421 }
1422 1439
1423 /** Clear the behaviors for the Mock. */ 1440 /** Clear the behaviors for the Mock. */
1424 void resetBehavior() => _behaviors.clear(); 1441 void resetBehavior() => _behaviors.clear();
1425 1442
1426 /** Clear the logs for the Mock. */ 1443 /** Clear the logs for the Mock. */
1427 void clearLogs() { 1444 void clearLogs() {
1428 if (log != null) { 1445 if (log != null) {
1429 if (name == null) { // This log is not shared. 1446 if (name == null) { // This log is not shared.
1430 log.logs.clear(); 1447 log.logs.clear();
1431 } else { // This log may be shared. 1448 } else { // This log may be shared.
1432 log.logs = log.logs.filter((e) => e.mockName != name); 1449 log.logs = log.logs.filter((e) => e.mockName != name);
1433 } 1450 }
1434 } 1451 }
1435 } 1452 }
1436 1453
1437 /** Clear both logs and behavior. */ 1454 /** Clear both logs and behavior. */
1438 void reset() { 1455 void reset() {
1439 resetBehavior(); 1456 resetBehavior();
1440 clearLogs(); 1457 clearLogs();
1441 } 1458 }
1442 } 1459 }
OLDNEW
« no previous file with comments | « lib/isolate/base.dart ('k') | pkg/unittest/unittest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698