summaryrefslogtreecommitdiffstats
path: root/googlemock/test/gmock-matchers_test.cc
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2019-02-05 12:18:47 -0500
committerGennadiy Civil <misterg@google.com>2019-02-12 13:20:14 -0500
commit28a96d1834dd4e973c9b95c07a1598a8aa9ac234 (patch)
tree2a8dad69b70ededf5d8e5b9ae606327eda06dabb /googlemock/test/gmock-matchers_test.cc
parentdda0df3b0ac67f291a2f5ed7b5451937ea386e7a (diff)
downloadgoogletest-28a96d1834dd4e973c9b95c07a1598a8aa9ac234.tar.gz
googletest-28a96d1834dd4e973c9b95c07a1598a8aa9ac234.zip
Googletest export
Fix matcher comparisons for std::reference_wrapper. The googletest docs indicate that std::reference_wrapper should be used to for objects that should not be copied by the matcher (in fact, the ByRef() function is basically the same as a call to std::cref). However, for many types (such as std::string), the overloaded operator== will not resolve correctly. Specifically, this is problematic if operator== depends on template argument deduction, where the same type is named on LHS and RHS. Because template argument deduction happens before any implict conversions for purposes of overload resolution, attempting to compare T with std::reference_wrapper<T> simply looks like a comparison of unlike types. For exapmle, std::reference_wrapper<std::string> is implicitly convertible to 'const std::string&', which would be able to choose an overload specialization of operator==. However, the implicit conversion can only happen after template argument deduction for operator==, so a specialization that would other be an applicable overload is never considered. Note also that this change only affects matchers. There are good reasons that matchers may need to transparently hold a std::reference_wrapper. Other comparisons (like EXPECT_EQ, et. al.) don't need to capture a reference: they don't need to defer evaluation (as in googlemock), and they don't need to avoid copies (as the call chain of matchers does). PiperOrigin-RevId: 232499175
Diffstat (limited to 'googlemock/test/gmock-matchers_test.cc')
-rw-r--r--googlemock/test/gmock-matchers_test.cc14
1 files changed, 14 insertions, 0 deletions
diff --git a/googlemock/test/gmock-matchers_test.cc b/googlemock/test/gmock-matchers_test.cc
index 1f14a056..c2f1d0ad 100644
--- a/googlemock/test/gmock-matchers_test.cc
+++ b/googlemock/test/gmock-matchers_test.cc
@@ -452,6 +452,20 @@ TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromStringView) {
}
#endif // GTEST_HAS_ABSL
+// Tests that a std::reference_wrapper<std::string> object can be implicitly
+// converted to a Matcher<std::string> or Matcher<const std::string&> via Eq().
+TEST(StringMatcherTest,
+ CanBeImplicitlyConstructedFromEqReferenceWrapperString) {
+ std::string value = "cats";
+ Matcher<std::string> m1 = Eq(std::ref(value));
+ EXPECT_TRUE(m1.Matches("cats"));
+ EXPECT_FALSE(m1.Matches("dogs"));
+
+ Matcher<const std::string&> m2 = Eq(std::ref(value));
+ EXPECT_TRUE(m2.Matches("cats"));
+ EXPECT_FALSE(m2.Matches("dogs"));
+}
+
// Tests that MakeMatcher() constructs a Matcher<T> from a
// MatcherInterface* without requiring the user to explicitly
// write the type.
OpenPOWER on IntegriCloud