Skip to content

Instantly share code, notes, and snippets.

@dcrosta
Created March 8, 2012 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcrosta/2002322 to your computer and use it in GitHub Desktop.
Save dcrosta/2002322 to your computer and use it in GitHub Desktop.
declarative, readable python testing
from functools import wraps
class Equalizer(object):
def __init__(self, rv):
self._rv = rv
def __eq__(self, other):
assert self._rv == other
class Puny(object):
def __init__(self, wrapped):
self._wrapped = wrapped
def __getattr__(self, name):
# import pdb; pdb.set_trace()
attr = getattr(self._wrapped, name)
if callable(attr):
@wraps(attr)
def wrapperfunc(*args, **kwargs):
return Equalizer(attr(*args, **kwargs))
return wrapperfunc
raise AttributeError('%r object has no attribute %s' % (self._wrapped.__class__.__name__, name))
if __name__ == '__main__':
class ThingToTest(object):
def return_string(self, rv):
return rv
t = Puny(ThingToTest())
t.return_string('foo') == 'foo'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment