mirror of
https://github.com/google/googletest.git
synced 2026-07-30 08:16:27 +08:00
Compare commits
5 Commits
6f92e6b481
...
1697e1e952
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1697e1e952 | ||
|
|
a0f06a70e3 | ||
|
|
3ff51c3e80 | ||
|
|
1269db9643 | ||
|
|
3df62357ab |
@ -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'
|
||||
|
||||
@ -587,7 +587,7 @@ class GTEST_API_ UnitTestImpl {
|
||||
// total_test_suite_count() - 1. If i is not in that range, returns NULL.
|
||||
const TestSuite* GetTestSuite(int i) const {
|
||||
const int index = GetElementOr(test_suite_indices_, i, -1);
|
||||
return index < 0 ? nullptr : test_suites_[static_cast<size_t>(i)];
|
||||
return index < 0 ? nullptr : test_suites_[static_cast<size_t>(index)];
|
||||
}
|
||||
|
||||
// Legacy API is deprecated but still available
|
||||
|
||||
@ -6885,9 +6885,12 @@ void ParseGoogleTestFlagsOnlyImpl(int* argc, CharType** argv) {
|
||||
LoadFlagsFromFile(flagfile_value);
|
||||
remove_flag = true;
|
||||
#endif // GTEST_USE_OWN_FLAGFILE_FLAG_ && GTEST_HAS_FILE_SYSTEM
|
||||
} else if (arg_string == "--help" || HasGoogleTestFlagPrefix(arg)) {
|
||||
// Both help flag and unrecognized Google Test flags (excluding
|
||||
// internal ones) trigger help display.
|
||||
} else if (arg_string == "--help") {
|
||||
g_help_flag = true;
|
||||
remove_flag = true;
|
||||
} else if (HasGoogleTestFlagPrefix(arg)) {
|
||||
// Unrecognized Google Test flags (excluding internal ones) trigger help
|
||||
// display, but remain available for other flag parsers.
|
||||
g_help_flag = true;
|
||||
}
|
||||
|
||||
|
||||
@ -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));
|
||||
|
||||
@ -5993,6 +5993,15 @@ TEST_F(ParseFlagsTest, UnrecognizedFlag) {
|
||||
GTEST_TEST_PARSING_FLAGS_(argv, argv2, flags, false);
|
||||
}
|
||||
|
||||
// Tests having a --help flag on the command line.
|
||||
TEST_F(ParseFlagsTest, HelpFlag) {
|
||||
const char* argv[] = {"foo.exe", "--help", "bar", nullptr};
|
||||
|
||||
const char* argv2[] = {"foo.exe", "bar", nullptr};
|
||||
|
||||
GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags(), true);
|
||||
}
|
||||
|
||||
// Tests having a --gtest_list_tests flag
|
||||
TEST_F(ParseFlagsTest, ListTestsFlag) {
|
||||
const char* argv[] = {"foo.exe", "--gtest_list_tests", nullptr};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user