Skip to content

Commit baa8db8

Browse files
committed
tests: refactor / simplify
1 parent 877c4de commit baa8db8

11 files changed

+887
-1184
lines changed

tests/test_type_traits.py

+391-460
Large diffs are not rendered by default.

tests/test_typedefs.py

+34-71
Original file line numberDiff line numberDiff line change
@@ -3,81 +3,44 @@
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
1210

1311

14-
class tester_src_t(parser_test_case.parser_test_case_t):
15-
# tester source reader
12+
def test_typedefs_src_reader():
1613
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
17-
18-
def __init__(self, *args):
19-
parser_test_case.parser_test_case_t.__init__(self, *args)
20-
self.header = 'typedefs_base.hpp'
21-
self.declarations = None
22-
23-
def setUp(self):
24-
if not self.declarations:
25-
self.declarations = parser.parse([self.header], self.config)
26-
self.global_ns = declarations.find_declaration(
27-
self.declarations,
28-
decl_type=declarations.namespace_t,
29-
name='::')
30-
self.global_ns.init_optimizer()
31-
32-
def test(self):
33-
item_cls = self.global_ns.class_(name='item_t')
34-
self.assertTrue(item_cls, "unable to find class 'item_t'")
35-
self.assertTrue(len(item_cls.aliases) == 1)
36-
self.assertTrue(item_cls.aliases[0].name == 'Item')
37-
38-
39-
class tester_prj_t(parser_test_case.parser_test_case_t):
40-
# tester source reader
14+
header = 'typedefs_base.hpp'
15+
config = autoconfig.cxx_parsers_cfg.config.clone()
16+
decls = parser.parse([header], config)
17+
global_ns = declarations.find_declaration(
18+
decls,
19+
decl_type=declarations.namespace_t,
20+
name='::')
21+
global_ns.init_optimizer()
22+
23+
item_cls = global_ns.class_(name='item_t')
24+
assert item_cls is not None
25+
assert len(item_cls.aliases) == 1
26+
assert item_cls.aliases[0].name == 'Item'
27+
28+
29+
def test_typedefs_source_reader():
4130
COMPILATION_MODE = parser.COMPILATION_MODE.FILE_BY_FILE
42-
43-
def __init__(self, *args):
44-
parser_test_case.parser_test_case_t.__init__(self, *args)
45-
self.declarations = None
46-
47-
def setUp(self):
48-
if not self.declarations:
49-
self.declarations = parser.parse(
50-
['typedefs1.hpp',
51-
'typedefs2.hpp'],
52-
self.config,
53-
self.COMPILATION_MODE)
54-
55-
def test(self):
56-
item_cls = declarations.find_declaration(
57-
self.declarations,
58-
decl_type=declarations.class_t,
59-
name='item_t')
60-
self.assertTrue(item_cls, "unable to find class 'item_t'")
61-
self.assertTrue(len(item_cls.aliases) == 3)
62-
expected_aliases = {'Item', 'Item1', 'Item2'}
63-
real_aliases = set([typedef.name for typedef in item_cls.aliases])
64-
self.assertTrue(real_aliases == expected_aliases)
65-
66-
67-
def create_suite():
68-
suite = unittest.TestSuite()
69-
suite.addTest(
70-
unittest.TestLoader().loadTestsFromTestCase(
71-
testCaseClass=tester_src_t))
72-
suite.addTest(
73-
unittest.TestLoader().loadTestsFromTestCase(
74-
testCaseClass=tester_prj_t))
75-
return suite
76-
77-
78-
def run_suite():
79-
unittest.TextTestRunner(verbosity=2).run(create_suite())
80-
81-
82-
if __name__ == "__main__":
83-
run_suite()
31+
config = autoconfig.cxx_parsers_cfg.config.clone()
32+
33+
decls = parser.parse(
34+
['typedefs1.hpp', 'typedefs2.hpp'],
35+
config,
36+
COMPILATION_MODE
37+
)
38+
item_cls = declarations.find_declaration(
39+
decls,
40+
decl_type=declarations.class_t,
41+
name='item_t')
42+
assert item_cls is not None
43+
assert len(item_cls.aliases) == 3
44+
expected_aliases = {'Item', 'Item1', 'Item2'}
45+
real_aliases = set([typedef.name for typedef in item_cls.aliases])
46+
assert real_aliases == expected_aliases

tests/test_unnamed_classes.py

+80-90
Original file line numberDiff line numberDiff line change
@@ -3,100 +3,90 @@
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
from pygccxml.declarations import type_traits
1313

1414

