File: test_declarations.py

package info (click to toggle)
pygccxml 3.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,444 kB
  • sloc: xml: 29,841; python: 13,914; cpp: 2,671; makefile: 163; ansic: 59
file content (296 lines) | stat: -rw-r--r-- 9,234 bytes parent folder | download | duplicates (2)
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
# Copyright 2014-2017 Insight Software Consortium.
# Copyright 2004-2009 Roman Yakovenko.
# Distributed under the Boost Software License, Version 1.0.
# See http://www.boost.org/LICENSE_1_0.txt

import pytest

from . import autoconfig

from pygccxml import parser
from pygccxml import declarations

TEST_FILES = [
    "declarations_enums.hpp",
    "declarations_variables.hpp",
    "declarations_calldef.hpp",
]


@pytest.fixture
def global_ns_fixture_all_at_once():
    COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
    config = autoconfig.cxx_parsers_cfg.config.clone()
    decls = parser.parse(TEST_FILES, config, COMPILATION_MODE)
    global_ns = declarations.get_global_namespace(decls)
    return global_ns


@pytest.fixture
def global_ns_fixture_file_by_file():
    COMPILATION_MODE = parser.COMPILATION_MODE.FILE_BY_FILE
    config = autoconfig.cxx_parsers_cfg.config.clone()
    decls = parser.parse(TEST_FILES, config, COMPILATION_MODE)
    global_ns = declarations.get_global_namespace(decls)
    return global_ns


@pytest.fixture
def global_ns(request):
    return request.getfixturevalue(request.param)


@pytest.mark.parametrize(
    "global_ns",
    [
        "global_ns_fixture_all_at_once",
        "global_ns_fixture_file_by_file",
    ],
    indirect=True,
)
def test_enumeration_t(global_ns):
    enum = global_ns.enumeration("ENumbers")
    expected_values = list(
        zip(
            ["e%d" % index for index in range(10)],
            [index for index in range(10)]
        )
    )
    assert expected_values == enum.values


def test_namespace():
    pass  # tested in core_tester


def test_types():
    pass  # tested in core_tester


@pytest.mark.parametrize(
    "global_ns",
    [
        "global_ns_fixture_all_at_once",
        "global_ns_fixture_file_by_file",
    ],
    indirect=True,
)
def test_variables(global_ns, helpers):
    global_ns.namespace("variables")
    initialized = global_ns.variable(name="initialized")

    expected_value = "10122004"
    assert initialized.value == expected_value
    helpers._test_type_composition(
        initialized.decl_type,
        declarations.const_t,
        declarations.long_unsigned_int_t
    )

    m_mutable = global_ns.variable(name="m_mutable")
    assert m_mutable.type_qualifiers.has_static is False
    assert m_mutable.type_qualifiers.has_mutable is True

    # External static variable
    extern_var = global_ns.variable(name="extern_var")
    assert extern_var.type_qualifiers.has_extern is True
    assert extern_var.type_qualifiers.has_static is False
    assert extern_var.type_qualifiers.has_mutable is False

    # Static variable
    static_var = global_ns.variable(name="static_var")
    assert static_var.type_qualifiers.has_static is True
    assert static_var.type_qualifiers.has_extern is False
    assert static_var.type_qualifiers.has_mutable is False

    ssv_static_var = global_ns.variable(name="ssv_static_var")
    assert ssv_static_var.type_qualifiers.has_static is True
    assert ssv_static_var.type_qualifiers.has_extern is False
    assert ssv_static_var.type_qualifiers.has_mutable is False

    ssv_static_var_value = global_ns.variable(name="ssv_static_var_value")
    assert ssv_static_var_value.type_qualifiers.has_static is True
    assert ssv_static_var_value.type_qualifiers.has_extern is False
    assert ssv_static_var_value.type_qualifiers.has_mutable is False


@pytest.mark.parametrize(
    "global_ns",
    [
        "global_ns_fixture_all_at_once",
        "global_ns_fixture_file_by_file",
    ],
    indirect=True,
)
def test_calldef_free_functions(global_ns, helpers):
    ns = global_ns.namespace("calldef")

    no_return_no_args = ns.free_function("no_return_no_args")

    helpers._test_calldef_return_type(no_return_no_args, declarations.void_t)
    assert no_return_no_args.has_extern is False

    # Static_call is explicetely defined as extern, this works with gccxml
    # and castxml.
    static_call = ns.free_function("static_call")
    assert static_call is not None

    return_no_args = ns.free_function("return_no_args")
    helpers._test_calldef_return_type(return_no_args, declarations.int_t)
    # from now there is no need to check return type.
    no_return_1_arg = ns.free_function(name="no_return_1_arg")
    assert no_return_1_arg is not None
    assert no_return_1_arg.arguments[0].name in ["arg", "arg0"]
    helpers._test_calldef_args(
        no_return_1_arg,
        [
            declarations.argument_t(
                name=no_return_1_arg.arguments[0].name,
                decl_type=declarations.int_t()
            )
        ],
    )

    return_default_args = ns.free_function("return_default_args")
    assert return_default_args.arguments[0].name in ["arg", "arg0"]
    assert return_default_args.arguments[1].name in ["arg1", "flag"]
    helpers._test_calldef_args(
        return_default_args,
        [
            declarations.argument_t(
                name=return_default_args.arguments[0].name,
                decl_type=declarations.int_t(),
                default_value="1",
            ),
            declarations.argument_t(
                name=return_default_args.arguments[1].name,
                decl_type=declarations.bool_t(),
                default_value="false",
            ),
        ],
    )
    helpers._test_calldef_exceptions(global_ns, return_default_args, [])


