Completed
Pull Request — master (#1098)
by Mischa
01:49
created

coalib.tests.bearlib.languages.documentation.DocumentationExtractionTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 213
Duplicated Lines 0 %
Metric Value
dl 0
loc 213
rs 10
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
B test_extract_documentation_CPP() 0 43 2
A test_extract_documentation_invalid_input() 0 3 2
A test_extract_documentation_with_docstyle_invalid_input() 0 21 4
A load_testdata() 0 8 2
A test_extract_documentation_C() 0 48 2
A test_extract_documentation_CPP_2() 0 12 1
A test_extract_documentation_PYTHON3() 0 71 2
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(filename):
49
        filename = (os.path.dirname(os.path.realpath(__file__)) +
50
                    "/documentation_extraction_testdata/" + filename)
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("data.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("data.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
                              " ends instantly",
136
                              docstyle_CPP_doxygen,
137
                              docstyle_CPP_doxygen.markers[0],
138
                              TextRange.from_values(26, 5, 26, 26)),
139
                          DocumentationComment(
140
                              (" Should work\n"
141
                               "\n"
142
                               " even without a function standing below.\n"
143
                               "\n"
144
                               " @param foo WHAT PARAM PLEASE!?\n"),
145
                              docstyle_CPP_doxygen,
146
                              docstyle_CPP_doxygen.markers[4],
147
                              TextRange.from_values(32, 1, 36, 36))))
148
149
    def test_extract_documentation_CPP_2(self):
150
        data = DocumentationExtractionTest.load_testdata("data2.cpp")
151
152
        docstyle_CPP_doxygen = DocstyleDefinition.load("CPP", "doxygen")
153
154
        self.assertEqual(tuple(extract_documentation(data, "CPP", "doxygen")),
155
                         (DocumentationComment(
156
                              ("module comment\n"
157
                               " hello world\n"),
158
                              docstyle_CPP_doxygen,
159
                              docstyle_CPP_doxygen.markers[0],
160
                              TextRange.from_values(1, 1, 3, 4)),))
161
162
    def test_extract_documentation_PYTHON3(self):
163
        data = DocumentationExtractionTest.load_testdata("data.py")
164
165
        docstyle_PYTHON3_default = DocstyleDefinition.load("PYTHON3",
166
                                                           "default")
167
        docstyle_PYTHON3_doxygen = DocstyleDefinition.load("PYTHON3",
168
                                                           "doxygen")
169
170
        expected = (DocumentationComment(
171
                        ("\n"
172
                         "Module description.\n"
173
                         "\n"
174
                         "Some more foobar-like text.\n"),
175
                        docstyle_PYTHON3_default,
176
                        docstyle_PYTHON3_default.markers[0],
177
                        TextRange.from_values(1, 1, 5, 4)),
178
                    DocumentationComment(
179
                        ("\n"
180
                         "A nice and neat way of documenting code.\n"
181
                         ":param radius: The explosion radius.\n"),
182
                        docstyle_PYTHON3_default,
183
                        docstyle_PYTHON3_default.markers[0],
184
                        TextRange.from_values(8, 5, 11, 8)),
185
                    DocumentationComment(
186
                        ("\n"
187
                         "Docstring with layouted text.\n"
188
                         "\n"
189
                         "    layouts inside docs are preserved for these "
190
                         "documentation styles.\n"
191
                         "this is intended.\n"),
192
                        docstyle_PYTHON3_default,
193
                        docstyle_PYTHON3_default.markers[0],
194
                        TextRange.from_values(14, 1, 19, 4)),
195
                    DocumentationComment(
196
                        (" Docstring directly besides triple quotes.\n"
197
                         "    Continues here. "),
198
                        docstyle_PYTHON3_default,
199
                        docstyle_PYTHON3_default.markers[0],
200
                        TextRange.from_values(21, 1, 22, 24)),
201
                    DocumentationComment(
202
                        ("super\n"
203
                         " nicely\n"
204
                         "short"),
205
                        docstyle_PYTHON3_default,
206
                        docstyle_PYTHON3_default.markers[0],
207
                        TextRange.from_values(35, 1, 37, 9)))
208
209
        self.assertEqual(
210
            tuple(extract_documentation(data, "PYTHON3", "default")),
211
            expected)
212
213
        # Change only the docstyle in expected results.
214
        expected = list(DocumentationComment(r.documentation,
215
                                             docstyle_PYTHON3_doxygen,
216
                                             r.marker,
217
                                             r.range)
218
                        for r in expected)
219
220
        expected.insert(4, DocumentationComment(
221
            (" Alternate documentation style in doxygen.\n"
222
             "  Subtext\n"
223
             " More subtext (not correctly aligned)\n"
224
             "      sub-sub-text\n"
225
             "\n"),
226
            docstyle_PYTHON3_doxygen,
227
            docstyle_PYTHON3_doxygen.markers[1],
228
            TextRange.from_values(25, 1, 29, 3)))
229
230
        self.assertEqual(
231
            list(extract_documentation(data, "PYTHON3", "doxygen")),
232
            expected)
233
234
235
if __name__ == '__main__':
236
    unittest.main(verbosity=2)
237