MyModernCppStarter/test/CMakeLists.txt
Lars Melchior 716a30d90b
Update Format.cmake (#29)
Now uses `find_program` to search for `clang-format`.
2020-04-23 12:04:02 +02:00

70 lines
1.7 KiB
CMake

cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(GreeterTests
LANGUAGES CXX
)
# ---- Options ----
option(ENABLE_TEST_COVERAGE "Enable test coverage" OFF)
option(TEST_INSTALLED_VERSION "Test the version found by find_package" OFF)
# ---- Dependencies ----
include(../cmake/CPM.cmake)
CPMAddPackage(
NAME doctest
GITHUB_REPOSITORY onqtam/doctest
GIT_TAG 2.3.7
)
if (TEST_INSTALLED_VERSION)
find_package(Greeter REQUIRED)
else()
CPMAddPackage(
NAME Greeter
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/..
)
endif()
CPMAddPackage(
NAME Format.cmake
GITHUB_REPOSITORY TheLartians/Format.cmake
VERSION 1.3
)
# ---- Create binary ----
file(GLOB sources CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp)
add_executable(GreeterTests ${sources})
target_link_libraries(GreeterTests doctest Greeter)
set_target_properties(GreeterTests PROPERTIES CXX_STANDARD 17)
# enable compiler warnings
if (NOT TEST_INSTALLED_VERSION)
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
target_compile_options(Greeter PUBLIC -Wall -pedantic -Wextra -Werror)
elseif(MSVC)
target_compile_options(Greeter PUBLIC /W4 /WX)
endif()
endif()
# ---- Add GreeterTests ----
ENABLE_TESTING()
# Note: doctest and similar testing frameworks can automatically configure CMake tests
# For other testing frameworks add the tests target instead:
# ADD_TEST(GreeterTests GreeterTests)
include(${doctest_SOURCE_DIR}/scripts/cmake/doctest.cmake)
doctest_discover_tests(GreeterTests)
# ---- code coverage ----
if (ENABLE_TEST_COVERAGE)
target_compile_options(Greeter PUBLIC -O0 -g -fprofile-arcs -ftest-coverage)
target_link_options(Greeter PUBLIC -fprofile-arcs -ftest-coverage)
endif()