Skip to content

Commit 365c504

Browse files
committed
Test: move another batch of tests to to pytest
Make also sure some type qualifiers attributes are booleans Fix some container traits
1 parent 2e11972 commit 365c504

File tree

11 files changed

+444
-567
lines changed

11 files changed

+444
-567
lines changed

src/pygccxml/declarations/container_traits.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,14 +647,14 @@ def remove_defaults(self, type_or_string):
647647

648648
unordered_set_traits = container_traits_impl_t(
649649
'unordered_set',
650-
1,
650+
0,
651651
'value_type',
652652
'erase_hash_allocator',
653653
unordered_maps_and_sets=True)
654654

655655
unordered_multiset_traits = container_traits_impl_t(
656656
'unordered_multiset',
657-
1,
657+
0,
658658
'value_type',
659659
'erase_hash_allocator',
660660
unordered_maps_and_sets=True)

src/pygccxml/parser/scanner.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -630,9 +630,9 @@ def __read_typedef(self, attrs):
630630

631631
def __read_variable(self, attrs):
632632
type_qualifiers = declarations.type_qualifiers_t()
633-
type_qualifiers.has_mutable = attrs.get(XML_AN_MUTABLE, False)
634-
type_qualifiers.has_static = attrs.get(XML_AN_STATIC, False)
635-
type_qualifiers.has_extern = attrs.get(XML_AN_EXTERN, False)
633+
type_qualifiers.has_mutable = bool(attrs.get(XML_AN_MUTABLE, False))
634+
type_qualifiers.has_static = bool(attrs.get(XML_AN_STATIC, False))
635+
type_qualifiers.has_extern = bool(attrs.get(XML_AN_EXTERN, False))
636636
bits = attrs.get(XML_AN_BITS)
637637
if bits:
638638
bits = int(bits)

tests/conftest.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,31 @@ def _test_type_composition(type_, expected_compound, expected_base):
1212
assert isinstance(type_, expected_compound)
1313
assert isinstance(type_.base, expected_base)
1414

15+
@staticmethod
16+
def _test_calldef_args(calldef, expected_args):
17+
assert len(calldef.arguments) == len(expected_args)
18+
19+
for i, expected_arg in enumerate(expected_args):
20+
arg = calldef.arguments[i]
21+
assert arg == expected_arg
22+
23+
@staticmethod
24+
def _test_calldef_return_type(calldef, expected_type):
25+
assert isinstance(calldef.return_type, expected_type)
26+
27+
@staticmethod
28+
def _test_calldef_exceptions(global_ns, calldef, exceptions):
29+
# exceptions is list of classes names
30+
exception_decls = []
31+
for name in exceptions:
32+
exception_decl = global_ns.class_(name)
33+
assert exception_decl is not None
34+
exception_decls.append(exception_decl)
35+
exception_decls.sort()
36+
assert len(calldef.exceptions) == len(exception_decls)
37+
exceptions_indeed = sorted(calldef.exceptions[:])
38+
assert exception_decls == exceptions_indeed
39+
1540

1641
@pytest.fixture
1742
def helpers():

