summaryrefslogtreecommitdiffstats
path: root/deluge/tests/test_decorators.py
blob: 7d4bd98c87f6efb1dc7f1305866e075b64f8012c (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
# -*- coding: utf-8 -*-
#
# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with
# the additional special exception to link portions of this program with the OpenSSL library.
# See LICENSE for more details.
#

from __future__ import unicode_literals

from twisted.trial import unittest

from deluge.decorators import proxy


class DecoratorsTestCase(unittest.TestCase):
    def test_proxy_with_simple_functions(self):
        def negate(func, *args, **kwargs):
            return not func(*args, **kwargs)

        @proxy(negate)
        def something(_bool):
            return _bool

        @proxy(negate)
        @proxy(negate)
        def double_nothing(_bool):
            return _bool

        self.assertTrue(something(False))
        self.assertFalse(something(True))
        self.assertTrue(double_nothing(True))
        self.assertFalse(double_nothing(False))

    def test_proxy_with_class_method(self):
        def negate(func, *args, **kwargs):
            return -func(*args, **kwargs)

        class Test(object):
            def __init__(self, number):
                self.number = number

            @proxy(negate)
            def diff(self, number):
                return self.number - number

            @proxy(negate)
            def no_diff(self, number):
                return self.diff(number)

        t = Test(5)
        self.assertEqual(t.diff(1), -4)
        self.assertEqual(t.no_diff(1), 4)