1
|
|
|
import os.path |
2
|
|
|
import unittest |
3
|
|
|
import sys |
4
|
|
|
|
5
|
|
|
sys.path.insert(0, ".") |
6
|
|
|
from coalib.bearlib.languages.documentation.DocstyleDefinition import ( |
7
|
|
|
DocstyleDefinition) |
8
|
|
|
from coalib.bearlib.languages.documentation.DocumentationComment import ( |
9
|
|
|
DocumentationComment) |
10
|
|
|
from coalib.bearlib.languages.documentation.DocumentationExtraction import ( |
11
|
|
|
extract_documentation, |
12
|
|
|
extract_documentation_with_docstyle) |
13
|
|
|
from coalib.misc.Compatability import FileNotFoundError |
14
|
|
|
from coalib.results.TextRange import TextRange |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
class DocumentationExtractionTest(unittest.TestCase): |
18
|
|
|
def test_extract_documentation_with_docstyle_invalid_input(self): |
19
|
|
|
with self.assertRaises(ValueError): |
20
|
|
|
extract_documentation_with_docstyle( |
21
|
|
|
"", |
22
|
|
|
DocstyleDefinition("C", |
23
|
|
|
"default", |
24
|
|
|
[["A", "B", "C", "D"]])) |
25
|
|
|
|
26
|
|
|
with self.assertRaises(ValueError): |
27
|
|
|
extract_documentation_with_docstyle( |
28
|
|
|
"", |
29
|
|
|
DocstyleDefinition("C", |
30
|
|
|
"default", |
31
|
|
|
[["A", "B"]])) |
32
|
|
|
|
33
|
|
|
with self.assertRaises(ValueError): |
34
|
|
|
extract_documentation_with_docstyle( |
35
|
|
|
"", |
36
|
|
|
DocstyleDefinition("C", |
37
|
|
|
"default", |
38
|
|
|
[["A"]])) |
39
|
|
|
|
40
|
|
|
def test_extract_documentation_invalid_input(self): |
41
|
|
|
with self.assertRaises(FileNotFoundError): |
42
|
|
|
tuple(extract_documentation("", "PYTHON", "INVALID")) |
43
|
|
|
|
44
|
|
|
@staticmethod |
45
|
|
|
def load_testdata(filename): |
46
|
|
|
filename = (os.path.dirname(os.path.realpath(__file__)) + |
47
|
|
|
"/documentation_extraction_testdata/" + filename) |
48
|
|
|
with open(filename, "r") as fl: |
49
|
|
|
data = fl.read() |
50
|
|
|
|
51
|
|
|
return data |
52
|
|
|
|
53
|
|
|
def test_extract_documentation_C(self): |
54
|
|
|
data = DocumentationExtractionTest.load_testdata("data.c") |
55
|
|
|
|
56
|
|
|
# No built-in documentation for C. |
57
|
|
|
with self.assertRaises(KeyError): |
58
|
|
|
tuple(extract_documentation(data, "C", "default")) |
59
|
|
|
|
60
|
|
|
docstyle_C_doxygen = DocstyleDefinition.load("C", "doxygen") |
61
|
|
|
|
62
|
|
|
expected_results = (DocumentationComment( |
63
|
|
|
("\n" |
64
|
|
|
" This is the main function.\n" |
65
|
|
|
"\n" |
66
|
|
|
" @returns Your favorite number.\n"), |
67
|
|
|
docstyle_C_doxygen, |
68
|
|
|
docstyle_C_doxygen.markers[0], |
69
|
|
|
TextRange.from_values(3, 1, 7, 4)), |
70
|
|
|
DocumentationComment( |
71
|
|
|
("\n" |
72
|
|
|
" Preserves alignment\n" |
73
|
|
|
" - Main item\n" |
74
|
|
|
" - sub item\n" |
75
|
|
|
" - sub sub item\n"), |
76
|
|
|
docstyle_C_doxygen, |
77
|
|
|
docstyle_C_doxygen.markers[2], |
78
|
|
|
TextRange.from_values(15, 1, 20, 4)), |
79
|
|
|
DocumentationComment( |
80
|
|
|
(" ABC\n" |
81
|
|
|
" Another type of comment\n" |
82
|
|
|
"\n" |
83
|
|
|
" ..."), |
84
|
|
|
docstyle_C_doxygen, |
85
|
|
|
docstyle_C_doxygen.markers[1], |
86
|
|
|
TextRange.from_values(23, 1, 26, 11)), |
87
|
|
|
DocumentationComment( |
88
|
|
|
(" foobar = barfoo.\n" |
89
|
|
|
" @param x whatever...\n"), |
90
|
|
|
docstyle_C_doxygen, |
91
|
|
|
docstyle_C_doxygen.markers[0], |
92
|
|
|
TextRange.from_values(28, 1, 30, 4))) |
93
|
|
|
|
94
|
|
|
self.assertEqual(tuple(extract_documentation(data, "C", "doxygen")), |
95
|
|
|
expected_results) |
96
|
|
|
|
97
|
|
|
# Presplitted lines should also work. |
98
|
|
|
data = data.splitlines(keepends=True) |
99
|
|
|
self.assertEqual(tuple(extract_documentation(data, "C", "doxygen")), |
100
|
|
|
expected_results) |
101
|
|
|
|
102
|
|
|
def test_extract_documentation_CPP(self): |
103
|
|
|
data = DocumentationExtractionTest.load_testdata("data.cpp") |
104
|
|
|
|
105
|
|
|
# No built-in documentation for C++. |
106
|
|
|
with self.assertRaises(KeyError): |
107
|
|
|
tuple(extract_documentation(data, "CPP", "default")) |
108
|
|
|
|
109
|
|
|
docstyle_CPP_doxygen = DocstyleDefinition.load("CPP", "doxygen") |
110
|
|
|
|
111
|
|
|
self.assertEqual(tuple(extract_documentation(data, "CPP", "doxygen")), |
112
|
|
|
(DocumentationComment( |
113
|
|
|
("\n" |
114
|
|
|
" This is the main function.\n" |
115
|
|
|
" @returns Exit code.\n" |
116
|
|
|
" Or any other number.\n"), |
117
|
|
|
docstyle_CPP_doxygen, |
118
|
|
|
docstyle_CPP_doxygen.markers[0], |
119
|
|
|
TextRange.from_values(4, 1, 8, 4)), |
120
|
|
|
DocumentationComment( |
121
|
|
|
(" foobar\n" |
122
|
|
|
" @param xyz\n"), |
123
|
|
|
docstyle_CPP_doxygen, |
124
|
|
|
docstyle_CPP_doxygen.markers[0], |
125
|
|
|
TextRange.from_values(15, 1, 17, 4)), |
126
|
|
|
DocumentationComment( |
127
|
|
|
" Some alternate style of documentation\n", |
128
|
|
|
docstyle_CPP_doxygen, |
129
|
|
|
docstyle_CPP_doxygen.markers[4], |
130
|
|
|
TextRange.from_values(22, 1, 22, 43)), |
131
|
|
|
DocumentationComment( |
132
|
|
|
" ends instantly", |
133
|
|
|
docstyle_CPP_doxygen, |
134
|
|
|
docstyle_CPP_doxygen.markers[0], |
135
|
|
|
TextRange.from_values(26, 5, 26, 26)), |
136
|
|
|
DocumentationComment( |
137
|
|
|
(" Should work\n" |
138
|
|
|
"\n" |
139
|
|
|
" even without a function standing below.\n" |
140
|
|
|
"\n" |
141
|
|
|
" @param foo WHAT PARAM PLEASE!?\n"), |
142
|
|
|
docstyle_CPP_doxygen, |
143
|
|
|
docstyle_CPP_doxygen.markers[4], |
144
|
|
|
TextRange.from_values(32, 1, 36, 36)))) |
145
|
|
|
|
146
|
|
|
def test_extract_documentation_CPP_2(self): |
147
|
|
|
data = DocumentationExtractionTest.load_testdata("data2.cpp") |
148
|
|
|
|
149
|
|
|
docstyle_CPP_doxygen = DocstyleDefinition.load("CPP", "doxygen") |
150
|
|
|
|
151
|
|
|
self.assertEqual(tuple(extract_documentation(data, "CPP", "doxygen")), |
152
|
|
|
(DocumentationComment( |
153
|
|
|
("module comment\n" |
154
|
|
|
" hello world\n"), |
155
|
|
|
docstyle_CPP_doxygen, |
156
|
|
|
docstyle_CPP_doxygen.markers[0], |
157
|
|
|
TextRange.from_values(1, 1, 3, 4)),)) |
158
|
|
|
|
159
|
|
|
def test_extract_documentation_PYTHON3(self): |
160
|
|
|
data = DocumentationExtractionTest.load_testdata("data.py") |
161
|
|
|
|
162
|
|
|
docstyle_PYTHON3_default = DocstyleDefinition.load("PYTHON3", |
163
|
|
|
"default") |
164
|
|
|
docstyle_PYTHON3_doxygen = DocstyleDefinition.load("PYTHON3", |
165
|
|
|
"doxygen") |
166
|
|
|
|
167
|
|
|
expected = (DocumentationComment( |
168
|
|
|
("\n" |
169
|
|
|
"Module description.\n" |
170
|
|
|
"\n" |
171
|
|
|
"Some more foobar-like text.\n"), |
172
|
|
|
docstyle_PYTHON3_default, |
173
|
|
|
docstyle_PYTHON3_default.markers[0], |
174
|
|
|
TextRange.from_values(1, 1, 5, 4)), |
175
|
|
|
DocumentationComment( |
176
|
|
|
("\n" |
177
|
|
|
"A nice and neat way of documenting code.\n" |
178
|
|
|
":param radius: The explosion radius.\n"), |
179
|
|
|
docstyle_PYTHON3_default, |
180
|
|
|
docstyle_PYTHON3_default.markers[0], |
181
|
|
|
TextRange.from_values(8, 5, 11, 8)), |
182
|
|
|
DocumentationComment( |
183
|
|
|
("\n" |
184
|
|
|
"Docstring with layouted text.\n" |
185
|
|
|
"\n" |
186
|
|
|
" layouts inside docs are preserved for these " |
187
|
|
|
"documentation styles.\n" |
188
|
|
|
"this is intended.\n"), |
189
|
|
|
docstyle_PYTHON3_default, |
190
|
|
|
docstyle_PYTHON3_default.markers[0], |
191
|
|
|
TextRange.from_values(14, 1, 19, 4)), |
192
|
|
|
DocumentationComment( |
193
|
|
|
(" Docstring directly besides triple quotes.\n" |
194
|
|
|
" Continues here. "), |
195
|
|
|
docstyle_PYTHON3_default, |
196
|
|
|
docstyle_PYTHON3_default.markers[0], |
197
|
|
|
TextRange.from_values(21, 1, 22, 24)), |
198
|
|
|
DocumentationComment( |
199
|
|
|
("super\n" |
200
|
|
|
" nicely\n" |
201
|
|
|
"short"), |
202
|
|
|
docstyle_PYTHON3_default, |
203
|
|
|
docstyle_PYTHON3_default.markers[0], |
204
|
|
|
TextRange.from_values(35, 1, 37, 9))) |
205
|
|
|
|
206
|
|
|
self.assertEqual( |
207
|
|
|
tuple(extract_documentation(data, "PYTHON3", "default")), |
208
|
|
|
expected) |
209
|
|
|
|
210
|
|
|
# Change only the docstyle in expected results. |
211
|
|
|
expected = list(DocumentationComment(r.documentation, |
212
|
|
|
docstyle_PYTHON3_doxygen, |
213
|
|
|
r.marker, |
214
|
|
|
r.range) |
215
|
|
|
for r in expected) |
216
|
|
|
|
217
|
|
|
expected.insert(4, DocumentationComment( |
218
|
|
|
(" Alternate documentation style in doxygen.\n" |
219
|
|
|
" Subtext\n" |
220
|
|
|
" More subtext (not correctly aligned)\n" |
221
|
|
|
" sub-sub-text\n" |
222
|
|
|
"\n"), |
223
|
|
|
docstyle_PYTHON3_doxygen, |
224
|
|
|
docstyle_PYTHON3_doxygen.markers[1], |
225
|
|
|
TextRange.from_values(25, 1, 29, 3))) |
226
|
|
|
|
227
|
|
|
self.assertEqual( |
228
|
|
|
list(extract_documentation(data, "PYTHON3", "doxygen")), |
229
|
|
|
expected) |
230
|
|
|
|
231
|
|
|
|
232
|
|
|
if __name__ == '__main__': |
233
|
|
|
unittest.main(verbosity=2) |
234
|
|
|
|