38 #ifdef GTEST_OS_WINDOWS
43 #endif // GTEST_OS_WINDOWS
48 #include <type_traits>
54 namespace gmock_function_mocker_test {
64 using testing::MockFunction;
68 using testing::TypedEq;
86 virtual bool Unary(
int x) = 0;
87 virtual long Binary(
short x,
int y) = 0;
88 virtual int Decimal(
bool b,
char c,
short d,
int e,
long f,
89 float g,
double h,
unsigned i,
char* j,
90 const std::string& k) = 0;
106 virtual int TypeWithComma(
const std::map<int, std::string>& a_map) = 0;
121 virtual
int RefQualifiedOverloaded() && = 0;
123 #ifdef GTEST_OS_WINDOWS
124 STDMETHOD_(
int, CTNullary)() = 0;
125 STDMETHOD_(
bool, CTUnary)(
int x) = 0;
126 STDMETHOD_(
int, CTDecimal)
127 (
bool b,
char c,
short d,
int e,
long f,
128 float g,
double h,
unsigned i,
char* j,
const std::string& k) = 0;
129 STDMETHOD_(
char, CTConst)(
int x)
const = 0;
130 #endif // GTEST_OS_WINDOWS
151 (
bool,
char,
short,
int,
long,
float,
152 double,
unsigned,
char*,
const std::string& str),
156 MOCK_METHOD(std::string, TakesConstReference, (
const int&));
160 MOCK_METHOD((std::map<int, std::string>), ReturnTypeWithComma, (), ());
161 MOCK_METHOD((std::map<int, std::string>), ReturnTypeWithComma, (
int),
165 MOCK_METHOD(
int, OverloadedOnArgumentNumber, (
int));
168 MOCK_METHOD(
char, OverloadedOnArgumentType, (
char));
170 MOCK_METHOD(
int, OverloadedOnConstness, (), (
override));
171 MOCK_METHOD(
char, OverloadedOnConstness, (), (
override,
const));
174 MOCK_METHOD(
int, TypeWithComma, ((
const std::map<int, std::string>&)));
178 MOCK_METHOD(
int (*)(
bool), ReturnsFunctionPointer1, (
int), ());
181 #ifdef GTEST_OS_WINDOWS
182 MOCK_METHOD(
int, CTNullary, (), (Calltype(STDMETHODCALLTYPE)));
183 MOCK_METHOD(
bool, CTUnary, (
int), (Calltype(STDMETHODCALLTYPE)));
185 (
bool b,
char c,
short d,
int e,
long f,
float g,
double h,
186 unsigned i,
char* j,
const std::string& k),
187 (Calltype(STDMETHODCALLTYPE)));
188 MOCK_METHOD(
char, CTConst, (
int), (
const, Calltype(STDMETHODCALLTYPE)));
189 MOCK_METHOD((std::map<int, std::string>), CTReturnTypeWithComma, (),
190 (Calltype(STDMETHODCALLTYPE)));
191 #endif // GTEST_OS_WINDOWS
194 MOCK_METHOD(
int, RefQualifiedConstRef, (), (
const, ref(&),
override));
195 MOCK_METHOD(
int, RefQualifiedConstRefRef, (), (
const, ref(&&),
override));
196 MOCK_METHOD(
int, RefQualifiedRef, (), (ref(&),
override));
197 MOCK_METHOD(
int, RefQualifiedRefRef, (), (ref(&&),
override));
199 MOCK_METHOD(
int, RefQualifiedOverloaded, (), (
const, ref(&),
override));
200 MOCK_METHOD(
int, RefQualifiedOverloaded, (), (
const, ref(&&),
override));
201 MOCK_METHOD(
int, RefQualifiedOverloaded, (), (ref(&),
override));
202 MOCK_METHOD(
int, RefQualifiedOverloaded, (), (ref(&&),
override));
222 double,
unsigned,
char*,
const std::string& str));
229 MOCK_METHOD0(ReturnTypeWithComma, std::map<int, std::string>());
231 std::map<int, std::string>(
int));
244 int(
const std::map<int, std::string>&));
251 #ifdef GTEST_OS_WINDOWS
255 int(
bool b,
char c,
short d,
int e,
256 long f,
float g,
double h,
257 unsigned i,
char* j,
const std::string& k));
263 std::map<int, std::string>());
264 #endif // GTEST_OS_WINDOWS
285 class FunctionMockerTest : public testing::
Test {
332 EXPECT_EQ(5, this->
foo_->
Decimal(
true,
'a', 0, 0, 1, 0, 0, 5,
nullptr,
"hi"));
336 TYPED_TEST(FunctionMockerTest, MocksFunctionWithNonConstReferenceArgument) {
345 TYPED_TEST(FunctionMockerTest, MocksFunctionWithConstReferenceArgument) {
348 .WillOnce(
Return(
"Hello"));
354 TYPED_TEST(FunctionMockerTest, MocksFunctionWithConstArgument) {
361 TYPED_TEST(FunctionMockerTest, MocksFunctionsOverloadedOnArgumentNumber) {
372 TYPED_TEST(FunctionMockerTest, MocksFunctionsOverloadedOnArgumentType) {
383 TYPED_TEST(FunctionMockerTest, MocksFunctionsOverloadedOnConstnessOfThis) {
393 const std::map<int, std::string> a_map;
401 TYPED_TEST(FunctionMockerTest, MocksTypeWithTemplatedCopyCtor) {
407 #ifdef GTEST_OS_WINDOWS
409 TYPED_TEST(FunctionMockerTest, MocksNullaryFunctionWithCallType) {
419 TYPED_TEST(FunctionMockerTest, MocksUnaryFunctionWithCallType) {
430 TYPED_TEST(FunctionMockerTest, MocksDecimalFunctionWithCallType) {
432 Lt(100), 5U, NULL,
"hi"))
435 EXPECT_EQ(10, this->
foo_->CTDecimal(
true,
'a', 0, 0, 1, 0, 0, 5, NULL,
"hi"));
439 TYPED_TEST(FunctionMockerTest, MocksFunctionsConstFunctionWithCallType) {
445 TYPED_TEST(FunctionMockerTest, MocksReturnTypeWithCommaAndCallType) {
446 const std::map<int, std::string> a_map;
452 #endif // GTEST_OS_WINDOWS
454 TEST(FunctionMockerTest, RefQualified) {
459 RefQualifiedConstRefRef)
466 EXPECT_CALL(static_cast<const MockFoo&>(mock_foo), RefQualifiedOverloaded())
468 EXPECT_CALL(static_cast<const MockFoo&&>(mock_foo), RefQualifiedOverloaded())
470 EXPECT_CALL(static_cast<MockFoo&>(mock_foo), RefQualifiedOverloaded())
472 EXPECT_CALL(static_cast<MockFoo&&>(mock_foo), RefQualifiedOverloaded())
476 EXPECT_EQ(std::move(mock_foo).RefQualifiedConstRefRef(), 2);
478 EXPECT_EQ(std::move(mock_foo).RefQualifiedRefRef(), 4);
480 EXPECT_EQ(std::cref(mock_foo).
get().RefQualifiedOverloaded(), 5);
481 EXPECT_EQ(std::move(std::cref(mock_foo).
get())
482 .RefQualifiedOverloaded(),
485 EXPECT_EQ(std::move(mock_foo).RefQualifiedOverloaded(), 8);
510 template <
typename T>
534 template <
typename T>
541 virtual void Pop() = 0;
542 virtual int GetSize()
const = 0;
544 virtual const T&
GetTop()
const = 0;
547 template <
typename T>
558 MOCK_METHOD((std::map<int, int>), ReturnTypeWithComma, (), ());
559 MOCK_METHOD((std::map<int, int>), ReturnTypeWithComma, (
int), (
const));
566 template <
typename T>
585 template <
typename T>
615 const std::map<int, int> a_map;
619 EXPECT_EQ(a_map, mock.ReturnTypeWithComma());
620 EXPECT_EQ(a_map, mock.ReturnTypeWithComma(1));
623 #ifdef GTEST_OS_WINDOWS
626 template <
typename T>
627 class StackInterfaceWithCallType {
629 virtual ~StackInterfaceWithCallType() {}
632 STDMETHOD_(
void, Push)(
const T&
value) = 0;
633 STDMETHOD_(
void, Pop)() = 0;
634 STDMETHOD_(
int, GetSize)()
const = 0;
636 STDMETHOD_(
const T&, GetTop)()
const = 0;
639 template <
typename T>
640 class MockStackWithCallType :
public StackInterfaceWithCallType<T> {
642 MockStackWithCallType() {}
645 (Calltype(STDMETHODCALLTYPE),
override));
646 MOCK_METHOD(
void, Pop, (), (Calltype(STDMETHODCALLTYPE),
override));
647 MOCK_METHOD(
int, GetSize, (), (Calltype(STDMETHODCALLTYPE),
override,
const));
649 (Calltype(STDMETHODCALLTYPE),
override,
const));
652 MockStackWithCallType(
const MockStackWithCallType&) =
delete;
653 MockStackWithCallType& operator=(
const MockStackWithCallType&) =
delete;
656 template <
typename T>
657 class LegacyMockStackWithCallType :
public StackInterfaceWithCallType<T> {
659 LegacyMockStackWithCallType() {}
667 LegacyMockStackWithCallType(
const LegacyMockStackWithCallType&) =
delete;
668 LegacyMockStackWithCallType& operator=(
const LegacyMockStackWithCallType&) =
672 template <
typename T>
674 using TemplateMockTestWithCallTypeTypes =
676 LegacyMockStackWithCallType<int>>;
678 TemplateMockTestWithCallTypeTypes);
681 TYPED_TEST(TemplateMockTestWithCallType, Works) {
700 #endif // GTEST_OS_WINDOWS
702 #define MY_MOCK_METHODS1_ \
703 MOCK_METHOD(void, Overloaded, ()); \
704 MOCK_METHOD(int, Overloaded, (int), (const)); \
705 MOCK_METHOD(bool, Overloaded, (bool f, int n))
707 #define LEGACY_MY_MOCK_METHODS1_ \
708 MOCK_METHOD0(Overloaded, void()); \
709 MOCK_CONST_METHOD1(Overloaded, int(int n)); \
710 MOCK_METHOD2(Overloaded, bool(bool f, int n))
737 template <
typename T>
755 #define MY_MOCK_METHODS2_ \
756 MOCK_CONST_METHOD1(Overloaded, int(int n)); \
757 MOCK_METHOD1(Overloaded, int(int n))
771 TEST(MockMethodOverloadedMockMethodTest, CanOverloadOnConstnessInMacroBody) {
781 TEST(MockMethodMockFunctionTest, WorksForVoidNullary) {
782 MockFunction<void()>
foo;
787 TEST(MockMethodMockFunctionTest, WorksForNonVoidNullary) {
788 MockFunction<int()>
foo;
794 TEST(MockMethodMockFunctionTest, WorksForVoidUnary) {
795 MockFunction<void(int)>
foo;
800 TEST(MockMethodMockFunctionTest, WorksForNonVoidBinary) {
801 MockFunction<int(bool, int)>
foo;
809 TEST(MockMethodMockFunctionTest, WorksFor10Arguments) {
810 MockFunction<int(
bool a0,
char a1,
int a2,
int a3,
int a4,
int a5,
int a6,
811 char a7,
int a8,
bool a9)>
813 EXPECT_CALL(foo, Call(
_,
'a',
_,
_,
_,
_,
_,
_,
_,
_))
816 EXPECT_EQ(1, foo.Call(
false,
'a', 0, 0, 0, 0, 0,
'b', 0,
true));
817 EXPECT_EQ(2, foo.Call(
true,
'a', 0, 0, 0, 0, 0,
'b', 1,
false));
820 TEST(MockMethodMockFunctionTest, AsStdFunction) {
821 MockFunction<int(int)>
foo;
822 auto call = [](
const std::function<int(int)>& f,
int i) {
return f(i); };
825 EXPECT_EQ(-1, call(foo.AsStdFunction(), 1));
826 EXPECT_EQ(-2, call(foo.AsStdFunction(), 2));
829 TEST(MockMethodMockFunctionTest, AsStdFunctionReturnsReference) {
830 MockFunction<int&()>
foo;
833 int& ref = foo.AsStdFunction()();
839 TEST(MockMethodMockFunctionTest, AsStdFunctionWithReferenceParameter) {
840 MockFunction<int(int&)>
foo;
841 auto call = [](
const std::function<int(int&)>& f,
int&
i) {
return f(i); };
849 template <
typename Expected,
typename F>
850 static constexpr
bool IsMockFunctionTemplateArgumentDeducedTo(
851 const internal::MockFunction<F>&) {
857 template <
typename F>
862 int(
bool,
char,
int,
int,
int,
int,
int,
char,
int,
bool)>;
867 IsMockFunctionTemplateArgumentDeducedForRawSignature) {
868 using Argument = TypeParam;
869 MockFunction<Argument>
foo;
870 EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<TypeParam>(foo));
874 IsMockFunctionTemplateArgumentDeducedForStdFunction) {
875 using Argument = std::function<TypeParam>;
876 MockFunction<Argument>
foo;
877 EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<TypeParam>(foo));
882 IsMockFunctionCallMethodSignatureTheSameForRawSignatureAndStdFunction) {
883 using ForRawSignature = decltype(&MockFunction<TypeParam>::Call);
884 using ForStdFunction =
885 decltype(&MockFunction<std::function<TypeParam>>::Call);
889 template <
typename F>
893 IsMockFunctionTemplateArgumentDeducedForAlternateCallable) {
895 MockFunction<Argument>
foo;
896 EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<TypeParam>(foo));
900 IsMockFunctionCallMethodSignatureTheSameForAlternateCallable) {
901 using ForRawSignature = decltype(&MockFunction<TypeParam>::Call);
902 using ForStdFunction =
903 decltype(&MockFunction<std::function<TypeParam>>::Call);
939 TEST(MockMethodMockFunctionTest, MockMethodSizeOverhead) {
953 TEST(MockMethodMockFunctionTest, EnsureNoUnusedMemberFunction) {
955 #pragma clang diagnostic push
956 #pragma clang diagnostic error "-Wunused-member-function"
964 #pragma clang diagnostic pop
983 TEST(MockMethodMockFunctionTest, NoexceptSpecifierPreserved) {
986 EXPECT_FALSE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func3()));
987 EXPECT_FALSE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func4()));
988 EXPECT_TRUE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func5()));
989 EXPECT_TRUE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func6()));
990 EXPECT_TRUE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func7()));
991 EXPECT_EQ(noexcept(std::declval<MockMethodNoexceptSpecifier>().func8()),
virtual bool Unary(int x)=0
MockOverloadedOnConstness()=default
#define MOCK_METHOD1_T_WITH_CALLTYPE(ct, m,...)
int RefQualifiedOverloaded()&&override
virtual int GetSize() const =0
MOCK_CONST_METHOD1_T(ReturnTypeWithComma, std::map< int, int >(int))
virtual int TypeWithTemplatedCopyCtor(const TemplatedCopyable< int > &)=0
MOCK_METHOD(void, DoB,())
GTEST_DISABLE_MSC_WARNINGS_POP_() TEST(LinkTest
TEST(FunctionMockerTest, RefQualified)
MOCK_METHOD0(func, void())
#define MOCK_METHOD1_WITH_CALLTYPE(ct, m,...)
int RefQualifiedOverloaded() const &override
int RefQualifiedConstRef() const &override
MOCK_METHOD1_T(Push, void(const T &elem))
int RefQualifiedRef()&override
const T func2(T &a, T &b)
virtual int OverloadedOnConstness()=0
LegacyMockB & operator=(const LegacyMockB &)=delete
void DoesntThrow() noexcept
virtual int RefQualifiedOverloaded() const &=0
int RefQualifiedRefRef()&&override
MOCK_METHOD(void, func,())
#define MOCK_METHOD0_WITH_CALLTYPE(ct, m,...)
MOCK_METHOD(void, func,(int, int, int, int))
MockStack & operator=(const MockStack &)=delete
MOCK_METHOD1(VoidReturning, void(int n))
MOCK_METHOD1(func, void(int))
MOCK_METHOD0_T(Pop, void())
virtual int OverloadedOnArgumentNumber()=0
virtual int(*)(bool) ReturnsFunctionPointer1(int)
MOCK_METHOD4(func, void(int, int, int, int))
GTEST_DISABLE_MSC_WARNINGS_POP_() template< class T > class FunctionMockerTest
virtual int RefQualifiedRefRef()&&=0
#define MOCK_CONST_METHOD1_WITH_CALLTYPE(ct, m,...)
internal::ReturnRefAction< R > ReturnRef(R &x)
TemplatedCopyable(const U &other)
virtual int RefQualifiedRef()&=0
LegacyMockStack()=default
MockOverloadedOnConstness & operator=(const MockOverloadedOnConstness &)=delete
MOCK_METHOD(void, func,(int, int, int))
LegacyMockOverloadedOnArgNumber()=default
virtual void VoidReturning(int x)=0
expr expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c *expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 c
void hasTwoParams(int, int)
virtual ~StackInterface()=default
#define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
MOCK_METHOD(void, func,(int))
virtual int TypeWithHole(int(*func)())=0
virtual int OverloadedOnArgumentType(int n)=0
MOCK_METHOD0(Nullary, int())
virtual int RefQualifiedConstRefRef() const &&=0
MockOverloadedOnArgNumber & operator=(const MockOverloadedOnArgNumber &)=delete
ADVar foo(double d, ADVar x, ADVar y)
MOCK_CONST_METHOD0_T(GetSize, int())
MOCK_METHOD0(DoB, void())
virtual ~FooInterface()=default
MOCK_METHOD(void, Push,(const T &elem),())
virtual long Binary(short x, int y)=0
virtual bool TakesConst(const int x)=0
MockB & operator=(const MockB &)=delete
TYPED_TEST(FunctionMockerTest, MocksVoidFunction)
TYPED_TEST_SUITE(FunctionMockerTest, FunctionMockerTestTypes)
GTEST_API_ Cardinality AnyNumber()
internal::DoDefaultAction DoDefault()
internal::ReturnAction< R > Return(R value)
#define EXPECT_CALL(obj, call)
MOCK_METHOD2(Binary, long(short, int))
virtual int RefQualifiedConstRef() const &=0
#define EXPECT_EQ(val1, val2)
int RefQualifiedOverloaded()&override
virtual const T & GetTop() const =0
int RefQualifiedConstRefRef() const &&override
virtual std::string TakesConstReference(const int &n)=0
const T func(int n, T *x)
#define MOCK_METHOD0_T_WITH_CALLTYPE(ct, m,...)
MOCK_CONST_METHOD0(OverloadedOnConstness, char())
#define EXPECT_TRUE(condition)
MOCK_METHOD(void, func,(int, int))
TemplatedCopyable()=default
virtual fn_ptr ReturnsFunctionPointer2(int)=0
MOCK_METHOD3(func, void(int, int, int))
MOCK_METHOD2(func, void(int, int))
void func1(const T &x1, const T &x2, T &y)
virtual void Push(const T &value)=0
#define MOCK_METHOD10_WITH_CALLTYPE(ct, m,...)
MockOverloadedOnArgNumber()=default
LegacyMockFoo & operator=(const LegacyMockFoo &)=delete
virtual bool TakesNonConstReference(int &n)=0
#define EXPECT_FALSE(condition)
virtual int TypeWithComma(const std::map< int, std::string > &a_map)=0
LegacyMockStack & operator=(const LegacyMockStack &)=delete
#define MOCK_CONST_METHOD0_T_WITH_CALLTYPE(ct, m,...)
MOCK_METHOD10(Decimal, int(bool, char, short, int, long, float, double, unsigned, char *, const std::string &str))
MOCK_CONST_METHOD1(ReturnTypeWithComma, std::map< int, std::string >(int))
const char * Binary(const char *input, short n)
int RefQualifiedOverloaded() const &&override
virtual int Decimal(bool b, char c, short d, int e, long f, float g, double h, unsigned i, char *j, const std::string &k)=0
LegacyMockOverloadedOnArgNumber & operator=(const LegacyMockOverloadedOnArgNumber &)=delete