Skip to content

Commit 5b4eea4

Browse files
committed
tests: normalize headers, ignore line nums in json
1 parent 18168a9 commit 5b4eea4

File tree

2 files changed

+37
-40
lines changed

2 files changed

+37
-40
lines changed

tests/ctypesgentest.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def generate(header_str, args=[], lang="py"):
5454
COUNTER += 1
5555

5656
tmp_in = TMP_DIR/f"in_header_{COUNTER:02d}.h"
57-
tmp_in.write_text(header_str)
57+
tmp_in.write_text(header_str.strip() + "\n")
5858
try:
5959
tmp_out = TMP_DIR/f"out_bindings_{COUNTER:02d}.{lang}"
6060
ctypesgen_main(["-i", tmp_in, "-o", tmp_out, "--output-language", lang, *args])
@@ -126,9 +126,12 @@ def _replace_anon_tag(self, json, tag, new_tag):
126126
elif key == "tag" and isinstance(value, str):
127127
if value == tag:
128128
json[key] = new_tag
129-
elif sys.platform == "win32" and key == "src" and isinstance(value, list) and value:
130-
# 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
131-
value[0] = value[0].replace("\\\\", "\\")
129+
elif key == "src" and isinstance(value, list) and value:
130+
# 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
131+
if sys.platform == "win32":
132+
value[0] = value[0].replace("\\\\", "\\")
133+
# # ignore the line number so changing headers does not cause erroneous test fails
134+
value[1] = None
132135
else:
133136
self._replace_anon_tag(value, tag, new_tag)
134137

tests/testsuite.py

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -530,13 +530,7 @@ class StructuresTest(unittest.TestCase):
530530

531531
@classmethod
532532
def setUpClass(cls):
533-
"""
534-
NOTE: Very possibly, if you change this header string, you need to
535-
change the line numbers in the JSON output test result below
536-
(in test_struct_json).
537-
"""
538533
header_str = """
539-
540534
struct foo
541535
{
542536
int a;
@@ -945,7 +939,7 @@ def test_struct_json(self):
945939
],
946940
"opaque": False,
947941
"attrib": {},
948-
"src": [self.tmp_header_path, 21],
942+
"src": [self.tmp_header_path, None],
949943
"tag": "anon_1",
950944
"variety": "struct",
951945
},
@@ -1110,7 +1104,7 @@ def test_struct_json(self):
11101104
],
11111105
"opaque": False,
11121106
"attrib": {"packed": True},
1113-
"src": [self.tmp_header_path, 30],
1107+
"src": [self.tmp_header_path, None],
11141108
"tag": "anon_2",
11151109
"variety": "struct",
11161110
},
@@ -1275,7 +1269,7 @@ def test_struct_json(self):
12751269
],
12761270
"opaque": False,
12771271
"attrib": {"packed": True, "aligned": [4]},
1278-
"src": [self.tmp_header_path, 40],
1272+
"src": [self.tmp_header_path, None],
12791273
"tag": "anon_3",
12801274
"variety": "struct",
12811275
},
@@ -1487,7 +1481,7 @@ def test_struct_json(self):
14871481
],
14881482
"opaque": False,
14891483
"attrib": {},
1490-
"src": [self.tmp_header_path, 77],
1484+
"src": [self.tmp_header_path, None],
14911485
"tag": "anon_4",
14921486
"variety": "struct",
14931487
},
@@ -1550,7 +1544,7 @@ def test_struct_json(self):
15501544
],
15511545
],
15521546
"opaque": False,
1553-
"src": [self.tmp_header_path, 81],
1547+
"src": [self.tmp_header_path, None],
15541548
"tag": "anon_5",
15551549
"variety": "struct",
15561550
},
@@ -1588,7 +1582,7 @@ def test_struct_json(self):
15881582
],
15891583
],
15901584
"opaque": False,
1591-
"src": [self.tmp_header_path, 81],
1585+
"src": [self.tmp_header_path, None],
15921586
"tag": "anon_5",
15931587
"variety": "struct",
15941588
},
@@ -1677,7 +1671,7 @@ def test_struct_json(self):
16771671
],
16781672
"opaque": False,
16791673
"attrib": {},
1680-
"src": [self.tmp_header_path, 3],
1674+
"src": [self.tmp_header_path, None],
16811675
"tag": "foo",
16821676
"variety": "struct",
16831677
},
@@ -1763,7 +1757,7 @@ def test_struct_json(self):
17631757
],
17641758
"opaque": False,
17651759
"attrib": {"packed": True},
1766-
"src": [self.tmp_header_path, 12],
1760+
"src": [self.tmp_header_path, None],
17671761
"tag": "packed_foo",
17681762
"variety": "struct",
17691763
},
@@ -1849,7 +1843,7 @@ def test_struct_json(self):
18491843
],
18501844
],
18511845
"opaque": False,
1852-
"src": [self.tmp_header_path, 56],
1846+
"src": [self.tmp_header_path, None],
18531847
"tag": "pragma_packed_foo2",
18541848
"variety": "struct",
18551849
},
@@ -1935,7 +1929,7 @@ def test_struct_json(self):
19351929
],
19361930
],
19371931
"opaque": False,
1938-
"src": [self.tmp_header_path, 66],
1932+
"src": [self.tmp_header_path, None],
19391933
"tag": "foo3",
19401934
"variety": "struct",
19411935
},
@@ -2078,11 +2072,11 @@ class EnumTest(unittest.TestCase):
20782072
@classmethod
20792073
def setUpClass(cls):
20802074
header_str = """
2081-
typedef enum {
2082-
TEST_1 = 0,
2083-
TEST_2
2084-
} test_status_t;
2085-
"""
2075+
typedef enum {
2076+
TEST_1 = 0,
2077+
TEST_2
2078+
} test_status_t;
2079+
"""
20862080
cls.module = generate(header_str)
20872081
cls.json, cls.tmp_header_path = generate(header_str, lang="json")
20882082

