1
|
|
|
import unittest |
2
|
|
|
|
3
|
|
|
from coalib.bearlib.languages.documentation.DocstyleDefinition import ( |
4
|
|
|
DocstyleDefinition) |
5
|
|
|
from coalib.bearlib.languages.documentation.DocumentationComment import ( |
6
|
|
|
DocumentationComment) |
7
|
|
|
from coalib.bearlib.languages.documentation.DocumentationExtraction import ( |
8
|
|
|
extract_documentation) |
9
|
|
|
from tests.bearlib.languages.documentation.TestUtils import ( |
10
|
|
|
load_testdata) |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class DocumentationCommentTest(unittest.TestCase): |
14
|
|
|
|
15
|
|
|
Description = DocumentationComment.Description |
16
|
|
|
Parameter = DocumentationComment.Parameter |
17
|
|
|
ReturnValue = DocumentationComment.ReturnValue |
18
|
|
|
|
19
|
|
|
Metadata = DocstyleDefinition.Metadata |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
class GeneralDocumentationCommentTest(DocumentationCommentTest): |
23
|
|
|
|
24
|
|
|
def test_fields(self): |
25
|
|
|
c_doxygen = DocstyleDefinition.load("C", "doxygen") |
26
|
|
|
uut = DocumentationComment("my doc", |
27
|
|
|
c_doxygen, |
28
|
|
|
" ", |
29
|
|
|
("/**", "*", "*/"), |
30
|
|
|
(25, 45)) |
31
|
|
|
|
32
|
|
|
self.assertEqual(uut.documentation, "my doc") |
33
|
|
|
self.assertEqual(uut.language, "c") |
34
|
|
|
self.assertEqual(uut.docstyle, "doxygen") |
35
|
|
|
self.assertEqual(uut.indent, " ") |
36
|
|
|
self.assertEqual(str(uut), "my doc") |
37
|
|
|
self.assertEqual(uut.marker, ("/**", "*", "*/")) |
38
|
|
|
self.assertEqual(uut.range, (25, 45)) |
39
|
|
|
|
40
|
|
|
python_doxygen = DocstyleDefinition.load("python", "doxygen") |
41
|
|
|
|
42
|
|
|
python_doxygen_metadata = self.Metadata("@param ", " ", "@return ") |
43
|
|
|
|
44
|
|
|
uut = DocumentationComment("qwertzuiop", |
45
|
|
|
python_doxygen, |
46
|
|
|
"\t", |
47
|
|
|
("##", "#", "#"), |
48
|
|
|
None) |
49
|
|
|
|
50
|
|
|
self.assertEqual(uut.documentation, "qwertzuiop") |
51
|
|
|
self.assertEqual(uut.language, "python") |
52
|
|
|
self.assertEqual(uut.docstyle, "doxygen") |
53
|
|
|
self.assertEqual(uut.indent, "\t") |
54
|
|
|
self.assertEqual(str(uut), "qwertzuiop") |
55
|
|
|
self.assertEqual(uut.marker, ("##", "#", "#")) |
56
|
|
|
self.assertEqual(uut.range, None) |
57
|
|
|
self.assertEqual(uut.metadata, python_doxygen_metadata) |
58
|
|
|
|
59
|
|
|
def test_not_implemented(self): |
60
|
|
|
raw_docstyle = DocstyleDefinition("nolang", "nostyle", ('', '', ''), |
61
|
|
|
self.Metadata('', '', '')) |
62
|
|
|
not_implemented = DocumentationComment( |
63
|
|
|
"some docs", raw_docstyle, None, None, None) |
64
|
|
|
with self.assertRaises(NotImplementedError): |
65
|
|
|
not_implemented.parse() |
66
|
|
|
|
67
|
|
|
def test_from_metadata(self): |
68
|
|
|
data = load_testdata("default.py") |
69
|
|
|
|
70
|
|
|
original = list(extract_documentation(data, "python", "default")) |
71
|
|
|
|
72
|
|
|
parsed_docs = [(doc.parse(), doc.marker, doc.indent, doc.range) |
73
|
|
|
for doc in original] |
74
|
|
|
|
75
|
|
|
docstyle_definition = DocstyleDefinition.load("python", "default") |
76
|
|
|
|
77
|
|
|
assembled_docs = [DocumentationComment.from_metadata( |
78
|
|
|
doc[0], docstyle_definition, doc[1], doc[2], doc[3]) |
79
|
|
|
for doc in parsed_docs] |
80
|
|
|
|
81
|
|
|
self.assertEqual(assembled_docs, original) |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
class PythonDocumentationCommentTest(DocumentationCommentTest): |
85
|
|
|
|
86
|
|
|
def check_docstring(self, docstring, expected=[]): |
87
|
|
|
self.assertIsInstance(docstring, |
88
|
|
|
str, |
89
|
|
|
"expected needs to be a string for this test.") |
90
|
|
|
|
91
|
|
|
self.assertIsInstance(expected, |
92
|
|
|
list, |
93
|
|
|
"expected needs to be a list for this test.") |
94
|
|
|
|
95
|
|
|
python_default = DocstyleDefinition.load("python", "default") |
96
|
|
|
|
97
|
|
|
doc_comment = DocumentationComment(docstring, python_default, |
98
|
|
|
None, None, None) |
99
|
|
|
parsed_metadata = doc_comment.parse() |
100
|
|
|
self.assertEqual(parsed_metadata, expected) |
101
|
|
|
|
102
|
|
|
def test_empty_docstring(self): |
103
|
|
|
self.check_docstring("", []) |
104
|
|
|
|
105
|
|
|
def test_description(self): |
106
|
|
|
doc = " description only " |
107
|
|
|
self.check_docstring(doc, [self.Description(desc=' description only ')]) |
108
|
|
|
|
109
|
|
|
def test_params_default(self): |
110
|
|
|
self.maxDiff = None |
111
|
|
|
doc = (" :param test: test description1 \n" |
112
|
|
|
" :param test: test description2 \n") |
113
|
|
|
expected = [self.Parameter(name='test', desc=' test description1 \n'), |
114
|
|
|
self.Parameter(name='test', desc=' test description2 \n')] |
115
|
|
|
self.check_docstring(doc, expected) |
116
|
|
|
|
117
|
|
|
def test_return_values_default(self): |
118
|
|
|
doc = (" :return: something1 \n" |
119
|
|
|
" :return: something2 ") |
120
|
|
|
expected = [self.ReturnValue(desc=' something1 \n'), |
121
|
|
|
self.ReturnValue(desc=' something2 ')] |
122
|
|
|
self.check_docstring(doc, expected) |
123
|
|
|
|
124
|
|
View Code Duplication |
def test_python_default(self): |
|
|
|
|
125
|
|
|
data = load_testdata("default.py") |
126
|
|
|
|
127
|
|
|
parsed_docs = [doc.parse() for doc in |
128
|
|
|
extract_documentation(data, "python", "default")] |
129
|
|
|
|
130
|
|
|
expected = [ |
131
|
|
|
[self.Description(desc='\nModule description.\n\n' |
132
|
|
|
'Some more foobar-like text.\n')], |
133
|
|
|
[self.Description(desc='\nA nice and neat way of ' |
134
|
|
|
'documenting code.\n'), |
135
|
|
|
self.Parameter(name='radius', desc=' The explosion radius. ')], |
136
|
|
|
[self.Description(desc='A function that returns 55.')], |
137
|
|
|
[self.Description(desc='\nDocstring with layouted text.\n\n ' |
138
|
|
|
'layouts inside docs are preserved.' |
139
|
|
|
'\nthis is intended.\n')], |
140
|
|
|
[self.Description(desc=' Docstring inline with triple quotes.\n' |
141
|
|
|
' Continues here. ')], |
142
|
|
|
[self.Description(desc='\nThis is the best docstring ever!\n\n'), |
143
|
|
|
self.Parameter(name='param1', |
144
|
|
|
desc='\n Very Very Long Parameter ' |
145
|
|
|
'description.\n'), |
146
|
|
|
self.Parameter(name='param2', |
147
|
|
|
desc='\n Short Param description.\n\n'), |
148
|
|
|
self.ReturnValue(desc=' Long Return Description That Makes No ' |
149
|
|
|
'Sense And Will\n Cut to the Next' |
150
|
|
|
' Line.\n')]] |
151
|
|
|
|
152
|
|
|
self.assertEqual(parsed_docs, expected) |
153
|
|
|
|
154
|
|
View Code Duplication |
def test_python_doxygen(self): |
|
|
|
|
155
|
|
|
data = load_testdata("doxygen.py") |
156
|
|
|
|
157
|
|
|
parsed_docs = [doc.parse() for doc in |
158
|
|
|
extract_documentation(data, "python", "doxygen")] |
159
|
|
|
|
160
|
|
|
expected = [ |
161
|
|
|
[self.Description(desc=' @package pyexample\n Documentation for' |
162
|
|
|
' this module.\n\n More details.\n')], |
163
|
|
|
[self.Description( |
164
|
|
|
desc=' Documentation for a class.\n\n More details.\n')], |
165
|
|
|
[self.Description(desc=' The constructor.\n')], |
166
|
|
|
[self.Description(desc=' Documentation for a method.\n'), |
167
|
|
|
self.Parameter(name='self', desc='The object pointer.\n')], |
168
|
|
|
[self.Description(desc=' A class variable.\n')], |
169
|
|
|
[self.Description(desc=' @var _memVar\n a member variable\n')], |
170
|
|
|
[self.Description(desc=' This is the best docstring ever!\n\n'), |
171
|
|
|
self.Parameter(name='param1', desc='Parameter 1\n'), |
172
|
|
|
self.Parameter(name='param2', desc='Parameter 2\n'), |
173
|
|
|
self.ReturnValue(desc='Nothing\n')]] |
174
|
|
|
|
175
|
|
|
self.assertEqual(parsed_docs, expected) |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
class JavaDocumentationCommentTest(DocumentationCommentTest): |
179
|
|
|
|
180
|
|
|
def test_java_default(self): |
181
|
|
|
data = load_testdata("default.java") |
182
|
|
|
|
183
|
|
|
parsed_docs = [doc.parse() for doc in |
184
|
|
|
extract_documentation(data, "java", "default")] |
185
|
|
|
|
186
|
|
|
expected = [[self.Description( |
187
|
|
|
desc='\n Returns an String that says Hello with the name' |
188
|
|
|
' argument.\n\n'), |
189
|
|
|
self.Parameter(name='name', |
190
|
|
|
desc='the name to which to say hello\n'), |
191
|
|
|
self.ReturnValue( |
192
|
|
|
desc=' the concatenated string\n')]] |
193
|
|
|
|
194
|
|
|
self.assertEqual(expected, parsed_docs) |
195
|
|
|
|
196
|
|
|
|
197
|
|
|
class DocumentationAssemblyTest(unittest.TestCase): |
198
|
|
|
|
199
|
|
|
def test_python_assembly(self): |
200
|
|
|
data = load_testdata("default.py") |
201
|
|
|
docs = "".join(data) |
202
|
|
|
|
203
|
|
|
for doc in extract_documentation(data, "python", "default"): |
204
|
|
|
self.assertIn(doc.assemble(), docs) |
205
|
|
|
|
206
|
|
|
def test_c_assembly(self): |
207
|
|
|
data = load_testdata("default.c") |
208
|
|
|
docs = "".join(data) |
209
|
|
|
|
210
|
|
|
for doc in extract_documentation(data, "c", "doxygen"): |
211
|
|
|
self.assertIn(doc.assemble(), docs) |
212
|
|
|
|