File: test_descriptors_py36.py

package info (click to toggle)
python-wrapt 2.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,592 kB
  • sloc: python: 8,452; ansic: 2,978; makefile: 168; sh: 46
file content (38 lines) | stat: -rw-r--r-- 928 bytes parent folder | download
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
import unittest

import wrapt


class TestObjectDescriptors(unittest.TestCase):

    def test_set_name(self):
        @wrapt.decorator
        def _decorator(wrapped, instance, args, kwargs):
            return wrapped(*args, **kwargs)

        attribute_name = []

        class _descriptor_wrapper:
            def __init__(self, descriptor):
                self.__wrapped__ = descriptor

            def __set_name__(self, owner, name):
                attribute_name.append(name)

            def __get__(self, instance, owner=None):
                return self.__wrapped__.__get__(instance, owner)

        class Instance:
            @_decorator
            @_descriptor_wrapper
            def method(self):
                return True

        instance = Instance()

        self.assertEqual(attribute_name, ["method"])
        self.assertEqual(instance.method(), True)


if __name__ == "__main__":
    unittest.main()