Skip to content

Commit 30793e6

Browse files
Fix nim to work with the default indent setting & Fix a bug on a unit test where a wrong execution file can be potentially executed on compile failure (kyuridenamida#151)
1 parent 2acb5ed commit 30793e6

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

atcodertools/codegen/code_style_config.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ class CodeStyleConfig:
2020

2121
def __init__(self,
2222
indent_type: str = INDENT_TYPE_SPACE,
23-
indent_width: int = 4,
23+
indent_width: Optional[int] = None,
2424
code_generator_file: Optional[str] = None,
2525
template_file: Optional[str] = None,
2626
workspace_dir: Optional[str] = None,
2727
lang: str = "cpp",
2828
):
29-
from atcodertools.common.language import Language, LanguageNotFoundError, ALL_LANGUAGE_NAMES
29+
from atcodertools.common.language import Language, LanguageNotFoundError, ALL_LANGUAGE_NAMES, NIM
3030

3131
code_generator_file = normalize_path(code_generator_file)
3232
template_file = normalize_path(template_file)
@@ -41,7 +41,7 @@ def __init__(self,
4141
raise CodeStyleConfigInitError(
4242
"indent_type must be 'space' or 'tab'")
4343

44-
if indent_width < 0:
44+
if indent_width is not None and indent_width < 0:
4545
raise CodeStyleConfigInitError(
4646
"indent_width must be a positive integer")
4747

@@ -56,7 +56,12 @@ def __init__(self,
5656
)
5757

5858
self.indent_type = indent_type
59-
self.indent_width = indent_width
59+
60+
if indent_width is not None:
61+
self.indent_width = indent_width
62+
else:
63+
# nim has a special default value
64+
self.indent_width = 2 if lang == NIM else 4
6065

6166
if code_generator_file is not None:
6267
try:

tests/test_codegen.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,22 @@ def _exec_file_and_args(self, lang: Language) -> Tuple[str, List[str]]:
201201
else:
202202
raise NotImplementedError()
203203

204+
def _clean_up(self, lang: Language):
205+
if lang == CPP:
206+
os.remove(os.path.join(self.temp_dir, "a.out"))
207+
elif lang == JAVA:
208+
return
209+
elif lang == RUST:
210+
os.remove(os.path.join(self.temp_dir, "main"))
211+
elif lang == PYTHON:
212+
return
213+
elif lang == NIM:
214+
os.remove(os.path.join(self.temp_dir, "main"))
215+
elif lang == DLANG:
216+
os.remove(os.path.join(self.temp_dir, "main"))
217+
else:
218+
raise NotImplementedError()
219+
204220
def _compile_and_run(self, lang, format, template_file, expected_generated_code_file, input_file):
205221
code_file = os.path.join(self.temp_dir, lang.source_code_name("main"))
206222
exec_file, exec_args = self._exec_file_and_args(lang)
@@ -215,16 +231,22 @@ def _compile_and_run(self, lang, format, template_file, expected_generated_code_
215231
constants=ProblemConstantSet(123, "yes", "NO"),
216232
config=cfg
217233
)
218-
219234
code = lang.default_code_generator(args)
220235
# to remove version strings from test resources
221236
code = re.sub(r'Generated by \d+.\d+.\d+', 'Generated by x.y.z', code)
222237
self.compare_two_texts_ignoring_trailing_spaces(
223238
load_text_file(expected_generated_code_file), code)
224239
create_code(code, code_file)
225-
print(run_command(compile_cmd, self.temp_dir))
226-
exec_result = run_program(
227-
exec_file, input_file, 2, exec_args, self.temp_dir)
240+
try:
241+
print("Executing:", compile_cmd)
242+
print(run_command(compile_cmd, self.temp_dir))
243+
244+
print("Run program:", [exec_file] + exec_args)
245+
exec_result = run_program(
246+
exec_file, input_file, 2, exec_args, self.temp_dir)
247+
finally:
248+
self._clean_up(lang)
249+
228250
print("== stdout ==")
229251
print(exec_result.output)
230252
print("== stderr ==")
@@ -255,10 +277,6 @@ def verify(self,
255277
self.assertEqual(
256278
load_intermediate_types(py_test_name),
257279
str(response.types))
258-
if lang == NIM:
259-
cfg = CodeStyleConfig(indent_width=2)
260-
else:
261-
cfg = CodeStyleConfig()
262280

263281
self.assertEqual(
264282
load_generated_code(py_test_name, lang),
@@ -267,7 +285,7 @@ def verify(self,
267285
self.get_template(lang, template_type),
268286
response.original_result.format,
269287
constants,
270-
cfg)
288+
CodeStyleConfig(lang=lang.name))
271289
))
272290

273291
def get_template(self, lang: Language, template_type: str) -> str:

0 commit comments

Comments
 (0)