|
| 1 | +# Copyright 2014-2017 Insight Software Consortium. |
| 2 | +# Copyright 2004-2009 Roman Yakovenko. |
| 3 | +# Distributed under the Boost Software License, Version 1.0. |
| 4 | +# See http://www.boost.org/LICENSE_1_0.txt |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from . import autoconfig |
| 9 | + |
| 10 | +from pygccxml import parser |
| 11 | +from pygccxml import declarations |
| 12 | + |
| 13 | +TEST_FILES = [ |
| 14 | + "declarations_enums.hpp", |
| 15 | + "declarations_variables.hpp", |
| 16 | + "declarations_calldef.hpp", |
| 17 | +] |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def global_ns_fixture_all_at_once(): |
| 22 | + COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE |
| 23 | + config = autoconfig.cxx_parsers_cfg.config.clone() |
| 24 | + decls = parser.parse(TEST_FILES, config, COMPILATION_MODE) |
| 25 | + global_ns = declarations.get_global_namespace(decls) |
| 26 | + return global_ns |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def global_ns_fixture_file_by_file(): |
| 31 | + COMPILATION_MODE = parser.COMPILATION_MODE.FILE_BY_FILE |
| 32 | + config = autoconfig.cxx_parsers_cfg.config.clone() |
| 33 | + decls = parser.parse(TEST_FILES, config, COMPILATION_MODE) |
| 34 | + global_ns = declarations.get_global_namespace(decls) |
| 35 | + return global_ns |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture |
| 39 | +def global_ns(request): |
| 40 | + return request.getfixturevalue(request.param) |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.parametrize( |
| 44 | + "global_ns", |
| 45 | + [ |
| 46 | + "global_ns_fixture_all_at_once", |
| 47 | + "global_ns_fixture_file_by_file", |
| 48 | + ], |
| 49 | + indirect=True, |
| 50 | +) |
| 51 | +def test_enumeration_t(global_ns): |
| 52 | + enum = global_ns.enumeration("ENumbers") |
| 53 | + expected_values = list( |
| 54 | + zip( |
| 55 | + ["e%d" % index for index in range(10)], |
| 56 | + [index for index in range(10)] |
| 57 | + ) |
| 58 | + ) |
| 59 | + assert expected_values == enum.values |
| 60 | + |
| 61 | + |
| 62 | +def test_namespace(): |
| 63 | + pass # tested in core_tester |
| 64 | + |
| 65 | + |
| 66 | +def test_types(): |
| 67 | + pass # tested in core_tester |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.parametrize( |
| 71 | + "global_ns", |
| 72 | + [ |
| 73 | + "global_ns_fixture_all_at_once", |
| 74 | + "global_ns_fixture_file_by_file", |
| 75 | + ], |
| 76 | + indirect=True, |
| 77 | +) |
| 78 | +def test_variables(global_ns, helpers): |
| 79 | + global_ns.namespace("variables") |
| 80 | + initialized = global_ns.variable(name="initialized") |
| 81 | + |
| 82 | + expected_value = "10122004" |
| 83 | + assert initialized.value == expected_value |
| 84 | + helpers._test_type_composition( |
| 85 | + initialized.decl_type, |
| 86 | + declarations.const_t, |
| 87 | + declarations.long_unsigned_int_t |
| 88 | + ) |
| 89 | + |
| 90 | + m_mutable = global_ns.variable(name="m_mutable") |
| 91 | + assert m_mutable.type_qualifiers.has_static is False |
| 92 | + assert m_mutable.type_qualifiers.has_mutable is True |
| 93 | + |
| 94 | + # External static variable |
| 95 | + extern_var = global_ns.variable(name="extern_var") |
| 96 | + assert extern_var.type_qualifiers.has_extern is True |
| 97 | + assert extern_var.type_qualifiers.has_static is False |
| 98 | + assert extern_var.type_qualifiers.has_mutable is False |
| 99 | + |
| 100 | + # Static variable |
| 101 | + static_var = global_ns.variable(name="static_var") |
| 102 | + assert static_var.type_qualifiers.has_static is True |
| 103 | + assert static_var.type_qualifiers.has_extern is False |
| 104 | + assert static_var.type_qualifiers.has_mutable is False |
| 105 | + |
| 106 | + ssv_static_var = global_ns.variable(name="ssv_static_var") |
| 107 | + assert ssv_static_var.type_qualifiers.has_static is True |
| 108 | + assert ssv_static_var.type_qualifiers.has_extern is False |
| 109 | + assert ssv_static_var.type_qualifiers.has_mutable is False |
| 110 | + |
| 111 | + ssv_static_var_value = global_ns.variable(name="ssv_static_var_value") |
| 112 | + assert ssv_static_var_value.type_qualifiers.has_static is True |
| 113 | + assert ssv_static_var_value.type_qualifiers.has_extern is False |
| 114 | + assert ssv_static_var_value.type_qualifiers.has_mutable is False |
| 115 | + |
| 116 | + |
| 117 | +@pytest.mark.parametrize( |
| 118 | + "global_ns", |
| 119 | + [ |
| 120 | + "global_ns_fixture_all_at_once", |
| 121 | + "global_ns_fixture_file_by_file", |
| 122 | + ], |
| 123 | + indirect=True, |
| 124 | +) |
| 125 | +def test_calldef_free_functions(global_ns, helpers): |
| 126 | + ns = global_ns.namespace("calldef") |
| 127 | + |
| 128 | + no_return_no_args = ns.free_function("no_return_no_args") |
| 129 | + |
| 130 | + helpers._test_calldef_return_type(no_return_no_args, declarations.void_t) |
| 131 | + assert no_return_no_args.has_extern is False |
| 132 | + |
| 133 | + # Static_call is explicetely defined as extern, this works with gccxml |
| 134 | + # and castxml. |
| 135 | + static_call = ns.free_function("static_call") |
| 136 | + assert static_call is not None |
| 137 | + |
| 138 | + return_no_args = ns.free_function("return_no_args") |
| 139 | + helpers._test_calldef_return_type(return_no_args, declarations.int_t) |
| 140 | + # from now there is no need to check return type. |
| 141 | + no_return_1_arg = ns.free_function(name="no_return_1_arg") |
| 142 | + assert no_return_1_arg is not None |
| 143 | + assert no_return_1_arg.arguments[0].name in ["arg", "arg0"] |
| 144 | + helpers._test_calldef_args( |
| 145 | + no_return_1_arg, |
| 146 | + [ |
| 147 | + declarations.argument_t( |
| 148 | + name=no_return_1_arg.arguments[0].name, |
| 149 | + decl_type=declarations.int_t() |
| 150 | + ) |
| 151 | + ], |
| 152 | + ) |
| 153 | + |
| 154 | + return_default_args = ns.free_function("return_default_args") |
| 155 | + assert return_default_args.arguments[0].name in ["arg", "arg0"] |
| 156 | + assert return_default_args.arguments[1].name in ["arg1", "flag"] |
| 157 | + helpers._test_calldef_args( |
| 158 | + return_default_args, |
| 159 | + [ |
| 160 | + declarations.argument_t( |
| 161 | + name=return_default_args.arguments[0].name, |
| 162 | + decl_type=declarations.int_t(), |
| 163 | + default_value="1", |
| 164 | + ), |
| 165 | + declarations.argument_t( |
| 166 | + name=return_default_args.arguments[1].name, |
| 167 | + decl_type=declarations.bool_t(), |
| 168 | + default_value="false", |
| 169 | + ), |
| 170 | + ], |
| 171 | + ) |
| 172 | + helpers._test_calldef_exceptions(global_ns, return_default_args, []) |
| 173 | + |
| 174 | + calldef_with_throw = ns.free_function("calldef_with_throw") |
| 175 | + assert calldef_with_throw is not None |
| 176 | + helpers._test_calldef_exceptions( |
| 177 | + global_ns, calldef_with_throw, |
| 178 | + ["some_exception_t", "other_exception_t"] |
| 179 | + ) |
| 180 | + # from now there is no need to check exception specification |
| 181 | + |
| 182 | + |
| 183 | +@pytest.mark.parametrize( |
| 184 | + "global_ns", |
| 185 | + [ |
| 186 | + "global_ns_fixture_all_at_once", |
| 187 | + "global_ns_fixture_file_by_file", |
| 188 | + ], |
| 189 | + indirect=True, |
| 190 | +) |
| 191 | +def test_calldef_member_functions(global_ns, helpers): |
| 192 | + struct_calldefs = global_ns.class_("calldefs_t") |
| 193 | + |
| 194 | + member_inline_call = struct_calldefs.member_function("member_inline_call") |
| 195 | + helpers._test_calldef_args( |
| 196 | + member_inline_call, |
| 197 | + [declarations.argument_t(name="i", decl_type=declarations.int_t())], |
| 198 | + ) |
| 199 | + |
| 200 | + member_const_call = struct_calldefs.member_function("member_const_call") |
| 201 | + assert member_const_call.has_const |
| 202 | + assert member_const_call.virtuality == \ |
| 203 | + declarations.VIRTUALITY_TYPES.NOT_VIRTUAL |
| 204 | + |
| 205 | + member_virtual_call = struct_calldefs.member_function( |
| 206 | + name="member_virtual_call" |
| 207 | + ) |
| 208 | + assert member_virtual_call.virtuality == \ |
| 209 | + declarations.VIRTUALITY_TYPES.VIRTUAL |
| 210 | + |
| 211 | + member_pure_virtual_call = struct_calldefs.member_function( |
| 212 | + "member_pure_virtual_call" |
| 213 | + ) |
| 214 | + assert ( |
| 215 | + member_pure_virtual_call.virtuality |
| 216 | + == declarations.VIRTUALITY_TYPES.PURE_VIRTUAL |
| 217 | + ) |
| 218 | + |
| 219 | + static_call = struct_calldefs.member_function("static_call") |
| 220 | + assert static_call.has_static is True |
| 221 | + |
| 222 | + |
| 223 | +@pytest.mark.parametrize( |
| 224 | + "global_ns", |
| 225 | + [ |
| 226 | + "global_ns_fixture_all_at_once", |
| 227 | + "global_ns_fixture_file_by_file", |
| 228 | + ], |
| 229 | + indirect=True, |
| 230 | +) |
| 231 | +def test_constructors_destructors(global_ns, helpers): |
| 232 | + struct_calldefs = global_ns.class_("calldefs_t") |
| 233 | + |
| 234 | + destructor = struct_calldefs.calldef("~calldefs_t") |
| 235 | + helpers._test_calldef_args(destructor, []) |
| 236 | + helpers._test_calldef_return_type(destructor, None.__class__) |
| 237 | + |
| 238 | + # well, now we have a few functions ( constructors ) with the same |
| 239 | + # name, there is no easy way to find the desired one. Well in my case |
| 240 | + # I have only 4 constructors |
| 241 | + # 1. from char |
| 242 | + # 2. from (int,double) |
| 243 | + # 3. default |
| 244 | + # 4. copy constructor |
| 245 | + constructor_found = struct_calldefs.constructors("calldefs_t") |
| 246 | + assert len(constructor_found) == 5 |
| 247 | + assert ( |
| 248 | + len( |
| 249 | + [ |
| 250 | + constructor |
| 251 | + for constructor in constructor_found |
| 252 | + if declarations.is_copy_constructor(constructor) |
| 253 | + ] |
| 254 | + ) |
| 255 | + == 1 |
| 256 | + ) |
| 257 | + # there is nothing to check about constructors - I know the |
| 258 | + # implementation of parser. |
| 259 | + # In this case it doesn't different from any other function |
| 260 | + |
| 261 | + c = struct_calldefs.constructor("calldefs_t", arg_types=["char"]) |
| 262 | + assert c.explicit is True |
| 263 | + |
| 264 | + arg_type = declarations.declarated_t(global_ns.class_("some_exception_t")) |
| 265 | + c = struct_calldefs.constructor("calldefs_t", arg_types=[arg_type]) |
| 266 | + assert c.explicit is False |
| 267 | + |
| 268 | + |
| 269 | +@pytest.mark.parametrize( |
| 270 | + "global_ns", |
| 271 | + [ |
| 272 | + "global_ns_fixture_all_at_once", |
| 273 | + "global_ns_fixture_file_by_file", |
| 274 | + ], |
| 275 | + indirect=True, |
| 276 | +) |
| 277 | +def test_operator_symbol(global_ns): |
| 278 | + calldefs_operators = ["=", "=="] |
| 279 | + calldefs_cast_operators = ["char *", "double"] |
| 280 | + struct_calldefs = global_ns.class_("calldefs_t") |
| 281 | + assert struct_calldefs is not None |
| 282 | + for decl in struct_calldefs.declarations: |
| 283 | + if not isinstance(decl, declarations.operator_t): |
| 284 | + continue |
| 285 | + if not isinstance(decl, declarations.casting_operator_t): |
| 286 | + assert decl.symbol in calldefs_operators |
| 287 | + else: |
| 288 | + assert decl.return_type.decl_string in calldefs_cast_operators |
| 289 | + |
| 290 | + |
| 291 | +@pytest.mark.parametrize( |
| 292 | + "global_ns", |
| 293 | + [ |
| 294 | + "global_ns_fixture_all_at_once", |
| 295 | + "global_ns_fixture_file_by_file", |
| 296 | + ], |
| 297 | + indirect=True, |
| 298 | +) |
| 299 | +def test_ellipsis(global_ns): |
| 300 | + ns = global_ns.namespace("ellipsis_tester") |
| 301 | + do_smth = ns.member_function("do_smth") |
| 302 | + assert do_smth.has_ellipsis is True |
| 303 | + do_smth_else = ns.free_function("do_smth_else") |
| 304 | + assert do_smth_else.has_ellipsis is True |
0 commit comments