Skip to content

Commit

Permalink
pep8 and a fix for #18
Browse files Browse the repository at this point in the history
  • Loading branch information
awestendorf committed May 6, 2014
1 parent 4c721d0 commit 695093d
Showing 1 changed file with 40 additions and 36 deletions.
76 changes: 40 additions & 36 deletions chai/chai.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
class ChaiTestType(type):

"""
Metaclass used to wrap all test methods to make sure the assert_expectations
in the correct context.
Metaclass used to wrap all test methods to make sure the
assert_expectations in the correct context.
"""

def __init__(cls, name, bases, d):
Expand All @@ -54,7 +54,8 @@ def __init__(cls, name, bases, d):
def test_wrapper(cls, func):
"""
Wraps a test method, when that test method has completed it
calls assert_expectations on the stub. This is to avoid getting to exceptions about the same error.
calls assert_expectations on the stub. This is to avoid getting to
exceptions about the same error.
"""

def wrapper(self, *args, **kwargs):
Expand All @@ -70,21 +71,21 @@ def wrapper(self, *args, **kwargs):
setattr(exc, '__traceback__', sys.exc_info()[-1])
raise exc
finally:
# Teardown all stubs so that if anyone stubbed methods that would be
# called during exception handling (e.g. "open"), the original method
# is used. Without, recursion limits are common with little insight
# into what went wrong.
# Teardown all stubs so that if anyone stubbed methods that
# would be called during exception handling (e.g. "open"),
# the original method is used. Without, recursion limits are
# common with little insight into what went wrong.
exceptions = []
try:
for stub in self._stubs:
for s in self._stubs:
# Make sure we collect any unmet expectations before
# teardown.
exceptions.extend(stub.unmet_expectations())
stub.teardown()
exceptions.extend(s.unmet_expectations())
s.teardown()
except:
# A rare case where this is about the best that can be done, as we
# don't want to supersede the actual exception if there is
# one.
# A rare case where this is about the best that can be
# done, as we don't want to supersede the actual
# exception if there is one.
traceback.print_exc()

if exceptions:
Expand All @@ -105,8 +106,6 @@ class ChaiBase(unittest.TestCase):
'''
Base class for all tests
'''
#__metaclass__ = ChaiTestType

# Load in the comparators
equals = Equals
almost_equals = AlmostEqual
Expand Down Expand Up @@ -135,11 +134,11 @@ def setUp(self):
self._mocks = deque()

# Try to load this into the module that the test case is defined in, so
# that 'self.' can be removed. This has to be done at the start of the test
# because we need the reference to be correct at the time of test run, not
# when the class is defined or an instance is created. Walks through the
# method resolution order to set it on every module for Chai subclasses
# to handle when tests are defined in subclasses.
# that 'self.' can be removed. This has to be done at the start of the
# test because we need the reference to be correct at the time of test
# run, not when the class is defined or an instance is created. Walks
# through the method resolution order to set it on every module for
# Chai subclasses to handle when tests are defined in subclasses.
for cls in inspect.getmro(self.__class__):
if cls.__module__.startswith('chai'):
break
Expand All @@ -149,28 +148,33 @@ def setUp(self):
continue
if attr.startswith('assert'):
setattr(mod, attr, getattr(self, attr))
elif isinstance(getattr(self, attr), type) and issubclass(getattr(self, attr), Comparator):
elif isinstance(getattr(self, attr), type) and \
issubclass(getattr(self, attr), Comparator):
setattr(mod, attr, getattr(self, attr))
setattr(mod, 'stub', self.stub)
setattr(mod, 'expect', self.expect)
setattr(mod, 'mock', self.mock)
if not hasattr(mod, 'stub'):
setattr(mod, 'stub', self.stub)
if not hasattr(mod, 'expect'):
setattr(mod, 'expect', self.expect)
if not hasattr(mod, 'mock'):
setattr(mod, 'mock', self.mock)

# Because cAmElCaSe sucks
setup = setUp

def tearDown(self):
super(ChaiBase, self).tearDown()

# Docs insist that this will be called no matter what happens in runTest(),
# so this should be a safe spot to unstub everything.
# Even with teardown at the end of test_wrapper, tear down here in case the
# test was skipped or there was otherwise a problem with that test.
# Docs insist that this will be called no matter what happens in
# runTest(), so this should be a safe spot to unstub everything.
# Even with teardown at the end of test_wrapper, tear down here in
# case the test was skipped or there was otherwise a problem with
# that test.
while len(self._stubs):
stub = self._stubs.popleft()
stub.teardown() # Teardown the reset of the stub

# Do the mocks in reverse order in the rare case someone called mock(obj,attr)
# twice.
# Do the mocks in reverse order in the rare case someone called
# mock(obj,attr) twice.
while len(self._mocks):
mock = self._mocks.pop()
if len(mock) == 2:
Expand All @@ -186,9 +190,9 @@ def tearDown(self):

def stub(self, obj, attr=None):
'''
Stub an object. If attr is not None, will attempt to stub that attribute
on the object. Only required for modules and other rare cases where we
can't determine the binding from the object.
Stub an object. If attr is not None, will attempt to stub that
attribute on the object. Only required for modules and other rare
cases where we can't determine the binding from the object.
'''
s = stub(obj, attr)
if s not in self._stubs:
Expand All @@ -197,8 +201,8 @@ def stub(self, obj, attr=None):

def expect(self, obj, attr=None):
'''
Open and return an expectation on an object. Will automatically create a
stub for the object. See stub documentation for argument information.
Open and return an expectation on an object. Will automatically create
a stub for the object. See stub documentation for argument information.
'''
return self.stub(obj, attr).expect()

Expand All @@ -207,7 +211,7 @@ def mock(self, obj=None, attr=None, **kwargs):
Return a mock object.
'''
rval = Mock(**kwargs)
if obj != None and attr != None:
if obj is not None and attr is not None:
rval._object = obj
rval._attr = attr

Expand Down

0 comments on commit 695093d

Please sign in to comment.