Skip to content

Commit 968473c

Browse files
committed
Problem: no test infrastructure
Solution: integrate googletest, with non-cppzmq test case for a start
1 parent 3937983 commit 968473c

File tree

8 files changed

+146
-5
lines changed

8 files changed

+146
-5
lines changed

ci_build.sh

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@ if [ "${ZMQ_VERSION}" != "" ] ; then install_zeromq ; fi
2525

2626
# build cppzmq
2727

28+
pushd .
2829
mkdir build
2930
cd build
3031
cmake ..
3132
sudo make -j4 install
33+
popd
3234

3335
# build cppzmq tests
34-
# cd tests
35-
# mkdir build
36-
# cd build
37-
# cmake ..
38-
# make -j5 test ARGS="-V"
36+
cd tests
37+
mkdir build
38+
cd build
39+
cmake ..
40+
cmake --build .
41+
ctest

external/gtest-demo/INFO.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
googletest integration into CMake and travis-ci was based on https://github.com/bast/gtest-demo

external/gtest-demo/LICENSE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright (c) 2015-2018, Radovan Bast
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
10+
* Redistributions in binary form must reproduce the above copyright notice,
11+
this list of conditions and the following disclaimer in the documentation
12+
and/or other materials provided with the distribution.
13+
14+
* Neither the name of gtest-demo nor the names of its
15+
contributors may be used to endorse or promote products derived from
16+
this software without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

tests/CMakeLists.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
2+
3+
project(cppzmq-test CXX)
4+
5+
# place binaries and libraries according to GNU standards
6+
# TODO check if we should do this
7+
8+
# include(GNUInstallDirs)
9+
# set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
10+
# set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
11+
# set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
12+
13+
# we use this to get code coverage
14+
if(CMAKE_CXX_COMPILER_ID MATCHES GNU)
15+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
16+
endif()
17+
18+
include(cmake/googletest.cmake)
19+
fetch_googletest(
20+
${PROJECT_SOURCE_DIR}/cmake
21+
${PROJECT_BINARY_DIR}/googletest
22+
)
23+
24+
#find_package(cppzmq)
25+
26+
enable_testing()
27+
28+
add_executable(
29+
unit_tests
30+
example_add.cpp
31+
)
32+
33+
target_link_libraries(
34+
unit_tests
35+
gtest_main
36+
)
37+
38+
add_test(
39+
NAME
40+
unit
41+
COMMAND
42+
${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}/unit_tests
43+
)

tests/cmake/googletest-download.cmake

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# code copied from https://crascit.com/2015/07/25/cmake-gtest/
2+
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
3+
4+
project(googletest-download NONE)
5+
6+
include(ExternalProject)
7+
8+
ExternalProject_Add(
9+
googletest
10+
SOURCE_DIR "@GOOGLETEST_DOWNLOAD_ROOT@/googletest-src"
11+
BINARY_DIR "@GOOGLETEST_DOWNLOAD_ROOT@/googletest-build"
12+
GIT_REPOSITORY
13+
https://github.com/google/googletest.git
14+
GIT_TAG
15+
release-1.8.0
16+
CONFIGURE_COMMAND ""
17+
BUILD_COMMAND ""
18+
INSTALL_COMMAND ""
19+
TEST_COMMAND ""
20+
)

tests/cmake/googletest.cmake

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# the following code to fetch googletest
2+
# is inspired by and adapted after https://crascit.com/2015/07/25/cmake-gtest/
3+
# download and unpack googletest at configure time
4+
5+
macro(fetch_googletest _download_module_path _download_root)
6+
set(GOOGLETEST_DOWNLOAD_ROOT ${_download_root})
7+
configure_file(
8+
${_download_module_path}/googletest-download.cmake
9+
${_download_root}/CMakeLists.txt
10+
@ONLY
11+
)
12+
unset(GOOGLETEST_DOWNLOAD_ROOT)
13+
14+
execute_process(
15+
COMMAND
16+
"${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" .
17+
WORKING_DIRECTORY
18+
${_download_root}
19+
)
20+
execute_process(
21+
COMMAND
22+
"${CMAKE_COMMAND}" --build .
23+
WORKING_DIRECTORY
24+
${_download_root}
25+
)
26+
27+
# adds the targers: gtest, gtest_main, gmock, gmock_main
28+
add_subdirectory(
29+
${_download_root}/googletest-src
30+
${_download_root}/googletest-build
31+
)
32+
endmacro()

tests/example_add.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "gtest/gtest.h"
2+
3+
TEST(example, add)
4+
{
5+
double res;
6+
res = 1.0 + 2.0;
7+
ASSERT_NEAR(res, 3.0, 1.0e-11);
8+
}

tests/main.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "gtest/gtest.h"
2+
3+
int main(int argc, char** argv)
4+
{
5+
::testing::InitGoogleTest(&argc, argv);
6+
return RUN_ALL_TESTS();
7+
}

0 commit comments

Comments
 (0)