-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_autoperatio.py
197 lines (172 loc) · 8.02 KB
/
test_autoperatio.py
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
from unittest import TestCase, main
from src.autoperatio import AutOperatio, _methods
class TestAutOperatio(TestCase):
def test_slicing(self):
"""Test slicing lists and tuples."""
for supercls in {list, tuple}:
class SubClass(AutOperatio, supercls):
...
self.assertIsInstance(SubClass()[:], SubClass)
self.assertEqual(SubClass([0])[0], 0)
def test_methods(self):
"""Execute full battery of rigorous subtests."""
for supercls in {list, dict, set, tuple}:
# Values which can become sets, dicts, or lists
val1 = [(1, 2)]
val2 = [(1, 2), (3, 4)]
if supercls is tuple:
val1 = (1, 2)
val2 = (3, 4)
class SubClass(AutOperatio, supercls):
...
for method in _methods.intersection(dir(supercls)):
with self.subTest(method=method, supercls=supercls):
sub_meth = getattr(SubClass, method)
sup_meth = getattr(supercls, method)
try:
self.assertIsInstance(
sub_meth(SubClass(val1), supercls(val2)),
SubClass
)
self.assertEqual(
sub_meth(SubClass(val1), SubClass(val2)),
sup_meth(supercls(val1), supercls(val2))
)
a = SubClass(val1)
self.assertIsNotNone(a)
b = sub_meth(a, a)
self.assertIsNot(a, b)
except TypeError:
self.assertIsInstance(
sub_meth(SubClass(val1), 2),
SubClass
)
self.assertEqual(
sub_meth(val1, 2),
sup_meth(val1, 2)
)
a = SubClass(val1)
self.assertIsNotNone(a)
b = sub_meth(a, 2)
self.assertIsNot(a, b)
class SubSubClass(SubClass):
"""For testing subsubclass behavior and different __new__ and __init__."""
counter = 0
def __new__(cls):
"""No args."""
cls.counter += 1
if supercls is tuple:
return super().__new__(cls, val1)
return super().__new__(cls)
def __init__(self):
"""No args, sets own value."""
self.counter += 1
if supercls is tuple:
return
super().__init__(val1)
with self.subTest(method=method, supercls=supercls, extra='SubSubClass weird init weird new'):
sub_meth = getattr(SubSubClass, method)
sup_meth = getattr(supercls, method)
try:
self.assertIsInstance(
sub_meth(SubSubClass(), supercls(val1)),
SubSubClass
)
self.assertEqual(
sub_meth(SubSubClass(), SubSubClass()),
sup_meth(supercls(val1), supercls(val1))
)
a = SubSubClass()
self.assertIsNotNone(a)
b = sub_meth(a, a)
self.assertIsNot(a, b)
self.assertTrue(SubSubClass.counter in {4, 8})
except TypeError:
self.assertIsInstance(
sub_meth(SubSubClass(), 2),
SubSubClass
)
self.assertEqual(
sub_meth(val1, 2),
sup_meth(val1, 2)
)
a = SubSubClass()
self.assertIsNotNone(a)
b = sub_meth(a, 2)
self.assertIsNot(a, b)
self.assertTrue(SubSubClass.counter in {3, 6})
class Foo:
"""Breaks expected inheritance structure"""
class SubClass(supercls, AutOperatio, Foo, superclass=supercls):
"""For testing superclass keyword argument which fixes inheritance"""
with self.subTest(method=method, supercls=supercls, extra='superclass keyword'):
sub_meth = getattr(SubClass, method)
sup_meth = getattr(supercls, method)
try:
self.assertIsInstance(
sub_meth(SubClass(val1), supercls(val2)),
SubClass
)
self.assertEqual(
sub_meth(SubClass(val1), SubClass(val2)),
sup_meth(supercls(val1), supercls(val2))
)
a = SubClass(val1)
self.assertIsNotNone(a)
b = sub_meth(a, a)
self.assertIsNot(a, b)
except TypeError:
self.assertIsInstance(
sub_meth(SubClass(val1), 2),
SubClass
)
self.assertEqual(
sub_meth(val1, 2),
sup_meth(val1, 2)
)
a = SubClass(val1)
self.assertIsNotNone(a)
b = sub_meth(a, 2)
self.assertIsNot(a, b)
class SubClass(supercls, AutOperatio, super_new=False, super_init=False):
"""Tests super_new and super_init keywords by incrementing counter."""
counter = 0
def __new__(cls, *args):
cls.counter += 1
return super().__new__(cls, *args)
def __init__(self, *args):
self.counter += 1
if supercls is tuple:
return
super().__init__(*args)
with self.subTest(method=method, supercls=supercls, extra='False super keywords'):
sub_meth = getattr(SubClass, method)
sup_meth = getattr(supercls, method)
try:
self.assertIsInstance(
sub_meth(SubClass(val1), supercls(val2)),
SubClass
)
self.assertEqual(
sub_meth(SubClass(val1), SubClass(val2)),
sup_meth(supercls(val1), supercls(val2))
)
a = SubClass(val1)
self.assertIsNotNone(a)
b = sub_meth(a, a)
self.assertIsNot(a, b)
self.assertEqual(SubClass.counter, 7)
except TypeError:
self.assertIsInstance(
sub_meth(SubClass(val1), 2),
SubClass
)
self.assertEqual(
sub_meth(val1, 2),
sup_meth(val1, 2)
)
a = SubClass(val1)
self.assertIsNotNone(a)
b = sub_meth(a, 2)
self.assertIsNot(a, b)
self.assertEqual(SubClass.counter, 6)