Skip to content

Commit e285b91

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 e285b91

File tree

11 files changed

+487
-567
lines changed

11 files changed

+487
-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: 299 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,299 @@
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, ["some_exception_t", "other_exception_t"]
178+
)
179+
# from now there is no need to check exception specification
180+
181+
182+
@pytest.mark.parametrize(
183+
"global_ns",
184+
[
185+
"global_ns_fixture_all_at_once",
186+
"global_ns_fixture_file_by_file",
187+
],
188+
indirect=True,
189+
)
190+
def test_calldef_member_functions(global_ns, helpers):
191+
struct_calldefs = global_ns.class_("calldefs_t")
192+
193+
member_inline_call = struct_calldefs.member_function("member_inline_call")
194+
helpers._test_calldef_args(
195+
member_inline_call,
196+
[declarations.argument_t(name="i", decl_type=declarations.int_t())],
197+
)
198+
199+
member_const_call = struct_calldefs.member_function("member_const_call")
200+
assert member_const_call.has_const
201+
assert member_const_call.virtuality == declarations.VIRTUALITY_TYPES.NOT_VIRTUAL
202+
203+
member_virtual_call = struct_calldefs.member_function(name="member_virtual_call")
204+
assert member_virtual_call.virtuality == declarations.VIRTUALITY_TYPES.VIRTUAL
205+
206+
member_pure_virtual_call = struct_calldefs.member_function(
207+
"member_pure_virtual_call"
208+
)
209+
assert (
210+
member_pure_virtual_call.virtuality
211+
== declarations.VIRTUALITY_TYPES.PURE_VIRTUAL
212+
)
213+
214+
static_call = struct_calldefs.member_function("static_call")
215+
assert static_call.has_static is True
216+
217+
218+
@pytest.mark.parametrize(
219+
"global_ns",
220+
[
221+
"global_ns_fixture_all_at_once",
222+
"global_ns_fixture_file_by_file",
223+
],
224+
indirect=True,
225+
)
226+
def test_constructors_destructors(global_ns, helpers):
227+
struct_calldefs = global_ns.class_("calldefs_t")
228+
229+
destructor = struct_calldefs.calldef("~calldefs_t")
230+
helpers._test_calldef_args(destructor, [])
231+
helpers._test_calldef_return_type(destructor, None.__class__)
232+
233+
# well, now we have a few functions ( constructors ) with the same
234+
# name, there is no easy way to find the desired one. Well in my case
235+
# I have only 4 constructors
236+
# 1. from char
237+
# 2. from (int,double)
238+
# 3. default
239+
# 4. copy constructor
240+
constructor_found = struct_calldefs.constructors("calldefs_t")
241+
assert len(constructor_found) == 5
242+
assert (
243+
len(
244+
[
245+
constructor
246+
for constructor in constructor_found
247+
if declarations.is_copy_constructor(constructor)
248+
]
249+
)
250+
== 1
251+
)
252+
# there is nothing to check about constructors - I know the
253+
# implementation of parser.
254+
# In this case it doesn't different from any other function
255+
256+
c = struct_calldefs.constructor("calldefs_t", arg_types=["char"])
257+
assert c.explicit is True
258+
259+
arg_type = declarations.declarated_t(global_ns.class_("some_exception_t"))
260+
c = struct_calldefs.constructor("calldefs_t", arg_types=[arg_type])
261+
assert c.explicit is False
262+
263+
264+
@pytest.mark.parametrize(
265+
"global_ns",
266+
[
267+
"global_ns_fixture_all_at_once",
268+
"global_ns_fixture_file_by_file",
269+
],
270+
indirect=True,
271+
)
272+
def test_operator_symbol(global_ns):
273+
calldefs_operators = ["=", "=="]
274+
calldefs_cast_operators = ["char *", "double"]
275+
struct_calldefs = global_ns.class_("calldefs_t")
276+
assert struct_calldefs is not None
277+
for decl in struct_calldefs.declarations:
278+
if not isinstance(decl, declarations.operator_t):
279+
continue
280+
if not isinstance(decl, declarations.casting_operator_t):
281+
assert decl.symbol in calldefs_operators
282+
else:
283+
assert decl.return_type.decl_string in calldefs_cast_operators
284+
285+
286+
@pytest.mark.parametrize(
287+
"global_ns",
288+
[
289+
"global_ns_fixture_all_at_once",
290+
"global_ns_fixture_file_by_file",
291+
],
292+
indirect=True,
293+
)
294+
def test_ellipsis(global_ns):
295+
ns = global_ns.namespace("ellipsis_tester")
296+
do_smth = ns.member_function("do_smth")
297+
assert do_smth.has_ellipsis is True
298+
do_smth_else = ns.free_function("do_smth_else")
299+
assert do_smth_else.has_ellipsis is True
File renamed without changes.

0 commit comments

Comments
 (0)