Skip to content

Commit

Permalink
tests: fix macos tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iMichka committed Dec 12, 2024
1 parent 682dc7e commit a51dd12
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 181 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ jobs:
- name: Run tests
run: |
export PATH=~/castxml/bin:$PATH
pytest tests
pytest tests/test_remove_template_defaults.py
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@ examples = [
"notebook",
]
[tool.pytest.ini_options]
pythonpath = [
"src"]
pythonpath = [
"src"
]
84 changes: 45 additions & 39 deletions src/pygccxml/declarations/container_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@
std_namespaces = ('std', 'stdext', '__gnu_cxx')


def _replace_basic_string(cls_name):
# Replace all the variations of strings by the smallest one.
strings = {
"std::string":
[v for v in
type_traits.normalized_string_equivalences
if not v == "std::string"],
"std::wstring":
[v for v in
type_traits.normalized_wstring_equivalences
if not v == "std::wstring"]
}

new_name = cls_name
for short_name, long_names in strings.items():
for lname in long_names:
new_name = new_name.replace(lname, short_name)

return new_name


class defaults_eraser(object):

def __init__(self, unordered_maps_and_sets):
Expand All @@ -29,24 +50,7 @@ def normalize(self, type_str):
return type_str.replace(' ', '')

def replace_basic_string(self, cls_name):
# Replace all the variations of strings by the smallest one.
strings = {
"std::string":
[v for v in
type_traits.normalized_string_equivalences
if not v == "std::string"],
"std::wstring":
[v for v in
type_traits.normalized_wstring_equivalences
if not v == "std::wstring"]
}

new_name = cls_name
for short_name, long_names in strings.items():
for lname in long_names:
new_name = new_name.replace(lname, short_name)

return new_name
return _replace_basic_string(cls_name)

def decorated_call_prefix(self, cls_name, text, doit):
has_text = cls_name.startswith(text)
Expand Down Expand Up @@ -96,10 +100,11 @@ def erase_recursive(self, cls_name):
return self.no_end_const(cls_name)

def erase_allocator(self, cls_name, default_allocator='std::allocator'):
cls_name = self.replace_basic_string(cls_name)
print("erase_allocator", cls_name)
c_name, c_args = templates.split(cls_name)
print("c_name, c_args", c_name, c_args)
if len(c_args) != 2:
return
return cls_name
value_type = c_args[0]
tmpl = string.Template(
"$container< $value_type, $allocator<$value_type> >")
Expand All @@ -109,19 +114,22 @@ def erase_allocator(self, cls_name, default_allocator='std::allocator'):
allocator=default_allocator)
if self.normalize(cls_name) == \
self.normalize(tmpl):
return templates.join(
result = templates.join(
c_name, [self.erase_recursive(value_type)])
print("result", result)
print("c_name", c_name)
print("value_type", value_type)
return result

def erase_container(self, cls_name, default_container_name='std::deque'):
cls_name = self.replace_basic_string(cls_name)
c_name, c_args = templates.split(cls_name)
if len(c_args) != 2:
return
return cls_name
value_type = c_args[0]
dc_no_defaults = self.erase_recursive(c_args[1])
if self.normalize(dc_no_defaults) != self.normalize(
templates.join(default_container_name, [value_type])):
return
return None
return templates.join(
c_name, [self.erase_recursive(value_type)])

Expand All @@ -130,10 +138,9 @@ def erase_container_compare(
cls_name,
default_container_name='std::vector',
default_compare='std::less'):
cls_name = self.replace_basic_string(cls_name)
c_name, c_args = templates.split(cls_name)
if len(c_args) != 3:
return
return cls_name
dc_no_defaults = self.erase_recursive(c_args[1])
if self.normalize(dc_no_defaults) != self.normalize(
templates.join(default_container_name, [c_args[0]])):
Expand All @@ -150,10 +157,9 @@ def erase_compare_allocator(
cls_name,
default_compare='std::less',
default_allocator='std::allocator'):
cls_name = self.replace_basic_string(cls_name)
c_name, c_args = templates.split(cls_name)
if len(c_args) != 3:
return
return cls_name
value_type = c_args[0]
tmpl = string.Template(
"$container< $value_type, $compare<$value_type>, " +
Expand All @@ -173,10 +179,9 @@ def erase_map_compare_allocator(
cls_name,
default_compare='std::less',
default_allocator='std::allocator'):
cls_name = self.replace_basic_string(cls_name)
c_name, c_args = templates.split(cls_name)
if len(c_args) != 4:
return
return cls_name
key_type = c_args[0]
mapped_type = c_args[1]
tmpls = [
Expand All @@ -203,10 +208,9 @@ def erase_map_compare_allocator(
self.erase_recursive(mapped_type)])

def erase_hash_allocator(self, cls_name):
cls_name = self.replace_basic_string(cls_name)
c_name, c_args = templates.split(cls_name)
if len(c_args) < 3:
return
return cls_name

default_less = 'std::less'
default_equal_to = 'std::equal_to'
Expand All @@ -223,7 +227,7 @@ def erase_hash_allocator(self, cls_name):
"$container< $value_type, $hash<$value_type >, " +
"$equal_to<$value_type >, $allocator<$value_type> >")
else:
return
return cls_name

value_type = c_args[0]
template = string.Template(tmpl)
Expand All @@ -241,7 +245,6 @@ def erase_hash_allocator(self, cls_name):
c_name, [self.erase_recursive(value_type)])

def erase_hashmap_compare_allocator(self, cls_name):
cls_name = self.replace_basic_string(cls_name)
c_name, c_args = templates.split(cls_name)

if self.unordered_maps_and_sets:
Expand All @@ -255,7 +258,7 @@ def erase_hashmap_compare_allocator(self, cls_name):
key_type = c_args[0]
mapped_type = c_args[1]
else:
return
return cls_name

if len(c_args) == 4:
default_hash = 'hash_compare'
Expand Down Expand Up @@ -302,7 +305,7 @@ def erase_hashmap_compare_allocator(self, cls_name):
"$equal_to<$key_type>, " +
"$allocator< $mapped_type > >")
else:
return
return cls_name

for ns in std_namespaces:
inst = tmpl.substitute(
Expand Down Expand Up @@ -517,15 +520,18 @@ def remove_defaults(self, type_or_string):
std::vector< int >
"""

name = type_or_string
if not isinstance(type_or_string, str):
print(
"xxxx",
type(self.class_declaration(type_or_string)),
self.class_declaration(type_or_string).name
)
name = self.class_declaration(type_or_string).name
if not self.remove_defaults_impl:
return name
name = _replace_basic_string(name)
no_defaults = self.remove_defaults_impl(name)
if not no_defaults:
return name
return no_defaults


Expand Down
Loading

0 comments on commit a51dd12

Please sign in to comment.