Skip to content

Commit

Permalink
#45: Refactoring of mocking lib for extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
aul12 committed Jan 1, 2023
1 parent 81571a0 commit 5d053ce
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 58 deletions.
17 changes: 0 additions & 17 deletions Tests/LowLevel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
23 changes: 7 additions & 16 deletions Tests/LowLevel/Mock/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -62,15 +52,16 @@ 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})

# Include mocked system directory
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})
Expand All @@ -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()

6 changes: 3 additions & 3 deletions Tests/LowLevel/Mock/Lib/Base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cassert>
#include <stdexcept>
Expand Down Expand Up @@ -81,4 +81,4 @@ namespace mock {
};
} // namespace mock

#endif // FLIGHTCONTROLLER_MOCKBASE_HPP
#endif // MOCK_BASE_HPP
12 changes: 11 additions & 1 deletion Tests/LowLevel/Mock/Lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 3 additions & 3 deletions Tests/LowLevel/Mock/Lib/FunctionInfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <functional>
#include <optional>
Expand Down Expand Up @@ -39,4 +39,4 @@ namespace mock {
using GetFunctionInfo = decltype(getFunctionInfoImpl(fun));
} // namespace mock

#endif // FLIGHTCONTROLLER_FUNCTIONINFO_HPP
#endif // MOCK_FUNCTIONINFO_HPP
6 changes: 3 additions & 3 deletions Tests/LowLevel/Mock/Lib/Handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cassert>
#include <gtest/gtest.h>
Expand Down Expand Up @@ -103,4 +103,4 @@ namespace mock {
};
} // namespace mock

#endif // FLIGHTCONTROLLER_HANDLER_HPP
#endif // MOCK_HANDLER_HPP
6 changes: 3 additions & 3 deletions Tests/LowLevel/Mock/Lib/Util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tuple>
#include <type_traits>
Expand Down Expand Up @@ -88,4 +88,4 @@ namespace mock::util {
}
} // namespace mock::util

#endif // FLIGHTCONTROLLER_MOCKUTIL_HPP
#endif // MOCK_UTIL_HPP
6 changes: 3 additions & 3 deletions Tests/LowLevel/Mock/System/avr/interrupt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions Tests/LowLevel/Mock/System/avr/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdint.h>

Expand All @@ -27,4 +27,4 @@ extern uint8_t MCUSR;

enum { WDRF, BORF };

#endif // FLIGHTCONTROLLER_IO_H
#endif // MOCK_AVR_IO_H
6 changes: 3 additions & 3 deletions Tests/LowLevel/Mock/System/avr/wdt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
6 changes: 3 additions & 3 deletions Tests/LowLevel/Mock/System/util/delay.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5d053ce

Please sign in to comment.