From 8e4fd15a231fe52d76661ccb3c04ae4fe4645480 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 9 Mar 2023 12:03:03 -0800 Subject: [PATCH] fix(tests): experimental support for external Catch3 (#827) * fix(tests): experimental support for external Catch3 Signed-off-by: Henry Schreiner * style: pre-commit.ci fixes --------- Signed-off-by: Henry Schreiner Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .gitignore | 3 +++ tests/AppTest.cpp | 2 -- tests/CMakeLists.txt | 23 +++++++++++++++-------- tests/ComplexTypeTest.cpp | 2 -- tests/ConfigFileTest.cpp | 2 -- tests/DeprecatedTest.cpp | 2 -- tests/FormatterTest.cpp | 2 -- tests/HelpTest.cpp | 4 +--- tests/NewParseTest.cpp | 2 -- tests/OptionGroupTest.cpp | 2 -- tests/SubcommandTest.cpp | 2 -- tests/TimerTest.cpp | 2 -- tests/catch.hpp | 22 ++++++++++++++++++++++ tests/meson.build | 25 ++++++++++++++++--------- 14 files changed, 57 insertions(+), 38 deletions(-) diff --git a/.gitignore b/.gitignore index 6f8a8d04..7b9bcb27 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,6 @@ a.out* /package.json /yarn.lock /CLI11.hpp + +/subprojects/Catch2-* +/subprojects/packagecache diff --git a/tests/AppTest.cpp b/tests/AppTest.cpp index 5a944b13..2cdefc4e 100644 --- a/tests/AppTest.cpp +++ b/tests/AppTest.cpp @@ -142,8 +142,6 @@ TEST_CASE_METHOD(TApp, "StrangeFlagNames", "[app]") { } TEST_CASE_METHOD(TApp, "RequireOptionsError", "[app]") { - using Catch::Matchers::Contains; - app.add_flag("-c"); app.add_flag("--q"); app.add_flag("--this,--that"); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 4952e486..7bd47744 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -66,32 +66,39 @@ endif() set(CLI11_MULTIONLY_TESTS TimerTest) -add_library(catch_main main.cpp catch.hpp) -target_include_directories(catch_main PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}") - find_package(Catch2 CONFIG) if(Catch2_FOUND) if(NOT TARGET Catch2::Catch2) message(FATAL_ERROR "Found Catch2 at ${Catch2_DIR} but targets are missing.") endif() - message(STATUS "Found Catch2") - target_link_libraries(catch_main PUBLIC Catch2::Catch2) + message(STATUS "Found Catch2 ${Catch2_VERSION}") + + if(Catch2_VERSION VERSION_LESS 3) + add_library(catch_main main.cpp catch.hpp) + target_include_directories(catch_main PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}") + target_link_libraries(catch_main PUBLIC Catch2::Catch2) + else() + add_library(catch_main ALIAS Catch2::Catch2WithMain) + target_compile_definitions(Catch2::Catch2WithMain INTERFACE -DCLI11_CATCH3) + endif() else() message(STATUS "Downloading Catch2") # FetchContent would be better, but requires newer CMake. file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/catch2") - set(url https://github.com/philsquared/Catch/releases/download/v2.13.7/catch.hpp) + set(url https://github.com/philsquared/Catch/releases/download/v2.13.10/catch.hpp) file( DOWNLOAD ${url} "${CMAKE_CURRENT_BINARY_DIR}/catch2/catch.hpp" STATUS status - EXPECTED_HASH SHA256=ea379c4a3cb5799027b1eb451163dff065a3d641aaba23bf4e24ee6b536bd9bc) + EXPECTED_HASH SHA256=3725c0f0a75f376a5005dde31ead0feb8f7da7507644c201b814443de8355170) list(GET status 0 error) if(error) message(FATAL_ERROR "Could not download ${url}, and Catch2 not found on your system.") endif() - target_include_directories(catch_main PUBLIC "${CMAKE_CURRENT_BINARY_DIR}") + add_library(catch_main main.cpp catch.hpp) + target_include_directories(catch_main PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}" + "${CMAKE_CURRENT_BINARY_DIR}") endif() # Add special target that copies the data directory for tests diff --git a/tests/ComplexTypeTest.cpp b/tests/ComplexTypeTest.cpp index 2c3e156f..adcd26c4 100644 --- a/tests/ComplexTypeTest.cpp +++ b/tests/ComplexTypeTest.cpp @@ -9,8 +9,6 @@ #include #include -using Catch::Matchers::Contains; - using cx = std::complex; CLI::Option * diff --git a/tests/ConfigFileTest.cpp b/tests/ConfigFileTest.cpp index b043b110..20687272 100644 --- a/tests/ConfigFileTest.cpp +++ b/tests/ConfigFileTest.cpp @@ -9,8 +9,6 @@ #include #include -using Catch::Matchers::Contains; - TEST_CASE("StringBased: convert_arg_for_ini", "[config]") { CHECK("\"\"" == CLI::detail::convert_arg_for_ini(std::string{})); diff --git a/tests/DeprecatedTest.cpp b/tests/DeprecatedTest.cpp index 2a823352..063c67e5 100644 --- a/tests/DeprecatedTest.cpp +++ b/tests/DeprecatedTest.cpp @@ -6,8 +6,6 @@ #include "app_helper.hpp" -using Catch::Matchers::Contains; - TEST_CASE("Deprecated: Empty", "[deprecated]") { // No deprecated features at this time. CHECK(true); diff --git a/tests/FormatterTest.cpp b/tests/FormatterTest.cpp index 641fd409..2563c942 100644 --- a/tests/FormatterTest.cpp +++ b/tests/FormatterTest.cpp @@ -13,8 +13,6 @@ #include "catch.hpp" #include -using Catch::Matchers::Contains; - class SimpleFormatter : public CLI::FormatterBase { public: SimpleFormatter() : FormatterBase() {} diff --git a/tests/HelpTest.cpp b/tests/HelpTest.cpp index 7550959f..c4403f75 100644 --- a/tests/HelpTest.cpp +++ b/tests/HelpTest.cpp @@ -13,8 +13,6 @@ #include "catch.hpp" #include -using Catch::Matchers::Contains; - TEST_CASE("THelp: Basic", "[help]") { CLI::App app{"My prog"}; @@ -1313,7 +1311,7 @@ TEST_CASE("TVersion: parse_throw", "[help]") { try { app.parse("--Version"); } catch(const CLI::CallForVersion &v) { - CHECK_THAT(CLI11_VERSION, Catch::Equals(v.what())); + CHECK_THAT(CLI11_VERSION, Equals(v.what())); CHECK(0 == v.get_exit_code()); const auto &appc = app; const auto *cptr = appc.get_version_ptr(); diff --git a/tests/NewParseTest.cpp b/tests/NewParseTest.cpp index 503d0290..a72af823 100644 --- a/tests/NewParseTest.cpp +++ b/tests/NewParseTest.cpp @@ -10,8 +10,6 @@ #include #include -using Catch::Matchers::Contains; - using cx = std::complex; TEST_CASE_METHOD(TApp, "ComplexOption", "[newparse]") { diff --git a/tests/OptionGroupTest.cpp b/tests/OptionGroupTest.cpp index 7c475e8a..ab4d3c63 100644 --- a/tests/OptionGroupTest.cpp +++ b/tests/OptionGroupTest.cpp @@ -6,8 +6,6 @@ #include "app_helper.hpp" -using Catch::Matchers::Contains; - using vs_t = std::vector; TEST_CASE_METHOD(TApp, "BasicOptionGroup", "[optiongroup]") { diff --git a/tests/SubcommandTest.cpp b/tests/SubcommandTest.cpp index 01512d57..25415eaa 100644 --- a/tests/SubcommandTest.cpp +++ b/tests/SubcommandTest.cpp @@ -6,8 +6,6 @@ #include "app_helper.hpp" -using Catch::Matchers::Contains; - using vs_t = std::vector; TEST_CASE_METHOD(TApp, "BasicSubcommands", "[subcom]") { diff --git a/tests/TimerTest.cpp b/tests/TimerTest.cpp index 625318a8..e15d928c 100644 --- a/tests/TimerTest.cpp +++ b/tests/TimerTest.cpp @@ -12,8 +12,6 @@ #include #include -using Catch::Matchers::Contains; - TEST_CASE("Timer: MSTimes", "[timer]") { CLI::Timer timer{"My Timer"}; std::this_thread::sleep_for(std::chrono::milliseconds(123)); diff --git a/tests/catch.hpp b/tests/catch.hpp index c1778f5f..e6de6673 100644 --- a/tests/catch.hpp +++ b/tests/catch.hpp @@ -6,4 +6,26 @@ #pragma once +#include + +#ifdef CLI11_CATCH3 + +#include +#include +#include +#include +#include + +using Catch::Approx; // NOLINT(google-global-names-in-headers) +using Catch::Matchers::Equals; // NOLINT(google-global-names-in-headers) + +inline auto Contains(const std::string &x) { return Catch::Matchers::ContainsSubstring(x); } + +#else + #include + +using Catch::Equals; // NOLINT(google-global-names-in-headers) +using Catch::Matchers::Contains; // NOLINT(google-global-names-in-headers) + +#endif diff --git a/tests/meson.build b/tests/meson.build index b37574d5..48479855 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -1,14 +1,21 @@ catch2 = dependency('catch2') -testmain = static_library( - 'catch_main', - 'main.cpp', 'catch.hpp', - dependencies: catch2, -) -testdep = declare_dependency( - link_with: testmain, - dependencies: [catch2, CLI11_dep] -) +if catch2.version().version_compare('<3') + testmain = static_library( + 'catch_main', + 'main.cpp', 'catch.hpp', + dependencies: catch2, + ) + testdep = declare_dependency( + link_with: testmain, + dependencies: [catch2, CLI11_dep] + ) +else + testdep = declare_dependency( + dependencies: [CLI11_dep, dependency('catch2-with-main')], + compile_args: '-DCLI11_CATCH3' + ) +endif link_test_lib = library( 'link_test_1',