diff --git a/tests/test_attributes.py b/tests/test_attributes.py index 38aaf3e6..43de7fdd 100644 --- a/tests/test_attributes.py +++ b/tests/test_attributes.py @@ -3,98 +3,63 @@ # 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 = [ + # TODO: once gccxml is removed; rename this to something like + # annotate_tester + "attributes_castxml.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) - # TODO: once gccxml is removed; rename this to something like - # annotate_tester - self.header = "attributes_" + self.config.xml_generator + ".hpp" - - def setUp(self): - # Reset flags before each test - self.config.flags = "" - - def test_attributes(self): - - decls = parser.parse([self.header], self.config) - Test.global_ns = declarations.get_global_namespace(decls) - Test.global_ns.init_optimizer() - - numeric = self.global_ns.class_('numeric_t') - do_nothing = numeric.member_function('do_nothing') - arg = do_nothing.arguments[0] - - generator = self.config.xml_generator_from_xml_file - if generator.is_castxml1 or \ - (generator.is_castxml and - float(generator.xml_output_version) >= 1.137): - # This works since: - # https://github.com/CastXML/CastXML/issues/25 - # https://github.com/CastXML/CastXML/pull/26 - # https://github.com/CastXML/CastXML/pull/27 - # The version bump to 1.137 came way later but this is the - # only way to make sure the test is running correctly - self.assertTrue("annotate(sealed)" == numeric.attributes) - self.assertTrue("annotate(no throw)" == do_nothing.attributes) - self.assertTrue("annotate(out)" == arg.attributes) - self.assertTrue( - numeric.member_operators(name="=")[0].attributes is None) - else: - self.assertTrue("gccxml(no throw)" == do_nothing.attributes) - self.assertTrue("gccxml(out)" == arg.attributes) - - def test_attributes_thiscall(self): - """ - Test attributes with the "f2" flag - - """ - if self.config.compiler != "msvc": - return - - self.config.flags = ["f2"] - - decls = parser.parse([self.header], self.config) - Test.global_ns = declarations.get_global_namespace(decls) - Test.global_ns.init_optimizer() - - numeric = self.global_ns.class_('numeric_t') - do_nothing = numeric.member_function('do_nothing') - arg = do_nothing.arguments[0] - - generator = self.config.xml_generator_from_xml_file - if generator.is_castxml1 or ( - generator.is_castxml and - float(generator.xml_output_version) >= 1.137): - self.assertTrue("annotate(sealed)" == numeric.attributes) - self.assertTrue("annotate(out)" == arg.attributes) - - self.assertTrue( - "__thiscall__ annotate(no throw)" == do_nothing.attributes) - self.assertTrue( - numeric.member_operators(name="=")[0].attributes == - "__thiscall__") - - -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() + 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_attributes(global_ns): + numeric = global_ns.class_('numeric_t') + do_nothing = numeric.member_function('do_nothing') + arg = do_nothing.arguments[0] + assert "annotate(sealed)" == numeric.attributes + assert "annotate(no throw)" == do_nothing.attributes + assert "annotate(out)" == arg.attributes + assert numeric.member_operators(name="=")[0].attributes is None + + +def test_attributes_thiscall(): + """ + Test attributes with the "f2" flag + + """ + + COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE + config = autoconfig.cxx_parsers_cfg.config.clone() + + config.flags = ["f2"] + + decls = parser.parse(TEST_FILES, config, COMPILATION_MODE) + global_ns = declarations.get_global_namespace(decls) + global_ns.init_optimizer() + + numeric = global_ns.class_('numeric_t') + do_nothing = numeric.member_function('do_nothing') + arg = do_nothing.arguments[0] + + assert "annotate(sealed)" == numeric.attributes + assert "annotate(out)" == arg.attributes + + assert "annotate(no throw)" == do_nothing.attributes + assert numeric.member_operators(name="=")[0].attributes is None diff --git a/tests/test_better_templates_matcher.py b/tests/test_better_templates_matcher.py index 86137969..97bc80d9 100644 --- a/tests/test_better_templates_matcher.py +++ b/tests/test_better_templates_matcher.py @@ -10,23 +10,24 @@ from pygccxml import parser from pygccxml import declarations +TEST_FILES = [ + "better_templates_matcher_tester.hpp", +] + @pytest.fixture -def global_ns_better(): +def global_ns(): COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE INIT_OPTIMIZER = True config = autoconfig.cxx_parsers_cfg.config.clone() - decls = parser.parse( - ['better_templates_matcher_tester.hpp'], - config, COMPILATION_MODE - ) + 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_better_templates_matcher(global_ns_better): +def test_better_templates_matcher(global_ns): classes = [ "::Ogre::PlaneBoundedVolume", "::Ogre::Plane", @@ -34,4 +35,4 @@ def test_better_templates_matcher(global_ns_better): "::Ogre::PCZoneFactoryManager", ] for i in classes: - global_ns_better.class_(i) + global_ns.class_(i) diff --git a/tests/test_bit_fields.py b/tests/test_bit_fields.py index 12fbb66b..ec9aa738 100644 --- a/tests/test_bit_fields.py +++ b/tests/test_bit_fields.py @@ -3,52 +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 +TEST_FILES = [ + "bit_fields.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 + INIT_OPTIMIZER = True + config = autoconfig.cxx_parsers_cfg.config.clone() + 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 __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.header = 'bit_fields.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_bit_fields(global_ns): + bf_x = global_ns.variable('x') + assert bf_x.bits == 1 - def test_bit_fields(self): - bf_x = self.global_ns.variable('x') - self.assertTrue(bf_x.bits == 1) + bf_y = global_ns.variable('y') + assert bf_y.bits == 7 - bf_y = self.global_ns.variable('y') - self.assertTrue(bf_y.bits == 7) - - mv_z = self.global_ns.variable('z') - self.assertTrue(mv_z.bits is None) - - 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() + mv_z = global_ns.variable('z') + assert mv_z.bits is None diff --git a/tests/test_cache_enums.py b/tests/test_cache_enums.py index 5a726e78..1bd07e69 100644 --- a/tests/test_cache_enums.py +++ b/tests/test_cache_enums.py @@ -4,61 +4,38 @@ # 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 tester_impl_t(parser_test_case.parser_test_case_t): - COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE +TEST_FILES = [ + "declarations_enums.hpp", +] - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.header = os.path.join( - autoconfig.data_directory, - 'declarations_enums.hpp') - self.cache_file = os.path.join( - autoconfig.data_directory, - 'pygccxml.cache') - if os.path.exists(self.cache_file) and os.path.isfile(self.cache_file): - os.remove(self.cache_file) - def test_cache(self): - cache = parser.file_cache_t(self.cache_file) - reader = parser.source_reader_t(self.config, cache) - decls1 = reader.read_file(self.header) - cache.flush() - cache = parser.file_cache_t(self.cache_file) - reader = parser.source_reader_t(self.config, cache) - decls2 = reader.read_file(self.header) +def test_cache(): + cache_file = os.path.join(autoconfig.data_directory, 'pygccxml.cache') + if os.path.exists(cache_file) and os.path.isfile(cache_file): + os.remove(cache_file) - enum_matcher = declarations.declaration_matcher_t( - name="EColor", - decl_type=declarations.enumeration_t) + config = autoconfig.cxx_parsers_cfg.config.clone() - color1 = declarations.matcher.get_single(enum_matcher, decls1) - color2 = declarations.matcher.get_single(enum_matcher, decls2) - self.assertTrue(color1.values == color2.values) + cache = parser.file_cache_t(cache_file) + reader = parser.source_reader_t(config, cache) + decls1 = reader.read_file(TEST_FILES[0]) + cache.flush() + cache = parser.file_cache_t(cache_file) + reader = parser.source_reader_t(config, cache) + decls2 = reader.read_file(TEST_FILES[0]) + enum_matcher = declarations.declaration_matcher_t( + name="EColor", + decl_type=declarations.enumeration_t + ) -class Test(tester_impl_t): - CXX_PARSER_CFG = autoconfig.cxx_parsers_cfg.config - - -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() + color1 = declarations.matcher.get_single(enum_matcher, decls1) + color2 = declarations.matcher.get_single(enum_matcher, decls2) + assert color1.values == color2.values diff --git a/tests/test_cached_source_file.py b/tests/test_cached_source_file.py index 5995badc..bdb6836f 100644 --- a/tests/test_cached_source_file.py +++ b/tests/test_cached_source_file.py @@ -5,50 +5,35 @@ import os import stat -import unittest -from . import parser_test_case +from . import autoconfig from pygccxml import utils from pygccxml import parser - -class Test(parser_test_case.parser_test_case_t): - - def __init__(self, *args): - parser_test_case.parser_test_case_t.__init__(self, *args) - self.__fname = 'core_types.hpp' - - def test(self): - fconfig = parser.file_configuration_t( - data=self.__fname, - content_type=parser.CONTENT_TYPE.CACHED_SOURCE_FILE) - try: - prj_reader = parser.project_reader_t(self.config) - prj_reader.read_files( - [fconfig], - compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE) - self.assertTrue(os.path.exists(fconfig.cached_source_file)) - mtime1 = os.stat(fconfig.cached_source_file)[stat.ST_MTIME] - prj_reader.read_files( - [fconfig], - compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE) - mtime2 = os.stat(fconfig.cached_source_file)[stat.ST_MTIME] - self.assertTrue(mtime1 == mtime2) - finally: - utils.remove_file_no_raise(fconfig.cached_source_file, self.config) - - -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_types.hpp", +] + + +def test_cached_source_file(): + + config = autoconfig.cxx_parsers_cfg.config.clone() + + fconfig = parser.file_configuration_t( + data=TEST_FILES[0], + content_type=parser.CONTENT_TYPE.CACHED_SOURCE_FILE) + try: + prj_reader = parser.project_reader_t(config) + prj_reader.read_files( + [fconfig], + compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE) + assert os.path.exists(fconfig.cached_source_file) + mtime1 = os.stat(fconfig.cached_source_file)[stat.ST_MTIME] + prj_reader.read_files( + [fconfig], + compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE) + mtime2 = os.stat(fconfig.cached_source_file)[stat.ST_MTIME] + assert mtime1 == mtime2 + finally: + utils.remove_file_no_raise(fconfig.cached_source_file, config) diff --git a/tests/test_call_invocation.py b/tests/test_call_invocation.py index 68b6c5c8..9115254d 100644 --- a/tests/test_call_invocation.py +++ b/tests/test_call_invocation.py @@ -3,101 +3,85 @@ # Distributed under the Boost Software License, Version 1.0. # See http://www.boost.org/LICENSE_1_0.txt -import unittest +from pygccxml import declarations -from . import parser_test_case -from pygccxml import declarations +def __test_split_impl(decl_string, name, args): + assert (name, args) == \ + declarations.call_invocation.split(decl_string) + + +def __test_split_recursive_impl(decl_string, control_seq): + assert control_seq == \ + list(declarations.call_invocation.split_recursive(decl_string)) + + +def __test_is_call_invocation_impl(decl_string): + assert declarations.call_invocation.is_call_invocation(decl_string) + + +def test_split_on_vector(): + __test_is_call_invocation_impl("vector(int,std::allocator(int) )") + + __test_split_impl( + "vector(int,std::allocator(int) )", + "vector", + ["int", "std::allocator(int)"]) + + __test_split_recursive_impl( + "vector(int,std::allocator(int) )", + [("vector", ["int", "std::allocator(int)"]), + ("std::allocator", ["int"])]) + + +def test_split_on_string(): + __test_is_call_invocation_impl( + "basic_string(char,std::char_traits(char),std::allocator(char) )") + + __test_split_impl( + "basic_string(char,std::char_traits(char),std::allocator(char) )", + "basic_string", + ["char", "std::char_traits(char)", "std::allocator(char)"]) + + +def test_split_on_map(): + __test_is_call_invocation_impl( + "map(long int,std::vector(int, std::allocator(int) )," + + "std::less(long int),std::allocator(std::pair" + + "(const long int, std::vector(int, std::allocator(int) ) ) ) )") + + __test_split_impl( + "map(long int,std::vector(int, std::allocator(int) )," + + "std::less(long int),std::allocator(std::pair" + + "(const long int, std::vector(int, std::allocator(int) ) ) ) )", + "map", + ["long int", "std::vector(int, std::allocator(int) )", + "std::less(long int)", + "std::allocator(std::pair(const long int," + + " std::vector(int, std::allocator(int) ) ) )"]) + + +def test_join_on_vector(): + assert "vector( int, std::allocator(int) )" == \ + declarations.call_invocation.join( + "vector", ("int", "std::allocator(int)")) + + +def test_find_args(): + temp = 'x()()' + found = declarations.call_invocation.find_args(temp) + assert (1, 2) == found + found = declarations.call_invocation.find_args(temp, found[1] + 1) + assert (3, 4) == found + temp = 'x(int,int)(1,2)' + found = declarations.call_invocation.find_args(temp) + assert (1, 9) == found + found = declarations.call_invocation.find_args(temp, found[1] + 1) + assert (10, 14) == found -class Test(parser_test_case.parser_test_case_t): - - def __test_split_impl(self, decl_string, name, args): - self.assertTrue( - (name, args) == declarations.call_invocation.split(decl_string)) - - def __test_split_recursive_impl(self, decl_string, control_seq): - self.assertTrue( - control_seq == - list(declarations.call_invocation.split_recursive(decl_string))) - - def __test_is_call_invocation_impl(self, decl_string): - self.assertTrue( - declarations.call_invocation.is_call_invocation(decl_string)) - - def test_split_on_vector(self): - self.__test_is_call_invocation_impl("vector(int,std::allocator(int) )") - - self.__test_split_impl( - "vector(int,std::allocator(int) )", - "vector", - ["int", "std::allocator(int)"]) - - self.__test_split_recursive_impl( - "vector(int,std::allocator(int) )", - [("vector", ["int", "std::allocator(int)"]), - ("std::allocator", ["int"])]) - - def test_split_on_string(self): - self.__test_is_call_invocation_impl( - "basic_string(char,std::char_traits(char),std::allocator(char) )") - - self.__test_split_impl( - "basic_string(char,std::char_traits(char),std::allocator(char) )", - "basic_string", - ["char", "std::char_traits(char)", "std::allocator(char)"]) - - def test_split_on_map(self): - self.__test_is_call_invocation_impl( - "map(long int,std::vector(int, std::allocator(int) )," + - "std::less(long int),std::allocator(std::pair" + - "(const long int, std::vector(int, std::allocator(int) ) ) ) )") - - self.__test_split_impl( - "map(long int,std::vector(int, std::allocator(int) )," + - "std::less(long int),std::allocator(std::pair" + - "(const long int, std::vector(int, std::allocator(int) ) ) ) )", - "map", - ["long int", "std::vector(int, std::allocator(int) )", - "std::less(long int)", - "std::allocator(std::pair(const long int," + - " std::vector(int, std::allocator(int) ) ) )"]) - - def test_join_on_vector(self): - self.assertTrue( - "vector( int, std::allocator(int) )" == - declarations.call_invocation.join( - "vector", ("int", "std::allocator(int)"))) - - def test_find_args(self): - temp = 'x()()' - found = declarations.call_invocation.find_args(temp) - self.assertTrue((1, 2) == found) - found = declarations.call_invocation.find_args(temp, found[1] + 1) - self.assertTrue((3, 4) == found) - temp = 'x(int,int)(1,2)' - found = declarations.call_invocation.find_args(temp) - self.assertTrue((1, 9) == found) - found = declarations.call_invocation.find_args(temp, found[1] + 1) - self.assertTrue((10, 14) == found) - - def test_bug_unmatched_brace(self): - src = 'AlternativeName((&string("")), (&string("")), (&string("")))' - self.__test_split_impl( - src, 'AlternativeName', [ - '(&string(""))', '(&string(""))', '(&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_bug_unmatched_brace(): + src = 'AlternativeName((&string("")), (&string("")), (&string("")))' + __test_split_impl( + src, 'AlternativeName', [ + '(&string(""))', '(&string(""))', '(&string(""))']) diff --git a/tests/test_calldef_matcher.py b/tests/test_calldef_matcher.py index 512f1864..cc02c61a 100644 --- a/tests/test_calldef_matcher.py +++ b/tests/test_calldef_matcher.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 -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_calldef.hpp' - self.declarations = None - - def setUp(self): - if not self.declarations: - self.declarations = parser.parse([self.header], self.config) - - def test_calldef_matcher(self): - criteria = declarations.calldef_matcher_t( - name='return_default_args', - return_type='int', - arg_types=[None, declarations.bool_t()]) - rda = declarations.matcher.get_single(criteria, self.declarations) - self.assertTrue(rda, "return_default_args function was not found.") - +TEST_FILES = [ + "declarations_calldef.hpp", +] -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 decls(): + COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE + config = autoconfig.cxx_parsers_cfg.config.clone() + decls = parser.parse(TEST_FILES, config, COMPILATION_MODE) + return decls + + +def test_calldef_matcher(decls): + criteria = declarations.calldef_matcher_t( + name='return_default_args', + return_type='int', + arg_types=[None, declarations.bool_t()]) + rda = declarations.matcher.get_single(criteria, decls) + assert rda is not None diff --git a/tests/test_calling_convention.py b/tests/test_calling_convention.py index 381f476d..eab26161 100644 --- a/tests/test_calling_convention.py +++ b/tests/test_calling_convention.py @@ -3,40 +3,17 @@ # Distributed under the Boost Software License, Version 1.0. # See http://www.boost.org/LICENSE_1_0.txt -import unittest - -from . import parser_test_case - from pygccxml import declarations -class Test(parser_test_case.parser_test_case_t): - - def test_extract(self): - data = [ - ('thiscall', - '(public: __thiscall std::auto_ptr' + - '::auto_ptr(class std::auto_ptr' + - ' &))'), - ('', "(const pof::number_t::`vftable')")] - - for expected, text in data: - got = declarations.CALLING_CONVENTION_TYPES.extract(text) - self.assertTrue( - got == expected, "Expected calling convention: %s, got %s" % - (expected, got)) - - -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()) - +def test_extract(): + data = [ + ('thiscall', + '(public: __thiscall std::auto_ptr' + + '::auto_ptr(class std::auto_ptr' + + ' &))'), + ('', "(const pof::number_t::`vftable')")] -if __name__ == "__main__": - run_suite() + for expected, text in data: + got = declarations.CALLING_CONVENTION_TYPES.extract(text) + assert got == expected