Skip to content

Commit 88e02a4

Browse files
committed
#45: Improved mock generation
1 parent 011348e commit 88e02a4

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

Tests/LowLevel/Mock/Lib/generate_mock.py

+16-14
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,26 @@
77

88
header_file = "".join(open(header_file_path, "r").readlines())
99

10-
# https://regex101.com/r/weFtWH/1
11-
matches = re.findall(r"([a-zA-Z0-9_]+)\s+([a-zA-Z0-9_]+)\s*\((([a-zA-Z0-9_*\s,]+)*)\);", header_file)
10+
identifier = r"[a-zA-Z0-9_]+"
11+
variable = r"(?:(?:const|volatile)\s)*\s*" + identifier + r"\s*\**\s*(?:const\s*)?(" + identifier + r")(?:\[[0-9]*\])*"
12+
function_declaration = r"(" + variable + r")\s*\(((?:" + variable + r"\s*,\s*)*(?:" + variable + r"|void))\)\s*;"
13+
14+
matches = re.findall(function_declaration, header_file)
1215
functions = []
1316
for match in matches:
14-
return_type = match[0]
17+
return_type_with_name = match[0].strip()
1518
name = match[1]
1619
args = match[2].split(",")
17-
args_with_name = []
20+
arg_with_names = []
1821
if not (len(args) == 1 and args[0] == "void"):
1922
for arg in args:
20-
# https://regex101.com/r/weFtWH/2
21-
arg_match = re.search(r"\s*([a-zA-Z0-9_]+\s+)*([a-zA-Z0-9_]+)\s+\*?([a-zA-Z0-9_]+)", arg)
22-
args_with_name.append((arg, arg_match.groups()[-1]))
23-
24-
functions.append((return_type, name, args_with_name))
23+
arg = arg.strip()
24+
var_result = re.search(variable, arg)
25+
arg_with_names.append((arg, var_result.group(1)))
26+
functions.append((return_type_with_name, name, arg_with_names))
2527

2628
# https://regex101.com/r/weFtWH/3
27-
matches = re.findall(r"extern\s+([a-zA-Z0-9_]+\s+[a-zA-Z0-9_]+\s*(,\s*[a-zA-Z0-9_]+\s*)*;)", header_file)
29+
matches = re.findall(r"extern\s+("+variable+r";)", header_file)
2830

2931
variables = []
3032
for match in matches:
@@ -52,14 +54,14 @@
5254
f"#include \"{module_name}.hpp\"\n"
5355
f"namespace mock {{Base<{function_list}> {module_name}; }}\n\n"
5456
)
55-
for return_type, name, args_with_name in functions:
56-
args = ", ".join([arg[0] for arg in args_with_name])
57+
for return_type_with_name, name, args_with_name in functions:
58+
args = ", ".join([f"{arg[0]}" for arg in args_with_name])
5759
args_forward = ", ".join([arg[1] for arg in args_with_name])
5860

59-
return_statement = "return" if return_type != "void" else ""
61+
return_statement = "return" if return_type_with_name.startswith("void") else ""
6062

6163
mock_src.write(
62-
f"{return_type} {name} ({args}) {{\n"
64+
f"{return_type_with_name}({args}) {{\n"
6365
f"\t{return_statement} mock::{module_name}.functionCallDelegate<{name}>({args_forward});\n"
6466
f"}}\n\n"
6567
)

0 commit comments

Comments
 (0)