diff --git a/src/pygccxml/declarations/container_traits.py b/src/pygccxml/declarations/container_traits.py index d4f6658e..e5035782 100644 --- a/src/pygccxml/declarations/container_traits.py +++ b/src/pygccxml/declarations/container_traits.py @@ -647,14 +647,14 @@ def remove_defaults(self, type_or_string): unordered_set_traits = container_traits_impl_t( 'unordered_set', - 1, + 0, 'value_type', 'erase_hash_allocator', unordered_maps_and_sets=True) unordered_multiset_traits = container_traits_impl_t( 'unordered_multiset', - 1, + 0, 'value_type', 'erase_hash_allocator', unordered_maps_and_sets=True) diff --git a/src/pygccxml/parser/scanner.py b/src/pygccxml/parser/scanner.py index 24abd68d..f39dd05d 100644 --- a/src/pygccxml/parser/scanner.py +++ b/src/pygccxml/parser/scanner.py @@ -630,9 +630,9 @@ def __read_typedef(self, attrs): def __read_variable(self, attrs): type_qualifiers = declarations.type_qualifiers_t() - type_qualifiers.has_mutable = attrs.get(XML_AN_MUTABLE, False) - type_qualifiers.has_static = attrs.get(XML_AN_STATIC, False) - type_qualifiers.has_extern = attrs.get(XML_AN_EXTERN, False) + type_qualifiers.has_mutable = bool(attrs.get(XML_AN_MUTABLE, False)) + type_qualifiers.has_static = bool(attrs.get(XML_AN_STATIC, False)) + type_qualifiers.has_extern = bool(attrs.get(XML_AN_EXTERN, False)) bits = attrs.get(XML_AN_BITS) if bits: bits = int(bits) diff --git a/tests/conftest.py b/tests/conftest.py index 625d4119..fb110455 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -12,6 +12,31 @@ def _test_type_composition(type_, expected_compound, expected_base): assert isinstance(type_, expected_compound) assert isinstance(type_.base, expected_base) + @staticmethod + def _test_calldef_args(calldef, expected_args): + assert len(calldef.arguments) == len(expected_args) + + for i, expected_arg in enumerate(expected_args): + arg = calldef.arguments[i] + assert arg == expected_arg + + @staticmethod + def _test_calldef_return_type(calldef, expected_type): + assert isinstance(calldef.return_type, expected_type) + + @staticmethod + def _test_calldef_exceptions(global_ns, calldef, exceptions): + # exceptions is list of classes names + exception_decls = [] + for name in exceptions: + exception_decl = global_ns.class_(name) + assert exception_decl is not None + exception_decls.append(exception_decl) + exception_decls.sort() + assert len(calldef.exceptions) == len(exception_decls) + exceptions_indeed = sorted(calldef.exceptions[:]) + assert exception_decls == exceptions_indeed + @pytest.fixture def helpers(): diff --git a/tests/test_declarations.py b/tests/test_declarations.py new file mode 100644 index 00000000..66b7a561 --- /dev/null +++ b/tests/test_declarations.py @@ -0,0 +1,304 @@ +# 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, []) + + calldef_with_throw = ns.free_function("calldef_with_throw") + assert calldef_with_throw is not None + helpers._test_calldef_exceptions( + global_ns, calldef_with_throw, + ["some_exception_t", "other_exception_t"] + ) + # from now there is no need to check exception specification + + +@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 diff --git a/unittests/dependencies_tester.py b/tests/test_dependencies.py similarity index 100% rename from unittests/dependencies_tester.py rename to tests/test_dependencies.py diff --git a/tests/test_find_container_traits.py b/tests/test_find_container_traits.py new file mode 100644 index 00000000..dbf6fd67 --- /dev/null +++ b/tests/test_find_container_traits.py @@ -0,0 +1,168 @@ +# 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 = ["remove_template_defaults.hpp", "indexing_suites2.hpp"] + + +@pytest.fixture +def global_ns(): + 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) + global_ns.init_optimizer() + return global_ns + + +def __cmp_traits(global_ns, typedef, expected, partial_name, key_type=None): + if isinstance(typedef, str): + typedef = global_ns.typedef(typedef) + traits = declarations.find_container_traits(typedef) + assert traits is not None + assert traits == expected + cls = declarations.remove_declarated(typedef) + assert declarations.find_container_traits(cls) == expected + assert cls.partial_name == partial_name + cls = traits.class_declaration(cls) + print("xxxx", traits, typedef) + assert traits.element_type(typedef) is not None + assert cls.cache.container_element_type is not None + + if key_type: + assert traits.is_mapping(typedef) is not None + real_key_type = traits.key_type(typedef) + assert real_key_type.decl_string == key_type + assert cls.cache.container_key_type is not None + else: + assert traits.is_sequence(typedef) + + +def test_find_traits(global_ns): + __cmp_traits( + global_ns, + "v_int", + declarations.vector_traits, + "vector< int >" + ) + __cmp_traits( + global_ns, + "l_int", + declarations.list_traits, + "list< int >" + ) + __cmp_traits( + global_ns, "d_v_int", + declarations.deque_traits, + "deque< std::vector< int > >" + ) + __cmp_traits( + global_ns, "q_int", + declarations.queue_traits, + "queue< int >" + ) + __cmp_traits( + global_ns, "pq_int", + declarations.priority_queue_traits, + "priority_queue< int >" + ) + __cmp_traits( + global_ns, "s_v_int", + declarations.set_traits, + "set< std::vector< int > >" + ) + __cmp_traits( + global_ns, + "ms_v_int", + declarations.multiset_traits, + "multiset< std::vector< int > >", + ) + __cmp_traits( + global_ns, "m_i2d", + declarations.map_traits, + "map< int, double >", + "int" + ) + __cmp_traits( + global_ns, + "mm_i2d", + declarations.multimap_traits, + "multimap< int, double >", + "int", + ) + __cmp_traits( + global_ns, + "hs_v_int", + declarations.unordered_set_traits, + "unordered_set< std::vector< int > >", + ) + __cmp_traits( + global_ns, + "mhs_v_int", + declarations.unordered_multiset_traits, + "unordered_multiset< std::vector< int > >", + ) + __cmp_traits( + global_ns, + "hm_i2d", + declarations.unordered_map_traits, + "unordered_map< int, double >", + "int", + ) + __cmp_traits( + global_ns, + "hmm_i2d", + declarations.unordered_multimap_traits, + "unordered_multimap< int, double >", + "int", + ) + + +def test_multimap(global_ns): + m = global_ns.class_(lambda decl: decl.name.startswith("multimap")) + declarations.find_container_traits(m) + assert m.partial_name == "multimap< int, int >" + + +def test_recursive_partial_name(global_ns): + f1 = global_ns.free_function("f1") + t1 = declarations.class_traits.get_declaration(f1.arguments[0].decl_type) + assert "type< std::set< std::vector< int > > >" == t1.partial_name + + +def test_remove_defaults_partial_name_namespace(global_ns): + f2 = global_ns.free_function("f2") + type_info = f2.return_type + traits = declarations.find_container_traits(type_info) + cls = traits.class_declaration(type_info) + decl_string = cls.partial_decl_string + key_type_string = traits.key_type(type_info).partial_decl_string + assert decl_string.startswith("::std::") + assert key_type_string.startswith("::std::") + + +def test_from_ogre(): + x = ( + "map, " + + "std::allocator > >" + ) + ct = declarations.find_container_traits(x) + ct.remove_defaults(x) + + +def test_infinite_loop(global_ns): + rt = global_ns.free_function("test_infinite_loop").return_type + map_traits = declarations.find_container_traits(rt) + assert map_traits == declarations.map_traits + elem = map_traits.element_type(rt) + assert elem.decl_string == "int" diff --git a/unittests/hierarchy_traveling.py b/tests/test_hierarchy_traveling.py similarity index 100% rename from unittests/hierarchy_traveling.py rename to tests/test_hierarchy_traveling.py diff --git a/unittests/non_copyable_classes_tester.py b/tests/test_non_copyable_classes.py similarity index 100% rename from unittests/non_copyable_classes_tester.py rename to tests/test_non_copyable_classes.py diff --git a/unittests/declarations_tester.py b/unittests/declarations_tester.py deleted file mode 100644 index 2f1921b9..00000000 --- a/unittests/declarations_tester.py +++ /dev/null @@ -1,344 +0,0 @@ -# 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 pprint -import unittest - -from . import autoconfig -from . import parser_test_case - -from pygccxml import parser -from pygccxml import declarations - - -class declarations_t(parser_test_case.parser_test_case_t): - - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.global_ns = None - - def test_enumeration_t(self): - enum = self.global_ns.enumeration('ENumbers') - expected_values = list( - zip(['e%d' % index for index in range(10)], - [index for index in range(10)])) - self.assertTrue( - expected_values == enum.values, - ("expected enum values ( '%s' ) and existings ( '%s' ) are " + - "different") % - (pprint.pformat(expected_values), pprint.pformat(enum.values))) - - def test_namespace(self): - pass # tested in core_tester - - def test_types(self): - pass # tested in core_tester - - def test_variables(self): - self.global_ns.namespace('variables') - initialized = self.global_ns.variable(name='initialized') - - expected_value = '10122004' - self.assertTrue( - initialized.value == expected_value, - ("there is a difference between expected value( %s ) and real " + - "value(%s) of 'initialized' variable") % - (expected_value, initialized.value)) - self._test_type_composition( - initialized.decl_type, - declarations.const_t, - declarations.long_unsigned_int_t) - - m_mutable = self.global_ns.variable(name="m_mutable") - self.assertFalse( - m_mutable.type_qualifiers.has_static, - "m_mutable must not have static type qualifier") - - self.assertTrue( - m_mutable.type_qualifiers.has_mutable, - "m_mutable must have mutable type qualifier") - - # External static variable - extern_var = self.global_ns.variable(name="extern_var") - self.assertTrue( - extern_var.type_qualifiers.has_extern, - "extern_var must have extern type qualifier") - self.assertFalse( - extern_var.type_qualifiers.has_static, - "extern_var must not have a static type qualifier") - self.assertFalse( - extern_var.type_qualifiers.has_mutable, - "static_var must not have mutable type qualifier") - - # Static variable - static_var = self.global_ns.variable(name="static_var") - self.assertTrue( - static_var.type_qualifiers.has_static, - "static_var must have static type qualifier") - self.assertFalse( - static_var.type_qualifiers.has_extern, - "static_var must not have an extern type qualifier") - self.assertFalse( - static_var.type_qualifiers.has_mutable, - "static_var must not have mutable type qualifier") - - ssv_static_var = self.global_ns.variable(name="ssv_static_var") - self.assertTrue( - ssv_static_var.type_qualifiers.has_static, - "ssv_static_var must have static type qualifier") - self.assertFalse( - ssv_static_var.type_qualifiers.has_extern, - "ssv_static_var must not have an extern type qualifier") - self.assertFalse( - ssv_static_var.type_qualifiers.has_mutable, - "ssv_static_var must not have mutable type qualifier") - - ssv_static_var_value = self.global_ns.variable( - name="ssv_static_var_value") - self.assertTrue( - ssv_static_var_value.type_qualifiers.has_static, - "ssv_static_var_value must have static type qualifier") - self.assertFalse( - ssv_static_var_value.type_qualifiers.has_extern, - "ssv_static_var_value must not have an extern type qualifier") - self.assertFalse( - ssv_static_var_value.type_qualifiers.has_mutable, - "ssv_static_var_value must not have mutable type qualifier") - - def test_calldef_free_functions(self): - ns = self.global_ns.namespace('calldef') - - no_return_no_args = ns.free_function('no_return_no_args') - - self._test_calldef_return_type(no_return_no_args, declarations.void_t) - self.assertTrue( - not no_return_no_args.has_extern, - "function 'no_return_no_args' should have an extern qualifier") - - # Static_call is explicetely defined as extern, this works with gccxml - # and castxml. - static_call = ns.free_function('static_call') - self.assertTrue( - static_call, - "function 'no_return_no_args' should have an extern qualifier") - - return_no_args = ns.free_function('return_no_args') - self._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') - self.assertTrue( - no_return_1_arg, - "unable to find 'no_return_1_arg' function") - self.assertTrue(no_return_1_arg.arguments[0].name in ['arg', 'arg0']) - self._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') - self.assertTrue( - return_default_args.arguments[0].name in ['arg', 'arg0']) - self.assertTrue( - return_default_args.arguments[1].name in ['arg1', 'flag']) - self._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')]) - self._test_calldef_exceptions(return_default_args, []) - - calldef_with_throw = ns.free_function('calldef_with_throw') - self.assertTrue( - calldef_with_throw, - "unable to find 'calldef_with_throw' function") - self._test_calldef_exceptions( - calldef_with_throw, [ - 'some_exception_t', 'other_exception_t']) - # from now there is no need to check exception specification - - def test_calldef_member_functions(self): - struct_calldefs = self.global_ns.class_('calldefs_t') - - member_inline_call = struct_calldefs.member_function( - 'member_inline_call') - self._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') - self.assertTrue( - member_const_call.has_const, - "function 'member_const_call' should have const qualifier") - self.assertTrue( - member_const_call.virtuality == - declarations.VIRTUALITY_TYPES.NOT_VIRTUAL, - "function 'member_const_call' should be non virtual function") - - member_virtual_call = struct_calldefs.member_function( - name='member_virtual_call') - self.assertTrue( - member_virtual_call.virtuality == - declarations.VIRTUALITY_TYPES.VIRTUAL, - "function 'member_virtual_call' should be virtual function") - - member_pure_virtual_call = struct_calldefs.member_function( - 'member_pure_virtual_call') - self.assertTrue( - member_pure_virtual_call.virtuality == - declarations.VIRTUALITY_TYPES.PURE_VIRTUAL, - ("function 'member_pure_virtual_call' should be pure virtual " + - "function")) - - static_call = struct_calldefs.member_function('static_call') - self.assertTrue( - static_call.has_static, - "function 'static_call' should have static qualifier") - # from now we there is no need to check static qualifier - - def test_constructors_destructors(self): - struct_calldefs = self.global_ns.class_('calldefs_t') - - destructor = struct_calldefs.calldef('~calldefs_t') - self._test_calldef_args(destructor, []) - self._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') - self.assertTrue( - len(constructor_found) == 5, - ("struct 'calldefs_t' has 5 constructors, pygccxml parser " + - "reports only about %d.") % - len(constructor_found)) - error_text = "copy constructor has not been found" - self.assertTrue(1 == len( - [constructor for constructor in constructor_found if - declarations.is_copy_constructor(constructor)]), error_text) - # 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']) - self.assertTrue( - c.explicit, - ("calldef_t constructor defined with 'explicit' keyword, " + - "for some reason the value is False ")) - - arg_type = declarations.declarated_t( - self.global_ns.class_('some_exception_t')) - c = struct_calldefs.constructor('calldefs_t', arg_types=[arg_type]) - self.assertTrue( - c.explicit is False, - ("calldef_t constructor defined without 'explicit' keyword, " + - "for some reason the value is True ")) - - def test_operator_symbol(self): - calldefs_operators = ['=', '=='] - calldefs_cast_operators = ['char *', 'double'] - struct_calldefs = self.global_ns.class_('calldefs_t') - self.assertTrue(struct_calldefs, "unable to find struct 'calldefs_t'") - for decl in struct_calldefs.declarations: - if not isinstance(decl, declarations.operator_t): - continue - if not isinstance(decl, declarations.casting_operator_t): - self.assertTrue( - decl.symbol in calldefs_operators, - "unable to find operator symbol for operator '%s'" % - decl.decl_string) - else: - self.assertTrue( - decl.return_type.decl_string in calldefs_cast_operators, - "unable to find operator symbol for operator '%s'" % - decl.decl_string) - - def test_ellipsis(self): - ns = self.global_ns.namespace('ellipsis_tester') - do_smth = ns.member_function('do_smth') - self.assertTrue(do_smth.has_ellipsis) - do_smth_else = ns.free_function('do_smth_else') - self.assertTrue(do_smth_else.has_ellipsis) - - -class gccxml_declarations_t(declarations_t): - global_ns = None - - def __init__(self, *args): - declarations_t.__init__(self, *args) - self.test_files = [ - 'declarations_enums.hpp', - 'declarations_variables.hpp', - 'declarations_calldef.hpp'] - self.global_ns = None - - def setUp(self): - if not gccxml_declarations_t.global_ns: - decls = parser.parse( - self.test_files, - self.config, - self.COMPILATION_MODE) - gccxml_declarations_t.global_ns = \ - declarations.get_global_namespace(decls) - gccxml_declarations_t.xml_generator_from_xml_file = \ - self.config.xml_generator_from_xml_file - if not self.global_ns: - self.xml_generator_from_xml_file = \ - gccxml_declarations_t.xml_generator_from_xml_file - self.global_ns = gccxml_declarations_t.global_ns - - -class all_at_once_tester_t(gccxml_declarations_t): - COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE - - def __init__(self, *args): - gccxml_declarations_t.__init__(self, *args) - - -class file_by_file_tester_t(gccxml_declarations_t): - COMPILATION_MODE = parser.COMPILATION_MODE.FILE_BY_FILE - - def __init__(self, *args): - gccxml_declarations_t.__init__(self, *args) - - -class pdb_based_tester_t(declarations_t): - - def __init__(self, *args): - declarations_t.__init__(self, *args) - self.global_ns = autoconfig.get_pdb_global_ns() - - -def create_suite(): - suite = unittest.TestSuite() - suite.addTest( - unittest.TestLoader().loadTestsFromTestCase( - testCaseClass=file_by_file_tester_t)) - suite.addTest( - unittest.TestLoader().loadTestsFromTestCase( - testCaseClass=all_at_once_tester_t)) - # if os.name == 'nt' and autoconfig.get_pdb_global_ns(): - # suite.addTest( unittest.makeSuite(pdb_based_tester_t)) - - return suite - - -def run_suite(): - unittest.TextTestRunner(verbosity=2).run(create_suite()) - - -if __name__ == "__main__": - run_suite() diff --git a/unittests/find_container_traits_tester.py b/unittests/find_container_traits_tester.py deleted file mode 100644 index 1571c373..00000000 --- a/unittests/find_container_traits_tester.py +++ /dev/null @@ -1,208 +0,0 @@ -# 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 unittest - -from . import parser_test_case - -from pygccxml import parser -from pygccxml import declarations - - -class Test(parser_test_case.parser_test_case_t): - global_ns = None - - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.headers = ['remove_template_defaults.hpp', 'indexing_suites2.hpp'] - - def setUp(self): - if not Test.global_ns: - decls = parser.parse(self.headers, self.config) - Test.global_ns = declarations.get_global_namespace(decls) - Test.global_ns.init_optimizer() - Test.xml_generator_from_xml_file = \ - self.config.xml_generator_from_xml_file - self.xml_generator_from_xml_file = Test.xml_generator_from_xml_file - self.global_ns = Test.global_ns - - def __cmp_traits(self, typedef, expected, partial_name, key_type=None): - if isinstance(typedef, str): - typedef = self.global_ns.typedef(typedef) - traits = declarations.find_container_traits(typedef) - self.assertTrue( - traits, - 'container traits for "%s" not found' % - str(typedef)) - self.assertTrue( - traits is expected, - 'container "%s", expected %s_traits, got %s_traits' % - (str(typedef), - expected.name(), - traits.name())) - cls = declarations.remove_declarated(typedef) - self.assertTrue(declarations.find_container_traits(cls) is expected) - self.assertTrue(cls.partial_name == partial_name) - cls = traits.class_declaration(cls) - - self.assertTrue(traits.element_type(typedef)) - self.assertTrue( - cls.cache.container_element_type, - "For some reason cache was not updated") - - if key_type: - self.assertTrue(traits.is_mapping(typedef)) - real_key_type = traits.key_type(typedef) - self.assertTrue( - real_key_type.decl_string == key_type, - 'Error extracting key type. Expected type "%s", got "%s"' % - (key_type, - real_key_type.decl_string)) - self.assertTrue( - cls.cache.container_key_type, - "For some reason cache was not updated") - else: - self.assertTrue(traits.is_sequence(typedef)) - - def test_find_traits(self): - self.__cmp_traits('v_int', declarations.vector_traits, "vector< int >") - self.__cmp_traits('l_int', declarations.list_traits, "list< int >") - self.__cmp_traits( - 'd_v_int', - declarations.deque_traits, - "deque< std::vector< int > >") - self.__cmp_traits('q_int', declarations.queue_traits, "queue< int >") - self.__cmp_traits( - 'pq_int', - declarations.priority_queue_traits, - "priority_queue< int >") - self.__cmp_traits( - 's_v_int', - declarations.set_traits, - "set< std::vector< int > >") - self.__cmp_traits( - 'ms_v_int', - declarations.multiset_traits, - "multiset< std::vector< int > >") - self.__cmp_traits( - 'm_i2d', - declarations.map_traits, - "map< int, double >", - 'int') - self.__cmp_traits( - 'mm_i2d', - declarations.multimap_traits, - "multimap< int, double >", - 'int') - - if self.xml_generator_from_xml_file.is_castxml: - self.__cmp_traits( - 'hs_v_int', - declarations.unordered_set_traits, - "unordered_set< std::vector< int > >") - else: - self.__cmp_traits( - 'hs_v_int', - declarations.hash_set_traits, - "hash_set< std::vector< int > >") - - if self.xml_generator_from_xml_file.is_castxml: - self.__cmp_traits( - 'mhs_v_int', - declarations.unordered_multiset_traits, - "unordered_multiset< std::vector< int > >") - else: - self.__cmp_traits( - 'mhs_v_int', - declarations.hash_multiset_traits, - "hash_multiset< std::vector< int > >") - - if self.xml_generator_from_xml_file.is_castxml: - self.__cmp_traits( - 'hm_i2d', - declarations.unordered_map_traits, - "unordered_map< int, double >", - 'int') - else: - self.__cmp_traits( - 'hm_i2d', - declarations.hash_map_traits, - "hash_map< int, double >", - 'int') - - if self.xml_generator_from_xml_file.is_castxml: - self.__cmp_traits( - 'hmm_i2d', - declarations.unordered_multimap_traits, - "unordered_multimap< int, double >", - 'int') - else: - self.__cmp_traits( - 'hmm_i2d', - declarations.hash_multimap_traits, - "hash_multimap< int, double >", - 'int') - - def test_multimap(self): - m = self.global_ns.class_( - lambda decl: decl.name.startswith('multimap')) - declarations.find_container_traits(m) - self.assertTrue(m.partial_name == 'multimap< int, int >') - - def test_recursive_partial_name(self): - f1 = self.global_ns.free_function('f1') - t1 = declarations.class_traits.get_declaration( - f1.arguments[0].decl_type) - self.assertTrue( - 'type< std::set< std::vector< int > > >' == t1.partial_name) - - def test_remove_defaults_partial_name_namespace(self): - f2 = self.global_ns.free_function('f2') - type_info = f2.return_type - traits = declarations.find_container_traits(type_info) - cls = traits.class_declaration(type_info) - # traits.remove_defaults(type_info) - decl_string = cls.partial_decl_string - key_type_string = traits.key_type(type_info).partial_decl_string - self.assertTrue( - decl_string.startswith('::std::'), - "declaration string %r doesn't start with 'std::'" % - decl_string) - self.assertTrue( - key_type_string.startswith('::std::'), - "key type string %r doesn't start with 'std::'" % - key_type_string) - - @staticmethod - def test_from_ogre(): - x = ( - 'map, ' + - 'std::allocator > >') - ct = declarations.find_container_traits(x) - ct.remove_defaults(x) - - def test_infinite_loop(self): - rt = self.global_ns.free_function('test_infinite_loop').return_type - map_traits = declarations.find_container_traits(rt) - self.assertTrue(map_traits is declarations.map_traits) - elem = map_traits.element_type(rt) - self.assertTrue(elem.decl_string == 'int') - - -def create_suite(): - suite = unittest.TestSuite() - suite.addTest( - unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test)) - return suite - - -def run_suite(): - unittest.TextTestRunner(verbosity=2).run(create_suite()) - - -if __name__ == "__main__": - run_suite() diff --git a/unittests/test_all.py b/unittests/test_all.py index 52d38bb8..d7757ecb 100644 --- a/unittests/test_all.py +++ b/unittests/test_all.py @@ -7,28 +7,18 @@ import sys import unittest -from . import declarations_tester from . import gccxml_runner_tester -from . import hierarchy_traveling from . import patcher_tester -from . import non_copyable_classes_tester from . import vector_traits_tester -from . import dependencies_tester -from . import find_container_traits_tester from . import gccxml10184_tester from . import gccxml10185_tester testers = [ - declarations_tester, gccxml_runner_tester, - hierarchy_traveling, - non_copyable_classes_tester, vector_traits_tester, - dependencies_tester, gccxml10184_tester, gccxml10185_tester, patcher_tester, - find_container_traits_tester, ]