Added ability to reset Mocks.
Review URL: https://chromiumcodereview.appspot.com//10855128 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@10588 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
+22
-1
@@ -493,7 +493,8 @@ class LogEntryList {
|
||||
* a bool. If [logFilter] is null, it will match any [LogEntry].
|
||||
* If no entry is found, then [failureReturnValue] is returned.
|
||||
* After each check the position is updated by [skip], so using
|
||||
* [skip] of -1 allows backward searches.
|
||||
* [skip] of -1 allows backward searches, using a [skip] of 2 can
|
||||
* be used to check pairs of adjacent entries, and so on.
|
||||
*/
|
||||
int findLogEntry(logFilter, [int start = 0, int failureReturnValue = -1,
|
||||
skip = 1]) {
|
||||
@@ -1435,4 +1436,24 @@ class Mock {
|
||||
arg9 = _noArg]) =>
|
||||
getLogs(callsTo(method, arg0, arg1, arg2, arg3, arg4,
|
||||
arg5, arg6, arg7, arg8, arg9));
|
||||
|
||||
/** Clear the behaviors for the Mock. */
|
||||
void resetBehavior() => _behaviors.clear();
|
||||
|
||||
/** Clear the logs for the Mock. */
|
||||
void clearLogs() {
|
||||
if (log != null) {
|
||||
if (name == null) { // This log is not shared.
|
||||
log.logs.clear();
|
||||
} else { // This log may be shared.
|
||||
log.logs = log.logs.filter((e) => e.mockName != name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Clear both logs and behavior. */
|
||||
void reset() {
|
||||
resetBehavior();
|
||||
clearLogs();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -602,4 +602,35 @@ main() {
|
||||
});
|
||||
expect(total, equals((0 * 1) + (2 * 3) + (4 * 5) + (6 * 7) + (8 * 9)));
|
||||
});
|
||||
|
||||
test('Mocking: clearLogs', () {
|
||||
var m = new Mock();
|
||||
m.foo();
|
||||
m.foo();
|
||||
m.foo();
|
||||
expect(m.log.logs, hasLength(3));
|
||||
m.clearLogs();
|
||||
expect(m.log.logs, hasLength(0));
|
||||
LogEntryList log = new LogEntryList();
|
||||
var m1 = new Mock.custom(name: 'm1', log: log);
|
||||
var m2 = new Mock.custom(name: 'm2', log: log);
|
||||
var m3 = new Mock.custom(name: 'm3', log: log);
|
||||
for (var i = 0; i < 3; i++) {
|
||||
m1.foo();
|
||||
m2.bar();
|
||||
m3.pow();
|
||||
}
|
||||
expect(log.logs, hasLength(9));
|
||||
m1.clearLogs();
|
||||
expect(log.logs, hasLength(6));
|
||||
m1.clearLogs();
|
||||
expect(log.logs, hasLength(6));
|
||||
expect(log.logs.every((e) => e.mockName == 'm2' || e.mockName == 'm3'),
|
||||
isTrue);
|
||||
m2.clearLogs();
|
||||
expect(log.logs, hasLength(3));
|
||||
expect(log.logs.every((e) => e.mockName =='m3'), isTrue);
|
||||
m3.clearLogs();
|
||||
expect(log.logs, hasLength(0));
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user