mirror of
https://github.com/google/googletest.git
synced 2026-06-15 00:16:13 +08:00
gmock-spec-builders: support mocking const-qualified function types.
In particular this automatically gives us support for examples like the
following:
using SomeFn = absl::AnyInvocable<R(Args...) const>;
MockFunction<SomeFn> some_fn;
PiperOrigin-RevId: 921303527
Change-Id: I19bf59671781e85db65cc20c0d6ea10b056c528a
This commit is contained in:
parent
09f45f51fb
commit
8736d2cd5c
@ -1957,6 +1957,11 @@ struct SignatureOf<R(Args...)> {
|
|||||||
using type = R(Args...);
|
using type = R(Args...);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename R, typename... Args>
|
||||||
|
struct SignatureOf<R(Args...) const> {
|
||||||
|
using type = R(Args...);
|
||||||
|
};
|
||||||
|
|
||||||
template <template <typename> class C, typename F>
|
template <template <typename> class C, typename F>
|
||||||
struct SignatureOf<C<F>,
|
struct SignatureOf<C<F>,
|
||||||
typename std::enable_if<std::is_function<F>::value>::type>
|
typename std::enable_if<std::is_function<F>::value>::type>
|
||||||
|
|||||||
@ -942,6 +942,15 @@ static constexpr bool IsMockFunctionTemplateArgumentDeducedTo(
|
|||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
// Like std::add_const, but for function types.
|
||||||
|
template <typename F>
|
||||||
|
struct AddConstToFunction;
|
||||||
|
|
||||||
|
template <typename R, typename... Args>
|
||||||
|
struct AddConstToFunction<R(Args...)> {
|
||||||
|
using type = R(Args...) const;
|
||||||
|
};
|
||||||
|
|
||||||
template <typename F>
|
template <typename F>
|
||||||
class MockMethodMockFunctionSignatureTest : public Test {};
|
class MockMethodMockFunctionSignatureTest : public Test {};
|
||||||
|
|
||||||
@ -953,25 +962,69 @@ TYPED_TEST_SUITE(MockMethodMockFunctionSignatureTest,
|
|||||||
|
|
||||||
TYPED_TEST(MockMethodMockFunctionSignatureTest,
|
TYPED_TEST(MockMethodMockFunctionSignatureTest,
|
||||||
IsMockFunctionTemplateArgumentDeducedForRawSignature) {
|
IsMockFunctionTemplateArgumentDeducedForRawSignature) {
|
||||||
using Argument = TypeParam;
|
// Non-const
|
||||||
MockFunction<Argument> foo;
|
{
|
||||||
EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<TypeParam>(foo));
|
using Argument = TypeParam;
|
||||||
|
MockFunction<Argument> foo;
|
||||||
|
EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<TypeParam>(foo));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Const
|
||||||
|
{
|
||||||
|
using Argument = typename AddConstToFunction<TypeParam>::type;
|
||||||
|
MockFunction<Argument> foo;
|
||||||
|
EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<TypeParam>(foo));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TYPED_TEST(MockMethodMockFunctionSignatureTest,
|
TYPED_TEST(MockMethodMockFunctionSignatureTest,
|
||||||
IsMockFunctionTemplateArgumentDeducedForStdFunction) {
|
IsMockFunctionTemplateArgumentDeducedForStdFunction) {
|
||||||
using Argument = std::function<TypeParam>;
|
// Non-const
|
||||||
MockFunction<Argument> foo;
|
{
|
||||||
EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<TypeParam>(foo));
|
using Argument = std::function<TypeParam>;
|
||||||
|
MockFunction<Argument> foo;
|
||||||
|
EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<TypeParam>(foo));
|
||||||
|
}
|
||||||
|
|
||||||
|
// As of 2026-05 MSVC doesn't know how to deal with this, providing pages of
|
||||||
|
// inscrutable errors about std::_Get_function_impl. But this is fine, since
|
||||||
|
// std::function<R(Args...) const> doesn't apply the const qualifier correctly
|
||||||
|
// anyway.
|
||||||
|
#if !defined(_MSC_VER)
|
||||||
|
|
||||||
|
// Const
|
||||||
|
{
|
||||||
|
using Argument =
|
||||||
|
std::function<typename AddConstToFunction<TypeParam>::type>;
|
||||||
|
|
||||||
|
MockFunction<Argument> foo;
|
||||||
|
EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<TypeParam>(foo));
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
TYPED_TEST(
|
TYPED_TEST(
|
||||||
MockMethodMockFunctionSignatureTest,
|
MockMethodMockFunctionSignatureTest,
|
||||||
IsMockFunctionCallMethodSignatureTheSameForRawSignatureAndStdFunction) {
|
IsMockFunctionCallMethodSignatureTheSameForRawSignatureAndStdFunction) {
|
||||||
using ForRawSignature = decltype(&MockFunction<TypeParam>::Call);
|
// Non-const
|
||||||
using ForStdFunction =
|
{
|
||||||
decltype(&MockFunction<std::function<TypeParam>>::Call);
|
using ForRawSignature = decltype(&MockFunction<TypeParam>::Call);
|
||||||
EXPECT_TRUE((std::is_same<ForRawSignature, ForStdFunction>::value));
|
using ForStdFunction =
|
||||||
|
decltype(&MockFunction<std::function<TypeParam>>::Call);
|
||||||
|
EXPECT_TRUE((std::is_same<ForRawSignature, ForStdFunction>::value));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Const
|
||||||
|
{
|
||||||
|
using ConstTypeParam = typename AddConstToFunction<TypeParam>::type;
|
||||||
|
using ForRawSignature = decltype(&MockFunction<ConstTypeParam>::Call);
|
||||||
|
|
||||||
|
using ForStdFunction =
|
||||||
|
decltype(&MockFunction<std::function<ConstTypeParam>>::Call);
|
||||||
|
|
||||||
|
EXPECT_TRUE((std::is_same<ForRawSignature, ForStdFunction>::value));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename F>
|
template <typename F>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user