1 | #!/usr/bin/env python3
|
2 | """
|
3 | pass_state_test.py: Tests for pass_state.py
|
4 | """
|
5 | from __future__ import print_function
|
6 |
|
7 | import unittest
|
8 |
|
9 | import pass_state # module under test
|
10 |
|
11 |
|
12 | class VirtualTest(unittest.TestCase):
|
13 |
|
14 | def testVirtual(self):
|
15 | """
|
16 | Example:
|
17 |
|
18 | class Base(object):
|
19 | def method(self): # we don't know if this is virtual yet
|
20 | pass
|
21 | def x(self):
|
22 | pass
|
23 |
|
24 | class Derived(Base):
|
25 | def method(self): # now it's virtual!
|
26 | pass
|
27 | def y(self):
|
28 | pass
|
29 | """
|
30 | v = pass_state.Virtual()
|
31 | v.OnMethod('Base', 'method')
|
32 | v.OnMethod('Base', 'x')
|
33 | v.OnSubclass('Base', 'Derived')
|
34 | v.OnMethod('Derived', 'method')
|
35 | v.OnMethod('Derived', 'y')
|
36 |
|
37 | v.Calculate()
|
38 |
|
39 | print(v.virtuals)
|
40 | self.assertEqual([('Base', 'method'), ('Derived', 'method')],
|
41 | v.virtuals)
|
42 |
|
43 | self.assertEqual(True, v.IsVirtual('Base', 'method'))
|
44 | self.assertEqual(True, v.IsVirtual('Derived', 'method'))
|
45 | self.assertEqual(False, v.IsVirtual('Derived', 'y'))
|
46 |
|
47 | self.assertEqual(False, v.IsVirtual('Klass', 'z'))
|
48 |
|
49 | self.assertEqual(True, v.HasVTable('Base'))
|
50 | self.assertEqual(True, v.HasVTable('Derived'))
|
51 |
|
52 | self.assertEqual(False, v.HasVTable('Klass'))
|
53 |
|
54 | def testNoInit(self):
|
55 | v = pass_state.Virtual()
|
56 | v.OnMethod('Base', '__init__')
|
57 | v.OnSubclass('Base', 'Derived')
|
58 | v.OnMethod('Derived', '__init__')
|
59 | v.Calculate()
|
60 | self.assertEqual(False, v.HasVTable('Base'))
|
61 | self.assertEqual(False, v.HasVTable('Derived'))
|
62 |
|
63 | def testCanReorderFields(self):
|
64 | """
|
65 | class Base(object):
|
66 | def __init__(self):
|
67 | self.s = '' # pointer
|
68 | self.i = 42
|
69 |
|
70 | class Derived(Base):
|
71 | def __init__(self):
|
72 | Base.__init__()
|
73 | self.mylist = [] # type: List[str]
|
74 |
|
75 | Note: we can't reorder these, even though there are no virtual methods.
|
76 | """
|
77 | v = pass_state.Virtual()
|
78 | v.OnSubclass('Base2', 'Derived2')
|
79 | v.Calculate()
|
80 |
|
81 | self.assertEqual(False, v.CanReorderFields('Base2'))
|
82 | self.assertEqual(False, v.CanReorderFields('Derived2'))
|
83 |
|
84 | self.assertEqual(True, v.CanReorderFields('Klass2'))
|
85 |
|
86 |
|
87 | if __name__ == '__main__':
|
88 | unittest.main()
|