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 14 matching lines...) Expand all Loading... |
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 class SimpleMock(object): | 33 class SimpleMock(object): |
34 """Really simple manual class mock.""" | 34 """Really simple manual class mock.""" |
35 def __init__(self, unit_test): | 35 def __init__(self, unit_test, sorted_kwargs=False): |
36 """Do not call __init__ if you want to use the global call list to detect | 36 """Do not call __init__ if you want to use the global call list to detect |
37 ordering across different instances. | 37 ordering across different instances. |
38 | 38 |
39 Args: | 39 Args: |
40 unit_test (unittest.TestCase): instance of a test class. | 40 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. |
41 """ | 43 """ |
| 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. |
42 self.calls = [] | 46 self.calls = [] |
| 47 self.sorted_kwargs = sorted_kwargs |
43 self.unit_test = unit_test | 48 self.unit_test = unit_test |
44 self.assertEqual = unit_test.assertEqual | 49 self.assertEqual = unit_test.assertEqual |
45 | 50 |
46 def pop_calls(self): | 51 def pop_calls(self): |
47 """Returns the list of calls up to date. | 52 """Returns the list of calls up to date. |
48 | 53 |
49 Good to do self.assertEqual(expected, mock.pop_calls()). | 54 Good to do self.assertEqual(expected, mock.pop_calls()). |
50 """ | 55 """ |
51 calls = self.calls | 56 calls = self.calls |
52 self.calls = [] | 57 self.calls = [] |
53 return calls | 58 return calls |
54 | 59 |
55 def check_calls(self, expected): | 60 def check_calls(self, expected): |
56 self.assertEqual(expected, self.pop_calls()) | 61 self.assertEqual(expected, self.pop_calls()) |
57 | 62 |
58 def _register_call(self, *args, **kwargs): | 63 def _register_call(self, *args, **kwargs): |
59 """Registers the name of the caller function.""" | 64 """Registers the name of the caller function.""" |
60 caller_name = kwargs.pop('caller_name', None) or inspect.stack()[1][3] | 65 caller_name = kwargs.pop('caller_name', None) or inspect.stack()[1][3] |
61 str_args = ', '.join(repr(arg) for arg in args) | 66 str_args = ', '.join(repr(arg) for arg in args) |
62 str_kwargs = ', '.join('%s=%r' % (k, v) for k, v in kwargs.iteritems()) | 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) |
63 self.calls.append('%s(%s)' % ( | 72 self.calls.append('%s(%s)' % ( |
64 caller_name, ', '.join(filter(None, [str_args, str_kwargs])))) | 73 caller_name, ', '.join(filter(None, [str_args, str_kwargs])))) |
65 | 74 |
66 | 75 |
67 class TestCase(thread_watcher.TestCase, AutoStubMixIn): | 76 class TestCase(thread_watcher.TestCase, AutoStubMixIn): |
68 """Adds self.mock() and self.has_failed() to a TestCase.""" | 77 """Adds self.mock() and self.has_failed() to a TestCase.""" |
69 def tearDown(self): | 78 def tearDown(self): |
70 AutoStubMixIn.tearDown(self) | 79 AutoStubMixIn.tearDown(self) |
71 super(TestCase, self).tearDown() | 80 super(TestCase, self).tearDown() |
OLD | NEW |