-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeaufort_cipher_test_harness.py
104 lines (90 loc) · 3.17 KB
/
beaufort_cipher_test_harness.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'''
This is a test harness for a beaufort cipher decoder
Place your function(s) here and run it.
please, name your function(s) as:
<your_name>_<suffix_you_define>
'''
def test_func(func):
import math
import inspect
def score(code_lines):
if code_lines <= 3:
return 'A+'
if code_lines <= 7:
return 'A'
if code_lines <= 12:
return 'B'
if code_lines <= 16:
return 'C'
if code_lines <= 20:
return 'D'
if 21 < code_lines:
return 'F'
else:
return 'unknown'
tests = [('YJTWPWAYHIXZPM', 'PYTHON', 'SPAM_SPAM_SPAM'),
('IJVAHZHPFGJPW_QQT', 'ARM_IS_OFF', 'TIS_BUT_A_SCRATCH'),
('YAQZFSX', 'CRUCIFIXION', 'FREEDOM'),
('GXCHVFFAJHUOPQPQLYT', 'NAUGHTY_BOY', 'HES_NOT_THE_MESSIAH'),
('N_PHW_AHXFVHTYKEAJSGEHWLMTD_KI_BFJJELOJL_ALAWIHWBKQNH',
'KNIGHTS_WHO_SAY_NI',
'YOU_MUST_CUT_DOWN_THE_MIGHTIEST_TREE___WITH_A_HERRING')
]
results = []
print(f'\n\nTesting function: {func.__name__}\n')
print('-----------')
for message, key, expected in tests:
print(f'Testing: {message}')
print(f'Key: {key}')
try:
result = func(message, key)
except (ValueError, TypeError) as errmsg:
print(f'Error raised by function: {errmsg}')
results.append(False)
else:
try:
assert result == expected
except AssertionError:
print(f'expected {expected}\nreceived {result}')
print('---Failed!\n')
results.append(False)
else:
print(f'Message: {result}')
print('---Passed!\n')
results.append(True)
print('-----------')
if all(results):
print('\nAll Tests Passed!')
func_code = inspect.getsource(func)
func_code_lines = []
func_code_line = []
for char in func_code:
if char != '\n':
func_code_line.append(char)
else:
func_code_lines.append(''.join(func_code_line))
func_code_line = []
count = 0
for line in func_code_lines:
if line:
line = line.lstrip(' ')
if func.__name__ in line:
continue
if line[0] == '#':
continue
if len(line) > 1:
line_count = math.ceil(len(line)/72)
count += line_count
if 'return' in line:
count -= 1
print(f'Code Line Count: {count}')
print(f'Rating: "{score(count)}"')
else:
print('\nTest Failed!')
print('-----------\n')
if __name__ == '__main__':
func_list = [func for name, func in globals().items()
if callable(func) and name[0:2] not in '__'
and name[0:4] not in ('exit', 'quit', 'get_', 'test')]
for func in func_list:
test_func(func)