mirror of
1
0
Fork 0

Merge branch 'example/fetch' into 'master'

feat: new example

See merge request CLIUtils/modern-cmake!40
This commit is contained in:
Henry Schreiner 2020-08-05 20:21:04 +00:00
commit a0e2335253
5 changed files with 39 additions and 4 deletions

View File

@ -14,7 +14,7 @@ For example, to download Catch2:
FetchContent_Declare(
catch
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v2.9.1
GIT_TAG v2.13.0
)
# CMake 3.14+
@ -48,4 +48,6 @@ endif()
Now you have the CMake 3.14+ syntax in CMake 3.11+.
See the example [here](https://gitlab.com/CLIUtils/modern-cmake/-/tree/master/examples/fetch).
[FetchContent]: https://cmake.org/cmake/help/latest/module/FetchContent.html

View File

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.11...3.16)
cmake_minimum_required(VERSION 3.11...3.18)
project(ModernCMakeExamples)
set(MODERN_CMAKE_BUILD_TESTING ON)
@ -7,6 +7,7 @@ include(CTest)
add_subdirectory(simple-project)
add_subdirectory(extended-project)
add_subdirectory(fetch)
add_subdirectory(root-usefile)
add_subdirectory(root-simple)

View File

@ -1,9 +1,9 @@
# Testing library
FetchContent_Declare(
catch2
catch
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v2.9.1)
FetchContent_MakeAvailable(catch2)
FetchContent_MakeAvailable(catch)
# Adds Catch2::Catch2
# Tests need to be added as executables first

View File

@ -0,0 +1,19 @@
cmake_minimum_required(VERSION 3.14...3.18)
project(FetchExample LANGUAGES CXX)
include(FetchContent)
include(CTest)
FetchContent_Declare(
catch
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v2.13.0
)
# CMake 3.14+
FetchContent_MakeAvailable(catch)
add_executable(fetch_example main.cpp)
target_link_libraries(fetch_example PRIVATE Catch2::Catch2)
add_test(NAME fetch_example COMMAND fetch_example)

13
examples/fetch/main.cpp Normal file
View File

@ -0,0 +1,13 @@
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#include "catch2/catch.hpp"
unsigned int Factorial( unsigned int number ) {
return number <= 1 ? number : Factorial(number-1)*number;
}
TEST_CASE( "Factorials are computed", "[factorial]" ) {
REQUIRE( Factorial(1) == 1 );
REQUIRE( Factorial(2) == 2 );
REQUIRE( Factorial(3) == 6 );
REQUIRE( Factorial(10) == 3628800 );
}