forked from canbula/PythonProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_functions.py
More file actions
134 lines (126 loc) · 6.96 KB
/
Copy pathtest_functions.py
File metadata and controls
134 lines (126 loc) · 6.96 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import os
import inspect
files = [f for f in os.listdir(os.path.dirname(__file__)) if f.startswith("functions")]
for f in files:
exec("import " + f[:-3] + " as " + f[:-3])
def test_names():
for f in files:
assert "custom_power" in dir(eval(f[:-3])), "custom_power is not defined in " + f[:-3]
assert "custom_equation" in dir(eval(f[:-3])), "custom_equation is not defined in " + f[:-3]
assert "fn_w_counter" in dir(eval(f[:-3])), "fn_w_counter is not defined in " + f[:-3]
def test_callables():
for f in files:
assert callable(eval(f[:-3]).custom_power), "custom_power is not callable in " + f[:-3]
assert callable(eval(f[:-3]).custom_equation), "custom_equation is not callable in " + f[:-3]
assert callable(eval(f[:-3]).fn_w_counter), "fn_w_counter is not callable in " + f[:-3]
def test_custom_power():
for f in files:
sig = inspect.signature(eval(f[:-3]).custom_power)
assert eval(f[:-3]).custom_power.__name__ == "<lambda>", \
"custom_power is not a lambda function in " + f[:-3]
assert 'x' in sig.parameters, \
"custom_power has wrong parameters in " + f[:-3]
assert 'e' in sig.parameters, \
"custom_power has wrong parameters in " + f[:-3]
assert sig.parameters['x'].kind == inspect._POSITIONAL_ONLY, \
"custom_power has wrong kind in " + f[:-3]
assert sig.parameters['e'].kind == inspect._POSITIONAL_OR_KEYWORD, \
"custom_power has wrong kind in " + f[:-3]
assert sig.parameters['x'].default == 0, \
"custom_power has wrong default value in " + f[:-3]
assert sig.parameters['e'].default == 1, \
"custom_power has wrong default value in " + f[:-3]
assert eval(f[:-3]).custom_power(2) == 2, \
"custom_power is not working in " + f[:-3]
assert eval(f[:-3]).custom_power(2, 3) == 8, \
"custom_power is not working in " + f[:-3]
assert eval(f[:-3]).custom_power(2, e=3) == 8, \
"custom_power is not working in " + f[:-3]
assert eval(f[:-3]).custom_power(49, e=0.5) == 7, \
"custom_power is not working in " + f[:-3]
def test_custom_equation():
for f in files:
sig = inspect.signature(eval(f[:-3]).custom_equation)
assert eval(f[:-3]).custom_equation.__doc__ is not None, \
"custom_equation has no docstring in " + f[:-3]
assert eval(f[:-3]).custom_equation.__doc__.count(":param") == 5, \
"custom_equation has wrong docstring in " + f[:-3] + " (param)"
assert eval(f[:-3]).custom_equation.__doc__.count(":return") == 1, \
"custom_equation has wrong docstring in " + f[:-3] + " (return)"
assert sig.return_annotation == float, \
"custom_equation has wrong return annotation in " + f[:-3]
assert 'x' in sig.parameters, \
"custom_equation has wrong parameters in " + f[:-3]
assert 'y' in sig.parameters, \
"custom_equation has wrong parameters in " + f[:-3]
assert 'a' in sig.parameters, \
"custom_equation has wrong parameters in " + f[:-3]
assert 'b' in sig.parameters, \
"custom_equation has wrong parameters in " + f[:-3]
assert 'c' in sig.parameters, \
"custom_equation has wrong parameters in " + f[:-3]
assert sig.parameters['x'].kind == inspect._POSITIONAL_ONLY, \
"custom_equation has wrong kind for x in " + f[:-3]
assert sig.parameters['y'].kind == inspect._POSITIONAL_ONLY, \
"custom_equation has wrong kind for y in " + f[:-3]
assert sig.parameters['a'].kind == inspect._POSITIONAL_OR_KEYWORD, \
"custom_equation has wrong kind for a in " + f[:-3]
assert sig.parameters['b'].kind == inspect._POSITIONAL_OR_KEYWORD, \
"custom_equation has wrong kind for b in " + f[:-3]
assert sig.parameters['c'].kind == inspect._KEYWORD_ONLY, \
"custom_equation has wrong kind for c in " + f[:-3]
assert sig.parameters['x'].annotation == int, \
"custom_equation has wrong annotation for x in " + f[:-3]
assert sig.parameters['y'].annotation == int, \
"custom_equation has wrong annotation for y in " + f[:-3]
assert sig.parameters['a'].annotation == int, \
"custom_equation has wrong annotation for a in " + f[:-3]
assert sig.parameters['b'].annotation == int, \
"custom_equation has wrong annotation for b in " + f[:-3]
assert sig.parameters['c'].annotation == int, \
"custom_equation has wrong annotation for c in " + f[:-3]
assert sig.parameters['x'].default == 0, \
"custom_equation has wrong default value for x in " + f[:-3]
assert sig.parameters['y'].default == 0, \
"custom_equation has wrong default value for y in " + f[:-3]
assert sig.parameters['a'].default == 1, \
"custom_equation has wrong default value for a in " + f[:-3]
assert sig.parameters['b'].default == 1, \
"custom_equation has wrong default value for b in " + f[:-3]
assert sig.parameters['c'].default == 1, \
"custom_equation has wrong default value for c in " + f[:-3]
try:
eval(f[:-3]).custom_equation(1.5, 2.5)
except TypeError as e:
assert str(e).count("must") > 0, \
"custom_equation has wrong raises in " + f[:-3]
try:
eval(f[:-3]).custom_equation(1, 2, c=1.5)
except TypeError as e:
assert str(e).count("must") > 0, \
"custom_equation has wrong raises in " + f[:-3]
assert eval(f[:-3]).custom_equation(1, 2) == 3, \
"custom_equation is not working in " + f[:-3]
assert eval(f[:-3]).custom_equation(1, 2, 3) == 3, \
"custom_equation is not working in " + f[:-3]
assert eval(f[:-3]).custom_equation(1, 2, 3, 4) == 17, \
"custom_equation is not working in " + f[:-3]
assert eval(f[:-3]).custom_equation(1, 2, 3, 4, c=5) == 3.4, \
"custom_equation is not working in " + f[:-3]
assert eval(f[:-3]).custom_equation(1, 2, 3, c=5, b=6) == 13, \
"custom_equation is not working in " + f[:-3]
def test_fn_w_counter():
for f in files:
sig = inspect.signature(eval(f[:-3]).fn_w_counter)
assert sig.return_annotation == (int, dict[str, int]), \
"fn_w_counter has wrong return annotation in " + f[:-3]
# call the function multiple times
for i in range(1, 10):
assert eval(f[:-3]).fn_w_counter() == (i, {f[:-3]: i}), \
"fn_w_counter is not working in " + f[:-3]
for i in range(10, 97):
assert eval(f[:-3]).fn_w_counter() == (i, {f[:-3]: i}), \
"fn_w_counter is not working in " + f[:-3]
for i in range(97, 100):
assert eval(f[:-3]).fn_w_counter() == (i, {f[:-3]: i}), \
"fn_w_counter is not working in " + f[:-3]