Created
March 8, 2012 17:50
-
-
Save dcrosta/2002322 to your computer and use it in GitHub Desktop.
declarative, readable python testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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