summaryrefslogtreecommitdiff
path: root/tests/mkdb.py
blob: 3f04fba684a0e400a88cee41fcaa9b17f262a15c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# -*- python -*-
#
# gtk-doc - GTK DocBook documentation generator.
# Copyright (C) 2018  Stefan Sauer
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#

import textwrap
import unittest

from gtkdoc import mkdb


class ScanSourceContentTestCase(unittest.TestCase):
    """Baseclass for the source scanner tests."""

    def setUp(self):
        mkdb.MODULE = 'test'
        mkdb.SymbolDocs = {}


class ScanSourceContent(ScanSourceContentTestCase):

    def test_EmptyInput(self):
        blocks = mkdb.ScanSourceContent([])
        self.assertEqual(0, len(blocks))

    def test_SkipsSingleLineComment(self):
        blocks = mkdb.ScanSourceContent("/** foo */")
        self.assertEqual(0, len(blocks))

    def test_FindsSingleDocComment(self):
        blocks = mkdb.ScanSourceContent("""\
            /**
             * symbol:
             *
             * Description.
             */""".splitlines(keepends=True))
        self.assertEqual(1, len(blocks))


class ParseCommentBlock(ScanSourceContentTestCase):

    def test_EmptyInput(self):
        mkdb.ParseCommentBlock([])
        self.assertEqual({}, mkdb.SourceSymbolDocs)

    def test_FindsDocComment(self):
        mkdb.ParseCommentBlock(textwrap.dedent("""\
             symbol:

             Description.
             """).splitlines(keepends=True))
        self.assertEqual({'symbol': 'Description.\n'}, mkdb.SourceSymbolDocs)

    def test_FindsDocCommentForSignal(self):
        mkdb.SourceSymbolDocs = {}
        mkdb.ParseCommentBlock(textwrap.dedent("""\
             Class::signal-with-dashes:

             Description.
             """).splitlines(keepends=True))
        self.assertEqual({'Class::signal-with-dashes': 'Description.\n'}, mkdb.SourceSymbolDocs)

    def test_FindsDocCommentForProperty(self):
        mkdb.SourceSymbolDocs = {}
        mkdb.ParseCommentBlock(textwrap.dedent("""\
             Class:property-with-dashes:

             Description.
             """).splitlines(keepends=True))
        self.assertEqual({'Class:property-with-dashes': 'Description.\n'}, mkdb.SourceSymbolDocs)

    def test_FindsDocCommentForActions(self):
        mkdb.SourceSymbolDocs = {}
        mkdb.ParseCommentBlock(textwrap.dedent("""\
             Class|action.with.dots-and-dashes:

             Description.
             """).splitlines(keepends=True))
        self.assertEqual({'Class|action.with.dots-and-dashes': 'Description.\n'}, mkdb.SourceSymbolDocs)

    def test_FindsDocCommentWithParam(self):
        mkdb.SourceSymbolDocs = {}
        mkdb.ParseCommentBlock(textwrap.dedent("""\
             symbol:
             @par: value

             Description.
             """).splitlines(keepends=True))
        self.assertEqual({'symbol': 'Description.\n'}, mkdb.SourceSymbolDocs)
        self.assertIn('symbol', mkdb.SourceSymbolParams)
        self.assertEqual({'par': 'value\n'}, mkdb.SourceSymbolParams['symbol'])

    def test_FindsDocCommentWithMultilineParam(self):
        mkdb.SourceSymbolDocs = {}
        mkdb.ParseCommentBlock(textwrap.dedent("""\
             symbol:
             @par: value docs with
               two lines

             Description.
             """).splitlines(keepends=True))
        self.assertEqual({'symbol': 'Description.\n'}, mkdb.SourceSymbolDocs)
        self.assertIn('symbol', mkdb.SourceSymbolParams)
        self.assertEqual({'par': 'value docs with\ntwo lines\n'}, mkdb.SourceSymbolParams['symbol'])

    def test_FindsDocCommentWithReturns(self):
        mkdb.SourceSymbolDocs = {}
        mkdb.ParseCommentBlock(textwrap.dedent("""\
             symbol:

             Description.

             Returns: result
             """).splitlines(keepends=True))
        # TODO: trim multiple newlines in code
        self.assertEqual({'symbol': 'Description.\n\n'}, mkdb.SourceSymbolDocs)
        self.assertIn('symbol', mkdb.SourceSymbolParams)
        # TODO: trim whitespace in code
        self.assertEqual({'Returns': ' result\n'}, mkdb.SourceSymbolParams['symbol'])

    def test_FindsDocCommentWithSince(self):
        mkdb.SourceSymbolDocs = {}
        mkdb.ParseCommentBlock(textwrap.dedent("""\
             symbol:

             Since: 0.1
             """).splitlines(keepends=True))
        self.assertIn('symbol', mkdb.Since)
        self.assertEqual('0.1', mkdb.Since['symbol'])

    def test_FindsDocCommentWithDeprecated(self):
        mkdb.SourceSymbolDocs = {}
        mkdb.ParseCommentBlock(textwrap.dedent("""\
             symbol:

             Deprecated: use function() instead
             """).splitlines(keepends=True))
        self.assertIn('symbol', mkdb.Deprecated)
        # TODO: trim whitespace in code
        self.assertEqual(' use function() instead\n', mkdb.Deprecated['symbol'])

    def test_FindsDocCommentWithStability(self):
        mkdb.SourceSymbolDocs = {}
        mkdb.ParseCommentBlock(textwrap.dedent("""\
             symbol:

             Stability: stable
             """).splitlines(keepends=True))
        self.assertIn('symbol', mkdb.StabilityLevel)
        self.assertEqual('Stable', mkdb.StabilityLevel['symbol'])

    def test_HandlesHTMLEntities(self):
        mkdb.SourceSymbolDocs = {}
        mkdb.ParseCommentBlock(textwrap.dedent("""\
             symbol:

             < & >.
             """).splitlines(keepends=True))
        self.assertEqual({'symbol': '&lt; &amp; &gt;.\n'}, mkdb.SourceSymbolDocs)


