Skip to content

Commit 08675cf

Browse files
committed
tests: refactor / simplify
1 parent 056799e commit 08675cf

11 files changed

+396
-546
lines changed

tests/test_attributes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def test_attributes_thiscall():
4949
config = autoconfig.cxx_parsers_cfg.config.clone()
5050

5151
config.flags = ["f2"]
52+
config.castxml_epic_version = 1
5253

5354
decls = parser.parse(TEST_FILES, config, COMPILATION_MODE)
5455
global_ns = declarations.get_global_namespace(decls)

tests/test_castxml_wrong_epic.py

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,21 @@
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

1212

13-
class Test(parser_test_case.parser_test_case_t):
1413

15-
def test_castxml_epic_version_check(self):
16-
"""
17-
Test using a forbidden value for the castxml epic version.
14+
def test_castxml_epic_version_check():
15+
"""
16+
Test using a forbidden value for the castxml epic version.
1817
19-
"""
18+
"""
2019

21-
if self.config.castxml_epic_version != 1:
22-
# Run this test only with castxml epic version == 1
23-
return
24-
25-
self.config.castxml_epic_version = 2
26-
self.assertRaises(
27-
RuntimeError, lambda: parser.parse_string("", self.config))
28-
29-
# Reset castxml epic version
30-
self.config.castxml_epic_version = 1
31-
32-
33-
def create_suite():
34-
suite = unittest.TestSuite()
35-
suite.addTest(
36-
unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test))
37-
return suite
38-
39-
40-
def run_suite():
41-
unittest.TextTestRunner(verbosity=2).run(create_suite())
42-
43-
44-
if __name__ == "__main__":
45-
run_suite()
20+
config = autoconfig.cxx_parsers_cfg.config.clone()
21+
config.castxml_epic_version = 2
22+
with pytest.raises(RuntimeError):
23+
parser.parse_string("", config)

tests/test_ccflags.py

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,47 @@
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

1313

14-
class Test(parser_test_case.parser_test_case_t):
15-
global_ns = None
14+
TEST_FILES = [
15+
"test_ccflags.hpp",
16+
]
1617

17-
def __init__(self, *args):
18-
parser_test_case.parser_test_case_t.__init__(self, *args)
19-
self.header = "test_ccflags.hpp"
20-
self.global_ns = None
21-
self.config.castxml_epic_version = 1
22-
self.config.append_cflags("-fopenmp")
18+
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
2319

24-
def _parse_src(self):
25-
decls = parser.parse([self.header], self.config)
26-
Test.global_ns = declarations.get_global_namespace(decls)
27-
Test.xml_generator_from_xml_file = (
28-
self.config.xml_generator_from_xml_file
29-
)
30-
self.xml_generator_from_xml_file = Test.xml_generator_from_xml_file
3120

32-
self.global_ns = Test.global_ns
21+
@pytest.fixture
22+
def config():
23+
config = autoconfig.cxx_parsers_cfg.config.clone()
24+
config.castxml_epic_version = 1
25+
config.append_cflags("-fopenmp")
26+
return config
3327

34-
def _add_ccflags(self):
35-
if "clang++" in self.config.compiler_path:
36-
self.config.append_ccflags("-Xpreprocessor")
3728

38-
self.config.append_ccflags("-fopenmp")
29+
def test_ccflags(config):
30+
# First check that macro is not defined.
31+
decls = parser.parse(TEST_FILES, config, COMPILATION_MODE)
32+
global_ns = declarations.get_global_namespace(decls)
3933

40-
def test(self):
41-
# First check that macro is not defined.
42-
self._parse_src()
43-
namespace_names = [
44-
n.name for n in self.global_ns.namespaces(allow_empty=True)
45-
]
46-
self.assertNotIn("ccflags_test_namespace", namespace_names)
34+
namespace_names = [
35+
n.name for n in global_ns.namespaces(allow_empty=True)
36+
]
37+
assert "ccflags_test_namespace" not in namespace_names
4738

