No longer require a functor passed to ResultOf matcher to define result_of type.

This makes ResultOf more convenient to use. In particular, the matcher now accepts
lambdas.

PiperOrigin-RevId: 210118509
This commit is contained in:
Abseil Team
2018-08-24 13:30:17 -04:00
committed by Gennadiy Civil
parent 9c96f500a3
commit a0e62d9f1a
2 changed files with 53 additions and 34 deletions

View File

@@ -4597,6 +4597,7 @@ struct PolymorphicFunctor {
typedef int result_type;
int operator()(int n) { return n; }
int operator()(const char* s) { return static_cast<int>(strlen(s)); }
std::string operator()(int *p) { return p ? "good ptr" : "null"; }
};
TEST(ResultOfTest, WorksForPolymorphicFunctors) {
@@ -4611,6 +4612,23 @@ TEST(ResultOfTest, WorksForPolymorphicFunctors) {
EXPECT_FALSE(matcher_string.Matches("shrt"));
}
#if GTEST_LANG_CXX11
TEST(ResultOfTest, WorksForPolymorphicFunctorsIgnoringResultType) {
Matcher<int*> matcher = ResultOf(PolymorphicFunctor(), "good ptr");
int n = 0;
EXPECT_TRUE(matcher.Matches(&n));
EXPECT_FALSE(matcher.Matches(nullptr));
}
TEST(ResultOfTest, WorksForLambdas) {
Matcher<int> matcher =
ResultOf([](int str_len) { return std::string(str_len, 'x'); }, "xxx");
EXPECT_TRUE(matcher.Matches(3));
EXPECT_FALSE(matcher.Matches(1));
}
#endif
const int* ReferencingFunction(const int& n) { return &n; }
struct ReferencingFunctor {