| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library mock.behavior; |
| 6 |
| 7 import 'action.dart'; |
| 8 import 'call_matcher.dart'; |
| 9 import 'responder.dart'; |
| 10 |
| 11 /** |
| 12 * A [Behavior] represents how a [Mock] will respond to one particular |
| 13 * type of method call. |
| 14 */ |
| 15 class Behavior { |
| 16 CallMatcher matcher; // The method call matcher. |
| 17 List<Responder> actions; // The values to return/throw or proxies to call. |
| 18 bool logging = true; |
| 19 |
| 20 Behavior (this.matcher) { |
| 21 actions = new List<Responder>(); |
| 22 } |
| 23 |
| 24 /** |
| 25 * Adds a [Responder] that returns a [value] for [count] calls |
| 26 * (1 by default). |
| 27 */ |
| 28 Behavior thenReturn(value, [count = 1]) { |
| 29 actions.add(new Responder(value, count, Action.RETURN)); |
| 30 return this; // For chaining calls. |
| 31 } |
| 32 |
| 33 /** Adds a [Responder] that repeatedly returns a [value]. */ |
| 34 Behavior alwaysReturn(value) { |
| 35 return thenReturn(value, 0); |
| 36 } |
| 37 |
| 38 /** |
| 39 * Adds a [Responder] that throws [value] [count] |
| 40 * times (1 by default). |
| 41 */ |
| 42 Behavior thenThrow(value, [count = 1]) { |
| 43 actions.add(new Responder(value, count, Action.THROW)); |
| 44 return this; // For chaining calls. |
| 45 } |
| 46 |
| 47 /** Adds a [Responder] that throws [value] endlessly. */ |
| 48 Behavior alwaysThrow(value) { |
| 49 return thenThrow(value, 0); |
| 50 } |
| 51 |
| 52 /** |
| 53 * [thenCall] creates a proxy Responder, that is called [count] |
| 54 * times (1 by default; 0 is used for unlimited calls, and is |
| 55 * exposed as [alwaysCall]). [value] is the function that will |
| 56 * be called with the same arguments that were passed to the |
| 57 * mock. Proxies can be used to wrap real objects or to define |
| 58 * more complex return/throw behavior. You could even (if you |
| 59 * wanted) use proxies to emulate the behavior of thenReturn; |
| 60 * e.g.: |
| 61 * |
| 62 * m.when(callsTo('foo')).thenReturn(0) |
| 63 * |
| 64 * is equivalent to: |
| 65 * |
| 66 * m.when(callsTo('foo')).thenCall(() => 0) |
| 67 */ |
| 68 Behavior thenCall(value, [count = 1]) { |
| 69 actions.add(new Responder(value, count, Action.PROXY)); |
| 70 return this; // For chaining calls. |
| 71 } |
| 72 |
| 73 /** Creates a repeating proxy call. */ |
| 74 Behavior alwaysCall(value) { |
| 75 return thenCall(value, 0); |
| 76 } |
| 77 |
| 78 /** Returns true if a method call matches the [Behavior]. */ |
| 79 bool matches(String method, List args) => matcher.matches(method, args); |
| 80 |
| 81 /** Returns the [matcher]'s representation. */ |
| 82 String toString() => matcher.toString(); |
| 83 } |
| OLD | NEW |