Skip to content

Commit c5735a5

Browse files
committed
tests: refactor / simplify
1 parent 461dab4 commit c5735a5

22 files changed

+997
-1431
lines changed

tests/test_gccxml10184.py

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

6-
import unittest
7-
8-
from . import parser_test_case
6+
from . import autoconfig
97

108
from pygccxml import parser
119
from pygccxml import declarations
@@ -21,28 +19,9 @@ class A {
2119
"""
2220

2321

24-
class Test(parser_test_case.parser_test_case_t):
25-
26-
def __init__(self, *args):
27-
parser_test_case.parser_test_case_t.__init__(self, *args)
28-
29-
def test(self):
30-
decls = parser.parse_string(code, self.config)
31-
global_ns = declarations.get_global_namespace(decls)
32-
self.assertTrue(global_ns.variable('a').bits == 1)
33-
self.assertTrue(global_ns.variable('unused').bits == 31)
34-
35-
36-
def create_suite():
37-
suite = unittest.TestSuite()
38-
suite.addTest(
39-
unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test))
40-
return suite
41-
42-
43-
def run_suite():
44-
unittest.TextTestRunner(verbosity=2).run(create_suite())
45-
46-
47-
if __name__ == "__main__":
48-
run_suite()
22+
def test_gccxml_10184():
23+
config = autoconfig.cxx_parsers_cfg.config.clone()
24+
decls = parser.parse_string(code, config)
25+
global_ns = declarations.get_global_namespace(decls)
26+
assert global_ns.variable('a').bits == 1
27+
assert global_ns.variable('unused').bits == 31

tests/test_gccxml10185.py

+18-38
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
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
@@ -18,43 +18,23 @@
1818
"""
1919

2020

21-
class Test(parser_test_case.parser_test_case_t):
22-
23-
def __init__(self, *args):
24-
parser_test_case.parser_test_case_t.__init__(self, *args)
25-
26-
def test(self):
27-
"""
28-
The purpose of this test was to check if changes to GCCXML
29-
would lead to changes in the outputted xml file (Meaning
30-
the bug was fixed).
31-
32-
GCCXML wrongly outputted partial template specialization.
33-
CastXML does not have this bug. In this case we check if
34-
the template specialization can not be found; which is the
35-
expected/wanted behaviour.
36-
37-
https://github.com/CastXML/CastXML/issues/20
38-
39-
"""
40-
41-
decls = parser.parse_string(code, self.config)
42-
global_ns = declarations.get_global_namespace(decls)
43-
self.assertRaises(
44-
declarations.declaration_not_found_t,
45-
lambda: global_ns.class_('A<const char [N]>'))
46-
47-
48-
def create_suite():
49-
suite = unittest.TestSuite()
50-
suite.addTest(
51-
unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test))
52-
return suite
21+
def test_partial_template():
22+
"""
23+
The purpose of this test was to check if changes to GCCXML
24+
would lead to changes in the outputted xml file (Meaning
25+
the bug was fixed).
5326
27+
GCCXML wrongly outputted partial template specialization.
28+
CastXML does not have this bug. In this case we check if
29+
the template specialization can not be found; which is the
30+
expected/wanted behaviour.
5431
55-
def run_suite():
56-
unittest.TextTestRunner(verbosity=2).run(create_suite())
32+
https://github.com/CastXML/CastXML/issues/20
5733
34+
"""
5835

59-
if __name__ == "__main__":
60-
run_suite()
36+
config = autoconfig.cxx_parsers_cfg.config.clone()
37+
decls = parser.parse_string(code, config)
38+
global_ns = declarations.get_global_namespace(decls)
39+
with pytest.raises(declarations.declaration_not_found_t):
40+
global_ns.class_('A<const char [N]>')

tests/test_has_binary_operator_traits.py

+27-54
Original file line numberDiff line numberDiff line change
@@ -3,72 +3,45 @@
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 = ["has_public_binary_operator_traits.hpp"]
1314