48-
# Next check that macro is defined when passed directly as ccflag
49-
self._add_ccflags()
50-
self._parse_src()
51-
namespace_names = [n.name for n in self.global_ns.namespaces()]
52-
self.assertIn("ccflags_test_namespace", namespace_names)
39+
# Next check that macro is defined when passed directly as ccflag
5340

41+
if "clang++" in config.compiler_path:
42+
config.append_ccflags("-Xpreprocessor")
43+
config.append_ccflags("-fopenmp")
5444

55-
def create_suite():
56-
suite = unittest.TestSuite()
57-
suite.addTest(
58-
unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test))
59-
return suite
45+
decls = parser.parse(TEST_FILES, config, COMPILATION_MODE)
46+
global_ns = declarations.get_global_namespace(decls)
6047

61-
62-
def run_suite():
63-
unittest.TextTestRunner(verbosity=2).run(create_suite())
64-
65-
66-
if __name__ == "__main__":
67-
run_suite()
48+
namespace_names = [n.name for n in global_ns.namespaces()]
49+
assert "ccflags_test_namespace" in namespace_names

tests/test_comments.py

Lines changed: 55 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -3,98 +3,79 @@
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

1313

14-
class Test(parser_test_case.parser_test_case_t):
15-
global_ns = None
14+
TEST_FILES = [
15+
"test_comments.hpp",
16+
]
1617

17-
def __init__(self, *args):
18-
parser_test_case.parser_test_case_t.__init__(self, *args)
19-
self.header = "test_comments.hpp"
20-
self.global_ns = None
21-
self.config.castxml_epic_version = 1
2218

23-
def _check_comment_content(self, list, comment_decl):
24-
if comment_decl.text:
25-
self.assertEqual(list, comment_decl.text)
26-
else:
27-
print("No text in comment to check")
19+
@pytest.fixture
20+
def global_ns():
21+
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
22+
INIT_OPTIMIZER = True
23+
config = autoconfig.cxx_parsers_cfg.config.clone()
24+
config.castxml_epic_version = 1
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
2830

29-
def setUp(self):
3031

31-
if not self.global_ns:
32-
decls = parser.parse([self.header], self.config)
33-
Test.global_ns = declarations.get_global_namespace(decls)
34-
Test.xml_generator_from_xml_file = \
35-
self.config.xml_generator_from_xml_file
36-
self.xml_generator_from_xml_file = Test.xml_generator_from_xml_file
32+
def _check_comment_content(list, comment_decl):
33+
if comment_decl.text:
34+
assert list == comment_decl.text
35+
else:
36+
print("No text in comment to check")
3737

38-
self.global_ns = Test.global_ns
3938

40-
def test(self):
41-
"""
42-
Check the comment parsing
43-
"""
44-
if self.config.castxml_epic_version != 1:
45-
# Run this test only with castxml epic version == 1
46-
return
47-
tnamespace = self.global_ns.namespace("comment")
39+
def test_comments(global_ns):
40+
"""
41+
Check the comment parsing
42+
"""
43+
tnamespace = global_ns.namespace("comment")
4844

49-
self.assertIn("comment", dir(tnamespace))
50-
self._check_comment_content(["//! Namespace Comment",
51-
"//! Across multiple lines"],
52-
tnamespace.comment)
45+
assert "comment" in dir(tnamespace)
46+
_check_comment_content(["//! Namespace Comment",
47+
"//! Across multiple lines"],
48+
tnamespace.comment)
5349

54-
tenumeration = tnamespace.enumeration("com_enum")
55-
self.assertIn("comment", dir(tenumeration))
56-
self._check_comment_content(['/// Outside Class enum comment'],
57-
tenumeration.comment)
50+
tenumeration = tnamespace.enumeration("com_enum")
51+
assert "comment" in dir(tenumeration)
52+
_check_comment_content(['/// Outside Class enum comment'],
53+
tenumeration.comment)
5854

