mirror of
https://github.com/google/googletest.git
synced 2026-07-30 16:26:24 +08:00
Make GoogleTest handle std::monostate and valueless_by_exception() for std::variant similarly to how it handles std::nullopt
Fixes: #2066 PiperOrigin-RevId: 955981465 Change-Id: I6b34dc2e634d4b727a50fb3e21e5d8e25c39a13b
This commit is contained in:
parent
3ff51c3e80
commit
a0f06a70e3
@ -945,14 +945,14 @@ template <typename T>
|
|||||||
class [[nodiscard]] UniversalPrinter<std::optional<T>> {
|
class [[nodiscard]] UniversalPrinter<std::optional<T>> {
|
||||||
public:
|
public:
|
||||||
static void Print(const std::optional<T>& value, ::std::ostream* os) {
|
static void Print(const std::optional<T>& value, ::std::ostream* os) {
|
||||||
*os << '(';
|
|
||||||
if (!value) {
|
if (!value) {
|
||||||
*os << "nullopt";
|
UniversalPrint(std::nullopt, os);
|
||||||
} else {
|
} else {
|
||||||
|
*os << '(';
|
||||||
UniversalPrint(*value, os);
|
UniversalPrint(*value, os);
|
||||||
}
|
|
||||||
*os << ')';
|
*os << ')';
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
@ -961,27 +961,38 @@ class [[nodiscard]] UniversalPrinter<std::nullopt_t> {
|
|||||||
static void Print(std::nullopt_t, ::std::ostream* os) { *os << "(nullopt)"; }
|
static void Print(std::nullopt_t, ::std::ostream* os) { *os << "(nullopt)"; }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct UniversalPrinterVisitor {
|
||||||
|
template <typename T>
|
||||||
|
void operator()(const T& arg) const {
|
||||||
|
*os << "'" << GetTypeName<T>() << "(index = " << index << ")' with value ";
|
||||||
|
UniversalPrint(arg, os);
|
||||||
|
}
|
||||||
|
::std::ostream* os;
|
||||||
|
std::size_t index;
|
||||||
|
};
|
||||||
|
|
||||||
// Printer for std::variant
|
// Printer for std::variant
|
||||||
template <typename... T>
|
template <typename... T>
|
||||||
class [[nodiscard]] UniversalPrinter<std::variant<T...>> {
|
class [[nodiscard]] UniversalPrinter<std::variant<T...>> {
|
||||||
public:
|
public:
|
||||||
static void Print(const std::variant<T...>& value, ::std::ostream* os) {
|
static void Print(const std::variant<T...>& value, ::std::ostream* os) {
|
||||||
|
if (value.valueless_by_exception()) {
|
||||||
|
*os << "(valueless)";
|
||||||
|
} else {
|
||||||
*os << '(';
|
*os << '(';
|
||||||
std::visit(Visitor{os, value.index()}, value);
|
std::visit(UniversalPrinterVisitor{os, value.index()}, value);
|
||||||
*os << ')';
|
*os << ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
|
||||||
struct Visitor {
|
|
||||||
template <typename U>
|
|
||||||
void operator()(const U& u) const {
|
|
||||||
*os << "'" << GetTypeName<U>() << "(index = " << index
|
|
||||||
<< ")' with value ";
|
|
||||||
UniversalPrint(u, os);
|
|
||||||
}
|
}
|
||||||
::std::ostream* os;
|
|
||||||
std::size_t index;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Printer for std::monostate
|
||||||
|
template <>
|
||||||
|
class [[nodiscard]] UniversalPrinter<std::monostate> {
|
||||||
|
public:
|
||||||
|
static void Print(std::monostate, ::std::ostream* os) {
|
||||||
|
*os << "(monostate)";
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// UniversalPrintArray(begin, len, os) prints an array of 'len'
|
// UniversalPrintArray(begin, len, os) prints an array of 'len'
|
||||||
|
|||||||
@ -2030,6 +2030,26 @@ TEST(PrintOneofTest, Basic) {
|
|||||||
PrintToString(Type(NonPrintable{})));
|
PrintToString(Type(NonPrintable{})));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(PrintVariantTest, Monostate) {
|
||||||
|
EXPECT_EQ("(monostate)", PrintToString(std::monostate()));
|
||||||
|
|
||||||
|
#if GTEST_HAS_EXCEPTIONS
|
||||||
|
struct ThrowOnMove {
|
||||||
|
ThrowOnMove() = default;
|
||||||
|
ThrowOnMove(ThrowOnMove&& other) { *this = std::move(other); }
|
||||||
|
ThrowOnMove& operator=(ThrowOnMove&&) {
|
||||||
|
(void)std::vector<bool>().at(0);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
std::variant<std::monostate, ThrowOnMove> v = std::monostate();
|
||||||
|
EXPECT_EQ("('std::monostate(index = 0)' with value (monostate))",
|
||||||
|
PrintToString(v));
|
||||||
|
EXPECT_THROW(v = ThrowOnMove(), std::out_of_range);
|
||||||
|
EXPECT_EQ("(valueless)", PrintToString(v));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
#if GTEST_INTERNAL_HAS_COMPARE_LIB
|
#if GTEST_INTERNAL_HAS_COMPARE_LIB
|
||||||
TEST(PrintOrderingTest, Basic) {
|
TEST(PrintOrderingTest, Basic) {
|
||||||
EXPECT_EQ("(less)", PrintToString(std::strong_ordering::less));
|
EXPECT_EQ("(less)", PrintToString(std::strong_ordering::less));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user