-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
123 lines (91 loc) · 2.9 KB
/
test.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
""" Basic functionality tests for selfless classes. """
import io
import sys
import timeit
from selfless import Selfless
class Writer(Selfless):
# All instance vars must be declared in order to work inside the class.
# Bare annotations such as '_strings: list' with no value do not work.
# Anything assigned in __init__ can just default to None here.
_strings = None
_file = None
delim = ''
def __init__(file=None):
""" Mutable objects such as lists must still be initialized
in __init__ even if declared above. The string <delim>
is immutable, so it can be left alone at the default. """
_strings = []
_file = file or sys.stdout
def __len__():
""" Special method test. """
return len(_strings)
def add(*args):
""" Augmented assignment test. """
_strings += args
def set_delim(delim):
""" Explicit self is still needed in the case of argument shadowing. """
self.delim = delim
def _write(s):
_file.write(s)
def write_all():
""" Implicit and explicit self should work the same. """
for s in _strings:
self._write(delim)
_write(s)
class WriterRight(Writer):
_foo = 'RIGHT' # gets shadowed
def write_all():
""" Super call goes to immediate parent. """
_write('right')
super.write_all()
class WriterLeft(Writer):
_foo = 'LEFT' # is exposed
def write_all():
""" Super call goes to *sibling* when combined in child class. """
_write('left')
super.write_all()
class WriterExtended(WriterLeft, WriterRight):
_header = None # New instance vars may be added in subclasses.
def __init__(header, **kwargs):
""" Super call with kwargs, skips 2 MRO levels. """
super.__init__(**kwargs)
_header = header
def write_all(with_header=True):
""" Test the full super call chain. """
if with_header:
_write(_header)
_write(_foo)
super.write_all()
_write('END')
def test():
buf = io.StringIO()
x = WriterExtended('START', file=buf)
x.add()
x.add('1')
x.add('2', '3')
Writer.add(x, '4')
string_count = len(x)
x.add(f'L{string_count}')
x.set_delim('/')
x.write_all()
return buf.getvalue()
def test_output():
actual = test()
expected = 'STARTLEFTleftright/1/2/3/4/L4END'
print('Need: ' + expected)
print('Got: ' + actual)
return int(actual != expected)
def test_bench(n, r=1):
if r > 1:
t = min(timeit.repeat(test, number=n//r, repeat=r)) * r
else:
t = timeit.timeit(test, number=n)
print(f'Extrapolated best time for {n} runs: {t:.3f} seconds.')
return 0
if __name__ == '__main__':
args = sys.argv[1:]
if args:
code = test_bench(*map(int, args))
else:
code = test_output()
sys.exit(code)