Compare commits

...

4 Commits

Author SHA1 Message Date
zhouronghua
3a94245863
Merge 1c9e9a635f3a7a6be7771559fe28ea70bcee8de4 into a0f06a70e3da7afa88da9527c43951bca1f7cef2 2026-07-29 20:27:36 +02:00
Abseil Team
a0f06a70e3 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
2026-07-29 11:08:13 -07:00
ronghua.zhou
1c9e9a635f add EXPECT_NO_COREFILE_DEATH 2024-10-21 09:56:08 +00:00
ronghua.zhou
35646c5804 rm log with path 2024-10-21 06:42:36 +00:00
5 changed files with 102 additions and 19 deletions

View File

@ -189,6 +189,17 @@ GTEST_API_ bool InDeathTestChild();
#define EXPECT_DEATH(statement, matcher) \
EXPECT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, matcher)
// check if it is a child of ExceptionTest
#define EXPECT_NO_COREFILE_DEATH(statement, matcher) \
do { \
if (dynamic_cast<::testing::ExceptionTest *>(this)) { \
EXPECT_DEATH(statement, matcher); \
} else { \
EXPECT_TRUE(false) << "EXPECT_NO_COREFILE_DEATH can only be used in " \
"::testing::ExceptionTest and its subclass"; \
} \
} while (0)
// Two predicate classes that can be used in {ASSERT,EXPECT}_EXIT*:
// Tests that an exit code describes a normal exit with a given exit code.

View File

@ -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'

View File

@ -49,6 +49,8 @@
#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_H_
#define GOOGLETEST_INCLUDE_GTEST_GTEST_H_
#include <sys/resource.h>
#include <cstddef>
#include <cstdint>
#include <limits>
@ -365,6 +367,45 @@ class GTEST_API_ [[nodiscard]] Test {
Test& operator=(const Test&) = delete;
};
// The class is a helpler to implements a test suite without core file
// generated.
class ExceptionTest : public ::testing::Test {
protected:
static void SetUpTestSuite() {
struct rlimit rl;
rl.rlim_cur = 0;
rl.rlim_max = 0;
// store old value of core dump file size
if (getrlimit(RLIMIT_CORE, &rl_old_) == -1) {
exit(EXIT_FAILURE);
}
// do not generate core dump file, we know it will fail
if (setrlimit(RLIMIT_CORE, &rl) == -1) {
exit(EXIT_FAILURE);
} else {
GTEST_LOG_(INFO) << "set RLIMIT_CORE success";
GTEST_LOG_(INFO) << "rl_old_ rlim_cur: " << rl_old_.rlim_cur;
GTEST_LOG_(INFO) << "rl_old_ rlim_max: " << rl_old_.rlim_max;
}
}
static void TearDownTestSuite() {
// do not generate core dump file, we know it will fail
if (setrlimit(RLIMIT_CORE, &rl_old_) == -1) {
exit(EXIT_FAILURE);
} else {
GTEST_LOG_(INFO) << "restore RLIMIT_CORE success";
}
}
void SetUp() override {}
void TearDown() override {}
private:
static struct rlimit rl_old_;
};
struct rlimit ExceptionTest::rl_old_ = {0, 0};
typedef internal::TimeInMillis TimeInMillis;
// A copyable object representing a user specified test property which can be

View File

@ -59,7 +59,7 @@ GTEST_API_ int main() {
// Normal platforms: program entry point is main, argc/argv are initialized.
GTEST_API_ int main(int argc, char **argv) {
printf("Running main() from %s\n", __FILE__);
// printf("Running main() from %s\n", __FILE__);
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -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));