Skip to content

Commit 2ca1011

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

File tree

11 files changed

+466
-567
lines changed

11 files changed

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

0 commit comments

Comments
 (0)