Skip to content

Commit

Permalink
Merge pull request #4 from lowks/pep8
Browse files Browse the repository at this point in the history
Minor pep8 cleanup
  • Loading branch information
lowks committed Feb 15, 2016
2 parents 78c8c09 + 2d38d71 commit 648d8c2
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions minimock.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def next(x):
#
mocked = []


def lookup_by_name(name, nsdicts):
"""
Look up an object by name from a sequence of namespace dictionaries.
Expand All @@ -79,7 +80,7 @@ def lookup_by_name(name, nsdicts):
of the object that completes the name.
>>> import os
>>> nsdict, obj_name, attrs = lookup_by_name("os.path.isdir",
>>> nsdict, obj_name, attrs = lookup_by_name("os.path.isdir",
... (locals(),))
>>> obj_name, attrs
('os', ['path', 'isdir'])
Expand Down Expand Up @@ -112,6 +113,7 @@ def lookup_by_name(name, nsdicts):

raise NameError("name '%s' is not defined" % name)


def mock(name, nsdicts=None, mock_obj=None, **kw):
"""
Mock the named object, placing a Mock instance in the correct namespace
Expand Down Expand Up @@ -244,6 +246,7 @@ def mock(name, nsdicts=None, mock_obj=None, **kw):

mocked.append((original, nsdict, obj_name, attrs))


def restore():
"""
Restore all mocked objects.
Expand All @@ -265,18 +268,19 @@ def restore():
tmp = getattr(tmp, attr)
setattr(tmp, attrs[-1], original)


def assert_same_trace(tracker, want):
r"""
Check that the mock objects using ``tracker`` have been used as expected.
:param tracker: a :class:`TraceTracker` instance
:param want: the expected :class:`Printer` output
:type want: string
:raises: :exc:`AssertionError` if the expected and observed outputs don't
match
Example::
>>> tt = TraceTracker()
>>> m = Mock('mock_obj', tracker=tt)
>>> m.some_meth('dummy argument')
Expand All @@ -288,7 +292,8 @@ def assert_same_trace(tracker, want):
AssertionError...
"""
assert tracker.check(want), tracker.diff(want)



class AbstractTracker(object):
def __init__(self, *args, **kw):
raise NotImplementedError
Expand All @@ -299,6 +304,7 @@ def call(self, *args, **kw):
def set(self, *args, **kw):
raise NotImplementedError


class Printer(AbstractTracker):
"""Prints all calls to the file it's instantiated with.
Can take any object that implements `write'.
Expand All @@ -316,14 +322,15 @@ def call(self, func_name, *args, **kw):
func_name, ',\n '.join(parts))
self.file.write(msg)

def set(self, obj_name, attr, value):
def set(self, obj_name, attr, value):
"""
>>> z = Mock('z', show_attrs=True)
>>> z.a = 2
Set z.a = 2
"""
self.file.write('Set %s.%s = %r\n' % (obj_name, attr, value))



class TraceTracker(Printer):
"""
:class:`AbstractTracker` implementation for using MiniMock in non-
Expand All @@ -335,22 +342,22 @@ def __init__(self, *args, **kw):
self.out = StringIO()
super(TraceTracker, self).__init__(self.out, *args, **kw)
self.checker = MinimockOutputChecker()
self.options = doctest.ELLIPSIS
self.options = doctest.ELLIPSIS
self.options |= doctest.NORMALIZE_INDENTATION
self.options |= doctest.NORMALIZE_FUNCTION_PARAMETERS
self.options |= doctest.REPORT_UDIFF

def check(self, want):
r"""
Compare observed MiniMock usage with that which we expected.
:param want: the :class:`Printer` output that results from expected
usage of mocked objects
:type want: string
:rtype: a ``True`` value if the check passed, ``False`` otherwise
Example::
>>> tt = TraceTracker()
>>> m = Mock('mock_obj', tracker=tt)
>>> m.some_meth('arg1')
Expand All @@ -363,20 +370,20 @@ def check(self, want):
"""
return self.checker.check_output(want, self.dump(),
optionflags=self.options)

def diff(self, want):
r"""
Analyse differences between observed MiniMock usage and that which
we expected, if any.
:param want: the :class:`Printer` output that results from expected
usage of mocked objects
:type want: string
:rtype: a string summary of differences between the observed usage and
the ``want`` parameter
Example::
>>> tt = TraceTracker()
>>> m = Mock('mock_obj', tracker=tt)
>>> m.some_meth('dummy argument')
Expand All @@ -392,13 +399,13 @@ def diff(self, want):
else:
return self.checker.output_difference(doctest.Example("", want),
self.dump(), optionflags=self.options)

def dump(self):
r"""
Return the MiniMock object usage so far.
Example::
>>> tt = TraceTracker()
>>> m = Mock('mock_obj', tracker=tt)
>>> m.some_meth('dummy argument')
Expand Down Expand Up @@ -434,7 +441,7 @@ def normalize_function_parameters(text):
are separated by a single space ' '.
Example::
>>> tt = TraceTracker()
>>> foo = Mock("foo", tracker=tt)
>>> expect_mock_output = '''\
Expand Down Expand Up @@ -494,6 +501,7 @@ def __repr__(self):
DefaultTracker = _DefaultTracker()
del _DefaultTracker


class Mock(object):

def __init__(self, name, returns=None, returns_iter=None,
Expand Down Expand Up @@ -555,17 +563,17 @@ def __setattr__(self, attr, value):
'mock_returns_iter',
'mock_tracker',
'mock_show_attrs',
)):
)):
if attr == 'mock_returns_iter' and value is not None:
value = iter(value)
object.__setattr__(self, attr, value)
else:
if self.mock_show_attrs and self.mock_tracker is not None:
self.mock_tracker.set(self.mock_name, attr, value)
self.mock_attrs[attr] = value
self.mock_attrs[attr] = value

__test__ = {
"Mock" :
"Mock":
r"""
Test setting various "mock_" attributes on an existing Mock object.
Expand Down Expand Up @@ -597,7 +605,7 @@ def __setattr__(self, attr, value):
Set mock_obj.a = 2
""",

"mock" :
"mock":
r"""
An additional test for mocking a function accessed directly (i.e.
not via object attributes).
Expand Down

0 comments on commit 648d8c2

Please sign in to comment.