Skip to content

Commit 95afec7

Browse files
committedOct 23, 2024··
tests: refactor / simplify
1 parent 783b3d6 commit 95afec7

8 files changed

+241
-382
lines changed
 

‎tests/test_attributes.py

+54-89
Original file line numberDiff line numberDiff line change
@@ -3,98 +3,63 @@
33
# Distributed under the Boost Software License, Version 1.0.
44
# See http://www.boost.org/LICENSE_1_0.txt
55

6-
import unittest
6+
import pytest
77

8-
from . import parser_test_case
8+
from . import autoconfig
99

1010
from pygccxml import parser
1111
from pygccxml import declarations
1212

13+
TEST_FILES = [
14+
# TODO: once gccxml is removed; rename this to something like
15+
# annotate_tester
16+
"attributes_castxml.hpp",
17+
]
1318

14-
class Test(parser_test_case.parser_test_case_t):
15-
global_ns = None
16-
17-
def __init__(self, *args):
18-
parser_test_case.parser_test_case_t.__init__(self, *args)
19-
# TODO: once gccxml is removed; rename this to something like
20-
# annotate_tester
21-
self.header = "attributes_" + self.config.xml_generator + ".hpp"
22-
23-
def setUp(self):
24-
# Reset flags before each test
25-
self.config.flags = ""
26-
27-
def test_attributes(self):
28-
29-
decls = parser.parse([self.header], self.config)
30-
Test.global_ns = declarations.get_global_namespace(decls)
31-
Test.global_ns.init_optimizer()
32-
33-
numeric = self.global_ns.class_('numeric_t')
34-
do_nothing = numeric.member_function('do_nothing')
35-
arg = do_nothing.arguments[0]
36-
37-
generator = self.config.xml_generator_from_xml_file
38-
if generator.is_castxml1 or \
39-
(generator.is_castxml and
40-
float(generator.xml_output_version) >= 1.137):
41-
# This works since:
42-
# https://github.com/CastXML/CastXML/issues/25
43-
# https://github.com/CastXML/CastXML/pull/26
44-
# https://github.com/CastXML/CastXML/pull/27
45-
# The version bump to 1.137 came way later but this is the
46-
# only way to make sure the test is running correctly
47-
self.assertTrue("annotate(sealed)" == numeric.attributes)
48-
self.assertTrue("annotate(no throw)" == do_nothing.attributes)
49-
self.assertTrue("annotate(out)" == arg.attributes)
50-
self.assertTrue(
51-
numeric.member_operators(name="=")[0].attributes is None)
52-
else:
53-
self.assertTrue("gccxml(no throw)" == do_nothing.attributes)
54-
self.assertTrue("gccxml(out)" == arg.attributes)
55-
56-
def test_attributes_thiscall(self):
57-
"""
58-
Test attributes with the "f2" flag
59-
60-
"""
61-
if self.config.compiler != "msvc":
62-
return
63-
64-
self.config.flags = ["f2"]
65-
66-
decls = parser.parse([self.header], self.config)
67-
Test.global_ns = declarations.get_global_namespace(decls)
68-
Test.global_ns.init_optimizer()
69-
70-
numeric = self.global_ns.class_('numeric_t')
71-
do_nothing = numeric.member_function('do_nothing')
72-
arg = do_nothing.arguments[0]
73-
74-
generator = self.config.xml_generator_from_xml_file
75-
if generator.is_castxml1 or (
76-
generator.is_castxml and
77-
float(generator.xml_output_version) >= 1.137):
78-
self.assertTrue("annotate(sealed)" == numeric.attributes)
79-
self.assertTrue("annotate(out)" == arg.attributes)
80-
81-
self.assertTrue(
82-
"__thiscall__ annotate(no throw)" == do_nothing.attributes)
83-
self.assertTrue(
84-
numeric.member_operators(name="=")[0].attributes ==
85-
"__thiscall__")
86-
87-
88-
def create_suite():
89-
suite = unittest.TestSuite()
90-
suite.addTest(
91-
unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test))
92-
return suite
93-
94-
95-
def run_suite():
96-
unittest.TextTestRunner(verbosity=2).run(create_suite())
97-
98-
99-
if __name__ == "__main__":
100-
run_suite()
19+
20+
@pytest.fixture
21+
def global_ns():
22+
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
23+
INIT_OPTIMIZER = True
24+
config = autoconfig.cxx_parsers_cfg.config.clone()
25+
decls = parser.parse(TEST_FILES, config, COMPILATION_MODE)
26+
global_ns = declarations.get_global_namespace(decls)
27+
if INIT_OPTIMIZER:
28+
global_ns.init_optimizer()
29+
return global_ns
30+
31+
32+
def test_attributes(global_ns):
33+
numeric = global_ns.class_('numeric_t')
34+
do_nothing = numeric.member_function('do_nothing')
35+
arg = do_nothing.arguments[0]
36+
assert "annotate(sealed)" == numeric.attributes
37+
assert "annotate(no throw)" == do_nothing.attributes
38+
assert "annotate(out)" == arg.attributes
39+
assert numeric.member_operators(name="=")[0].attributes is None
40+
41+
42+
def test_attributes_thiscall():
43+
"""
44+
Test attributes with the "f2" flag
45+
46+
"""
47+
48+
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
49+
config = autoconfig.cxx_parsers_cfg.config.clone()
50+
51+
config.flags = ["f2"]
52+
53+
decls = parser.parse(TEST_FILES, config, COMPILATION_MODE)
54+
global_ns = declarations.get_global_namespace(decls)
55+
global_ns.init_optimizer()
56+
57+
numeric = global_ns.class_('numeric_t')
58+
do_nothing = numeric.member_function('do_nothing')
59+
arg = do_nothing.arguments[0]
60+
61+
assert "annotate(sealed)" == numeric.attributes
62+
assert "annotate(out)" == arg.attributes
63+
64+
assert "annotate(no throw)" == do_nothing.attributes
65+
assert numeric.member_operators(name="=")[0].attributes is None

