diff --git a/lib/unittest/mock.dart b/lib/unittest/mock.dart index a560c06192c..3d1a6f4e9df 100644 --- a/lib/unittest/mock.dart +++ b/lib/unittest/mock.dart @@ -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(); + } } diff --git a/tests/lib/unittest/mock_test.dart b/tests/lib/unittest/mock_test.dart index e2076ac0a9c..4d483c641d6 100644 --- a/tests/lib/unittest/mock_test.dart +++ b/tests/lib/unittest/mock_test.dart @@ -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)); + }); }