Skip to content

Commit

Permalink
macos: fix tests
Browse files Browse the repository at this point in the history
Attempt to fix test by normalizing spaces in templates
  • Loading branch information
iMichka committed Nov 14, 2024
1 parent ff5bfd2 commit f32b2da
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 70 deletions.
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ docs = [
examples = [
"notebook",
]
[tool.pytest.ini_options]
pythonpath = [
"src"
]
62 changes: 31 additions & 31 deletions src/pygccxml/declarations/container_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def erase_allocator(self, cls_name, default_allocator='std::allocator'):
return
value_type = c_args[0]
tmpl = string.Template(
"$container< $value_type, $allocator<$value_type> >")
"$container<$value_type, $allocator<$value_type>>")
tmpl = tmpl.substitute(
container=c_name,
value_type=value_type,
Expand Down Expand Up @@ -159,8 +159,8 @@ def erase_compare_allocator(
return
value_type = c_args[0]
tmpl = string.Template(
"$container< $value_type, $compare<$value_type>, " +
"$allocator<$value_type> >")
"$container<$value_type, $compare<$value_type>, " +
"$allocator<$value_type>>")
tmpl = tmpl.substitute(
container=c_name,
value_type=value_type,
Expand All @@ -184,14 +184,14 @@ def erase_map_compare_allocator(
mapped_type = c_args[1]
tmpls = [
string.Template(
"$container< $key_type, $mapped_type, $compare<$key_type>, " +
"$allocator< std::pair< const $key_type, $mapped_type> > >"),
"$container<$key_type, $mapped_type, $compare<$key_type>, " +
"$allocator<std::pair<const $key_type, $mapped_type>>>"),
string.Template(
"$container< $key_type, $mapped_type, $compare<$key_type>, " +
"$allocator< std::pair< $key_type const, $mapped_type> > >"),
"$container<$key_type, $mapped_type, $compare<$key_type>, " +
"$allocator<std::pair<$key_type const, $mapped_type>>>"),
string.Template(
"$container< $key_type, $mapped_type, $compare<$key_type>, " +
"$allocator< std::pair< $key_type, $mapped_type> > >")]
"$container<$key_type, $mapped_type, $compare<$key_type>, " +
"$allocator<std::pair<$key_type, $mapped_type>>>")]
for tmpl in tmpls:
tmpl = tmpl.substitute(
container=c_name,
Expand All @@ -218,13 +218,13 @@ def erase_hash_allocator(self, cls_name):
if len(c_args) == 3:
default_hash = 'hash_compare'
tmpl = (
"$container< $value_type, $hash<$value_type, " +
"$less<$value_type> >, $allocator<$value_type> >")
"$container<$value_type, $hash<$value_type, " +
"$less<$value_type>>, $allocator<$value_type>>")
elif len(c_args) == 4:
default_hash = 'hash'
tmpl = (
"$container< $value_type, $hash<$value_type >, " +
"$equal_to<$value_type >, $allocator<$value_type> >")
"$container<$value_type, $hash<$value_type>, " +
"$equal_to<$value_type>, $allocator<$value_type>>")
else:
return

