Failed Conditions
Pull Request — master (#1098)
by Mischa
01:51
created

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

Complexity

Total Complexity 14

Size/Duplication

Total Lines 188
Duplicated Lines 0 %
Metric Value
dl 0
loc 188
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
B test_extract_documentation_CPP() 0 38 2
A load_testdata() 0 8 2
B test_extract_documentation_C() 0 41 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):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
        self.assertEqual(tuple(extract_documentation(data, "C", "doxygen")),
66
                         (DocumentationComment(
67
                              ("\n"
68
                               " This is the main function.\n"
69
                               "\n"
70
                               " @returns Your favorite number.\n"),
71
                              docstyle_C_doxygen,
72
                              docstyle_C_doxygen.markers[0],
73
                              TextRange.from_values(3, 1, 7, 4)),
74
                          DocumentationComment(
75
                              ("\n"
76
                               " Preserves alignment\n"
77
                               " - Main item\n"
78
                               "   - sub item\n"
79
                               "     - sub sub item\n"),
80
                              docstyle_C_doxygen,
81
                              docstyle_C_doxygen.markers[2],
82
                              TextRange.from_values(15, 1, 20, 4)),
83
                          DocumentationComment(
84
                              (" ABC\n"
85
                               "    Another type of comment\n"
86
                               "\n"
87
                               "    ..."),
88
                              docstyle_C_doxygen,
89
                              docstyle_C_doxygen.markers[1],
90
                              TextRange.from_values(23, 1, 26, 11)),
91
                          DocumentationComment(
92
                              (" foobar = barfoo.\n"
93
                               " @param x whatever...\n"),
94
                              docstyle_C_doxygen,
95
                              docstyle_C_doxygen.markers[0],
96
                              TextRange.from_values(28, 1, 30, 4))))
97
98
    def test_extract_documentation_CPP(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
        data = DocumentationExtractionTest.load_testdata(".cpp")
100
101
        # No built-in documentation for C++.
102
        with self.assertRaises(KeyError):
103
            tuple(extract_documentation(data, "CPP", "default"))
104
105
        docstyle_CPP_doxygen = DocstyleDefinition.load("CPP", "doxygen")
106
107
        self.assertEqual(tuple(extract_documentation(data, "CPP", "doxygen")),
108
                         (DocumentationComment(
109
                              ("\n"
110
                               " This is the main function.\n"
111
                               " @returns Exit code.\n"
112
                               "          Or any other number.\n"),
113
                              docstyle_CPP_doxygen,
114
                              docstyle_CPP_doxygen.markers[0],
115
                              TextRange.from_values(4, 1, 8, 4)),
116
                          DocumentationComment(
117
                              (" foobar\n"
118
                               " @param xyz\n"),
119
                              docstyle_CPP_doxygen,
120
                              docstyle_CPP_doxygen.markers[0],
121
                              TextRange.from_values(15, 1, 17, 4)),
122
                          DocumentationComment(
123
                              " Some alternate style of documentation\n",
124
                              docstyle_CPP_doxygen,
125
                              docstyle_CPP_doxygen.markers[4],
126
                              TextRange.from_values(22, 1, 22, 43)),
127
                          DocumentationComment(
128
                              (" Should work\n"
129
                               "\n"
130
                               " even without a function standing below.\n"
131
                               "\n"
132
                               " @param foo WHAT PARAM PLEASE!?\n"),
133
                              docstyle_CPP_doxygen,
134
                              docstyle_CPP_doxygen.markers[4],
135
                              TextRange.from_values(26, 1, 30, 36))))
136
137
    def test_extract_documentation_PYTHON3(self):
138
        data = DocumentationExtractionTest.load_testdata(".py")
139
140
        docstyle_PYTHON3_default = DocstyleDefinition.load("PYTHON3",
141
                                                           "default")
142
        docstyle_PYTHON3_doxygen = DocstyleDefinition.load("PYTHON3",
143
                                                           "doxygen")
144
145
        expected = (DocumentationComment(
146
                        ("\n"
147
                         "Module description.\n"
148
                         "\n"
149
                         "Some more foobar-like text.\n"),
150
                        docstyle_PYTHON3_default,
151
                        docstyle_PYTHON3_default.markers[0],
152
                        TextRange.from_values(1, 1, 5, 4)),
153
                    DocumentationComment(
154
                        ("\n"
155
                         "A nice and neat way of documenting code.\n"
156
                         ":param radius: The explosion radius.\n"),
157
                        docstyle_PYTHON3_default,
158
                        docstyle_PYTHON3_default.markers[0],
159
                        TextRange.from_values(8, 5, 11, 8)),
160
                    DocumentationComment(
161
                        ("\n"
162
                         "Docstring with layouted text.\n"
163
                         "\n"
164
                         "    layouts inside docs are preserved for these "
165
                         "documentation styles.\n"
166
                         "this is intended.\n"),
167
                        docstyle_PYTHON3_default,
168
                        docstyle_PYTHON3_default.markers[0],
169
                        TextRange.from_values(14, 1, 19, 4)),
170
                    DocumentationComment(
171
                        (" Docstring directly besides triple quotes.\n"
172
                         "    Continues here. "),
173
                        docstyle_PYTHON3_default,
174
                        docstyle_PYTHON3_default.markers[0],
175
                        TextRange.from_values(21, 1, 22, 24)),
176
                    DocumentationComment(
177
                        ("super\n"
178
                         " nicely\n"
179
                         "short"),
180
                        docstyle_PYTHON3_default,
181
                        docstyle_PYTHON3_default.markers[0],
182
                        TextRange.from_values(35, 1, 37, 9)))
183
184
        self.assertEqual(
185
            tuple(extract_documentation(data, "PYTHON3", "default")),
186
            expected)
187
188
        # Change only the docstyle in expected results.
189
        expected = list(DocumentationComment(r.documentation,
190
                                             docstyle_PYTHON3_doxygen,
191
                                             r.marker,
192
                                             r.range)
193
                        for r in expected)
194
195
        expected.insert(4, DocumentationComment(
196
            (" Alternate documentation style in doxygen.\n"
197
             "  Subtext\n"
198
             " More subtext (not correctly aligned)\n"
199
             "      sub-sub-text\n"
200
             "\n"),
201
            docstyle_PYTHON3_doxygen,
202
            docstyle_PYTHON3_doxygen.markers[1],
203
            TextRange.from_values(25, 1, 29, 3)))
204
205
        self.assertEqual(
206
            list(extract_documentation(data, "PYTHON3", "doxygen")),
207
            expected)
208
209
210
if __name__ == '__main__':
211
    unittest.main(verbosity=2)
212