Skip to content

Commit

Permalink
another decorator tested
Browse files Browse the repository at this point in the history
  • Loading branch information
leifj committed Sep 9, 2014
1 parent 43008d5 commit 49368a7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
17 changes: 9 additions & 8 deletions src/pyff/decorators.py
Expand Up @@ -55,19 +55,20 @@ def f_retry(*args, **kwargs):
return deco_retry


def deprecated(func):
def deprecated(logger=log):
"""This is a decorator which can be used to mark functions
as deprecated. It will result in a warning being emitted
when the function is used."""

@functools.wraps(func)
def new_func(*args, **kwargs):
log.warn("Call to deprecated function %s at %s:%d" % (func.__name__,
func.func_code.co_filename,
func.func_code.co_firstlineno + 1))
return func(*args, **kwargs)
def decorating(func):
def new_func(*args, **kwargs):
logger.warn("Call to deprecated function %s at %s:%d" % (func.__name__,
func.func_code.co_filename,
func.func_code.co_firstlineno + 1))
return func(*args, **kwargs)

return new_func
return new_func
return decorating


class _HashedSeq(list):
Expand Down
25 changes: 24 additions & 1 deletion src/pyff/test/test_decorators.py
@@ -1,5 +1,6 @@
import logging
from unittest import TestCase
from pyff.decorators import retry
from pyff.decorators import retry, deprecated


class TestRetry(TestCase):
Expand All @@ -25,3 +26,25 @@ def fails():
except ValueError, ex:
pass
assert(not status[0])

class TestDeprecated(TestCase):
def test_deprecate(self):

class Logger():
def __init__(self):
self.messages = []

def warn(self, message):
self.messages.append((logging.WARNING, message))

_logger = Logger()

@deprecated(logger=_logger)
def test():
pass

assert(len(_logger.messages) == 0)
test()
assert(len(_logger.messages) == 1)
assert('Call to deprecated function' in _logger.messages[0][1])
assert(_logger.messages[0][0] == logging.WARNING)

0 comments on commit 49368a7

Please sign in to comment.