summaryrefslogtreecommitdiff
path: root/tests/scanner/annotationparser/test_parser.py
blob: ce3ccd22b4fe2b0821b6209d17e3c7730ec0da1d (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
289
290
291
292
293
294
295
296
297
298
# -*- Mode: Python -*-
# GObject-Introspection - a framework for introspecting GObject libraries
# Copyright (C) 2012 Dieter Verfaillie <dieterv@optionexplicit.be>
#
# 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.
#


'''
test_parser.py

Tests ensuring the "parse tree" built by annotationparser.py
continues to function correctly.
'''


import difflib
import os
import xml.etree.ElementTree as etree
import unittest

from giscanner.annotationparser import AnnotationParser
from giscanner.ast import Namespace
from giscanner.message import MessageLogger


def parsed2tree(docblock):
    parsed = ''

    if docblock is not None:
        parsed += '<docblock>\n'

        parsed += '  <identifier>\n'
        # An identifier name is always required, but we can't trust our
        # own parser to ensure this when testing so fall back to an empty
        # string when no name has been parsed...
        parsed += '    <name>%s</name>\n' % (docblock.name or '', )
        if docblock.options.values:
            parsed += '    <annotations>\n'
            for key, value in docblock.options.values:
                parsed += '      <annotation>\n'
                parsed += '        <name>%s</name>\n' % (key, )
                if value is not None:
                    options = value.all()
                    parsed += '        <options>\n'
                    for option in options:
                        parsed += '          <option>\n'
                        parsed += '            <name>%s</name>\n' % (option, )
                        if options[option] is not None:
                            parsed += '            <value>%s</value>\n' % (options[option], )
                        parsed += '          </option>\n'
                    parsed += '        </options>\n'
                parsed += '      </annotation>\n'
            parsed += '    </annotations>\n'
        parsed += '  </identifier>\n'

        if docblock.params:
            parsed += '  <parameters>\n'
            for param_name in docblock.params:
                param = docblock.params.get(param_name)
                parsed += '    <parameter>\n'
                parsed += '      <name>%s</name>\n' % (param_name, )
                if param.options.values:
                    parsed += '      <annotations>\n'
                    for key, value in param.options.values:
                        parsed += '        <annotation>\n'
                        parsed += '          <name>%s</name>\n' % (key, )
                        if value is not None:
                            options = value.all()
                            parsed += '          <options>\n'
                            for option in options:
                                parsed += '            <option>\n'
                                parsed += '              <name>%s</name>\n' % (option, )
                                if options[option] is not None:
                                    parsed += '              <value>%s</value>\n' % (options[option], )
                                parsed += '            </option>\n'
                            parsed += '          </options>\n'
                        parsed += '        </annotation>\n'
                    parsed += '      </annotations>\n'
                if param.comment or param.value:
                    parsed += '      <description>%s</description>\n' % (param.comment or param.value, )
                parsed += '    </parameter>\n'
            parsed += '  </parameters>\n'

        if docblock.comment or docblock.value:
            parsed += '  <description>%s</description>\n' % (docblock.comment or docblock.value, )

        if docblock.tags:
            parsed += '  <tags>\n'
            for tag_name in docblock.tags:
                tag = docblock.tags.get(tag_name)
                parsed += '    <tag>\n'
                parsed += '      <name>%s</name>\n' % (tag_name, )
                if tag.options.values:
                    parsed += '      <annotations>\n'
                    for key, value in tag.options.values:
                        parsed += '        <annotation>\n'
                        parsed += '          <name>%s</name>\n' % (key, )
                        if value is not None:
                            options = value.all()
                            parsed += '          <options>\n'
                            for option in options:
                                parsed += '            <option>\n'
                                parsed += '              <name>%s</name>\n' % (option, )
                                if options[option] is not None:
                                    parsed += '              <value>%s</value>\n' % (options[option], )
                                parsed += '            </option>\n'
                            parsed += '          </options>\n'
                        parsed += '        </annotation>\n'
                    parsed += '      </annotations>\n'
                if tag.comment or tag.value:
                    parsed += '      <description>%s</description>\n' % (tag.comment or tag.value, )
                parsed += '    </tag>\n'
            parsed += '  </tags>\n'

        parsed += '<docblock>'

    return parsed


def expected2tree(docblock):
    # Note: this sucks, but we can't rely on etree.tostring() to generate useable output :(

    expected = ''

    if docblock is not None:
        expected += '<docblock>\n'

        if docblock.find('identifier') is not None:
            expected += '  <identifier>\n'
            # Expecting an identifier name is required, don't bother checking if it's there or not
            expected += '    <name>%s</name>\n' % (docblock.find('identifier/name').text, )
            annotations = docblock.find('identifier/annotations')
            if annotations is not None:
                expected += '    <annotations>\n'
                for annotation in annotations.findall('annotation'):
                    expected += '      <annotation>\n'
                    expected += '        <name>%s</name>\n' % (annotation.find('name').text, )
                    if annotation.find('options') is not None:
                        expected += '        <options>\n'
                        for option in annotation.findall('options/option'):
                            expected += '          <option>\n'
                            expected += '            <name>%s</name>\n' % (option.find('name').text, )
                            if option.find('value') is not None:
                                expected += '            <value>%s</value>\n' % (option.find('value').text, )
                            expected += '          </option>\n'
                        expected += '        </options>\n'
                    expected += '      </annotation>\n'
                expected += '    </annotations>\n'
            expected += '  </identifier>\n'

        parameters = docblock.find('parameters')
        if parameters is not None:
            expected += '  <parameters>\n'
            for parameter in parameters.findall('parameter'):
                expected += '    <parameter>\n'
                expected += '      <name>%s</name>\n' % (parameter.find('name').text, )
                annotations = parameter.find('annotations')
                if annotations is not None:
                    expected += '      <annotations>\n'
                    for annotation in parameter.findall('annotations/annotation'):
                        expected += '        <annotation>\n'
                        expected += '          <name>%s</name>\n' % (annotation.find('name').text, )
                        if annotation.find('options') is not None:
                            expected += '          <options>\n'
                            for option in annotation.findall('options/option'):
                                expected += '            <option>\n'
                                expected += '              <name>%s</name>\n' % (option.find('name').text, )
                                if option.find('value') is not None:
                                    expected += '              <value>%s</value>\n' % (option.find('value').text, )
                                expected += '            </option>\n'
                            expected += '          </options>\n'
                        expected += '        </annotation>\n'
                    expected += '      </annotations>\n'
                if parameter.find('description') is not None:
                    expected += '      <description>%s</description>\n' % (parameter.find('description').text, )
                expected += '    </parameter>\n'
            expected += '  </parameters>\n'

        description = docblock.find('description')
        if description is not None:
            expected += '  <description>%s</description>\n' % (description.text, )

        tags = docblock.find('tags')
        if tags is not None:
            expected += '  <tags>\n'
            for tag in tags.findall('tag'):
                expected += '    <tag>\n'
                expected += '      <name>%s</name>\n' % (tag.find('name').text, )
                annotations = tag.find('annotations')
                if annotations is not None:
                    expected += '      <annotations>\n'
                    for annotation in tag.findall('annotations/annotation'):
                        expected += '        <annotation>\n'
                        expected += '          <name>%s</name>\n' % (annotation.find('name').text, )
                        if annotation.find('options') is not None:
                            expected += '          <options>\n'
                            for option in annotation.findall('options/option'):
                                expected += '            <option>\n'
                                expected += '              <name>%s</name>\n' % (option.find('name').text, )
                                if option.find('value') is not None:
                                    expected += '              <value>%s</value>\n' % (option.find('value').text, )
                                expected += '            </option>\n'
                            expected += '          </options>\n'
                        expected += '        </annotation>\n'
                    expected += '      </annotations>\n'
                if tag.find('description') is not None:
                    expected += '      <description>%s</description>\n' % (tag.find('description').text, )
                expected += '    </tag>\n'
            expected += '  </tags>\n'

        expected += '<docblock>'

    return expected


def create_tests(tests_dir, tests_file):
    tests_name = os.path.relpath(tests_file[:-4], tests_dir)
    tests_name = tests_name.replace('/', '.').replace('\\', '.')

    tests_tree = etree.parse(tests_file).getroot()

    fix_cdata_elements = tests_tree.findall('test/commentblock')
    fix_cdata_elements += tests_tree.findall('.//description')

    for element in fix_cdata_elements:
        if element.text:
            element.text = element.text.replace('{{?', '<!')
            element.text = element.text.replace('}}', '>')

    for counter, test in enumerate(tests_tree.findall('test')):
        test_name = 'test_%s.%03d' % (tests_name, counter + 1)
        test_method = TestCommentBlock.__create_test__(test)
        setattr(TestCommentBlock, test_name, test_method)


class TestCommentBlock(unittest.TestCase):
    @classmethod
    def __create_test__(cls, testcase):
        def do_test(self):
            # Parse GTK-Doc comment block
            commentblock = testcase.find('commentblock').text
            parsed_docblock = AnnotationParser().parse_comment_block((commentblock, 'test.c', 1))
            parsed_tree = parsed2tree(parsed_docblock).split('\n')

            # Get expected output
            expected_docblock = testcase.find('docblock')
            expected_tree = expected2tree(expected_docblock).split('\n')

            # Construct a meaningful message
            msg = 'Parsed DocBlock object tree does not match expected output:\n\n'
            msg += '%s\n\n' % (commentblock, )

            diff = difflib.unified_diff(expected_tree, parsed_tree,
                                        'Expected DocBlock', 'Parsed DocBlock',
                                        n=max(len(expected_tree), len(parsed_tree)),
                                        lineterm='')
            for line in diff:
                msg += '%s\n' % (line, )

            # Compare parsed with expected DocBlock tree
            self.assertEqual(parsed_tree, expected_tree, msg)

        return do_test


if __name__ == '__main__':
    # Initialize message logger
    # TODO: at some point it might be a good idea to test warnings emitted
    # by annotationparser here, instead of having them in tests/warn/annotationparser.h?
    namespace = Namespace('Test', '1.0')
    logger = MessageLogger.get(namespace=namespace)
    logger.enable_warnings(False)

    # Load test cases from disc
    tests_dir = os.path.dirname(os.path.abspath(__file__))

    for dirpath, dirnames, filenames in os.walk(tests_dir):
        for filename in filenames:
            tests_file = os.path.join(dirpath, filename)
            if os.path.basename(tests_file).endswith('.xml'):
                create_tests(tests_dir, tests_file)

    # Run test suite
    unittest.main()