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