feat: Add Unittest example
This commit is contained in:
@@ -21,3 +21,12 @@ target_compile_features(hello PRIVATE cxx_std_23)
|
|||||||
target_compile_options(hello PRIVATE ${BASE_OPTIONS})
|
target_compile_options(hello PRIVATE ${BASE_OPTIONS})
|
||||||
target_compile_definitions(hello PRIVATE ${BASE_DEFINITIONS})
|
target_compile_definitions(hello PRIVATE ${BASE_DEFINITIONS})
|
||||||
target_link_libraries(hello PRIVATE fmt::fmt)
|
target_link_libraries(hello PRIVATE fmt::fmt)
|
||||||
|
|
||||||
|
option(BUILD_TESTING OFF "Build Unity/CMock test suites")
|
||||||
|
if (BUILD_TESTING AND NOT IS_EMSCRIPTEN)
|
||||||
|
set(CMAKE_SKIP_TEST_ALL_DEPENDENCY FALSE)
|
||||||
|
enable_testing()
|
||||||
|
find_package(Unity REQUIRED)
|
||||||
|
find_package(CMock REQUIRED)
|
||||||
|
add_subdirectory(tests)
|
||||||
|
endif()
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
set(MOCK_GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/mocks")
|
||||||
|
file(MAKE_DIRECTORY "${MOCK_GEN_DIR}")
|
||||||
|
|
||||||
|
function(cmock_generate_mock target header)
|
||||||
|
cmake_parse_arguments(ARG "" "" "CONFIG" ${ARGN})
|
||||||
|
if (NOT RUBY_EXECUTABLE)
|
||||||
|
message(FATAL_ERROR "Ruby is required for CMock generation")
|
||||||
|
endif()
|
||||||
|
get_filename_component(name "${header}" NAME_WE)
|
||||||
|
get_filename_component(header_dir "${header}" DIRECTORY)
|
||||||
|
set(mock_src "${MOCK_GEN_DIR}/Mock${name}.c")
|
||||||
|
set(mock_hdr "${MOCK_GEN_DIR}/Mock${name}.h")
|
||||||
|
if (ARG_CONFIG)
|
||||||
|
set(CMOCK_CONFIG_FLAG "-o${ARG_CONFIG}")
|
||||||
|
set(CMOCK_CONFIG_DEPENDS "${ARG_CONFIG}")
|
||||||
|
endif()
|
||||||
|
add_custom_command(
|
||||||
|
OUTPUT "${mock_src}" "${mock_hdr}"
|
||||||
|
COMMAND "${RUBY_EXECUTABLE}" "${CMOCK_SCRIPT}"
|
||||||
|
${CMOCK_CONFIG_FLAG}
|
||||||
|
"--mock_path=${MOCK_GEN_DIR}"
|
||||||
|
"${header}"
|
||||||
|
DEPENDS "${header}" ${CMOCK_CONFIG_DEPENDS}
|
||||||
|
COMMENT "CMock: generating Mock${name}"
|
||||||
|
VERBATIM
|
||||||
|
)
|
||||||
|
target_sources("${target}" PRIVATE "${mock_src}")
|
||||||
|
target_include_directories("${target}" PRIVATE "${MOCK_GEN_DIR}" "${header_dir}")
|
||||||
|
endfunction()
|
||||||
|
|
||||||
|
set(TEST_TARGETS "")
|
||||||
|
|
||||||
|
find_package(asio REQUIRED)
|
||||||
|
|
||||||
|
add_executable(test_example test_example.cpp)
|
||||||
|
target_include_directories(test_example PRIVATE "${CMAKE_SOURCE_DIR}")
|
||||||
|
target_link_libraries(test_example PRIVATE Unity::Unity)
|
||||||
|
target_compile_features(test_example PRIVATE cxx_std_23)
|
||||||
|
add_test(NAME test_example COMMAND test_example)
|
||||||
|
list(APPEND TEST_TARGETS test_example)
|
||||||
|
|
||||||
|
add_custom_target(check
|
||||||
|
COMMAND ${CMAKE_CTEST_COMMAND}
|
||||||
|
--test-dir "${CMAKE_BINARY_DIR}"
|
||||||
|
--output-on-failure
|
||||||
|
--progress
|
||||||
|
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
|
||||||
|
USES_TERMINAL
|
||||||
|
DEPENDS ${TEST_TARGETS}
|
||||||
|
)
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
#include "unity.h"
|
||||||
|
|
||||||
|
void setUp() {}
|
||||||
|
void tearDown() {}
|
||||||
|
|
||||||
|
void test_example() {
|
||||||
|
TEST_ASSERT_TRUE(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto main() -> int {
|
||||||
|
UNITY_BEGIN();
|
||||||
|
RUN_TEST(test_example);
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user