summaryrefslogtreecommitdiff
path: root/pecan/tests/test_util.py
blob: c1cdfbdadd5773a2b1f617a860da40102e5ec883 (plain)
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
import functools
import inspect
import unittest

from pecan import expose
from pecan import util


class TestArgSpec(unittest.TestCase):

    @property
    def controller(self):

        class RootController(object):

            @expose()
            def index(self, a, b, c=1, *args, **kwargs):
                return 'Hello, World!'

        return RootController()

    def test_no_decorator(self):
        expected = inspect.getargspec(self.controller.index.__func__)
        actual = util.getargspec(self.controller.index.__func__)
        assert expected == actual

    def test_simple_decorator(self):
        def dec(f):
            return f

        expected = inspect.getargspec(self.controller.index.__func__)
        actual = util.getargspec(dec(self.controller.index.__func__))
        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 = inspect.getargspec(self.controller.index.__func__)
        actual = util.getargspec(dec(self.controller.index.__func__))
        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 = inspect.getargspec(self.controller.index.__func__)
        actual = util.getargspec(dec(dec(dec(self.controller.index.__func__))))
        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 = inspect.getargspec(self.controller.index.__func__)
        actual = util.getargspec(dec(True)(self.controller.index.__func__))
        assert expected == actual