From 33f3a5e1a340a96d2481d2ef62d12bb26fa8d1bb Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 5 Oct 2017 00:04:52 +0100 Subject: Add GTest library to build and basic unit tests --- CMakeLists.txt | 24 ++++++++++++++++++++---- build-instructions.md | 3 ++- clean-build.sh | 6 ++++-- conanfile.txt | 4 ++++ src/test/all_tests.cpp | 10 ++++++++++ src/test/main_test.cpp | 19 +++++++++++++++++++ 6 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 src/test/all_tests.cpp create mode 100644 src/test/main_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d70caad..0ff92bb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,9 +12,8 @@ set(CMAKE_CXX_STANDARD 14) # these instructions search the directory tree when cmake is # invoked and put all files that match the pattern in the variables # `sources` and `data` -file(GLOB_RECURSE sources src/main/*.cpp src/main/*.h) -file(GLOB_RECURSE sources_test src/test/*.cpp) -file(GLOB_RECURSE data resources/*) +set(sources src/main/main.cpp) +set(sources_test src/test/all_tests.cpp src/test/main_test.cpp) ############################################################################### ## conan.io setup ############################################################# @@ -32,7 +31,7 @@ endif() ############################################################################### # add the data to the target, so it becomes visible in some IDE -add_executable(solitaire_pip ${sources} ${data}) +add_executable(solitaire_pip ${sources}) # just for example add some compiler flags target_compile_options(solitaire_pip PUBLIC -std=c++1y -Wall -Wfloat-conversion) @@ -47,6 +46,23 @@ target_link_libraries(solitaire_pip ${CONAN_LIBS}) # we need this, because we want to work with paths relative to the executable file(COPY ${data} DESTINATION resources) +############################################################################### +## testing #################################################################### +############################################################################### + +add_executable(unit_tests ${sources_test}) + +# this allows us to use our executable as a link library +# therefore we can inherit all compiler options and library dependencies +set_target_properties(solitaire_pip PROPERTIES ENABLE_EXPORTS on) + +target_link_libraries(unit_tests ${CONAN_LIBS} solitaire_pip) + +ENABLE_TESTING() +ADD_TEST(NAME unit_tests + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin + COMMAND unit_tests) + ############################################################################### ## packaging ################################################################## ############################################################################### diff --git a/build-instructions.md b/build-instructions.md index 8ecb321..1240c7b 100644 --- a/build-instructions.md +++ b/build-instructions.md @@ -5,7 +5,8 @@ building the project, running the program, and creating an installer. ### Installing dependencies -`cd cmake-build-[debug|release]; conan install ..; cd ..` +`cd cmake-build-[debug|release]; conan install .. --build missing -s \ +compiler=gcc -s compiler.version=6.3 -s compiler.libcxx=libstdc++11; cd ..` ### Generate Build System diff --git a/clean-build.sh b/clean-build.sh index b9847a3..d566226 100755 --- a/clean-build.sh +++ b/clean-build.sh @@ -4,8 +4,10 @@ echo "Cleaning..." rm -r cmake-build-release/*; rm -r cmake-build-debug/* echo "Installing..." -cd cmake-build-debug; conan install ..; cd .. -cd cmake-build-release; conan install ..; cd .. +cd cmake-build-debug; conan install .. --build missing -s compiler=gcc \ + -s compiler.version=6.3 -s compiler.libcxx=libstdc++11; cd .. +cd cmake-build-release; conan install .. --build missing -s compiler=gcc \ + -s compiler.version=6.3 -s compiler.libcxx=libstdc++11; cd .. echo "Generating build..." cmake -Bcmake-build-debug -H. diff --git a/conanfile.txt b/conanfile.txt index 2bcb821..f181ce5 100644 --- a/conanfile.txt +++ b/conanfile.txt @@ -1,5 +1,9 @@ [requires] Boost/1.60.0@lasote/stable +gtest/1.8.0@lasote/stable + +[options] +gtest:shared=False [generators] cmake diff --git a/src/test/all_tests.cpp b/src/test/all_tests.cpp new file mode 100644 index 0000000..7dce333 --- /dev/null +++ b/src/test/all_tests.cpp @@ -0,0 +1,10 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include + +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/src/test/main_test.cpp b/src/test/main_test.cpp new file mode 100644 index 0000000..3f2081f --- /dev/null +++ b/src/test/main_test.cpp @@ -0,0 +1,19 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include + +const int actualValTrue = 4; +const int actualValFalse = 8; +const int expectVal = 4; + +namespace { + TEST(StrCompare, CStrEqual) { + EXPECT_EQ(expectVal, actualValTrue); + } + + TEST(StrCompare, CStrNotEqual) { + EXPECT_NE(expectVal, actualValFalse); + } +} -- cgit v1.2.1