mirror of
https://git.anonymousland.org/anonymousland/synapse-product.git
synced 2024-10-01 08:25:44 -04:00
Define a CLOS-like 'around' modifier as a decorator, to neaten up the 'orig_*' noise of wrapping the setUp()/tearDown() methods
This commit is contained in:
parent
aeb69c0f8c
commit
7a77aabb4b
@ -26,6 +26,23 @@ logging.getLogger().addHandler(logging.StreamHandler())
|
|||||||
logging.getLogger().setLevel(NEVER)
|
logging.getLogger().setLevel(NEVER)
|
||||||
|
|
||||||
|
|
||||||
|
def around(target):
|
||||||
|
"""A CLOS-style 'around' modifier, which wraps the original method of the
|
||||||
|
given instance with another piece of code.
|
||||||
|
|
||||||
|
@around(self)
|
||||||
|
def method_name(orig, *args, **kwargs):
|
||||||
|
return orig(*args, **kwargs)
|
||||||
|
"""
|
||||||
|
def _around(code):
|
||||||
|
name = code.__name__
|
||||||
|
orig = getattr(target, name)
|
||||||
|
def new(*args, **kwargs):
|
||||||
|
return code(orig, *args, **kwargs)
|
||||||
|
setattr(target, name, new)
|
||||||
|
return _around
|
||||||
|
|
||||||
|
|
||||||
class TestCase(unittest.TestCase):
|
class TestCase(unittest.TestCase):
|
||||||
"""A subclass of twisted.trial's TestCase which looks for 'loglevel'
|
"""A subclass of twisted.trial's TestCase which looks for 'loglevel'
|
||||||
attributes on both itself and its individual test methods, to override the
|
attributes on both itself and its individual test methods, to override the
|
||||||
@ -40,23 +57,19 @@ class TestCase(unittest.TestCase):
|
|||||||
getattr(self, "loglevel",
|
getattr(self, "loglevel",
|
||||||
NEVER))
|
NEVER))
|
||||||
|
|
||||||
orig_setUp = self.setUp
|
@around(self)
|
||||||
|
def setUp(orig):
|
||||||
def setUp():
|
|
||||||
old_level = logging.getLogger().level
|
old_level = logging.getLogger().level
|
||||||
|
|
||||||
if old_level != level:
|
if old_level != level:
|
||||||
orig_tearDown = self.tearDown
|
@around(self)
|
||||||
|
def tearDown(orig):
|
||||||
def tearDown():
|
ret = orig()
|
||||||
ret = orig_tearDown()
|
|
||||||
logging.getLogger().setLevel(old_level)
|
logging.getLogger().setLevel(old_level)
|
||||||
return ret
|
return ret
|
||||||
self.tearDown = tearDown
|
|
||||||
|
|
||||||
logging.getLogger().setLevel(level)
|
logging.getLogger().setLevel(level)
|
||||||
return orig_setUp()
|
return orig()
|
||||||
self.setUp = setUp
|
|
||||||
|
|
||||||
|
|
||||||
def DEBUG(target):
|
def DEBUG(target):
|
||||||
|
Loading…
Reference in New Issue
Block a user