diff options
author | Abseil Team <absl-team@google.com> | 2024-12-10 19:22:24 (GMT) |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2024-12-10 19:23:04 (GMT) |
commit | d122c0d435a6d305cdd50526127c84a98b77d87b (patch) | |
tree | b31546480ac7fd0779533dccf0cd8d8aeedfdf16 | |
parent | 35d0c365609296fa4730d62057c487e3cfa030ff (diff) | |
download | googletest-d122c0d435a6d305cdd50526127c84a98b77d87b.zip googletest-d122c0d435a6d305cdd50526127c84a98b77d87b.tar.gz googletest-d122c0d435a6d305cdd50526127c84a98b77d87b.tar.bz2 |
Add support for printing C++20 std::*_ordering types to gtest.
Adds feature test macro for C++20 <compare> header, a pretty-printer, and tests.
Inexplicably, these types aren't enums, so can't be handled with a switch.
PiperOrigin-RevId: 704783038
Change-Id: I29688989d18f43520fe610c12a447a20d2f98c95
-rw-r--r-- | googletest/include/gtest/gtest-printers.h | 39 | ||||
-rw-r--r-- | googletest/include/gtest/internal/gtest-port.h | 8 | ||||
-rw-r--r-- | googletest/test/googletest-printers-test.cc | 24 |
3 files changed, 71 insertions, 0 deletions
diff --git a/googletest/include/gtest/gtest-printers.h b/googletest/include/gtest/gtest-printers.h index b2822bc..198a769 100644 --- a/googletest/include/gtest/gtest-printers.h +++ b/googletest/include/gtest/gtest-printers.h @@ -126,6 +126,10 @@ #include <span> // NOLINT #endif // GTEST_INTERNAL_HAS_STD_SPAN +#if GTEST_INTERNAL_HAS_COMPARE_LIB +#include <compare> // NOLINT +#endif // GTEST_INTERNAL_HAS_COMPARE_LIB + namespace testing { // Definitions in the internal* namespaces are subject to change without notice. @@ -782,6 +786,41 @@ void PrintTo(const std::shared_ptr<T>& ptr, std::ostream* os) { (PrintSmartPointer<T>)(ptr, os, 0); } +#if GTEST_INTERNAL_HAS_COMPARE_LIB +template <typename T> +void PrintOrderingHelper(T ordering, std::ostream* os) { + if (ordering == T::less) { + *os << "(less)"; + } else if (ordering == T::greater) { + *os << "(greater)"; + } else if (ordering == T::equivalent) { + *os << "(equivalent)"; + } else { + *os << "(unknown ordering)"; + } +} + +inline void PrintTo(std::strong_ordering ordering, std::ostream* os) { + if (ordering == std::strong_ordering::equal) { + *os << "(equal)"; + } else { + PrintOrderingHelper(ordering, os); + } +} + +inline void PrintTo(std::partial_ordering ordering, std::ostream* os) { + if (ordering == std::partial_ordering::unordered) { + *os << "(unordered)"; + } else { + PrintOrderingHelper(ordering, os); + } +} + +inline void PrintTo(std::weak_ordering ordering, std::ostream* os) { + PrintOrderingHelper(ordering, os); +} +#endif + // Helper function for printing a tuple. T must be instantiated with // a tuple type. template <typename T> diff --git a/googletest/include/gtest/internal/gtest-port.h b/googletest/include/gtest/internal/gtest-port.h index 8d27c2c..ca18513 100644 --- a/googletest/include/gtest/internal/gtest-port.h +++ b/googletest/include/gtest/internal/gtest-port.h @@ -2533,4 +2533,12 @@ using Variant = ::std::variant<T...>; #define GTEST_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL 1 #endif +#if (defined(__cpp_lib_three_way_comparison) || \ + (GTEST_INTERNAL_HAS_INCLUDE(<compare>) && \ + GTEST_INTERNAL_CPLUSPLUS_LANG >= 201907L)) +#define GTEST_INTERNAL_HAS_COMPARE_LIB 1 +#else +#define GTEST_INTERNAL_HAS_COMPARE_LIB 0 +#endif + #endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ diff --git a/googletest/test/googletest-printers-test.cc b/googletest/test/googletest-printers-test.cc index d5061be..cf49af0 100644 --- a/googletest/test/googletest-printers-test.cc +++ b/googletest/test/googletest-printers-test.cc @@ -64,6 +64,10 @@ #include <span> // NOLINT #endif // GTEST_INTERNAL_HAS_STD_SPAN +#if GTEST_INTERNAL_HAS_COMPARE_LIB +#include <compare> // NOLINT +#endif // GTEST_INTERNAL_HAS_COMPARE_LIB + // Some user-defined types for testing the universal value printer. // An anonymous enum type. @@ -1970,6 +1974,26 @@ TEST(PrintOneofTest, Basic) { PrintToString(Type(NonPrintable{}))); } #endif // GTEST_INTERNAL_HAS_VARIANT + +#if GTEST_INTERNAL_HAS_COMPARE_LIB +TEST(PrintOrderingTest, Basic) { + EXPECT_EQ("(less)", PrintToString(std::strong_ordering::less)); + EXPECT_EQ("(greater)", PrintToString(std::strong_ordering::greater)); + // equal == equivalent for strong_ordering. + EXPECT_EQ("(equal)", PrintToString(std::strong_ordering::equivalent)); + EXPECT_EQ("(equal)", PrintToString(std::strong_ordering::equal)); + + EXPECT_EQ("(less)", PrintToString(std::weak_ordering::less)); + EXPECT_EQ("(greater)", PrintToString(std::weak_ordering::greater)); + EXPECT_EQ("(equivalent)", PrintToString(std::weak_ordering::equivalent)); + + EXPECT_EQ("(less)", PrintToString(std::partial_ordering::less)); + EXPECT_EQ("(greater)", PrintToString(std::partial_ordering::greater)); + EXPECT_EQ("(equivalent)", PrintToString(std::partial_ordering::equivalent)); + EXPECT_EQ("(unordered)", PrintToString(std::partial_ordering::unordered)); +} +#endif + namespace { class string_ref; |