Commit b0d73b7b authored by Shai Szulanski's avatar Shai Szulanski Committed by Facebook GitHub Bot

Add method pointer overloads of CoInvoke gmock helpers

Summary: Match overloads of upstream Invoke action to ease migration

Reviewed By: ispeters

Differential Revision: D32706320

fbshipit-source-id: d9564f63e534e3651433806fcf4f1fff7730fcc5
parent 9d7c9e70
...@@ -70,6 +70,14 @@ auto CoInvoke(F&& f) { ...@@ -70,6 +70,14 @@ auto CoInvoke(F&& f) {
}); });
} }
// Member function overload
template <class Class, typename MethodPtr>
auto CoInvoke(Class* obj_ptr, MethodPtr method_ptr) {
return ::testing::Invoke([=](auto&&... a) {
return co_invoke(method_ptr, obj_ptr, static_cast<decltype(a)>(a)...);
});
}
// CoInvoke variant that does not pass arguments to callback function. // CoInvoke variant that does not pass arguments to callback function.
// //
// Example: // Example:
...@@ -91,6 +99,13 @@ auto CoInvokeWithoutArgs(F&& f) { ...@@ -91,6 +99,13 @@ auto CoInvokeWithoutArgs(F&& f) {
[f = static_cast<F&&>(f)]() { return co_invoke(f); }); [f = static_cast<F&&>(f)]() { return co_invoke(f); });
} }
// Member function overload
template <class Class, typename MethodPtr>
auto CoInvokeWithoutArgs(Class* obj_ptr, MethodPtr method_ptr) {
return ::testing::InvokeWithoutArgs(
[=]() { return co_invoke(method_ptr, obj_ptr); });
}
namespace detail { namespace detail {
template <typename Fn> template <typename Fn>
auto makeCoAction(Fn&& fn) { auto makeCoAction(Fn&& fn) {
......
...@@ -43,6 +43,7 @@ class Foo { ...@@ -43,6 +43,7 @@ class Foo {
virtual folly::coro::Task<std::vector<std::string>> getValues() = 0; virtual folly::coro::Task<std::vector<std::string>> getValues() = 0;
virtual folly::coro::Task<std::string> getString() = 0; virtual folly::coro::Task<std::string> getString() = 0;
virtual folly::coro::Task<std::string> getStringArg(std::string) = 0;
virtual folly::coro::Task<void> getVoid() = 0; virtual folly::coro::Task<void> getVoid() = 0;
}; };
...@@ -52,6 +53,7 @@ class MockFoo : Foo { ...@@ -52,6 +53,7 @@ class MockFoo : Foo {
MOCK_METHOD0(getValues, folly::coro::Task<std::vector<std::string>>()); MOCK_METHOD0(getValues, folly::coro::Task<std::vector<std::string>>());
MOCK_METHOD0(getString, folly::coro::Task<std::string>()); MOCK_METHOD0(getString, folly::coro::Task<std::string>());
MOCK_METHOD1(getStringArg, folly::coro::Task<std::string>(std::string));
MOCK_METHOD0(getVoid, folly::coro::Task<void>()); MOCK_METHOD0(getVoid, folly::coro::Task<void>());
}; };
...@@ -180,4 +182,19 @@ CO_TEST(CoAssertThat, CoAssertThat) { ...@@ -180,4 +182,19 @@ CO_TEST(CoAssertThat, CoAssertThat) {
CO_ASSERT_THAT(1, Gt(0)); CO_ASSERT_THAT(1, Gt(0));
} }
CO_TEST(CoroGTestHelpers, CoInvokeMemberFunction) {
struct S {
folly::coro::Task<std::string> arg(std::string x) { co_return x + " bar"; }
folly::coro::Task<std::string> noArg() { co_return "foo"; }
} s;
MockFoo mock;
EXPECT_CALL(mock, getStringArg(_))
.WillOnce(CoInvoke(&s, &S::arg))
.WillOnce(CoInvokeWithoutArgs(&s, &S::noArg));
EXPECT_EQ(co_await mock.getStringArg("baz"), "baz bar");
EXPECT_EQ(co_await mock.getStringArg(""), "foo");
}
#endif // FOLLY_HAS_COROUTINES #endif // FOLLY_HAS_COROUTINES
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment