Skip to content

Commit

Permalink
Add support for std::function in MockFunction (#2277)
Browse files Browse the repository at this point in the history
  • Loading branch information
adambadura committed Dec 16, 2019
1 parent d166e09 commit 8cfb40c
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 41 deletions.
129 changes: 88 additions & 41 deletions googlemock/include/gmock/gmock-spec-builders.h
Original file line number Diff line number Diff line change
Expand Up @@ -1786,10 +1786,77 @@ void ReportUninterestingCall(CallReaction reaction, const std::string& msg);

} // namespace internal

// A MockFunction<F> class has one mock method whose type is F. It is
// useful when you just want your test code to emit some messages and
// have Google Mock verify the right messages are sent (and perhaps at
// the right times). For example, if you are exercising code:
namespace internal {

template <typename F>
class MockFunction;

template <typename R, typename... Args>
class MockFunction<R(Args...)> {
public:
MockFunction() {};
MockFunction(const MockFunction&) = delete;
MockFunction& operator=(const MockFunction&) = delete;

std::function<R(Args...)> AsStdFunction() {
return [this](Args... args) -> R {
return this->Call(std::forward<Args>(args)...);
};
}

// Implementation detail: the expansion of the MOCK_METHOD macro.
R Call(Args... args) {
mock_.SetOwnerAndName(this, "Call");
return mock_.Invoke(std::forward<Args>(args)...);
}

MockSpec<R(Args...)> gmock_Call(Matcher<Args>... m) {
mock_.RegisterOwner(this);
return mock_.With(std::move(m)...);
}

MockSpec<R(Args...)> gmock_Call(const WithoutMatchers&,
R (*)(Args...)) {
return this->gmock_Call(::testing::A<Args>()...);
}

private:
FunctionMocker<R(Args...)> mock_;
};

/*
The SignatureOf<F> struct is a meta-function returning function signature
corresponding to the provided F argument.
It makes use of MockFunction easier by allowing it to accept more F arguments
than just function signatures.
Specializations provided here cover only a signature type itself and
std::function. However, if need be it can be easily extended to cover also other
types (like for example boost::function).
*/

template <typename F>
struct SignatureOf;

template <typename R, typename... Args>
struct SignatureOf<R(Args...)> {
using type = R(Args...);
};

template <typename F>
struct SignatureOf<std::function<F>> : SignatureOf<F> {};

template <typename F>
using SignatureOfT = typename SignatureOf<F>::type;

} // namespace internal

// A MockFunction<F> type has one mock method whose type is
// internal::SignatureOfT<F>. It is useful when you just want your
// test code to emit some messages and have Google Mock verify the
// right messages are sent (and perhaps at the right times). For
// example, if you are exercising code:
//
// Foo(1);
// Foo(2);
Expand Down Expand Up @@ -1823,50 +1890,30 @@ void ReportUninterestingCall(CallReaction reaction, const std::string& msg);
// Bar("a") is called by which call to Foo().
//
// MockFunction<F> can also be used to exercise code that accepts
// std::function<F> callbacks. To do so, use AsStdFunction() method
// to create std::function proxy forwarding to original object's Call.
// Example:
// std::function<SignatureOfT<F>> callbacks. To do so, use
// AsStdFunction() method to create std::function proxy forwarding to
// original object's Call. Example:
//
// TEST(FooTest, RunsCallbackWithBarArgument) {
// MockFunction<int(string)> callback;
// EXPECT_CALL(callback, Call("bar")).WillOnce(Return(1));
// Foo(callback.AsStdFunction());
// }
//
// The SignatureOfT<F> indirection allows to use other types than just
// function signature type. This is typically useful when providing
// a mock for a predefined std::function type. Example:
//
// using predicate = std::function<bool(string)>;
// void MyFilterAlgorithm(predicate pred);
//
// TEST(FooTest, PredicateAlwaysAccepts) {
// MockFunction<predicate> pred_mock;
// EXPECT_CALL(pred_mock, Call(_)).WillRepeatedly(Return(true));
// MyFilterAlgorithm(pred_mock.AsStdFunction());
// }
template <typename F>
class MockFunction;

