OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 """ | 5 """ |
6 A test facility to assert call sequences while mocking their behavior. | 6 A test facility to assert call sequences while mocking their behavior. |
7 """ | 7 """ |
8 | 8 |
9 import os | 9 import os |
10 import sys | 10 import sys |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 return mock.patch(call.name, **kwargs) | 115 return mock.patch(call.name, **kwargs) |
116 | 116 |
117 def watchCalls(self, calls): | 117 def watchCalls(self, calls): |
118 """Add calls to the set of watched calls. | 118 """Add calls to the set of watched calls. |
119 | 119 |
120 Args: | 120 Args: |
121 calls: a sequence of mock.call instances identifying targets to watch | 121 calls: a sequence of mock.call instances identifying targets to watch |
122 """ | 122 """ |
123 self._watched.update((call.name, call) for call in calls) | 123 self._watched.update((call.name, call) for call in calls) |
124 | 124 |
125 def watchMethodCalls(self, call): | 125 def watchMethodCalls(self, call, ignore=None): |
126 """Watch all public methods of the target identified by a self.call. | 126 """Watch all public methods of the target identified by a self.call. |
127 | 127 |
128 Args: | 128 Args: |
129 call: a self.call instance indetifying an object | 129 call: a self.call instance indetifying an object |
| 130 ignore: a list of public methods to ignore when watching for calls |
130 """ | 131 """ |
131 target = self.call_target(call) | 132 target = self.call_target(call) |
| 133 if ignore is None: |
| 134 ignore = [] |
132 self.watchCalls(getattr(call, method) | 135 self.watchCalls(getattr(call, method) |
133 for method in dir(target.__class__) | 136 for method in dir(target.__class__) |
134 if not method.startswith('_')) | 137 if not method.startswith('_') and not method in ignore) |
135 | 138 |
136 def clearWatched(self): | 139 def clearWatched(self): |
137 """Clear the set of watched calls.""" | 140 """Clear the set of watched calls.""" |
138 self._watched = {} | 141 self._watched = {} |
139 | 142 |
140 def assertCalls(self, *calls): | 143 def assertCalls(self, *calls): |
141 """A context manager to assert that a sequence of calls is made. | 144 """A context manager to assert that a sequence of calls is made. |
142 | 145 |
143 During the assertion, a number of functions and methods will be "watched", | 146 During the assertion, a number of functions and methods will be "watched", |
144 and any calls made to them is expected to appear---in the exact same order, | 147 and any calls made to them is expected to appear---in the exact same order, |
(...skipping 20 matching lines...) Expand all Loading... |
165 Raises: | 168 Raises: |
166 AssertionError if the watched targets do not receive the exact sequence | 169 AssertionError if the watched targets do not receive the exact sequence |
167 of calls specified. Missing calls, extra calls, and calls with | 170 of calls specified. Missing calls, extra calls, and calls with |
168 mismatching arguments, all cause the assertion to fail. | 171 mismatching arguments, all cause the assertion to fail. |
169 """ | 172 """ |
170 return self._AssertCalls(self, calls, self._watched) | 173 return self._AssertCalls(self, calls, self._watched) |
171 | 174 |
172 def assertCall(self, call, action=None): | 175 def assertCall(self, call, action=None): |
173 return self.assertCalls((call, action)) | 176 return self.assertCalls((call, action)) |
174 | 177 |
OLD | NEW |