This is an archive of the discontinued LLVM Phabricator instance.

[unittest] teach gTest to print entries of DenseMap as pairs
ClosedPublic

Authored by sammccall on Jun 27 2023, 4:22 PM.

Details

Summary

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.

Diff Detail

Event Timeline

sammccall created this revision.Jun 27 2023, 4:22 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 27 2023, 4:22 PM
sammccall requested review of this revision.Jun 27 2023, 4:22 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 27 2023, 4:22 PM
sammccall updated this revision to Diff 535185.Jun 27 2023, 4:24 PM

fix include inside #if

mboehme accepted this revision.Jun 27 2023, 9:07 PM

This is a great quality-of-life improvement!

I agree that the practical risk of referring to DenseMapPair is low. If it ever does change so it no longer inherits from std::pair then, in the worst case, we can remove the PrintTo overload introduced here and be in a no worse position than before. (Preferably, though, we would change the implementation so that it continues to print the same thing.)

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

Is there a reason we can't simply do this?

::testing::PrintTo(static_cast<const std::pair<K, V> &>(Pair), OS);
This revision is now accepted and ready to land.Jun 27 2023, 9:07 PM
sammccall added inline comments.Jun 28 2023, 1:09 AM
third-party/unittest/googletest/include/gtest/internal/custom/gtest-printers.h
74

This does technically work, but it's relying on internals of gtest (it's actually ::testing::detail::PrintTo).

PrintToString is the public API for "print this thing you know how to print", exactly what functions that dispatches to is an implementation detail.

mboehme added inline comments.Jun 28 2023, 1:19 AM
third-party/unittest/googletest/include/gtest/internal/custom/gtest-printers.h
74

Thanks for the explanation, LGTM!