| 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 library mock; | 5 library mock; |
| 6 import 'matcher.dart'; | 6 import 'matcher.dart'; |
| 7 | 7 |
| 8 /** | 8 /** |
| 9 * The error formatter for mocking is a bit different from the default one | 9 * The error formatter for mocking is a bit different from the default one |
| 10 * for unit testing; instead of the third argument being a 'reason' | 10 * for unit testing; instead of the third argument being a 'reason' |
| (...skipping 1260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1271 } | 1271 } |
| 1272 } | 1272 } |
| 1273 | 1273 |
| 1274 /** | 1274 /** |
| 1275 * This is the handler for method calls. We loop through the list | 1275 * This is the handler for method calls. We loop through the list |
| 1276 * of [Behavior]s, and find the first match that still has return | 1276 * of [Behavior]s, and find the first match that still has return |
| 1277 * values available, and then do the action specified by that | 1277 * values available, and then do the action specified by that |
| 1278 * return value. If we find no [Behavior] to apply an exception is | 1278 * return value. If we find no [Behavior] to apply an exception is |
| 1279 * thrown. | 1279 * thrown. |
| 1280 */ | 1280 */ |
| 1281 noSuchMethod(String method, List args) { | 1281 noSuchMethod(InvocationMirror invocation) { |
| 1282 if (method.startsWith('get:')) { | 1282 var method = invocation.memberName; |
| 1283 var args = invocation.positionalArguments; |
| 1284 if (invocation.isGetter) { |
| 1285 method = 'get $method'; |
| 1286 } else if (method.startsWith('get:')) { |
| 1287 // TODO(gram): Remove this when VM handles the isGetter version above. |
| 1283 method = 'get ${method.substring(4)}'; | 1288 method = 'get ${method.substring(4)}'; |
| 1284 } | 1289 } |
| 1285 bool matchedMethodName = false; | 1290 bool matchedMethodName = false; |
| 1286 MatchState matchState = new MatchState(); | 1291 MatchState matchState = new MatchState(); |
| 1287 for (String k in _behaviors.keys) { | 1292 for (String k in _behaviors.keys) { |
| 1288 Behavior b = _behaviors[k]; | 1293 Behavior b = _behaviors[k]; |
| 1289 if (b.matcher.nameFilter.matches(method, matchState)) { | 1294 if (b.matcher.nameFilter.matches(method, matchState)) { |
| 1290 matchedMethodName = true; | 1295 matchedMethodName = true; |
| 1291 } | 1296 } |
| 1292 if (b.matches(method, args)) { | 1297 if (b.matches(method, args)) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 1310 if (_logging && b.logging) { | 1315 if (_logging && b.logging) { |
| 1311 log.add(new LogEntry(name, method, args, action, value)); | 1316 log.add(new LogEntry(name, method, args, action, value)); |
| 1312 } | 1317 } |
| 1313 return value; | 1318 return value; |
| 1314 } else if (action == Action.THROW) { | 1319 } else if (action == Action.THROW) { |
| 1315 if (_logging && b.logging) { | 1320 if (_logging && b.logging) { |
| 1316 log.add(new LogEntry(name, method, args, action, value)); | 1321 log.add(new LogEntry(name, method, args, action, value)); |
| 1317 } | 1322 } |
| 1318 throw value; | 1323 throw value; |
| 1319 } else if (action == Action.PROXY) { | 1324 } else if (action == Action.PROXY) { |
| 1325 // TODO(gram): Replace all this with: |
| 1326 // var rtn = invocation.invokeOn(value); |
| 1327 // once that is supported. |
| 1320 var rtn; | 1328 var rtn; |
| 1321 switch (args.length) { | 1329 switch (args.length) { |
| 1322 case 0: | 1330 case 0: |
| 1323 rtn = value(); | 1331 rtn = value(); |
| 1324 break; | 1332 break; |
| 1325 case 1: | 1333 case 1: |
| 1326 rtn = value(args[0]); | 1334 rtn = value(args[0]); |
| 1327 break; | 1335 break; |
| 1328 case 2: | 1336 case 2: |
| 1329 rtn = value(args[0], args[1]); | 1337 rtn = value(args[0], args[1]); |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1454 } | 1462 } |
| 1455 } | 1463 } |
| 1456 } | 1464 } |
| 1457 | 1465 |
| 1458 /** Clear both logs and behavior. */ | 1466 /** Clear both logs and behavior. */ |
| 1459 void reset() { | 1467 void reset() { |
| 1460 resetBehavior(); | 1468 resetBehavior(); |
| 1461 clearLogs(); | 1469 clearLogs(); |
| 1462 } | 1470 } |
| 1463 } | 1471 } |
| OLD | NEW |