|
1
|
|
|
from pathlib import Path |
|
2
|
|
|
from unittest import TestCase |
|
3
|
|
|
|
|
4
|
|
|
from barentsz._discover import ( |
|
5
|
|
|
discover_attributes, |
|
6
|
|
|
_match_attribute, |
|
7
|
|
|
_find_attribute_docstring, |
|
8
|
|
|
) |
|
9
|
|
|
from test_resources.examples_for_tests import module1 |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class TestDiscoverClasses(TestCase): |
|
13
|
|
|
|
|
14
|
|
|
def test_match_attribute(self): |
|
15
|
|
|
# EXECUTE |
|
16
|
|
|
match1 = _match_attribute(' some_attr = 2 ') |
|
17
|
|
|
match2 = _match_attribute(' some_attr : int = 2 ') |
|
18
|
|
|
match3 = _match_attribute( |
|
19
|
|
|
' some_attr : int = 2 # bla bla bla! ') |
|
20
|
|
|
match4 = _match_attribute( |
|
21
|
|
|
' some_attr int = 2 # bla bla bla! ') |
|
22
|
|
|
match5 = _match_attribute( |
|
23
|
|
|
' some attr = 2 # bla bla bla! ') |
|
24
|
|
|
match6 = _match_attribute( |
|
25
|
|
|
'some attr == 2') |
|
26
|
|
|
|
|
27
|
|
|
# VERIFY |
|
28
|
|
|
self.assertTupleEqual(('some_attr', None, '2', None), match1) |
|
29
|
|
|
self.assertTupleEqual(('some_attr', 'int', '2', None), match2) |
|
30
|
|
|
self.assertTupleEqual(('some_attr', 'int', '2', 'bla bla bla!'), match3) |
|
31
|
|
|
self.assertEqual(None, match4) |
|
32
|
|
|
self.assertEqual(None, match5) |
|
33
|
|
|
self.assertEqual(None, match6) |
|
34
|
|
|
|
|
35
|
|
|
def test_discover_attributes_in_path(self): |
|
36
|
|
|
# SETUP |
|
37
|
|
|
path_to_resources = (Path(__file__).parent.parent / 'test_resources' |
|
38
|
|
|
/ 'examples_for_tests') |
|
39
|
|
|
|
|
40
|
|
|
# EXECUTE |
|
41
|
|
|
attributes = discover_attributes(path_to_resources) |
|
42
|
|
|
attribute_names = [attribute.name for attribute in attributes] |
|
43
|
|
|
|
|
44
|
|
|
# VERIFY |
|
45
|
|
|
self.assertEqual(2, len(attributes)) |
|
46
|
|
|
self.assertListEqual(['ATTR1', 'ATTR1'], attribute_names) |
|
47
|
|
|
|
|
48
|
|
|
def test_discover_attributes_in_module(self): |
|
49
|
|
|
# EXECUTE |
|
50
|
|
|
attributes = discover_attributes(module1) |
|
51
|
|
|
|
|
52
|
|
|
# VERIFY |
|
53
|
|
|
self.assertEqual(1, len(attributes)) |
|
54
|
|
|
self.assertEqual('ATTR1', attributes[0].name) |
|
55
|
|
|
self.assertEqual(int, attributes[0].type_) |
|
56
|
|
|
self.assertEqual(42, attributes[0].value) |
|
57
|
|
|
self.assertEqual('Lets put some\ncomments for ATTR1 here\n\nwith ' |
|
58
|
|
|
'multiple lines...', attributes[0].doc) |
|
59
|
|
|
self.assertEqual('And some more comments here...', attributes[0].comment) |
|
60
|
|
|
|
|
61
|
|
|
def test_discover_attributes_in_private_modules(self): |
|
62
|
|
|
# SETUP |
|
63
|
|
|
path_to_resources = (Path(__file__).parent.parent / 'test_resources' |
|
64
|
|
|
/ 'examples_for_tests') |
|
65
|
|
|
|
|
66
|
|
|
# EXECUTE |
|
67
|
|
|
attributes = discover_attributes(path_to_resources, in_private_modules=True) |
|
68
|
|
|
|
|
69
|
|
|
# VERIFY |
|
70
|
|
|
self.assertEqual(3, len(attributes)) |
|
71
|
|
|
self.assertTrue(all([attribute.is_public for attribute in attributes])) |
|
72
|
|
|
|
|
73
|
|
|
def test_discover_private_attributes(self): |
|
74
|
|
|
# SETUP |
|
75
|
|
|
path_to_resources = (Path(__file__).parent.parent / 'test_resources' |
|
76
|
|
|
/ 'examples_for_tests') |
|
77
|
|
|
|
|
78
|
|
|
# EXECUTE |
|
79
|
|
|
attributes = discover_attributes(path_to_resources, include_privates=True) |
|
80
|
|
|
|
|
81
|
|
|
# VERIFY |
|
82
|
|
|
self.assertEqual(4, len(attributes)) |
|
83
|
|
|
self.assertTrue(any([attribute.is_private for attribute in attributes])) |
|
84
|
|
|
|
|
85
|
|
|
def test_discover_attributes_with_signature(self): |
|
86
|
|
|
# SETUP |
|
87
|
|
|
path_to_resources = (Path(__file__).parent.parent / 'test_resources' |
|
88
|
|
|
/ 'examples_for_tests') |
|
89
|
|
|
|
|
90
|
|
|
# EXECUTE |
|
91
|
|
|
attributes_str = discover_attributes(path_to_resources, signature=str) |
|
92
|
|
|
attributes_int = discover_attributes(path_to_resources, signature=int) |
|
93
|
|
|
|
|
94
|
|
|
# VERIFY |
|
95
|
|
|
self.assertEqual(1, len(attributes_str)) |
|
96
|
|
|
self.assertEqual(1, len(attributes_int)) |
|
97
|
|
|
self.assertEqual(str, attributes_str[0].type_) |
|
98
|
|
|
self.assertEqual(int, attributes_int[0].type_) |
|
99
|
|
|
|
|
100
|
|
|
def test_discover_attributes_with_wrong_argument(self): |
|
101
|
|
|
# EXECUTE & VALIDATE |
|
102
|
|
|
with self.assertRaises(ValueError): |
|
103
|
|
|
discover_attributes(123) |
|
104
|
|
|
|
|
105
|
|
|
def test_find_docstring(self): |
|
106
|
|
|
# SETUP |
|
107
|
|
|
expected1 = 'Some\ndocstring...' |
|
108
|
|
|
lines1 = [ |
|
109
|
|
|
' """ \n', |
|
110
|
|
|
'\n', |
|
111
|
|
|
'Some\n' |
|
112
|
|
|
'docstring...\n', |
|
113
|
|
|
' """ \n', |
|
114
|
|
|
] |
|
115
|
|
|
expected2 = 'Another\ndocstring...' |
|
116
|
|
|
lines2 = [ |
|
117
|
|
|
" ''' Another\n", |
|
118
|
|
|
'docstring...\n', |
|
119
|
|
|
" ''' \n", |
|
120
|
|
|
] |
|
121
|
|
|
expected3 = 'A\ndocstring, that\nhovers a bit' |
|
122
|
|
|
lines3 = [ |
|
123
|
|
|
'"""A\n', |
|
124
|
|
|
'docstring, that\n', |
|
125
|
|
|
'hovers a bit"""\n', |
|
126
|
|
|
' \n', |
|
127
|
|
|
' \n', |
|
128
|
|
|
] |
|
129
|
|
|
expected4 = None |
|
130
|
|
|
lines4 = [ |
|
131
|
|
|
'""Almost a docstring"""' |
|
132
|
|
|
] |
|
133
|
|
|
expected5 = None |
|
134
|
|
|
lines5 = [ |
|
135
|
|
|
'"""Also almost..."""\n# Nope' |
|
136
|
|
|
] |
|
137
|
|
|
|
|
138
|
|
|
# EXECUTE |
|
139
|
|
|
docstring1 = _find_attribute_docstring(lines1) |
|
140
|
|
|
docstring2 = _find_attribute_docstring(lines2) |
|
141
|
|
|
docstring3 = _find_attribute_docstring(lines3) |
|
142
|
|
|
docstring4 = _find_attribute_docstring(lines4) |
|
143
|
|
|
docstring5 = _find_attribute_docstring(lines5) |
|
144
|
|
|
|
|
145
|
|
|
# VERIFY |
|
146
|
|
|
self.assertEqual(expected1, docstring1) |
|
147
|
|
|
self.assertEqual(expected2, docstring2) |
|
148
|
|
|
self.assertEqual(expected3, docstring3) |
|
149
|
|
|
self.assertEqual(expected4, docstring4) |
|
150
|
|
|
self.assertEqual(expected5, docstring5) |
|
151
|
|
|
|