tests/test_declarations.py

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
# Copyright 2014-2017 Insight Software Consortium.
2+
# Copyright 2004-2009 Roman Yakovenko.
3+
# Distributed under the Boost Software License, Version 1.0.
4+
# See http://www.boost.org/LICENSE_1_0.txt
5+
6+
import pytest
7+
8+
from . import autoconfig
9+
10+
from pygccxml import parser
11+
from pygccxml import declarations
12+
13+
TEST_FILES = [
14+
'declarations_enums.hpp',
15+
'declarations_variables.hpp',
16+
'declarations_calldef.hpp'
17+
]
18+
19+
@pytest.fixture
20+
def global_ns_fixture_all_at_once():
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+
return global_ns
26+
27+
@pytest.fixture
28+
def global_ns_fixture_file_by_file():
29+
COMPILATION_MODE = parser.COMPILATION_MODE.FILE_BY_FILE
30+
config = autoconfig.cxx_parsers_cfg.config.clone()
31+
decls = parser.parse(TEST_FILES, config, COMPILATION_MODE)
32+
global_ns = declarations.get_global_namespace(decls)
33+
return global_ns
34+
35+
@pytest.fixture
36+
def global_ns(request):
37+
return request.getfixturevalue(request.param)
38+
39+
@pytest.mark.parametrize(
40+
"global_ns",
41+
[
42+
"global_ns_fixture_all_at_once",
43+
"global_ns_fixture_file_by_file",
44+
],
45+
indirect=True,
46+
)
47+
def test_enumeration_t(global_ns):
48+
enum = global_ns.enumeration('ENumbers')
49+
expected_values = list(
50+
zip(['e%d' % index for index in range(10)],
51+
[index for index in range(10)]))
52+
assert expected_values == enum.values
53+
54+
def test_namespace():
55+
pass # tested in core_tester
56+
57+
def test_types():
58+
pass # tested in core_tester
59+
60+
@pytest.mark.parametrize(
61+
"global_ns",
62+
[
63+
"global_ns_fixture_all_at_once",
64+
"global_ns_fixture_file_by_file",
65+
],
66+
indirect=True,
67+
)
68+
def test_variables(global_ns, helpers):
69+
global_ns.namespace('variables')
70+
initialized = global_ns.variable(name='initialized')
71+
72+
expected_value = '10122004'
73+
assert initialized.value == expected_value
74+
helpers._test_type_composition(
75+
initialized.decl_type,
76+
declarations.const_t,
77+
declarations.long_unsigned_int_t)
78+
79+
m_mutable = global_ns.variable(name="m_mutable")
80+
assert m_mutable.type_qualifiers.has_static is False
81+
assert m_mutable.type_qualifiers.has_mutable is True
82+
83+
# External static variable
84+
extern_var = global_ns.variable(name="extern_var")
85+
assert extern_var.type_qualifiers.has_extern is True
86+
assert extern_var.type_qualifiers.has_static is False
87+
assert extern_var.type_qualifiers.has_mutable is False
88+
89+
# Static variable
90+
static_var = global_ns.variable(name="static_var")
91+
assert static_var.type_qualifiers.has_static is True
92+
assert static_var.type_qualifiers.has_extern is False
93+
assert static_var.type_qualifiers.has_mutable is False
94+
95+
ssv_static_var = global_ns.variable(name="ssv_static_var")
96+
assert ssv_static_var.type_qualifiers.has_static is True
97+
assert ssv_static_var.type_qualifiers.has_extern is False
98+
assert ssv_static_var.type_qualifiers.has_mutable is False
99+
100+
ssv_static_var_value = global_ns.variable(
101+
name="ssv_static_var_value")
102+
assert ssv_static_var_value.type_qualifiers.has_static is True
103+
assert ssv_static_var_value.type_qualifiers.has_extern is False
104+
assert ssv_static_var_value.type_qualifiers.has_mutable is False
105+
106+
@pytest.mark.parametrize(
107+
"global_ns",
108+
[
109+
"global_ns_fixture_all_at_once",
110+
"global_ns_fixture_file_by_file",
111+
],
112+
indirect=True,
113+
)
114+
def test_calldef_free_functions(global_ns, helpers):
115+
ns = global_ns.namespace('calldef')
116+
117+
no_return_no_args = ns.free_function('no_return_no_args')
118+
119+
helpers._test_calldef_return_type(no_return_no_args, declarations.void_t)
120+
assert no_return_no_args.has_extern is False
121+
122+
# Static_call is explicetely defined as extern, this works with gccxml
123+
# and castxml.
124+
static_call = ns.free_function('static_call')
125+
assert static_call is not None
126+
127+
return_no_args = ns.free_function('return_no_args')
128+
helpers._test_calldef_return_type(return_no_args, declarations.int_t)
129+
# from now there is no need to check return type.
130+
no_return_1_arg = ns.free_function(name='no_return_1_arg')
131+
assert no_return_1_arg is not None
132+
assert no_return_1_arg.arguments[0].name in ['arg', 'arg0']
133+
helpers._test_calldef_args(
134+
no_return_1_arg,
135+
[declarations.argument_t(
136+
name=no_return_1_arg.arguments[0].name,
137+
decl_type=declarations.int_t())])
138+
139+
return_default_args = ns.free_function('return_default_args')
140+
assert return_default_args.arguments[0].name in ['arg', 'arg0']
141+
assert return_default_args.arguments[1].name in ['arg1', 'flag']
142+
helpers._test_calldef_args(
143+
return_default_args,
144+
[declarations.argument_t(
145+
name=return_default_args.arguments[0].name,
146+
decl_type=declarations.int_t(),
147+
default_value='1'),
148+
declarations.argument_t(
149+
name=return_default_args.arguments[1].name,
150+
decl_type=declarations.bool_t(),
151+
default_value='false')])
152+
helpers._test_calldef_exceptions(global_ns, return_default_args, [])
153+
154+
calldef_with_throw = ns.free_function('calldef_with_throw')
155+
assert calldef_with_throw is not None
156+
helpers._test_calldef_exceptions(
157+
global_ns,
158+
calldef_with_throw, [
159+
'some_exception_t', 'other_exception_t'])
160+
# from now there is no need to check exception specification
161+
162+
@pytest.mark.parametrize(
163+
"global_ns",
164+
[
165+
"global_ns_fixture_all_at_once",
166+
"global_ns_fixture_file_by_file",
167+
],
168+
indirect=True,
169+
)
170+
def test_calldef_member_functions(global_ns, helpers):
171+
struct_calldefs = global_ns.class_('calldefs_t')
172+
173+
member_inline_call = struct_calldefs.member_function(
174+
'member_inline_call')
175+
helpers._test_calldef_args(
176+
member_inline_call, [
177+
declarations.argument_t(
178+
name='i', decl_type=declarations.int_t())])
179+
180+
member_const_call = struct_calldefs.member_function(
181+
'member_const_call')
182+
assert member_const_call.has_const
183+
assert member_const_call.virtuality == declarations.VIRTUALITY_TYPES.NOT_VIRTUAL
184+
185+
member_virtual_call = struct_calldefs.member_function(
186+
name='member_virtual_call')
187+
assert member_virtual_call.virtuality == declarations.VIRTUALITY_TYPES.VIRTUAL
188+
189+
member_pure_virtual_call = struct_calldefs.member_function(
190+
'member_pure_virtual_call')
191+
assert member_pure_virtual_call.virtuality == declarations.VIRTUALITY_TYPES.PURE_VIRTUAL
192+
193+
static_call = struct_calldefs.member_function('static_call')
194+
assert static_call.has_static is True
195+
196+
@pytest.mark.parametrize(
197+
"global_ns",
198+
[
199+
"global_ns_fixture_all_at_once",
200+
"global_ns_fixture_file_by_file",
201+
],
202+
indirect=True,
203+
)
204+
def test_constructors_destructors(global_ns, helpers):
205+
struct_calldefs = global_ns.class_('calldefs_t')
206+
207+
destructor = struct_calldefs.calldef('~calldefs_t')
208+
helpers._test_calldef_args(destructor, [])
209+
helpers._test_calldef_return_type(destructor, None.__class__)
210+
211+
# well, now we have a few functions ( constructors ) with the same
212+
# name, there is no easy way to find the desired one. Well in my case
213+
# I have only 4 constructors
214+
# 1. from char
215+
# 2. from (int,double)
216+
# 3. default
217+
# 4. copy constructor
218+
constructor_found = struct_calldefs.constructors('calldefs_t')
219+
assert len(constructor_found) == 5
220+
assert len(
221+
[constructor for constructor in constructor_found if
222+
declarations.is_copy_constructor(constructor)]) == 1
223+
# there is nothing to check about constructors - I know the
224+
# implementation of parser.
225+
# In this case it doesn't different from any other function
226+
227+
c = struct_calldefs.constructor('calldefs_t', arg_types=['char'])
228+
assert c.explicit is True
229+
230+
arg_type = declarations.declarated_t(
231+
global_ns.class_('some_exception_t'))
232+
c = struct_calldefs.constructor('calldefs_t', arg_types=[arg_type])
233+
assert c.explicit is False
234+
235+
@pytest.mark.parametrize(
236+
"global_ns",
237+
[
238+
"global_ns_fixture_all_at_once",
239+
"global_ns_fixture_file_by_file",
240+
],
241+
indirect=True,
242+
)
243+
def test_operator_symbol(global_ns):
244+
calldefs_operators = ['=', '==']
245+
calldefs_cast_operators = ['char *', 'double']
246+
struct_calldefs = global_ns.class_('calldefs_t')
247+
assert struct_calldefs is not None
248+
for decl in struct_calldefs.declarations:
249+
if not isinstance(decl, declarations.operator_t):
250+
continue
251+
if not isinstance(decl, declarations.casting_operator_t):
252+
assert decl.symbol in calldefs_operators
253+
else:
254+
assert decl.return_type.decl_string in calldefs_cast_operators
255+
256+
@pytest.mark.parametrize(
257+
"global_ns",
258+
[
259+
"global_ns_fixture_all_at_once",
260+
"global_ns_fixture_file_by_file",
261+
],
262+
indirect=True,
263+
)
264+
def test_ellipsis(global_ns):
265+
ns = global_ns.namespace('ellipsis_tester')
266+
do_smth = ns.member_function('do_smth')
267+
assert do_smth.has_ellipsis is True
268+
do_smth_else = ns.free_function('do_smth_else')
269+
assert do_smth_else.has_ellipsis is True
File renamed without changes.

0 commit comments

Comments
 (0)