14-
class Test(parser_test_case.parser_test_case_t):
15-
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
16-
global_ns = None
17-
18-
def __init__(self, *args):
19-
parser_test_case.parser_test_case_t.__init__(self, *args)
20-
self.header = 'has_public_binary_operator_traits.hpp'
21-
self.global_ns = None
22-
23-
def setUp(self):
24-
if not Test.global_ns:
25-
decls = parser.parse([self.header], self.config)
26-
Test.global_ns = declarations.get_global_namespace(decls)
27-
self.global_ns = Test.global_ns
2815

29-
def test_yes_equal(self):
30-
yes_ns = self.global_ns.namespace('yesequal')
31-
for typedef in yes_ns.typedefs():
32-
self.assertTrue(
33-
declarations.has_public_equal(typedef),
34-
"Class '%s' should have public operator==" %
35-
typedef.decl_string)
36-
37-
def test_no_equal(self):
38-
no_ns = self.global_ns.namespace('noequal')
39-
for typedef in no_ns.typedefs():
40-
self.assertTrue(
41-
not declarations.has_public_equal(typedef),
42-
"Class '%s' should not have public operator==" %
43-
typedef.decl_string)
16+
@pytest.fixture
17+
def global_ns():
18+
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
19+
config = autoconfig.cxx_parsers_cfg.config.clone()
20+
decls = parser.parse(TEST_FILES, config, COMPILATION_MODE)
21+
global_ns = declarations.get_global_namespace(decls)
22+
global_ns.init_optimizer()
23+
return global_ns
4424

45-
def test_yes_less(self):
46-
yes_ns = self.global_ns.namespace('yesless')
47-
for typedef in yes_ns.typedefs():
48-
self.assertTrue(
49-
declarations.has_public_less(typedef),
50-
"Class '%s' should have public operator<" %
51-
typedef.decl_string)
5225

53-
def test_no_less(self):
54-
no_ns = self.global_ns.namespace('noless')
55-
for typedef in no_ns.typedefs():
56-
self.assertTrue(
57-
not declarations.has_public_less(typedef),
58-
"Class '%s' should not have public operator<" %
59-
typedef.decl_string)
26+
def test_yes_equal(global_ns):
27+
yes_ns = global_ns.namespace('yesequal')
28+
for typedef in yes_ns.typedefs():
29+
assert declarations.has_public_equal(typedef) is True
6030

6131

62-
def create_suite():
63-
suite = unittest.TestSuite()
64-
suite.addTest(
65-
unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test))
66-
return suite
32+
def test_no_equal(global_ns):
33+
no_ns = global_ns.namespace('noequal')
34+
for typedef in no_ns.typedefs():
35+
assert declarations.has_public_equal(typedef) is False
6736

6837

69-
def run_suite():
70-
unittest.TextTestRunner(verbosity=2).run(create_suite())
38+
def test_yes_less(global_ns):
39+
yes_ns = global_ns.namespace('yesless')
40+
for typedef in yes_ns.typedefs():
41+
assert declarations.has_public_less(typedef)
7142

7243

73-
if __name__ == "__main__":
74-
run_suite()
44+
def test_no_less(global_ns):
45+
no_ns = global_ns.namespace('noless')
46+
for typedef in no_ns.typedefs():
47+
assert declarations.has_public_less(typedef) is False

tests/test_hash.py

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

6-
import sys
7-
import unittest
86
import inspect
97

108
from pygccxml import declarations
119

1210

