Skip to content

Commit

Permalink
#45: Improved mock generation
Browse files Browse the repository at this point in the history
  • Loading branch information
aul12 committed Jan 1, 2023
1 parent 011348e commit 88e02a4
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions Tests/LowLevel/Mock/Lib/generate_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@

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

# https://regex101.com/r/weFtWH/1
matches = re.findall(r"([a-zA-Z0-9_]+)\s+([a-zA-Z0-9_]+)\s*\((([a-zA-Z0-9_*\s,]+)*)\);", header_file)
identifier = r"[a-zA-Z0-9_]+"
variable = r"(?:(?:const|volatile)\s)*\s*" + identifier + r"\s*\**\s*(?:const\s*)?(" + identifier + r")(?:\[[0-9]*\])*"
function_declaration = r"(" + variable + r")\s*\(((?:" + variable + r"\s*,\s*)*(?:" + variable + r"|void))\)\s*;"

matches = re.findall(function_declaration, header_file)
functions = []
for match in matches:
return_type = match[0]
return_type_with_name = match[0].strip()
name = match[1]
args = match[2].split(",")
args_with_name = []
arg_with_names = []
if not (len(args) == 1 and args[0] == "void"):
for arg in args:
# https://regex101.com/r/weFtWH/2
arg_match = re.search(r"\s*([a-zA-Z0-9_]+\s+)*([a-zA-Z0-9_]+)\s+\*?([a-zA-Z0-9_]+)", arg)
args_with_name.append((arg, arg_match.groups()[-1]))

functions.append((return_type, name, args_with_name))
arg = arg.strip()
var_result = re.search(variable, arg)
arg_with_names.append((arg, var_result.group(1)))
functions.append((return_type_with_name, name, arg_with_names))

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

variables = []
for match in matches:
Expand Down Expand Up @@ -52,14 +54,14 @@
f"#include \"{module_name}.hpp\"\n"
f"namespace mock {{Base<{function_list}> {module_name}; }}\n\n"
)
for return_type, name, args_with_name in functions:
args = ", ".join([arg[0] for arg in args_with_name])
for return_type_with_name, name, args_with_name in functions:
args = ", ".join([f"{arg[0]}" for arg in args_with_name])
args_forward = ", ".join([arg[1] for arg in args_with_name])

return_statement = "return" if return_type != "void" else ""
return_statement = "return" if return_type_with_name.startswith("void") else ""

mock_src.write(
f"{return_type} {name} ({args}) {{\n"
f"{return_type_with_name}({args}) {{\n"
f"\t{return_statement} mock::{module_name}.functionCallDelegate<{name}>({args_forward});\n"
f"}}\n\n"
)
Expand Down

0 comments on commit 88e02a4

Please sign in to comment.