1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 class MockFunction(object):
6 '''Decorates a function to record the number of times it's called, and
7 use that to make test assertions.
12 def my_function(): pass
15 self.assertTrue(*my_function.CheckAndReset(2))
19 my_constructor = MockFunction(HTMLParser)
21 self.assertTrue(*my_constructor.CheckAndReset(1))
26 def __init__(self, fn):
30 def __call__(self, *args, **optargs):
32 return self._fn(*args, **optargs)
34 def CheckAndReset(self, expected_call_count):
35 actual_call_count = self._call_count
37 if expected_call_count == actual_call_count:
39 return (False, '%s: expected %s call(s), got %s' %
40 (self._fn.__name__, expected_call_count, actual_call_count))