Skip to content

Commit

Permalink
#45: Started with extraction of mocking lib
Browse files Browse the repository at this point in the history
  • Loading branch information
aul12 committed Dec 30, 2022
1 parent 835f134 commit c08d18d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
3 changes: 3 additions & 0 deletions Tests/LowLevel/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions Tests/LowLevel/Mock/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 _
Expand Down
30 changes: 15 additions & 15 deletions Tests/LowLevel/Mock/Lib/generate_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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])
Expand All @@ -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"
)

Expand Down

0 comments on commit c08d18d

Please sign in to comment.