Skip to content

Commit 662fe38

Browse files
Abseil TeamCJ-Johnson
authored andcommitted
Googletest export
Support templating MockFunction over function objects besides std::function. PiperOrigin-RevId: 373586967
1 parent d69a112 commit 662fe38

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

googlemock/include/gmock/gmock-spec-builders.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,21 +1838,23 @@ corresponding to the provided F argument.
18381838
It makes use of MockFunction easier by allowing it to accept more F arguments
18391839
than just function signatures.
18401840
1841-
Specializations provided here cover only a signature type itself and
1842-
std::function. However, if need be it can be easily extended to cover also other
1843-
types (like for example boost::function).
1841+
Specializations provided here cover a signature type itself and any template
1842+
that can be parameterized with a signature, including std::function and
1843+
boost::function.
18441844
*/
18451845

1846-
template <typename F>
1846+
template <typename F, typename = void>
18471847
struct SignatureOf;
18481848

18491849
template <typename R, typename... Args>
18501850
struct SignatureOf<R(Args...)> {
18511851
using type = R(Args...);
18521852
};
18531853

1854-
template <typename F>
1855-
struct SignatureOf<std::function<F>> : SignatureOf<F> {};
1854+
template <template <typename> class C, typename F>
1855+
struct SignatureOf<C<F>,
1856+
typename std::enable_if<std::is_function<F>::value>::type>
1857+
: SignatureOf<F> {};
18561858

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

googlemock/test/gmock-function-mocker_test.cc

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ namespace {
849849

850850
template <typename Expected, typename F>
851851
static constexpr bool IsMockFunctionTemplateArgumentDeducedTo(
852-
const MockFunction<F>&) {
852+
const internal::MockFunction<F>&) {
853853
return std::is_same<F, Expected>::value;
854854
}
855855

@@ -868,14 +868,14 @@ TYPED_TEST(MockMethodMockFunctionSignatureTest,
868868
IsMockFunctionTemplateArgumentDeducedForRawSignature) {
869869
using Argument = TypeParam;
870870
MockFunction<Argument> foo;
871-
EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<Argument>(foo));
871+
EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<TypeParam>(foo));
872872
}
873873

874874
TYPED_TEST(MockMethodMockFunctionSignatureTest,
875875
IsMockFunctionTemplateArgumentDeducedForStdFunction) {
876876
using Argument = std::function<TypeParam>;
877877
MockFunction<Argument> foo;
878-
EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<Argument>(foo));
878+
EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<TypeParam>(foo));
879879
}
880880

881881
TYPED_TEST(
@@ -887,15 +887,27 @@ TYPED_TEST(
887887
EXPECT_TRUE((std::is_same<ForRawSignature, ForStdFunction>::value));
888888
}
889889

890+
template <typename F>
891+
struct AlternateCallable {
892+
};
893+
894+
TYPED_TEST(MockMethodMockFunctionSignatureTest,
895+
IsMockFunctionTemplateArgumentDeducedForAlternateCallable) {
896+
using Argument = AlternateCallable<TypeParam>;
897+
MockFunction<Argument> foo;
898+
EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<TypeParam>(foo));
899+
}
900+
890901
TYPED_TEST(
891902
MockMethodMockFunctionSignatureTest,
892-
IsMockFunctionAsStdFunctionMethodSignatureTheSameForRawSignatureAndStdFunction) {
893-
using ForRawSignature = decltype(&MockFunction<TypeParam>::AsStdFunction);
903+
IsMockFunctionCallMethodSignatureTheSameForAlternateCallable) {
904+
using ForRawSignature = decltype(&MockFunction<TypeParam>::Call);
894905
using ForStdFunction =
895-
decltype(&MockFunction<std::function<TypeParam>>::AsStdFunction);
906+
decltype(&MockFunction<std::function<TypeParam>>::Call);
896907
EXPECT_TRUE((std::is_same<ForRawSignature, ForStdFunction>::value));
897908
}
898909

910+
899911
struct MockMethodSizes0 {
900912
MOCK_METHOD(void, func, ());
901913
};

0 commit comments

Comments
 (0)