Skip to content

Commit 1343ca9

Browse files
committed
added tests in cmake
1 parent e184a48 commit 1343ca9

File tree

4 files changed

+52
-15
lines changed

4 files changed

+52
-15
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
1111
add_library(${PROJECT_NAME} INTERFACE)
1212

1313
target_include_directories(${PROJECT_NAME} INTERFACE ${${PROJECT_NAME}_SOURCE_DIR})
14-
14+
enable_testing()
15+
add_subdirectory(samples)
1516
add_subdirectory(tests)
1617

17-
add_subdirectory(samples)
1818

1919
install(TARGETS circularbuffer DESTINATION lib)

tests/CMakeLists.txt

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
11
cmake_minimum_required(VERSION 3.13)
22

3-
message(STATUS "cmake in tests, project name ${PROJECT_NAME}" )
4-
53
set(CMAKE_CXX_STANDARD 11)
64
set(CMAKE_CXX_STANDARD_REQUIRED ON)
75

8-
add_executable(test_circularbuffer_int test_circularbuffer_int.cpp)
9-
target_link_libraries(test_circularbuffer_int
10-
PRIVATE
6+
message(STATUS "cmake in tests, project name ${PROJECT_NAME}" )
7+
8+
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
9+
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
10+
RESULT_VARIABLE result
11+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download
12+
)
13+
if(result)
14+
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
15+
endif()
16+
17+
execute_process(COMMAND ${CMAKE_COMMAND} --build .
18+
RESULT_VARIABLE result
19+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download
20+
)
21+
22+
if(result)
23+
message(FATAL_ERROR "Build step for googletest failed: ${result}")
24+
endif()
25+
26+
# Prevent overriding the parent project's compiler/linker
27+
# settings on Windows
28+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
29+
30+
# Add googletest directly to our build. This defines
31+
# the gtest and gtest_main targets.
32+
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
33+
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
34+
EXCLUDE_FROM_ALL)
35+
36+
add_executable(circularbuffer_int circularbuffer_int.cpp)
37+
target_link_libraries(circularbuffer_int
1138
circularbuffer
12-
/usr/local/lib/libgtest.a
13-
pthread
39+
gtest_main
1440
)
41+
add_test(NAME test_circularbuffer_int COMMAND circularbuffer_int)

tests/CMakeLists.txt.in

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
3+
project(googletest-download NONE)
4+
5+
include(ExternalProject)
6+
ExternalProject_Add(googletest
7+
GIT_REPOSITORY https://github.com/google/googletest.git
8+
GIT_TAG master
9+
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
10+
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
11+
CONFIGURE_COMMAND ""
12+
BUILD_COMMAND ""
13+
INSTALL_COMMAND ""
14+
TEST_COMMAND ""
15+
)

tests/test_circularbuffer_int.cpp renamed to tests/circularbuffer_int.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,4 @@ TEST_F(CircularBufferTest, PushAndPopTest){
9090
}
9191

9292

93-
int main(int argc, char *argv[])
94-
{
95-
96-
::testing::InitGoogleTest(&argc, argv);
97-
return RUN_ALL_TESTS();
98-
}
93+

0 commit comments

Comments
 (0)