1
|
|
|
"""Testing class inheritance attributes changes.""" |
2
|
|
|
import unittest |
3
|
|
|
|
4
|
|
|
from pyof.foundation.base import GenericStruct |
5
|
|
|
from pyof.foundation.basic_types import UBInt8, UBInt16, UBInt32, UBInt64 |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class TestInheritance(unittest.TestCase): |
9
|
|
|
"""Testing GenericStruct class inheritance.""" |
10
|
|
|
|
11
|
|
|
def setUp(self): |
12
|
|
|
"""Basic Test Setup.""" |
13
|
|
|
class MyClassA(GenericStruct): |
14
|
|
|
"""Example class.""" |
15
|
|
|
|
16
|
|
|
a1 = UBInt8(1) |
17
|
|
|
a2 = UBInt16(2) |
18
|
|
|
a3 = UBInt8(3) |
19
|
|
|
a4 = UBInt16(4) |
20
|
|
|
a5 = UBInt32(5) |
21
|
|
|
|
22
|
|
|
class MyClassB(MyClassA): |
23
|
|
|
"""Example class.""" |
24
|
|
|
|
25
|
|
|
a0 = UBInt32(0) |
26
|
|
|
a2 = UBInt64(2) |
27
|
|
|
b6 = UBInt8(6) |
28
|
|
|
|
29
|
|
|
_removed_attributes = ['a3'] |
30
|
|
|
# _rename_attributes = [('a4', 'b4')] |
31
|
|
|
# _insert_attributes_before = {'a0': 'a1'} |
32
|
|
|
|
33
|
|
|
self.MyClassA = MyClassA |
34
|
|
|
self.MyClassB = MyClassB |
35
|
|
|
self.a_expected_names = ['a1', 'a2', 'a3', 'a4', 'a5'] |
36
|
|
|
self.b_expected_names = ['a1', 'a2', 'a4', 'a5', 'a0', 'b6'] |
37
|
|
|
# self.b_expected_names = ['a0', 'a1', 'a2', 'b4', 'a5', 'b6'] |
38
|
|
|
|
39
|
|
|
def test_modifications(self): |
40
|
|
|
"""[Foundation/Base/GenericStruct] - Attributes Modifications.""" |
41
|
|
|
m1 = self.MyClassA() |
42
|
|
|
m2 = self.MyClassB() |
43
|
|
|
# Checking keys (attributes names) and its ordering |
44
|
|
|
self.assertEqual([attr[0] for attr in m1.get_class_attributes()], |
45
|
|
|
self.a_expected_names) |
46
|
|
|
self.assertEqual([attr[0] for attr in m2.get_class_attributes()], |
47
|
|
|
self.b_expected_names) |
48
|
|
|
|
49
|
|
|
# Check if there is no shared attribute between instances |
50
|
|
|
self.assertIsNot(m1, m2) |
51
|
|
|
self.assertIsNot(m1.a1, m2.a1) |
52
|
|
|
self.assertIsNot(m1.a2, m2.a2) |
53
|
|
|
self.assertIsNot(m1.a3, m2.a4) |
54
|
|
|
self.assertIsNot(m1.a4, m2.a4) |
55
|
|
|
self.assertIsNot(m1.a5, m2.a5) |
56
|
|
|
|
57
|
|
|
# Check attributes types on MyClassA |
58
|
|
|
self.assertIsInstance(self.MyClassA.a1, UBInt8) |
59
|
|
|
self.assertIsInstance(self.MyClassA.a2, UBInt16) |
60
|
|
|
self.assertIsInstance(self.MyClassA.a3, UBInt8) |
61
|
|
|
self.assertIsInstance(self.MyClassA.a4, UBInt16) |
62
|
|
|
self.assertIsInstance(self.MyClassA.a5, UBInt32) |
63
|
|
|
|
64
|
|
|
# Check attributes types on MyClassA |
65
|
|
|
self.assertIsInstance(self.MyClassB.a1, UBInt8) |
66
|
|
|
self.assertIsInstance(self.MyClassB.a2, UBInt64) |
67
|
|
|
self.assertIsInstance(self.MyClassB.a4, UBInt16) |
68
|
|
|
self.assertIsInstance(self.MyClassB.a5, UBInt32) |
69
|
|
|
self.assertIsInstance(self.MyClassB.a0, UBInt32) |
70
|
|
|
self.assertIsInstance(self.MyClassB.b6, UBInt8) |
71
|
|
|
|