Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fe8a168

Browse files
committed
[unittest] teach gTest to print entries of DenseMap as pairs
When an assertion like the following fails: EXPECT_THAT(map, ElementsAre(Pair("p", "nullable")))); Error message before: Actual: { 40-byte object <E8-A5 9C-7F 25-37 00-00 58-7E 51-51 D0-7F 00-00 00-00 00-00 00-00 00-00 01-00 00-00 00-00 00-00 00-DA C7-7F 25-37 00-00> } After: Actual: { ("p", "nonnull") } It is not ideal that we need to refer directly to DenseMapPair inside the internal namespace, but I believe the practical maintenance risk is low. This change is covered by DenseMap's unittests, as we've covered SmallString etc in the past. Differential Revision: https://reviews.llvm.org/D153930
1 parent 7049393 commit fe8a168

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

llvm/unittests/ADT/DenseMapTest.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "llvm/ADT/DenseMap.h"
1010
#include "llvm/ADT/DenseMapInfo.h"
1111
#include "llvm/ADT/DenseMapInfoVariant.h"
12+
#include "llvm/ADT/StringRef.h"
1213
#include "gmock/gmock.h"
1314
#include "gtest/gtest.h"
1415
#include <map>
@@ -756,4 +757,12 @@ TEST(DenseMapCustomTest, VariantSupport) {
756757
// operator==.
757758
EXPECT_FALSE(DenseMapInfo<variant>::isEqual(Keys[2], Keys[2]));
758759
}
760+
761+
// Test that gTest prints map entries as pairs instead of opaque objects.
762+
// See third-party/unittest/googletest/internal/custom/gtest-printers.h
763+
TEST(DenseMapCustomTest, PairPrinting) {
764+
DenseMap<int, StringRef> Map = {{1, "one"}, {2, "two"}};
765+
EXPECT_EQ(R"({ (1, "one"), (2, "two") })", ::testing::PrintToString(Map));
766+
}
767+
759768
} // namespace

third-party/unittest/googletest/include/gtest/internal/custom/gtest-printers.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#define GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PRINTERS_H_
4141

4242
#if !GTEST_NO_LLVM_SUPPORT
43+
#include "llvm/ADT/DenseMap.h"
4344
#include "llvm/ADT/SmallString.h"
4445
#include "llvm/ADT/StringRef.h"
4546
#include <ostream>
@@ -63,6 +64,17 @@ inline void PrintTo(const SmallString<N> &S, std::ostream *OS) {
6364
inline void PrintTo(const SmallVectorImpl<char> &S, std::ostream *OS) {
6465
*OS << ::testing::PrintToString(std::string(S.data(), S.size()));
6566
}
67+
68+
// DenseMap's entries inherit from std::pair, and should act like pairs.
69+
// However gTest's provided `PrintTo(pair<K,V>)` template won't deduce K and V
70+
// because of the needed derived-to-base conversion.
71+
namespace detail {
72+
template <typename K, typename V>
73+
inline void PrintTo(const DenseMapPair<K, V> &Pair, std::ostream *OS) {
74+
*OS << ::testing::PrintToString(static_cast<const std::pair<K, V> &>(Pair));
75+
}
76+
} // namespace detail
77+
6678
} // namespace llvm
6779
#endif // !GTEST_NO_LLVM_SUPPORT
6880

0 commit comments

Comments
 (0)