‎tests/test_better_templates_matcher.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,29 @@
1010
from pygccxml import parser
1111
from pygccxml import declarations
1212

13+
TEST_FILES = [
14+
"better_templates_matcher_tester.hpp",
15+
]
16+
1317

1418
@pytest.fixture
15-
def global_ns_better():
19+
def global_ns():
1620
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
1721
INIT_OPTIMIZER = True
1822
config = autoconfig.cxx_parsers_cfg.config.clone()
19-
decls = parser.parse(
20-
['better_templates_matcher_tester.hpp'],
21-
config, COMPILATION_MODE
22-
)
23+
decls = parser.parse(TEST_FILES, config, COMPILATION_MODE)
2324
global_ns = declarations.get_global_namespace(decls)
2425
if INIT_OPTIMIZER:
2526
global_ns.init_optimizer()
2627
return global_ns
2728

2829

29-
def test_better_templates_matcher(global_ns_better):
30+
def test_better_templates_matcher(global_ns):
3031
classes = [
3132
"::Ogre::PlaneBoundedVolume",
3233
"::Ogre::Plane",
3334
"::Ogre::Singleton<Ogre::PCZoneFactoryManager>",
3435
"::Ogre::PCZoneFactoryManager",
3536
]
3637
for i in classes:
37-
global_ns_better.class_(i)
38+
global_ns.class_(i)

‎tests/test_bit_fields.py

+22-38
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,36 @@
33
# Distributed under the Boost Software License, Version 1.0.
44
# See http://www.boost.org/LICENSE_1_0.txt
55

6-
import unittest
6+
import pytest
77

8-
from . import parser_test_case
8+
from . import autoconfig
99

1010
from pygccxml import parser
1111
from pygccxml import declarations
1212

13+
TEST_FILES = [
14+
"bit_fields.hpp",
15+
]
1316

14-
class Test(parser_test_case.parser_test_case_t):
1517

16-
global_ns = None
18+
@pytest.fixture
19+
def global_ns():
20+
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
21+
INIT_OPTIMIZER = True
22+
config = autoconfig.cxx_parsers_cfg.config.clone()
23+
decls = parser.parse(TEST_FILES, config, COMPILATION_MODE)
24+
global_ns = declarations.get_global_namespace(decls)
25+
if INIT_OPTIMIZER:
26+
global_ns.init_optimizer()
27+
return global_ns
1728

18-
def __init__(self, *args):
19-
parser_test_case.parser_test_case_t.__init__(self, *args)
20-
self.header = 'bit_fields.hpp'
2129

22-
def setUp(self):
23-
if not Test.global_ns:
24-
decls = parser.parse([self.header], self.config)
25-
Test.global_ns = declarations.get_global_namespace(decls)
26-
Test.global_ns.init_optimizer()
30+
def test_bit_fields(global_ns):
31+
bf_x = global_ns.variable('x')
32+
assert bf_x.bits == 1
2733

