1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
import functools
import unittest
from pecan import expose
from pecan import util
from pecan.compat import getargspec
class TestArgSpec(unittest.TestCase):
@property
def controller(self):
class RootController(object):
@expose()
def index(self, a, b, c=1, *args, **kwargs):
return 'Hello, World!'
@staticmethod
@expose()
def static_index(a, b, c=1, *args, **kwargs):
return 'Hello, World!'
return RootController()
def test_no_decorator(self):
expected = getargspec(self.controller.index.__func__)
actual = util.getargspec(self.controller.index.__func__)
assert expected == actual
expected = getargspec(self.controller.static_index)
actual = util.getargspec(self.controller.static_index)
assert expected == actual
def test_simple_decorator(self):
def dec(f):
return f
expected = getargspec(self.controller.index.__func__)
actual = util.getargspec(dec(self.controller.index.__func__))
assert expected == actual
expected = getargspec(self.controller.static_index)
actual = util.getargspec(dec(self.controller.static_index))
assert expected == actual
def test_simple_wrapper(self):
def dec(f):
@functools.wraps(f)
def wrapped(*a, **kw):
return f(*a, **kw)
return wrapped
expected = getargspec(self.controller.index.__func__)
actual = util.getargspec(dec(self.controller.index.__func__))
assert expected == actual
expected = getargspec(self.controller.static_index)
actual = util.getargspec(dec(self.controller.static_index))
assert expected == actual
def test_multiple_decorators(self):
def dec(f):
@functools.wraps(f)
def wrapped(*a, **kw):
return f(*a, **kw)
return wrapped
expected = getargspec(self.controller.index.__func__)
actual = util.getargspec(dec(dec(dec(self.controller.index.__func__))))
assert expected == actual
expected = getargspec(self.controller.static_index)
actual = util.getargspec(dec(dec(dec(
self.controller.static_index))))
assert expected == actual
def test_decorator_with_args(self):
def dec(flag):
def inner(f):
@functools.wraps(f)
def wrapped(*a, **kw):
return f(*a, **kw)
return wrapped
return inner
expected = getargspec(self.controller.index.__func__)
actual = util.getargspec(dec(True)(self.controller.index.__func__))
assert expected == actual
expected = getargspec(self.controller.static_index)
actual = util.getargspec(dec(True)(
self.controller.static_index))
assert expected == actual
def test_nested_cells(self):
def before(handler):
def deco(f):
def wrapped(*args, **kwargs):
if callable(handler):
handler()
return f(*args, **kwargs)
return wrapped
return deco
class RootController(object):
@expose()
@before(lambda: True)
def index(self, a, b, c):
return 'Hello, World!'
argspec = util._cfg(RootController.index)['argspec']
assert argspec.args == ['self', 'a', 'b', 'c']
def test_class_based_decorator(self):
class deco(object):
def __init__(self, arg):
self.arg = arg
def __call__(self, f):
@functools.wraps(f)
def wrapper(*args, **kw):
assert self.arg == '12345'
return f(*args, **kw)
return wrapper
class RootController(object):
@expose()
@deco('12345')
def index(self, a, b, c):
return 'Hello, World!'
argspec = util._cfg(RootController.index)['argspec']
assert argspec.args == ['self', 'a', 'b', 'c']
|