Skip to content

Commit

Permalink
Add to string for std::optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Moussu committed Jan 29, 2019
1 parent 64a9c02 commit 773de5b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
12 changes: 12 additions & 0 deletions include/internal/catch_compiler_capabilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions include/internal/catch_tostring.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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>
Expand Down
1 change: 1 addition & 0 deletions projects/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions projects/SelfTest/UsageTests/ToStringOptional.tests.cpp
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 773de5b

Please sign in to comment.