From 5d053ceeeca17082a69ec28e90413e0b17d15c6b Mon Sep 17 00:00:00 2001 From: Paul Nykiel Date: Sun, 1 Jan 2023 18:35:21 +0100 Subject: [PATCH] #45: Refactoring of mocking lib for extraction --- Tests/LowLevel/CMakeLists.txt | 17 ---------------- Tests/LowLevel/Mock/CMakeLists.txt | 23 +++++++--------------- Tests/LowLevel/Mock/Lib/Base.hpp | 6 +++--- Tests/LowLevel/Mock/Lib/CMakeLists.txt | 12 ++++++++++- Tests/LowLevel/Mock/Lib/FunctionInfo.hpp | 6 +++--- Tests/LowLevel/Mock/Lib/Handler.hpp | 6 +++--- Tests/LowLevel/Mock/Lib/Util.hpp | 6 +++--- Tests/LowLevel/Mock/System/avr/interrupt.h | 6 +++--- Tests/LowLevel/Mock/System/avr/io.h | 6 +++--- Tests/LowLevel/Mock/System/avr/wdt.h | 6 +++--- Tests/LowLevel/Mock/System/util/delay.h | 6 +++--- 11 files changed, 42 insertions(+), 58 deletions(-) diff --git a/Tests/LowLevel/CMakeLists.txt b/Tests/LowLevel/CMakeLists.txt index 44d12800..02598490 100644 --- a/Tests/LowLevel/CMakeLists.txt +++ b/Tests/LowLevel/CMakeLists.txt @@ -42,20 +42,3 @@ make_fc_test(MODULE Drivers/sbus DEPS HAL/uart Drivers/ring_buffer) target_link_libraries(MessageEncoding.mock PUBLIC ToolboxPlaneMessageDefs) target_link_libraries(MessageDecoding.mock PUBLIC ToolboxPlaneMessageDefs) target_link_libraries(protobuf.mock PUBLIC ToolboxPlaneMessageDefs) - -# -#add_custom_target(coverage.info -# COMMAND lcov --capture --directory ${CMAKE_CURRENT_BINARY_DIR} --output-file ${CMAKE_CURRENT_BINARY_DIR}/coverage.info -# DEPENDS ${AllTests}) -# -#add_custom_target(Coverage -# COMMAND genhtml ${CMAKE_CURRENT_BINARY_DIR}/coverage.info --output-directory ${CMAKE_CURRENT_BINARY_DIR}/coverage -# DEPENDS coverage.info) -# -#add_custom_target(report.xml -# COMMAND junit2html . --merge report.xml -# DEPENDS ${AllTests}) -# -#add_custom_target(LLReport -# COMMAND junit2html report.xml --report-matrix LLReport.html -# DEPENDS report.xml) diff --git a/Tests/LowLevel/Mock/CMakeLists.txt b/Tests/LowLevel/Mock/CMakeLists.txt index 7fe46f6b..b3d9b32f 100644 --- a/Tests/LowLevel/Mock/CMakeLists.txt +++ b/Tests/LowLevel/Mock/CMakeLists.txt @@ -1,12 +1,3 @@ -# Download GTest -include(FetchContent) -FetchContent_Declare( - gtest - GIT_REPOSITORY "https://github.com/google/googletest.git" - GIT_TAG "main" -) -FetchContent_MakeAvailable(gtest) - # Load mocking lib add_subdirectory(Lib) @@ -37,8 +28,7 @@ function(declare_mock mocked_module_header mock_lib_out) endif () endfunction() - - +# Add a target which can be used to run all tests add_custom_target(RunAllTests) # Function to build a mock test for a module @@ -62,7 +52,8 @@ function(make_test) target_compile_options(${TestExecutable} PRIVATE --coverage -fprofile-arcs -ftest-coverage) target_link_options(${TestExecutable} PRIVATE --coverage -fprofile-arcs -ftest-coverage) - # Set the macro TEST_NAME to the name of the tested module + # Set the macro TEST_NAME to the name of the tested module and the compiler + string(REPLACE "." "_" compiler_version ${CMAKE_C_COMPILER_VERSION}) set(test_name "${CMAKE_C_COMPILER_ID}_${compiler_version}_${CMAKE_BUILD_TYPE}_${TestSuite}") target_compile_definitions(${TestExecutable} PRIVATE -DTEST_NAME=${test_name}) @@ -70,7 +61,7 @@ function(make_test) target_include_directories(${TestExecutable} PRIVATE ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/System) # Link against GTest libraries - target_link_libraries(${TestExecutable} PRIVATE gtest gmock pthread) + target_link_libraries(${TestExecutable} PRIVATE gtest pthread) # Generate mocked libraries and link against them foreach (REQUIRED_HEADER ${MAKE_TEST_REQUIRED_HEADERS}) @@ -79,11 +70,11 @@ function(make_test) endforeach () # Create a target to run the test to be used as a dependency - add_custom_target(${test_name}.run + add_custom_target(${TestExecutable}.run COMMAND ${TestExecutable} --gtest_repeat=10 --gtest_shuffle --gtest_output=xml:${test_name}.xml DEPENDS ${TestExecutable}) - # Add test to list of tests - add_dependencies(RunAllTests ${test_name}.run) + # Add test run as dependency to RunAllTests + add_dependencies(RunAllTests ${TestExecutable}.run) endfunction() diff --git a/Tests/LowLevel/Mock/Lib/Base.hpp b/Tests/LowLevel/Mock/Lib/Base.hpp index 1e39fbfb..4dd81a36 100644 --- a/Tests/LowLevel/Mock/Lib/Base.hpp +++ b/Tests/LowLevel/Mock/Lib/Base.hpp @@ -4,8 +4,8 @@ * @date 08.11.22 * Description here TODO */ -#ifndef FLIGHTCONTROLLER_MOCKBASE_HPP -#define FLIGHTCONTROLLER_MOCKBASE_HPP +#ifndef MOCK_BASE_HPP +#define MOCK_BASE_HPP #include #include @@ -81,4 +81,4 @@ namespace mock { }; } // namespace mock -#endif // FLIGHTCONTROLLER_MOCKBASE_HPP +#endif // MOCK_BASE_HPP diff --git a/Tests/LowLevel/Mock/Lib/CMakeLists.txt b/Tests/LowLevel/Mock/Lib/CMakeLists.txt index 50991500..bccde895 100644 --- a/Tests/LowLevel/Mock/Lib/CMakeLists.txt +++ b/Tests/LowLevel/Mock/Lib/CMakeLists.txt @@ -1,5 +1,15 @@ project(MockLib) +# Download GTest +include(FetchContent) +FetchContent_Declare( + gtest + GIT_REPOSITORY "https://github.com/google/googletest.git" + GIT_TAG "main" +) +FetchContent_MakeAvailable(gtest) + +# Set target which adds include path and gtest dependencies add_library(${PROJECT_NAME} INTERFACE) target_include_directories(${PROJECT_NAME} INTERFACE ..) -target_link_libraries(${PROJECT_NAME} INTERFACE gtest gmock pthread) +target_link_libraries(${PROJECT_NAME} INTERFACE gtest pthread) diff --git a/Tests/LowLevel/Mock/Lib/FunctionInfo.hpp b/Tests/LowLevel/Mock/Lib/FunctionInfo.hpp index 78f75878..46acab98 100644 --- a/Tests/LowLevel/Mock/Lib/FunctionInfo.hpp +++ b/Tests/LowLevel/Mock/Lib/FunctionInfo.hpp @@ -4,8 +4,8 @@ * @date 08.11.22 * Description here TODO */ -#ifndef FLIGHTCONTROLLER_FUNCTIONINFO_HPP -#define FLIGHTCONTROLLER_FUNCTIONINFO_HPP +#ifndef MOCK_FUNCTIONINFO_HPP +#define MOCK_FUNCTIONINFO_HPP #include #include @@ -39,4 +39,4 @@ namespace mock { using GetFunctionInfo = decltype(getFunctionInfoImpl(fun)); } // namespace mock -#endif // FLIGHTCONTROLLER_FUNCTIONINFO_HPP +#endif // MOCK_FUNCTIONINFO_HPP diff --git a/Tests/LowLevel/Mock/Lib/Handler.hpp b/Tests/LowLevel/Mock/Lib/Handler.hpp index a37528c1..01bbcf06 100644 --- a/Tests/LowLevel/Mock/Lib/Handler.hpp +++ b/Tests/LowLevel/Mock/Lib/Handler.hpp @@ -4,8 +4,8 @@ * @date 08.11.22 * Description here TODO */ -#ifndef FLIGHTCONTROLLER_HANDLER_HPP -#define FLIGHTCONTROLLER_HANDLER_HPP +#ifndef MOCK_HANDLER_HPP +#define MOCK_HANDLER_HPP #include #include @@ -103,4 +103,4 @@ namespace mock { }; } // namespace mock -#endif // FLIGHTCONTROLLER_HANDLER_HPP +#endif // MOCK_HANDLER_HPP diff --git a/Tests/LowLevel/Mock/Lib/Util.hpp b/Tests/LowLevel/Mock/Lib/Util.hpp index 8c4ea905..cd5c3d21 100644 --- a/Tests/LowLevel/Mock/Lib/Util.hpp +++ b/Tests/LowLevel/Mock/Lib/Util.hpp @@ -4,8 +4,8 @@ * @date 08.11.22 * Description here TODO */ -#ifndef FLIGHTCONTROLLER_MOCKUTIL_HPP -#define FLIGHTCONTROLLER_MOCKUTIL_HPP +#ifndef MOCK_UTIL_HPP +#define MOCK_UTIL_HPP #include #include @@ -88,4 +88,4 @@ namespace mock::util { } } // namespace mock::util -#endif // FLIGHTCONTROLLER_MOCKUTIL_HPP +#endif // MOCK_UTIL_HPP diff --git a/Tests/LowLevel/Mock/System/avr/interrupt.h b/Tests/LowLevel/Mock/System/avr/interrupt.h index e5c1b518..ca5d9091 100644 --- a/Tests/LowLevel/Mock/System/avr/interrupt.h +++ b/Tests/LowLevel/Mock/System/avr/interrupt.h @@ -4,10 +4,10 @@ * @date 22.12.22 * @brief Description here TODO */ -#ifndef FLIGHTCONTROLLER_INTERRUPT_H -#define FLIGHTCONTROLLER_INTERRUPT_H +#ifndef MOCK_AVR_INTERRUPT_H +#define MOCK_AVR_INTERRUPT_H void cli(void); void sei(void); -#endif // FLIGHTCONTROLLER_INTERRUPT_H +#endif // MOCK_AVR_INTERRUPT_H diff --git a/Tests/LowLevel/Mock/System/avr/io.h b/Tests/LowLevel/Mock/System/avr/io.h index d2ba48ba..99812e7f 100644 --- a/Tests/LowLevel/Mock/System/avr/io.h +++ b/Tests/LowLevel/Mock/System/avr/io.h @@ -4,8 +4,8 @@ * @date 27.11.22 * Description here TODO */ -#ifndef FLIGHTCONTROLLER_IO_H -#define FLIGHTCONTROLLER_IO_H +#ifndef MOCK_AVR_IO_H +#define MOCK_AVR_IO_H #include @@ -27,4 +27,4 @@ extern uint8_t MCUSR; enum { WDRF, BORF }; -#endif // FLIGHTCONTROLLER_IO_H +#endif // MOCK_AVR_IO_H diff --git a/Tests/LowLevel/Mock/System/avr/wdt.h b/Tests/LowLevel/Mock/System/avr/wdt.h index c2114f25..f517c758 100644 --- a/Tests/LowLevel/Mock/System/avr/wdt.h +++ b/Tests/LowLevel/Mock/System/avr/wdt.h @@ -4,8 +4,8 @@ * @date 22.12.22 * @brief Description here TODO */ -#ifndef FLIGHTCONTROLLER_WDT_H -#define FLIGHTCONTROLLER_WDT_H +#ifndef MOCK_AVR_WDT_H +#define MOCK_AVR_WDT_H typedef enum { WDTO_15MS = 0, @@ -23,4 +23,4 @@ typedef enum { void wdt_enable(wdt_t wdt); void wdt_reset(void); -#endif // FLIGHTCONTROLLER_WDT_H +#endif // MOCK_AVR_WDT_H diff --git a/Tests/LowLevel/Mock/System/util/delay.h b/Tests/LowLevel/Mock/System/util/delay.h index 448123b8..7dc406d7 100644 --- a/Tests/LowLevel/Mock/System/util/delay.h +++ b/Tests/LowLevel/Mock/System/util/delay.h @@ -4,9 +4,9 @@ * @date 09.11.22 * Description here TODO */ -#ifndef FLIGHTCONTROLLER_DELAY_H -#define FLIGHTCONTROLLER_DELAY_H +#ifndef MOCK_UTIL_DELAY_H +#define MOCK_UTIL_DELAY_H void _delay_ms(int delay); -#endif // FLIGHTCONTROLLER_DELAY_H +#endif // MOCK_UTIL_DELAY_H