Commit 0c3df9150cbf6c495803b3023e8e905390ee6eb4
Committed by
Henry Schreiner
1 parent
f0595285
Adding FetchContent on CMake 3.11
Showing
2 changed files
with
49 additions
and
16 deletions
cmake/AddGoogletest.cmake
| ... | ... | @@ -4,24 +4,39 @@ |
| 4 | 4 | # gives output on failed tests without having to set an environment variable. |
| 5 | 5 | # |
| 6 | 6 | # |
| 7 | -set(UPDATE_DISCONNECTED_IF_AVAILABLE "UPDATE_DISCONNECTED 1") | |
| 7 | +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) | |
| 8 | 8 | |
| 9 | -include(DownloadProject) | |
| 10 | -download_project(PROJ googletest | |
| 11 | - GIT_REPOSITORY https://github.com/google/googletest.git | |
| 12 | - GIT_TAG release-1.8.0 | |
| 13 | - UPDATE_DISCONNECTED 1 | |
| 14 | - QUIET | |
| 15 | -) | |
| 9 | +if(CMAKE_VERSION VERSION_LESS 3.11) | |
| 10 | + set(UPDATE_DISCONNECTED_IF_AVAILABLE "UPDATE_DISCONNECTED 1") | |
| 11 | + include(DownloadProject) | |
| 12 | + download_project(PROJ googletest | |
| 13 | + GIT_REPOSITORY https://github.com/google/googletest.git | |
| 14 | + GIT_TAG release-1.8.0 | |
| 15 | + UPDATE_DISCONNECTED 1 | |
| 16 | + QUIET | |
| 17 | + ) | |
| 18 | + | |
| 19 | + # CMake warning suppression will not be needed in version 1.9 | |
| 20 | + set(CMAKE_SUPPRESS_DEVELOPER_WARNINGS 1 CACHE BOOL "") | |
| 21 | + add_subdirectory(${googletest_SOURCE_DIR} ${googletest_SOURCE_DIR} EXCLUDE_FROM_ALL) | |
| 22 | + unset(CMAKE_SUPPRESS_DEVELOPER_WARNINGS) | |
| 23 | +else() | |
| 24 | + include(FetchContent) | |
| 25 | + FetchContent_Declare(googletest | |
| 26 | + GIT_REPOSITORY https://github.com/google/googletest.git | |
| 27 | + GIT_TAG release-1.8.0) | |
| 28 | + FetchContent_GetProperties(googletest) | |
| 29 | + if(NOT googletest_POPULATED) | |
| 30 | + FetchContent_Populate(googletest) | |
| 31 | + set(CMAKE_SUPPRESS_DEVELOPER_WARNINGS 1 CACHE BOOL "") | |
| 32 | + add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR} EXCLUDE_FROM_ALL) | |
| 33 | + unset(CMAKE_SUPPRESS_DEVELOPER_WARNINGS) | |
| 34 | + endif() | |
| 35 | +endif() | |
| 16 | 36 | |
| 17 | -set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) | |
| 18 | 37 | |
| 19 | -# CMake warning suppression will not be needed in version 1.9 | |
| 20 | -set(CMAKE_SUPPRESS_DEVELOPER_WARNINGS 1 CACHE BOOL "") | |
| 21 | -add_subdirectory(${googletest_SOURCE_DIR} ${googletest_SOURCE_DIR} EXCLUDE_FROM_ALL) | |
| 22 | -unset(CMAKE_SUPPRESS_DEVELOPER_WARNINGS) | |
| 23 | 38 | |
| 24 | -if (CMAKE_CONFIGURATION_TYPES) | |
| 39 | +if(CMAKE_CONFIGURATION_TYPES) | |
| 25 | 40 | add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} |
| 26 | 41 | --force-new-ctest-process --output-on-failure |
| 27 | 42 | --build-config "$<CONFIGURATION>") |
| ... | ... | @@ -54,16 +69,17 @@ macro(add_gtest TESTNAME) |
| 54 | 69 | gtest_add_tests(TARGET ${TESTNAME} |
| 55 | 70 | TEST_PREFIX "${TESTNAME}." |
| 56 | 71 | TEST_LIST TmpTestList) |
| 72 | + set_tests_properties(${TmpTestList} PROPERTIES FOLDER "Tests") | |
| 57 | 73 | else() |
| 58 | 74 | gtest_discover_tests(${TESTNAME} |
| 59 | 75 | TEST_PREFIX "${TESTNAME}." |
| 60 | - ) | |
| 76 | + PROPERTIES FOLDER "Tests") | |
| 61 | 77 | |
| 62 | 78 | endif() |
| 63 | 79 | else() |
| 64 | 80 | add_test(${TESTNAME} ${TESTNAME}) |
| 81 | + set_target_properties(${TESTNAME} PROPERTIES FOLDER "Tests") | |
| 65 | 82 | endif() |
| 66 | - set_target_properties(${TESTNAME} PROPERTIES FOLDER "Tests") | |
| 67 | 83 | |
| 68 | 84 | endmacro() |
| 69 | 85 | ... | ... |
tests/AppTest.cpp
| ... | ... | @@ -353,6 +353,23 @@ TEST_F(TApp, MissingValueMoreThan) { |
| 353 | 353 | EXPECT_THROW(run(), CLI::ArgumentMismatch); |
| 354 | 354 | } |
| 355 | 355 | |
| 356 | +TEST_F(TApp, NoMissingValueMoreThan) { | |
| 357 | + std::vector<int> vals1; | |
| 358 | + std::vector<int> vals2; | |
| 359 | + app.add_option("-v", vals1)->expected(-2); | |
| 360 | + app.add_option("--vals", vals2)->expected(-2); | |
| 361 | + | |
| 362 | + args = {"-v", "2", "3", "4"}; | |
| 363 | + run(); | |
| 364 | + EXPECT_EQ(vals1, std::vector<int>({2,3,4})); | |
| 365 | + | |
| 366 | + app.reset(); | |
| 367 | + | |
| 368 | + args = {"--vals", "2", "3", "4"}; | |
| 369 | + run(); | |
| 370 | + EXPECT_EQ(vals2, std::vector<int>({2,3,4})); | |
| 371 | +} | |
| 372 | + | |
| 356 | 373 | TEST_F(TApp, NotRequiredOptsSingle) { |
| 357 | 374 | |
| 358 | 375 | std::string str; | ... | ... |