diff --git a/googlemock/test/gmock-matchers-comparisons_test.cc b/googlemock/test/gmock-matchers-comparisons_test.cc index cd2b80a0..3a21a9a7 100644 --- a/googlemock/test/gmock-matchers-comparisons_test.cc +++ b/googlemock/test/gmock-matchers-comparisons_test.cc @@ -798,6 +798,7 @@ TEST(ExpectThat, TakesLiterals) { EXPECT_THAT(1, 1); EXPECT_THAT(1.0, 1.0); EXPECT_THAT(std::string(), ""); + EXPECT_THAT(std::shared_ptr(), nullptr); } TEST(ExpectThat, TakesFunctions) { @@ -1126,6 +1127,16 @@ TEST(IsNullTest, CanDescribeSelf) { EXPECT_EQ("isn't NULL", DescribeNegation(m)); } +struct SmartPtrHelper { + MOCK_METHOD(void, Call, (std::shared_ptr)); +}; + +TEST(IsNullTest, WorksWithSmartPtr) { + SmartPtrHelper helper; + EXPECT_CALL(helper, Call(nullptr)); + helper.Call(nullptr); +} + // Tests that NotNull() matches any non-NULL pointer of any type. TEST(NotNullTest, MatchesNonNullPointer) { Matcher m1 = NotNull(); diff --git a/googletest/include/gtest/gtest-matchers.h b/googletest/include/gtest/gtest-matchers.h index 08ffbeb9..6d2ab14d 100644 --- a/googletest/include/gtest/gtest-matchers.h +++ b/googletest/include/gtest/gtest-matchers.h @@ -40,6 +40,7 @@ #define GOOGLETEST_INCLUDE_GTEST_GTEST_MATCHERS_H_ #include +#include #include #include #include @@ -485,6 +486,15 @@ class [[nodiscard]] Matcher : public internal::MatcherBase { // Implicit constructor here allows people to write // EXPECT_CALL(foo, Bar(5)) instead of EXPECT_CALL(foo, Bar(Eq(5))) sometimes Matcher(T value); // NOLINT + + // Implicit constructor here allows people to write + // EXPECT_THAT(foo, nullptr) instead of EXPECT_THAT(foo, IsNull()) for smart + // pointer types. + // + // The second argument is needed to avoid capturing literal '0'. + template + Matcher(U, // NOLINT + std::enable_if_t>* = nullptr); }; // The following two specializations allow the user to write str @@ -895,13 +905,21 @@ inline internal::EqMatcher Eq(T x) { return internal::EqMatcher(x); } -// Constructs a Matcher from a 'value' of type T. The constructed +// Constructs a Matcher from a 'value' of type T. The constructed // matcher matches any value that's equal to 'value'. template Matcher::Matcher(T value) { *this = Eq(value); } +// Constructs a Matcher from nullptr. The constructed matcher matches any +// value that is equal to nullptr. +template +template +Matcher::Matcher(U, std::enable_if_t>*) { + *this = Eq(nullptr); +} + // Creates a monomorphic matcher that matches anything with type Lhs // and equal to rhs. A user may need to use this instead of Eq(...) // in order to resolve an overloading ambiguity.