aboutsummaryrefslogtreecommitdiffstats
path: root/tests/samplebinding/derived_test.py
blob: 22d49470f74fbbf70187232f4381282cc4fcea08 (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
#!/usr/bin/python

'''Test cases for Derived class'''

import sys
import unittest

import sample
from sample import Abstract, Derived, PolymorphicFuncEnum

class Deviant(Derived):
    def __init__(self):
        Derived.__init__(self)
        self.pure_virtual_called = False
        self.unpure_virtual_called = False

    def pureVirtual(self):
        self.pure_virtual_called = True

    def unpureVirtual(self):
        self.unpure_virtual_called = True

    def className(self):
        return 'Deviant'

class DerivedTest(unittest.TestCase):
    '''Test case for Derived class'''

    def testParentClassMethodsAvailability(self):
        '''Test if Derived class really inherits its methods from parent.'''
        inherited_methods = set(['callPureVirtual', 'callUnpureVirtual',
                                 'id_', 'pureVirtual', 'unpureVirtual'])
        self.assert_(inherited_methods.issubset(dir(Derived)))

    def testPolymorphicMethodCall(self):
        '''Test if the correct polymorphic method is being called.'''
        derived = Derived()

        result = derived.polymorphic(1, 2)
        self.assertEqual(type(result), PolymorphicFuncEnum)
        self.assertEqual(result, sample.PolymorphicFunc_ii)

        result = derived.polymorphic(3)
        self.assertEqual(type(result), PolymorphicFuncEnum)
        self.assertEqual(result, sample.PolymorphicFunc_ii)

        result = derived.polymorphic(4.4)
        self.assertEqual(type(result), PolymorphicFuncEnum)
        self.assertEqual(result, sample.PolymorphicFunc_d)

    def testOtherPolymorphicMethodCall(self):
        '''Another test to check polymorphic method calling, just to double check.'''
        derived = Derived()

        result = derived.otherPolymorphic(1, 2, True, 3.3)
        self.assertEqual(type(result), Derived.OtherPolymorphicFuncEnum)
        self.assertEqual(result, sample.Derived.OtherPolymorphicFunc_iibd)

        result = derived.otherPolymorphic(1, 2.2)
        self.assertEqual(type(result), Derived.OtherPolymorphicFuncEnum)
        self.assertEqual(result, Derived.OtherPolymorphicFunc_id)

    def testPolymorphicMethodCallWithDifferentNumericTypes(self):
        '''Test if the correct polymorphic method accepts a different numeric type as argument.'''
        derived = Derived()
        result = derived.polymorphic(1.1, 2.2)
        self.assertEqual(type(result), PolymorphicFuncEnum)
        self.assertEqual(result, sample.PolymorphicFunc_ii)

    def testPolymorphicMethodCallWithWrongNumberOfArguments(self):
        '''Test if a call to a polymorphic method with the wrong number of arguments raises an exception.'''
        derived = Derived()
        self.assertRaises(TypeError, lambda : derived.otherPolymorphic(1, 2, True))

    def testReimplementedPureVirtualMethodCall(self):
        '''Test if a Python override of a implemented pure virtual method is correctly called from C++.'''
        d = Deviant()
        d.callPureVirtual()
        self.assert_(d.pure_virtual_called)

    def testReimplementedVirtualMethodCall(self):
        '''Test if a Python override of a reimplemented virtual method is correctly called from C++.'''
        d = Deviant()
        d.callUnpureVirtual()
        self.assert_(d.unpure_virtual_called)

    def testVirtualMethodCallString(self):
        '''Test virtual method call returning string.'''
        d = Derived()
        self.assertEqual(d.className(), 'Derived')
        self.assertEqual(d.getClassName(), 'Derived')

    def testReimplementedVirtualMethodCallReturningString(self):
        '''Test if a Python override of a reimplemented virtual method is correctly called from C++.'''
        d = Deviant()
        self.assertEqual(d.className(), 'Deviant')
        self.assertEqual(d.getClassName(), 'Deviant')

    def testSingleArgument(self):
        '''Test singleArgument call.'''
        d = Derived()
        self.assert_(d.singleArgument(False))
        self.assert_(not d.singleArgument(True))

    def testMethodCallWithDefaultValue(self):
        '''Test method call with default value.'''
        d = Derived()
        self.assertEqual(d.defaultValue(3), 3.1)
        self.assertEqual(d.defaultValue(), 0.1)

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