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 * A simple mocking/spy library. | 6 * A simple mocking/spy library. |
7 * | 7 * |
8 * ## Installing ## | 8 * ## Installing ## |
9 * | 9 * |
10 * Use [pub][] to install this package. Add the following to your `pubspec.yaml` | 10 * Use [pub][] to install this package. Add the following to your `pubspec.yaml` |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 * this.when(callsTo('bar')).alwaysCall(real.bar); | 101 * this.when(callsTo('bar')).alwaysCall(real.bar); |
102 * } | 102 * } |
103 * } | 103 * } |
104 * | 104 * |
105 * [pub]: http://pub.dartlang.org | 105 * [pub]: http://pub.dartlang.org |
106 */ | 106 */ |
107 | 107 |
108 library mock; | 108 library mock; |
109 | 109 |
110 import 'dart:mirrors' show MirrorSystem; | 110 import 'dart:mirrors' show MirrorSystem; |
| 111 import 'dart:collection' show LinkedHashMap; |
111 | 112 |
112 import 'matcher.dart'; | 113 import 'matcher.dart'; |
113 | 114 |
114 /** | 115 /** |
115 * The error formatter for mocking is a bit different from the default one | 116 * The error formatter for mocking is a bit different from the default one |
116 * for unit testing; instead of the third argument being a 'reason' | 117 * for unit testing; instead of the third argument being a 'reason' |
117 * it is instead a [signature] describing the method signature filter | 118 * it is instead a [signature] describing the method signature filter |
118 * that was used to select the logs that were verified. | 119 * that was used to select the logs that were verified. |
119 */ | 120 */ |
120 String _mockingErrorFormatter(actual, Matcher matcher, String signature, | 121 String _mockingErrorFormatter(actual, Matcher matcher, String signature, |
(...skipping 1109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1230 | 1231 |
1231 /** The shared log used for named mocks. */ | 1232 /** The shared log used for named mocks. */ |
1232 LogEntryList sharedLog = null; | 1233 LogEntryList sharedLog = null; |
1233 | 1234 |
1234 /** The base class for all mocked objects. */ | 1235 /** The base class for all mocked objects. */ |
1235 class Mock { | 1236 class Mock { |
1236 /** The mock name. Needed if the log is shared; optional otherwise. */ | 1237 /** The mock name. Needed if the log is shared; optional otherwise. */ |
1237 final String name; | 1238 final String name; |
1238 | 1239 |
1239 /** The set of [Behavior]s supported. */ | 1240 /** The set of [Behavior]s supported. */ |
1240 Map<String,Behavior> _behaviors; | 1241 LinkedHashMap<String,Behavior> _behaviors; |
1241 | 1242 |
1242 /** The [log] of calls made. Only used if [name] is null. */ | 1243 /** The [log] of calls made. Only used if [name] is null. */ |
1243 LogEntryList log; | 1244 LogEntryList log; |
1244 | 1245 |
1245 /** How to handle unknown method calls - swallow or throw. */ | 1246 /** How to handle unknown method calls - swallow or throw. */ |
1246 final bool _throwIfNoBehavior; | 1247 final bool _throwIfNoBehavior; |
1247 | 1248 |
1248 /** Whether to create an audit log or not. */ | 1249 /** Whether to create an audit log or not. */ |
1249 bool _logging; | 1250 bool _logging; |
1250 | 1251 |
1251 bool get logging => _logging; | 1252 bool get logging => _logging; |
1252 set logging(bool value) { | 1253 set logging(bool value) { |
1253 if (value && log == null) { | 1254 if (value && log == null) { |
1254 log = new LogEntryList(); | 1255 log = new LogEntryList(); |
1255 } | 1256 } |
1256 _logging = value; | 1257 _logging = value; |
1257 } | 1258 } |
1258 | 1259 |
1259 /** | 1260 /** |
1260 * Default constructor. Unknown method calls are allowed and logged, | 1261 * Default constructor. Unknown method calls are allowed and logged, |
1261 * the mock has no name, and has its own log. | 1262 * the mock has no name, and has its own log. |
1262 */ | 1263 */ |
1263 Mock() : _throwIfNoBehavior = false, log = null, name = null { | 1264 Mock() : _throwIfNoBehavior = false, log = null, name = null { |
1264 logging = true; | 1265 logging = true; |
1265 _behaviors = new Map<String,Behavior>(); | 1266 _behaviors = new LinkedHashMap<String,Behavior>(); |
1266 } | 1267 } |
1267 | 1268 |
1268 /** | 1269 /** |
1269 * This constructor makes a mock that has a [name] and possibly uses | 1270 * This constructor makes a mock that has a [name] and possibly uses |
1270 * a shared [log]. If [throwIfNoBehavior] is true, any calls to methods | 1271 * a shared [log]. If [throwIfNoBehavior] is true, any calls to methods |
1271 * that have no defined behaviors will throw an exception; otherwise they | 1272 * that have no defined behaviors will throw an exception; otherwise they |
1272 * will be allowed and logged (but will not do anything). | 1273 * will be allowed and logged (but will not do anything). |
1273 * If [enableLogging] is false, no logging will be done initially (whether | 1274 * If [enableLogging] is false, no logging will be done initially (whether |
1274 * or not a [log] is supplied), but [logging] can be set to true later. | 1275 * or not a [log] is supplied), but [logging] can be set to true later. |
1275 */ | 1276 */ |
1276 Mock.custom({this.name, | 1277 Mock.custom({this.name, |
1277 this.log, | 1278 this.log, |
1278 throwIfNoBehavior: false, | 1279 throwIfNoBehavior: false, |
1279 enableLogging: true}) : _throwIfNoBehavior = throwIfNoBehavior { | 1280 enableLogging: true}) : _throwIfNoBehavior = throwIfNoBehavior { |
1280 if (log != null && name == null) { | 1281 if (log != null && name == null) { |
1281 throw new Exception("Mocks with shared logs must have a name."); | 1282 throw new Exception("Mocks with shared logs must have a name."); |
1282 } | 1283 } |
1283 logging = enableLogging; | 1284 logging = enableLogging; |
1284 _behaviors = new Map<String,Behavior>(); | 1285 _behaviors = new LinkedHashMap<String,Behavior>(); |
1285 } | 1286 } |
1286 | 1287 |
1287 /** | 1288 /** |
1288 * [when] is used to create a new or extend an existing [Behavior]. | 1289 * [when] is used to create a new or extend an existing [Behavior]. |
1289 * A [CallMatcher] [filter] must be supplied, and the [Behavior]s for | 1290 * A [CallMatcher] [filter] must be supplied, and the [Behavior]s for |
1290 * that signature are returned (being created first if needed). | 1291 * that signature are returned (being created first if needed). |
1291 * | 1292 * |
1292 * Typical use case: | 1293 * Typical use case: |
1293 * | 1294 * |
1294 * mock.when(callsTo(...)).alwaysReturn(...); | 1295 * mock.when(callsTo(...)).alwaysReturn(...); |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1498 } | 1499 } |
1499 } | 1500 } |
1500 } | 1501 } |
1501 | 1502 |
1502 /** Clear both logs and behavior. */ | 1503 /** Clear both logs and behavior. */ |
1503 void reset() { | 1504 void reset() { |
1504 resetBehavior(); | 1505 resetBehavior(); |
1505 clearLogs(); | 1506 clearLogs(); |
1506 } | 1507 } |
1507 } | 1508 } |
OLD | NEW |