A mock that can be used to mock anything. This is helpful for mocking classes that do not provide a public interface.
| 266 | |
| 267 | |
| 268 | class MockAnything: |
| 269 | """A mock that can be used to mock anything. |
| 270 | |
| 271 | This is helpful for mocking classes that do not provide a public interface. |
| 272 | """ |
| 273 | |
| 274 | def __init__(self): |
| 275 | """ """ |
| 276 | self._Reset() |
| 277 | |
| 278 | def __getattr__(self, method_name): |
| 279 | """Intercept method calls on this object. |
| 280 | |
| 281 | A new MockMethod is returned that is aware of the MockAnything's |
| 282 | state (record or replay). The call will be recorded or replayed |
| 283 | by the MockMethod's __call__. |
| 284 | |
| 285 | Args: |
| 286 | # method name: the name of the method being called. |
| 287 | method_name: str |
| 288 | |
| 289 | Returns: |
| 290 | A new MockMethod aware of MockAnything's state (record or replay). |
| 291 | """ |
| 292 | |
| 293 | return self._CreateMockMethod(method_name) |
| 294 | |
| 295 | def _CreateMockMethod(self, method_name): |
| 296 | """Create a new mock method call and return it. |
| 297 | |
| 298 | Args: |
| 299 | # method name: the name of the method being called. |
| 300 | method_name: str |
| 301 | |
| 302 | Returns: |
| 303 | A new MockMethod aware of MockAnything's state (record or replay). |
| 304 | """ |
| 305 | |
| 306 | return MockMethod(method_name, self._expected_calls_queue, |
| 307 | self._replay_mode) |
| 308 | |
| 309 | def __nonzero__(self): |
| 310 | """Return 1 for nonzero so the mock can be used as a conditional.""" |
| 311 | |
| 312 | return 1 |
| 313 | |
| 314 | def __eq__(self, rhs): |
| 315 | """Provide custom logic to compare objects.""" |
| 316 | |
| 317 | return (isinstance(rhs, MockAnything) and |
| 318 | self._replay_mode == rhs._replay_mode and |
| 319 | self._expected_calls_queue == rhs._expected_calls_queue) |
| 320 | |
| 321 | def __ne__(self, rhs): |
| 322 | """Provide custom logic to compare objects.""" |
| 323 | |
| 324 | return not self == rhs |
| 325 |