|
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 | + |
| 20 | +@pytest.fixture |
| 21 | +def global_ns(): |
| 22 | + COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE |
| 23 | + config = autoconfig.cxx_parsers_cfg.config.clone() |
| 24 | + decls = parser.parse(TEST_FILES, config, COMPILATION_MODE) |
| 25 | + global_ns = declarations.get_global_namespace(decls) |
| 26 | + global_ns.init_optimizer() |
| 27 | + return global_ns |
| 28 | + |
| 29 | + |
| 30 | +def validate_bitfields(parent, bitfields): |
| 31 | + for key in bitfields: |
| 32 | + var = parent.variable(key) |
| 33 | + assert var.bits == bitfields[key] |
| 34 | + |
| 35 | + |
| 36 | +def do_union_test(global_ns, union_name, bitfields): |
| 37 | + s2 = global_ns.class_('S2') |
| 38 | + assert declarations.is_union(s2) is False |
| 39 | + assert declarations.is_struct(s2) is True |
| 40 | + assert s2.parent.name == 'S1' |
| 41 | + assert declarations.is_union(s2.parent) is False |
| 42 | + |
| 43 | + union = s2.variable(union_name) |
| 44 | + assert declarations.is_union(union.decl_type) is True |
| 45 | + assert declarations.is_struct(union.decl_type) is False |
| 46 | + |
| 47 | + union_type = type_traits.remove_declarated(union.decl_type) |
| 48 | + validate_bitfields(union_type, bitfields) |
| 49 | + assert union_type.variable('raw') is not None |
| 50 | + |
| 51 | + |
| 52 | +def test_union_Flags(global_ns): |
| 53 | + flags_bitfields = { |
| 54 | + 'hasItemIdList': 1, |
| 55 | + 'pointsToFileOrDir': 1, |
| 56 | + 'hasDescription': 1, |
| 57 | + 'hasRelativePath': 1, |
| 58 | + 'hasWorkingDir': 1, |
| 59 | + 'hasCmdLineArgs': 1, |
| 60 | + 'hasCustomIcon': 1, |
| 61 | + 'useWorkingDir': 1, |
| 62 | + 'unused': 24, |
| 63 | + } |
| 64 | + do_union_test(global_ns, 'flags', flags_bitfields) |
| 65 | + |
| 66 | + |
| 67 | +def test_unnamed_unions(global_ns): |
| 68 | + fileattribs_bitfields = { |
| 69 | + 'isReadOnly': 1, |
| 70 | + 'isHidden': 1, |
| 71 | + 'isSystem': 1, |
| 72 | + 'isVolumeLabel': 1, |
| 73 | + 'isDir': 1, |
| 74 | + 'isModified': 1, |
| 75 | + 'isEncrypted': 1, |
| 76 | + 'isNormal': 1, |
| 77 | + 'isTemporary': 1, |
| 78 | + 'isSparse': 1, |
| 79 | + 'hasReparsePoint': 1, |
| 80 | + 'isCompressed': 1, |
| 81 | + 'isOffline': 1, |
| 82 | + 'unused': 19, |
| 83 | + } |
| 84 | + do_union_test(global_ns, 'fileattribs', fileattribs_bitfields) |
| 85 | + |
| 86 | + |
| 87 | +def test_anonymous_unions(global_ns): |
| 88 | + s3 = global_ns.class_('S3') |
| 89 | + assert s3.parent.name == 'S1' |
| 90 | + |
| 91 | + s3_vars = ['anon_mem_c', 'anon_mem_i', 's3_mem', 's2'] |
| 92 | + for var in s3_vars: |
| 93 | + assert declarations.is_union(s3.variable(var).decl_type) is False |
0 commit comments