15-
class Test(parser_test_case.parser_test_case_t):
16-
17-
def __init__(self, *args):
18-
parser_test_case.parser_test_case_t.__init__(self, *args)
19-
self.header = 'unnamed_classes.hpp'
20-
self.global_ns = None
21-
22-
def setUp(self):
23-
if not self.global_ns:
24-
decls = parser.parse([self.header], self.config)
25-
self.global_ns = declarations.get_global_namespace(decls)
26-
self.global_ns.init_optimizer()
27-
28-
def validate_bitfields(self, parent, bitfields):
29-
for key in bitfields:
30-
var = parent.variable(key)
31-
self.assertEqual(var.bits, bitfields[key])
32-
33-
def do_union_test(self, union_name, bitfields):
34-
s2 = self.global_ns.class_('S2')
35-
self.assertFalse(declarations.is_union(s2))
36-
self.assertTrue(declarations.is_struct(s2))
37-
self.assertEqual(s2.parent.name, 'S1')
38-
self.assertFalse(declarations.is_union(s2.parent))
39-
40-
union = s2.variable(union_name)
41-
self.assertTrue(declarations.is_union(union.decl_type))
42-
self.assertFalse(declarations.is_struct(union.decl_type))
43-
44-
union_type = type_traits.remove_declarated(union.decl_type)
45-
self.validate_bitfields(union_type, bitfields)
46-
self.assertIsNotNone(union_type.variable('raw'))
47-
48-
def test_union_Flags(self):
49-
flags_bitfields = {
50-
'hasItemIdList': 1,
51-
'pointsToFileOrDir': 1,
52-
'hasDescription': 1,
53-
'hasRelativePath': 1,
54-
'hasWorkingDir': 1,
55-
'hasCmdLineArgs': 1,
56-
'hasCustomIcon': 1,
57-
'useWorkingDir': 1,
58-
'unused': 24,
59-
}
60-
self.do_union_test('flags', flags_bitfields)
61-
62-
def test_unnamed_unions(self):
63-
fileattribs_bitfields = {
64-
'isReadOnly': 1,
65-
'isHidden': 1,
66-
'isSystem': 1,
67-
'isVolumeLabel': 1,
68-
'isDir': 1,
69-
'isModified': 1,
70-
'isEncrypted': 1,
71-
'isNormal': 1,
72-
'isTemporary': 1,
73-
'isSparse': 1,
74-
'hasReparsePoint': 1,
75-
'isCompressed': 1,
76-
'isOffline': 1,
77-
'unused': 19,
78-
}
79-
self.do_union_test('fileattribs', fileattribs_bitfields)
80-
81-
def test_anonymous_unions(self):
82-
s3 = self.global_ns.class_('S3')
83-
self.assertEqual(s3.parent.name, 'S1')
84-
85-
s3_vars = ['anon_mem_c', 'anon_mem_i', 's3_mem', 's2']
86-
for var in s3_vars:
87-
self.assertFalse(declarations.is_union(s3.variable(var).decl_type))
88-
89-
90-
def create_suite():
91-
suite = unittest.TestSuite()
92-
suite.addTest(
93-
unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test))
94-
return suite
95-
96-
97-
def run_suite():
98-
unittest.TextTestRunner(verbosity=2).run(create_suite())
99-
100-
101-
if __name__ == "__main__":
102-
run_suite()
15+
TEST_FILES = [
16+
"unnamed_classes.hpp",
17+
]
18+
19+
@pytest.fixture
20+
def global_ns():
21+
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
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+
global_ns.init_optimizer()
26+
return global_ns
27+
28+
29+
def validate_bitfields(parent, bitfields):
30+
for key in bitfields:
31+
var = parent.variable(key)
32+
assert var.bits == bitfields[key]
33+
34+
35+
def do_union_test(global_ns, union_name, bitfields):
36+
s2 = global_ns.class_('S2')
37+
assert declarations.is_union(s2) is False
38+
assert declarations.is_struct(s2) is True
39+
assert s2.parent.name == 'S1'
40+
assert declarations.is_union(s2.parent) is False
41+
42+
union = s2.variable(union_name)
43+
assert declarations.is_union(union.decl_type) is True
44+
assert declarations.is_struct(union.decl_type) is False
45+
46+
union_type = type_traits.remove_declarated(union.decl_type)
47+
validate_bitfields(union_type, bitfields)
48+
assert union_type.variable('raw') is not None
49+
50+
51+
def test_union_Flags(global_ns):
52+
flags_bitfields = {
53+
'hasItemIdList': 1,
54+
'pointsToFileOrDir': 1,
55+
'hasDescription': 1,
56+
'hasRelativePath': 1,
57+
'hasWorkingDir': 1,
58+
'hasCmdLineArgs': 1,
59+
'hasCustomIcon': 1,
60+
'useWorkingDir': 1,
61+
'unused': 24,
62+
}
63+
do_union_test(global_ns, 'flags', flags_bitfields)
64+
65+
66+
def test_unnamed_unions(global_ns):
67+
fileattribs_bitfields = {
68+
'isReadOnly': 1,
69+
'isHidden': 1,
70+
'isSystem': 1,
71+
'isVolumeLabel': 1,
72+
'isDir': 1,
73+
'isModified': 1,
74+
'isEncrypted': 1,
75+
'isNormal': 1,
76+
'isTemporary': 1,
77+
'isSparse': 1,
78+
'hasReparsePoint': 1,
79+
'isCompressed': 1,
80+
'isOffline': 1,
81+
'unused': 19,
82+
}
83+
do_union_test(global_ns, 'fileattribs', fileattribs_bitfields)
84+
85+
86+
def test_anonymous_unions(global_ns):
87+
s3 = global_ns.class_('S3')
88+
assert s3.parent.name == 'S1'
89+
90+
s3_vars = ['anon_mem_c', 'anon_mem_i', 's3_mem', 's2']
91+
for var in s3_vars:
92+
assert declarations.is_union(s3.variable(var).decl_type) is False

0 commit comments

Comments
 (0)