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