59-
tclass = tnamespace.class_("test")
60-
self.assertIn("comment", dir(tclass))
61-
self._check_comment_content(["/** class comment */"], tclass.comment)
55+
tclass = tnamespace.class_("test")
56+
assert "comment" in dir(tclass)
57+
_check_comment_content(["/** class comment */"], tclass.comment)
6258

63-
tcls_enumeration = tclass.enumeration("test_enum")
64-
self.assertIn("comment", dir(tcls_enumeration))
65-
self._check_comment_content(['/// inside class enum comment'],
66-
tcls_enumeration.comment)
59+
tcls_enumeration = tclass.enumeration("test_enum")
60+
assert "comment" in dir(tcls_enumeration)
61+
_check_comment_content(['/// inside class enum comment'],
62+
tcls_enumeration.comment)
6763

68-
tmethod = tclass.member_functions()[0]
64+
tmethod = tclass.member_functions()[0]
6965

70-
self.assertIn("comment", dir(tmethod))
71-
self._check_comment_content(["/// cxx comment",
72-
"/// with multiple lines"],
73-
tmethod.comment)
66+
assert "comment" in dir(tmethod)
67+
_check_comment_content(["/// cxx comment",
68+
"/// with multiple lines"],
69+
tmethod.comment)
7470

75-
tconstructor = tclass.constructors()[0]
71+
tconstructor = tclass.constructors()[0]
7672

77-
self.assertIn("comment", dir(tconstructor))
78-
self._check_comment_content(["/** doc comment */"],
79-
tconstructor.comment)
73+
assert "comment" in dir(tconstructor)
74+
_check_comment_content(["/** doc comment */"],
75+
tconstructor.comment)
8076

81-
for indx, cmt in enumerate(['//! mutable field comment',
82-
"/// bit field comment"]):
83-
tvariable = tclass.variables()[indx]
84-
self.assertIn("comment", dir(tvariable))
85-
self._check_comment_content([cmt], tvariable.comment)
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()
77+
for indx, cmt in enumerate(['//! mutable field comment',
78+
"/// bit field comment"]):
79+
tvariable = tclass.variables()[indx]
80+
assert "comment" in dir(tvariable)
81+
_check_comment_content([cmt], tvariable.comment)

tests/test_complex_types.py

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +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 os
7-
import unittest
6+
import pytest
87

9-
from . import parser_test_case
8+
from . import autoconfig
109

10+
from pygccxml import declarations
1111
from pygccxml import parser
1212

1313

14-
class Test(parser_test_case.parser_test_case_t):
15-
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
16-
17-
def __init__(self, *args):
18-
parser_test_case.parser_test_case_t.__init__(self, *args)
19-
self.header = 'complex_types.hpp'
20-
self.declarations = None
21-
22-
def setUp(self):
23-
if not self.declarations:
24-
self.declarations = parser.parse([self.header], self.config)
25-
26-
def test(self):
27-
"""
28-
This test tests presence of complex long double, float within
29-
FUNDAMENTAL_TYPES map
30-
"""
31-
pass
32-
14+
TEST_FILES = [
15+
"complex_types.hpp",
16+
]
3317

34-
def create_suite():
35-
suite = unittest.TestSuite()
36-
if os.name != 'nt':
37-
suite.addTest(
38-
unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test))
39-
return suite
4018

41-
42-
def run_suite():
43-
unittest.TextTestRunner(verbosity=2).run(create_suite())
44-
45-
46-
if __name__ == "__main__":
47-
run_suite()
19+
@pytest.fixture
20+
def global_ns():
21+
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
22+
INIT_OPTIMIZER = True
23+
config = autoconfig.cxx_parsers_cfg.config.clone()
24+
config.castxml_epic_version = 1
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_complex_types():
33+
"""
34+
This test tests presence of complex long double, float within
35+
FUNDAMENTAL_TYPES map
36+
"""
37+
pass
38+
# TODO: write test

0 commit comments

Comments
 (0)