Skip to content

Commit 37f1b51

Browse files
committed
Add helper function to build unittest
1 parent dde5732 commit 37f1b51

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

CMake/ystdlib-cpp-helpers.cmake

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,29 @@ function(cpp_library)
3333
target_compile_features(${arg_cpp_lib_NAME} INTERFACE cxx_std_20)
3434
add_library(${arg_cpp_lib_NAMESPACE}::${arg_cpp_lib_NAME} ALIAS ${arg_cpp_lib_NAME})
3535
endfunction()
36+
37+
# Adds the current C++ library's unit test source files and dependencies to global list variables
38+
# used to build the Catch2 unit test target.
39+
# NOTE: There's no need to specify Catch2 related dependencies.
40+
#
41+
# @param SRCS
42+
# @param DEPS
43+
function(cpp_test)
44+
set(options "")
45+
set(oneValueArgs "")
46+
set(multiValueArgs
47+
SRCS
48+
DEPS
49+
)
50+
cmake_parse_arguments(arg_cpp_test "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
51+
52+
file(RELATIVE_PATH SRC_DIR "${PROJECT_SOURCE_DIR}" "${CMAKE_CURRENT_LIST_DIR}")
53+
54+
foreach(SRC_FILE IN LISTS arg_cpp_test_SRCS)
55+
list(APPEND SOURCE_FILES_unitTest ${SRC_DIR}/${SRC_FILE})
56+
endforeach()
57+
set(SOURCE_FILES_unitTest "${SOURCE_FILES_unitTest}" CACHE INTERNAL "")
58+
59+
list(APPEND LIB_DEPS_unitTest "${arg_cpp_test_DEPS}")
60+
set(LIB_DEPS_unitTest "${LIB_DEPS_unitTest}" CACHE INTERNAL "")
61+
endfunction()

CMakeLists.txt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@ endif()
2222
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/CMake)
2323
include(ystdlib-cpp-helpers)
2424

25+
# Initiate unit test global list variables
26+
set(SOURCE_FILES_unitTest "" CACHE INTERNAL "")
27+
set(LIB_DEPS_unitTest "Catch2::Catch2WithMain" CACHE INTERNAL "")
28+
2529
add_subdirectory(src/ystdlib)
2630

2731
add_executable(unitTest)
28-
target_sources(unitTest PRIVATE src/main.cpp)
29-
target_link_libraries(
30-
unitTest
31-
PRIVATE
32-
ystdlib::testlib
33-
Catch2::Catch2WithMain
34-
)
32+
target_sources(unitTest PRIVATE ${SOURCE_FILES_unitTest})
33+
target_link_libraries(unitTest PRIVATE ${LIB_DEPS_unitTest})
3534
target_compile_features(unitTest PRIVATE cxx_std_20)

src/ystdlib/testlib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
cpp_library(NAME testlib NAMESPACE ystdlib)
2+
cpp_test(SRCS test-hello.cpp DEPS ystdlib::testlib)
File renamed without changes.

0 commit comments

Comments
 (0)