Completed
Pull Request — master (#2423)
by
unknown
01:54
created

test_extract_documentation_C()   B

Complexity

Conditions 2

Size

Total Lines 44

Duplication

Lines 44
Ratio 100 %

Importance

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