Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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(); | |
|
kasperl
2012/09/18 10:07:56
Can you also get rid of the _Sentinel class?
bakster
2012/09/18 10:23:54
Look inside unittest.dart. It was eliminated.
| |
| 45 | |
| 46 /** The ways in which a call to a mock method can be handled. */ | 41 /** The ways in which a call to a mock method can be handled. */ |
| 47 class Action { | 42 class Action { |
| 48 /** Do nothing (void method) */ | 43 /** Do nothing (void method) */ |
| 49 static const IGNORE = const Action._('IGNORE'); | 44 static const IGNORE = const Action._('IGNORE'); |
| 50 | 45 |
| 51 /** Return a supplied value. */ | 46 /** Return a supplied value. */ |
| 52 static const RETURN = const Action._('RETURN'); | 47 static const RETURN = const Action._('RETURN'); |
| 53 | 48 |
| 54 /** Throw a supplied value. */ | 49 /** Throw a supplied value. */ |
| 55 static const THROW = const Action._('THROW'); | 50 static const THROW = const Action._('THROW'); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 86 class CallMatcher { | 81 class CallMatcher { |
| 87 Matcher nameFilter; | 82 Matcher nameFilter; |
| 88 List<Matcher> argMatchers; | 83 List<Matcher> argMatchers; |
| 89 | 84 |
| 90 /** | 85 /** |
| 91 * Constructor for [CallMatcher]. [name] can be null to | 86 * Constructor for [CallMatcher]. [name] can be null to |
| 92 * match anything, or a literal [String], a predicate [Function], | 87 * match anything, or a literal [String], a predicate [Function], |
| 93 * or a [Matcher]. The various arguments can be scalar values or | 88 * or a [Matcher]. The various arguments can be scalar values or |
| 94 * [Matcher]s. | 89 * [Matcher]s. |
| 95 */ | 90 */ |
| 96 CallMatcher([name, | 91 CallMatcher([name, arg0, arg1, arg2, arg3, arg4, |
| 97 arg0 = _noArg, | 92 arg5, arg6, arg7, arg8, arg9]) { |
| 98 arg1 = _noArg, | 93 nameFilter = name != null ? wrapMatcher(name) : anything; |
| 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 } | |
| 112 argMatchers = new List<Matcher>(); | 94 argMatchers = new List<Matcher>(); |
| 113 if (arg0 === _noArg) return; | 95 if (!?arg0) return; |
| 114 argMatchers.add(wrapMatcher(arg0)); | 96 argMatchers.add(wrapMatcher(arg0)); |
| 115 if (arg1 === _noArg) return; | 97 if (!?arg1) return; |
| 116 argMatchers.add(wrapMatcher(arg1)); | 98 argMatchers.add(wrapMatcher(arg1)); |
| 117 if (arg2 === _noArg) return; | 99 if (!?arg2) return; |
| 118 argMatchers.add(wrapMatcher(arg2)); | 100 argMatchers.add(wrapMatcher(arg2)); |
| 119 if (arg3 === _noArg) return; | 101 if (!?arg3) return; |
| 120 argMatchers.add(wrapMatcher(arg3)); | 102 argMatchers.add(wrapMatcher(arg3)); |
| 121 if (arg4 === _noArg) return; | 103 if (!?arg4) return; |
| 122 argMatchers.add(wrapMatcher(arg4)); | 104 argMatchers.add(wrapMatcher(arg4)); |
| 123 if (arg5 === _noArg) return; | 105 if (!?arg5) return; |
| 124 argMatchers.add(wrapMatcher(arg5)); | 106 argMatchers.add(wrapMatcher(arg5)); |
| 125 if (arg6 === _noArg) return; | 107 if (!?arg6) return; |
| 126 argMatchers.add(wrapMatcher(arg6)); | 108 argMatchers.add(wrapMatcher(arg6)); |
| 127 if (arg7 === _noArg) return; | 109 if (!?arg7) return; |
| 128 argMatchers.add(wrapMatcher(arg7)); | 110 argMatchers.add(wrapMatcher(arg7)); |
| 129 if (arg8 === _noArg) return; | 111 if (!?arg8) return; |
| 130 argMatchers.add(wrapMatcher(arg8)); | 112 argMatchers.add(wrapMatcher(arg8)); |
| 131 if (arg9 === _noArg) return; | 113 if (!?arg9) return; |
| 132 argMatchers.add(wrapMatcher(arg9)); | 114 argMatchers.add(wrapMatcher(arg9)); |
| 133 } | 115 } |
| 134 | 116 |
| 135 /** | 117 /** |
| 136 * We keep our behavior specifications in a Map, which is keyed | 118 * We keep our behavior specifications in a Map, which is keyed |
| 137 * by the [CallMatcher]. To make the keys unique and to get a | 119 * by the [CallMatcher]. To make the keys unique and to get a |
| 138 * descriptive value for the [CallMatcher] we have this override | 120 * descriptive value for the [CallMatcher] we have this override |
| 139 * of [toString()]. | 121 * of [toString()]. |
| 140 */ | 122 */ |
| 141 String toString() { | 123 String toString() { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 175 } | 157 } |
| 176 return true; | 158 return true; |
| 177 } | 159 } |
| 178 } | 160 } |
| 179 | 161 |
| 180 /** | 162 /** |
| 181 * Returns a [CallMatcher] for the specified signature. [method] can be | 163 * Returns a [CallMatcher] for the specified signature. [method] can be |
| 182 * null to match anything, or a literal [String], a predicate [Function], | 164 * null to match anything, or a literal [String], a predicate [Function], |
| 183 * or a [Matcher]. The various arguments can be scalar values or [Matcher]s. | 165 * or a [Matcher]. The various arguments can be scalar values or [Matcher]s. |
| 184 */ | 166 */ |
| 185 CallMatcher callsTo([method, | 167 CallMatcher callsTo([method, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]) { |
| 186 arg0 = _noArg, | 168 if (!?a0) return new CallMatcher(method); |
| 187 arg1 = _noArg, | 169 if (!?a1) return new CallMatcher(method, a0); |
| 188 arg2 = _noArg, | 170 if (!?a2) return new CallMatcher(method, a0, a1); |
| 189 arg3 = _noArg, | 171 if (!?a3) return new CallMatcher(method, a0, a1, a2); |
| 190 arg4 = _noArg, | 172 if (!?a4) return new CallMatcher(method, a0, a1, a2, a3); |
| 191 arg5 = _noArg, | 173 if (!?a5) return new CallMatcher(method, a0, a1, a2, a3, a4); |
| 192 arg6 = _noArg, | 174 if (!?a6) return new CallMatcher(method, a0, a1, a2, a3, a4, a5); |
| 193 arg7 = _noArg, | 175 if (!?a7) return new CallMatcher(method, a0, a1, a2, a3, a4, a5, a6); |
| 194 arg8 = _noArg, | 176 if (!?a8) return new CallMatcher(method, a0, a1, a2, a3, a4, a5, a6, a7); |
| 195 arg9 = _noArg]) { | 177 if (!?a9) return new CallMatcher(method, a0, a1, a2, a3, a4, a5, a6, a7, a8); |
| 196 return new CallMatcher(method, arg0, arg1, arg2, arg3, arg4, | 178 return new CallMatcher(method, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); |
| 197 arg5, arg6, arg7, arg8, arg9); | |
| 198 } | 179 } |
| 199 | 180 |
| 200 /** | 181 /** |
| 201 * A [Behavior] represents how a [Mock] will respond to one particular | 182 * A [Behavior] represents how a [Mock] will respond to one particular |
| 202 * type of method call. | 183 * type of method call. |
| 203 */ | 184 */ |
| 204 class Behavior { | 185 class Behavior { |
| 205 CallMatcher matcher; // The method call matcher. | 186 CallMatcher matcher; // The method call matcher. |
| 206 List<Responder> actions; // The values to return/throw or proxies to call. | 187 List<Responder> actions; // The values to return/throw or proxies to call. |
| 207 bool logging = true; | 188 bool logging = true; |
| (...skipping 1208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1416 Exception("Can't retrieve logs when logging was never enabled."); | 1397 Exception("Can't retrieve logs when logging was never enabled."); |
| 1417 } else { | 1398 } else { |
| 1418 return log.getMatches(name, logFilter, actionMatcher, destructive); | 1399 return log.getMatches(name, logFilter, actionMatcher, destructive); |
| 1419 } | 1400 } |
| 1420 } | 1401 } |
| 1421 | 1402 |
| 1422 /** | 1403 /** |
| 1423 * Useful shorthand method that creates a [CallMatcher] from its arguments | 1404 * Useful shorthand method that creates a [CallMatcher] from its arguments |
| 1424 * and then calls [getLogs]. | 1405 * and then calls [getLogs]. |
| 1425 */ | 1406 */ |
| 1426 LogEntryList calls(method, | 1407 LogEntryList calls(method, [a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]) { |
| 1427 [arg0 = _noArg, | 1408 if (!?a0) return getLogs(callsTo(method)); |
| 1428 arg1 = _noArg, | 1409 if (!?a1) return getLogs(callsTo(method, a0)); |
| 1429 arg2 = _noArg, | 1410 if (!?a2) return getLogs(callsTo(method, a0, a1)); |
| 1430 arg3 = _noArg, | 1411 if (!?a3) return getLogs(callsTo(method, a0, a1, a2)); |
| 1431 arg4 = _noArg, | 1412 if (!?a4) return getLogs(callsTo(method, a0, a1, a2, a3)); |
| 1432 arg5 = _noArg, | 1413 if (!?a5) return getLogs(callsTo(method, a0, a1, a2, a3, a4)); |
| 1433 arg6 = _noArg, | 1414 if (!?a6) return getLogs(callsTo(method, a0, a1, a2, a3, a4, a5)); |
| 1434 arg7 = _noArg, | 1415 if (!?a7) return getLogs(callsTo(method, a0, a1, a2, a3, a4, a5, a6)); |
| 1435 arg8 = _noArg, | 1416 if (!?a8) return getLogs(callsTo(method, a0, a1, a2, a3, a4, a5, a6, a7)); |
| 1436 arg9 = _noArg]) => | 1417 if (!?a9) { |
| 1437 getLogs(callsTo(method, arg0, arg1, arg2, arg3, arg4, | 1418 return getLogs(callsTo(method, a0, a1, a2, a3, a4, a5, a6, a7, a8)); |
| 1438 arg5, arg6, arg7, arg8, arg9)); | 1419 } |
| 1420 return getLogs(callsTo(method, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)); | |
| 1421 } | |
| 1439 | 1422 |
| 1440 /** Clear the behaviors for the Mock. */ | 1423 /** Clear the behaviors for the Mock. */ |
| 1441 void resetBehavior() => _behaviors.clear(); | 1424 void resetBehavior() => _behaviors.clear(); |
| 1442 | 1425 |
| 1443 /** Clear the logs for the Mock. */ | 1426 /** Clear the logs for the Mock. */ |
| 1444 void clearLogs() { | 1427 void clearLogs() { |
| 1445 if (log != null) { | 1428 if (log != null) { |
| 1446 if (name == null) { // This log is not shared. | 1429 if (name == null) { // This log is not shared. |
| 1447 log.logs.clear(); | 1430 log.logs.clear(); |
| 1448 } else { // This log may be shared. | 1431 } else { // This log may be shared. |
| 1449 log.logs = log.logs.filter((e) => e.mockName != name); | 1432 log.logs = log.logs.filter((e) => e.mockName != name); |
| 1450 } | 1433 } |
| 1451 } | 1434 } |
| 1452 } | 1435 } |
| 1453 | 1436 |
| 1454 /** Clear both logs and behavior. */ | 1437 /** Clear both logs and behavior. */ |
| 1455 void reset() { | 1438 void reset() { |
| 1456 resetBehavior(); | 1439 resetBehavior(); |
| 1457 clearLogs(); | 1440 clearLogs(); |
| 1458 } | 1441 } |
| 1459 } | 1442 } |
| OLD | NEW |