File size: 3,110 Bytes
065fee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import print_function

import unittest
import inspect
import types

import wrapt

from compat import PY2, PY3, exec_

class TestInheritance(unittest.TestCase):

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

        @passthru
        class BaseClass(object):
            def __init__(self):
                self.value = 1

        class DerivedClass(BaseClass):
            def __init__(self):
                super(DerivedClass, self).__init__()
                self.value = 2

        base = BaseClass()

        self.assertEqual(type(base), BaseClass.__wrapped__)
        self.assertTrue(isinstance(base, BaseClass.__wrapped__))
        self.assertEqual(base.value, 1)

        self.assertEqual(type(base).__mro__, (BaseClass.__wrapped__, object))
        self.assertEqual(BaseClass.__mro_entries__(()), (BaseClass.__wrapped__,))

        derived = DerivedClass()

        self.assertEqual(type(derived), DerivedClass)
        self.assertTrue(isinstance(derived, BaseClass.__wrapped__))
        self.assertTrue(isinstance(derived, DerivedClass))
        self.assertEqual(derived.value, 2)

        self.assertEqual(type(derived).__mro__, (DerivedClass, BaseClass.__wrapped__, object))

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

        @passthru
        class BaseClass1(object):
            pass

        @passthru
        class BaseClass2(object):
            pass

        class DerivedClass(BaseClass1, BaseClass2):
            pass

        derived = DerivedClass()

        self.assertEqual(type(derived), DerivedClass)
        self.assertTrue(isinstance(derived, BaseClass1.__wrapped__))
        self.assertTrue(isinstance(derived, BaseClass2.__wrapped__))
        self.assertTrue(isinstance(derived, DerivedClass))

        self.assertEqual(type(derived).__mro__, (DerivedClass,
                BaseClass1.__wrapped__, BaseClass2.__wrapped__, object))

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

        @passthru
        class CommonClass(object):
            pass

        @passthru
        class BaseClass1(CommonClass):
            pass

        @passthru
        class BaseClass2(CommonClass):
            pass

        class DerivedClass(BaseClass1, BaseClass2):
            pass

        derived = DerivedClass()

        self.assertEqual(type(derived), DerivedClass)
        self.assertTrue(isinstance(derived, CommonClass.__wrapped__))
        self.assertTrue(isinstance(derived, BaseClass1.__wrapped__))
        self.assertTrue(isinstance(derived, BaseClass2.__wrapped__))
        self.assertTrue(isinstance(derived, DerivedClass))

        self.assertEqual(type(derived).__mro__, (DerivedClass,
            BaseClass1.__wrapped__, BaseClass2.__wrapped__,
            CommonClass.__wrapped__, object))