Skip to content

Commit 1afc7bb

Browse files
Update compare function to reduce duplicated code
1 parent 7fc6111 commit 1afc7bb

File tree

1 file changed

+35
-31
lines changed

1 file changed

+35
-31
lines changed

tests/conftest.py

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -271,29 +271,44 @@ def _compare(
271271
Returns:
272272
bool: true if comparison is successful, false otherwise
273273
"""
274-
# Compare two strings
275-
if type(reference) is str and type(result) is str:
274+
275+
# Compare two universal files
276+
if isinstance(reference, Path) and isinstance(result, Path):
277+
if reference.suffix != result.suffix:
278+
raise RuntimeError(
279+
"Reference and result file must be of same file type!"
280+
)
281+
elif reference.suffix in [".vtk", ".vtu"]:
282+
return compare_vtk_files(reference, result, rtol, atol)
283+
else:
284+
raise NotImplementedError(
285+
f"Comparison is not yet implemented for {reference.suffix} files."
286+
)
287+
# Compare two strings (no path is available for reference file => no error handling)
288+
elif type(reference) is str and type(result) is str:
276289
return compare_strings(reference, result, rtol, atol, **kwargs)
277290

278-
# Compare a file with a string
279-
elif isinstance(reference, Path) and type(result) is str:
291+
# Compare a file with a string/InputFile
292+
elif (
293+
isinstance(reference, Path)
294+
and type(result) is str
295+
or isinstance(reference, Path)
296+
and isinstance(result, InputFile)
297+
):
298+
# retrieve strings
280299
reference_string = get_string(reference)
281300

282-
try:
283-
return compare_strings(reference_string, result, rtol, atol, **kwargs)
284-
except AssertionError as error:
285-
return handle_unequal_strings(
286-
error, tmp_path, current_test_name, result, reference
301+
if isinstance(result, str):
302+
result_string = result
303+
elif isinstance(result, InputFile):
304+
result_string = result.get_string(
305+
check_nox=input_file_kwargs["input_file_string_check_nox"],
306+
header=input_file_kwargs["input_file_string_check_header"],
287307
)
308+
else:
309+
raise TypeError("Result must be either string or InputFile.")
288310

289-
# Compare a file with an InputFile
290-
elif isinstance(reference, Path) and isinstance(result, InputFile):
291-
reference_string = get_string(reference)
292-
result_string = result.get_string(
293-
check_nox=input_file_kwargs["input_file_string_check_nox"],
294-
header=input_file_kwargs["input_file_string_check_header"],
295-
)
296-
311+
# compare strings and handle errors
297312
try:
298313
return compare_strings(
299314
reference_string, result_string, rtol, atol, **kwargs
@@ -302,21 +317,10 @@ def _compare(
302317
return handle_unequal_strings(
303318
error, tmp_path, current_test_name, result_string, reference
304319
)
305-
306-
# Compare two universal files
307-
elif isinstance(reference, Path) and isinstance(result, Path):
308-
if reference.suffix != result.suffix:
309-
raise RuntimeError(
310-
"Reference and result file must be of same file type!"
311-
)
312-
elif reference.suffix in [".vtk", ".vtu"]:
313-
return compare_vtk_files(reference, result, rtol, atol)
314-
else:
315-
raise NotImplementedError(
316-
f"Comparison is not yet implemented for {reference.suffix} files."
317-
)
318320
else:
319-
raise TypeError("Reference and result must be either string or Path.")
321+
raise TypeError(
322+
"Reference and result must either be string/InputFile or Path!"
323+
)
320324

321325
return _compare
322326

0 commit comments

Comments
 (0)