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

Side by Side Diff: pkg/mock/lib/src/mock.dart

Issue 278613003: pkg/mock: fixes for v0.11 (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: status file fix for mismatched version constraints Created 6 years, 7 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 | « pkg/mock/lib/src/log_entry_list.dart ('k') | pkg/mock/lib/src/result_matcher.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) 2014, the Dart project authors. Please see the AUTHORS file 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 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.mock; 5 library mock.mock;
6 6
7 // TOOD(kevmoo): just use `Map` 7 // TOOD(kevmoo): just use `Map`
8 import 'dart:collection' show LinkedHashMap; 8 import 'dart:collection' show LinkedHashMap;
9 import 'dart:mirrors'; 9 import 'dart:mirrors';
10 10
11 import 'package:matcher/matcher.dart'; 11 import 'package:matcher/matcher.dart';
12 12
13 import 'action.dart'; 13 import 'action.dart';
14 import 'behavior.dart'; 14 import 'behavior.dart';
15 import 'call_matcher.dart'; 15 import 'call_matcher.dart';
16 import 'log_entry.dart'; 16 import 'log_entry.dart';
17 import 'log_entry_list.dart'; 17 import 'log_entry_list.dart';
18 import 'responder.dart'; 18 import 'responder.dart';
19 import 'util.dart'; 19 import 'util.dart';
20 20
21 /** The base class for all mocked objects. */ 21 /** The base class for all mocked objects. */
22 @proxy 22 @proxy
23 class Mock { 23 class Mock {
24 /** The mock name. Needed if the log is shared; optional otherwise. */ 24 /** The mock name. Needed if the log is shared; optional otherwise. */
25 final String name; 25 final String name;
26 26
27 /** The set of [Behavior]s supported. */ 27 /** The set of [Behavior]s supported. */
28 final LinkedHashMap<String,Behavior> _behaviors; 28 final LinkedHashMap<String, Behavior> _behaviors;
29 29
30 /** How to handle unknown method calls - swallow or throw. */ 30 /** How to handle unknown method calls - swallow or throw. */
31 final bool _throwIfNoBehavior; 31 final bool _throwIfNoBehavior;
32 32
33 /** For spys, the real object that we are spying on. */ 33 /** For spys, the real object that we are spying on. */
34 final Object _realObject; 34 final Object _realObject;
35 35
36 /** The [log] of calls made. Only used if [name] is null. */ 36 /** The [log] of calls made. Only used if [name] is null. */
37 LogEntryList log; 37 LogEntryList log;
38 38
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 } 78 }
79 79
80 /** 80 /**
81 * This constructor creates a spy with no user-defined behavior. 81 * This constructor creates a spy with no user-defined behavior.
82 * This is simply a proxy for a real object that passes calls 82 * This is simply a proxy for a real object that passes calls
83 * through to that real object but captures an audit trail of 83 * through to that real object but captures an audit trail of
84 * calls made to the object that can be queried and validated 84 * calls made to the object that can be queried and validated
85 * later. 85 * later.
86 */ 86 */
87 Mock.spy(this._realObject, {this.name, this.log}) 87 Mock.spy(this._realObject, {this.name, this.log})
88 : _behaviors = null, 88 : _behaviors = null,
89 _throwIfNoBehavior = true { 89 _throwIfNoBehavior = true {
90 logging = true; 90 logging = true;
91 } 91 }
92 92
93 /** 93 /**
94 * [when] is used to create a new or extend an existing [Behavior]. 94 * [when] is used to create a new or extend an existing [Behavior].
95 * A [CallMatcher] [filter] must be supplied, and the [Behavior]s for 95 * A [CallMatcher] [filter] must be supplied, and the [Behavior]s for
96 * that signature are returned (being created first if needed). 96 * that signature are returned (being created first if needed).
97 * 97 *
98 * Typical use case: 98 * Typical use case:
99 * 99 *
(...skipping 18 matching lines...) Expand all
118 * thrown. 118 * thrown.
119 */ 119 */
120 noSuchMethod(Invocation invocation) { 120 noSuchMethod(Invocation invocation) {
121 var method = MirrorSystem.getName(invocation.memberName); 121 var method = MirrorSystem.getName(invocation.memberName);
122 var args = invocation.positionalArguments; 122 var args = invocation.positionalArguments;
123 if (invocation.isGetter) { 123 if (invocation.isGetter) {
124 method = 'get $method'; 124 method = 'get $method';
125 } else if (invocation.isSetter) { 125 } else if (invocation.isSetter) {
126 method = 'set $method'; 126 method = 'set $method';
127 // Remove the trailing '='. 127 // Remove the trailing '='.
128 if (method[method.length-1] == '=') { 128 if (method[method.length - 1] == '=') {
129 method = method.substring(0, method.length - 1); 129 method = method.substring(0, method.length - 1);
130 } 130 }
131 } 131 }
132 if (_behaviors == null) { // Spy. 132 if (_behaviors == null) { // Spy.
133 var mirror = reflect(_realObject); 133 var mirror = reflect(_realObject);
134 try { 134 try {
135 var result = mirror.delegate(invocation); 135 var result = mirror.delegate(invocation);
136 log.add(new LogEntry(name, method, args, Action.PROXY, result)); 136 log.add(new LogEntry(name, method, args, Action.PROXY, result));
137 return result; 137 return result;
138 } catch (e) { 138 } catch (e) {
(...skipping 29 matching lines...) Expand all
168 if (_logging && b.logging) { 168 if (_logging && b.logging) {
169 log.add(new LogEntry(name, method, args, action, value)); 169 log.add(new LogEntry(name, method, args, action, value));
170 } 170 }
171 return value; 171 return value;
172 } else if (action == Action.THROW) { 172 } else if (action == Action.THROW) {
173 if (_logging && b.logging) { 173 if (_logging && b.logging) {
174 log.add(new LogEntry(name, method, args, action, value)); 174 log.add(new LogEntry(name, method, args, action, value));
175 } 175 }
176 throw value; 176 throw value;
177 } else if (action == Action.PROXY) { 177 } else if (action == Action.PROXY) {
178 // TODO(gram): Replace all this with: 178 var mir = reflect(value) as ClosureMirror;
179 // var rtn = reflect(value).apply(invocation.positionalArguments, 179 var rtn = mir.invoke(#call, invocation.positionalArguments,
180 // invocation.namedArguments); 180 invocation.namedArguments).reflectee;
181 // once that is supported.
182 var rtn;
183 switch (args.length) {
184 case 0:
185 rtn = value();
186 break;
187 case 1:
188 rtn = value(args[0]);
189 break;
190 case 2:
191 rtn = value(args[0], args[1]);
192 break;
193 case 3:
194 rtn = value(args[0], args[1], args[2]);
195 break;
196 case 4:
197 rtn = value(args[0], args[1], args[2], args[3]);
198 break;
199 case 5:
200 rtn = value(args[0], args[1], args[2], args[3], args[4]);
201 break;
202 case 6:
203 rtn = value(args[0], args[1], args[2], args[3],
204 args[4], args[5]);
205 break;
206 case 7:
207 rtn = value(args[0], args[1], args[2], args[3],
208 args[4], args[5], args[6]);
209 break;
210 case 8:
211 rtn = value(args[0], args[1], args[2], args[3],
212 args[4], args[5], args[6], args[7]);
213 break;
214 case 9:
215 rtn = value(args[0], args[1], args[2], args[3],
216 args[4], args[5], args[6], args[7], args[8]);
217 break;
218 case 9:
219 rtn = value(args[0], args[1], args[2], args[3],
220 args[4], args[5], args[6], args[7], args[8], args[9]);
221 break;
222 default:
223 throw new Exception(
224 "Cannot proxy calls with more than 10 parameters.");
225 }
226 if (_logging && b.logging) { 181 if (_logging && b.logging) {
227 log.add(new LogEntry(name, method, args, action, rtn)); 182 log.add(new LogEntry(name, method, args, action, rtn));
228 } 183 }
229 return rtn; 184 return rtn;
230 } 185 }
231 } 186 }
232 } 187 }
233 if (matchedMethodName) { 188 if (matchedMethodName) {
234 // User did specify behavior for this method, but all the 189 // User did specify behavior for this method, but all the
235 // actions are exhausted. This is considered an error. 190 // actions are exhausted. This is considered an error.
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 } 271 }
317 } 272 }
318 } 273 }
319 274
320 /** Clear both logs and behavior. */ 275 /** Clear both logs and behavior. */
321 void reset() { 276 void reset() {
322 resetBehavior(); 277 resetBehavior();
323 clearLogs(); 278 clearLogs();
324 } 279 }
325 } 280 }
OLDNEW
« no previous file with comments | « pkg/mock/lib/src/log_entry_list.dart ('k') | pkg/mock/lib/src/result_matcher.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698