Failed Conditions
Pull Request — master (#1152)
by Lasse
03:36
created

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

Complexity

Total Complexity 14

Size/Duplication

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