Skip to content

Commit 274ad68

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 274ad68

11 files changed

+502
-567
lines changed

src/pygccxml/declarations/container_traits.py

+2-2
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

+3-3
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

+25
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

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

0 commit comments

Comments
 (0)