Expand Down Expand Up @@ -263,14 +263,14 @@ def erase_hashmap_compare_allocator(self, cls_name):
if len(c_args) == 4:
default_hash = 'hash_compare'
tmpl = string.Template(
"$container< $key_type, $mapped_type, " +
"$hash<$key_type, $less<$key_type> >, " +
"$allocator< std::pair< const $key_type, $mapped_type> > >")
"$container<$key_type, $mapped_type, " +
"$hash<$key_type, $less<$key_type>>, " +
"$allocator<std::pair<const $key_type, $mapped_type>>>")
if key_type.startswith('const ') or key_type.endswith(' const'):
tmpl = string.Template(
"$container< $key_type, $mapped_type, $hash<$key_type, " +
"$less<$key_type> >, $allocator< std::pair< $key_type, " +
"$mapped_type> > >")
"$container<$key_type, $mapped_type, $hash<$key_type, " +
"$less<$key_type>>, $allocator<std::pair<$key_type, " +
"$mapped_type>>>")
elif len(c_args) == 5:
default_hash = 'hash'
if self.unordered_maps_and_sets:
Expand All @@ -279,31 +279,31 @@ def erase_hashmap_compare_allocator(self, cls_name):
"$hash<$key_type>, " +
"$equal_to<$key_type>, " +
"$allocator<std::pair<const$key_type, " +
"$mapped_type> > >")
"$mapped_type>>>")
if key_type.startswith('const ') or \
key_type.endswith(' const'):
tmpl = string.Template(
"$container<$key_type, $mapped_type, " +
"$hash<$key_type >, " +
"$equal_to<$key_type >, " +
"$hash<$key_type>, " +
"$equal_to<$key_type>, " +
"$allocator<std::pair<$key_type, " +
"$mapped_type> > >")
"$mapped_type>>>")
else:
tmpl = string.Template(
"$container< $key_type, $mapped_type, "
"$hash<$key_type >, " +
"$container<$key_type, $mapped_type, "
"$hash<$key_type>, " +
"$equal_to<$key_type>, "
"$allocator< $mapped_type> >")
"$allocator<$mapped_type>>")
if key_type.startswith('const ') or \
key_type.endswith(' const'):
# TODO: this template is the same than above.
# Make sure why this was needed and if this is
# tested. There may be a const missing somewhere.
tmpl = string.Template(
"$container< $key_type, $mapped_type, " +
"$hash<$key_type >, " +
"$container<$key_type, $mapped_type, " +
"$hash<$key_type>, " +
"$equal_to<$key_type>, " +
"$allocator< $mapped_type > >")
"$allocator<$mapped_type>>")
else:
return

