Skip to content

Commit

Permalink
Googletest export
Browse files Browse the repository at this point in the history
Support templating MockFunction over function objects besides std::function.

PiperOrigin-RevId: 373586967
  • Loading branch information
Abseil Team authored and CJ-Johnson committed May 13, 2021
1 parent d69a112 commit 662fe38
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
14 changes: 8 additions & 6 deletions googlemock/include/gmock/gmock-spec-builders.h
Original file line number Diff line number Diff line change
Expand Up @@ -1838,21 +1838,23 @@ 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).
Specializations provided here cover a signature type itself and any template
that can be parameterized with a signature, including std::function and
boost::function.
*/

template <typename F>
template <typename F, typename = void>
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 <template <typename> class C, typename F>
struct SignatureOf<C<F>,
typename std::enable_if<std::is_function<F>::value>::type>
: SignatureOf<F> {};

template <typename F>
using SignatureOfT = typename SignatureOf<F>::type;
Expand Down
24 changes: 18 additions & 6 deletions googlemock/test/gmock-function-mocker_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ namespace {

template <typename Expected, typename F>
static constexpr bool IsMockFunctionTemplateArgumentDeducedTo(
const MockFunction<F>&) {
const internal::MockFunction<F>&) {
return std::is_same<F, Expected>::value;
}

Expand All @@ -868,14 +868,14 @@ TYPED_TEST(MockMethodMockFunctionSignatureTest,
IsMockFunctionTemplateArgumentDeducedForRawSignature) {
using Argument = TypeParam;
MockFunction<Argument> foo;
EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<Argument>(foo));
EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<TypeParam>(foo));
}

TYPED_TEST(MockMethodMockFunctionSignatureTest,
IsMockFunctionTemplateArgumentDeducedForStdFunction) {
using Argument = std::function<TypeParam>;
MockFunction<Argument> foo;
EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<Argument>(foo));
EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<TypeParam>(foo));
}

TYPED_TEST(
Expand All @@ -887,15 +887,27 @@ TYPED_TEST(
EXPECT_TRUE((std::is_same<ForRawSignature, ForStdFunction>::value));
}

template <typename F>
struct AlternateCallable {
};

TYPED_TEST(MockMethodMockFunctionSignatureTest,
IsMockFunctionTemplateArgumentDeducedForAlternateCallable) {
using Argument = AlternateCallable<TypeParam>;
MockFunction<Argument> foo;
EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<TypeParam>(foo));
}

TYPED_TEST(
MockMethodMockFunctionSignatureTest,
IsMockFunctionAsStdFunctionMethodSignatureTheSameForRawSignatureAndStdFunction) {
using ForRawSignature = decltype(&MockFunction<TypeParam>::AsStdFunction);
IsMockFunctionCallMethodSignatureTheSameForAlternateCallable) {
using ForRawSignature = decltype(&MockFunction<TypeParam>::Call);
using ForStdFunction =
decltype(&MockFunction<std::function<TypeParam>>::AsStdFunction);
decltype(&MockFunction<std::function<TypeParam>>::Call);
EXPECT_TRUE((std::is_same<ForRawSignature, ForStdFunction>::value));
}


struct MockMethodSizes0 {
MOCK_METHOD(void, func, ());
};
Expand Down

0 comments on commit 662fe38

Please sign in to comment.