diff --git a/tests/test_gccxml10184.py b/tests/test_gccxml10184.py index 1469dc27..0ac6ba0e 100644 --- a/tests/test_gccxml10184.py +++ b/tests/test_gccxml10184.py @@ -3,9 +3,7 @@ # 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 . import autoconfig from pygccxml import parser from pygccxml import declarations @@ -20,29 +18,9 @@ class A { }; """ - -class Test(parser_test_case.parser_test_case_t): - - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - - def test(self): - decls = parser.parse_string(code, self.config) - global_ns = declarations.get_global_namespace(decls) - self.assertTrue(global_ns.variable('a').bits == 1) - self.assertTrue(global_ns.variable('unused').bits == 31) - - -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() +def test_gccxml_10184(): + config = autoconfig.cxx_parsers_cfg.config.clone() + decls = parser.parse_string(code, config) + global_ns = declarations.get_global_namespace(decls) + assert global_ns.variable('a').bits == 1 + assert global_ns.variable('unused').bits == 31 diff --git a/tests/test_gccxml10185.py b/tests/test_gccxml10185.py index 9a6ba293..51e3f558 100644 --- a/tests/test_gccxml10185.py +++ b/tests/test_gccxml10185.py @@ -3,9 +3,9 @@ # Distributed under the Boost Software License, Version 1.0. # See http://www.boost.org/LICENSE_1_0.txt -import unittest +import pytest -from . import parser_test_case +from . import autoconfig from pygccxml import parser from pygccxml import declarations @@ -18,43 +18,23 @@ """ -class Test(parser_test_case.parser_test_case_t): - - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - - def test(self): - """ - The purpose of this test was to check if changes to GCCXML - would lead to changes in the outputted xml file (Meaning - the bug was fixed). - - GCCXML wrongly outputted partial template specialization. - CastXML does not have this bug. In this case we check if - the template specialization can not be found; which is the - expected/wanted behaviour. - - https://github.com/CastXML/CastXML/issues/20 - - """ - - decls = parser.parse_string(code, self.config) - global_ns = declarations.get_global_namespace(decls) - self.assertRaises( - declarations.declaration_not_found_t, - lambda: global_ns.class_('A')) - - -def create_suite(): - suite = unittest.TestSuite() - suite.addTest( - unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test)) - return suite +def test_partial_template(): + """ + The purpose of this test was to check if changes to GCCXML + would lead to changes in the outputted xml file (Meaning + the bug was fixed). + GCCXML wrongly outputted partial template specialization. + CastXML does not have this bug. In this case we check if + the template specialization can not be found; which is the + expected/wanted behaviour. -def run_suite(): - unittest.TextTestRunner(verbosity=2).run(create_suite()) + https://github.com/CastXML/CastXML/issues/20 + """ -if __name__ == "__main__": - run_suite() + config = autoconfig.cxx_parsers_cfg.config.clone() + decls = parser.parse_string(code, config) + global_ns = declarations.get_global_namespace(decls) + with pytest.raises(declarations.declaration_not_found_t): + global_ns.class_('A') diff --git a/tests/test_has_binary_operator_traits.py b/tests/test_has_binary_operator_traits.py index 699b15c7..e7eb0f00 100644 --- a/tests/test_has_binary_operator_traits.py +++ b/tests/test_has_binary_operator_traits.py @@ -3,72 +3,45 @@ # Distributed under the Boost Software License, Version 1.0. # See http://www.boost.org/LICENSE_1_0.txt -import unittest +import pytest -from . import parser_test_case +from . import autoconfig from pygccxml import parser from pygccxml import declarations +TEST_FILES = ["has_public_binary_operator_traits.hpp"] -class Test(parser_test_case.parser_test_case_t): - COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE - global_ns = None - - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.header = 'has_public_binary_operator_traits.hpp' - self.global_ns = None - - def setUp(self): - if not Test.global_ns: - decls = parser.parse([self.header], self.config) - Test.global_ns = declarations.get_global_namespace(decls) - self.global_ns = Test.global_ns - def test_yes_equal(self): - yes_ns = self.global_ns.namespace('yesequal') - for typedef in yes_ns.typedefs(): - self.assertTrue( - declarations.has_public_equal(typedef), - "Class '%s' should have public operator==" % - typedef.decl_string) - - def test_no_equal(self): - no_ns = self.global_ns.namespace('noequal') - for typedef in no_ns.typedefs(): - self.assertTrue( - not declarations.has_public_equal(typedef), - "Class '%s' should not have public operator==" % - typedef.decl_string) +@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 test_yes_less(self): - yes_ns = self.global_ns.namespace('yesless') - for typedef in yes_ns.typedefs(): - self.assertTrue( - declarations.has_public_less(typedef), - "Class '%s' should have public operator<" % - typedef.decl_string) - def test_no_less(self): - no_ns = self.global_ns.namespace('noless') - for typedef in no_ns.typedefs(): - self.assertTrue( - not declarations.has_public_less(typedef), - "Class '%s' should not have public operator<" % - typedef.decl_string) +def test_yes_equal(global_ns): + yes_ns = global_ns.namespace('yesequal') + for typedef in yes_ns.typedefs(): + assert declarations.has_public_equal(typedef) is True -def create_suite(): - suite = unittest.TestSuite() - suite.addTest( - unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test)) - return suite +def test_no_equal(global_ns): + no_ns = global_ns.namespace('noequal') + for typedef in no_ns.typedefs(): + assert declarations.has_public_equal(typedef) is False -def run_suite(): - unittest.TextTestRunner(verbosity=2).run(create_suite()) +def test_yes_less(global_ns): + yes_ns = global_ns.namespace('yesless') + for typedef in yes_ns.typedefs(): + assert declarations.has_public_less(typedef) -if __name__ == "__main__": - run_suite() +def test_no_less(global_ns): + no_ns = global_ns.namespace('noless') + for typedef in no_ns.typedefs(): + assert declarations.has_public_less(typedef) is False diff --git a/tests/test_hash.py b/tests/test_hash.py index f455ffe6..a994cdfd 100644 --- a/tests/test_hash.py +++ b/tests/test_hash.py @@ -3,69 +3,62 @@ # Distributed under the Boost Software License, Version 1.0. # See http://www.boost.org/LICENSE_1_0.txt -import sys -import unittest import inspect from pygccxml import declarations -class Test(unittest.TestCase): +def test_types_hashes(): + """ + Test if all the type_t instances implement a hash method. - def test_types_hashes(self): - """ - Test if all the type_t instances implement a hash method. + The hash is part of the public API, as there are multiple tools + that rely on it to compare type_t instances. - The hash is part of the public API, as there are multiple tools - that rely on it to compare type_t instances. + The best way to test this is to instanciate dummy type_t objects + for each class that subclasses type_t, and check that the hash of + these objects is not None. - The best way to test this is to instanciate dummy type_t objects - for each class that subclasses type_t, and check that the hash of - these objects is not None. + """ - """ + members = inspect.getmembers(declarations, inspect.isclass) + for member in members: + member_type = member[1] + is_type_t_subclass = issubclass(member_type, declarations.type_t) + is_not_type_t = member_type != declarations.type_t + if is_type_t_subclass and is_not_type_t: + type_mockup = _create_type_t_mockup(member_type) + assert hash(type_mockup) is not None - if sys.version_info[:2] == (2, 7): - # _create_type_t_mockup calls inspect.signature, which does not - # exist for legacy Python - return - members = inspect.getmembers(declarations, inspect.isclass) - for member in members: - member_type = member[1] - is_type_t_subclass = issubclass(member_type, declarations.type_t) - is_not_type_t = member_type != declarations.type_t - if is_type_t_subclass and is_not_type_t: - type_mockup = _create_type_t_mockup(member_type) - self.assertIsNotNone(hash(type_mockup)) +def test_declarations_hashes(): + """ + Test if all the declaration_t instances implement a hash method. - def test_declarations_hashes(self): - """ - Test if all the declaration_t instances implement a hash method. + The hash is part of the public API, as there are multiple tools + that rely on it to compare declaration_t instances. - The hash is part of the public API, as there are multiple tools - that rely on it to compare declaration_t instances. + The best way to test this is to instanciate dummy declaration_t objects + for each class that subclasses declaration_t, and check that the hash + of these objects is not None. - The best way to test this is to instanciate dummy declaration_t objects - for each class that subclasses declaration_t, and check that the hash - of these objects is not None. + """ + members = inspect.getmembers(declarations, inspect.isclass) + for member in members: + member_type = member[1] + if issubclass(member_type, declarations.declaration_t): + assert hash(member_type()) is not None - """ - members = inspect.getmembers(declarations, inspect.isclass) - for member in members: - member_type = member[1] - if issubclass(member_type, declarations.declaration_t): - self.assertIsNotNone(hash(member_type())) - def test_type_qualifiers_t_hash(self): - """ - Test if the type_qualifiers_t instance implements a hash method. +def test_type_qualifiers_t_hash(): + """ + Test if the type_qualifiers_t instance implements a hash method. - The hash is part of the public API, as there are multiple tools - that rely on it to compare type_qualifiers_t instances. + The hash is part of the public API, as there are multiple tools + that rely on it to compare type_qualifiers_t instances. - """ - self.assertIsNotNone(hash(declarations.type_qualifiers_t())) + """ + assert hash(declarations.type_qualifiers_t()) is not None def _create_type_t_mockup(member_type): @@ -91,18 +84,3 @@ def __init__(self): def build_decl_string(self, with_defaults=False): return self._decl_string - - -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/tests/test_hierarchy_traveling.py b/tests/test_hierarchy_traveling.py index 06737725..d299d817 100644 --- a/tests/test_hierarchy_traveling.py +++ b/tests/test_hierarchy_traveling.py @@ -4,88 +4,75 @@ # See http://www.boost.org/LICENSE_1_0.txt import os -import unittest -from . import parser_test_case +from . import autoconfig from pygccxml import parser from pygccxml import declarations -class Test(parser_test_case.parser_test_case_t): - - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.__code = os.linesep.join(['struct a{};', - 'struct b{};', - 'struct c{};', - 'struct d : public a{};', - 'struct e : public a, public b{};', - 'struct f{};', - 'struct g : public d, public f{};', - 'struct h : public f{};', - 'struct i : public h, public g{};']) - - self.__recursive_bases = {'a': set(), - 'b': set(), - 'c': set(), - 'd': {'a'}, - 'e': {'a', 'b'}, - 'f': set(), - 'g': {'d', 'f', 'a'}, - 'h': {'f'}, - 'i': {'h', 'g', 'd', 'f', 'a'}} - - self.__recursive_derived = { - 'a': {'d', 'e', 'g', 'i'}, - 'b': {'e'}, - 'c': set(), - 'd': {'g', 'i'}, - 'e': set(), - 'f': {'g', 'h', 'i'}, - 'g': {'i'}, - 'h': {'i'}, - 'i': set()} - - def test_recursive_bases(self): - src_reader = parser.source_reader_t(self.config) - decls = declarations.make_flatten(src_reader.read_string(self.__code)) - classes = [ - inst for inst in decls if isinstance(inst, declarations.class_t)] - for class_ in classes: - self.assertTrue(class_.name in self.__recursive_bases) - all_bases = class_.recursive_bases - control_bases = self.__recursive_bases[class_.name] - self.assertTrue(len(control_bases) == len(all_bases)) - all_bases_names = [hi.related_class.name for hi in all_bases] - self.assertTrue(set(all_bases_names) == control_bases) - - def test_recursive_derived(self): - src_reader = parser.source_reader_t(self.config) - decls = declarations.make_flatten(src_reader.read_string(self.__code)) - classes = [ - inst for inst in decls if isinstance( - inst, - declarations.class_t)] - for class_ in classes: - self.assertTrue(class_.name in self.__recursive_derived) - all_derived = class_.recursive_derived - control_derived = self.__recursive_derived[class_.name] - self.assertTrue(len(control_derived) == len(all_derived)) - all_derived_names = [hi.related_class.name for hi in all_derived] - self.assertTrue(set(all_derived_names) == control_derived) - - -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() +__code = os.linesep.join([ + 'struct a{};', + 'struct b{};', + 'struct c{};', + 'struct d : public a{};', + 'struct e : public a, public b{};', + 'struct f{};', + 'struct g : public d, public f{};', + 'struct h : public f{};', + 'struct i : public h, public g{};']) + + +__recursive_bases = { + 'a': set(), + 'b': set(), + 'c': set(), + 'd': {'a'}, + 'e': {'a', 'b'}, + 'f': set(), + 'g': {'d', 'f', 'a'}, + 'h': {'f'}, + 'i': {'h', 'g', 'd', 'f', 'a'}} + +__recursive_derived = { + 'a': {'d', 'e', 'g', 'i'}, + 'b': {'e'}, + 'c': set(), + 'd': {'g', 'i'}, + 'e': set(), + 'f': {'g', 'h', 'i'}, + 'g': {'i'}, + 'h': {'i'}, + 'i': set()} + + +def test_recursive_bases(): + config = autoconfig.cxx_parsers_cfg.config.clone() + src_reader = parser.source_reader_t(config) + decls = declarations.make_flatten(src_reader.read_string(__code)) + classes = [ + inst for inst in decls if isinstance(inst, declarations.class_t)] + for class_ in classes: + assert class_.name in __recursive_bases + all_bases = class_.recursive_bases + control_bases = __recursive_bases[class_.name] + assert len(control_bases) == len(all_bases) + all_bases_names = [hi.related_class.name for hi in all_bases] + assert set(all_bases_names) == control_bases + + +def test_recursive_derived(): + config = autoconfig.cxx_parsers_cfg.config.clone() + src_reader = parser.source_reader_t(config) + decls = declarations.make_flatten(src_reader.read_string(__code)) + classes = [ + inst for inst in decls if isinstance( + inst, + declarations.class_t)] + for class_ in classes: + assert class_.name in __recursive_derived + all_derived = class_.recursive_derived + control_derived = __recursive_derived[class_.name] + assert len(control_derived) == len(all_derived) + all_derived_names = [hi.related_class.name for hi in all_derived] + assert set(all_derived_names) == control_derived diff --git a/tests/test_inline_specifier.py b/tests/test_inline_specifier.py index dfc3e420..6daf02bd 100644 --- a/tests/test_inline_specifier.py +++ b/tests/test_inline_specifier.py @@ -3,53 +3,34 @@ # Distributed under the Boost Software License, Version 1.0. # See http://www.boost.org/LICENSE_1_0.txt -import unittest +import pytest -from . import parser_test_case +from . import autoconfig from pygccxml import parser from pygccxml import declarations +TEST_FILES = ["inline_specifier.hpp"] -class Test(parser_test_case.parser_test_case_t): - global_ns = None +@pytest.fixture +def global_ns(): + COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE + config = autoconfig.cxx_parsers_cfg.config.clone() + config.cflags = "-std=c++11" + decls = parser.parse(TEST_FILES, config, COMPILATION_MODE) + global_ns = declarations.get_global_namespace(decls) + global_ns.init_optimizer() + return global_ns - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.header = 'inline_specifier.hpp' - def setUp(self): - if not Test.global_ns: - decls = parser.parse([self.header], self.config) - Test.global_ns = declarations.get_global_namespace(decls) - Test.global_ns.init_optimizer() +def test_inline_specifier(global_ns): + inlined_funcs = global_ns.calldefs('inlined') + assert len(inlined_funcs) != 0 + for f in inlined_funcs: + assert f.has_inline is True - def test(self): - inlined_funcs = self.global_ns.calldefs('inlined') - self.assertTrue(len(inlined_funcs)) - for f in inlined_funcs: - self.assertTrue(f.has_inline) - - not_inlined_funcs = self.global_ns.calldefs('not_inlined') - self.assertTrue(len(not_inlined_funcs)) - for f in not_inlined_funcs: - self.assertTrue(f.has_inline is False) - - def test2(self): - pass - - -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() + not_inlined_funcs = global_ns.calldefs('not_inlined') + assert len(not_inlined_funcs) != 0 + for f in not_inlined_funcs: + assert f.has_inline is False diff --git a/tests/test_map_gcc5.py b/tests/test_map_gcc5.py index bf579e2f..cbd790ce 100644 --- a/tests/test_map_gcc5.py +++ b/tests/test_map_gcc5.py @@ -3,57 +3,38 @@ # 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 . import autoconfig from pygccxml import parser from pygccxml import declarations +TEST_FILES = ["test_map_gcc5.hpp"] -class Test(parser_test_case.parser_test_case_t): - - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.header = "test_map_gcc5.hpp" - self.config.cflags = "-std=c++11" - - def test_map_gcc5(self): - """ - The code in test_map_gcc5.hpp was breaking pygccxml. - - Test that case (gcc5 + castxml + c++11). - - See issue #45 and #55 - - """ - - decls = parser.parse([self.header], self.config) - global_ns = declarations.get_global_namespace(decls) - # This calldef is defined with gcc > 4.9 (maybe earlier, not tested) - # and -std=c++11. Calling create_decl_string is failing with gcc. - # With clang the calldef does not exist so the matcher - # will just return an empty list, letting the test pass. - # See the test_argument_without_name.py for an equivalent test, - # which is not depending on the presence of the _M_clone_node - # method in the stl_tree.h file. - criteria = declarations.calldef_matcher(name="_M_clone_node") - free_funcs = declarations.matcher.find(criteria, global_ns) - for free_funcs in free_funcs: - free_funcs.create_decl_string(with_defaults=False) +def test_map_gcc5(): + """ + The code in test_map_gcc5.hpp was breaking pygccxml. + Test that case (gcc5 + castxml + c++11). -def create_suite(): - suite = unittest.TestSuite() - suite.addTest( - unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test)) - return suite + See issue #45 and #55 + """ -def run_suite(): - unittest.TextTestRunner(verbosity=2).run(create_suite()) + config = autoconfig.cxx_parsers_cfg.config.clone() + config.cflags = "-std=c++11" + decls = parser.parse(TEST_FILES, config) + global_ns = declarations.get_global_namespace(decls) -if __name__ == "__main__": - run_suite() + # This calldef is defined with gcc > 4.9 (maybe earlier, not tested) + # and -std=c++11. Calling create_decl_string is failing with gcc. + # With clang the calldef does not exist so the matcher + # will just return an empty list, letting the test pass. + # See the test_argument_without_name.py for an equivalent test, + # which is not depending on the presence of the _M_clone_node + # method in the stl_tree.h file. + criteria = declarations.calldef_matcher(name="_M_clone_node") + free_funcs = declarations.matcher.find(criteria, global_ns) + for free_funcs in free_funcs: + free_funcs.create_decl_string(with_defaults=False) diff --git a/tests/test_namespace_matcher.py b/tests/test_namespace_matcher.py index 9c1b1c7b..01dcf51e 100644 --- a/tests/test_namespace_matcher.py +++ b/tests/test_namespace_matcher.py @@ -3,70 +3,32 @@ # 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 . import autoconfig from pygccxml import parser from pygccxml import declarations - -class Test(parser_test_case.parser_test_case_t): - COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE - - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.header = 'bit_fields.hpp' - self.declarations = None - - def setUp(self): - if not self.declarations: - self.declarations = parser.parse([self.header], self.config) - - def test(self): - criteria = declarations.namespace_matcher_t(name='bit_fields') - declarations.matcher.get_single(criteria, self.declarations) - self.assertTrue( - str(criteria) == '(decl type==namespace_t) and (name==bit_fields)') - - def test_allow_empty(self): - global_ns = declarations.get_global_namespace(self.declarations) - global_ns.init_optimizer() - self.assertTrue( - 0 == len(global_ns.namespaces('does not exist', allow_empty=True))) - - -class unnamed_ns_tester_t(parser_test_case.parser_test_case_t): - COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE - - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.header = 'unnamed_ns_bug.hpp' - self.declarations = None - - def setUp(self): - if not self.declarations: - self.declarations = parser.parse([self.header], self.config) - - def test(self): - declarations.matcher.get_single( - declarations.namespace_matcher_t(name='::'), self.declarations) +TEST_FILES1 = ["bit_fields.hpp"] +TEST_FILES2 = ["unnamed_ns_bug.hpp"] -def create_suite(): - suite = unittest.TestSuite() - suite.addTest( - unittest.TestLoader().loadTestsFromTestCase( - testCaseClass=Test)) - suite.addTest( - unittest.TestLoader().loadTestsFromTestCase( - testCaseClass=unnamed_ns_tester_t)) - return suite +def test_namespace_matcher_get_single(): + config = autoconfig.cxx_parsers_cfg.config.clone() + decls = parser.parse(TEST_FILES1, config) + criteria = declarations.namespace_matcher_t(name='bit_fields') + declarations.matcher.get_single(criteria, decls) + assert str(criteria) == '(decl type==namespace_t) and (name==bit_fields)' -def run_suite(): - unittest.TextTestRunner(verbosity=2).run(create_suite()) +def test_namespace_matcher_allow_empty(): + config = autoconfig.cxx_parsers_cfg.config.clone() + decls = parser.parse(TEST_FILES1, config) + global_ns = declarations.get_global_namespace(decls) + assert 0 == len(global_ns.namespaces('does not exist', allow_empty=True)) -if __name__ == "__main__": - run_suite() +def test_namespace_matcher_upper(): + config = autoconfig.cxx_parsers_cfg.config.clone() + decls = parser.parse(TEST_FILES2, config) + declarations.matcher.get_single( + declarations.namespace_matcher_t(name='::'), decls) diff --git a/tests/test_non_copyable_classes.py b/tests/test_non_copyable_classes.py index e9405a5d..b3d88786 100644 --- a/tests/test_non_copyable_classes.py +++ b/tests/test_non_copyable_classes.py @@ -3,77 +3,55 @@ # Distributed under the Boost Software License, Version 1.0. # See http://www.boost.org/LICENSE_1_0.txt -import unittest +import pytest -from . import parser_test_case +from . import autoconfig from pygccxml import parser from pygccxml import declarations +TEST_FILES = ["non_copyable_classes.hpp"] -class Test(parser_test_case.parser_test_case_t): - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.header = 'non_copyable_classes.hpp' - self.global_ns = None +@pytest.fixture +def global_ns(): + COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE + config = autoconfig.cxx_parsers_cfg.config.clone() + config.cflags = "-std=c++11" + decls = parser.parse(TEST_FILES, config, COMPILATION_MODE) + global_ns = declarations.get_global_namespace(decls) + global_ns.init_optimizer() + return global_ns - def setUp(self): - if not self.global_ns: - decls = parser.parse([self.header], self.config) - self.global_ns = declarations.get_global_namespace(decls) - self.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 - def test(self): +def test(global_ns): + """ + Search for classes which can not be copied. - """ - Search for classes which can not be copied. + See bug #13 - See bug #13 + 1) non copyable class + 2) non copyable const variable (fundamental type) + 3) non copyable const variable (class type) + 4) non copyable const variable (array type) + 5) non copyable const variable (class type) - 1) non copyable class - 2) non copyable const variable (fundamental type) - 3) non copyable const variable (class type) - 4) non copyable const variable (array type) - 5) non copyable const variable (class type) + """ - """ + main_foo_1 = global_ns.class_('MainFoo1') + assert declarations.is_noncopyable(main_foo_1) is True - main_foo_1 = self.global_ns.class_('MainFoo1') - self.assertTrue(declarations.is_noncopyable(main_foo_1)) + main_foo_2 = global_ns.class_('MainFoo2') + assert declarations.is_noncopyable(main_foo_2) is True - main_foo_2 = self.global_ns.class_('MainFoo2') - self.assertTrue(declarations.is_noncopyable(main_foo_2)) + main_foo_3 = global_ns.class_('MainFoo3') + assert declarations.is_noncopyable(main_foo_3) is True - main_foo_3 = self.global_ns.class_('MainFoo3') - self.assertTrue(declarations.is_noncopyable(main_foo_3)) + main_foo_4 = global_ns.class_('MainFoo4') + assert declarations.is_noncopyable(main_foo_4) is True - main_foo_4 = self.global_ns.class_('MainFoo4') - self.assertTrue(declarations.is_noncopyable(main_foo_4)) + main_foo_5 = global_ns.class_('MainFoo5') + assert declarations.is_noncopyable(main_foo_5) is True - main_foo_5 = self.global_ns.class_('MainFoo5') - self.assertTrue(declarations.is_noncopyable(main_foo_5)) - - if self.xml_generator_from_xml_file.is_castxml: - # CastXML only test - # MainFoo6 is copyable - main_foo_6 = self.global_ns.class_('MainFoo6') - self.assertFalse(declarations.is_noncopyable(main_foo_6)) - - -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() + main_foo_6 = global_ns.class_('MainFoo6') + assert declarations.is_noncopyable(main_foo_6) is False diff --git a/tests/test_non_copyable_recursive.py b/tests/test_non_copyable_recursive.py index cd78a9ae..fe365447 100644 --- a/tests/test_non_copyable_recursive.py +++ b/tests/test_non_copyable_recursive.py @@ -3,78 +3,60 @@ # 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 . import autoconfig from pygccxml import parser from pygccxml import declarations - -class Test(parser_test_case.parser_test_case_t): - - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.header = "test_non_copyable_recursive.hpp" - - def test_infinite_recursion_base_classes(self): - """ - Test find_noncopyable_vars - - See #71 - - find_noncopyable_vars was throwing: - RuntimeError: maximum recursion depth exceeded while - calling a Python object - """ - decls = parser.parse([self.header], self.config) - global_ns = declarations.get_global_namespace(decls) - - # Description of the problem (before the fix): - # find_noncopyable_vars (on Child class) looks up the variables, - # and finds aBasePtr2 (a pointer to the Base2 class). - # Then it looks recursively at the base classes of Base2, and finds - # Base1. Then, it looks up the variables from Base, to check if Base1 - # is non copyable. It finds another aBasePtr2 variable, which leads to - # a new check of Base2; this recurses infinitely. - test_ns = global_ns.namespace('Test1') - cls = test_ns.class_('Child') - declarations.type_traits_classes.find_noncopyable_vars(cls) - self.assertTrue(declarations.type_traits_classes.is_noncopyable(cls)) - - def test_infinite_recursion_sstream(self): - """ - Test find_noncopyable_vars - - See #71 - - find_noncopyable_vars was throwing: - RuntimeError: maximum recursion depth exceeded while - calling a Python object - """ - decls = parser.parse([self.header], self.config) - global_ns = declarations.get_global_namespace(decls) - - # Real life example of the bug. This leads to a similar error, - # but the situation is more complex as there are multiple - # classes that are related the one to the others - # (basic_istream, basic_ios, ios_base, ...) - test_ns = global_ns.namespace('Test2') - cls = test_ns.class_('FileStreamDataStream') - declarations.type_traits_classes.find_noncopyable_vars(cls) - self.assertFalse(declarations.type_traits_classes.is_noncopyable(cls)) - - -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() +TEST_FILES = ["test_non_copyable_recursive.hpp"] + + +def test_infinite_recursion_base_classes(): + """ + Test find_noncopyable_vars + + See #71 + + find_noncopyable_vars was throwing: + RuntimeError: maximum recursion depth exceeded while + calling a Python object + """ + config = autoconfig.cxx_parsers_cfg.config.clone() + decls = parser.parse(TEST_FILES, config) + global_ns = declarations.get_global_namespace(decls) + + # Description of the problem (before the fix): + # find_noncopyable_vars (on Child class) looks up the variables, + # and finds aBasePtr2 (a pointer to the Base2 class). + # Then it looks recursively at the base classes of Base2, and finds + # Base1. Then, it looks up the variables from Base, to check if Base1 + # is non copyable. It finds another aBasePtr2 variable, which leads to + # a new check of Base2; this recurses infinitely. + test_ns = global_ns.namespace('Test1') + cls = test_ns.class_('Child') + declarations.type_traits_classes.find_noncopyable_vars(cls) + assert declarations.type_traits_classes.is_noncopyable(cls) is True + + +def test_infinite_recursion_sstream(): + """ + Test find_noncopyable_vars + + See #71 + + find_noncopyable_vars was throwing: + RuntimeError: maximum recursion depth exceeded while + calling a Python object + """ + config = autoconfig.cxx_parsers_cfg.config.clone() + decls = parser.parse(TEST_FILES, config) + global_ns = declarations.get_global_namespace(decls) + + # Real life example of the bug. This leads to a similar error, + # but the situation is more complex as there are multiple + # classes that are related the one to the others + # (basic_istream, basic_ios, ios_base, ...) + test_ns = global_ns.namespace('Test2') + cls = test_ns.class_('FileStreamDataStream') + declarations.type_traits_classes.find_noncopyable_vars(cls) + assert declarations.type_traits_classes.is_noncopyable(cls) is False diff --git a/tests/test_null_comparison.py b/tests/test_null_comparison.py index 51caf4ab..af489043 100644 --- a/tests/test_null_comparison.py +++ b/tests/test_null_comparison.py @@ -3,47 +3,29 @@ # 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 . import autoconfig from pygccxml import parser from pygccxml import declarations - -class Test(parser_test_case.parser_test_case_t): - - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.header = "null_comparison.hpp" - - def test_argument_null_comparison(self): - """ - Test for None comparisons with default arguments - """ - - decls = parser.parse([self.header], self.config) - global_ns = declarations.get_global_namespace(decls) - - ns = global_ns.namespace("ns") - - func = ns.free_function(name="TestFunction1") - assert (func.arguments[0] > func.arguments[1]) is False - - func = ns.free_function(name="TestFunction2") - assert (func.arguments[0] > func.arguments[1]) is False +TEST_FILES = [ + "null_comparison.hpp", +] -def create_suite(): - suite = unittest.TestSuite() - suite.addTest( - unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test)) - return suite +def test_argument_null_comparison(): + """ + Test for None comparisons with default arguments + """ + config = autoconfig.cxx_parsers_cfg.config.clone() + decls = parser.parse(TEST_FILES, config) + global_ns = declarations.get_global_namespace(decls) -def run_suite(): - unittest.TextTestRunner(verbosity=2).run(create_suite()) + ns = global_ns.namespace("ns") + func = ns.free_function(name="TestFunction1") + assert (func.arguments[0] > func.arguments[1]) is False -if __name__ == "__main__": - run_suite() + func = ns.free_function(name="TestFunction2") + assert (func.arguments[0] > func.arguments[1]) is False diff --git a/tests/test_pattern_parser.py b/tests/test_pattern_parser.py index 0567db88..2f3d8532 100644 --- a/tests/test_pattern_parser.py +++ b/tests/test_pattern_parser.py @@ -3,171 +3,167 @@ # Distributed under the Boost Software License, Version 1.0. # See http://www.boost.org/LICENSE_1_0.txt -import unittest +import pytest -from . import parser_test_case +from . import autoconfig from pygccxml import parser from pygccxml import declarations -class Test(parser_test_case.parser_test_case_t): - - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.header = "test_pattern_parser.hpp" - self.config.cflags = "-std=c++11" - - def test_template_split_std_vector(self): - """ - Demonstrate error in pattern parser, see #60 - - """ - - decls = parser.parse([self.header], self.config) - - for decl in declarations.make_flatten(decls): - if "myClass" in decl.name: - _ = decl.partial_name - - def test_matcher(self): - """ - Run the matcher on all the templated classes. - - This exercises the whole pipeline even more. - - """ - - decls = parser.parse([self.header], self.config) - global_ns = declarations.get_global_namespace(decls) - criteria = declarations.declaration_matcher(name="myClass") - _ = declarations.matcher.find(criteria, global_ns) - - def test_split(self): - """ - Test a bunch of tricky name/args splits. More combinations could be - tested but this is already covering most of the cases. - - In test_template_split_std_vector we test for a specific case that - was failing (in a real world scenario). - Here we test more possible combinations to make sure the splitting - method is robust enough. - - """ - - p1 = "std::vector >" - p2 = "std::vector >" - args_list = [ - "const std::basic_string &", "const int &", "const double &"] - - for arg in args_list: - - li = [p1] - name, args = declarations.templates.split( - "myClass0a<" + ", ".join(li) + ">") - self.assertEqual(name, "myClass0a") - self.assertEqual(args, li) - - li = [p1, p2] - name, args = declarations.templates.split( - "myClass0b<" + ", ".join(li) + ">") - self.assertEqual(name, "myClass0b") - self.assertEqual(args, li) - - li = [p1, p2, p2] - name, args = declarations.templates.split( - "myClass0c<" + ", ".join(li) + ">") - self.assertEqual(name, "myClass0c") - self.assertEqual(args, li) - - li = [p1 + " (" + arg + ")"] - name, args = declarations.templates.split( - "myClass1<" + ", ".join(li) + ">") - self.assertEqual(name, "myClass1") - self.assertEqual(args, li) - - li = [p1 + " (" + arg + ", " + arg + ")"] - name, args = declarations.templates.split( - "myClass2<" + ", ".join(li) + ">") - self.assertEqual(name, "myClass2") - self.assertEqual(args, li) - - li = [p2 + " (" + arg + ", " + arg + ")"] - name, args = declarations.templates.split( - "myClass3<" + ", ".join(li) + ">") - self.assertEqual(name, "myClass3") - self.assertEqual(args, li) - - li = [p1 + " (" + arg + ", " + arg + ", " + arg + ")"] - name, args = declarations.templates.split( - "myClass4<" + ", ".join(li) + ">") - self.assertEqual(name, "myClass4") - self.assertEqual(args, li) - - li = [ - p1 + " (" + arg + ", " + arg + ", " + arg + ")", - p1] - name, args = declarations.templates.split( - "myClass5<" + ", ".join(li) + ">") - self.assertEqual(name, "myClass5") - self.assertEqual(args, li) - - li = [ - p1, - p1 + " (" + arg + ", " + arg + ", " + arg + ")"] - name, args = declarations.templates.split( - "myClass6<" + ", ".join(li) + ">") - self.assertEqual(name, "myClass6") - self.assertEqual(args, li) - - li = [ - p2 + " (" + arg + ")", - p1, - p1 + " (" + arg + ", " + arg + ", " + arg + ")"] - name, args = declarations.templates.split( - "myClass7<" + ", ".join(li) + ">") - self.assertEqual(name, "myClass7") - self.assertEqual(args, li) - - li = [ - p1, - p2 + " (" + arg + ")", - p1 + " (" + arg + ", " + arg + ", " + arg + ")"] - name, args = declarations.templates.split( - "myClass8<" + ", ".join(li) + ">") - self.assertEqual(name, "myClass8") - self.assertEqual(args, li) - - li = [ - p2 + " (" + arg + ")", - p1 + " (" + arg + ", " + arg + ")", - p1] - name, args = declarations.templates.split( - "myClass9<" + ", ".join(li) + ">") - self.assertEqual(name, "myClass9") - self.assertEqual(args, li) - - li = [ - p2 + " (" + arg + ")", - p1 + " (" + arg + ", " + arg + ", " + arg + ")", - p1, - p2] - name, args = declarations.templates.split( - "myClass10<" + ", ".join(li) + ">") - self.assertEqual(name, "myClass10") - self.assertEqual(args, li) - - -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() +TEST_FILES = [ + 'test_pattern_parser.hpp' +] + + +@pytest.fixture +def global_ns(): + COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE + config = autoconfig.cxx_parsers_cfg.config.clone() + config.cflags = "-std=c++11" + decls = parser.parse(TEST_FILES, config, COMPILATION_MODE) + global_ns = declarations.get_global_namespace(decls) + global_ns.init_optimizer() + return global_ns + + +def test_template_split_std_vector(global_ns): + """ + Demonstrate error in pattern parser, see #60 + + """ + + config = autoconfig.cxx_parsers_cfg.config.clone() + config.cflags = "-std=c++11" + decls = parser.parse(TEST_FILES, config) + + for decl in declarations.make_flatten(decls): + if "myClass" in decl.name: + _ = decl.partial_name + + +def test_matcher(global_ns): + """ + Run the matcher on all the templated classes. + + This exercises the whole pipeline even more. + + """ + + criteria = declarations.declaration_matcher(name="myClass") + _ = declarations.matcher.find(criteria, global_ns) + + +def test_split(): + """ + Test a bunch of tricky name/args splits. More combinations could be + tested but this is already covering most of the cases. + + In test_template_split_std_vector we test for a specific case that + was failing (in a real world scenario). + Here we test more possible combinations to make sure the splitting + method is robust enough. + + """ + + p1 = "std::vector >" + p2 = "std::vector >" + args_list = [ + "const std::basic_string &", "const int &", "const double &"] + + for arg in args_list: + + li = [p1] + name, args = declarations.templates.split( + "myClass0a<" + ", ".join(li) + ">") + assert name == "myClass0a" + assert args == li + + li = [p1, p2] + name, args = declarations.templates.split( + "myClass0b<" + ", ".join(li) + ">") + assert name == "myClass0b" + assert args == li + + li = [p1, p2, p2] + name, args = declarations.templates.split( + "myClass0c<" + ", ".join(li) + ">") + assert name == "myClass0c" + assert args == li + + li = [p1 + " (" + arg + ")"] + name, args = declarations.templates.split( + "myClass1<" + ", ".join(li) + ">") + assert name == "myClass1" + assert args == li + + li = [p1 + " (" + arg + ", " + arg + ")"] + name, args = declarations.templates.split( + "myClass2<" + ", ".join(li) + ">") + assert name == "myClass2" + assert args == li + + li = [p2 + " (" + arg + ", " + arg + ")"] + name, args = declarations.templates.split( + "myClass3<" + ", ".join(li) + ">") + assert name == "myClass3" + assert args == li + + li = [p1 + " (" + arg + ", " + arg + ", " + arg + ")"] + name, args = declarations.templates.split( + "myClass4<" + ", ".join(li) + ">") + assert name == "myClass4" + assert args == li + + li = [ + p1 + " (" + arg + ", " + arg + ", " + arg + ")", + p1] + name, args = declarations.templates.split( + "myClass5<" + ", ".join(li) + ">") + assert name == "myClass5" + assert args == li + + li = [ + p1, + p1 + " (" + arg + ", " + arg + ", " + arg + ")"] + name, args = declarations.templates.split( + "myClass6<" + ", ".join(li) + ">") + assert name == "myClass6" + assert args == li + + li = [ + p2 + " (" + arg + ")", + p1, + p1 + " (" + arg + ", " + arg + ", " + arg + ")"] + name, args = declarations.templates.split( + "myClass7<" + ", ".join(li) + ">") + assert name == "myClass7" + assert args == li + + li = [ + p1, + p2 + " (" + arg + ")", + p1 + " (" + arg + ", " + arg + ", " + arg + ")"] + name, args = declarations.templates.split( + "myClass8<" + ", ".join(li) + ">") + assert name == "myClass8" + assert args == li + + li = [ + p2 + " (" + arg + ")", + p1 + " (" + arg + ", " + arg + ")", + p1] + name, args = declarations.templates.split( + "myClass9<" + ", ".join(li) + ">") + assert name == "myClass9" + assert args == li + + li = [ + p2 + " (" + arg + ")", + p1 + " (" + arg + ", " + arg + ", " + arg + ")", + p1, + p2] + name, args = declarations.templates.split( + "myClass10<" + ", ".join(li) + ">") + assert name == "myClass10" + assert args == li diff --git a/tests/test_plain_c.py b/tests/test_plain_c.py index 1014d46e..da66ac34 100644 --- a/tests/test_plain_c.py +++ b/tests/test_plain_c.py @@ -3,45 +3,31 @@ # Distributed under the Boost Software License, Version 1.0. # See http://www.boost.org/LICENSE_1_0.txt -import unittest +import pytest -from . import parser_test_case +from . import autoconfig from pygccxml import parser from pygccxml import declarations +TEST_FILES = [ + 'plain_c.c' +] -class Test(parser_test_case.parser_test_case_t): - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.header = 'plain_c.c' - self.global_ns = None +@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 setUp(self): - if not self.global_ns: - decls = parser.parse([self.header], self.config) - self.global_ns = declarations.get_global_namespace(decls) - self.global_ns.init_optimizer() - def test(self): - self.global_ns.free_function('hello_sum') - self.global_ns.free_function('hello_print') - f = self.global_ns.free_function('do_smth') - for arg in f.arguments: - self.assertTrue(arg.decl_type.decl_string) - - -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() +def test_plain_c(global_ns): + global_ns.free_function('hello_sum') + global_ns.free_function('hello_print') + f = global_ns.free_function('do_smth') + for arg in f.arguments: + assert arg.decl_type.decl_string is not None diff --git a/tests/test_project_reader_correctness.py b/tests/test_project_reader_correctness.py index dde56600..ee2c8e88 100644 --- a/tests/test_project_reader_correctness.py +++ b/tests/test_project_reader_correctness.py @@ -4,106 +4,79 @@ # See http://www.boost.org/LICENSE_1_0.txt import os -import unittest from . import autoconfig -from . import parser_test_case from pygccxml import parser from pygccxml import declarations - -class Test(parser_test_case.parser_test_case_t): - - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.__files = [ - 'core_types.hpp', - 'core_ns_join_1.hpp', - 'core_ns_join_2.hpp', - 'core_ns_join_3.hpp', - 'core_membership.hpp', - 'core_class_hierarchy.hpp', - 'core_diamand_hierarchy_base.hpp', - 'core_diamand_hierarchy_derived1.hpp', - 'core_diamand_hierarchy_derived2.hpp', - 'core_diamand_hierarchy_final_derived.hpp', - 'core_overloads_1.hpp', - 'core_overloads_2.hpp'] - - def __test_correctness_impl(self, file_name): - prj_reader = parser.project_reader_t(self.config) - prj_decls = prj_reader.read_files( - [file_name] * 2, - compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE) - src_reader = parser.source_reader_t(self.config) - src_decls = src_reader.read_file(file_name) - if src_decls != prj_decls: - s = src_decls[0] - p = prj_decls[0] - bdir = autoconfig.build_directory - with open(os.path.join(bdir, file_name + '.sr.txt'), 'w+') as sr: - with open( - os.path.join(bdir, file_name + '.pr.txt'), 'w+') as pr: - - declarations.print_declarations( - s, writer=lambda x: sr.write(l + os.linesep)) - declarations.print_declarations( - p, writer=lambda x: pr.write(l + os.linesep)) - - self.fail( - "There is a difference between declarations in file %s." % - file_name) - - def test_correctness(self): - for src in self.__files: - self.__test_correctness_impl(src) - - -class tester2_t(parser_test_case.parser_test_case_t): - - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.__files = [ - 'separate_compilation/data.h', - 'separate_compilation/base.h', - 'separate_compilation/derived.h'] - - def test(self): - prj_reader = parser.project_reader_t(self.config) - prj_decls = prj_reader.read_files( - self.__files, - compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE) - src_reader = parser.source_reader_t(self.config) - src_decls = src_reader.read_file('separate_compilation/all.h') - - declarations.dump_declarations( - src_decls, - os.path.join( - autoconfig.build_directory, 'separate_compilation.sr.txt')) - - declarations.dump_declarations( - prj_decls, - os.path.join( - autoconfig.build_directory, 'separate_compilation.pr.txt')) - - self.assertTrue( - src_decls == prj_decls, - "There is a difference between declarations") - - -def create_suite(): - suite = unittest.TestSuite() - suite.addTest( - unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test)) - suite.addTest( - unittest.TestLoader().loadTestsFromTestCase(testCaseClass=tester2_t)) - return suite - - -def run_suite(): - unittest.TextTestRunner(verbosity=2).run(create_suite()) - - -if __name__ == "__main__": - run_suite() +TEST_FILES1 = [ + 'core_types.hpp', + 'core_ns_join_1.hpp', + 'core_ns_join_2.hpp', + 'core_ns_join_3.hpp', + 'core_membership.hpp', + 'core_class_hierarchy.hpp', + 'core_diamand_hierarchy_base.hpp', + 'core_diamand_hierarchy_derived1.hpp', + 'core_diamand_hierarchy_derived2.hpp', + 'core_diamand_hierarchy_final_derived.hpp', + 'core_overloads_1.hpp', + 'core_overloads_2.hpp' +] + +TEST_FILES2 = [ + 'separate_compilation/data.h', + 'separate_compilation/base.h', + 'separate_compilation/derived.h' +] + + +def test_correctness(): + for src in TEST_FILES1: + __test_correctness_impl(src) + + +def __test_correctness_impl(file_name): + config = autoconfig.cxx_parsers_cfg.config.clone() + prj_reader = parser.project_reader_t(config) + prj_decls = prj_reader.read_files( + [file_name] * 2, + compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE) + src_reader = parser.source_reader_t(config) + src_decls = src_reader.read_file(file_name) + if src_decls != prj_decls: + s = src_decls[0] + p = prj_decls[0] + bdir = autoconfig.build_directory + with open(os.path.join(bdir, file_name + '.sr.txt'), 'w+') as sr: + with open( + os.path.join(bdir, file_name + '.pr.txt'), 'w+') as pr: + + declarations.print_declarations( + s, writer=lambda x: sr.write(l + os.linesep)) + declarations.print_declarations( + p, writer=lambda x: pr.write(l + os.linesep)) + raise("There is a difference between declarations in file %s." % file_name) + + +def test_separate_compilation(): + config = autoconfig.cxx_parsers_cfg.config.clone() + prj_reader = parser.project_reader_t(config) + prj_decls = prj_reader.read_files( + TEST_FILES2, + compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE) + src_reader = parser.source_reader_t(config) + src_decls = src_reader.read_file('separate_compilation/all.h') + + declarations.dump_declarations( + src_decls, + os.path.join( + autoconfig.build_directory, 'separate_compilation.sr.txt')) + + declarations.dump_declarations( + prj_decls, + os.path.join( + autoconfig.build_directory, 'separate_compilation.pr.txt')) + + assert src_decls == prj_decls diff --git a/tests/test_remove_template_defaults.py b/tests/test_remove_template_defaults.py index 0021447f..a93a6995 100644 --- a/tests/test_remove_template_defaults.py +++ b/tests/test_remove_template_defaults.py @@ -3,219 +3,176 @@ # Distributed under the Boost Software License, Version 1.0. # See http://www.boost.org/LICENSE_1_0.txt -import unittest +import pytest -from . import parser_test_case +from . import autoconfig from pygccxml import parser from pygccxml import declarations from pygccxml import utils - -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.header = 'remove_template_defaults.hpp' - - def setUp(self): - if not Test.global_ns: - decls = parser.parse([self.header], 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 - - def test_vector(self): - v_int = self.global_ns.typedef('v_int') - v_traits = declarations.vector_traits - self.assertTrue('vector< int >' == v_traits.remove_defaults(v_int)) - v_string = self.global_ns.typedef('v_string') - self.assertTrue( - 'vector< std::string >' == v_traits.remove_defaults(v_string)) - v_v_int = self.global_ns.typedef('v_v_int') - self.assertTrue( - 'vector< std::vector< int > >' == - v_traits.remove_defaults(v_v_int)) - - def test_list(self): - l_int = self.global_ns.typedef('l_int') - l_traits = declarations.list_traits - self.assertTrue('list< int >' == l_traits.remove_defaults(l_int)) - l_wstring = self.global_ns.typedef('l_wstring') - self.assertTrue( - 'list< std::wstring >' == l_traits.remove_defaults(l_wstring)) - - def test_deque(self): - d_v_int = self.global_ns.typedef('d_v_int') - d_v_traits = declarations.deque_traits - self.assertTrue( - 'deque< std::vector< int > >' == - d_v_traits.remove_defaults(d_v_int)) - d_l_string = self.global_ns.typedef('d_l_string') - self.assertTrue( - 'deque< std::list< std::string > >' == - d_v_traits.remove_defaults(d_l_string)) - - def test_queue(self): - q_int = self.global_ns.typedef('q_int') - q_traits = declarations.queue_traits - self.assertTrue('queue< int >' == q_traits.remove_defaults(q_int)) - q_string = self.global_ns.typedef('q_string') - self.assertTrue( - 'queue< std::string >' == q_traits.remove_defaults(q_string)) - - def test_priority_queue(self): - pq_int = self.global_ns.typedef('pq_int') - pq_traits = declarations.priority_queue_traits - self.assertTrue( - 'priority_queue< int >' == pq_traits.remove_defaults(pq_int)) - pq_string = self.global_ns.typedef('pq_string') - self.assertTrue( - 'priority_queue< std::string >' == - pq_traits.remove_defaults(pq_string)) - - def test_set(self): - s_v_int = self.global_ns.typedef('s_v_int') - self.assertTrue( - 'set< std::vector< int > >' == - declarations.set_traits.remove_defaults(s_v_int)) - s_string = self.global_ns.typedef('s_string') - self.assertTrue( - 'set< std::string >' == - declarations.set_traits.remove_defaults(s_string)) - - def test_multiset(self): - ms_v_int = self.global_ns.typedef('ms_v_int') - ms_v_traits = declarations.multiset_traits - self.assertTrue( - 'multiset< std::vector< int > >' == - ms_v_traits.remove_defaults(ms_v_int)) - ms_string = self.global_ns.typedef('ms_string') - self.assertTrue( - 'multiset< std::string >' == - ms_v_traits.remove_defaults(ms_string)) - - def test_map(self): - m_i2d = self.global_ns.typedef('m_i2d') - self.assertTrue( - 'map< int, double >' == - declarations.map_traits.remove_defaults(m_i2d)) - m_wstr2d = self.global_ns.typedef('m_wstr2d') - self.assertTrue( - 'map< std::wstring, double >' == - declarations.map_traits.remove_defaults(m_wstr2d)) - m_v_i2m_wstr2d = self.global_ns.typedef('m_v_i2m_wstr2d') - m = 'map< const std::vector< int >, std::map< std::wstring, double > >' - self.assertTrue( - m == declarations.map_traits.remove_defaults(m_v_i2m_wstr2d)) - - def test_multimap(self): - mm_i2d = self.global_ns.typedef('mm_i2d') - mm_traits = declarations.multimap_traits - self.assertTrue( - 'multimap< int, double >' == mm_traits.remove_defaults(mm_i2d)) - mm_wstr2d = self.global_ns.typedef('mm_wstr2d') - self.assertTrue( - 'multimap< const std::wstring, double >' == - mm_traits.remove_defaults(mm_wstr2d)) - mm_v_i2mm_wstr2d = self.global_ns.typedef('mm_v_i2mm_wstr2d') - self.assertTrue( - ('multimap< const std::vector< int >, ' + - 'const std::multimap< const std::wstring, double > >') == - mm_traits.remove_defaults(mm_v_i2mm_wstr2d)) - - def test_hash_set(self): - hs_v_int = self.global_ns.typedef('hs_v_int') - if self.xml_generator_from_xml_file.is_castxml: - hs_traits = declarations.unordered_set_traits - name = 'unordered_set' - else: - hs_traits = declarations.hash_set_traits - name = 'hash_set' - self.assertTrue( - (name + '< std::vector< int > >') == - hs_traits.remove_defaults(hs_v_int), - hs_traits.remove_defaults(hs_v_int)) - hs_string = self.global_ns.typedef('hs_string') - self.assertTrue( - (name + '< std::string >') == hs_traits.remove_defaults(hs_string)) - - def test_hash_multiset(self): - mhs_v_int = self.global_ns.typedef('mhs_v_int') - if self.xml_generator_from_xml_file.is_castxml: - mhs_traits = declarations.unordered_multiset_traits - name = 'unordered_multiset' - else: - mhs_traits = declarations.hash_multiset_traits - name = 'hash_multiset' - self.assertTrue( - (name + '< std::vector< int > >') == - mhs_traits.remove_defaults(mhs_v_int)) - mhs_string = self.global_ns.typedef('mhs_string') - self.assertTrue( - (name + '< std::string >') == - mhs_traits.remove_defaults(mhs_string)) - - def test_hash_map(self): - hm_i2d = self.global_ns.typedef('hm_i2d') - if self.xml_generator_from_xml_file.is_castxml: - hm_traits = declarations.unordered_map_traits - name = 'unordered_map' - else: - hm_traits = declarations.hash_map_traits - name = 'hash_map' - self.assertTrue( - (name + '< int, double >') == hm_traits.remove_defaults(hm_i2d)) - hm_wstr2d = self.global_ns.typedef('hm_wstr2d') - self.assertTrue( - (name + '< std::wstring, double >') == - hm_traits.remove_defaults(hm_wstr2d)) - - def test_hash_multimap(self): - hmm_i2d = self.global_ns.typedef('hmm_i2d') - if self.xml_generator_from_xml_file.is_castxml: - hmm_traits = declarations.unordered_multimap_traits - name = 'unordered_multimap' - else: - hmm_traits = declarations.hash_multimap_traits - name = 'hash_multimap' - self.assertTrue( - (name + '< int, double >') == - hmm_traits.remove_defaults(hmm_i2d)) - hmm_wstr2d = self.global_ns.typedef('hmm_wstr2d') - self.assertTrue( - (name + '< const std::wstring, double >') == - hmm_traits.remove_defaults(hmm_wstr2d)) - - hmm_v_i2mm_wstr2d = self.global_ns.typedef('hmm_v_i2mm_wstr2d') - - hmm_traits_value = hmm_traits.remove_defaults(hmm_v_i2mm_wstr2d) - - possible_values = ( - name + '< const std::vector< int >, ' + - 'const __gnu_cxx::' + name + '< const std::wstring, double > >', - name + '< const std::vector< int >, ' + - 'const std::' + utils.get_tr1(hmm_traits_value) + name + - '< const std::wstring, double > >', - name + '< const std::vector< int >, ' + - 'const stdext::' + name + '< const std::wstring, double > >') - - self.assertTrue(hmm_traits_value in possible_values, hmm_traits_value) - - -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() +TEST_FILES = [ + 'remove_template_defaults.hpp' +] + + +@pytest.fixture +def global_ns(): + COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE + config = autoconfig.cxx_parsers_cfg.config.clone() + config.cflags = "-std=c++11" + decls = parser.parse(TEST_FILES, config, COMPILATION_MODE) + global_ns = declarations.get_global_namespace(decls) + global_ns.init_optimizer() + return global_ns + + +def test_vector(global_ns): + v_int = global_ns.typedef('v_int') + v_traits = declarations.vector_traits + assert 'vector< int >' == v_traits.remove_defaults(v_int) + v_string = global_ns.typedef('v_string') + assert 'vector< std::string >' == \ + v_traits.remove_defaults(v_string) + v_v_int = global_ns.typedef('v_v_int') + assert 'vector< std::vector< int > >' == \ + v_traits.remove_defaults(v_v_int) + + +def test_list(global_ns): + l_int = global_ns.typedef('l_int') + l_traits = declarations.list_traits + assert 'list< int >' == l_traits.remove_defaults(l_int) + l_wstring = global_ns.typedef('l_wstring') + assert 'list< std::wstring >' == l_traits.remove_defaults(l_wstring) + + +def test_deque(global_ns): + d_v_int = global_ns.typedef('d_v_int') + d_v_traits = declarations.deque_traits + assert 'deque< std::vector< int > >' == \ + d_v_traits.remove_defaults(d_v_int) + d_l_string = global_ns.typedef('d_l_string') + assert 'deque< std::list< std::string > >' == \ + d_v_traits.remove_defaults(d_l_string) + + +def test_queue(global_ns): + q_int = global_ns.typedef('q_int') + q_traits = declarations.queue_traits + assert 'queue< int >' == q_traits.remove_defaults(q_int) + q_string = global_ns.typedef('q_string') + assert 'queue< std::string >' == q_traits.remove_defaults(q_string) + + +def test_priority_queue(global_ns): + pq_int = global_ns.typedef('pq_int') + pq_traits = declarations.priority_queue_traits + assert 'priority_queue< int >' == pq_traits.remove_defaults(pq_int) + pq_string = global_ns.typedef('pq_string') + assert 'priority_queue< std::string >' == \ + pq_traits.remove_defaults(pq_string) + + +def test_set(global_ns): + s_v_int = global_ns.typedef('s_v_int') + assert 'set< std::vector< int > >' == \ + declarations.set_traits.remove_defaults(s_v_int) + s_string = global_ns.typedef('s_string') + assert 'set< std::string >' == \ + declarations.set_traits.remove_defaults(s_string) + + +def test_multiset(global_ns): + ms_v_int = global_ns.typedef('ms_v_int') + ms_v_traits = declarations.multiset_traits + assert 'multiset< std::vector< int > >' == \ + ms_v_traits.remove_defaults(ms_v_int) + ms_string = global_ns.typedef('ms_string') + assert 'multiset< std::string >' == \ + ms_v_traits.remove_defaults(ms_string) + + +def test_map(global_ns): + m_i2d = global_ns.typedef('m_i2d') + assert 'map< int, double >' == \ + declarations.map_traits.remove_defaults(m_i2d) + m_wstr2d = global_ns.typedef('m_wstr2d') + assert 'map< std::wstring, double >' == \ + declarations.map_traits.remove_defaults(m_wstr2d) + m_v_i2m_wstr2d = global_ns.typedef('m_v_i2m_wstr2d') + m = 'map< const std::vector< int >, std::map< std::wstring, double > >' + assert m == declarations.map_traits.remove_defaults(m_v_i2m_wstr2d) + + +def test_multimap(global_ns): + mm_i2d = global_ns.typedef('mm_i2d') + mm_traits = declarations.multimap_traits + assert 'multimap< int, double >' == mm_traits.remove_defaults(mm_i2d) + mm_wstr2d = global_ns.typedef('mm_wstr2d') + assert 'multimap< const std::wstring, double >' == \ + mm_traits.remove_defaults(mm_wstr2d) + mm_v_i2mm_wstr2d = global_ns.typedef('mm_v_i2mm_wstr2d') + assert ('multimap< const std::vector< int >, ' + + 'const std::multimap< const std::wstring, double > >') == \ + mm_traits.remove_defaults(mm_v_i2mm_wstr2d) + + +def test_hash_set(global_ns): + hs_v_int = global_ns.typedef('hs_v_int') + hs_traits = declarations.unordered_set_traits + name = 'unordered_set' + assert (name + '< std::vector< int > >') == \ + hs_traits.remove_defaults(hs_v_int), \ + hs_traits.remove_defaults(hs_v_int) + hs_string = global_ns.typedef('hs_string') + assert (name + '< std::string >') == \ + hs_traits.remove_defaults(hs_string) + + +def test_hash_multiset(global_ns): + mhs_v_int = global_ns.typedef('mhs_v_int') + mhs_traits = declarations.unordered_multiset_traits + name = 'unordered_multiset' + assert (name + '< std::vector< int > >') == \ + mhs_traits.remove_defaults(mhs_v_int) + mhs_string = global_ns.typedef('mhs_string') + assert (name + '< std::string >') == \ + mhs_traits.remove_defaults(mhs_string) + + +def test_hash_map(global_ns): + hm_i2d = global_ns.typedef('hm_i2d') + hm_traits = declarations.hash_map_traits + name = 'hash_map' + assert (name + '< int, double >') == \ + hm_traits.remove_defaults(hm_i2d) + hm_wstr2d = global_ns.typedef('hm_wstr2d') + assert (name + '< std::wstring, double >') == \ + hm_traits.remove_defaults(hm_wstr2d) + + +def test_hash_multimap(global_ns): + hmm_i2d = global_ns.typedef('hmm_i2d') + hmm_traits = declarations.unordered_multimap_traits + name = 'unordered_multimap' + assert (name + '< int, double >') == \ + hmm_traits.remove_defaults(hmm_i2d) + hmm_wstr2d = global_ns.typedef('hmm_wstr2d') + assert (name + '< const std::wstring, double >') == \ + hmm_traits.remove_defaults(hmm_wstr2d) + + hmm_v_i2mm_wstr2d = global_ns.typedef('hmm_v_i2mm_wstr2d') + + hmm_traits_value = hmm_traits.remove_defaults(hmm_v_i2mm_wstr2d) + + possible_values = ( + name + '< const std::vector< int >, ' + + 'const __gnu_cxx::' + name + '< const std::wstring, double > >', + name + '< const std::vector< int >, ' + + 'const std::' + utils.get_tr1(hmm_traits_value) + name + + '< const std::wstring, double > >', + name + '< const std::vector< int >, ' + + 'const stdext::' + name + '< const std::wstring, double > >') + + assert hmm_traits_value in possible_values, hmm_traits_value diff --git a/tests/test_smart_pointer.py b/tests/test_smart_pointer.py index 9335debe..6b74d94f 100644 --- a/tests/test_smart_pointer.py +++ b/tests/test_smart_pointer.py @@ -3,104 +3,93 @@ # Distributed under the Boost Software License, Version 1.0. # See http://www.boost.org/LICENSE_1_0.txt -import unittest +import pytest -from . import parser_test_case +from . import autoconfig from pygccxml import parser from pygccxml import declarations -class Test(parser_test_case.parser_test_case_t): +TEST_FILES = [ + 'test_smart_pointer.hpp' +] - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.header = "test_smart_pointer.hpp" - self.config.cflags = "-std=c++11" - self.global_ns = None - def setUp(self): - decls = parser.parse([self.header], self.config) - self.global_ns = declarations.get_global_namespace(decls) +@pytest.fixture +def global_ns(): + COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE + config = autoconfig.cxx_parsers_cfg.config.clone() + config.cflags = "-std=c++11" + decls = parser.parse(TEST_FILES, config, COMPILATION_MODE) + global_ns = declarations.get_global_namespace(decls) + global_ns.init_optimizer() + return global_ns - def test_is_smart_pointer(self): - """ - Test smart_pointer_traits.is_smart_pointer method. - """ +def test_is_smart_pointer(global_ns): + """ + Test smart_pointer_traits.is_smart_pointer method. - criteria = declarations.declaration_matcher(name="yes1") - decls = declarations.matcher.find(criteria, self.global_ns) - self.assertTrue( - declarations.smart_pointer_traits.is_smart_pointer( - decls[0].decl_type)) + """ - criteria = declarations.declaration_matcher(name="no1") - decls = declarations.matcher.find(criteria, self.global_ns) - self.assertFalse( - declarations.smart_pointer_traits.is_smart_pointer( - decls[0].decl_type)) + criteria = declarations.declaration_matcher(name="yes1") + decls = declarations.matcher.find(criteria, global_ns) + assert declarations.smart_pointer_traits.is_smart_pointer( + decls[0].decl_type) is True - criteria = declarations.declaration_matcher(name="no2") - decls = declarations.matcher.find(criteria, self.global_ns) - self.assertFalse( - declarations.smart_pointer_traits.is_smart_pointer( - decls[0].decl_type)) + criteria = declarations.declaration_matcher(name="no1") + decls = declarations.matcher.find(criteria, global_ns) + assert declarations.smart_pointer_traits.is_smart_pointer( + decls[0].decl_type) is False - def test_is_auto_pointer(self): - """ - Test auto_ptr_traits.is_smart_pointer method. + criteria = declarations.declaration_matcher(name="no2") + decls = declarations.matcher.find(criteria, global_ns) + assert declarations.smart_pointer_traits.is_smart_pointer( + decls[0].decl_type) is False - """ - criteria = declarations.declaration_matcher(name="yes2") - decls = declarations.matcher.find(criteria, self.global_ns) - self.assertTrue( - declarations.auto_ptr_traits.is_smart_pointer(decls[0].decl_type)) +def test_is_auto_pointer(global_ns): + """ + Test auto_ptr_traits.is_smart_pointer method. - criteria = declarations.declaration_matcher(name="no1") - decls = declarations.matcher.find(criteria, self.global_ns) - self.assertFalse( - declarations.auto_ptr_traits.is_smart_pointer(decls[0].decl_type)) + """ - criteria = declarations.declaration_matcher(name="no2") - decls = declarations.matcher.find(criteria, self.global_ns) - self.assertFalse( - declarations.auto_ptr_traits.is_smart_pointer(decls[0].decl_type)) + criteria = declarations.declaration_matcher(name="yes2") + decls = declarations.matcher.find(criteria, global_ns) + assert declarations.auto_ptr_traits.is_smart_pointer( + decls[0].decl_type) is True - def test_smart_pointer_value_type(self): - """ - Test smart_pointer_traits.value_type method. + criteria = declarations.declaration_matcher(name="no1") + decls = declarations.matcher.find(criteria, global_ns) + assert declarations.auto_ptr_traits.is_smart_pointer( + decls[0].decl_type) is False - """ + criteria = declarations.declaration_matcher(name="no2") + decls = declarations.matcher.find(criteria, global_ns) + assert declarations.auto_ptr_traits.is_smart_pointer( + decls[0].decl_type) is False - criteria = declarations.declaration_matcher(name="yes1") - decls = declarations.matcher.find(criteria, self.global_ns) - vt = declarations.smart_pointer_traits.value_type(decls[0].decl_type) - self.assertIsInstance(vt, declarations.int_t) - def test_auto_pointer_value_type(self): - """ - Test auto_pointer_traits.value_type method. +def test_smart_pointer_value_type(global_ns): + """ + Test smart_pointer_traits.value_type method. - """ + """ - criteria = declarations.declaration_matcher(name="yes2") - decls = declarations.matcher.find(criteria, self.global_ns) - vt = declarations.auto_ptr_traits.value_type(decls[0].decl_type) - self.assertIsInstance(vt, declarations.double_t) + criteria = declarations.declaration_matcher(name="yes1") + decls = declarations.matcher.find(criteria, global_ns) + vt = declarations.smart_pointer_traits.value_type(decls[0].decl_type) + assert isinstance(vt, declarations.int_t) is True -def create_suite(): - suite = unittest.TestSuite() - suite.addTest( - unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test)) - return suite +def test_auto_pointer_value_type(global_ns): + """ + Test auto_pointer_traits.value_type method. + """ -def run_suite(): - unittest.TextTestRunner(verbosity=2).run(create_suite()) - - -if __name__ == "__main__": - run_suite() + criteria = declarations.declaration_matcher(name="yes2") + decls = declarations.matcher.find(criteria, global_ns) + vt = declarations.auto_ptr_traits.value_type(decls[0].decl_type) + assert isinstance(vt, declarations.double_t) is True diff --git a/tests/test_source_reader.py b/tests/test_source_reader.py index fbb7c236..355e225e 100644 --- a/tests/test_source_reader.py +++ b/tests/test_source_reader.py @@ -3,45 +3,29 @@ # Distributed under the Boost Software License, Version 1.0. # See http://www.boost.org/LICENSE_1_0.txt -import unittest +import pytest -from . import parser_test_case +from . import autoconfig from pygccxml import parser from pygccxml import declarations +TEST_FILES = [ + 'declarations_calldef.hpp' +] -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.header = 'declarations_calldef.hpp' - self.global_ns = None +@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 setUp(self): - if not Test.global_ns: - decls = parser.parse([self.header], self.config) - Test.global_ns = declarations.get_global_namespace(decls) - Test.global_ns.init_optimizer() - self.global_ns = Test.global_ns - def test_compound_argument_type(self): - do_smth = self.global_ns.calldefs('do_smth') - self.assertTrue(do_smth, "unable to find do_smth") - do_smth.function_type() - - -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() +def test_compound_argument_type(global_ns): + do_smth = global_ns.calldefs('do_smth') + assert do_smth is not None + do_smth.function_type() diff --git a/tests/test_start_with_declarations.py b/tests/test_start_with_declarations.py index 0aad14c6..5a778893 100644 --- a/tests/test_start_with_declarations.py +++ b/tests/test_start_with_declarations.py @@ -3,66 +3,52 @@ # Distributed under the Boost Software License, Version 1.0. # See http://www.boost.org/LICENSE_1_0.txt -import unittest +import pytest from . import autoconfig -from . import parser_test_case + from pygccxml import parser from pygccxml import declarations - -class Test(parser_test_case.parser_test_case_t): - - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.header = 'core_ns_join_1.hpp' - self.config = autoconfig.cxx_parsers_cfg.config.clone() - self.config.start_with_declarations.extend(['E11', 'ns::ns12::E13']) - - def __check_result(self, decls): - E11 = declarations.find_declaration(decls, fullname='::E11') - self.assertTrue(E11, "unable to find 'E11' enum") - ns12 = declarations.find_declaration(decls, fullname='::ns::ns12') - self.assertTrue(ns12, "unable to find 'ns12' namespace") - E13 = declarations.find_declaration(ns12.declarations, name='E13') - self.assertTrue(E13, "unable to find 'E13' enum") - E14 = declarations.find_declaration(decls, name='E14') - self.assertTrue( - not E14, - "enum 'E14' should not be found in declarations") - - def test_simple(self): - decls = parser.parse([self.header], self.config) - self.__check_result(decls) - - def test_project_reader_file_by_file(self): - reader = parser.project_reader_t(self.config) - decls = reader.read_files( - [parser.file_configuration_t( - self.header, self.config.start_with_declarations)], - parser.COMPILATION_MODE.FILE_BY_FILE) - self.__check_result(decls) - - def test_project_reader_all_at_once(self): - reader = parser.project_reader_t(self.config) - decls = reader.read_files( - [parser.file_configuration_t( - self.header, self.config.start_with_declarations)], - parser.COMPILATION_MODE.ALL_AT_ONCE) - self.__check_result(decls) - - -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() +TEST_FILES = "core_ns_join_1.hpp" + + +def __check_result(decls): + E11 = declarations.find_declaration(decls, fullname='::E11') + assert E11 is not None + ns12 = declarations.find_declaration(decls, fullname='::ns::ns12') + assert ns12 is not None + E13 = declarations.find_declaration(ns12.declarations, name='E13') + assert E13 is not None + E14 = declarations.find_declaration(decls, name='E14') + assert E14 is None + + +def test_simple_start_with_declarations(): + config = autoconfig.cxx_parsers_cfg.config.clone() + config.start_with_declarations.extend(['E11', 'ns::ns12::E13']) + decls = parser.parse([TEST_FILES], config) + __check_result(decls) + + +def test_project_reader_file_by_file_start_with_declarations(): + config = autoconfig.cxx_parsers_cfg.config.clone() + config.start_with_declarations.extend(['E11', 'ns::ns12::E13']) + reader = parser.project_reader_t(config) + decls = reader.read_files( + [parser.file_configuration_t( + TEST_FILES, config.start_with_declarations)], + parser.COMPILATION_MODE.FILE_BY_FILE) + __check_result(decls) + + +def test_project_reader_all_at_once_start_with_declarations(): + config = autoconfig.cxx_parsers_cfg.config.clone() + config.start_with_declarations.extend(['E11', 'ns::ns12::E13']) + reader = parser.project_reader_t(config) + decls = reader.read_files( + [parser.file_configuration_t( + TEST_FILES, config.start_with_declarations)], + parser.COMPILATION_MODE.ALL_AT_ONCE) + __check_result(decls) diff --git a/tests/test_string_traits.py b/tests/test_string_traits.py index 5755322e..9519845e 100644 --- a/tests/test_string_traits.py +++ b/tests/test_string_traits.py @@ -3,66 +3,51 @@ # Distributed under the Boost Software License, Version 1.0. # See http://www.boost.org/LICENSE_1_0.txt -import unittest +import pytest -from . import parser_test_case +from . import autoconfig from pygccxml import parser from pygccxml import declarations -class Test(parser_test_case.parser_test_case_t): - COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE - global_ns = None - - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.header = 'string_traits.hpp' - self.global_ns = None - - def setUp(self): - if not Test.global_ns: - decls = parser.parse([self.header], self.config) - Test.global_ns = declarations.get_global_namespace(decls) - self.global_ns = Test.global_ns - - def validate_yes(self, ns, controller): - for typedef in ns.typedefs(): - self.assertTrue(controller(typedef.decl_type)) - - def validate_no(self, ns, controller): - for typedef in ns.typedefs(): - self.assertTrue(not controller(typedef.decl_type)) - - def test_string(self): - string_traits = self.global_ns.namespace('string_traits') - self.validate_yes( - string_traits.namespace('yes'), - declarations.is_std_string) - self.validate_no( - string_traits.namespace('no'), - declarations.is_std_string) +TEST_FILES = [ + "string_traits.hpp", +] - def test_wstring(self): - wstring_traits = self.global_ns.namespace('wstring_traits') - self.validate_yes( - wstring_traits.namespace('yes'), - declarations.is_std_wstring) - self.validate_no( - wstring_traits.namespace('no'), - declarations.is_std_wstring) - -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() +@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 validate_yes(ns, controller): + for typedef in ns.typedefs(): + assert controller(typedef.decl_type) is True + +def validate_no(ns, controller): + for typedef in ns.typedefs(): + assert controller(typedef.decl_type) is False + +def test_string(global_ns): + string_traits = global_ns.namespace('string_traits') + validate_yes( + string_traits.namespace('yes'), + declarations.is_std_string) + validate_no( + string_traits.namespace('no'), + declarations.is_std_string) + +def test_wstring(global_ns): + wstring_traits = global_ns.namespace('wstring_traits') + validate_yes( + wstring_traits.namespace('yes'), + declarations.is_std_wstring) + validate_no( + wstring_traits.namespace('no'), + declarations.is_std_wstring) diff --git a/tests/test_templates.py b/tests/test_templates.py index d7f51903..bf829ae6 100644 --- a/tests/test_templates.py +++ b/tests/test_templates.py @@ -3,97 +3,71 @@ # Distributed under the Boost Software License, Version 1.0. # See http://www.boost.org/LICENSE_1_0.txt -from . import parser_test_case - from pygccxml import declarations -class TestTemplates(parser_test_case.parser_test_case_t): - - def __test_split_impl(self, decl_string, name, args): - self.assertTrue( - (name, args) == declarations.templates.split(decl_string)) - - def __test_split_recursive_impl(self, decl_string, control_seq): - self.assertTrue( - control_seq == - list(declarations.templates.split_recursive(decl_string))) - - def __test_is_template_impl(self, decl_string): - self.assertTrue(declarations.templates.is_instantiation(decl_string)) - - def test_split_on_vector(self): - self.__test_is_template_impl("vector >") - - self.__test_split_impl( - "vector >", - "vector", - ["int", "std::allocator"]) - - self.__test_split_recursive_impl( - "vector >", - [("vector", ["int", "std::allocator"]), - ("std::allocator", ["int"])]) - - def test_split_on_string(self): - self.__test_is_template_impl( - "basic_string,std::allocator >") - - self.__test_split_impl( - "basic_string,std::allocator >", - "basic_string", - ["char", - "std::char_traits", - "std::allocator"]) - - def test_split_on_map(self): - self.__test_is_template_impl( - "map >," + - "std::less,std::allocator > > > >") - - self.__test_split_impl( - "map >," + - "std::less,std::allocator > > > >", - "map", - ["long int", - "std::vector >", - "std::less", - "std::allocator > > >"]) - - def test_join_on_vector(self): - self.assertTrue( - "vector< int, std::allocator >" == - declarations.templates.join( - "vector", ("int", "std::allocator"))) - - def test_bug_is_tmpl_inst(self): - self.assertTrue( - declarations.templates.is_instantiation( - "::FX::QMemArray::setRawData") is False) - - # disable broken test - # def test_split_bug_fptr(self): - # x = 'map, ' + - # 'std::allocator > >' - # name, args = declarations.templates.split( x ) - # self.assertTrue( len(x) == 4, "This test is expected to fail." ) - - -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() +def __test_split_impl(decl_string, name, args): + assert (name, args) == declarations.templates.split(decl_string) + + +def __test_split_recursive_impl(decl_string, control_seq): + assert control_seq == list(declarations.templates.split_recursive(decl_string)) + + +def __test_is_template_impl(decl_string): + assert declarations.templates.is_instantiation(decl_string) + + +def test_split_on_vector(): + __test_is_template_impl("vector >") + + __test_split_impl( + "vector >", + "vector", + ["int", "std::allocator"]) + + __test_split_recursive_impl( + "vector >", + [("vector", ["int", "std::allocator"]), + ("std::allocator", ["int"])]) + + +def test_split_on_string(): + __test_is_template_impl( + "basic_string,std::allocator >") + + __test_split_impl( + "basic_string,std::allocator >", + "basic_string", + ["char", + "std::char_traits", + "std::allocator"]) + + +def test_split_on_map(): + __test_is_template_impl( + "map >," + + "std::less,std::allocator > > > >") + + __test_split_impl( + "map >," + + "std::less,std::allocator > > > >", + "map", + ["long int", + "std::vector >", + "std::less", + "std::allocator > > >"]) + + +def test_join_on_vector(): + assert "vector< int, std::allocator >" == \ + declarations.templates.join( + "vector", ("int", "std::allocator")) + + +def test_bug_is_tmpl_inst(): + assert declarations.templates.is_instantiation( + "::FX::QMemArray::setRawData") is False diff --git a/tests/test_text_reader.py b/tests/test_text_reader.py index 8708c496..b2b0584d 100644 --- a/tests/test_text_reader.py +++ b/tests/test_text_reader.py @@ -3,45 +3,25 @@ # 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 . import autoconfig from pygccxml import parser from pygccxml import declarations -class Test(parser_test_case.parser_test_case_t): - - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - - def test(self): - fconfig = parser.file_configuration_t( - data='int i;', - start_with_declarations=None, - content_type=parser.file_configuration_t.CONTENT_TYPE.TEXT) - - prj_reader = parser.project_reader_t(self.config) - decls = prj_reader.read_files( - [fconfig], - compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE) - - var_i = declarations.find_declaration( - decls, decl_type=declarations.variable_t, name='i') - self.assertTrue(var_i, "Variable i has not been found.") - - -def create_suite(): - suite = unittest.TestSuite() - suite.addTest( - unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test)) - return suite - +def test_text_reader(): + config = autoconfig.cxx_parsers_cfg.config.clone() -def run_suite(): - unittest.TextTestRunner(verbosity=2).run(create_suite()) + fconfig = parser.file_configuration_t( + data='int i;', + start_with_declarations=None, + content_type=parser.file_configuration_t.CONTENT_TYPE.TEXT) + prj_reader = parser.project_reader_t(config) + decls = prj_reader.read_files( + [fconfig], + compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE) -if __name__ == "__main__": - run_suite() + var_i = declarations.find_declaration( + decls, decl_type=declarations.variable_t, name='i') + assert var_i is not None diff --git a/tests/test_type_as_exception_bug.py b/tests/test_type_as_exception_bug.py index 57dc5539..5a6c2cc1 100644 --- a/tests/test_type_as_exception_bug.py +++ b/tests/test_type_as_exception_bug.py @@ -3,48 +3,34 @@ # Distributed under the Boost Software License, Version 1.0. # See http://www.boost.org/LICENSE_1_0.txt -import unittest +import pytest -from . import parser_test_case +from . import autoconfig from pygccxml import parser from pygccxml import declarations -class Test(parser_test_case.parser_test_case_t): - global_ns = None +TEST_FILES = [ + "type_as_exception_bug.h", +] - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.header = 'type_as_exception_bug.h' - def setUp(self): - if not Test.global_ns: - decls = parser.parse([self.header], self.config) - Test.global_ns = declarations.get_global_namespace(decls) - Test.global_ns.init_optimizer() +@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) + return global_ns - def test(self): - buggy = self.global_ns.member_function('buggy') - expression_error = self.global_ns.class_('ExpressionError') - self.assertTrue(len(buggy.exceptions) == 1) - err = buggy.exceptions[0] - self.assertTrue(declarations.is_reference(err)) - err = declarations.remove_declarated( - declarations.remove_reference(err)) - self.assertTrue(err is expression_error) - -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() +def test_type_as_exception(global_ns): + buggy = global_ns.member_function('buggy') + expression_error = global_ns.class_('ExpressionError') + assert len(buggy.exceptions) == 1 + err = buggy.exceptions[0] + assert declarations.is_reference(err) + err = declarations.remove_declarated( + declarations.remove_reference(err)) + assert err is expression_error