Skip to content

Commit

Permalink
tests: move out big json engine + expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
mara004 committed Dec 18, 2023
1 parent 5b4eea4 commit 0066f70
Show file tree
Hide file tree
Showing 3 changed files with 1,661 additions and 1,649 deletions.
70 changes: 0 additions & 70 deletions tests/ctypesgentest.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,76 +80,6 @@ def ctypesgen_version():
return VERSION


def sort_anon_fn(anon_tag):
return int(anon_tag.split("_")[1])


class JsonHelper:
"""
Utility class preparing generated JSON result for testing.
JSON stores the path to some source items. These need to be genericized in
order for tests to succeed on all machines/user accounts. This is also the
case for "anon_" tags, which are "reset" for each test to start from
"anon_1".
"""

def __init__(self):
self.anons = list()

def prepare(self, json):
"""Prepares generated JSON result for testing"""
self._search_anon_tags(json)
unique_list = list(set(self.anons))
unique_sorted_list = sorted(unique_list, key=sort_anon_fn)

mapped_tags = dict()
counter = 1
for i in unique_sorted_list:
mapped_tags[i] = "anon_{0}".format(counter)
counter += 1

for (old_tag, new_tag) in mapped_tags.items():
self._replace_anon_tag(json, old_tag, new_tag)

def _replace_anon_tag(self, json, tag, new_tag):
"""Replaces source paths and resets anon_ tags to increment from 1"""
if isinstance(json, list):
for item in json:
self._replace_anon_tag(item, tag, new_tag)
return
if isinstance(json, dict):
for key, value in json.items():
if key == "name" and isinstance(value, str):
if value == tag:
json[key] = new_tag
elif key == "tag" and isinstance(value, str):
if value == tag:
json[key] = new_tag
elif key == "src" and isinstance(value, list) and value:
# for whatever reason, on windows ctypesgen's json output contains double slashes in paths, whereas the expectation contains only single slashes, so normalize the output
if sys.platform == "win32":
value[0] = value[0].replace("\\\\", "\\")
# # ignore the line number so changing headers does not cause erroneous test fails
value[1] = None
else:
self._replace_anon_tag(value, tag, new_tag)

def _search_anon_tags(self, json):
"""Search for anon_ tags"""
if isinstance(json, list):
for item in json:
self._search_anon_tags(item)
return
if isinstance(json, dict):
for key, value in json.items():
if key == "name" and isinstance(value, str):
if value.startswith("anon_"):
self.anons.append(value)
else:
self._search_anon_tags(value)


# -- Functions facilitating tests of use of cross inclusion --


Expand Down
Loading

0 comments on commit 0066f70

Please sign in to comment.