diff --git a/tests/test_attributes.py b/tests/test_attributes.py index 43de7fdd..c45d0cf9 100644 --- a/tests/test_attributes.py +++ b/tests/test_attributes.py @@ -49,6 +49,7 @@ def test_attributes_thiscall(): config = autoconfig.cxx_parsers_cfg.config.clone() config.flags = ["f2"] + config.castxml_epic_version = 1 decls = parser.parse(TEST_FILES, config, COMPILATION_MODE) global_ns = declarations.get_global_namespace(decls) diff --git a/tests/test_castxml_wrong_epic.py b/tests/test_castxml_wrong_epic.py index 607b02de..902c00d2 100644 --- a/tests/test_castxml_wrong_epic.py +++ b/tests/test_castxml_wrong_epic.py @@ -3,43 +3,20 @@ # 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 -class Test(parser_test_case.parser_test_case_t): +def test_castxml_epic_version_check(): + """ + Test using a forbidden value for the castxml epic version. - def test_castxml_epic_version_check(self): - """ - Test using a forbidden value for the castxml epic version. + """ - """ - - if self.config.castxml_epic_version != 1: - # Run this test only with castxml epic version == 1 - return - - self.config.castxml_epic_version = 2 - self.assertRaises( - RuntimeError, lambda: parser.parse_string("", self.config)) - - # Reset castxml epic version - self.config.castxml_epic_version = 1 - - -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() + config = autoconfig.cxx_parsers_cfg.config.clone() + config.castxml_epic_version = 2 + with pytest.raises(RuntimeError): + parser.parse_string("", config) diff --git a/tests/test_ccflags.py b/tests/test_ccflags.py index daf46f05..fe25b300 100644 --- a/tests/test_ccflags.py +++ b/tests/test_ccflags.py @@ -3,65 +3,47 @@ # 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 = [ + "test_ccflags.hpp", +] - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.header = "test_ccflags.hpp" - self.global_ns = None - self.config.castxml_epic_version = 1 - self.config.append_cflags("-fopenmp") +COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE - def _parse_src(self): - decls = parser.parse([self.header], self.config) - Test.global_ns = declarations.get_global_namespace(decls) - 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 +@pytest.fixture +def config(): + config = autoconfig.cxx_parsers_cfg.config.clone() + config.castxml_epic_version = 1 + config.append_cflags("-fopenmp") + return config - def _add_ccflags(self): - if "clang++" in self.config.compiler_path: - self.config.append_ccflags("-Xpreprocessor") - self.config.append_ccflags("-fopenmp") +def test_ccflags(config): + # First check that macro is not defined. + decls = parser.parse(TEST_FILES, config, COMPILATION_MODE) + global_ns = declarations.get_global_namespace(decls) - def test(self): - # First check that macro is not defined. - self._parse_src() - namespace_names = [ - n.name for n in self.global_ns.namespaces(allow_empty=True) - ] - self.assertNotIn("ccflags_test_namespace", namespace_names) + namespace_names = [ + n.name for n in global_ns.namespaces(allow_empty=True) + ] + assert "ccflags_test_namespace" not in namespace_names - # Next check that macro is defined when passed directly as ccflag - self._add_ccflags() - self._parse_src() - namespace_names = [n.name for n in self.global_ns.namespaces()] - self.assertIn("ccflags_test_namespace", namespace_names) + # Next check that macro is defined when passed directly as ccflag + if "clang++" in config.compiler_path: + config.append_ccflags("-Xpreprocessor") + config.append_ccflags("-fopenmp") -def create_suite(): - suite = unittest.TestSuite() - suite.addTest( - unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test)) - return suite + decls = parser.parse(TEST_FILES, config, COMPILATION_MODE) + global_ns = declarations.get_global_namespace(decls) - -def run_suite(): - unittest.TextTestRunner(verbosity=2).run(create_suite()) - - -if __name__ == "__main__": - run_suite() + namespace_names = [n.name for n in global_ns.namespaces()] + assert "ccflags_test_namespace" in namespace_names diff --git a/tests/test_comments.py b/tests/test_comments.py index 4d4e9f05..214bd17e 100644 --- a/tests/test_comments.py +++ b/tests/test_comments.py @@ -3,98 +3,97 @@ # 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 - - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.header = "test_comments.hpp" - self.global_ns = None - self.config.castxml_epic_version = 1 - - def _check_comment_content(self, list, comment_decl): - if comment_decl.text: - self.assertEqual(list, comment_decl.text) - else: - print("No text in comment to check") - - def setUp(self): - - if not self.global_ns: - decls = parser.parse([self.header], self.config) - Test.global_ns = declarations.get_global_namespace(decls) - 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 test(self): - """ - Check the comment parsing - """ - if self.config.castxml_epic_version != 1: - # Run this test only with castxml epic version == 1 - return - tnamespace = self.global_ns.namespace("comment") - - self.assertIn("comment", dir(tnamespace)) - self._check_comment_content(["//! Namespace Comment", - "//! Across multiple lines"], - tnamespace.comment) - - tenumeration = tnamespace.enumeration("com_enum") - self.assertIn("comment", dir(tenumeration)) - self._check_comment_content(['/// Outside Class enum comment'], - tenumeration.comment) - - tclass = tnamespace.class_("test") - self.assertIn("comment", dir(tclass)) - self._check_comment_content(["/** class comment */"], tclass.comment) - - tcls_enumeration = tclass.enumeration("test_enum") - self.assertIn("comment", dir(tcls_enumeration)) - self._check_comment_content(['/// inside class enum comment'], - tcls_enumeration.comment) - - tmethod = tclass.member_functions()[0] - - self.assertIn("comment", dir(tmethod)) - self._check_comment_content(["/// cxx comment", - "/// with multiple lines"], - tmethod.comment) - - tconstructor = tclass.constructors()[0] - - self.assertIn("comment", dir(tconstructor)) - self._check_comment_content(["/** doc comment */"], - tconstructor.comment) - - for indx, cmt in enumerate(['//! mutable field comment', - "/// bit field comment"]): - tvariable = tclass.variables()[indx] - self.assertIn("comment", dir(tvariable)) - self._check_comment_content([cmt], tvariable.comment) - - -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_comments.hpp", +] + + +@pytest.fixture +def global_ns(): + COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE + INIT_OPTIMIZER = True + config = autoconfig.cxx_parsers_cfg.config.clone() + config.castxml_epic_version = 1 + decls = parser.parse(TEST_FILES, config, COMPILATION_MODE) + global_ns = declarations.get_global_namespace(decls) + if INIT_OPTIMIZER: + global_ns.init_optimizer() + return global_ns + + +def _check_comment_content(list, comment_decl): + if comment_decl.text: + assert list == comment_decl.text + else: + print("No text in comment to check") + + +def test_comments(global_ns): + """ + Check the comment parsing + """ + tnamespace = global_ns.namespace("comment") + + assert "comment" in dir(tnamespace) + _check_comment_content( + [ + "//! Namespace Comment", + "//! Across multiple lines" + ], + tnamespace.comment + ) + + tenumeration = tnamespace.enumeration("com_enum") + assert "comment" in dir(tenumeration) + _check_comment_content( + ['/// Outside Class enum comment'], + tenumeration.comment + ) + + tclass = tnamespace.class_("test") + assert "comment" in dir(tclass) + _check_comment_content( + ["/** class comment */"], + tclass.comment + ) + + tcls_enumeration = tclass.enumeration("test_enum") + assert "comment" in dir(tcls_enumeration) + _check_comment_content( + ['/// inside class enum comment'], + tcls_enumeration.comment + ) + + tmethod = tclass.member_functions()[0] + + assert "comment" in dir(tmethod) + _check_comment_content( + ["/// cxx comment", "/// with multiple lines"], + tmethod.comment + ) + + tconstructor = tclass.constructors()[0] + + assert "comment" in dir(tconstructor) + _check_comment_content( + ["/** doc comment */"], + tconstructor.comment + ) + + for indx, cmt in enumerate( + [ + '//! mutable field comment', + "/// bit field comment" + ] + ): + tvariable = tclass.variables()[indx] + assert "comment" in dir(tvariable) + _check_comment_content([cmt], tvariable.comment) diff --git a/tests/test_complex_types.py b/tests/test_complex_types.py index 5fe8a255..25f511e6 100644 --- a/tests/test_complex_types.py +++ b/tests/test_complex_types.py @@ -3,45 +3,36 @@ # Distributed under the Boost Software License, Version 1.0. # See http://www.boost.org/LICENSE_1_0.txt -import os -import unittest +import pytest -from . import parser_test_case +from . import autoconfig +from pygccxml import declarations from pygccxml import parser -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 = 'complex_types.hpp' - self.declarations = None - - def setUp(self): - if not self.declarations: - self.declarations = parser.parse([self.header], self.config) - - def test(self): - """ - This test tests presence of complex long double, float within - FUNDAMENTAL_TYPES map - """ - pass - +TEST_FILES = [ + "complex_types.hpp", +] -def create_suite(): - suite = unittest.TestSuite() - if os.name != 'nt': - 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 + INIT_OPTIMIZER = True + config = autoconfig.cxx_parsers_cfg.config.clone() + config.castxml_epic_version = 1 + decls = parser.parse(TEST_FILES, config, COMPILATION_MODE) + global_ns = declarations.get_global_namespace(decls) + if INIT_OPTIMIZER: + global_ns.init_optimizer() + return global_ns + + +def test_complex_types(): + """ + This test tests presence of complex long double, float within + FUNDAMENTAL_TYPES map + """ + pass + # TODO: write test diff --git a/tests/test_const_volatile_arg.py b/tests/test_const_volatile_arg.py index b7cbf9ab..929024f0 100644 --- a/tests/test_const_volatile_arg.py +++ b/tests/test_const_volatile_arg.py @@ -3,48 +3,36 @@ # 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 = [ + "const_volatile_arg.hpp", +] - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.header = 'const_volatile_arg.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) - Test.global_ns.init_optimizer() - self.global_ns = Test.global_ns +@pytest.fixture +def global_ns(): + COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE + INIT_OPTIMIZER = True + config = autoconfig.cxx_parsers_cfg.config.clone() + config.castxml_epic_version = 1 + decls = parser.parse(TEST_FILES, config, COMPILATION_MODE) + global_ns = declarations.get_global_namespace(decls) + if INIT_OPTIMIZER: + global_ns.init_optimizer() + return global_ns - def test_const_volatile_arg(self): - f = self.global_ns.free_function('pygccxml_bug') - t = f.arguments[0].decl_type - self.assertTrue(isinstance(t, declarations.pointer_t)) - self.assertTrue(isinstance(t.base, declarations.volatile_t)) - self.assertTrue(isinstance(t.base.base, declarations.const_t)) - self.assertTrue(declarations.is_integral(t.base.base.base)) - -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_const_volatile_arg(global_ns): + f = global_ns.free_function('pygccxml_bug') + t = f.arguments[0].decl_type + assert isinstance(t, declarations.pointer_t) + assert isinstance(t.base, declarations.volatile_t) + assert isinstance(t.base.base, declarations.const_t) + assert declarations.is_integral(t.base.base.base) diff --git a/tests/test_copy_constructor.py b/tests/test_copy_constructor.py index 9e4c525e..975c470b 100644 --- a/tests/test_copy_constructor.py +++ b/tests/test_copy_constructor.py @@ -3,74 +3,59 @@ # 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 - - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.header = "test_copy_constructor.hpp" - self.global_ns = None - - def setUp(self): - if not self.global_ns: - decls = parser.parse([self.header], self.config) - Test.global_ns = declarations.get_global_namespace(decls) - 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 test_copy_constructor(self): - """ - Check the is_copy_constructor method. - - This fails when using CastXML, see issue #27. - - """ - - tclass = self.global_ns.class_("test") - ctors = [] - for decl in tclass.declarations: - if isinstance(decl, declarations.constructor_t): - ctors.append(decl) - - # test::test(test const & t0) [copy constructor] - self.assertTrue(declarations.is_copy_constructor(ctors[0])) - # test::test(float const & t0) [constructor] - self.assertFalse(declarations.is_copy_constructor(ctors[1])) - # test::test(myvar t0) [constructor] - self.assertFalse(declarations.is_copy_constructor(ctors[2])) - - t2class = self.global_ns.class_("test2") - ctors = [] - for decl in t2class.declarations: - if isinstance(decl, declarations.constructor_t): - ctors.append(decl) - - # test2::test2() [constructor] - self.assertFalse(declarations.is_copy_constructor(ctors[0])) - # test2::test2(test2 const & arg0) [copy constructor] - self.assertTrue(declarations.is_copy_constructor(ctors[1])) - - -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_copy_constructor.hpp", +] + + +@pytest.fixture +def global_ns(): + COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE + INIT_OPTIMIZER = True + config = autoconfig.cxx_parsers_cfg.config.clone() + config.castxml_epic_version = 1 + decls = parser.parse(TEST_FILES, config, COMPILATION_MODE) + global_ns = declarations.get_global_namespace(decls) + if INIT_OPTIMIZER: + global_ns.init_optimizer() + return global_ns + + +def test_copy_constructor(global_ns): + """ + Check the is_copy_constructor method. + + This fails when using CastXML, see issue #27. + + """ + + tclass = global_ns.class_("test") + ctors = [] + for decl in tclass.declarations: + if isinstance(decl, declarations.constructor_t): + ctors.append(decl) + + # test::test(test const & t0) [copy constructor] + assert declarations.is_copy_constructor(ctors[0]) + # test::test(float const & t0) [constructor] + assert not declarations.is_copy_constructor(ctors[1]) + # test::test(myvar t0) [constructor] + assert not declarations.is_copy_constructor(ctors[2]) + + t2class = global_ns.class_("test2") + ctors = [] + for decl in t2class.declarations: + if isinstance(decl, declarations.constructor_t): + ctors.append(decl) + + # test2::test2() [constructor] + assert not declarations.is_copy_constructor(ctors[0]) + # test2::test2(test2 const & arg0) [copy constructor] + assert declarations.is_copy_constructor(ctors[1]) diff --git a/tests/test_create_decl_string.py b/tests/test_create_decl_string.py index 14a622c4..aed3f0cf 100644 --- a/tests/test_create_decl_string.py +++ b/tests/test_create_decl_string.py @@ -3,61 +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): +TEST_FILES = [ + "declaration_string.hpp", +] - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.header = "declaration_string.hpp" - self.global_ns = None - 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() +@pytest.fixture +def global_ns(): + COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE + INIT_OPTIMIZER = True + config = autoconfig.cxx_parsers_cfg.config.clone() + config.castxml_epic_version = 1 + decls = parser.parse(TEST_FILES, config, COMPILATION_MODE) + global_ns = declarations.get_global_namespace(decls) + if INIT_OPTIMIZER: + global_ns.init_optimizer() + return global_ns - def test(self): - """ - Test the create_decl_string method. - """ +def test_create_decl_string(global_ns): + """ + Test the create_decl_string method. - myfunc = self.global_ns.free_function("myfunc") + """ - decl = declarations.free_function_type_t.create_decl_string( - myfunc.return_type, myfunc.argument_types) + myfunc = global_ns.free_function("myfunc") - self.assertTrue(decl != "('int (*)( int,int )', 'int (*)( int,int )')") + decl = declarations.free_function_type_t.create_decl_string( + myfunc.return_type, myfunc.argument_types) - box = self.global_ns.class_("Box") - myinternfunc = box.member_function("myinternfunc") - decl = declarations.member_function_type_t.create_decl_string( - myinternfunc.return_type, - box.decl_string, - myinternfunc.argument_types, - myinternfunc.has_const) + assert decl != "('int (*)( int,int )', 'int (*)( int,int )')" - self.assertTrue(decl != "short int ( ::Box::* )( ) ") + box = global_ns.class_("Box") + myinternfunc = box.member_function("myinternfunc") + decl = declarations.member_function_type_t.create_decl_string( + myinternfunc.return_type, + box.decl_string, + myinternfunc.argument_types, + myinternfunc.has_const) - -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() + assert decl != "short int ( ::Box::* )( ) " diff --git a/tests/test_decl_string.py b/tests/test_decl_string.py index 1205dd6a..fe13fde2 100644 --- a/tests/test_decl_string.py +++ b/tests/test_decl_string.py @@ -3,85 +3,69 @@ # Distributed under the Boost Software License, Version 1.0. # See http://www.boost.org/LICENSE_1_0.txt -import os -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): - 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 = os.path.join( - autoconfig.data_directory, - 'declarations_calldef.hpp') - self.template = """ - //test generated declaration string using gcc(xml) compiler - #include "declarations_calldef.hpp" - void test_generated_decl_string( %s ); - """ +TEST_FILES = [ + "declarations_calldef.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_member_function(self): - member_inline_call = \ - self.global_ns.member_function('member_inline_call') +@pytest.fixture +def global_ns(): + COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE + INIT_OPTIMIZER = True + config = autoconfig.cxx_parsers_cfg.config.clone() + config.castxml_epic_version = 1 + decls = parser.parse(TEST_FILES, config, COMPILATION_MODE) + global_ns = declarations.get_global_namespace(decls) + if INIT_OPTIMIZER: + global_ns.init_optimizer() + return global_ns + + +TEMPLATE = """ + //test generated declaration string using gcc(xml) compiler + #include "declarations_calldef.hpp" + void test_generated_decl_string( %s ); + """ + + +def test_member_function(global_ns): + config = autoconfig.cxx_parsers_cfg.config.clone() + member_inline_call = \ + global_ns.member_function('member_inline_call') + decls = parser.parse_string( + TEMPLATE % + member_inline_call.decl_string, + config) + assert decls is not None + + +def test_free_function(global_ns): + config = autoconfig.cxx_parsers_cfg.config.clone() + return_default_args = \ + global_ns.free_function('return_default_args') + decls = parser.parse_string( + TEMPLATE % + return_default_args.decl_string, + config) + assert decls is not None + + +def test_all_mem_and_free_funs(global_ns): + config = autoconfig.cxx_parsers_cfg.config.clone() + ns = global_ns.namespace('::declarations::calldef') + for f in ns.member_functions(): decls = parser.parse_string( - self.template % - member_inline_call.decl_string, - self.config) - self.assertTrue( - decls, - "Created decl_string for member function contains mistake") - - def test_free_function(self): - return_default_args = \ - self.global_ns.free_function('return_default_args') + TEMPLATE % f.decl_string, config) + assert decls is not None + for f in ns.free_functions(): decls = parser.parse_string( - self.template % - return_default_args.decl_string, - self.config) - self.assertTrue( - decls, - "Created decl_string for global function contains mistake") - - def test_all_mem_and_free_funs(self): - ns = self.global_ns.namespace('::declarations::calldef') - for f in ns.member_functions(): - decls = parser.parse_string( - self.template % f.decl_string, self.config) - self.assertTrue( - decls, - "Created decl_string for member function contains mistake") - for f in ns.free_functions(): - decls = parser.parse_string( - self.template % f.decl_string, self.config) - self.assertTrue( - decls, - "Created decl_string for member function contains mistake") - - -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() + TEMPLATE % f.decl_string, config) + assert decls is not None diff --git a/tests/test_declaration_matcher.py b/tests/test_declaration_matcher.py index 3dc848e7..3a36d5b0 100644 --- a/tests/test_declaration_matcher.py +++ b/tests/test_declaration_matcher.py @@ -3,79 +3,66 @@ # 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 = [ + "classes.hpp", +] -class Test(parser_test_case.parser_test_case_t): - global_ns = None - COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE - - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.header = 'classes.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) - Test.global_ns.init_optimizer() - self.global_ns = Test.global_ns - - def test_global(self): - gns = self.global_ns - gns.class_('cls') - gns.class_('::cls') - - def test_typedefs(self): - gns = self.global_ns - gns.class_('cls2') - if self.config.xml_generator == "castxml": - gns.typedef('cls2') - gns.class_('::cls2') - - gns.class_('cls3') - if self.config.xml_generator == "castxml": - gns.typedef('cls3') - cls3 = gns.class_('::cls3') - cls3.variable('i') - - def test_ns1(self): - gns = self.global_ns - ns1 = gns.namespace('ns') - - gns.class_('nested_cls') - self.assertRaises(Exception, lambda: gns.class_('ns::nested_cls')) - gns.class_('::ns::nested_cls') - - self.assertRaises(Exception, lambda: ns1.class_('::nested_cls')) - ns1.class_('nested_cls') - ns1.class_('::ns::nested_cls') - gns.class_('nested_cls2') - self.assertRaises(Exception, lambda: gns.class_('ns::nested_cls2')) - gns.class_('::ns::nested_cls2') - - gns.class_('nested_cls3') - self.assertRaises(Exception, lambda: gns.class_('ns::nested_cls3')) - gns.class_('::ns::nested_cls3') - - -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 + INIT_OPTIMIZER = True + config = autoconfig.cxx_parsers_cfg.config.clone() + config.castxml_epic_version = 1 + decls = parser.parse(TEST_FILES, config, COMPILATION_MODE) + global_ns = declarations.get_global_namespace(decls) + if INIT_OPTIMIZER: + global_ns.init_optimizer() + return global_ns + + +def test_global(global_ns): + global_ns.class_('cls') + global_ns.class_('::cls') + + +def test_typedefs(global_ns): + global_ns.class_('cls2') + global_ns.typedef('cls2') + global_ns.class_('::cls2') + + global_ns.class_('cls3') + global_ns.typedef('cls3') + cls3 = global_ns.class_('::cls3') + cls3.variable('i') + + +def test_ns1(global_ns): + ns1 = global_ns.namespace('ns') + + global_ns.class_('nested_cls') + with pytest.raises(Exception): + global_ns.class_('ns::nested_cls') + global_ns.class_('::ns::nested_cls') + + with pytest.raises(Exception): + ns1.class_('::nested_cls') + ns1.class_('nested_cls') + ns1.class_('::ns::nested_cls') + + global_ns.class_('nested_cls2') + with pytest.raises(Exception): + global_ns.class_('ns::nested_cls2') + global_ns.class_('::ns::nested_cls2') + + global_ns.class_('nested_cls3') + with pytest.raises(Exception): + global_ns.class_('ns::nested_cls3') + global_ns.class_('::ns::nested_cls3') diff --git a/tests/test_declarations_comparison.py b/tests/test_declarations_comparison.py index a6a008b4..6510e682 100644 --- a/tests/test_declarations_comparison.py +++ b/tests/test_declarations_comparison.py @@ -4,84 +4,70 @@ # See http://www.boost.org/LICENSE_1_0.txt import copy -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 = 'declarations_comparison.hpp' - - def test_comparison_declaration_by_declaration(self): - parsed = parser.parse([self.header], self.config) - copied = copy.deepcopy(parsed) - parsed = declarations.make_flatten(parsed) - copied = declarations.make_flatten(copied) - parsed.sort() - copied.sort() - failuers = [] - for parsed_decl, copied_decl, index in \ - zip(parsed, copied, list(range(len(copied)))): - - if parsed_decl != copied_decl: - failuers.append( - ("__lt__ and/or __qe__ does not working " + - "properly in case of %s, %s, index %d") % - (parsed_decl.__class__.__name__, - copied_decl.__class__.__name__, index)) - self.assertTrue(not failuers, 'Failures: ' + '\n\t'.join(failuers)) - - def test_comparison_from_reverse(self): - parsed = parser.parse([self.header], self.config) - copied = copy.deepcopy(parsed) - parsed.sort() - copied.reverse() - copied.sort() - x = parsed[4:6] - x.sort() - y = copied[4:6] - y.sort() - self.assertTrue( - parsed == copied, - "__lt__ and/or __qe__ does not working properly") - - def test___lt__transitivnost(self): - ns_std = declarations.namespace_t(name='std') - ns_global = declarations.namespace_t(name='::') - ns_internal = declarations.namespace_t(name='ns') - ns_internal.parent = ns_global - ns_global.declarations.append(ns_internal) - left2right = [ns_std, ns_global] - right2left = [ns_global, ns_std] - left2right.sort() - right2left.sort() - self.assertTrue(left2right == right2left, "bug: find me") - - def test_same_declarations_different_intances(self): - parsed = parser.parse([self.header], self.config) - copied = copy.deepcopy(parsed) - self.assertTrue( - parsed == copied, - "__lt__ and/or __qe__ does not working properly") - - -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 = [ + "declarations_comparison.hpp", +] + + +def test_comparison_declaration_by_declaration(): + config = autoconfig.cxx_parsers_cfg.config.clone() + config.castxml_epic_version = 1 + parsed = parser.parse(TEST_FILES, config) + copied = copy.deepcopy(parsed) + parsed = declarations.make_flatten(parsed) + copied = declarations.make_flatten(copied) + parsed.sort() + copied.sort() + failuers = [] + for parsed_decl, copied_decl, index in \ + zip(parsed, copied, list(range(len(copied)))): + + if parsed_decl != copied_decl: + failuers.append( + ("__lt__ and/or __qe__ does not working " + + "properly in case of %s, %s, index %d") % + (parsed_decl.__class__.__name__, + copied_decl.__class__.__name__, index)) + assert failuers == [] + + +def test_comparison_from_reverse(): + config = autoconfig.cxx_parsers_cfg.config.clone() + config.castxml_epic_version = 1 + parsed = parser.parse(TEST_FILES, config) + copied = copy.deepcopy(parsed) + parsed.sort() + copied.reverse() + copied.sort() + x = parsed[4:6] + x.sort() + y = copied[4:6] + y.sort() + assert parsed == copied + + +def test___lt__transitivnost(): + ns_std = declarations.namespace_t(name='std') + ns_global = declarations.namespace_t(name='::') + ns_internal = declarations.namespace_t(name='ns') + ns_internal.parent = ns_global + ns_global.declarations.append(ns_internal) + left2right = [ns_std, ns_global] + right2left = [ns_global, ns_std] + left2right.sort() + right2left.sort() + assert left2right == right2left + + +def test_same_declarations_different_intances(): + config = autoconfig.cxx_parsers_cfg.config.clone() + config.castxml_epic_version = 1 + parsed = parser.parse(TEST_FILES, config) + copied = copy.deepcopy(parsed) + assert parsed == copied