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 21, 2024
1 parent 5f6f235 commit 0874e3d
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 84 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
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
2 changes: 1 addition & 1 deletion tests/test_call_invocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_split_on_map():


def test_join_on_vector():
assert "vector( int, std::allocator(int) )" == \
assert "vector(int, std::allocator(int))" == \
declarations.call_invocation.join(
"vector", ("int", "std::allocator(int)"))

Expand Down
30 changes: 15 additions & 15 deletions tests/test_find_container_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,91 +51,91 @@ def test_find_traits(global_ns):
global_ns,
"v_int",
declarations.vector_traits,
"vector< int >"
"vector<int>"
)
__cmp_traits(
global_ns,
"l_int",
declarations.list_traits,
"list< int >"
"list<int>"
)
__cmp_traits(
global_ns, "d_v_int",
declarations.deque_traits,
"deque< std::vector< int > >"
"deque<std::vector<int>>"
)
__cmp_traits(
global_ns, "q_int",
declarations.queue_traits,
"queue< int >"
"queue<int>"
)
__cmp_traits(
global_ns, "pq_int",
declarations.priority_queue_traits,
"priority_queue< int >"
"priority_queue<int>"
)
__cmp_traits(
global_ns, "s_v_int",
declarations.set_traits,
"set< std::vector< int > >"
"set<std::vector<int>>"
)
__cmp_traits(
global_ns,
"ms_v_int",
declarations.multiset_traits,
"multiset< std::vector< int > >",
"multiset<std::vector<int>>",
)
__cmp_traits(
global_ns, "m_i2d",
declarations.map_traits,
"map< int, double >",
"map<int, double>",
"int"
)
__cmp_traits(
global_ns,
"mm_i2d",
declarations.multimap_traits,
"multimap< int, double >",
"multimap<int, double>",
"int",
)
__cmp_traits(
global_ns,
"hs_v_int",
declarations.unordered_set_traits,
"unordered_set< std::vector< int > >",
"unordered_set<std::vector<int>>",
)
__cmp_traits(
global_ns,
"mhs_v_int",
declarations.unordered_multiset_traits,
"unordered_multiset< std::vector< int > >",
"unordered_multiset<std::vector<int>>",
)
__cmp_traits(
global_ns,
"hm_i2d",
declarations.unordered_map_traits,
"unordered_map< int, double >",
"unordered_map<int, double>",
"int",
)
__cmp_traits(
global_ns,
"hmm_i2d",
declarations.unordered_multimap_traits,
"unordered_multimap< int, double >",
"unordered_multimap<int, double>",
"int",
)


def test_multimap(global_ns):
m = global_ns.class_(lambda decl: decl.name.startswith("multimap"))
declarations.find_container_traits(m)
assert m.partial_name == "multimap< int, int >"
assert m.partial_name == "multimap<int, int>"


def test_recursive_partial_name(global_ns):
f1 = global_ns.free_function("f1")
t1 = declarations.class_traits.get_declaration(f1.arguments[0].decl_type)
assert "type< std::set< std::vector< int > > >" == t1.partial_name
assert "type<std::set<std::vector<int>>>" == t1.partial_name


def test_remove_defaults_partial_name_namespace(global_ns):
Expand Down
Loading

0 comments on commit 0874e3d

Please sign in to comment.