mirror of
https://github.com/google/googletest.git
synced 2026-07-30 16:26:24 +08:00
Compare commits
4 Commits
f27f736aef
...
6d98087682
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6d98087682 | ||
|
|
a0f06a70e3 | ||
|
|
55da83185f | ||
|
|
fcdc66adcb |
@ -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'
|
||||
|
||||
@ -84,6 +84,9 @@
|
||||
#define GTEST_OS_GNU_HURD 1
|
||||
#elif defined(__GLIBC__) && defined(__FreeBSD_kernel__)
|
||||
#define GTEST_OS_GNU_KFREEBSD 1
|
||||
#elif defined(__ZEPHYR__)
|
||||
// Define it before linux as it could be built as a linux application
|
||||
#define GTEST_OS_ZEPHYR 1
|
||||
#elif defined __linux__
|
||||
#define GTEST_OS_LINUX 1
|
||||
#if defined __ANDROID__
|
||||
|
||||
@ -141,6 +141,7 @@
|
||||
// GTEST_OS_WINDOWS_PHONE - Windows Phone
|
||||
// GTEST_OS_WINDOWS_RT - Windows Store App/WinRT
|
||||
// GTEST_OS_ZOS - z/OS
|
||||
// GTEST_OS_ZEPHYR - Zephyr OS
|
||||
//
|
||||
// Among the platforms, Cygwin, Linux, Mac OS X, and Windows have the
|
||||
// most stable support. Since core members of the Google Test project
|
||||
@ -516,6 +517,10 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
|
||||
#endif
|
||||
#endif // GTEST_HAS_STD_WSTRING
|
||||
|
||||
#ifdef GTEST_OS_ZEPHYR
|
||||
#define GTEST_HAS_FILE_SYSTEM 0
|
||||
#endif
|
||||
|
||||
#ifndef GTEST_HAS_FILE_SYSTEM
|
||||
// Most platforms support a file system.
|
||||
#define GTEST_HAS_FILE_SYSTEM 1
|
||||
@ -2063,6 +2068,18 @@ inline int RmDir(const char* dir) { return rmdir(dir); }
|
||||
inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
|
||||
#endif
|
||||
|
||||
#elif defined(GTEST_OS_ZEPHYR)
|
||||
static inline int FileNo(FILE* file) {
|
||||
if (file == stdin)
|
||||
return 1;
|
||||
else if (file == stdout)
|
||||
return 2;
|
||||
else if (file == stderr)
|
||||
return 3;
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
static inline int isatty(int fd) { return true; }
|
||||
#else
|
||||
|
||||
typedef struct stat StatStruct;
|
||||
|
||||
@ -43,7 +43,9 @@
|
||||
#include <algorithm>
|
||||
#include <chrono> // NOLINT
|
||||
#include <cmath>
|
||||
#ifndef GTEST_OS_ZEPHYR
|
||||
#include <csignal> // NOLINT: raise(3) is used on some platforms
|
||||
#endif
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
@ -47,13 +47,23 @@ void loop() { RUN_ALL_TESTS(); }
|
||||
}
|
||||
#endif
|
||||
|
||||
#elif defined(GTEST_OS_QURT)
|
||||
// QuRT: program entry point is main, but argc/argv are unusable.
|
||||
#elif defined(GTEST_OS_QURT) || defined(GTEST_OS_ZEPHYR)
|
||||
// Program entry point is main, but argc/argv are unusable.
|
||||
|
||||
#if defined(GTEST_OS_ZEPHYR)
|
||||
#undef GTEST_API_
|
||||
#define GTEST_API_
|
||||
#endif
|
||||
|
||||
GTEST_API_ int main() {
|
||||
printf("Running main() from %s\n", __FILE__);
|
||||
testing::InitGoogleTest();
|
||||
return RUN_ALL_TESTS();
|
||||
|
||||
int ret = RUN_ALL_TESTS();
|
||||
#if defined(GTEST_OS_ZEPHYR)
|
||||
_exit( ret );
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
#else
|
||||
// Normal platforms: program entry point is main, argc/argv are initialized.
|
||||
|
||||
@ -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