@@ -2172,7 +2166,7 @@ def test_enum_json(self):
21722166
],
21732167
"errors": [],
21742168
"opaque": False,
2175-
"src": [self.tmp_header_path, 2],
2169+
"src": [self.tmp_header_path, None],
21762170
"tag": "anon_1",
21772171
},
21782172
"name": "test_status_t",
@@ -2187,14 +2181,14 @@ class PrototypeTest(unittest.TestCase):
21872181
@classmethod
21882182
def setUpClass(cls):
21892183
header_str = """
2190-
int bar2(int a);
2191-
int bar(int);
2192-
void foo(void);
2193-
void foo2(void) __attribute__((stdcall));
2194-
void * __attribute__((stdcall)) foo3(void);
2195-
void * __attribute__((stdcall)) * foo4(void);
2196-
void foo5(void) __attribute__((__stdcall__));
2197-
"""
2184+
int bar2(int a);
2185+
int bar(int);
2186+
void foo(void);
2187+
void foo2(void) __attribute__((stdcall));
2188+
void * __attribute__((stdcall)) foo3(void);
2189+
void * __attribute__((stdcall)) * foo4(void);
2190+
void foo5(void) __attribute__((__stdcall__));
2191+
"""
21982192
cls.json, _ = generate(header_str, lang="json")
21992193

22002194
@classmethod
@@ -2341,12 +2335,12 @@ class LongDoubleTest(unittest.TestCase):
23412335
@classmethod
23422336
def setUpClass(cls):
23432337
header_str = """
2344-
struct foo
2345-
{
2346-
long double is_bar;
2347-
int a;
2348-
};
2349-
"""
2338+
struct foo
2339+
{
2340+
long double is_bar;
2341+
int a;
2342+
};
2343+
"""
23502344
cls.module = generate(header_str) # ["--all-headers"]
23512345

23522346
@classmethod

0 commit comments

Comments
 (0)