-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
83 lines (72 loc) · 2.55 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
from __future__ import with_statement
from popen2 import popen3
# Match-Python Test Harness
# For each source file in tests/, pipes it through the compiler and into
# a python interpreter in a seperate process, and compares standard output
# to an expected result.
#
import difflib, site, os, glob, sys, traceback
ROOTPATH = os.path.abspath(os.path.dirname(__file__))
TESTPATH = os.path.join(ROOTPATH, 'tests')
site.addsitedir(ROOTPATH)
import matchlib
def run_mpython(filename):
with open(filename) as source_file:
(stdout, stdin, stderr) = popen3('python -')
try:
matchlib.compiler.compile_piped(source_file, stdin, filename)
stdin.close()
except Exception, err:
return (False, err)
errors = stderr.read()
if errors:
return (False, errors)
return (True, stdout.read())
def run_tests(tests, out=None):
if out is None:
out = sys.stdout
failures = list()
for source_filename, expected_filename in tests:
(success, details) = run_mpython(source_filename)
if not success:
out.write("E")
failures.append((source_filename, details))
else:
with open(expected_filename) as expected_file:
expected = expected_file.read()
if details != expected:
out.write("F")
failures.append((source_filename, (expected, details)))
else:
out.write(".")
out.flush()
out.write("\n")
if failures:
for filename, details in failures:
if isinstance(details, tuple):
out.write("\nFAILURE: %s\n" % filename)
elif isinstance(details, Exception):
out.write("\nERROR: %s\n" % filename)
traceback.print_exception (details.__class__, details, tb=None, file=out)
else:
out.write("\nERROR: %s\n" % filename)
out.write(details)
return 1
else:
return 0
def main(argv=None):
global TESTPATH
if argv is None:
argv = sys.argv
if len(argv) > 1:
return run_tests([
(os.path.join(TESTPATH,test)+'.mpy', os.path.join(TESTPATH, test)+'.out')
for test in argv[1:]
])
else:
return run_tests([
(source_filename, os.path.splitext(source_filename)[0]+'.out')
for source_filename in glob.glob(os.path.join(TESTPATH, '*.mpy'))
])
if __name__ == '__main__':
sys.exit(main(sys.argv))