13-
class Test(unittest.TestCase):
11+
def test_types_hashes():
12+
"""
13+
Test if all the type_t instances implement a hash method.
1414
15-
def test_types_hashes(self):
16-
"""
17-
Test if all the type_t instances implement a hash method.
15+
The hash is part of the public API, as there are multiple tools
16+
that rely on it to compare type_t instances.
1817
19-
The hash is part of the public API, as there are multiple tools
20-
that rely on it to compare type_t instances.
18+
The best way to test this is to instanciate dummy type_t objects
19+
for each class that subclasses type_t, and check that the hash of
20+
these objects is not None.
2121
22-
The best way to test this is to instanciate dummy type_t objects
23-
for each class that subclasses type_t, and check that the hash of
24-
these objects is not None.
22+
"""
2523

26-
"""
24+
members = inspect.getmembers(declarations, inspect.isclass)
25+
for member in members:
26+
member_type = member[1]
27+
is_type_t_subclass = issubclass(member_type, declarations.type_t)
28+
is_not_type_t = member_type != declarations.type_t
29+
if is_type_t_subclass and is_not_type_t:
30+
type_mockup = _create_type_t_mockup(member_type)
31+
assert hash(type_mockup) is not None
2732

28-
if sys.version_info[:2] == (2, 7):
29-
# _create_type_t_mockup calls inspect.signature, which does not
30-
# exist for legacy Python
31-
return
3233

33-
members = inspect.getmembers(declarations, inspect.isclass)
34-
for member in members:
35-
member_type = member[1]
36-
is_type_t_subclass = issubclass(member_type, declarations.type_t)
37-
is_not_type_t = member_type != declarations.type_t
38-
if is_type_t_subclass and is_not_type_t:
39-
type_mockup = _create_type_t_mockup(member_type)
40-
self.assertIsNotNone(hash(type_mockup))
34+
def test_declarations_hashes():
35+
"""
36+
Test if all the declaration_t instances implement a hash method.
4137
42-
def test_declarations_hashes(self):
43-
"""
44-
Test if all the declaration_t instances implement a hash method.
38+
The hash is part of the public API, as there are multiple tools
39+
that rely on it to compare declaration_t instances.
4540
46-
The hash is part of the public API, as there are multiple tools
47-
that rely on it to compare declaration_t instances.
41+
The best way to test this is to instanciate dummy declaration_t objects
42+
for each class that subclasses declaration_t, and check that the hash
43+
of these objects is not None.
4844
49-
The best way to test this is to instanciate dummy declaration_t objects
50-
for each class that subclasses declaration_t, and check that the hash
51-
of these objects is not None.
45+
"""
46+
members = inspect.getmembers(declarations, inspect.isclass)
47+
for member in members:
48+
member_type = member[1]
49+
if issubclass(member_type, declarations.declaration_t):
50+
assert hash(member_type()) is not None
5251

53-
"""
54-
members = inspect.getmembers(declarations, inspect.isclass)
55-
for member in members:
56-
member_type = member[1]
57-
if issubclass(member_type, declarations.declaration_t):
58-
self.assertIsNotNone(hash(member_type()))
5952

60-
def test_type_qualifiers_t_hash(self):
61-
"""
62-
Test if the type_qualifiers_t instance implements a hash method.
53+
def test_type_qualifiers_t_hash():
54+
"""
55+
Test if the type_qualifiers_t instance implements a hash method.
6356
64-
The hash is part of the public API, as there are multiple tools
65-
that rely on it to compare type_qualifiers_t instances.
57+
The hash is part of the public API, as there are multiple tools
58+
that rely on it to compare type_qualifiers_t instances.
6659
67-
"""
68-
self.assertIsNotNone(hash(declarations.type_qualifiers_t()))
60+
"""
61+
assert hash(declarations.type_qualifiers_t()) is not None
6962

7063

7164
def _create_type_t_mockup(member_type):
@@ -91,18 +84,3 @@ def __init__(self):
9184

9285
def build_decl_string(self, with_defaults=False):
9386
return self._decl_string
94-
95-
96-
def create_suite():
97-
suite = unittest.TestSuite()
98-
suite.addTest(
99-
unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test))
100-
return suite
101-
102-
103-
def run_suite():
104-
unittest.TextTestRunner(verbosity=2).run(create_suite())
105-
106-
107-
if __name__ == "__main__":
108-
run_suite()

0 commit comments

Comments
 (0)