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