Skip to content

Instantly share code, notes, and snippets.

@apg
Created August 16, 2010 15:31
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 apg/527135 to your computer and use it in GitHub Desktop.
Save apg/527135 to your computer and use it in GitHub Desktop.
@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