forked from csvoss/onelinerizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtests.py
64 lines (55 loc) · 1.79 KB
/
runtests.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
import unittest
import os
import sys
from StringIO import StringIO
from main import to_one_line
TEST_DIRECTORY = 'tests'
class TestOneLine(unittest.TestCase):
def runTest(self):
pass
def make_test(filename):
"""Return a function that verifies that the file and its onelined version
both output the same."""
def new_test(self):
with open(filename, 'r') as fi:
self.longMessage = True
original = fi.read().strip()
onelined = to_one_line(original)
self.assertEqual(capture_exec(original),
capture_exec(onelined),
msg="Onelined: "+onelined)
return new_test
class FakeStdin(object):
"""Sometimes tests use raw_input; this feeds those deterministically."""
def __init__(self):
self.counter = 0
def readline(self):
self.counter += 1
return str(self.counter)
def capture_exec(code_string):
"""Run the code with FakeStdin as stdin, return its stdout."""
## TODO: Seed the RNG.
new_stdout = StringIO()
old_stdout = sys.stdout
old_stdin = sys.stdin
sys.stdout = new_stdout
sys.stdin = FakeStdin()
namespace = {}
try:
exec code_string in namespace
except Exception as e:
import traceback
exc = traceback.format_exc()
raise type(e)(code_string + '\n\n' + exc)
sys.stdout = old_stdout
sys.stdin = old_stdin
return new_stdout.getvalue()
## Monkey-patch
for subdir, dirs, files in os.walk(TEST_DIRECTORY):
for filename in files:
if not 'unimplemented' in subdir:
setattr(TestOneLine,
'test_%s' % filename.split('.')[0],
make_test(os.path.join(subdir, filename)))
if __name__ == '__main__':
unittest.main()