Created
January 5, 2019 12:49
-
-
Save tuanchauict/f231463503aa21297651981dc0e77277 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def testcase(*testcases): | |
def build_format(): | |
max_param_len = 0 | |
max_result_len = 0 | |
num_params = 0 | |
for case in testcases: | |
num_params = len(case) - 1 | |
for param in case[1:]: | |
str_param = str(param) | |
max_param_len = max(max_param_len, len(str_param)) | |
max_result_len = max(max_result_len, len(str(case[0]))) | |
param_format = ("%" + "%ds" % (max_param_len + 2)) * num_params | |
result_format = "%s --> %-" + "%ds" % (max_result_len + 2) + " | %s" | |
return param_format, result_format | |
def deco(fun): | |
def wrapper(): | |
param_format, result_format = build_format() | |
for case in testcases: | |
expected = case[0] | |
params = case[1:] | |
result = fun(*params) | |
param_text = param_format % params | |
test_result = 'UNCHECKED' if expected is None else 'PASSED' if result == expected else 'FAILED' | |
print result_format % (param_text, result, test_result) | |
return wrapper | |
return deco |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment