Index: lib/unittest/mock.dart |
=================================================================== |
--- lib/unittest/mock.dart (revision 9570) |
+++ lib/unittest/mock.dart (working copy) |
@@ -12,7 +12,7 @@ |
var description = new StringDescription(); |
description.add('Expected ${signature} ').addDescriptionOf(matcher). |
add('\n but: '); |
- matcher.describeMismatch(actual, description); |
+ matcher.describeMismatch(actual, description).add('.'); |
return description.toString(); |
} |
@@ -156,7 +156,7 @@ |
return false; |
} |
if (arguments.length < argMatchers.length) { |
- throw new Exception("Less arguments than matchers for $method"); |
+ throw new Exception("Less arguments than matchers for $method."); |
} |
for (var i = 0; i < argMatchers.length; i++) { |
if (!argMatchers[i].matches(arguments[i])) { |
@@ -735,13 +735,16 @@ |
LogEntryList log; |
/** How to handle unknown method calls - swallow or throw. */ |
- final bool throwIfNoBehavior = false; |
+ final bool throwIfNoBehavior; |
+ /** Whether to create an audit log or not. */ |
+ final bool logging; |
+ |
/** |
* Default constructor. Unknown method calls are allowed and logged, |
* the mock has no name, and has its own log. |
*/ |
- Mock() : throwIfNoBehavior = false, name = null { |
+ Mock() : throwIfNoBehavior = false, logging = true, name = null { |
log = new LogEntryList(); |
behaviors = new Map<String,Behavior>(); |
} |
@@ -750,12 +753,16 @@ |
* This constructor makes a mock that has a [name] and possibly uses |
* a shared [log]. If [throwIfNoBehavior] is true, any calls to methods |
* that have no defined behaviors will throw an exception; otherwise they |
- * will be allowed and logged (but will not do anything). |
+ * will be allowed and logged (but will not do anything). If [logging] |
+ * is false, no logging will be done (whether or not a [log] is supplied). |
*/ |
Mock.custom([this.name, |
this.log, |
- this.throwIfNoBehavior = false]) { |
- if (log == null) { |
+ this.throwIfNoBehavior = false, |
+ this.logging = true]) { |
+ if (!logging) { |
+ log = null; |
+ } else if (log == null) { |
log = new LogEntryList(); |
} |
behaviors = new Map<String,Behavior>(); |
@@ -816,10 +823,14 @@ |
_Action action = response.action; |
var value = response.value; |
if (action == _Action.RETURN) { |
- log.add(new LogEntry(name, method, args, action, value)); |
+ if (log != null) { |
Siggi Cherem (dart-lang)
2012/07/11 22:46:04
why not continbue using the flag? ('if (logging)'
gram
2012/07/11 23:24:07
You're right - I originally didn't mean to make th
|
+ log.add(new LogEntry(name, method, args, action, value)); |
+ } |
return value; |
} else if (action == _Action.THROW) { |
- log.add(new LogEntry(name, method, args, action, value)); |
+ if (log != null) { |
+ log.add(new LogEntry(name, method, args, action, value)); |
+ } |
throw value; |
} else if (action == _Action.PROXY) { |
var rtn; |
@@ -864,9 +875,11 @@ |
break; |
default: |
throw new Exception( |
- "Cannot proxy calls with more than 10 parameters"); |
+ "Cannot proxy calls with more than 10 parameters."); |
} |
- log.add(new LogEntry(name, method, args, action, rtn)); |
+ if (log != null) { |
+ log.add(new LogEntry(name, method, args, action, rtn)); |
+ } |
return rtn; |
} |
} |
@@ -875,18 +888,25 @@ |
// User did specify behavior for this method, but all the |
// actions are exhausted. This is considered an error. |
throw new Exception('No more actions for method ' |
- '${_qualifiedName(name, method)}'); |
+ '${_qualifiedName(name, method)}.'); |
} else if (throwIfNoBehavior) { |
throw new Exception('No behavior specified for method ' |
- '${_qualifiedName(name, method)}'); |
+ '${_qualifiedName(name, method)}.'); |
} |
- // User hasn't specified behavior for this method; we don't throw |
+ // Otherwise user hasn't specified behavior for this method; we don't throw |
// so we can underspecify. |
- log.add(new LogEntry(name, method, args, _Action.IGNORE)); |
+ if (log != null) { |
+ log.add(new LogEntry(name, method, args, _Action.IGNORE)); |
+ } |
} |
/** [verifyZeroInteractions] returns true if no calls were made */ |
- bool verifyZeroInteractions() => log.logs.length == 0; |
+ bool verifyZeroInteractions() { |
+ if (log == null) { |
+ throw new Exception("Can't verify behavior when logging is disabled."); |
+ } |
+ return log.logs.length == 0; |
+ } |
/** |
* [getLogs] extracts all calls from the call log that match the |
@@ -904,6 +924,10 @@ |
LogEntryList getLogs([CallMatcher logFilter, |
Matcher actionMatcher, |
bool destructive = false]) { |
- return log.getMatches(name, logFilter, actionMatcher, destructive); |
+ if (log == null) { |
+ throw new Exception("Can't retrieve logs when logging is disabled."); |
+ } else { |
+ return log.getMatches(name, logFilter, actionMatcher, destructive); |
+ } |
} |
} |