Completed
Pull Request — master (#1098)
by Mischa
02:19
created

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

Complexity

Total Complexity 14

Size/Duplication

Total Lines 195
Duplicated Lines 0 %
Metric Value
dl 0
loc 195
rs 10
wmc 14

6 Methods

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