forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testoutput.py
90 lines (73 loc) · 2.85 KB
/
testoutput.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
"""Tests for parse tree pretty printing that preserves formatting
Test case descriptions are in file test/data/output.test."""
import os.path
import re
import sys
from myunit import Suite, run_test
from testhelpers import assert_string_arrays_equal
from testdata import parse_test_cases
from testconfig import test_data_prefix, test_temp_dir
from build import build
from parse import parse
from output import OutputVisitor
from errors import CompileError
# Files which contain test case descriptions.
output_files = ['output.test']
class OutputSuite(Suite):
def cases(self):
c = []
for f in output_files:
c += parse_test_cases(os.path.join(test_data_prefix, f),
test_output, test_temp_dir, True)
return c
def test_output(testcase):
"""Perform an identity source code transformation test case."""
expected = testcase.output
if expected == []:
expected = testcase.input
try:
src = '\n'.join(testcase.input)
# Parse and analyze the source program.
# Parse and semantically analyze the source program.
any trees, any symtable, any infos, any types
# Test case names with a special suffix get semantically analyzed. This
# lets us test that semantic analysis does not break source code pretty
# printing.
if testcase.name.endswith('_SemanticAnalyzer'):
trees, symtable, infos, types = build(src, 'main', True,
test_temp_dir)
else:
trees = [parse(src, 'main')]
a = []
first = True
# Produce an output containing the pretty-printed forms (with original
# formatting) of all the relevant source files.
for t in trees:
# Omit the builtins and files marked for omission.
if (not t.path.endswith(os.sep + 'builtins.py') and
'-skip.' not in t.path):
# Add file name + colon for files other than the first.
if not first:
a.append('{}:'.format(fix_path(remove_prefix(
t.path, test_temp_dir))))
v = OutputVisitor()
t.accept(v)
s = v.output()
if s != '':
a += s.split('\n')
first = False
except CompileError as e:
a = e.messages
assert_string_arrays_equal(
expected, a, 'Invalid source code output ({}, line {})'.format(
testcase.file, testcase.line))
def remove_prefix(path, prefix):
regexp = '^' + prefix.replace('\\', '\\\\')
np = re.sub(regexp, '', path)
if np.startswith(os.sep):
np = np[1:]
return np
def fix_path(path):
return path.replace('\\', '/')
if __name__ == '__main__':
run_test(OutputSuite(), sys.argv[1:])