Add to string for std::optional

This commit is contained in:
Robin Moussu 2019-01-25 22:05:40 +01:00 committed by Martin Hořeňovský
parent 63d1a96908
commit 16dc219704
4 changed files with 55 additions and 0 deletions

View File

@ -183,6 +183,14 @@
#endif #endif
#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 // Check if variant is available and usable
#if defined(__has_include) #if defined(__has_include)
@ -222,6 +230,10 @@
# define CATCH_CONFIG_CPP11_TO_STRING # define CATCH_CONFIG_CPP11_TO_STRING
#endif #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) #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 # define CATCH_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS
#endif #endif

View File

@ -348,6 +348,7 @@ namespace Catch {
# define CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER # define CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER
# define CATCH_CONFIG_ENABLE_VARIANT_STRINGMAKER # define CATCH_CONFIG_ENABLE_VARIANT_STRINGMAKER
# define CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER # define CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER
# define CATCH_CONFIG_ENABLE_OPTIONAL_STRINGMAKER
#endif #endif
// Separate std::pair specialization // Separate std::pair specialization
@ -369,6 +370,24 @@ namespace Catch {
} }
#endif // CATCH_CONFIG_ENABLE_PAIR_STRINGMAKER #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 // Separate std::tuple specialization
#if defined(CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER) #if defined(CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER)
#include <tuple> #include <tuple>

View File

@ -36,6 +36,7 @@ set(TEST_SOURCES
${SELF_TEST_DIR}/UsageTests/Misc.tests.cpp ${SELF_TEST_DIR}/UsageTests/Misc.tests.cpp
${SELF_TEST_DIR}/UsageTests/ToStringChrono.tests.cpp ${SELF_TEST_DIR}/UsageTests/ToStringChrono.tests.cpp
${SELF_TEST_DIR}/UsageTests/ToStringGeneral.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/ToStringPair.tests.cpp
${SELF_TEST_DIR}/UsageTests/ToStringTuple.tests.cpp ${SELF_TEST_DIR}/UsageTests/ToStringTuple.tests.cpp
${SELF_TEST_DIR}/UsageTests/ToStringVariant.tests.cpp ${SELF_TEST_DIR}/UsageTests/ToStringVariant.tests.cpp

View File

@ -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