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 Dec 3, 2024
1 parent 7c72a13 commit 688576e
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 85 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"
]
63 changes: 31 additions & 32 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 @@ -383,7 +383,6 @@ def get_container_or_none(self, type_):

utils.loggers.queries_engine.debug(
"Container traits: cleaned up search %s", type_)

if isinstance(type_, cpptypes.declarated_t):
cls_declaration = type_traits.remove_alias(type_.declaration)
elif isinstance(type_, class_declaration.class_t):
Expand Down Expand Up @@ -512,12 +511,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
4 changes: 4 additions & 0 deletions src/pygccxml/declarations/declaration.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ def partial_name(self):

return self._partial_name

@partial_name.setter
def partial_name(self, new_partial_name):
self._partial_name = new_partial_name

@property
def parent(self):
"""
Expand Down
14 changes: 14 additions & 0 deletions src/pygccxml/parser/patcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,17 @@ 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:
decl.name = decl.name.replace(" >", ">").replace("< ", "<")
decl.partial_name = \
decl.partial_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
31 changes: 15 additions & 16 deletions tests/test_find_container_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def __cmp_traits(global_ns, typedef, expected, partial_name, key_type=None):
assert declarations.find_container_traits(cls) == expected
assert cls.partial_name == partial_name
cls = traits.class_declaration(cls)
print("xxxx", traits, typedef)
assert traits.element_type(typedef) is not None
assert cls.cache.container_element_type is not None

Expand All @@ -51,91 +50,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 688576e

Please sign in to comment.