OLD | NEW |
1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import collections | 5 import collections |
6 import inspect | 6 import inspect |
7 | 7 |
8 from testing_support import thread_watcher | 8 from testing_support import thread_watcher |
9 | 9 |
10 | 10 |
(...skipping 12 matching lines...) Expand all Loading... |
23 return old_value | 23 return old_value |
24 | 24 |
25 def tearDown(self): | 25 def tearDown(self): |
26 """Restore all the mocked members.""" | 26 """Restore all the mocked members.""" |
27 if self._saved: | 27 if self._saved: |
28 for obj, items in self._saved.iteritems(): | 28 for obj, items in self._saved.iteritems(): |
29 for member, previous_value in items.iteritems(): | 29 for member, previous_value in items.iteritems(): |
30 setattr(obj, member, previous_value) | 30 setattr(obj, member, previous_value) |
31 | 31 |
32 | 32 |
| 33 def format_call(caller_name, *args, **kwargs): |
| 34 """Returns expectation suitable for passing to `SimpleMock.check_calls`.""" |
| 35 str_args = ', '.join(repr(arg) for arg in args) |
| 36 str_kwargs = ', '.join('%s=%r' % (k, v) |
| 37 for k, v in sorted(kwargs.iteritems())) |
| 38 return '%s(%s)' % ( |
| 39 caller_name, ', '.join(x for x in [str_args, str_kwargs] if x)) |
| 40 |
33 class SimpleMock(object): | 41 class SimpleMock(object): |
34 """Really simple manual class mock.""" | 42 """Really simple manual class mock.""" |
35 def __init__(self, unit_test, sorted_kwargs=False): | 43 def __init__(self, unit_test): |
36 """Do not call __init__ if you want to use the global call list to detect | 44 """Do not call __init__ if you want to use the global call list to detect |
37 ordering across different instances. | 45 ordering across different instances. |
38 | 46 |
39 Args: | 47 Args: |
40 unit_test (unittest.TestCase): instance of a test class. | 48 unit_test (unittest.TestCase): instance of a test class. |
41 sorted_kwargs (bool): if True, kwargs in expectations will always be | |
42 sorted by key. | |
43 """ | 49 """ |
44 # TODO(tandrii): sorted_kwargs MUST BE REALLY TRUE by default, but i don't | |
45 # want to fix all the tests, so keeping backwards compatability. | |
46 self.calls = [] | 50 self.calls = [] |
47 self.sorted_kwargs = sorted_kwargs | |
48 self.unit_test = unit_test | 51 self.unit_test = unit_test |
49 self.assertEqual = unit_test.assertEqual | 52 self.assertEqual = unit_test.assertEqual |
50 | 53 |
51 def pop_calls(self): | 54 def pop_calls(self): |
52 """Returns the list of calls up to date. | 55 """Returns the list of calls up to date. |
53 | 56 |
54 Good to do self.assertEqual(expected, mock.pop_calls()). | 57 Good to do self.assertEqual(expected, mock.pop_calls()). |
55 """ | 58 """ |
56 calls = self.calls | 59 calls = self.calls |
57 self.calls = [] | 60 self.calls = [] |
58 return calls | 61 return calls |
59 | 62 |
60 def check_calls(self, expected): | 63 def check_calls(self, expected): |
61 self.assertEqual(expected, self.pop_calls()) | 64 self.assertEqual(expected, self.pop_calls()) |
62 | 65 |
63 def _register_call(self, *args, **kwargs): | 66 def _register_call(self, *args, **kwargs): |
64 """Registers the name of the caller function.""" | 67 """Registers the name of the caller function.""" |
65 caller_name = kwargs.pop('caller_name', None) or inspect.stack()[1][3] | 68 caller_name = kwargs.pop('caller_name', None) or inspect.stack()[1][3] |
66 str_args = ', '.join(repr(arg) for arg in args) | 69 self.calls.append(format_call(caller_name, *args, **kwargs)) |
67 | |
68 kwargs_items = kwargs.items() | |
69 if self.sorted_kwargs: | |
70 kwargs_items.sort() | |
71 str_kwargs = ', '.join('%s=%r' % (k, v) for k, v in kwargs_items) | |
72 self.calls.append('%s(%s)' % ( | |
73 caller_name, ', '.join(filter(None, [str_args, str_kwargs])))) | |
74 | 70 |
75 | 71 |
76 class TestCase(thread_watcher.TestCase, AutoStubMixIn): | 72 class TestCase(thread_watcher.TestCase, AutoStubMixIn): |
77 """Adds self.mock() and self.has_failed() to a TestCase.""" | 73 """Adds self.mock() and self.has_failed() to a TestCase.""" |
78 def tearDown(self): | 74 def tearDown(self): |
79 AutoStubMixIn.tearDown(self) | 75 AutoStubMixIn.tearDown(self) |
80 super(TestCase, self).tearDown() | 76 super(TestCase, self).tearDown() |
OLD | NEW |