41 #endif // GTEST_OS_WINDOWS
46 #include <type_traits>
52 namespace gmock_function_mocker_test {
62 using testing::MockFunction;
66 using testing::TypedEq;
84 virtual bool Unary(
int x) = 0;
85 virtual long Binary(
short x,
int y) = 0;
86 virtual int Decimal(
bool b,
char c,
short d,
int e,
long f,
87 float g,
double h,
unsigned i,
char* j,
88 const std::string& k) = 0;
104 virtual int TypeWithComma(
const std::map<int, std::string>& a_map) = 0;
112 STDMETHOD_(
int, CTNullary)() = 0;
113 STDMETHOD_(
bool, CTUnary)(
int x) = 0;
114 STDMETHOD_(
int, CTDecimal)
115 (
bool b,
char c,
short d,
int e,
long f,
116 float g,
double h,
unsigned i,
char* j,
const std::string& k) = 0;
117 STDMETHOD_(
char, CTConst)(
int x)
const = 0;
118 #endif // GTEST_OS_WINDOWS
126 # pragma warning(push)
127 # pragma warning(disable : 4373)
142 (
bool,
char,
short,
int,
long,
float,
143 double,
unsigned,
char*,
const std::string& str),
151 MOCK_METHOD((std::map<int, std::string>), ReturnTypeWithComma, (), ());
152 MOCK_METHOD((std::map<int, std::string>), ReturnTypeWithComma, (
int),
173 MOCK_METHOD(
int, CTNullary, (), (Calltype(STDMETHODCALLTYPE)));
174 MOCK_METHOD(
bool, CTUnary, (
int), (Calltype(STDMETHODCALLTYPE)));
176 (
bool b,
char c,
short d,
int e,
long f,
float g,
double h,
177 unsigned i,
char* j,
const std::string& k),
178 (Calltype(STDMETHODCALLTYPE)));
179 MOCK_METHOD(
char, CTConst, (
int), (
const, Calltype(STDMETHODCALLTYPE)));
180 MOCK_METHOD((std::map<int, std::string>), CTReturnTypeWithComma, (),
181 (Calltype(STDMETHODCALLTYPE)));
182 #endif // GTEST_OS_WINDOWS
201 double,
unsigned,
char*,
const std::string& str));
208 MOCK_METHOD0(ReturnTypeWithComma, std::map<int, std::string>());
210 std::map<int, std::string>(
int));
223 int(
const std::map<int, std::string>&));
234 int(
bool b,
char c,
short d,
int e,
235 long f,
float g,
double h,
236 unsigned i,
char* j,
const std::string& k));
242 std::map<int, std::string>());
243 #endif // GTEST_OS_WINDOWS
250 # pragma warning(pop)
266 EXPECT_CALL(this->mock_foo_, VoidReturning(Lt(100)));
267 this->foo_->VoidReturning(0);
298 Decimal(
true,
'a', 0, 0, 1L, A<float>(), Lt(100), 5U, NULL,
"hi"))
301 EXPECT_EQ(5, this->foo_->Decimal(
true,
'a', 0, 0, 1, 0, 0, 5,
nullptr,
"hi"));
307 EXPECT_CALL(this->mock_foo_, TakesNonConstReference(Ref(a)))
310 EXPECT_TRUE(this->foo_->TakesNonConstReference(a));
316 EXPECT_CALL(this->mock_foo_, TakesConstReference(Ref(a)))
317 .WillOnce(
Return(
"Hello"));
319 EXPECT_EQ(
"Hello", this->foo_->TakesConstReference(a));
331 EXPECT_CALL(this->mock_foo_, OverloadedOnArgumentNumber())
333 EXPECT_CALL(this->mock_foo_, OverloadedOnArgumentNumber(
_))
336 EXPECT_EQ(2, this->foo_->OverloadedOnArgumentNumber(1));
337 EXPECT_EQ(1, this->foo_->OverloadedOnArgumentNumber());
342 EXPECT_CALL(this->mock_foo_, OverloadedOnArgumentType(An<int>()))
344 EXPECT_CALL(this->mock_foo_, OverloadedOnArgumentType(TypedEq<char>(
'a')))
347 EXPECT_EQ(1, this->foo_->OverloadedOnArgumentType(0));
348 EXPECT_EQ(
'b', this->foo_->OverloadedOnArgumentType(
'a'));
353 EXPECT_CALL(this->mock_foo_, OverloadedOnConstness());
354 EXPECT_CALL(Const(this->mock_foo_), OverloadedOnConstness())
357 EXPECT_EQ(0, this->foo_->OverloadedOnConstness());
358 EXPECT_EQ(
'a', Const(*this->foo_).OverloadedOnConstness());
362 const std::map<int, std::string> a_map;
366 EXPECT_EQ(a_map, this->mock_foo_.ReturnTypeWithComma());
367 EXPECT_EQ(a_map, this->mock_foo_.ReturnTypeWithComma(42));
371 EXPECT_CALL(this->mock_foo_, TypeWithTemplatedCopyCtor(
_))
378 TYPED_TEST(FunctionMockerTest, MocksNullaryFunctionWithCallType) {
388 TYPED_TEST(FunctionMockerTest, MocksUnaryFunctionWithCallType) {
399 TYPED_TEST(FunctionMockerTest, MocksDecimalFunctionWithCallType) {
401 Lt(100), 5U, NULL,
"hi"))
404 EXPECT_EQ(10, this->foo_->CTDecimal(
true,
'a', 0, 0, 1, 0, 0, 5, NULL,
"hi"));
408 TYPED_TEST(FunctionMockerTest, MocksFunctionsConstFunctionWithCallType) {
411 EXPECT_EQ(
'a', Const(*this->foo_).CTConst(0));
414 TYPED_TEST(FunctionMockerTest, MocksReturnTypeWithCommaAndCallType) {
415 const std::map<int, std::string> a_map;
418 EXPECT_EQ(a_map, this->mock_foo_.CTReturnTypeWithComma());
421 #endif // GTEST_OS_WINDOWS
443 template <
typename T>
467 template <
typename T>
474 virtual void Pop() = 0;
475 virtual int GetSize()
const = 0;
477 virtual const T&
GetTop()
const = 0;
480 template <
typename T>
491 MOCK_METHOD((std::map<int, int>), ReturnTypeWithComma, (), ());
492 MOCK_METHOD((std::map<int, int>), ReturnTypeWithComma, (
int), (
const));
498 template <
typename T>
516 template <
typename T>
548 const std::map<int, int> a_map;
554 EXPECT_EQ(a_map, mock.ReturnTypeWithComma());
555 EXPECT_EQ(a_map, mock.ReturnTypeWithComma(1));
561 template <
typename T>
562 class StackInterfaceWithCallType {
564 virtual ~StackInterfaceWithCallType() {}
567 STDMETHOD_(
void, Push)(
const T&
value) = 0;
568 STDMETHOD_(
void, Pop)() = 0;
569 STDMETHOD_(
int, GetSize)()
const = 0;
571 STDMETHOD_(
const T&, GetTop)()
const = 0;
574 template <
typename T>
575 class MockStackWithCallType :
public StackInterfaceWithCallType<T> {
577 MockStackWithCallType() {}
580 (Calltype(STDMETHODCALLTYPE),
override));
581 MOCK_METHOD(
void, Pop, (), (Calltype(STDMETHODCALLTYPE),
override));
582 MOCK_METHOD(
int, GetSize, (), (Calltype(STDMETHODCALLTYPE),
override,
const));
584 (Calltype(STDMETHODCALLTYPE),
override,
const));
590 template <
typename T>
591 class LegacyMockStackWithCallType :
public StackInterfaceWithCallType<T> {
593 LegacyMockStackWithCallType() {}
604 template <
typename T>
606 using TemplateMockTestWithCallTypeTypes =
608 LegacyMockStackWithCallType<int>>;
610 TemplateMockTestWithCallTypeTypes);
613 TYPED_TEST(TemplateMockTestWithCallType, Works) {
634 #endif // GTEST_OS_WINDOWS
636 #define MY_MOCK_METHODS1_ \
637 MOCK_METHOD(void, Overloaded, ()); \
638 MOCK_METHOD(int, Overloaded, (int), (const)); \
639 MOCK_METHOD(bool, Overloaded, (bool f, int n))
641 #define LEGACY_MY_MOCK_METHODS1_ \
642 MOCK_METHOD0(Overloaded, void()); \
643 MOCK_CONST_METHOD1(Overloaded, int(int n)); \
644 MOCK_METHOD2(Overloaded, bool(bool f, int n))
666 template <
typename T>
684 #define MY_MOCK_METHODS2_ \
685 MOCK_CONST_METHOD1(Overloaded, int(int n)); \
686 MOCK_METHOD1(Overloaded, int(int n))
698 TEST(MockMethodOverloadedMockMethodTest, CanOverloadOnConstnessInMacroBody) {
708 TEST(MockMethodMockFunctionTest, WorksForVoidNullary) {
709 MockFunction<void()>
foo;
714 TEST(MockMethodMockFunctionTest, WorksForNonVoidNullary) {
715 MockFunction<int()>
foo;
723 TEST(MockMethodMockFunctionTest, WorksForVoidUnary) {
724 MockFunction<void(int)>
foo;
729 TEST(MockMethodMockFunctionTest, WorksForNonVoidBinary) {
730 MockFunction<int(bool, int)>
foo;
741 TEST(MockMethodMockFunctionTest, WorksFor10Arguments) {
742 MockFunction<int(
bool a0,
char a1,
int a2,
int a3,
int a4,
743 int a5,
int a6,
char a7,
int a8,
bool a9)>
foo;
744 EXPECT_CALL(foo, Call(
_,
'a',
_,
_,
_,
_,
_,
_,
_,
_))
747 EXPECT_EQ(1, foo.Call(
false,
'a', 0, 0, 0, 0, 0,
'b', 0,
true));
748 EXPECT_EQ(2, foo.Call(
true,
'a', 0, 0, 0, 0, 0,
'b', 1,
false));
751 TEST(MockMethodMockFunctionTest, AsStdFunction) {
752 MockFunction<int(int)>
foo;
753 auto call = [](
const std::function<int(int)> &f,
int i) {
758 EXPECT_EQ(-1, call(foo.AsStdFunction(), 1));
759 EXPECT_EQ(-2, call(foo.AsStdFunction(), 2));
762 TEST(MockMethodMockFunctionTest, AsStdFunctionReturnsReference) {
763 MockFunction<int&()>
foo;
766 int& ref = foo.AsStdFunction()();
772 TEST(MockMethodMockFunctionTest, AsStdFunctionWithReferenceParameter) {
773 MockFunction<int(int &)>
foo;
774 auto call = [](
const std::function<int(int& )> &f,
int &
i) {
784 template <
typename Expected,
typename F>
785 static constexpr
bool IsMockFunctionTemplateArgumentDeducedTo(
786 const MockFunction<F>&) {
792 template <
typename F>
797 int(
bool,
char,
int,
int,
int,
int,
int,
char,
int,
bool)>;
802 IsMockFunctionTemplateArgumentDeducedForRawSignature) {
803 using Argument = TypeParam;
804 MockFunction<Argument>
foo;
805 EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<Argument>(foo));
809 IsMockFunctionTemplateArgumentDeducedForStdFunction) {
810 using Argument = std::function<TypeParam>;
811 MockFunction<Argument>
foo;
812 EXPECT_TRUE(IsMockFunctionTemplateArgumentDeducedTo<Argument>(foo));
817 IsMockFunctionCallMethodSignatureTheSameForRawSignatureAndStdFunction) {
818 using ForRawSignature = decltype(&MockFunction<TypeParam>::Call);
819 using ForStdFunction =
820 decltype(&MockFunction<std::function<TypeParam>>::Call);
826 IsMockFunctionAsStdFunctionMethodSignatureTheSameForRawSignatureAndStdFunction) {
827 using ForRawSignature = decltype(&MockFunction<TypeParam>::AsStdFunction);
828 using ForStdFunction =
829 decltype(&MockFunction<std::function<TypeParam>>::AsStdFunction);
866 TEST(MockMethodMockFunctionTest, MockMethodSizeOverhead) {
895 TEST(MockMethodMockFunctionTest, NoexceptSpecifierPreserved) {
898 EXPECT_FALSE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func3()));
899 EXPECT_FALSE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func4()));
900 EXPECT_TRUE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func5()));
901 EXPECT_TRUE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func6()));
902 EXPECT_TRUE(noexcept(std::declval<MockMethodNoexceptSpecifier>().func7()));
903 EXPECT_EQ(noexcept(std::declval<MockMethodNoexceptSpecifier>().func8()),
virtual bool Unary(int x)=0
#define MOCK_METHOD1_T_WITH_CALLTYPE(ct, m,...)
virtual int GetSize() const =0
LegacyMockOverloadedOnArgNumber()
MOCK_CONST_METHOD1_T(ReturnTypeWithComma, std::map< int, int >(int))
virtual int TypeWithTemplatedCopyCtor(const TemplatedCopyable< int > &)=0
MOCK_METHOD(void, DoB,())
TEST(MockMethodOverloadedMockMethodTest, CanOverloadOnConstnessInMacroBody)
MOCK_METHOD0(func, void())
#define MOCK_METHOD1_WITH_CALLTYPE(ct, m,...)
GTEST_DISALLOW_COPY_AND_ASSIGN_(LegacyMockStack)
MOCK_METHOD1_T(Push, void(const T &elem))
GTEST_DISALLOW_COPY_AND_ASSIGN_(MockB)
Types< void(), int(), void(int), int(int), int(bool, int), int(bool, char, int, int, int, int, int, char, int, bool)> MockMethodMockFunctionSignatureTypes
::testing::Types< MockB, LegacyMockB > ExpectCallTestTypes
const T func2(T &a, T &b)
virtual int OverloadedOnConstness()=0
void DoesntThrow() noexcept
MOCK_METHOD(void, func,())
#define MOCK_METHOD0_WITH_CALLTYPE(ct, m,...)
MOCK_METHOD(void, func,(int, int, int, int))
MockOverloadedOnArgNumber()
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)
internal::ProxyTypeList< Ts...> Types
MOCK_METHOD4(func, void(int, int, int, int))
MOCK_METHOD(void, VoidReturning,(int n))
#define MOCK_CONST_METHOD1_WITH_CALLTYPE(ct, m,...)
internal::ReturnRefAction< R > ReturnRef(R &x)
TemplatedCopyable(const U &other)
MOCK_METHOD(void, func,(int, int, int))
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)
MockOverloadedOnConstness()
GTEST_DISALLOW_COPY_AND_ASSIGN_(LegacyMockB)
GTEST_DISALLOW_COPY_AND_ASSIGN_(LegacyMockFoo)
::testing::Types< MockStack< int >, LegacyMockStack< int >> TemplateMockTestTypes
MOCK_METHOD(void, func,(int))
virtual int TypeWithHole(int(*func)())=0
virtual int OverloadedOnArgumentType(int n)=0
MOCK_METHOD0(Nullary, int())
GTEST_DISALLOW_COPY_AND_ASSIGN_(MockStack)
ADVar foo(double d, ADVar x, ADVar y)
MOCK_CONST_METHOD0_T(GetSize, int())
MOCK_METHOD0(DoB, void())
const char * Binary(const char *input, short n)
MOCK_METHOD(void, Push,(const T &elem),())
virtual long Binary(short x, int y)=0
virtual bool TakesConst(const int x)=0
::testing::Types< MockOverloadedOnArgNumber, LegacyMockOverloadedOnArgNumber > OverloadedMockMethodTestTypes
TYPED_TEST(FunctionMockerTest, MocksVoidFunction)
TYPED_TEST_SUITE(FunctionMockerTest, FunctionMockerTestTypes)
GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo)
GTEST_API_ Cardinality AnyNumber()
internal::DoDefaultAction DoDefault()
internal::ReturnAction< R > Return(R value)
#define EXPECT_CALL(obj, call)
MOCK_METHOD2(Binary, long(short, int))
#define EXPECT_EQ(val1, val2)
GTEST_DISALLOW_COPY_AND_ASSIGN_(MockOverloadedOnConstness)
virtual const T & GetTop() const =0
GTEST_DISALLOW_COPY_AND_ASSIGN_(MockOverloadedOnArgNumber)
::testing::Types< MockFoo, LegacyMockFoo > FunctionMockerTestTypes
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
virtual std::string TakesConstReference(const int &n)=0
GTEST_DISALLOW_COPY_AND_ASSIGN_(LegacyMockOverloadedOnArgNumber)
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))
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 ~StackInterface()
virtual void Push(const T &value)=0
#define MOCK_METHOD10_WITH_CALLTYPE(ct, m,...)
virtual bool TakesNonConstReference(int &n)=0
#define EXPECT_FALSE(condition)
virtual int TypeWithComma(const std::map< int, std::string > &a_map)=0
#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))
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