-
Notifications
You must be signed in to change notification settings - Fork 0
/
testhelper.py
82 lines (66 loc) · 2.07 KB
/
testhelper.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
from __future__ import print_function, unicode_literals
import atexit
import collections
import readline
import rlcompleter
import textwrap
TESTS = collections.OrderedDict()
def register_test(f):
tname = f.__name__.partition('_')[2]
TESTS[tname] = f
return f
def pprint_list(lst, sep=", ", cnv=unicode, prefix=""):
lst_s = sep.join(cnv(x) for x in lst)
for line in textwrap.wrap(lst_s):
print(prefix, line, sep="")
def pprint_tests(tests):
def print_documented_tests():
for tname, test in tests.items():
if test.__doc__:
summary = test.__doc__.splitlines()[0]
print(" {} - {}".format(tname, summary))
else:
yield tname
undoc = print_documented_tests()
pprint_list(undoc, prefix=" ")
class TestsCompleter(object):
def __init__(self, tests):
self.tests = list(tests.keys())
self.matches = self.tests
def complete(self, text, state):
if state == 0:
self.matches = [tname for tname in self.tests if tname.startswith(text)]
try:
return self.matches[state]
except IndexError:
return None
@classmethod
def setup(cls, histfile, tests):
readline.set_completer_delims("")
readline.set_completer(cls(tests).complete)
readline.parse_and_bind("tab: complete")
try:
readline.read_history_file(histfile)
except IOError:
pass
atexit.register(readline.write_history_file, histfile)
def test_main():
TestsCompleter.setup(".thhist", TESTS)
print("Available tests:")
pprint_tests(TESTS)
test = None
while not test:
try:
tname = raw_input("Your choice: ")
except EOFError:
print()
return
except KeyboardInterrupt:
print()
return
test = TESTS.get(tname, None)
if not test:
print("Invalid test name. Please try again")
print(" Running test {!r} ".format(tname).center(80, "-"))
print()
test()