34 #include "gmock/gmock-generated-function-mockers.h"
41 #endif // GTEST_OS_WINDOWS
45 #include "gmock/gmock.h"
46 #include "gtest/gtest.h"
49 namespace gmock_function_mocker_test {
81 virtual bool Unary(
int x) = 0;
82 virtual long Binary(
short x,
int y) = 0;
83 virtual int Decimal(
bool b,
char c,
short d,
int e,
long f,
84 float g,
double h,
unsigned i,
char* j,
101 virtual int TypeWithComma(
const std::map<int, std::string>& a_map) = 0;
105 STDMETHOD_(
int, CTNullary)() = 0;
106 STDMETHOD_(
bool, CTUnary)(
int x) = 0;
107 STDMETHOD_(
int, CTDecimal)
108 (
bool b,
char c,
short d,
int e,
long f,
109 float g,
double h,
unsigned i,
char* j,
const std::string&
k) = 0;
110 STDMETHOD_(
char, CTConst)(
int x)
const = 0;
111 #endif // GTEST_OS_WINDOWS
119 # pragma warning(push)
120 # pragma warning(disable : 4373)
135 (
bool,
char,
short,
int,
long,
float,
144 MOCK_METHOD((std::map<int, std::string>), ReturnTypeWithComma, (), ());
145 MOCK_METHOD((std::map<int, std::string>), ReturnTypeWithComma, (
int),
163 MOCK_METHOD(
int, CTNullary, (), (Calltype(STDMETHODCALLTYPE)));
164 MOCK_METHOD(
bool, CTUnary, (
int), (Calltype(STDMETHODCALLTYPE)));
166 (
bool b,
char c,
short d,
int e,
long f,
float g,
double h,
168 (Calltype(STDMETHODCALLTYPE)));
169 MOCK_METHOD(
char, CTConst, (
int), (
const, Calltype(STDMETHODCALLTYPE)));
170 MOCK_METHOD((std::map<int, std::string>), CTReturnTypeWithComma, (),
171 (Calltype(STDMETHODCALLTYPE)));
172 #endif // GTEST_OS_WINDOWS
178 # pragma warning(pop)
192 foo_->VoidReturning(0);
225 EXPECT_CALL(mock_foo_, Decimal(
true,
'a', 0, 0, 1
L, A<float>(),
226 Lt(100), 5U, NULL,
"hi"))
229 EXPECT_EQ(5, foo_->Decimal(
true,
'a', 0, 0, 1, 0, 0, 5,
nullptr,
"hi"));
234 MocksFunctionWithNonConstReferenceArgument) {
246 .WillOnce(
Return(
"Hello"));
248 EXPECT_EQ(
"Hello", foo_->TakesConstReference(
a));
261 EXPECT_CALL(mock_foo_, OverloadedOnArgumentNumber())
266 EXPECT_EQ(2, foo_->OverloadedOnArgumentNumber(1));
267 EXPECT_EQ(1, foo_->OverloadedOnArgumentNumber());
272 EXPECT_CALL(mock_foo_, OverloadedOnArgumentType(An<int>()))
274 EXPECT_CALL(mock_foo_, OverloadedOnArgumentType(TypedEq<char>(
'a')))
277 EXPECT_EQ(1, foo_->OverloadedOnArgumentType(0));
278 EXPECT_EQ(
'b', foo_->OverloadedOnArgumentType(
'a'));
283 MocksFunctionsOverloadedOnConstnessOfThis) {
288 EXPECT_EQ(0, foo_->OverloadedOnConstness());
293 const std::map<int, std::string> a_map;
299 EXPECT_EQ(a_map, mock_foo_.ReturnTypeWithComma());
300 EXPECT_EQ(a_map, mock_foo_.ReturnTypeWithComma(42));
310 TEST_F(MockMethodFunctionMockerTest, MocksNullaryFunctionWithCallType) {
320 TEST_F(MockMethodFunctionMockerTest, MocksUnaryFunctionWithCallType) {
331 TEST_F(MockMethodFunctionMockerTest, MocksDecimalFunctionWithCallType) {
333 Lt(100), 5U, NULL,
"hi"))
336 EXPECT_EQ(10, foo_->CTDecimal(
true,
'a', 0, 0, 1, 0, 0, 5, NULL,
"hi"));
340 TEST_F(MockMethodFunctionMockerTest, MocksFunctionsConstFunctionWithCallType) {
347 TEST_F(MockMethodFunctionMockerTest, MocksReturnTypeWithCommaAndCallType) {
348 const std::map<int, std::string> a_map;
352 EXPECT_EQ(a_map, mock_foo_.CTReturnTypeWithComma());
355 #endif // GTEST_OS_WINDOWS
369 TEST(MockMethodExpectCallTest, UnmentionedFunctionCanBeCalledAnyNumberOfTimes) {
388 template <
typename T>
395 virtual void Pop() = 0;
396 virtual int GetSize()
const = 0;
398 virtual const T&
GetTop()
const = 0;
401 template <
typename T>
412 MOCK_METHOD((std::map<int, int>), ReturnTypeWithComma, (), ());
413 MOCK_METHOD((std::map<int, int>), ReturnTypeWithComma, (
int), (
const));
420 TEST(MockMethodTemplateMockTest, Works) {
442 TEST(MockMethodTemplateMockTest, MethodWithCommaInReturnTypeWorks) {
445 const std::map<int, int> a_map;
451 EXPECT_EQ(a_map, mock.ReturnTypeWithComma());
452 EXPECT_EQ(a_map, mock.ReturnTypeWithComma(1));
458 template <
typename T>
459 class StackInterfaceWithCallType {
461 virtual ~StackInterfaceWithCallType() {}
464 STDMETHOD_(
void, Push)(
const T&
value) = 0;
465 STDMETHOD_(
void,
Pop)() = 0;
466 STDMETHOD_(
int, GetSize)()
const = 0;
468 STDMETHOD_(
const T&, GetTop)()
const = 0;
471 template <
typename T>
472 class MockStackWithCallType :
public StackInterfaceWithCallType<T> {
474 MockStackWithCallType() {}
477 (Calltype(STDMETHODCALLTYPE),
override));
478 MOCK_METHOD(
void,
Pop, (), (Calltype(STDMETHODCALLTYPE),
override));
479 MOCK_METHOD(
int, GetSize, (), (Calltype(STDMETHODCALLTYPE),
override,
const));
481 (Calltype(STDMETHODCALLTYPE),
override,
const));
488 TEST(MockMethodTemplateMockTestWithCallType, Works) {
489 MockStackWithCallType<int> mock;
509 #endif // GTEST_OS_WINDOWS
511 #define MY_MOCK_METHODS1_ \
512 MOCK_METHOD(void, Overloaded, ()); \
513 MOCK_METHOD(int, Overloaded, (int), (const)); \
514 MOCK_METHOD(bool, Overloaded, (bool f, int n))
526 TEST(MockMethodOverloadedMockMethodTest, CanOverloadOnArgNumberInMacroBody) {
537 #define MY_MOCK_METHODS2_ \
538 MOCK_CONST_METHOD1(Overloaded, int(int n)); \
539 MOCK_METHOD1(Overloaded, int(int n))
551 TEST(MockMethodOverloadedMockMethodTest, CanOverloadOnConstnessInMacroBody) {
561 TEST(MockMethodMockFunctionTest, WorksForVoidNullary) {
567 TEST(MockMethodMockFunctionTest, WorksForNonVoidNullary) {
576 TEST(MockMethodMockFunctionTest, WorksForVoidUnary) {
582 TEST(MockMethodMockFunctionTest, WorksForNonVoidBinary) {
594 TEST(MockMethodMockFunctionTest, WorksFor10Arguments) {
596 int a5,
int a6,
char a7,
int a8,
bool a9)>
foo;
597 EXPECT_CALL(
foo,
Call(
_,
'a',
_,
_,
_,
_,
_,
_,
_,
_))
600 EXPECT_EQ(1,
foo.Call(
false,
'a', 0, 0, 0, 0, 0,
'b', 0,
true));
601 EXPECT_EQ(2,
foo.Call(
true,
'a', 0, 0, 0, 0, 0,
'b', 1,
false));
604 TEST(MockMethodMockFunctionTest, AsStdFunction) {
615 TEST(MockMethodMockFunctionTest, AsStdFunctionReturnsReference) {
619 int&
ref =
foo.AsStdFunction()();
625 TEST(MockMethodMockFunctionTest, AsStdFunctionWithReferenceParameter) {
652 TEST(MockMethodMockFunctionTest, MockMethodSizeOverhead) {