File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -18,13 +18,15 @@ target_sources(mcpplibs-templates
1818 ${MODULE_SOURCES}
1919)
2020
21- # Test
22- add_executable (templates_test tests/main.cpp )
23- target_link_libraries (templates_test PRIVATE mcpplibs-templates )
24-
25- enable_testing ()
26- add_test (NAME mcpplibs-templates-test COMMAND templates_test )
27-
2821# Example
2922add_executable (basic examples/basic.cpp )
3023target_link_libraries (basic PRIVATE mcpplibs-templates )
24+
25+ # Test
26+ option (BUILD_TESTING "Build the testing directories" ON )
27+
28+ if (BUILD_TESTING)
29+ include (CTest )
30+ enable_testing ()
31+ add_subdirectory (tests )
32+ endif ()
Original file line number Diff line number Diff line change 1+ enable_testing ()
2+
3+ if (NOT TARGET GTest::gtest)
4+ include (FetchContent )
5+
6+ set (GTEST_VERSION_STABLE release-1.12.1 CACHE STRING "Stable GTest version" )
7+ set (GTEST_VERSION_LATEST v1.16.0 CACHE STRING "Latest GTest version" )
8+
9+ if (NOT DEFINED GTEST_REPO)
10+ set (REPO_PRIORITY_LIST
11+ "git@github.com:google/googletest.git"
12+ "https://github.com/google/googletest.git"
13+ "https://gitee.com/mirrors/googletest.git"
14+ )
15+
16+ foreach (repo IN LISTS REPO_PRIORITY_LIST)
17+ execute_process (
18+ COMMAND git ls-remote ${repo} -q
19+ RESULT_VARIABLE result
20+ OUTPUT_QUIET
21+ ERROR_QUIET
22+ TIMEOUT 5
23+ )
24+ if (result EQUAL 0)
25+ set (GTEST_REPO ${repo} CACHE STRING "Selected GTest repository" )
26+ set (GTEST_VERSION ${GTEST_VERSION_LATEST} CACHE STRING "Selected GTest version" )
27+ message (STATUS "Selected GTest repository: ${repo} " )
28+ break ()
29+ endif ()
30+ endforeach ()
31+
32+ if (NOT GTEST_REPO)
33+ message (FATAL_ERROR "All GTest repositories are unavailable!" )
34+ endif ()
35+ endif ()
36+
37+ FetchContent_Declare (
38+ GTest
39+ GIT_REPOSITORY ${GTEST_REPO}
40+ GIT_TAG ${GTEST_VERSION}
41+ GIT_SHALLOW TRUE
42+ )
43+
44+ FetchContent_MakeAvailable (GTest)
45+ endif ()
46+
47+ add_subdirectory (main )
Original file line number Diff line number Diff line change 1+ enable_testing ()
2+
3+ file (GLOB TESTS_SRC "*.cpp" )
4+
5+ set (TEST_EXECUTABLE_NAME main_tests)
6+ add_executable (${TEST_EXECUTABLE_NAME} ${TESTS_SRC} )
7+
8+ target_link_libraries (${TEST_EXECUTABLE_NAME} PRIVATE GTest::gtest GTest::gmock GTest::gmock_main GTest::gtest_main )
9+ target_compile_features (${TEST_EXECUTABLE_NAME} PRIVATE cxx_std_23 )
10+
11+ target_link_libraries (${TEST_EXECUTABLE_NAME} PRIVATE ${PROJECT_NAME} )
12+
13+ add_test (
14+ NAME ${TEST_EXECUTABLE_NAME}
15+ COMMAND $<TARGET_FILE :${TEST_EXECUTABLE_NAME} >
16+ WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
17+ )
You can’t perform that action at this time.
0 commit comments