forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myunit.py
292 lines (236 loc) · 7.78 KB
/
myunit.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import sys
import re
import time
import traceback
# TODO remove global state
is_verbose = False
is_quiet = False
patterns = <str> []
times = <tuple<float, str>> []
class AssertionFailure(Exception):
void __init__(self, str s=None):
if s:
super().__init__(s)
else:
super().__init__()
class SkipTestCaseException(Exception): pass
void assert_true(bool b, str msg=None):
"""Exception used to signal skipped test cases."""
if not b:
raise AssertionFailure(msg)
void assert_equal(object a, object b, str fmt='{} != {}'):
if a != b:
raise AssertionFailure(fmt.format(repr(a), repr(b)))
void assert_not_equal(object a, object b, str fmt='{} == {}'):
if a == b:
raise AssertionFailure(fmt.format(repr(a), repr(b)))
void assert_raises(type typ, any *rest):
"""Usage: assert_raises(exception class[, message], function[, args])
Call function with the given arguments and expect an exception of the given
type.
TODO use overloads for better type checking
"""
# Parse arguments.
str msg = None
if isinstance(rest[0], str) or rest[0] is None:
msg = rest[0]
rest = rest[1:]
f = rest[0]
args = <any> []
if len(rest) > 1:
args = rest[1]
assert len(rest) <= 2
# Perform call and verify the exception.
try:
f(*args)
except Exception as e:
assert_type(typ, e)
if msg:
assert_equal(e.args[0], msg, 'Invalid message {}, expected {}')
else:
raise AssertionFailure('No exception raised')
void assert_type(type typ, object value):
if type(value) != typ:
raise AssertionFailure('Invalid type {}, expected {}'.format(
typename(type(value)), typename(typ)))
void fail():
raise AssertionFailure()
class TestCase:
void __init__(self, str name, Suite suite=None, func<void()> func=None):
self.func = func
self.name = name
self.suite = suite
void run(self):
if self.func:
self.func()
void set_up(self):
if self.suite:
self.suite.set_up()
void tear_down(self):
if self.suite:
self.suite.tear_down()
class Suite:
void __init__(self):
self.prefix = typename(type(self)) + '.'
# Each test case is either a TestCase object or (str, function).
self._test_cases = <any> []
self.init()
void set_up(self):
pass
void tear_down(self):
pass
void init(self):
for m in dir(self):
if m.startswith('test'):
t = getattr(self, m)
if isinstance(t, Suite):
self.add_test((m + '.', t))
else:
self.add_test(TestCase(m, self, getattr(self, m)))
void add_test(self, TestCase test):
self._test_cases.append(test)
void add_test(self, tuple<str, func<void()>> test):
self._test_cases.append(test)
any[] cases(self):
return self._test_cases[:]
void skip(self):
raise SkipTestCaseException()
void run_test(Suite t, str[] args=None):
global patterns, is_verbose, is_quiet
if not args:
args = []
is_verbose = False
is_quiet = False
patterns = []
i = 0
while i < len(args):
a = args[i]
if a == '-v':
is_verbose = True
elif a == '-q':
is_quiet = True
elif len(a) > 0 and a[0] != '-':
patterns.append(a)
else:
raise ValueError('Invalid arguments')
i += 1
if len(patterns) == 0:
patterns.append('*')
num_total, num_fail, num_skip = run_test_recursive(t, 0, 0, 0, '', 0)
skip_msg = ''
if num_skip > 0:
skip_msg = ', {} skipped'.format(num_skip)
if num_fail == 0:
if not is_quiet:
print('%d test cases run%s, all passed.' % (num_total, skip_msg))
print('*** OK ***')
else:
sys.stderr.write('%d/%d test cases failed%s.\n' % (num_fail,
num_total,
skip_msg))
sys.stderr.write('*** FAILURE ***\n')
tuple<int, int, int> run_test_recursive(any test, int num_total, int num_fail,
int num_skip, str prefix, int depth):
"""The first argument may be TestCase, Suite or (str, Suite)."""
if isinstance(test, TestCase):
name = prefix + test.name
for pattern in patterns:
if match_pattern(name, pattern):
match = True
break
else:
match = False
if match:
is_fail, is_skip = run_single_test(name, test)
if is_fail: num_fail += 1
if is_skip: num_skip += 1
num_total += 1
else:
Suite suite
str suite_prefix
if isinstance(test, list) or isinstance(test, tuple):
suite = test[1]
suite_prefix = test[0]
else:
suite = test
suite_prefix = test.prefix
for stest in suite.cases():
new_prefix = prefix
if depth > 0:
new_prefix = prefix + suite_prefix
num_total, num_fail, num_skip = run_test_recursive(
stest, num_total, num_fail, num_skip, new_prefix, depth + 1)
return num_total, num_fail, num_skip
tuple<bool, bool> run_single_test(str name, any test):
if is_verbose:
sys.stderr.write(name)
time0 = time.time()
test.set_up() # FIX: check exceptions
try:
test.run()
except Exception:
exc_type, exc_value, exc_traceback = sys.exc_info()
else:
exc_traceback = None
test.tear_down() # FIX: check exceptions
times.append((time.time() - time0, name))
if exc_traceback:
if isinstance(exc_value, SkipTestCaseException):
if is_verbose:
sys.stderr.write(' (skipped)\n')
return False, True
else:
handle_failure(name, exc_type, exc_value, exc_traceback)
return True, False
elif is_verbose:
sys.stderr.write('\n')
return False, False
void handle_failure(name, exc_type, exc_value, exc_traceback):
# Report failed test case.
if is_verbose:
sys.stderr.write('\n\n')
str msg
if exc_value.args and exc_value.args[0]:
msg = ': ' + exc_value.args[0]
else:
msg = ''
sys.stderr.write('Traceback (most recent call last):\n')
tb = traceback.format_tb(exc_traceback)
tb = clean_traceback(tb)
for s in tb:
sys.stderr.write(s)
exception = typename(exc_type)
sys.stderr.write('{}{}\n\n'.format(exception, msg))
sys.stderr.write('{} failed\n\n'.format(name))
str typename(type t):
if '.' in str(t):
return str(t).split('.')[-1].rstrip("'>")
else:
return str(t)[8:-2]
bool match_pattern(str s, str p):
if len(p) == 0:
return len(s) == 0
elif p[0] == '*':
if len(p) == 1:
return True
else:
for i in range(len(s) + 1):
if match_pattern(s[i:], p[1:]):
return True
return False
elif len(s) == 0:
return False
else:
return s[0] == p[0] and match_pattern(s[1:], p[1:])
str[] clean_traceback(str[] tb):
# Remove clutter from the traceback.
start = 0
for i, s in enumerate(tb):
if '\n test.run()\n' in s or '\n self.func()\n' in s:
start = i + 1
tb = tb[start:]
for f in ['assert_equal', 'assert_not_equal', 'assert_type',
'assert_raises', 'assert_true']:
if tb != [] and ', in {}\n'.format(f) in tb[-1]:
tb = tb[:-1]
return tb