Commit eef86a25 authored by Oleg Osovitskiy's avatar Oleg Osovitskiy Committed by Facebook GitHub Bot

Add CoInvokeWithoutArgs googletest helper

Summary: Add `CoInvokeWithoutArgs` helper, coroutine-aware version of `InvokeWithoutArgs`

Reviewed By: yfeldblum

Differential Revision: D27060013

fbshipit-source-id: 8024a5818fbe7759d3f9827d260c4e7370444bb0
parent 29de57bd
......@@ -56,7 +56,8 @@ namespace gmock_helpers {
// int fooCallCount = 0;
//
// EXPECT_CALL(mock, foo(_))
// .WillRepeatedly(CoInvoke([&](int x) -> folly::coro::Task<int> {
// .WillRepeatedly(CoInvoke(
// [&](int x) -> folly::coro::Task<int> {
// ++fooCallCount;
// co_return x + 1;
// }));
......@@ -68,6 +69,27 @@ auto CoInvoke(F&& f) {
});
}
// CoInvoke variant that does not pass arguments to callback function.
//
// Example:
// using namespace ::testing
// using namespace folly::coro::gmock_helpers;
//
// MockFoo mock;
// int fooCallCount = 0;
//
// EXPECT_CALL(mock, foo(_))
// .WillRepeatedly(CoInvokeWithoutArgs(
// [&]() -> folly::coro::Task<int> {
// ++fooCallCount;
// co_return 42;
// }));
template <typename F>
auto CoInvokeWithoutArgs(F&& f) {
return ::testing::InvokeWithoutArgs(
[f = static_cast<F&&>(f)]() { return co_invoke(f); });
}
namespace detail {
template <typename Fn>
auto makeCoAction(Fn&& fn) {
......
......@@ -73,6 +73,29 @@ TEST(CoroGTestHelpers, CoInvokeAvoidsDanglingReferences) {
EXPECT_EQ(ret2, values);
}
TEST(CoroGTestHelpers, CoInvokeWithoutArgsTest) {
MockFoo mock;
int numCalls = 0;
EXPECT_CALL(mock, getString())
.WillRepeatedly(
CoInvokeWithoutArgs([&numCalls]() -> folly::coro::Task<std::string> {
if (numCalls++ == 0) {
co_return "123";
} else {
co_return "abc";
}
}));
auto ret = folly::coro::blockingWait(mock.getString());
EXPECT_EQ(ret, "123");
EXPECT_EQ(numCalls, 1);
ret = folly::coro::blockingWait(mock.getString());
EXPECT_EQ(ret, "abc");
EXPECT_EQ(numCalls, 2);
}
TEST(CoroGTestHelpers, CoReturnTest) {
MockFoo mock;
......
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