Expand Down Expand Up @@ -512,12 +512,12 @@ def remove_defaults(self, type_or_string):
For example:
.. code-block:: c++
std::vector< int, std::allocator< int > >
std::vector<int, std::allocator<int>>
will become:
.. code-block:: c++
std::vector< int >
std::vector<int>
"""

Expand Down
6 changes: 3 additions & 3 deletions src/pygccxml/declarations/pattern_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,11 @@ def join(self, name, args, arg_separator=None):
args = [_f for _f in args if _f]

if not args:
args_str = ' '
args_str = ''
elif len(args) == 1:
args_str = ' ' + args[0] + ' '
args_str = '' + args[0] + ''
else:
args_str = ' ' + arg_separator.join(args) + ' '
args_str = '' + arg_separator.join(args) + ''

return ''.join([name, self.__begin, args_str, self.__end])

Expand Down
13 changes: 13 additions & 0 deletions src/pygccxml/parser/patcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,16 @@ def update_unnamed_class(decls):
if referent.name or not isinstance(referent, declarations.class_t):
continue
referent.name = decl.name


def remove_spaces_from_template_names(decls):
"""
Cleanup names that can have different spaces at different places.
This depends on the compiler / platform, so just remove spaces.
Examples:
before hash<std::vector<int> >
after hash<std::vector<int>>
"""
for decl in decls:
if isinstance(decl, declarations.declaration_t):
decl.name = decl.name.replace(" >", ">").replace("< ", "<")
1 change: 1 addition & 0 deletions src/pygccxml/parser/source_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ def __parse_xml_file(self, xml_file):
patcher.update_unnamed_class(decls.values())
patcher.fix_calldef_decls(
scanner_.calldefs(), scanner_.enums(), self.__cxx_std)
patcher.remove_spaces_from_template_names(decls.values())

decls = [inst for inst in iter(decls.values()) if self.__check(inst)]
return decls, list(files.values())
Expand Down
72 changes: 36 additions & 36 deletions tests/test_remove_template_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,149 +30,149 @@ def global_ns():
def test_vector(global_ns):
v_int = global_ns.typedef('v_int')
v_traits = declarations.vector_traits
assert 'vector< int >' == v_traits.remove_defaults(v_int)
assert 'vector<int>' == v_traits.remove_defaults(v_int)
v_string = global_ns.typedef('v_string')
assert 'vector< std::string >' == \
assert 'vector<std::string>' == \
v_traits.remove_defaults(v_string)
v_v_int = global_ns.typedef('v_v_int')
assert 'vector< std::vector< int > >' == \
assert 'vector<std::vector<int>>' == \
v_traits.remove_defaults(v_v_int)


def test_list(global_ns):
l_int = global_ns.typedef('l_int')
l_traits = declarations.list_traits
assert 'list< int >' == l_traits.remove_defaults(l_int)
assert 'list<int>' == l_traits.remove_defaults(l_int)
l_wstring = global_ns.typedef('l_wstring')
assert 'list< std::wstring >' == l_traits.remove_defaults(l_wstring)
assert 'list<std::wstring>' == l_traits.remove_defaults(l_wstring)


def test_deque(global_ns):
d_v_int = global_ns.typedef('d_v_int')
d_v_traits = declarations.deque_traits
assert 'deque< std::vector< int > >' == \
assert 'deque<std::vector<int>>' == \
d_v_traits.remove_defaults(d_v_int)
d_l_string = global_ns.typedef('d_l_string')
assert 'deque< std::list< std::string > >' == \
assert 'deque<std::list<std::string>>' == \
d_v_traits.remove_defaults(d_l_string)


def test_queue(global_ns):
q_int = global_ns.typedef('q_int')
q_traits = declarations.queue_traits
assert 'queue< int >' == q_traits.remove_defaults(q_int)
assert 'queue<int>' == q_traits.remove_defaults(q_int)
q_string = global_ns.typedef('q_string')
assert 'queue< std::string >' == q_traits.remove_defaults(q_string)
assert 'queue<std::string>' == q_traits.remove_defaults(q_string)


def test_priority_queue(global_ns):
pq_int = global_ns.typedef('pq_int')
pq_traits = declarations.priority_queue_traits
assert 'priority_queue< int >' == pq_traits.remove_defaults(pq_int)
assert 'priority_queue<int>' == pq_traits.remove_defaults(pq_int)
pq_string = global_ns.typedef('pq_string')
assert 'priority_queue< std::string >' == \
assert 'priority_queue<std::string>' == \
pq_traits.remove_defaults(pq_string)


def test_set(global_ns):
s_v_int = global_ns.typedef('s_v_int')
assert 'set< std::vector< int > >' == \
assert 'set<std::vector<int>>' == \
declarations.set_traits.remove_defaults(s_v_int)
s_string = global_ns.typedef('s_string')
assert 'set< std::string >' == \
assert 'set<std::string>' == \
declarations.set_traits.remove_defaults(s_string)


def test_multiset(global_ns):
ms_v_int = global_ns.typedef('ms_v_int')
ms_v_traits = declarations.multiset_traits
assert 'multiset< std::vector< int > >' == \
assert 'multiset<std::vector<int>>' == \
ms_v_traits.remove_defaults(ms_v_int)
ms_string = global_ns.typedef('ms_string')
assert 'multiset< std::string >' == \
assert 'multiset<std::string>' == \
ms_v_traits.remove_defaults(ms_string)


def test_map(global_ns):
m_i2d = global_ns.typedef('m_i2d')
assert 'map< int, double >' == \
assert 'map<int, double>' == \
declarations.map_traits.remove_defaults(m_i2d)
m_wstr2d = global_ns.typedef('m_wstr2d')
assert 'map< std::wstring, double >' == \
assert 'map<std::wstring, double>' == \
declarations.map_traits.remove_defaults(m_wstr2d)
m_v_i2m_wstr2d = global_ns.typedef('m_v_i2m_wstr2d')
m = 'map< const std::vector< int >, std::map< std::wstring, double > >'
m = 'map<const std::vector<int>, std::map<std::wstring, double>>'
assert m == declarations.map_traits.remove_defaults(m_v_i2m_wstr2d)


def test_multimap(global_ns):
mm_i2d = global_ns.typedef('mm_i2d')
mm_traits = declarations.multimap_traits
assert 'multimap< int, double >' == mm_traits.remove_defaults(mm_i2d)
assert 'multimap<int, double>' == mm_traits.remove_defaults(mm_i2d)
mm_wstr2d = global_ns.typedef('mm_wstr2d')
assert 'multimap< const std::wstring, double >' == \
assert 'multimap<const std::wstring, double>' == \
mm_traits.remove_defaults(mm_wstr2d)
mm_v_i2mm_wstr2d = global_ns.typedef('mm_v_i2mm_wstr2d')
assert ('multimap< const std::vector< int >, ' +
'const std::multimap< const std::wstring, double > >') == \
assert ('multimap<const std::vector<int>, ' +
'const std::multimap<const std::wstring, double>>') == \
mm_traits.remove_defaults(mm_v_i2mm_wstr2d)


def test_hash_set(global_ns):
hs_v_int = global_ns.typedef('hs_v_int')
hs_traits = declarations.unordered_set_traits
name = 'unordered_set'
assert (name + '< std::vector< int > >') == \
assert (name + '<std::vector<int>>') == \
hs_traits.remove_defaults(hs_v_int), \
hs_traits.remove_defaults(hs_v_int)
hs_string = global_ns.typedef('hs_string')
assert (name + '< std::string >') == \
assert (name + '<std::string>') == \
hs_traits.remove_defaults(hs_string)


def test_hash_multiset(global_ns):
mhs_v_int = global_ns.typedef('mhs_v_int')
mhs_traits = declarations.unordered_multiset_traits
name = 'unordered_multiset'
assert (name + '< std::vector< int > >') == \
assert (name + '<std::vector<int>>') == \
mhs_traits.remove_defaults(mhs_v_int)
mhs_string = global_ns.typedef('mhs_string')
assert (name + '< std::string >') == \
assert (name + '<std::string>') == \
mhs_traits.remove_defaults(mhs_string)


def test_hash_map(global_ns):
hm_i2d = global_ns.typedef('hm_i2d')
hm_traits = declarations.unordered_map_traits
name = 'unordered_map'
assert (name + '< int, double >') == \
assert (name + '<int, double>') == \
hm_traits.remove_defaults(hm_i2d)
hm_wstr2d = global_ns.typedef('hm_wstr2d')
assert (name + '< std::wstring, double >') == \
assert (name + '<std::wstring, double>') == \
hm_traits.remove_defaults(hm_wstr2d)


def test_hash_multimap(global_ns):
hmm_i2d = global_ns.typedef('hmm_i2d')
hmm_traits = declarations.unordered_multimap_traits
name = 'unordered_multimap'
assert (name + '< int, double >') == \
assert (name + '<int, double>') == \
hmm_traits.remove_defaults(hmm_i2d)
hmm_wstr2d = global_ns.typedef('hmm_wstr2d')
assert (name + '< const std::wstring, double >') == \
assert (name + '<const std::wstring, double>') == \
hmm_traits.remove_defaults(hmm_wstr2d)

hmm_v_i2mm_wstr2d = global_ns.typedef('hmm_v_i2mm_wstr2d')

hmm_traits_value = hmm_traits.remove_defaults(hmm_v_i2mm_wstr2d)

possible_values = (
name + '< const std::vector< int >, ' +
'const __gnu_cxx::' + name + '< const std::wstring, double > >',
name + '< const std::vector< int >, ' +
name + '<const std::vector<int>, ' +
'const __gnu_cxx::' + name + '<const std::wstring, double>>',
name + '<const std::vector<int>, ' +
'const std::' + utils.get_tr1(hmm_traits_value) + name +
'< const std::wstring, double > >',
name + '< const std::vector< int >, ' +
'const stdext::' + name + '< const std::wstring, double > >')
'<const std::wstring, double>>',
name + '<const std::vector<int>, ' +
'const stdext::' + name + '<const std::wstring, double>>')

assert hmm_traits_value in possible_values, hmm_traits_value

0 comments on commit f32b2da

Please sign in to comment.