-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconftest.py
More file actions
193 lines (161 loc) · 5.86 KB
/
Copy pathconftest.py
File metadata and controls
193 lines (161 loc) · 5.86 KB
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
"""pytest config file"""
import collections
import itertools
import all_language_data
import pytest
import source_code_examples
_ALL_TEST_DATA = all_language_data.get_all_test_data()
def pytest_addoption(parser):
"""specifies command line options"""
parser.addoption(
"--iteration_size",
action="store",
type=int,
default=2,
help="Maximal number of iterations in test_string_to_code_iteration",
)
parser.addoption(
"--composition_chain_size",
action="store",
type=int,
default=2,
help="Size of the composition_chain in test_string_to_code_composition",
)
parser.addoption(
"--skip",
action="append",
default=[],
choices=all_language_data.get_all_ids(),
help="list of languages to skip",
)
parser.addoption(
"--select",
action="append",
default=[],
choices=all_language_data.get_all_ids(),
help="list of languages to run",
)
_SKIPPED_BY_CMD_ARGS = "Skipped by command line arguments"
def get_tool_list(languages_to_run):
"""returns data for the parametrized fixture 'tool_name'"""
res = []
for cur_langauge_data in _ALL_TEST_DATA:
cur_language = cur_langauge_data.id
is_skipped = cur_language not in languages_to_run
for cur_tool in cur_langauge_data.tool_names:
res.append(
pytest.param(
cur_tool,
id=f"{cur_language}-{cur_tool}",
marks=pytest.mark.skipif(is_skipped, reason=_SKIPPED_BY_CMD_ARGS),
)
)
return res
def check_format(in_code):
"""Checks the format of the code."""
code_lines = in_code.split("\n")
assert not code_lines[-1], "Code must end with empty line."
if len(code_lines) > 1:
assert code_lines[-2], "Code must end with exactly single empty line."
for _ in code_lines:
if _:
assert _[-1] not in {" ", "\t"}, "No trailing whitespaces."
FunctionPair = collections.namedtuple("FunctionPair", ["string_to_code", "run_code"])
def extract_function_pair(in_language):
"""
returns FunctionPair for given in_language.
The function 'string_to_code' is decorated check_format.
"""
def _add_check_format(in_string_to_code_fun):
def _inner(in_str, **kwargs):
res_code = in_string_to_code_fun(in_str, **kwargs)
check_format(res_code)
return res_code
return _inner
return FunctionPair(
_add_check_format(in_language.string_to_code), in_language.run_code
)
def get_function_pair_list(languages_to_run):
"""returns data for the parametrized fixture 'function_pair'"""
def proc_single(in_language):
return pytest.param(
extract_function_pair(in_language),
id=in_language.id,
marks=pytest.mark.skipif(
in_language.id not in languages_to_run,
reason=_SKIPPED_BY_CMD_ARGS,
),
)
return [proc_single(_) for _ in _ALL_TEST_DATA]
def get_composition_chain_list(languages_to_run, in_chain_size):
"""returns data for the parametrized fixture 'composition_chain'"""
def proc_single(in_chain):
id_list = [_.id for _ in in_chain]
return pytest.param(
[extract_function_pair(_) for _ in in_chain],
id="-".join(id_list),
marks=pytest.mark.skipif(
any(_ not in languages_to_run for _ in id_list),
reason=_SKIPPED_BY_CMD_ARGS,
),
)
return [
proc_single(_) for _ in itertools.product(_ALL_TEST_DATA, repeat=in_chain_size)
]
def _get_source_code_list(in_examples, languages_to_run):
souce_code = collections.namedtuple(
"souce_code", ["example_id", "language_id", "expected_output"]
)
res = []
for _ in in_examples:
for cur_lang in _.languages:
cur_source_code = souce_code(_.id, cur_lang, _.output)
res.append(
pytest.param(
cur_source_code,
id=f"{_.id}-{cur_lang}",
marks=pytest.mark.skipif(
cur_lang not in languages_to_run,
reason=_SKIPPED_BY_CMD_ARGS,
),
)
)
return res
def pytest_generate_tests(metafunc):
"""generates all test data"""
specified_to_skip = set(metafunc.config.getoption("skip"))
specified_to_run = set(metafunc.config.getoption("select"))
if not specified_to_run:
languages_to_run = set(all_language_data.get_all_ids()) - specified_to_skip
else:
assert not specified_to_skip, "Do not mix --skip and --select."
languages_to_run = specified_to_run
if "tool_name" in metafunc.fixturenames:
metafunc.parametrize("tool_name", get_tool_list(languages_to_run))
if "function_pair" in metafunc.fixturenames:
metafunc.parametrize("function_pair", get_function_pair_list(languages_to_run))
if "composition_chain" in metafunc.fixturenames:
metafunc.parametrize(
"composition_chain",
get_composition_chain_list(
languages_to_run,
metafunc.config.getoption("composition_chain_size"),
),
)
if "source_code" in metafunc.fixturenames:
metafunc.parametrize(
"source_code",
_get_source_code_list(
source_code_examples.get_full_examples(),
languages_to_run,
),
)
if "target_language_id" in metafunc.fixturenames:
metafunc.parametrize(
"target_language_id",
all_language_data.get_all_ids(),
)
@pytest.fixture
def iteration_size(request):
"""fixture returning the 'iteration_size'"""
return request.config.getoption("--iteration_size")