class ParseSectionCommentBlock(ScanSourceContentTestCase):

    def test_FindsSectionBlock(self):
        # TODO: maybe override common.LogWarning() instead and capture the messages
        # Suppress: 'Section symbol is not defined in the test-sections.txt file'
        mkdb.KnownSymbols['symbol:long_description'] = 1
        mkdb.ParseCommentBlock(textwrap.dedent("""\
             SECTION:symbol
             @short_description: short module description

             Module description.
             """).splitlines(keepends=True))
        self.assertIn('symbol:short_description', mkdb.SourceSymbolDocs)
        self.assertEqual('short module description\n', mkdb.SourceSymbolDocs['symbol:short_description'])
        self.assertIn('symbol:long_description', mkdb.SourceSymbolDocs)
        self.assertEqual('Module description.\n', mkdb.SourceSymbolDocs['symbol:long_description'])

    # TODO(ensonic): we need to refactor the code first (see comment there)
    # def test_FindsProgramBlock(self):
    #     mkdb.ParseCommentBlock(textwrap.dedent("""\
    #         PROGRAM:symbol
    #         @short_description: short program description
    #         @synopsis: test-program [*OPTIONS*...] --arg1 *arg* *FILE*
    #         @see_also: test(1)
    #         @--arg1 *arg*: set arg1 to *arg*
    #         @-v, --version: Print the version number
    #         @-h, --help: Print the help message
    #
    #         Program description.
    #          """).splitlines(keepends=True))
    #     self.assertIn('symbol:short_description', mkdb.SourceSymbolDocs)
    #     self.assertEqual('short program description\n', mkdb.SourceSymbolDocs['symbol:short_description'])
    #     self.assertIn('symbol:long_description', mkdb.SourceSymbolDocs)
    #     self.assertEqual('Program description.\n', mkdb.SourceSymbolDocs['symbol:long_description'])


class ScanSourceContentAnnotations(ScanSourceContentTestCase):

    def test_ParamAnnotation(self):
        mkdb.ParseCommentBlock(textwrap.dedent("""\
             symbol:
             @par: (allow-none): value

             description.
             """).splitlines(keepends=True))
        # TODO: we only extract those when outputting docbook, thats silly
        # self.assertEqual({'par': 'value\n'}, mkdb.SourceSymbolParams['symbol'])
        self.assertEqual({}, mkdb.SymbolAnnotations)

    def test_RetunsAnnotation(self):
        mkdb.ParseCommentBlock(textwrap.dedent("""\
             symbol:

             description.

             Returns: (transfer full) result.
             """).splitlines(keepends=True))
        # TODO: we only extract those when outputting docbook, thats silly
        self.assertEqual({}, mkdb.SymbolAnnotations)

    # multiple annotations, multiline annotations, symbol-level ...


class OutputStruct(unittest.TestCase):

    def test_SimpleStructGetNormalized(self):
        res = mkdb.extract_struct_body('data', textwrap.dedent("""\
            struct data
            {
                int i;
                char *c;
            };
            """), False, True)
        self.assertEqual(textwrap.dedent("""\
            struct data {
                int i;
                char *c;
            };
            """), res)

    def test_SimpleTypedefStructGetNormalized(self):
        res = mkdb.extract_struct_body('data', textwrap.dedent("""\
            typedef struct _data
            {
                int i;
                char *c;
            } data;
            """), True, True)
        self.assertEqual(textwrap.dedent("""\
            typedef struct {
                int i;
                char *c;
            } data;
            """), res)

    def test_InternalStructNameIsNormalized(self):
        res = mkdb.extract_struct_body('data', textwrap.dedent("""\
            struct _data {
                int i;
                char *c;
            };
            """), False, True)
        self.assertEqual(textwrap.dedent("""\
            struct data {
                int i;
                char *c;
            };
            """), res)


if __name__ == '__main__':
    unittest.main()