Skip to content

Commit 35c39d9

Browse files
refactor: Add CMake configuration for basic tests and integrate GTest
1 parent c17b323 commit 35c39d9

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

tests/CMakeLists.txt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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(basic)

tests/basic/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# tests/basic/CMakeLists.txt
2+
3+
enable_testing()
4+
5+
file(GLOB BASIC_TESTS "test_*.cpp")
6+
7+
add_executable(basic_tests ${BASIC_TESTS})
8+
9+
target_link_libraries(basic_tests PRIVATE GTest::gtest GTest::gmock GTest::gmock_main GTest::gtest_main)
10+
target_compile_features(basic_tests PRIVATE cxx_std_23)
11+
12+
target_link_libraries(basic_tests PRIVATE mcpplibs-primitives)
13+
14+
add_test(
15+
NAME BASIC_TESTS
16+
COMMAND $<TARGET_FILE:basic_tests>
17+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
18+
)

0 commit comments

Comments
 (0)