Skip to content

Commit 705230e

Browse files
authored
Merge pull request #206 from CastXML/refactor2
tests: refactor / simplify
2 parents 056799e + 5528f12 commit 705230e

11 files changed

+432
-562
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 & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,20 @@
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):
13+
def test_castxml_epic_version_check():
14+
"""
15+
Test using a forbidden value for the castxml epic version.
1416
15-
def test_castxml_epic_version_check(self):
16-
"""
17-
Test using a forbidden value for the castxml epic version.
17+
"""
1818

19-
"""
20-
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()
19+
config = autoconfig.cxx_parsers_cfg.config.clone()
20+
config.castxml_epic_version = 2
21+
with pytest.raises(RuntimeError):
22+
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: 88 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -3,98 +3,97 @@
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
16-
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
22-
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")
28-
29-
def setUp(self):
30-
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
37-
38-
self.global_ns = Test.global_ns
39-
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")
48-
49-
self.assertIn("comment", dir(tnamespace))
50-
self._check_comment_content(["//! Namespace Comment",
51-
"//! Across multiple lines"],
52-
tnamespace.comment)
53-
54-
tenumeration = tnamespace.enumeration("com_enum")
55-
self.assertIn("comment", dir(tenumeration))
56-
self._check_comment_content(['/// Outside Class enum comment'],
57-
tenumeration.comment)
58-
59-
tclass = tnamespace.class_("test")
60-
self.assertIn("comment", dir(tclass))
61-
self._check_comment_content(["/** class comment */"], tclass.comment)
62-
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)
67-
68-
tmethod = tclass.member_functions()[0]
69-
70-
self.assertIn("comment", dir(tmethod))
71-
self._check_comment_content(["/// cxx comment",
72-
"/// with multiple lines"],
73-
tmethod.comment)
74-
75-
tconstructor = tclass.constructors()[0]
76-
77-
self.assertIn("comment", dir(tconstructor))
78-
self._check_comment_content(["/** doc comment */"],
79-
tconstructor.comment)
80-
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()
14+
TEST_FILES = [
15+
"test_comments.hpp",
16+
]
17+
18+
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 _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")
37+
38+
39+
def test_comments(global_ns):
40+
"""
41+
Check the comment parsing
42+
"""
43+
tnamespace = global_ns.namespace("comment")
44+
45+
assert "comment" in dir(tnamespace)
46+
_check_comment_content(
47+
[
48+
"//! Namespace Comment",
49+
"//! Across multiple lines"
50+
],
51+
tnamespace.comment
52+
)
53+
54+
tenumeration = tnamespace.enumeration("com_enum")
55+
assert "comment" in dir(tenumeration)
56+
_check_comment_content(
57+
['/// Outside Class enum comment'],
58+
tenumeration.comment
59+
)
60+
61+
tclass = tnamespace.class_("test")
62+
assert "comment" in dir(tclass)
63+
_check_comment_content(
64+
["/** class comment */"],
65+
tclass.comment
66+
)
67+
68+
tcls_enumeration = tclass.enumeration("test_enum")
69+
assert "comment" in dir(tcls_enumeration)
70+
_check_comment_content(
71+
['/// inside class enum comment'],
72+
tcls_enumeration.comment
73+
)
74+
75+
tmethod = tclass.member_functions()[0]
76+
77+
assert "comment" in dir(tmethod)
78+
_check_comment_content(
79+
["/// cxx comment", "/// with multiple lines"],
80+
tmethod.comment
81+
)
82+
83+
tconstructor = tclass.constructors()[0]
84+
85+
assert "comment" in dir(tconstructor)
86+
_check_comment_content(
87+
["/** doc comment */"],
88+
tconstructor.comment
89+
)
90+
91+
for indx, cmt in enumerate(
92+
[
93+
'//! mutable field comment',
94+
"/// bit field comment"
95+
]
96+
):
97+
tvariable = tclass.variables()[indx]
98+
assert "comment" in dir(tvariable)
99+
_check_comment_content([cmt], tvariable.comment)

0 commit comments

Comments
 (0)