From 587397f9c1378e889f98fb6fd658ae06e9e6be16 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Tue, 23 Sep 2025 09:20:54 +0000 Subject: [PATCH] test suite: use match statement where practicable (2 files) --- test/objtest.py | 54 ++++++++++++++++++++++++----------------------- test/tooltest2.py | 48 +++++++++++++++++++++++------------------ 2 files changed, 56 insertions(+), 46 deletions(-) diff --git a/test/objtest.py b/test/objtest.py index 56138cc7..a3304e51 100755 --- a/test/objtest.py +++ b/test/objtest.py @@ -70,32 +70,34 @@ def run_test(mod, test, arg, input_data, arg1, exc_name): if input_data == 'good' and isinstance(arg, tuple): arg, ret_chk = arg - if isinstance(arg, dict): # pass one arg + kwargs to constructor - arg_copy = arg.copy() - if 'arg' in arg: - args = [arg['arg']] - ret_chk = args[0] - del arg['arg'] - else: - args = [] - ret_chk = list(arg.values())[0] # assume only one key present - if 'ret' in arg: - ret_chk = arg['ret'] - del arg['ret'] - del arg_copy['ret'] - if 'exc_name' in arg: - exc_name = arg['exc_name'] - del arg['exc_name'] - del arg_copy['exc_name'] - if 'ret_idx' in arg: - ret_idx = arg['ret_idx'] - del arg['ret_idx'] - del arg_copy['ret_idx'] - kwargs.update(arg) - elif isinstance(arg, tuple): - args = arg - else: - args = [arg] + + match arg: + case dict(): # pass one arg + kwargs to constructor + arg_copy = arg.copy() + if 'arg' in arg: + args = [arg['arg']] + ret_chk = args[0] + del arg['arg'] + else: + args = [] + ret_chk = list(arg.values())[0] # assume only one key present + if 'ret' in arg: + ret_chk = arg['ret'] + del arg['ret'] + del arg_copy['ret'] + if 'exc_name' in arg: + exc_name = arg['exc_name'] + del arg['exc_name'] + del arg_copy['exc_name'] + if 'ret_idx' in arg: + ret_idx = arg['ret_idx'] + del arg['ret_idx'] + del arg_copy['ret_idx'] + kwargs.update(arg) + case tuple(): + args = arg + case _: + args = [arg] if cfg.getobj: if args: diff --git a/test/tooltest2.py b/test/tooltest2.py index 76d5c1bb..c88d7b69 100755 --- a/test/tooltest2.py +++ b/test/tooltest2.py @@ -151,12 +151,17 @@ def tool_api(cls, cmd_name, args, opts): return getattr(tool, cmd_name)(*pargs, **kwargs) def check_output(out, chk): - if isinstance(chk, str): - chk = chk.encode() - if isinstance(out, int): - out = str(out).encode() - if isinstance(out, str): - out = out.encode() + + match chk: + case str(): + chk = chk.encode() + + match out: + case int(): + out = str(out).encode() + case str(): + out = out.encode() + try: outd = out.decode() except: @@ -164,20 +169,23 @@ def check_output(out, chk): err_fs = "Output ({!r}) doesn't match expected output ({!r})" - if type(chk).__name__ == 'function': - assert chk(outd), f'{chk.__name__}({outd}) failed!' - elif isinstance(chk, dict): - for k, v in chk.items(): - match k: - case 'boolfunc': - assert v(outd), f'{v.__name__}({outd}) failed!' - case 'value': - assert outd == v, err_fs.format(outd, v) - case _: - if (outval := getattr(__builtins__, k)(out)) != v: - die(1, f'{k}({out}) returned {outval}, not {v}!') - elif chk is not None: - assert out == chk, err_fs.format(out, chk) + match type(chk).__name__: + case 'NoneType': + pass + case 'function': + assert chk(outd), f'{chk.__name__}({outd}) failed!' + case 'dict': + for k, v in chk.items(): + match k: + case 'boolfunc': + assert v(outd), f'{v.__name__}({outd}) failed!' + case 'value': + assert outd == v, err_fs.format(outd, v) + case _: + if (outval := getattr(__builtins__, k)(out)) != v: + die(1, f'{k}({out}) returned {outval}, not {v}!') + case _: + assert out == chk, err_fs.format(out, chk) def run_test(cls, gid, cmd_name): data = tests[gid][cmd_name]