28-
def test_bit_fields(self):
29-
bf_x = self.global_ns.variable('x')
30-
self.assertTrue(bf_x.bits == 1)
34+
bf_y = global_ns.variable('y')
35+
assert bf_y.bits == 7
3136

32-
bf_y = self.global_ns.variable('y')
33-
self.assertTrue(bf_y.bits == 7)
34-
35-
mv_z = self.global_ns.variable('z')
36-
self.assertTrue(mv_z.bits is None)
37-
38-
def test2(self):
39-
pass
40-
41-
42-
def create_suite():
43-
suite = unittest.TestSuite()
44-
suite.addTest(
45-
unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test))
46-
return suite
47-
48-
49-
def run_suite():
50-
unittest.TextTestRunner(verbosity=2).run(create_suite())
51-
52-
53-
if __name__ == "__main__":
54-
run_suite()
37+
mv_z = global_ns.variable('z')
38+
assert mv_z.bits is None

‎tests/test_cache_enums.py

+22-45
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,38 @@
44
# See http://www.boost.org/LICENSE_1_0.txt
55

66
import os
7-
import unittest
87

98
from . import autoconfig
10-
from . import parser_test_case
119

1210
from pygccxml import parser
1311
from pygccxml import declarations
1412

1513

16-
class tester_impl_t(parser_test_case.parser_test_case_t):
17-
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
14+
TEST_FILES = [
15+
"declarations_enums.hpp",
16+
]
1817

19-
def __init__(self, *args):
20-
parser_test_case.parser_test_case_t.__init__(self, *args)
21-
self.header = os.path.join(
22-
autoconfig.data_directory,
23-
'declarations_enums.hpp')
24-
self.cache_file = os.path.join(
25-
autoconfig.data_directory,
26-
'pygccxml.cache')
27-
if os.path.exists(self.cache_file) and os.path.isfile(self.cache_file):
28-
os.remove(self.cache_file)
2918

30-
def test_cache(self):
31-
cache = parser.file_cache_t(self.cache_file)
32-
reader = parser.source_reader_t(self.config, cache)
33-
decls1 = reader.read_file(self.header)
34-
cache.flush()
35-
cache = parser.file_cache_t(self.cache_file)
36-
reader = parser.source_reader_t(self.config, cache)
37-
decls2 = reader.read_file(self.header)
19+
def test_cache():
20+
cache_file = os.path.join(autoconfig.data_directory, 'pygccxml.cache')
21+
if os.path.exists(cache_file) and os.path.isfile(cache_file):
22+
os.remove(cache_file)
3823

39-
enum_matcher = declarations.declaration_matcher_t(
40-
name="EColor",
41-
decl_type=declarations.enumeration_t)
24+
config = autoconfig.cxx_parsers_cfg.config.clone()
4225

43-
color1 = declarations.matcher.get_single(enum_matcher, decls1)
44-
color2 = declarations.matcher.get_single(enum_matcher, decls2)
45-
self.assertTrue(color1.values == color2.values)
26+
cache = parser.file_cache_t(cache_file)
27+
reader = parser.source_reader_t(config, cache)
28+
decls1 = reader.read_file(TEST_FILES[0])
29+
cache.flush()
30+
cache = parser.file_cache_t(cache_file)
31+
reader = parser.source_reader_t(config, cache)
32+
decls2 = reader.read_file(TEST_FILES[0])
4633

34+
enum_matcher = declarations.declaration_matcher_t(
35+
name="EColor",
36+
decl_type=declarations.enumeration_t
37+
)
4738

48-
class Test(tester_impl_t):
49-
CXX_PARSER_CFG = autoconfig.cxx_parsers_cfg.config
50-
51-
52-
def create_suite():
53-
suite = unittest.TestSuite()
54-
suite.addTest(
55-
unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test))
56-
return suite
57-
58-
59-
def run_suite():
60-
unittest.TextTestRunner(verbosity=2).run(create_suite())
61-
62-
63-
if __name__ == "__main__":
64-
run_suite()
39+
color1 = declarations.matcher.get_single(enum_matcher, decls1)
40+
color2 = declarations.matcher.get_single(enum_matcher, decls2)
41+
assert color1.values == color2.values

0 commit comments

Comments
 (0)
Please sign in to comment.