[PATCH] D153930: [unittest] teach gTest to print entries of DenseMap as pairs

Sam McCall via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 28 02:30:19 PDT 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rGfe8a168161a3: [unittest] teach gTest to print entries of DenseMap as pairs (authored by sammccall).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153930/new/

https://reviews.llvm.org/D153930

Files:
  llvm/unittests/ADT/DenseMapTest.cpp
  third-party/unittest/googletest/include/gtest/internal/custom/gtest-printers.h


Index: third-party/unittest/googletest/include/gtest/internal/custom/gtest-printers.h
===================================================================
--- third-party/unittest/googletest/include/gtest/internal/custom/gtest-printers.h
+++ third-party/unittest/googletest/include/gtest/internal/custom/gtest-printers.h
@@ -40,6 +40,7 @@
 #define GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PRINTERS_H_
 
 #if !GTEST_NO_LLVM_SUPPORT
+#include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringRef.h"
 #include <ostream>
@@ -63,6 +64,17 @@
 inline void PrintTo(const SmallVectorImpl<char> &S, std::ostream *OS) {
   *OS << ::testing::PrintToString(std::string(S.data(), S.size()));
 }
+
+// DenseMap's entries inherit from std::pair, and should act like pairs.
+// However gTest's provided `PrintTo(pair<K,V>)` template won't deduce K and V
+// because of the needed derived-to-base conversion.
+namespace detail {
+template <typename K, typename V>
+inline void PrintTo(const DenseMapPair<K, V> &Pair, std::ostream *OS) {
+  *OS << ::testing::PrintToString(static_cast<const std::pair<K, V> &>(Pair));
+}
+} // namespace detail
+
 } // namespace llvm
 #endif // !GTEST_NO_LLVM_SUPPORT
 
Index: llvm/unittests/ADT/DenseMapTest.cpp
===================================================================
--- llvm/unittests/ADT/DenseMapTest.cpp
+++ llvm/unittests/ADT/DenseMapTest.cpp
@@ -9,6 +9,7 @@
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/DenseMapInfo.h"
 #include "llvm/ADT/DenseMapInfoVariant.h"
+#include "llvm/ADT/StringRef.h"
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
 #include <map>
@@ -756,4 +757,12 @@
   // operator==.
   EXPECT_FALSE(DenseMapInfo<variant>::isEqual(Keys[2], Keys[2]));
 }
+
+// Test that gTest prints map entries as pairs instead of opaque objects.
+// See third-party/unittest/googletest/internal/custom/gtest-printers.h
+TEST(DenseMapCustomTest, PairPrinting) {
+  DenseMap<int, StringRef> Map = {{1, "one"}, {2, "two"}};
+  EXPECT_EQ(R"({ (1, "one"), (2, "two") })", ::testing::PrintToString(Map));
+}
+
 } // namespace


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153930.535296.patch
Type: text/x-patch
Size: 2097 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230628/6d0bdf1c/attachment.bin>


More information about the llvm-commits mailing list