aboutsummaryrefslogtreecommitdiffstats
path: root/tests/foo_test.py
blob: 45b7e80e7154ae890c632bf129f8c84c86e85d34 (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

'''Test cases for virtual methods called through generated bindings'''

import unittest
try:
    from foo import Foo, Bar
except:
    import sys
    print 'You need to set correct paths for libfoo and foo bindings'
    import os
    sys.exit(1)


class DerivedFoo(Foo):

    def __init__(self):
        Foo.__init__(self)

    def pureVirtual(self):
        print 'DerivedFoo.pureVirtual'


class VirtualMethods(unittest.TestCase):
    '''Test case for virtual methods'''

    def setUp(self):
        self.foo = Foo()
        self.bar = Bar()
        self.derivedfoo = DerivedFoo()

    def tearDown(self):
        self.foo = None
        self.bar = None
        self.derivedfoo = None

    def testDerivedClassVirtualMethod(self):
        '''Test reinplemented virtual methods from derived class'''
        called = True
        try:
            self.bar.unpureVirtual()
            self.bar.pureVirtual()
        except:
            called = False
        self.assertTrue(called)

    def testBaseClassVirtualMethod(self):
        '''Test virtual method from base class'''
        called = True
        try:
            self.foo.unpureVirtual()
        except:
            called = False
        self.assertTrue(called)


    def testBaseClassPureVirtualMethod(self):
        '''Test pure virtual method from base class'''
        called = False
        try:
            self.foo.pureVirtual()
        except:
            called = False
        self.assertFalse(called)

    def testBaseClassIndirectCallToUnpureVirtualMethod(self):
        '''Test call to unpure virtual method from C++ to Python'''
        called = True
        try:
            self.foo.unpureVirtual()
        except:
            called = False
        self.assertTrue(called)

    def testDerivedClassIndirectCallToUnpureVirtualMethod(self):
        '''Test call to unpure virtual method from C++ to Python'''
        called = True
        try:
            self.bar.unpureVirtual()
        except:
            called = False
        self.assertTrue(called)

    def testCppDerivedClassIndirectCallToPureVirtualMethod(self):
        '''Test call to pure virtual method from C++ to Python'''
        called = False
        try:
            self.bar.callPureVirtual()
        except:
            called = False
        self.assertFalse(called)


    def testDerivedClassIndirectCallToPureVirtualMethod(self):
        '''Test call to pure virtual method from C++ to Python'''
        called = False
        try:
            self.derivedfoo.callPureVirtual()
        except:
            called = False
        self.assertFalse(called)


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