MCPcopy Create free account
hub / github.com/apple/coremltools / MockObject

Class MockObject

deps/protobuf/python/mox.py:359–507  ·  view source on GitHub ↗

A mock object that simulates the public/protected interface of a class.

Source from the content-addressed store, hash-verified

357
358
359class MockObject(MockAnything, object):
360 """A mock object that simulates the public/protected interface of a class."""
361
362 def __init__(self, class_to_mock):
363 """Initialize a mock object.
364
365 This determines the methods and properties of the class and stores them.
366
367 Args:
368 # class_to_mock: class to be mocked
369 class_to_mock: class
370 """
371
372 # This is used to hack around the mixin/inheritance of MockAnything, which
373 # is not a proper object (it can be anything. :-)
374 MockAnything.__dict__['__init__'](self)
375
376 # Get a list of all the public and special methods we should mock.
377 self._known_methods = set()
378 self._known_vars = set()
379 self._class_to_mock = class_to_mock
380 for method in dir(class_to_mock):
381 if callable(getattr(class_to_mock, method)):
382 self._known_methods.add(method)
383 else:
384 self._known_vars.add(method)
385
386 def __getattr__(self, name):
387 """Intercept attribute request on this object.
388
389 If the attribute is a public class variable, it will be returned and not
390 recorded as a call.
391
392 If the attribute is not a variable, it is handled like a method
393 call. The method name is checked against the set of mockable
394 methods, and a new MockMethod is returned that is aware of the
395 MockObject's state (record or replay). The call will be recorded
396 or replayed by the MockMethod's __call__.
397
398 Args:
399 # name: the name of the attribute being requested.
400 name: str
401
402 Returns:
403 Either a class variable or a new MockMethod that is aware of the state
404 of the mock (record or replay).
405
406 Raises:
407 UnknownMethodCallError if the MockObject does not mock the requested
408 method.
409 """
410
411 if name in self._known_vars:
412 return getattr(self._class_to_mock, name)
413
414 if name in self._known_methods:
415 return self._CreateMockMethod(name)
416

Callers 1

CreateMockMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected