Created
August 16, 2010 15:31
-
-
Save apg/527135 to your computer and use it in GitHub Desktop.
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
@contextmanager | |
def assertingBeforeAfter(test, pre_post_f, *args, **kwargs): | |
""">>> x = [1, 2, 3] | |
>>> with assertingBeforeAfter(lambda o, n: o != n, lambda: len(x)) | |
... x.append(1) | |
>>> | |
>>> x = [1, 2, 3] | |
>>> with assertingBeforeAfter(lambda o, n: o != n, lambda: len(x)) | |
... print "hi" | |
Traceback (most recent call last): | |
File "<stdin>", line 2, in <module> | |
File "/usr/lib64/python2.6/contextlib.py", line 23, in __exit__ | |
self.gen.next() | |
File "<stdin>", line 8, in assertingBeforeAfter | |
AssertionError | |
""" | |
old = pre_post_f(*args, **kwargs) | |
try: | |
yield | |
finally: | |
new = pre_post_f(*args, **kwargs) | |
assert test(old, new), "test was not true for args=%r, kwargs=%r" % \ | |
(args, kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment