AtCoderStudyBooster 0.2__py3-none-any.whl → 0.3.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- atcdr/download.py +251 -240
- atcdr/generate.py +184 -193
- atcdr/login.py +133 -0
- atcdr/logout.py +24 -0
- atcdr/main.py +24 -17
- atcdr/markdown.py +22 -22
- atcdr/open.py +30 -30
- atcdr/submit.py +297 -0
- atcdr/test.py +382 -244
- atcdr/util/execute.py +52 -52
- atcdr/util/filetype.py +81 -67
- atcdr/util/gpt.py +102 -96
- atcdr/util/parse.py +206 -0
- atcdr/util/problem.py +94 -91
- atcdr/util/session.py +140 -0
- atcoderstudybooster-0.3.1.dist-info/METADATA +205 -0
- atcoderstudybooster-0.3.1.dist-info/RECORD +21 -0
- {atcoderstudybooster-0.2.dist-info → atcoderstudybooster-0.3.1.dist-info}/WHEEL +1 -1
- atcdr/util/cost.py +0 -120
- atcoderstudybooster-0.2.dist-info/METADATA +0 -96
- atcoderstudybooster-0.2.dist-info/RECORD +0 -17
- {atcoderstudybooster-0.2.dist-info → atcoderstudybooster-0.3.1.dist-info}/entry_points.txt +0 -0
atcdr/test.py
CHANGED
@@ -2,293 +2,431 @@ import os
|
|
2
2
|
import subprocess
|
3
3
|
import tempfile
|
4
4
|
import time
|
5
|
-
from dataclasses import dataclass
|
5
|
+
from dataclasses import dataclass, field
|
6
6
|
from enum import Enum
|
7
|
-
from typing import
|
7
|
+
from typing import Dict, List, Optional, Tuple, Union
|
8
8
|
|
9
|
-
from
|
10
|
-
from rich.
|
9
|
+
from rich.console import Group, RenderableType
|
10
|
+
from rich.live import Live
|
11
11
|
from rich.markup import escape
|
12
12
|
from rich.panel import Panel
|
13
|
+
from rich.progress import BarColumn, Progress, SpinnerColumn, TextColumn
|
14
|
+
from rich.rule import Rule
|
15
|
+
from rich.style import Style
|
16
|
+
from rich.syntax import Syntax
|
13
17
|
from rich.table import Table
|
14
18
|
from rich.text import Text
|
15
19
|
|
16
20
|
from atcdr.util.execute import execute_files
|
17
|
-
from atcdr.util.filetype import
|
21
|
+
from atcdr.util.filetype import (
|
22
|
+
COMPILED_LANGUAGES,
|
23
|
+
INTERPRETED_LANGUAGES,
|
24
|
+
Lang,
|
25
|
+
detect_language,
|
26
|
+
lang2str,
|
27
|
+
)
|
28
|
+
from atcdr.util.parse import ProblemHTML
|
18
29
|
|
19
30
|
|
20
31
|
@dataclass
|
21
32
|
class TestCase:
|
22
|
-
|
23
|
-
|
33
|
+
input: str
|
34
|
+
output: str
|
24
35
|
|
25
36
|
|
26
37
|
@dataclass
|
27
38
|
class LabeledTestCase:
|
28
|
-
|
29
|
-
|
39
|
+
label: str
|
40
|
+
case: TestCase
|
30
41
|
|
31
42
|
|
32
43
|
class ResultStatus(Enum):
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
44
|
+
AC = 'Accepted'
|
45
|
+
WA = 'Wrong Answer'
|
46
|
+
TLE = 'Time Limit Exceeded'
|
47
|
+
MLE = 'Memory Limit Exceeded'
|
48
|
+
RE = 'Runtime Error'
|
49
|
+
CE = 'Compilation Error'
|
50
|
+
WJ = 'Juding ...'
|
39
51
|
|
40
52
|
|
41
53
|
@dataclass
|
42
54
|
class TestCaseResult:
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
55
|
+
output: str
|
56
|
+
executed_time: Union[int, None]
|
57
|
+
# memory_usage: Union[int, None]
|
58
|
+
passed: ResultStatus
|
47
59
|
|
48
60
|
|
49
61
|
@dataclass
|
50
62
|
class LabeledTestCaseResult:
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
result: TestCaseResult
|
63
|
+
label: str
|
64
|
+
testcase: TestCase
|
65
|
+
result: TestCaseResult
|
55
66
|
|
56
67
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
68
|
+
@dataclass
|
69
|
+
class TestInformation:
|
70
|
+
lang: Lang
|
71
|
+
sourcename: str
|
72
|
+
case_number: int
|
73
|
+
results: List[ResultStatus] = field(default_factory=list)
|
74
|
+
compiler_message: str = ''
|
75
|
+
compile_time: Optional[int] = None
|
76
|
+
_summary: Optional[ResultStatus] = None
|
77
|
+
|
78
|
+
@property
|
79
|
+
def summary(self) -> ResultStatus:
|
80
|
+
if self._summary:
|
81
|
+
return self._summary
|
82
|
+
else:
|
83
|
+
priority_order = [
|
84
|
+
ResultStatus.CE,
|
85
|
+
ResultStatus.RE,
|
86
|
+
ResultStatus.WA,
|
87
|
+
ResultStatus.TLE,
|
88
|
+
ResultStatus.MLE,
|
89
|
+
ResultStatus.WJ,
|
90
|
+
ResultStatus.AC,
|
91
|
+
]
|
92
|
+
priority_dict = {
|
93
|
+
status: index + 1 for index, status in enumerate(priority_order)
|
94
|
+
}
|
95
|
+
summary = min(
|
96
|
+
self.results,
|
97
|
+
key=lambda status: priority_dict[status],
|
98
|
+
default=ResultStatus.WJ,
|
99
|
+
)
|
100
|
+
|
101
|
+
if len(self.results) == self.case_number:
|
102
|
+
return summary
|
103
|
+
|
104
|
+
return ResultStatus.WJ if summary == ResultStatus.AC else summary
|
105
|
+
|
106
|
+
@summary.setter
|
107
|
+
def summary(self, value: ResultStatus) -> None:
|
108
|
+
self._summary = value
|
109
|
+
|
110
|
+
def update(
|
111
|
+
self, updator: Union[TestCaseResult, LabeledTestCaseResult, ResultStatus]
|
112
|
+
) -> None:
|
113
|
+
match updator:
|
114
|
+
case TestCaseResult():
|
115
|
+
self.results.append(updator.passed)
|
116
|
+
case LabeledTestCaseResult():
|
117
|
+
self.results.append(updator.result.passed)
|
118
|
+
case ResultStatus():
|
119
|
+
self.results.append(updator)
|
120
|
+
|
121
|
+
def __iadd__(
|
122
|
+
self, other: Union[TestCaseResult, LabeledTestCaseResult, ResultStatus]
|
123
|
+
) -> 'TestInformation':
|
124
|
+
self.update(other)
|
125
|
+
return self
|
126
|
+
|
127
|
+
|
128
|
+
class TestRunner:
|
129
|
+
def __init__(self, path: str, lcases: List[LabeledTestCase]) -> None:
|
130
|
+
self.source = path
|
131
|
+
self.lcases = iter(lcases)
|
132
|
+
self.info = TestInformation(
|
133
|
+
lang=detect_language(self.source),
|
134
|
+
sourcename=path,
|
135
|
+
case_number=len(lcases),
|
136
|
+
)
|
137
|
+
|
138
|
+
def __iter__(self):
|
139
|
+
lang = self.info.lang
|
140
|
+
if lang in COMPILED_LANGUAGES:
|
141
|
+
exe_path, compile_result, compile_time = run_compile(self.source, lang)
|
142
|
+
self.info.compiler_message = compile_result.stderr
|
143
|
+
self.info.compile_time = compile_time
|
144
|
+
if compile_result.returncode != 0:
|
145
|
+
self.info.results = [ResultStatus.CE]
|
146
|
+
return iter([])
|
147
|
+
|
148
|
+
self.cmd = [
|
149
|
+
arg.format(source_path=self.source, exec_path=exe_path)
|
150
|
+
for arg in LANGUAGE_RUN_COMMANDS[lang]
|
151
|
+
]
|
152
|
+
self.exe = exe_path
|
153
|
+
run_code(self.cmd, TestCase(input='', output='')) # バイナリーの慣らし運転
|
154
|
+
elif lang in INTERPRETED_LANGUAGES:
|
155
|
+
self.cmd = [
|
156
|
+
arg.format(source_path=self.source)
|
157
|
+
for arg in LANGUAGE_RUN_COMMANDS[lang]
|
158
|
+
]
|
159
|
+
self.exe = None
|
160
|
+
else:
|
161
|
+
raise ValueError(f'{lang}の適切な言語のランナーが見つかりませんでした.')
|
162
|
+
|
163
|
+
return self
|
164
|
+
|
165
|
+
def __next__(self):
|
166
|
+
try:
|
167
|
+
lcase = next(self.lcases)
|
168
|
+
result = run_code(self.cmd, lcase.case)
|
169
|
+
self.info += result
|
170
|
+
return LabeledTestCaseResult(lcase.label, lcase.case, result)
|
171
|
+
except StopIteration:
|
172
|
+
if self.exe and os.path.exists(self.exe):
|
173
|
+
os.remove(self.exe)
|
174
|
+
raise
|
66
175
|
|
67
|
-
sample_input_pre = sample_input_section.find_next('pre')
|
68
|
-
sample_output_pre = sample_output_section.find_next('pre')
|
69
176
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
177
|
+
def run_code(cmd: list, case: TestCase) -> TestCaseResult:
|
178
|
+
start_time = time.time()
|
179
|
+
try:
|
180
|
+
proc = subprocess.run(
|
181
|
+
cmd, input=case.input, text=True, capture_output=True, timeout=4
|
182
|
+
)
|
183
|
+
end_time = time.time()
|
184
|
+
executed_time = int((end_time - start_time) * 1000)
|
185
|
+
except subprocess.TimeoutExpired as e_proc:
|
186
|
+
end_time = time.time()
|
187
|
+
executed_time = int((end_time - start_time) * 1000)
|
188
|
+
stdout_text = e_proc.stdout.decode('utf-8') if e_proc.stdout is not None else ''
|
189
|
+
stderr_text = e_proc.stderr.decode('utf-8') if e_proc.stderr is not None else ''
|
190
|
+
text = stdout_text + '\n' + stderr_text
|
191
|
+
return TestCaseResult(
|
192
|
+
output=text, executed_time=executed_time, passed=ResultStatus.TLE
|
193
|
+
)
|
194
|
+
|
195
|
+
# プロセスの終了コードを確認し、異常終了ならREを返す
|
196
|
+
if proc.returncode != 0:
|
197
|
+
return TestCaseResult(
|
198
|
+
output=proc.stdout + '\n' + proc.stderr,
|
199
|
+
executed_time=executed_time,
|
200
|
+
passed=ResultStatus.RE,
|
201
|
+
)
|
202
|
+
|
203
|
+
# 実際の出力と期待される出力を比較
|
204
|
+
actual_output = proc.stdout.strip()
|
205
|
+
expected_output = case.output.strip()
|
206
|
+
|
207
|
+
if actual_output != expected_output:
|
208
|
+
return TestCaseResult(
|
209
|
+
output=actual_output,
|
210
|
+
executed_time=executed_time,
|
211
|
+
passed=ResultStatus.WA,
|
212
|
+
)
|
213
|
+
else:
|
214
|
+
return TestCaseResult(
|
215
|
+
output=actual_output, executed_time=executed_time, passed=ResultStatus.AC
|
216
|
+
)
|
217
|
+
|
218
|
+
|
219
|
+
LANGUAGE_RUN_COMMANDS: Dict[Lang, list] = {
|
220
|
+
Lang.PYTHON: ['python3', '{source_path}'],
|
221
|
+
Lang.JAVASCRIPT: ['node', '{source_path}'],
|
222
|
+
Lang.C: ['{exec_path}'],
|
223
|
+
Lang.CPP: ['{exec_path}'],
|
224
|
+
Lang.RUST: ['{exec_path}'],
|
225
|
+
Lang.JAVA: ['java', os.path.splitext(os.path.basename('{source_path}'))[0]],
|
226
|
+
}
|
80
227
|
|
81
|
-
|
82
|
-
|
83
|
-
|
228
|
+
LANGUAGE_COMPILE_COMMANDS: Dict[Lang, list] = {
|
229
|
+
Lang.C: ['gcc', '{source_path}', '-o', '{exec_path}'],
|
230
|
+
Lang.CPP: ['g++', '{source_path}', '-o', '{exec_path}'],
|
231
|
+
Lang.RUST: ['rustc', '{source_path}', '-o', '{exec_path}'],
|
232
|
+
Lang.JAVA: ['javac', '{source_path}'],
|
233
|
+
}
|
84
234
|
|
85
|
-
return test_cases
|
86
235
|
|
236
|
+
def run_compile(
|
237
|
+
path: str, lang: Lang
|
238
|
+
) -> Tuple[str, subprocess.CompletedProcess, Optional[int]]:
|
239
|
+
with tempfile.NamedTemporaryFile(delete=False) as tmp:
|
240
|
+
exec_path = tmp.name
|
241
|
+
cmd = [
|
242
|
+
arg.format(source_path=path, exec_path=exec_path)
|
243
|
+
for arg in LANGUAGE_COMPILE_COMMANDS[lang]
|
244
|
+
]
|
245
|
+
start_time = time.time()
|
246
|
+
compile_result = subprocess.run(cmd, capture_output=True, text=True)
|
247
|
+
compile_time = int((time.time() - start_time) * 1000)
|
248
|
+
|
249
|
+
return exec_path, compile_result, compile_time
|
250
|
+
|
251
|
+
|
252
|
+
COLOR_MAP = {
|
253
|
+
ResultStatus.AC: 'green',
|
254
|
+
ResultStatus.WA: 'red',
|
255
|
+
ResultStatus.TLE: 'yellow',
|
256
|
+
ResultStatus.MLE: 'yellow',
|
257
|
+
ResultStatus.RE: 'yellow',
|
258
|
+
ResultStatus.CE: 'yellow',
|
259
|
+
ResultStatus.WJ: 'grey',
|
260
|
+
}
|
87
261
|
|
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
|
-
output=actual_output, executed_time=execution_time, passed=ResultStatus.AC
|
115
|
-
)
|
116
|
-
except subprocess.TimeoutExpired:
|
117
|
-
return TestCaseResult(
|
118
|
-
output='Time Limit Exceeded', executed_time=None, passed=ResultStatus.TLE
|
119
|
-
)
|
120
|
-
except Exception as e:
|
121
|
-
return TestCaseResult(output=str(e), executed_time=None, passed=ResultStatus.RE)
|
122
|
-
|
123
|
-
|
124
|
-
def run_python(path: str, case: TestCase) -> TestCaseResult:
|
125
|
-
return run_code(['python3', path], case)
|
126
|
-
|
127
|
-
|
128
|
-
def run_javascript(path: str, case: TestCase) -> TestCaseResult:
|
129
|
-
return run_code(['node', path], case)
|
130
|
-
|
131
|
-
|
132
|
-
def run_c(path: str, case: TestCase) -> TestCaseResult:
|
133
|
-
with tempfile.NamedTemporaryFile(delete=True) as tmp:
|
134
|
-
exec_path = tmp.name
|
135
|
-
compile_result = subprocess.run(
|
136
|
-
['gcc', path, '-o', exec_path], capture_output=True, text=True
|
137
|
-
)
|
138
|
-
if compile_result.returncode != 0:
|
139
|
-
return TestCaseResult(
|
140
|
-
output=compile_result.stderr, executed_time=None, passed=ResultStatus.CE
|
141
|
-
)
|
142
|
-
return run_code([exec_path], case)
|
143
|
-
|
144
|
-
|
145
|
-
def run_cpp(path: str, case: TestCase) -> TestCaseResult:
|
146
|
-
with tempfile.NamedTemporaryFile(delete=True) as tmp:
|
147
|
-
exec_path = tmp.name
|
148
|
-
compile_result = subprocess.run(
|
149
|
-
['g++', path, '-o', exec_path], capture_output=True, text=True
|
150
|
-
)
|
151
|
-
if compile_result.returncode != 0:
|
152
|
-
return TestCaseResult(
|
153
|
-
output=compile_result.stderr, executed_time=None, passed=ResultStatus.CE
|
154
|
-
)
|
155
|
-
return run_code([exec_path], case)
|
156
|
-
|
157
|
-
|
158
|
-
def run_rust(path: str, case: TestCase) -> TestCaseResult:
|
159
|
-
with tempfile.NamedTemporaryFile(delete=True) as tmp:
|
160
|
-
exec_path = tmp.name
|
161
|
-
compile_result = subprocess.run(
|
162
|
-
['rustc', path, '-o', exec_path], capture_output=True, text=True
|
163
|
-
)
|
164
|
-
if compile_result.returncode != 0:
|
165
|
-
return TestCaseResult(
|
166
|
-
output=compile_result.stderr, executed_time=None, passed=ResultStatus.CE
|
167
|
-
)
|
168
|
-
return run_code([exec_path], case)
|
169
|
-
|
170
|
-
|
171
|
-
def run_java(path: str, case: TestCase) -> TestCaseResult:
|
172
|
-
compile_result = subprocess.run(['javac', path], capture_output=True, text=True)
|
173
|
-
if compile_result.returncode != 0:
|
174
|
-
return TestCaseResult(
|
175
|
-
output=compile_result.stderr, executed_time=None, passed=ResultStatus.CE
|
176
|
-
)
|
177
|
-
class_file = os.path.splitext(path)[0]
|
178
|
-
try:
|
179
|
-
return run_code(['java', class_file], case)
|
180
|
-
finally:
|
181
|
-
class_path = class_file + '.class'
|
182
|
-
if os.path.exists(class_path):
|
183
|
-
os.remove(class_path)
|
184
|
-
|
185
|
-
|
186
|
-
LANGUAGE_RUNNERS: Dict[Lang, Callable[[str, TestCase], TestCaseResult]] = {
|
187
|
-
Lang.PYTHON: run_python,
|
188
|
-
Lang.JAVASCRIPT: run_javascript,
|
189
|
-
Lang.C: run_c,
|
190
|
-
Lang.CPP: run_cpp,
|
191
|
-
Lang.RUST: run_rust,
|
192
|
-
Lang.JAVA: run_java,
|
262
|
+
STATUS_TEXT_MAP = {
|
263
|
+
ResultStatus.AC: Text.assemble(
|
264
|
+
('\u2713 ', 'green'),
|
265
|
+
(
|
266
|
+
f'{ResultStatus.AC.value}',
|
267
|
+
Style(bgcolor=COLOR_MAP[ResultStatus.AC], bold=True),
|
268
|
+
),
|
269
|
+
),
|
270
|
+
ResultStatus.WA: Text(
|
271
|
+
f'\u00d7 {ResultStatus.WA.value}', style=COLOR_MAP[ResultStatus.WA]
|
272
|
+
),
|
273
|
+
ResultStatus.TLE: Text(
|
274
|
+
f'\u00d7 {ResultStatus.TLE.value}', style=COLOR_MAP[ResultStatus.TLE]
|
275
|
+
),
|
276
|
+
ResultStatus.MLE: Text(
|
277
|
+
f'\u00d7 {ResultStatus.MLE.value}', style=COLOR_MAP[ResultStatus.MLE]
|
278
|
+
),
|
279
|
+
ResultStatus.RE: Text(
|
280
|
+
f'\u00d7 {ResultStatus.RE.value}', style=COLOR_MAP[ResultStatus.RE]
|
281
|
+
),
|
282
|
+
ResultStatus.CE: Text(
|
283
|
+
f'\u00d7 {ResultStatus.CE.value}', style=COLOR_MAP[ResultStatus.CE]
|
284
|
+
),
|
285
|
+
ResultStatus.WJ: Text(
|
286
|
+
f'\u23f3 {ResultStatus.WJ.value}', style=COLOR_MAP[ResultStatus.WJ]
|
287
|
+
),
|
193
288
|
}
|
194
289
|
|
195
290
|
|
196
|
-
def
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
291
|
+
def create_renderable_test_info(
|
292
|
+
test_info: TestInformation, progress: Optional[Progress] = None
|
293
|
+
) -> RenderableType:
|
294
|
+
components = []
|
295
|
+
|
296
|
+
success_count = sum(1 for result in test_info.results if result == ResultStatus.AC)
|
297
|
+
total_count = test_info.case_number
|
298
|
+
|
299
|
+
status_text = STATUS_TEXT_MAP[test_info.summary]
|
300
|
+
|
301
|
+
header_text = Text.assemble(
|
302
|
+
Text.from_markup(f'[cyan]{test_info.sourcename}[/]のテスト \n'),
|
303
|
+
Text.from_markup(
|
304
|
+
f'[italic #0f0f0f]コンパイルにかかった時間: [not italic cyan]{test_info.compile_time}[/] ms[/]\n'
|
305
|
+
)
|
306
|
+
if test_info.compile_time
|
307
|
+
else Text(''),
|
308
|
+
status_text,
|
309
|
+
Text.from_markup(
|
310
|
+
f' [{COLOR_MAP[test_info.summary]} bold]{success_count}[/] / [white bold]{total_count}[/]'
|
311
|
+
),
|
312
|
+
)
|
313
|
+
|
314
|
+
if progress:
|
315
|
+
components.append(Panel(Group(header_text, progress), expand=False))
|
316
|
+
else:
|
317
|
+
components.append(Panel(header_text, expand=False))
|
318
|
+
|
319
|
+
if test_info.compiler_message:
|
320
|
+
rule = Rule(
|
321
|
+
title='コンパイラーのメッセージ',
|
322
|
+
style=COLOR_MAP[ResultStatus.CE],
|
323
|
+
)
|
324
|
+
components.append(rule)
|
325
|
+
error_message = Syntax(
|
326
|
+
test_info.compiler_message, lang2str(test_info.lang), line_numbers=False
|
327
|
+
)
|
328
|
+
components.append(error_message)
|
329
|
+
|
330
|
+
return Group(*components)
|
331
|
+
|
332
|
+
|
333
|
+
def create_renderable_test_result(
|
334
|
+
i: int,
|
335
|
+
test_result: LabeledTestCaseResult,
|
336
|
+
) -> RenderableType:
|
337
|
+
rule = Rule(
|
338
|
+
title=f'No.{i+1} {test_result.label}',
|
339
|
+
style=COLOR_MAP[test_result.result.passed],
|
340
|
+
)
|
341
|
+
|
342
|
+
# 以下の部分は if-else ブロックの外に移動
|
343
|
+
status_header = Text.assemble(
|
344
|
+
'ステータス ',
|
345
|
+
STATUS_TEXT_MAP[test_result.result.passed], # status_text をここに追加
|
346
|
+
)
|
347
|
+
|
348
|
+
execution_time_text = None
|
349
|
+
if test_result.result.executed_time is not None:
|
350
|
+
execution_time_text = Text.from_markup(
|
351
|
+
f'実行時間 [cyan]{test_result.result.executed_time}[/cyan] ms'
|
352
|
+
)
|
353
|
+
|
354
|
+
table = Table(show_header=True, header_style='bold')
|
355
|
+
table.add_column('入力', style='cyan', min_width=10)
|
356
|
+
|
357
|
+
if test_result.result.passed != ResultStatus.AC:
|
358
|
+
table.add_column(
|
359
|
+
'出力',
|
360
|
+
style=COLOR_MAP[test_result.result.passed],
|
361
|
+
min_width=10,
|
362
|
+
overflow='fold',
|
363
|
+
)
|
364
|
+
table.add_column('正解の出力', style=COLOR_MAP[ResultStatus.AC], min_width=10)
|
365
|
+
table.add_row(
|
366
|
+
escape(test_result.testcase.input),
|
367
|
+
escape(test_result.result.output),
|
368
|
+
escape(test_result.testcase.output),
|
369
|
+
)
|
370
|
+
else:
|
371
|
+
table.add_column(
|
372
|
+
'出力', style=COLOR_MAP[test_result.result.passed], min_width=10
|
373
|
+
)
|
374
|
+
table.add_row(
|
375
|
+
escape(test_result.testcase.input), escape(test_result.result.output)
|
376
|
+
)
|
377
|
+
|
378
|
+
components = [
|
379
|
+
rule,
|
380
|
+
status_header,
|
381
|
+
execution_time_text if execution_time_text else '',
|
382
|
+
table,
|
383
|
+
]
|
384
|
+
|
385
|
+
return Group(*components)
|
386
|
+
|
387
|
+
|
388
|
+
def render_results(test: TestRunner) -> None:
|
389
|
+
progress = Progress(
|
390
|
+
SpinnerColumn(style='white', spinner_name='circleHalves'),
|
391
|
+
TextColumn('{task.description}'),
|
392
|
+
SpinnerColumn(style='white', spinner_name='simpleDots'),
|
393
|
+
BarColumn(),
|
394
|
+
)
|
395
|
+
task_id = progress.add_task(description='テスト進行中', total=test.info.case_number)
|
396
|
+
|
397
|
+
current_display = [create_renderable_test_info(test.info, progress)]
|
398
|
+
|
399
|
+
with Live(Group(*current_display)) as live:
|
400
|
+
for i, result in enumerate(test):
|
401
|
+
progress.advance(task_id, advance=1)
|
402
|
+
current_display[-1] = create_renderable_test_info(test.info, progress)
|
403
|
+
current_display.insert(-1, (create_renderable_test_result(i, result)))
|
404
|
+
live.update(Group(*current_display))
|
405
|
+
|
406
|
+
progress.update(task_id, description='テスト完了') # 完了メッセージに更新
|
407
|
+
current_display[-1] = create_renderable_test_info(test.info, progress)
|
408
|
+
live.update(Group(*current_display))
|
275
409
|
|
276
410
|
|
277
411
|
def run_test(path_of_code: str) -> None:
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
412
|
+
html_paths = [f for f in os.listdir('.') if f.endswith('.html')]
|
413
|
+
if not html_paths:
|
414
|
+
print(
|
415
|
+
'問題のファイルが見つかりません。\n問題のファイルが存在するディレクトリーに移動してから実行してください。'
|
416
|
+
)
|
417
|
+
return
|
284
418
|
|
285
|
-
|
286
|
-
|
419
|
+
with open(html_paths[0], 'r') as file:
|
420
|
+
html = file.read()
|
287
421
|
|
288
|
-
|
289
|
-
|
290
|
-
|
422
|
+
lcases = ProblemHTML(html).load_labeled_testcase()
|
423
|
+
test = TestRunner(path_of_code, lcases)
|
424
|
+
render_results(test)
|
291
425
|
|
292
426
|
|
293
427
|
def test(*args: str) -> None:
|
294
|
-
|
428
|
+
execute_files(
|
429
|
+
*args,
|
430
|
+
func=run_test,
|
431
|
+
target_filetypes=INTERPRETED_LANGUAGES + COMPILED_LANGUAGES,
|
432
|
+
)
|