@@ -271,29 +271,44 @@ def _compare(
271
271
Returns:
272
272
bool: true if comparison is successful, false otherwise
273
273
"""
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 :
276
289
return compare_strings (reference , result , rtol , atol , ** kwargs )
277
290
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
280
299
reference_string = get_string (reference )
281
300
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" ],
287
307
)
308
+ else :
309
+ raise TypeError ("Result must be either string or InputFile." )
288
310
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
297
312
try :
298
313
return compare_strings (
299
314
reference_string , result_string , rtol , atol , ** kwargs
@@ -302,21 +317,10 @@ def _compare(
302
317
return handle_unequal_strings (
303
318
error , tmp_path , current_test_name , result_string , reference
304
319
)
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
- )
318
320
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
+ )
320
324
321
325
return _compare
322
326
0 commit comments