Debian Codegraph @

lib/tests/mock.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2009 Alexander Belchenko
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 """Simple mock objects."""
20
21 from breezy.tests import TestCase
22
23
24 class MockFunction(object):
25 """Mock function object that remember how many times it called
26 and which arguments were used.
27 """
28
29 def __init__(self, func=None, ret=None):
30 self.count = 0
31 self.args = []
32 self._func = func
33 self.ret = ret
34
35 def __call__(self, *args, **kw):
36 self.count += 1
37 self.args.append((args, kw))
38 if self._func is not None:
39 return self._func(*args, **kw)
40 else:
41 return self.ret
42
43 class TestMockFunction(TestCase):
44
45 def test_call(self):
46 mf = MockFunction()
47 self.assertEqual(0, mf.count)
48 self.assertEqual([], mf.args)
49 # 1st call
50 mf(None, 1, 'foo')
51 self.assertEqual(1, mf.count)
52 self.assertEqual([((None, 1, 'foo'), {})], mf.args)
53 # 2nd call
54 mf('bar', baz='spam')
55 self.assertEqual(2, mf.count)
56 self.assertEqual([
57 ((None, 1, 'foo'), {}),
58 (('bar',), {'baz': 'spam'}),
59 ], mf.args)