From c08d18d260912a067c1f43006efcb1799470807d Mon Sep 17 00:00:00 2001 From: Paul Nykiel Date: Fri, 30 Dec 2022 22:12:20 +0100 Subject: [PATCH] #45: Started with extraction of mocking lib --- Tests/LowLevel/CMakeLists.txt | 3 +++ Tests/LowLevel/Mock/CMakeLists.txt | 20 ++++++++++++++++ Tests/LowLevel/Mock/Lib/generate_mock.py | 30 ++++++++++++------------ 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/Tests/LowLevel/CMakeLists.txt b/Tests/LowLevel/CMakeLists.txt index 94c19715..8781be3c 100644 --- a/Tests/LowLevel/CMakeLists.txt +++ b/Tests/LowLevel/CMakeLists.txt @@ -1,5 +1,7 @@ add_subdirectory(Mock) +declare_mock(${CMAKE_SOURCE_DIR}/Src/HAL/uart.h) + make_test(Drivers/bno055_uart) target_link_libraries(Test_Drivers_bno055_uart PRIVATE Mock_HAL_uart) @@ -55,6 +57,7 @@ target_link_libraries(Test_Application_mode_handler PRIVATE Mock_Components_imu) target_link_libraries(Test_Application_mode_handler PRIVATE Mock_Components_remote) target_link_libraries(Test_Application_mode_handler PRIVATE Mock_Components_flightcomputer) + add_custom_target(RunAllTests DEPENDS ${AllTests}) add_custom_target(coverage.info diff --git a/Tests/LowLevel/Mock/CMakeLists.txt b/Tests/LowLevel/Mock/CMakeLists.txt index 9d8bfb5f..7206a643 100644 --- a/Tests/LowLevel/Mock/CMakeLists.txt +++ b/Tests/LowLevel/Mock/CMakeLists.txt @@ -40,6 +40,26 @@ set(MockedSystemModules avr/io avr/wdt) +function(declare_mock mocked_module_header) + # Build names/paths + get_filename_component(module_name ${mocked_module_header} NAME_WE) + set(mock_path ${CMAKE_CURRENT_BINARY_DIR}/Mock/Mock) + set(mock_cpp ${mock_path}/${module_name}.cpp) + set(mock_hpp ${mock_path}/${module_name}.hpp) + set(mock_lib ${module_name}.mock) + + # Add a target which will autogenerate the mock source files + add_custom_command( + COMMAND python3 ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/Lib/generate_mock.py ${mocked_module_header} ${mock_path} + DEPENDS ${mocked_module_header} ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/Lib/generate_mock.py + OUTPUT ${mock_cpp} ${mock_hpp}) + + # Add a library for the mocked module + add_library(${mock_lib} STATIC ${mock_cpp} ${mock_hpp}) + target_link_libraries(${mock_lib} PUBLIC MockLib) + target_include_directories(${mock_lib} INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/Mock) +endfunction() + # Build mock sources and target for module mocks foreach (MockedModule ${MockedModules}) # For the name generation replace all / in the path with _ diff --git a/Tests/LowLevel/Mock/Lib/generate_mock.py b/Tests/LowLevel/Mock/Lib/generate_mock.py index 634ba859..61081433 100644 --- a/Tests/LowLevel/Mock/Lib/generate_mock.py +++ b/Tests/LowLevel/Mock/Lib/generate_mock.py @@ -2,11 +2,10 @@ import re import sys -header_path = sys.argv[1] -module = sys.argv[2] -out_dir = sys.argv[3] +header_file_path = sys.argv[1] # Path to the header file, i.e. Src/HAL/uart.h +out_dir = sys.argv[2] # Path to generate the mock file to, i.e. cmake-build-debug/..../Mock/ -header_file = "".join(open(header_path, "r").readlines()) +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) @@ -31,26 +30,27 @@ for match in matches: variables.append(match[0]) -mock_name = module.split("/")[-1] function_list = ", ".join([function[1] for function in functions]) -out_dir = os.path.dirname(f"{out_dir}/{module}") +module_name = os.path.basename(header_file_path).split(".")[0] # Name of the module i.e. uart + os.makedirs(out_dir, exist_ok=True) -mock_header = open(f"{out_dir}/{mock_name}.hpp", "w") +mock_header = open(f"{out_dir}/{module_name}.hpp", "w") +print(f"{out_dir}/{module_name}.hpp") mock_header.write( - f"#ifndef MOCK_{mock_name}_HPP \n" - f"#define MOCK_{mock_name}_HPP \n" + f"#ifndef MOCK_{module_name.upper()}_HPP \n" + f"#define MOCK_{module_name.upper()}_HPP \n" f"#include \"Lib/Base.hpp\" \n" f"extern \"C\" {{\n" - f" #include \"{module}.h\"\n" + f" #include \"{header_file_path}\"\n" f"}}\n" - f"namespace mock {{extern Base<{function_list}> {mock_name}; }}\n" + f"namespace mock {{extern Base<{function_list}> {module_name}; }}\n" f"#endif") -mock_src = open(f"{out_dir}/{mock_name}.cpp", "w") +mock_src = open(f"{out_dir}/{module_name}.cpp", "w") mock_src.write( - f"#include \"{mock_name}.hpp\"\n" - f"namespace mock {{Base<{function_list}> {mock_name}; }}\n\n" + 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]) @@ -60,7 +60,7 @@ mock_src.write( f"{return_type} {name} ({args}) {{\n" - f"\t{return_statement} mock::{mock_name}.functionCallDelegate<{name}>({args_forward});\n" + f"\t{return_statement} mock::{module_name}.functionCallDelegate<{name}>({args_forward});\n" f"}}\n\n" )