|
3 | 3 | # Distributed under the Boost Software License, Version 1.0.
|
4 | 4 | # See http://www.boost.org/LICENSE_1_0.txt
|
5 | 5 |
|
6 |
| -import unittest |
| 6 | +import pytest |
7 | 7 |
|
8 |
| -from . import parser_test_case |
| 8 | +from . import autoconfig |
9 | 9 |
|
10 | 10 | from pygccxml import parser
|
11 | 11 | from pygccxml import declarations
|
12 | 12 | from pygccxml.declarations import type_traits
|
13 | 13 |
|
14 | 14 |
|
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