mirror of
https://github.com/google/googletest.git
synced 2026-07-30 16:26:24 +08:00
Compare commits
4 Commits
23d2aed3ea
...
3a94245863
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3a94245863 | ||
|
|
a0f06a70e3 | ||
|
|
1c9e9a635f | ||
|
|
35646c5804 |
@ -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.
|
||||
|
||||
@ -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'
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
@ -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