Browse Source

test suite: use match statement where practicable (2 files)

The MMGen Project 2 months ago
parent
commit
587397f9c1
2 changed files with 56 additions and 46 deletions
  1. 28 26
      test/objtest.py
  2. 28 20
      test/tooltest2.py

+ 28 - 26
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:

+ 28 - 20
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]