aboutsummaryrefslogtreecommitdiffstats
path: root/googlemock
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2020-02-25 10:52:53 -0500
committerCJ Johnson <johnsoncj@google.com>2020-02-28 16:41:01 -0500
commit909b1ccfcacc9d6f920aa79e3f643c1a4e806eb2 (patch)
treeeea1e402de2fe2ae0bbb370cbe68f3019efcc063 /googlemock
parentfd538161f484c202e68512ca37aaa73f2e06f606 (diff)
downloadgoogletest-909b1ccfcacc9d6f920aa79e3f643c1a4e806eb2.tar.gz
googletest-909b1ccfcacc9d6f920aa79e3f643c1a4e806eb2.tar.bz2
googletest-909b1ccfcacc9d6f920aa79e3f643c1a4e806eb2.zip
Googletest export
Relax the implementation of MatcherCast to allow conversion of `Matcher<T>` to `Matcher<const T&>`. They have the same match signature. PiperOrigin-RevId: 297115843
Diffstat (limited to 'googlemock')
-rw-r--r--googlemock/include/gmock/gmock-matchers.h13
-rw-r--r--googlemock/test/gmock-matchers_test.cc9
2 files changed, 15 insertions, 7 deletions
diff --git a/googlemock/include/gmock/gmock-matchers.h b/googlemock/include/gmock/gmock-matchers.h
index 4b6ac563..fe88a7c7 100644
--- a/googlemock/include/gmock/gmock-matchers.h
+++ b/googlemock/include/gmock/gmock-matchers.h
@@ -424,7 +424,14 @@ class MatcherCastImpl<T, Matcher<U> > {
!std::is_base_of<FromType, ToType>::value,
"Can't implicitly convert from <base> to <derived>");
- return source_matcher_.MatchAndExplain(static_cast<U>(x), listener);
+ // Do the cast to `U` explicitly if necessary.
+ // Otherwise, let implicit conversions do the trick.
+ using CastType =
+ typename std::conditional<std::is_convertible<T&, const U&>::value,
+ T&, U>::type;
+
+ return source_matcher_.MatchAndExplain(static_cast<CastType>(x),
+ listener);
}
void DescribeTo(::std::ostream* os) const override {
@@ -524,8 +531,8 @@ inline Matcher<T> SafeMatcherCast(const M& polymorphic_matcher_or_value) {
template <typename T, typename U>
inline Matcher<T> SafeMatcherCast(const Matcher<U>& matcher) {
// Enforce that T can be implicitly converted to U.
- GTEST_COMPILE_ASSERT_((std::is_convertible<T, U>::value),
- "T must be implicitly convertible to U");
+ static_assert(std::is_convertible<const T&, const U&>::value,
+ "T must be implicitly convertible to U");
// Enforce that we are not converting a non-reference type T to a reference
// type U.
GTEST_COMPILE_ASSERT_(
diff --git a/googlemock/test/gmock-matchers_test.cc b/googlemock/test/gmock-matchers_test.cc
index c1949e63..3619959f 100644
--- a/googlemock/test/gmock-matchers_test.cc
+++ b/googlemock/test/gmock-matchers_test.cc
@@ -765,10 +765,11 @@ TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
- Matcher<int> m1 = Eq(0);
- Matcher<const int&> m2 = SafeMatcherCast<const int&>(m1);
- EXPECT_TRUE(m2.Matches(0));
- EXPECT_FALSE(m2.Matches(1));
+ Matcher<std::unique_ptr<int>> m1 = IsNull();
+ Matcher<const std::unique_ptr<int>&> m2 =
+ SafeMatcherCast<const std::unique_ptr<int>&>(m1);
+ EXPECT_TRUE(m2.Matches(std::unique_ptr<int>()));
+ EXPECT_FALSE(m2.Matches(std::unique_ptr<int>(new int)));
}
// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>.