/
testsemanal.py
209 lines (176 loc) · 7.39 KB
/
testsemanal.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
"""Semantic analyzer test cases"""
import os.path
from typing import Dict, List
from mypy import build
from mypy.modulefinder import BuildSource
from mypy.defaults import PYTHON3_VERSION
from mypy.test.helpers import (
assert_string_arrays_equal, normalize_error_messages, testfile_pyversion, parse_options
)
from mypy.test.data import DataDrivenTestCase, DataSuite
from mypy.test.config import test_temp_dir
from mypy.errors import CompileError
from mypy.nodes import TypeInfo
from mypy.options import Options
# Semantic analyzer test cases: dump parse tree
# Semantic analysis test case description files.
semanal_files = [
'semanal-basic.test',
'semanal-expressions.test',
'semanal-classes.test',
'semanal-types.test',
'semanal-typealiases.test',
'semanal-modules.test',
'semanal-statements.test',
'semanal-abstractclasses.test',
'semanal-namedtuple.test',
'semanal-typeddict.test',
'semenal-literal.test',
'semanal-classvar.test',
'semanal-python2.test',
'semanal-lambda.test',
]
def get_semanal_options(program_text: str, testcase: DataDrivenTestCase) -> Options:
options = parse_options(program_text, testcase, 1)
options.use_builtins_fixtures = True
options.semantic_analysis_only = True
options.show_traceback = True
options.python_version = PYTHON3_VERSION
return options
class SemAnalSuite(DataSuite):
files = semanal_files
native_sep = True
def run_case(self, testcase: DataDrivenTestCase) -> None:
test_semanal(testcase)
def test_semanal(testcase: DataDrivenTestCase) -> None:
"""Perform a semantic analysis test case.
The testcase argument contains a description of the test case
(inputs and output).
"""
try:
src = '\n'.join(testcase.input)
options = get_semanal_options(src, testcase)
options.python_version = testfile_pyversion(testcase.file)
result = build.build(sources=[BuildSource('main', None, src)],
options=options,
alt_lib_path=test_temp_dir)
a = result.errors
if a:
raise CompileError(a)
# Include string representations of the source files in the actual
# output.
for fnam in sorted(result.files.keys()):
f = result.files[fnam]
# Omit the builtins module and files with a special marker in the
# path.
# TODO the test is not reliable
if (not f.path.endswith((os.sep + 'builtins.pyi',
'typing.pyi',
'mypy_extensions.pyi',
'typing_extensions.pyi',
'abc.pyi',
'collections.pyi',
'sys.pyi'))
and not os.path.basename(f.path).startswith('_')
and not os.path.splitext(
os.path.basename(f.path))[0].endswith('_')):
a += str(f).split('\n')
except CompileError as e:
a = e.messages
if testcase.normalize_output:
a = normalize_error_messages(a)
assert_string_arrays_equal(
testcase.output, a,
'Invalid semantic analyzer output ({}, line {})'.format(testcase.file,
testcase.line))
# Semantic analyzer error test cases
class SemAnalErrorSuite(DataSuite):
files = ['semanal-errors.test']
def run_case(self, testcase: DataDrivenTestCase) -> None:
test_semanal_error(testcase)
def test_semanal_error(testcase: DataDrivenTestCase) -> None:
"""Perform a test case."""
try:
src = '\n'.join(testcase.input)
res = build.build(sources=[BuildSource('main', None, src)],
options=get_semanal_options(src, testcase),
alt_lib_path=test_temp_dir)
a = res.errors
assert a, 'No errors reported in {}, line {}'.format(testcase.file, testcase.line)
except CompileError as e:
# Verify that there was a compile error and that the error messages
# are equivalent.
a = e.messages
if testcase.normalize_output:
a = normalize_error_messages(a)
assert_string_arrays_equal(
testcase.output, a,
'Invalid compiler output ({}, line {})'.format(testcase.file, testcase.line))
# SymbolNode table export test cases
class SemAnalSymtableSuite(DataSuite):
required_out_section = True
files = ['semanal-symtable.test']
def run_case(self, testcase: DataDrivenTestCase) -> None:
"""Perform a test case."""
try:
# Build test case input.
src = '\n'.join(testcase.input)
result = build.build(sources=[BuildSource('main', None, src)],
options=get_semanal_options(src, testcase),
alt_lib_path=test_temp_dir)
# The output is the symbol table converted into a string.
a = result.errors
if a:
raise CompileError(a)
for f in sorted(result.files.keys()):
if f not in ('builtins', 'typing', 'abc'):
a.append('{}:'.format(f))
for s in str(result.files[f].names).split('\n'):
a.append(' ' + s)
except CompileError as e:
a = e.messages
assert_string_arrays_equal(
testcase.output, a,
'Invalid semantic analyzer output ({}, line {})'.format(
testcase.file, testcase.line))
# Type info export test cases
class SemAnalTypeInfoSuite(DataSuite):
required_out_section = True
files = ['semanal-typeinfo.test']
def run_case(self, testcase: DataDrivenTestCase) -> None:
"""Perform a test case."""
try:
# Build test case input.
src = '\n'.join(testcase.input)
result = build.build(sources=[BuildSource('main', None, src)],
options=get_semanal_options(src, testcase),
alt_lib_path=test_temp_dir)
a = result.errors
if a:
raise CompileError(a)
# Collect all TypeInfos in top-level modules.
typeinfos = TypeInfoMap()
for f in result.files.values():
for n in f.names.values():
if isinstance(n.node, TypeInfo):
assert n.fullname is not None
typeinfos[n.fullname] = n.node
# The output is the symbol table converted into a string.
a = str(typeinfos).split('\n')
except CompileError as e:
a = e.messages
assert_string_arrays_equal(
testcase.output, a,
'Invalid semantic analyzer output ({}, line {})'.format(
testcase.file, testcase.line))
class TypeInfoMap(Dict[str, TypeInfo]):
def __str__(self) -> str:
a: List[str] = ["TypeInfoMap("]
for x, y in sorted(self.items()):
if isinstance(x, str) and (not x.startswith('builtins.') and
not x.startswith('typing.') and
not x.startswith('abc.')):
ti = ('\n' + ' ').join(str(y).split('\n'))
a.append(' {} : {}'.format(x, ti))
a[-1] += ')'
return '\n'.join(a)