Skip to content

Commit

Permalink
tests: normalize headers, ignore line nums in json
Browse files Browse the repository at this point in the history
  • Loading branch information
mara004 committed Dec 18, 2023
1 parent 18168a9 commit 5b4eea4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 40 deletions.
11 changes: 7 additions & 4 deletions tests/ctypesgentest.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def generate(header_str, args=[], lang="py"):
COUNTER += 1

tmp_in = TMP_DIR/f"in_header_{COUNTER:02d}.h"
tmp_in.write_text(header_str)
tmp_in.write_text(header_str.strip() + "\n")
try:
tmp_out = TMP_DIR/f"out_bindings_{COUNTER:02d}.{lang}"
ctypesgen_main(["-i", tmp_in, "-o", tmp_out, "--output-language", lang, *args])
Expand Down Expand Up @@ -126,9 +126,12 @@ def _replace_anon_tag(self, json, tag, new_tag):
elif key == "tag" and isinstance(value, str):
if value == tag:
json[key] = new_tag
elif sys.platform == "win32" and 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 thing
value[0] = value[0].replace("\\\\", "\\")
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)

Expand Down
66 changes: 30 additions & 36 deletions tests/testsuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,13 +530,7 @@ class StructuresTest(unittest.TestCase):

@classmethod
def setUpClass(cls):
"""
NOTE: Very possibly, if you change this header string, you need to
change the line numbers in the JSON output test result below
(in test_struct_json).
"""
header_str = """
struct foo
{
int a;
Expand Down Expand Up @@ -945,7 +939,7 @@ def test_struct_json(self):
],
"opaque": False,
"attrib": {},
"src": [self.tmp_header_path, 21],
"src": [self.tmp_header_path, None],
"tag": "anon_1",
"variety": "struct",
},
Expand Down Expand Up @@ -1110,7 +1104,7 @@ def test_struct_json(self):
],
"opaque": False,
"attrib": {"packed": True},
"src": [self.tmp_header_path, 30],
"src": [self.tmp_header_path, None],
"tag": "anon_2",
"variety": "struct",
},
Expand Down Expand Up @@ -1275,7 +1269,7 @@ def test_struct_json(self):
],
"opaque": False,
"attrib": {"packed": True, "aligned": [4]},
"src": [self.tmp_header_path, 40],
"src": [self.tmp_header_path, None],
"tag": "anon_3",
"variety": "struct",
},
Expand Down Expand Up @@ -1487,7 +1481,7 @@ def test_struct_json(self):
],
"opaque": False,
"attrib": {},
"src": [self.tmp_header_path, 77],
"src": [self.tmp_header_path, None],
"tag": "anon_4",
"variety": "struct",
},
Expand Down Expand Up @@ -1550,7 +1544,7 @@ def test_struct_json(self):
],
],
"opaque": False,
"src": [self.tmp_header_path, 81],
"src": [self.tmp_header_path, None],
"tag": "anon_5",
"variety": "struct",
},
Expand Down Expand Up @@ -1588,7 +1582,7 @@ def test_struct_json(self):
],
],
"opaque": False,
"src": [self.tmp_header_path, 81],
"src": [self.tmp_header_path, None],
"tag": "anon_5",
"variety": "struct",
},
Expand Down Expand Up @@ -1677,7 +1671,7 @@ def test_struct_json(self):
],
"opaque": False,
"attrib": {},
"src": [self.tmp_header_path, 3],
"src": [self.tmp_header_path, None],
"tag": "foo",
"variety": "struct",
},
Expand Down Expand Up @@ -1763,7 +1757,7 @@ def test_struct_json(self):
],
"opaque": False,
"attrib": {"packed": True},
"src": [self.tmp_header_path, 12],
"src": [self.tmp_header_path, None],
"tag": "packed_foo",
"variety": "struct",
},
Expand Down Expand Up @@ -1849,7 +1843,7 @@ def test_struct_json(self):
],
],
"opaque": False,
"src": [self.tmp_header_path, 56],
"src": [self.tmp_header_path, None],
"tag": "pragma_packed_foo2",
"variety": "struct",
},
Expand Down Expand Up @@ -1935,7 +1929,7 @@ def test_struct_json(self):
],
],
"opaque": False,
"src": [self.tmp_header_path, 66],
"src": [self.tmp_header_path, None],
"tag": "foo3",
"variety": "struct",
},
Expand Down Expand Up @@ -2078,11 +2072,11 @@ class EnumTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
header_str = """
typedef enum {
TEST_1 = 0,
TEST_2
} test_status_t;
"""
typedef enum {
TEST_1 = 0,
TEST_2
} test_status_t;
"""
cls.module = generate(header_str)
cls.json, cls.tmp_header_path = generate(header_str, lang="json")

Expand Down Expand Up @@ -2172,7 +2166,7 @@ def test_enum_json(self):
],
"errors": [],
"opaque": False,
"src": [self.tmp_header_path, 2],
"src": [self.tmp_header_path, None],
"tag": "anon_1",
},
"name": "test_status_t",
Expand All @@ -2187,14 +2181,14 @@ class PrototypeTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
header_str = """
int bar2(int a);
int bar(int);
void foo(void);
void foo2(void) __attribute__((stdcall));
void * __attribute__((stdcall)) foo3(void);
void * __attribute__((stdcall)) * foo4(void);
void foo5(void) __attribute__((__stdcall__));
"""
int bar2(int a);
int bar(int);
void foo(void);
void foo2(void) __attribute__((stdcall));
void * __attribute__((stdcall)) foo3(void);
void * __attribute__((stdcall)) * foo4(void);
void foo5(void) __attribute__((__stdcall__));
"""
cls.json, _ = generate(header_str, lang="json")

@classmethod
Expand Down Expand Up @@ -2341,12 +2335,12 @@ class LongDoubleTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
header_str = """
struct foo
{
long double is_bar;
int a;
};
"""
struct foo
{
long double is_bar;
int a;
};
"""
cls.module = generate(header_str) # ["--all-headers"]

@classmethod
Expand Down

0 comments on commit 5b4eea4

Please sign in to comment.