From 88e02a49a930c5a5b821df0125ecbe9010cd01f6 Mon Sep 17 00:00:00 2001 From: Paul Nykiel Date: Sun, 1 Jan 2023 10:49:49 +0100 Subject: [PATCH] #45: Improved mock generation --- Tests/LowLevel/Mock/Lib/generate_mock.py | 30 +++++++++++++----------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/Tests/LowLevel/Mock/Lib/generate_mock.py b/Tests/LowLevel/Mock/Lib/generate_mock.py index 6108143..f9bdf70 100644 --- a/Tests/LowLevel/Mock/Lib/generate_mock.py +++ b/Tests/LowLevel/Mock/Lib/generate_mock.py @@ -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: @@ -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" )