@pytest.mark.parametrize(
    "global_ns",
    [
        "global_ns_fixture_all_at_once",
        "global_ns_fixture_file_by_file",
    ],
    indirect=True,
)
def test_calldef_member_functions(global_ns, helpers):
    struct_calldefs = global_ns.class_("calldefs_t")

    member_inline_call = struct_calldefs.member_function("member_inline_call")
    helpers._test_calldef_args(
        member_inline_call,
        [declarations.argument_t(name="i", decl_type=declarations.int_t())],
    )

    member_const_call = struct_calldefs.member_function("member_const_call")
    assert member_const_call.has_const
    assert member_const_call.virtuality == \
        declarations.VIRTUALITY_TYPES.NOT_VIRTUAL

    member_virtual_call = struct_calldefs.member_function(
        name="member_virtual_call"
        )
    assert member_virtual_call.virtuality == \
        declarations.VIRTUALITY_TYPES.VIRTUAL

    member_pure_virtual_call = struct_calldefs.member_function(
        "member_pure_virtual_call"
    )
    assert (
        member_pure_virtual_call.virtuality
        == declarations.VIRTUALITY_TYPES.PURE_VIRTUAL
    )

    static_call = struct_calldefs.member_function("static_call")
    assert static_call.has_static is True


@pytest.mark.parametrize(
    "global_ns",
    [
        "global_ns_fixture_all_at_once",
        "global_ns_fixture_file_by_file",
    ],
    indirect=True,
)
def test_constructors_destructors(global_ns, helpers):
    struct_calldefs = global_ns.class_("calldefs_t")

    destructor = struct_calldefs.calldef("~calldefs_t")
    helpers._test_calldef_args(destructor, [])
    helpers._test_calldef_return_type(destructor, None.__class__)

    # well, now we have a few functions ( constructors ) with the same
    # name, there is no easy way to find the desired one. Well in my case
    # I have only 4 constructors
    # 1. from char
    # 2. from (int,double)
    # 3. default
    # 4. copy constructor
    constructor_found = struct_calldefs.constructors("calldefs_t")
    assert len(constructor_found) == 5
    assert (
        len(
            [
                constructor
                for constructor in constructor_found
                if declarations.is_copy_constructor(constructor)
            ]
        )
        == 1
    )
    # there is nothing to check about constructors - I know the
    # implementation of parser.
    # In this case it doesn't different from any other function

    c = struct_calldefs.constructor("calldefs_t", arg_types=["char"])
    assert c.explicit is True

    arg_type = declarations.declarated_t(global_ns.class_("some_exception_t"))
    c = struct_calldefs.constructor("calldefs_t", arg_types=[arg_type])
    assert c.explicit is False


@pytest.mark.parametrize(
    "global_ns",
    [
        "global_ns_fixture_all_at_once",
        "global_ns_fixture_file_by_file",
    ],
    indirect=True,
)
def test_operator_symbol(global_ns):
    calldefs_operators = ["=", "=="]
    calldefs_cast_operators = ["char *", "double"]
    struct_calldefs = global_ns.class_("calldefs_t")
    assert struct_calldefs is not None
    for decl in struct_calldefs.declarations:
        if not isinstance(decl, declarations.operator_t):
            continue
        if not isinstance(decl, declarations.casting_operator_t):
            assert decl.symbol in calldefs_operators
        else:
            assert decl.return_type.decl_string in calldefs_cast_operators


@pytest.mark.parametrize(
    "global_ns",
    [
        "global_ns_fixture_all_at_once",
        "global_ns_fixture_file_by_file",
    ],
    indirect=True,
)
def test_ellipsis(global_ns):
    ns = global_ns.namespace("ellipsis_tester")
    do_smth = ns.member_function("do_smth")
    assert do_smth.has_ellipsis is True
    do_smth_else = ns.free_function("do_smth_else")
    assert do_smth_else.has_ellipsis is True