aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken6/tests/samplebinding/multiple_derived_test.py
blob: 46e44601d0edf2b9ee552d7a217b558a6d97adc5 (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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

'''Test cases for multiple inheritance'''

import os
import sys
import unittest

from pathlib import Path
sys.path.append(os.fspath(Path(__file__).resolve().parents[1]))
from shiboken_paths import init_paths
init_paths()

from sample import Base1, Base2, Base3, Base4, Base5, Base6
from sample import MDerived1, MDerived2, MDerived3, MDerived4, MDerived5, SonOfMDerived1

class ExtMDerived1(MDerived1):
    def __init__(self):
        MDerived1.__init__(self)
        self.multiplier = 20
        self.base2Method_called = False
    def base2Method(self):
        return Base2.base2Method(self) * self.multiplier

class MultipleDerivedTest(unittest.TestCase):
    '''Test cases for multiple inheritance'''

    def testIsInstance(self):
        '''MDerived1 is instance of its parents Base1 and Base2.'''
        a = MDerived1()
        self.assertTrue(isinstance(a, MDerived1))
        self.assertTrue(isinstance(a, Base1))
        self.assertTrue(isinstance(a, Base2))

    def testIsSubclass(self):
        '''MDerived1 is subclass of its parents Base1 and Base2.'''
        self.assertTrue(issubclass(MDerived1, Base1))
        self.assertTrue(issubclass(MDerived1, Base2))

    def testCallToFunctionWithBase1ArgumentThatCastsBackToMDerived1(self):
        '''MDerived1 is passed as an Base1 argument to a method that returns it casted back to MDerived1.'''
        a = MDerived1()
        b = MDerived1.transformFromBase1(a)
        self.assertEqual(a, b)

    def testCallToFunctionWithBase2ArgumentThatCastsBackToMDerived1(self):
        '''MDerived1 is passed as an Base2 argument to a method that returns it casted back to MDerived1.'''
        a = MDerived1()
        b = MDerived1.transformFromBase2(a)
        self.assertEqual(a, b)

    def testPythonClassIsInstance(self):
        '''Python defined class ExtMDerived1 is instance of its parents MDerived1, Base1 and Base2.'''
        a = ExtMDerived1()
        self.assertTrue(isinstance(a, ExtMDerived1))
        self.assertTrue(isinstance(a, MDerived1))
        self.assertTrue(isinstance(a, Base1))
        self.assertTrue(isinstance(a, Base2))

    def testPythonClassIsSubclass(self):
        '''Python defined class ExtMDerived1 is subclass of its parents MDerived1, Base1 and Base2.'''
        self.assertTrue(issubclass(ExtMDerived1, MDerived1))
        self.assertTrue(issubclass(ExtMDerived1, Base1))
        self.assertTrue(issubclass(ExtMDerived1, Base2))

    @unittest.skipUnless(hasattr(sys, "getrefcount"), f"{sys.implementation.name} has no refcount")
    def testCastFromMDerived1ToBases(self):
        '''MDerived1 is casted by C++ to its parents and the binding must return the MDerived1 wrapper.'''
        a = MDerived1()
        refcnt = sys.getrefcount(a)
        b1 = a.castToBase1()
        b2 = a.castToBase2()
        self.assertTrue(isinstance(b1, MDerived1))
        self.assertTrue(isinstance(b2, MDerived1))
        self.assertEqual(a, b1)
        self.assertEqual(a, b2)
        self.assertEqual(sys.getrefcount(a), refcnt + 2)

    @unittest.skipUnless(hasattr(sys, "getrefcount"), f"{sys.implementation.name} has no refcount")
    def testCastFromExtMDerived1ToMDerived1Bases(self):
        '''Python defined class ExtMDerived1 is casted by C++ to MDerived1 parents and the binding must return the correct ExtMDerived1 instance.'''
        a = ExtMDerived1()
        refcnt = sys.getrefcount(a)
        b1 = a.castToBase1()
        self.assertTrue(isinstance(b1, MDerived1))
        self.assertTrue(isinstance(b1, ExtMDerived1))
        b2 = a.castToBase2()
        self.assertTrue(isinstance(b2, MDerived1))
        self.assertTrue(isinstance(b2, ExtMDerived1))
        self.assertEqual(a, b1)
        self.assertEqual(a, b2)
        self.assertEqual(sys.getrefcount(a), refcnt + 2)

    @unittest.skipUnless(hasattr(sys, "getrefcount"), f"{sys.implementation.name} has no refcount")
    def testCastFromSonOfMDerived1ToBases(self):
        '''SonOfMDerived1 is casted by C++ to its parents and the binding must return the SonOfMDerived1 wrapper.'''
        a = SonOfMDerived1()
        refcnt = sys.getrefcount(a)
        md1 = a.castToMDerived1()
        b1 = a.castToBase1()
        b2 = a.castToBase2()
        self.assertTrue(isinstance(md1, SonOfMDerived1))
        self.assertTrue(isinstance(b2, SonOfMDerived1))
        self.assertTrue(isinstance(b2, SonOfMDerived1))
        self.assertEqual(a, md1)
        self.assertEqual(a, b1)
        self.assertEqual(a, b2)
        self.assertEqual(sys.getrefcount(a), refcnt + 3)

    def testReimplementedBase2VirtualMethodOnClassInheritingFromMDerived1(self):
        a = ExtMDerived1()
        value = a.base2Method()
        self.assertTrue(value, Base2.base2Method(a) * a.multiplier)

    @unittest.skipUnless(hasattr(sys, "getrefcount"), f"{sys.implementation.name} has no refcount")
    def testCastFromMDerived2ToBases(self):
        '''MDerived2 is casted by C++ to its parents and the binding must return the MDerived2 wrapper.'''
        a = MDerived2()
        refcnt = sys.getrefcount(a)
        b3 = a.castToBase3()
        b4 = a.castToBase4()
        b5 = a.castToBase5()
        b6 = a.castToBase6()
        self.assertTrue(isinstance(b3, MDerived2))
        self.assertTrue(isinstance(b4, MDerived2))
        self.assertTrue(isinstance(b5, MDerived2))
        self.assertTrue(isinstance(b6, MDerived2))
        self.assertEqual(a, b3)
        self.assertEqual(a, b4)
        self.assertEqual(a, b5)
        self.assertEqual(a, b6)
        self.assertEqual(sys.getrefcount(a), refcnt + 4)

    @unittest.skipUnless(hasattr(sys, "getrefcount"), f"{sys.implementation.name} has no refcount")
    def testCastFromMDerived3ToBases(self):
        '''MDerived3 is casted by C++ to its parents and the binding must return the MDerived3 wrapper.'''
        a = MDerived3()
        refcnt = sys.getrefcount(a)
        md1 = a.castToMDerived1()
        md2 = a.castToMDerived2()
        b1 = a.castToBase1()
        b2 = a.castToBase2()
        b3 = a.castToBase3()
        b4 = a.castToBase4()
        b5 = a.castToBase5()
        b6 = a.castToBase6()
        self.assertTrue(isinstance(md1, MDerived3))
        self.assertTrue(isinstance(md2, MDerived3))
        self.assertTrue(isinstance(b1, MDerived3))
        self.assertTrue(isinstance(b2, MDerived3))
        self.assertTrue(isinstance(b3, MDerived3))
        self.assertTrue(isinstance(b4, MDerived3))
        self.assertTrue(isinstance(b5, MDerived3))
        self.assertTrue(isinstance(b6, MDerived3))
        self.assertEqual(a, md1)
        self.assertEqual(a, md2)
        self.assertEqual(a, b1)
        self.assertEqual(a, b2)
        self.assertEqual(a, b3)
        self.assertEqual(a, b4)
        self.assertEqual(a, b5)
        self.assertEqual(a, b6)
        self.assertEqual(sys.getrefcount(a), refcnt + 8)

    @unittest.skipUnless(hasattr(sys, "getrefcount"), f"{sys.implementation.name} has no refcount")
    def testCastFromMDerived4ToBases(self):
        '''MDerived4 is casted by C++ to its parents and the binding must return the MDerived4 wrapper.'''
        a = MDerived4()
        refcnt = sys.getrefcount(a)
        b3 = a.castToBase3()
        b4 = a.castToBase4()
        self.assertTrue(isinstance(b3, MDerived4))
        self.assertTrue(isinstance(b4, MDerived4))
        self.assertEqual(a, b3)
        self.assertEqual(a, b4)
        self.assertEqual(sys.getrefcount(a), refcnt + 2)

    @unittest.skipUnless(hasattr(sys, "getrefcount"), f"{sys.implementation.name} has no refcount")
    def testCastFromMDerived5ToBases(self):
        '''MDerived5 is casted by C++ to its parents and the binding must return the MDerived5 wrapper.'''
        a = MDerived5()
        refcnt = sys.getrefcount(a)
        b3 = a.castToBase3()
        b4 = a.castToBase4()
        self.assertTrue(isinstance(b3, MDerived5))
        self.assertTrue(isinstance(b4, MDerived5))
        self.assertEqual(a, b3)
        self.assertEqual(a, b4)
        self.assertEqual(sys.getrefcount(a), refcnt + 2)

    @unittest.skipUnless(hasattr(sys, "getrefcount"), f"{sys.implementation.name} has no refcount")
    def testCastFromMDerived3ToBase3(self):
        '''MDerived3 is casted by C++ to Base3 grandparent using both the inherited and reimplement castToBase3 methods.'''
        a = MDerived3()
        refcnt = sys.getrefcount(a)
        b3_reimplemented = a.castToBase3()
        b3_inherited = MDerived2.castToBase3(a)
        self.assertTrue(isinstance(b3_reimplemented, MDerived3))
        self.assertTrue(isinstance(b3_inherited, MDerived3))
        self.assertEqual(a, b3_reimplemented)
        self.assertEqual(a, b3_inherited)
        self.assertEqual(sys.getrefcount(a), refcnt + 2)

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