mirror of
https://github.com/google/googletest.git
synced 2026-07-30 16:26:24 +08:00
Compare commits
3 Commits
fe655ad7d3
...
442f708851
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
442f708851 | ||
|
|
a0f06a70e3 | ||
|
|
763d27219c |
@ -945,13 +945,13 @@ template <typename T>
|
||||
class [[nodiscard]] UniversalPrinter<std::optional<T>> {
|
||||
public:
|
||||
static void Print(const std::optional<T>& value, ::std::ostream* os) {
|
||||
*os << '(';
|
||||
if (!value) {
|
||||
*os << "nullopt";
|
||||
UniversalPrint(std::nullopt, os);
|
||||
} else {
|
||||
*os << '(';
|
||||
UniversalPrint(*value, os);
|
||||
*os << ')';
|
||||
}
|
||||
*os << ')';
|
||||
}
|
||||
};
|
||||
|
||||
@ -961,27 +961,38 @@ class [[nodiscard]] UniversalPrinter<std::nullopt_t> {
|
||||
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
|
||||
template <typename... T>
|
||||
class [[nodiscard]] UniversalPrinter<std::variant<T...>> {
|
||||
public:
|
||||
static void Print(const std::variant<T...>& value, ::std::ostream* os) {
|
||||
*os << '(';
|
||||
std::visit(Visitor{os, value.index()}, value);
|
||||
*os << ')';
|
||||
}
|
||||
|
||||
private:
|
||||
struct Visitor {
|
||||
template <typename U>
|
||||
void operator()(const U& u) const {
|
||||
*os << "'" << GetTypeName<U>() << "(index = " << index
|
||||
<< ")' with value ";
|
||||
UniversalPrint(u, os);
|
||||
if (value.valueless_by_exception()) {
|
||||
*os << "(valueless)";
|
||||
} else {
|
||||
*os << '(';
|
||||
std::visit(UniversalPrinterVisitor{os, value.index()}, value);
|
||||
*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'
|
||||
|
||||
@ -2135,7 +2135,11 @@ GTEST_DISABLE_DEPRECATED_PUSH_()
|
||||
!defined(GTEST_OS_WINDOWS_RT) && !defined(GTEST_OS_WINDOWS_GAMES) && \
|
||||
!defined(GTEST_OS_ESP8266) && !defined(GTEST_OS_XTENSA) && \
|
||||
!defined(GTEST_OS_QURT)
|
||||
#ifdef GTEST_OS_WINDOWS
|
||||
inline int ChDir(const char* dir) { return _chdir(dir); }
|
||||
#else
|
||||
inline int ChDir(const char* dir) { return chdir(dir); }
|
||||
#endif // GTEST_OS_WINDOWS
|
||||
#endif
|
||||
inline FILE* FOpen(const char* path, const char* mode) {
|
||||
#if defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_MINGW)
|
||||
@ -2152,10 +2156,23 @@ inline FILE* FOpen(const char* path, const char* mode) {
|
||||
inline FILE* FReopen(const char* path, const char* mode, FILE* stream) {
|
||||
return freopen(path, mode, stream);
|
||||
}
|
||||
#ifdef GTEST_OS_WINDOWS
|
||||
inline FILE* FDOpen(int fd, const char* mode) { return _fdopen(fd, mode); }
|
||||
#else
|
||||
inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); }
|
||||
#endif // GTEST_OS_WINDOWS
|
||||
#endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT
|
||||
inline int FClose(FILE* fp) { return fclose(fp); }
|
||||
#if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_QURT)
|
||||
#ifdef GTEST_OS_WINDOWS
|
||||
inline int Read(int fd, void* buf, unsigned int count) {
|
||||
return static_cast<int>(_read(fd, buf, count));
|
||||
}
|
||||
inline int Write(int fd, const void* buf, unsigned int count) {
|
||||
return static_cast<int>(_write(fd, buf, count));
|
||||
}
|
||||
inline int Close(int fd) { return _close(fd); }
|
||||
#else
|
||||
inline int Read(int fd, void* buf, unsigned int count) {
|
||||
return static_cast<int>(read(fd, buf, count));
|
||||
}
|
||||
@ -2163,6 +2180,7 @@ inline int Write(int fd, const void* buf, unsigned int count) {
|
||||
return static_cast<int>(write(fd, buf, count));
|
||||
}
|
||||
inline int Close(int fd) { return close(fd); }
|
||||
#endif // GTEST_OS_WINDOWS
|
||||
#endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT
|
||||
#endif // GTEST_HAS_FILE_SYSTEM
|
||||
|
||||
|
||||
@ -2030,6 +2030,26 @@ TEST(PrintOneofTest, Basic) {
|
||||
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
|
||||
TEST(PrintOrderingTest, Basic) {
|
||||
EXPECT_EQ("(less)", PrintToString(std::strong_ordering::less));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user