|
1
|
|
|
import sys |
|
2
|
|
|
|
|
3
|
|
|
sys.path.insert(0, ".") |
|
4
|
|
|
import unittest |
|
5
|
|
|
from coalib.settings.FunctionMetadata import FunctionMetadata |
|
6
|
|
|
from coalib.settings.Section import Section |
|
7
|
|
|
from coalib.settings.Setting import Setting |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class TestClass: |
|
11
|
|
|
def __init__(self, param1, param2, param3=5, param4: int=6): |
|
12
|
|
|
""" |
|
13
|
|
|
Description |
|
14
|
|
|
|
|
15
|
|
|
:param param2: d |
|
16
|
|
|
:param param4: p4 desc |
|
17
|
|
|
:return: ret |
|
18
|
|
|
""" |
|
19
|
|
|
|
|
20
|
|
|
def good_function(self, a_param: int): |
|
21
|
|
|
pass |
|
22
|
|
|
|
|
23
|
|
|
def bad_function(self, bad_param: "no function"): |
|
24
|
|
|
pass |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
class FunctionMetadataTest(unittest.TestCase): |
|
28
|
|
|
def test_construction(self): |
|
29
|
|
|
self.check_function_metadata_data_set(FunctionMetadata("name"), "name") |
|
30
|
|
|
|
|
31
|
|
|
def test_from_function(self): |
|
32
|
|
|
uut = FunctionMetadata.from_function(self.test_from_function) |
|
33
|
|
|
self.check_function_metadata_data_set(uut, "test_from_function") |
|
34
|
|
|
# setattr on bound methods will fail, __dict__ will use the dict from |
|
35
|
|
|
# the unbound method which is ok. |
|
36
|
|
|
self.test_from_function.__dict__["__metadata__"] = ( |
|
37
|
|
|
FunctionMetadata("t")) |
|
38
|
|
|
uut = FunctionMetadata.from_function(self.test_from_function) |
|
39
|
|
|
self.check_function_metadata_data_set(uut, "t") |
|
40
|
|
|
|
|
41
|
|
|
uut = FunctionMetadata.from_function(TestClass(5, 5).__init__) |
|
42
|
|
|
self.check_function_metadata_data_set( |
|
43
|
|
|
uut, |
|
44
|
|
|
"__init__", |
|
45
|
|
|
desc="Description", |
|
46
|
|
|
retval_desc="ret", |
|
47
|
|
|
non_optional_params={ |
|
48
|
|
|
"param1": (uut.str_nodesc, None), |
|
49
|
|
|
"param2": ("d", None) |
|
50
|
|
|
}, |
|
51
|
|
|
optional_params={ |
|
52
|
|
|
"param3": (uut.str_nodesc + " (" |
|
53
|
|
|
+ uut.str_optional.format("5") + ")", |
|
54
|
|
|
None, 5), |
|
55
|
|
|
"param4": ("p4 desc (" |
|
56
|
|
|
+ uut.str_optional.format("6") + ")", int, 6)}) |
|
57
|
|
|
|
|
58
|
|
|
uut = FunctionMetadata.from_function(TestClass(5, 5).__init__, |
|
59
|
|
|
omit={"param3", "param2"}) |
|
60
|
|
|
self.check_function_metadata_data_set( |
|
61
|
|
|
uut, |
|
62
|
|
|
"__init__", |
|
63
|
|
|
desc="Description", |
|
64
|
|
|
retval_desc="ret", |
|
65
|
|
|
non_optional_params={ |
|
66
|
|
|
"param1": (uut.str_nodesc, |
|
67
|
|
|
None) |
|
68
|
|
|
}, |
|
69
|
|
|
optional_params={ |
|
70
|
|
|
"param4": ("p4 desc (" + uut.str_optional.format("6") + ")", |
|
71
|
|
|
int, |
|
72
|
|
|
6)}) |
|
73
|
|
|
|
|
74
|
|
|
def test_create_params_from_section_invalid(self): |
|
75
|
|
|
section = Section("name") |
|
76
|
|
|
section.append(Setting("bad_param", "value")) |
|
77
|
|
|
uut = FunctionMetadata.from_function(TestClass(5, 5).bad_function) |
|
78
|
|
|
|
|
79
|
|
|
with self.assertRaises(ValueError): |
|
80
|
|
|
uut.create_params_from_section(section) |
|
81
|
|
|
|
|
82
|
|
|
def test_create_params_from_section_valid(self): |
|
83
|
|
|
section = Section("name") |
|
84
|
|
|
section.append(Setting("a_param", "value")) |
|
85
|
|
|
uut = FunctionMetadata.from_function(TestClass(5, 5).good_function) |
|
86
|
|
|
|
|
87
|
|
|
with self.assertRaises(ValueError): |
|
88
|
|
|
uut.create_params_from_section(section) |
|
89
|
|
|
|
|
90
|
|
|
section.append(Setting("a_param", "5")) |
|
91
|
|
|
params = uut.create_params_from_section(section) |
|
92
|
|
|
self.assertEqual(params['a_param'], 5) |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
def check_function_metadata_data_set(self, |
|
96
|
|
|
metadata, |
|
97
|
|
|
name, |
|
98
|
|
|
desc="", |
|
99
|
|
|
retval_desc="", |
|
100
|
|
|
non_optional_params=None, |
|
101
|
|
|
optional_params=None): |
|
102
|
|
|
non_optional_params = non_optional_params or {} |
|
103
|
|
|
optional_params = optional_params or {} |
|
104
|
|
|
|
|
105
|
|
|
self.assertEqual(metadata.name, name) |
|
106
|
|
|
self.assertEqual(metadata.desc, desc) |
|
107
|
|
|
self.assertEqual(metadata.retval_desc, retval_desc) |
|
108
|
|
|
self.assertEqual(metadata.non_optional_params, non_optional_params) |
|
109
|
|
|
self.assertEqual(metadata.optional_params, optional_params) |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
if __name__ == '__main__': |
|
113
|
|
|
unittest.main(verbosity=2) |
|
114
|
|
|
|