template <typename R, typename... Args>
class MockFunction<R(Args...)> {
public:
MockFunction() {}
MockFunction(const MockFunction&) = delete;
MockFunction& operator=(const MockFunction&) = delete;

std::function<R(Args...)> AsStdFunction() {
return [this](Args... args) -> R {
return this->Call(std::forward<Args>(args)...);
};
}

// Implementation detail: the expansion of the MOCK_METHOD macro.
R Call(Args... args) {
mock_.SetOwnerAndName(this, "Call");
return mock_.Invoke(std::forward<Args>(args)...);
}

internal::MockSpec<R(Args...)> gmock_Call(Matcher<Args>... m) {
mock_.RegisterOwner(this);
return mock_.With(std::move(m)...);
}

internal::MockSpec<R(Args...)> gmock_Call(const internal::WithoutMatchers&,
R (*)(Args...)) {
return this->gmock_Call(::testing::A<Args>()...);
}

private:
internal::FunctionMocker<R(Args...)> mock_;
};
using MockFunction = internal::MockFunction<internal::SignatureOfT<F>>;

// The style guide prohibits "using" statements in a namespace scope
// inside a header file. However, the MockSpec class template is
Expand Down
22 changes: 22 additions & 0 deletions googlemock/test/gmock-function-mocker_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
# include <objbase.h>
#endif // GTEST_OS_WINDOWS

#include <functional>
#include <map>
#include <string>
#include <type_traits>
Expand Down Expand Up @@ -567,6 +568,27 @@ TEST(MockMethodOverloadedMockMethodTest, CanOverloadOnConstnessInMacroBody) {
EXPECT_EQ(3, const_mock->Overloaded(1));
}

template <typename Signature>
class MockMethodMockFunctionSignatureTest : public ::testing::Test {
};

using SignatureTypes = ::testing::Types<
void(),
int(),
void(int),
int(bool, int),
int(bool, char, int, int, int, int, int, char, int, bool)
>;
TYPED_TEST_SUITE(MockMethodMockFunctionSignatureTest, SignatureTypes);

TYPED_TEST(MockMethodMockFunctionSignatureTest, MockFunctionOfSignatureIsTheSameTypeAsMockFunctionOfStdFunction) {
using signature = TypeParam;
using expected = MockFunction<signature>;
using actual = MockFunction<std::function<signature>>;
constexpr auto is_same = std::is_same<actual, expected>::value;
EXPECT_TRUE(is_same);
}

TEST(MockMethodMockFunctionTest, WorksForVoidNullary) {
MockFunction<void()> foo;
EXPECT_CALL(foo, Call());
Expand Down
23 changes: 23 additions & 0 deletions googlemock/test/gmock-generated-function-mockers_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
# include <objbase.h>
#endif // GTEST_OS_WINDOWS

#include <functional>
#include <map>
#include <string>
#include <type_traits>
#include "gmock/gmock.h"
#include "gtest/gtest.h"

Expand Down Expand Up @@ -557,6 +559,27 @@ TEST(OverloadedMockMethodTest, CanOverloadOnConstnessInMacroBody) {
EXPECT_EQ(3, const_mock->Overloaded(1));
}

template <typename Signature>
class MockFunctionSignatureTest : public ::testing::Test {
};

using SignatureTypes = ::testing::Types<
void(),
int(),
void(int),
int(bool, int),
int(bool, char, int, int, int, int, int, char, int, bool)
>;
TYPED_TEST_SUITE(MockFunctionSignatureTest, SignatureTypes);

TYPED_TEST(MockFunctionSignatureTest, MockFunctionOfSignatureIsTheSameTypeAsMockFunctionOfStdFunction) {
using signature = TypeParam;
using expected = MockFunction<signature>;
using actual = MockFunction<std::function<signature>>;
constexpr auto is_same = std::is_same<actual, expected>::value;
EXPECT_TRUE(is_same);
}

TEST(MockFunctionTest, WorksForVoidNullary) {
MockFunction<void()> foo;
EXPECT_CALL(foo, Call());
Expand Down

0 comments on commit 8cfb40c

Please sign in to comment.