From 773de5b8725e04db593fde75b18567f233108d65 Mon Sep 17 00:00:00 2001 From: Robin Moussu Date: Fri, 25 Jan 2019 22:05:40 +0100 Subject: [PATCH] Add to string for std::optional --- .../internal/catch_compiler_capabilities.h | 12 ++++++++++ include/internal/catch_tostring.h | 19 +++++++++++++++ projects/CMakeLists.txt | 1 + .../UsageTests/ToStringOptional.tests.cpp | 23 +++++++++++++++++++ 4 files changed, 55 insertions(+) create mode 100644 projects/SelfTest/UsageTests/ToStringOptional.tests.cpp diff --git a/include/internal/catch_compiler_capabilities.h b/include/internal/catch_compiler_capabilities.h index 968f662fce..012bf462a8 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() && defined(CATCH_CPP17_OR_GREATER) +# define CATCH_INTERNAL_CONFIG_CPP17_OPTIONAL +# endif // __has_include() && 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 3aff268bc3..13a43b0c8f 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 +namespace Catch { + template + struct StringMaker > { + static std::string convert(const std::optional& 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 diff --git a/projects/CMakeLists.txt b/projects/CMakeLists.txt index 62fa7167fc..bc3914e0d4 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 0000000000..d78579d905 --- /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 -> toString", "[toString][optional][approvals]" ) { + using type = std::optional; + REQUIRE( "{ }" == ::Catch::Detail::stringify( type{} ) ); + REQUIRE( "0" == ::Catch::Detail::stringify( type{ 0 } ) ); +} + +TEST_CASE( "std::optional -> toString", "[toString][optional][approvals]" ) { + using type = std::optional; + REQUIRE( "{ }" == ::Catch::Detail::stringify( type{} ) ); + REQUIRE( "\"abc\"" == ::Catch::Detail::stringify( type{ "abc" } ) ); +} + +TEST_CASE( "std::vector > -> toString", "[toString][optional][approvals]" ) { + using type = std::vector >; + REQUIRE( "{ 0, { }, 2 }" == ::Catch::Detail::stringify( type{ 0, {}, 2 } ) ); +} + +#endif // CATCH_INTERNAL_CONFIG_CPP17_OPTIONAL