catch3
Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.
Clonegit clone https://git.progmasoft.com/catch3
Add to string for std::optional
- Commit
16dc2197046dd1362e26027e789f15d65275a185- Author
- Robin Moussu ·
- Committer
- Martin Hořeňovský
- Parents
63d1a96908ac
Changes
include/internal/catch_compiler_capabilities.h | 12 +++++++++++ include/internal/catch_tostring.h | 19 ++++++++++++++++++ projects/CMakeLists.txt | 1 + .../SelfTest/UsageTests/ToStringOptional.tests.cpp | 23 ++++++++++++++++++++++ 4 files changed, 55 insertions(+) diff --git a/include/internal/catch_compiler_capabilities.h b/include/internal/catch_compiler_capabilities.h index 968f662f..012bf462 100644 --- a/include/internal/catch_compiler_capabilities.h +++ b/include/internal/catch_compiler_capabilities.h @@ -183,6 +183,14 @@ #endif #endif +//////////////////////////////////////////////////////////////////////////////// +// Check if optional is available and usable +#if defined(__has_include) +# if __has_include(<optional>) && defined(CATCH_CPP17_OR_GREATER) +# define CATCH_INTERNAL_CONFIG_CPP17_OPTIONAL +# endif // __has_include(<optional>) && defined(CATCH_CPP17_OR_GREATER) +#endif // __has_include + //////////////////////////////////////////////////////////////////////////////// // Check if variant is available and usable #if defined(__has_include) @@ -222,6 +230,10 @@ # define CATCH_CONFIG_CPP11_TO_STRING #endif +#if defined(CATCH_INTERNAL_CONFIG_CPP17_OPTIONAL) && !defined(CATCH_CONFIG_NO_CPP17_OPTIONAL) && !defined(CATCH_CONFIG_CPP17_OPTIONAL) +# define CATCH_CONFIG_CPP17_OPTIONAL +#endif + #if defined(CATCH_INTERNAL_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS) && !defined(CATCH_CONFIG_NO_CPP17_UNCAUGHT_EXCEPTIONS) && !defined(CATCH_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS) # define CATCH_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS #endif diff --git a/include/internal/catch_tostring.h b/include/internal/catch_tostring.h index 3aff268b..13a43b0c 100644 --- a/include/internal/catch_tostring.h +++ b/include/internal/catch_tostring.h @@ -348,6 +348,7 @@ namespace Catch { # define CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER # define CATCH_CONFIG_ENABLE_VARIANT_STRINGMAKER # define CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER +# define CATCH_CONFIG_ENABLE_OPTIONAL_STRINGMAKER #endif // Separate std::pair specialization @@ -369,6 +370,24 @@ namespace Catch { } #endif // CATCH_CONFIG_ENABLE_PAIR_STRINGMAKER +#if defined(CATCH_CONFIG_ENABLE_OPTIONAL_STRINGMAKER) && defined(CATCH_CONFIG_CPP17_OPTIONAL) +#include <optional> +namespace Catch { + template<typename T> + struct StringMaker<std::optional<T> > { + static std::string convert(const std::optional<T>& optional) { + ReusableStringStream rss; + if (optional.has_value()) { + rss << ::Catch::Detail::stringify(*optional); + } else { + rss << "{ }"; + } + return rss.str(); + } + }; +} +#endif // CATCH_CONFIG_ENABLE_OPTIONAL_STRINGMAKER + // Separate std::tuple specialization #if defined(CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER) #include <tuple> diff --git a/projects/CMakeLists.txt b/projects/CMakeLists.txt index 62fa7167..bc3914e0 100644 --- a/projects/CMakeLists.txt +++ b/projects/CMakeLists.txt @@ -36,6 +36,7 @@ set(TEST_SOURCES ${SELF_TEST_DIR}/UsageTests/Misc.tests.cpp ${SELF_TEST_DIR}/UsageTests/ToStringChrono.tests.cpp ${SELF_TEST_DIR}/UsageTests/ToStringGeneral.tests.cpp + ${SELF_TEST_DIR}/UsageTests/ToStringOptional.tests.cpp ${SELF_TEST_DIR}/UsageTests/ToStringPair.tests.cpp ${SELF_TEST_DIR}/UsageTests/ToStringTuple.tests.cpp ${SELF_TEST_DIR}/UsageTests/ToStringVariant.tests.cpp diff --git a/projects/SelfTest/UsageTests/ToStringOptional.tests.cpp b/projects/SelfTest/UsageTests/ToStringOptional.tests.cpp new file mode 100644 index 00000000..d78579d9 --- /dev/null +++ b/projects/SelfTest/UsageTests/ToStringOptional.tests.cpp @@ -0,0 +1,23 @@ +#define CATCH_CONFIG_ENABLE_OPTIONAL_STRINGMAKER +#include "catch.hpp" + +#if defined(CATCH_CONFIG_CPP17_OPTIONAL) + +TEST_CASE( "std::optional<int> -> toString", "[toString][optional][approvals]" ) { + using type = std::optional<int>; + REQUIRE( "{ }" == ::Catch::Detail::stringify( type{} ) ); + REQUIRE( "0" == ::Catch::Detail::stringify( type{ 0 } ) ); +} + +TEST_CASE( "std::optional<std::string> -> toString", "[toString][optional][approvals]" ) { + using type = std::optional<std::string>; + REQUIRE( "{ }" == ::Catch::Detail::stringify( type{} ) ); + REQUIRE( "\"abc\"" == ::Catch::Detail::stringify( type{ "abc" } ) ); +} + +TEST_CASE( "std::vector<std::optional<int> > -> toString", "[toString][optional][approvals]" ) { + using type = std::vector<std::optional<int> >; + REQUIRE( "{ 0, { }, 2 }" == ::Catch::Detail::stringify( type{ 0, {}, 2 } ) ); +} + +#endif // CATCH_INTERNAL_CONFIG_CPP17_OPTIONAL