mirror of
https://github.com/catchorg/Catch2.git
synced 2025-09-17 02:25:38 +02:00
Add StringMaker for std::variant, std::monostate (#1380)
The StringMaker is off by default and can be enabled by a new macro `CATCH_CONFIG_ENABLE_VARIANT_STRINGMAKER`, to avoid increasing the footprint of stringification machinery by default.
This commit is contained in:

committed by
Martin Hořeňovský

parent
a575536abe
commit
c638c57209
@@ -354,6 +354,7 @@ namespace Catch {
|
||||
#if defined(CATCH_CONFIG_ENABLE_ALL_STRINGMAKERS)
|
||||
# define CATCH_CONFIG_ENABLE_PAIR_STRINGMAKER
|
||||
# define CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER
|
||||
# define CATCH_CONFIG_ENABLE_VARIANT_STRINGMAKER
|
||||
# define CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER
|
||||
#endif
|
||||
|
||||
@@ -418,6 +419,34 @@ namespace Catch {
|
||||
}
|
||||
#endif // CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER
|
||||
|
||||
#if defined(CATCH_CONFIG_ENABLE_VARIANT_STRINGMAKER) && defined(CATCH_CONFIG_CPP17_VARIANT)
|
||||
#include <variant>
|
||||
namespace Catch {
|
||||
template<>
|
||||
struct StringMaker<std::monostate> {
|
||||
static std::string convert(const std::monostate&) {
|
||||
return "{ }";
|
||||
}
|
||||
};
|
||||
|
||||
template<typename... Elements>
|
||||
struct StringMaker<std::variant<Elements...>> {
|
||||
static std::string convert(const std::variant<Elements...>& variant) {
|
||||
if (variant.valueless_by_exception()) {
|
||||
return "{valueless variant}";
|
||||
} else {
|
||||
return std::visit(
|
||||
[](const auto& value) {
|
||||
return ::Catch::Detail::stringify(value);
|
||||
},
|
||||
variant
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
#endif // CATCH_CONFIG_ENABLE_VARIANT_STRINGMAKER
|
||||
|
||||
namespace Catch {
|
||||
struct not_this_one {}; // Tag type for detecting which begin/ end are being selected
|
||||
|
||||
|
Reference in New Issue
Block a user