43 #endif // GTEST_OS_WINDOWS
53 #if !GTEST_OS_WINDOWS || (_MSC_VER >= 1500)
54 # define GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
55 #endif // !GTEST_OS_WINDOWS || (_MSC_VER >= 1500)
58 namespace gmock_generated_function_mockers_test {
69 using testing::MockFunction;
82 virtual bool Unary(
int x) = 0;
83 virtual long Binary(
short x,
int y) = 0;
84 virtual int Decimal(
bool b,
char c,
short d,
int e,
long f,
85 float g,
double h,
unsigned i,
char*
j,
const string&
k)
90 #ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
92 #endif // GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
104 virtual int TypeWithComma(
const std::map<int, string>& a_map) = 0;
107 STDMETHOD_(
int, CTNullary)() = 0;
108 STDMETHOD_(
bool, CTUnary)(
int x) = 0;
109 STDMETHOD_(
int, CTDecimal)(
bool b,
char c,
short d,
int e,
long f,
110 float g,
double h,
unsigned i,
char*
j,
const string&
k) = 0;
111 STDMETHOD_(
char, CTConst)(
int x)
const = 0;
112 #endif // GTEST_OS_WINDOWS
120 # pragma warning(push)
121 # pragma warning(disable : 4373)
136 double,
unsigned,
char*,
const string&
str));
141 #ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
146 MOCK_METHOD0(ReturnTypeWithComma, std::map<int, string>());
148 std::map<int, string>(
int));
166 short d,
int e,
long f,
float g,
double h,
unsigned i,
char*
j,
172 std::map<int, string>());
173 #endif // GTEST_OS_WINDOWS
179 # pragma warning(pop)
193 foo_->VoidReturning(0);
226 EXPECT_CALL(mock_foo_, Decimal(
true,
'a', 0, 0, 1
L, A<float>(),
227 Lt(100), 5U, NULL,
"hi"))
230 EXPECT_EQ(5, foo_->Decimal(
true,
'a', 0, 0, 1, 0, 0, 5, NULL,
"hi"));
246 .WillOnce(
Return(
"Hello"));
248 EXPECT_EQ(
"Hello", foo_->TakesConstReference(a));
251 #ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
259 #endif // GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
263 EXPECT_CALL(mock_foo_, OverloadedOnArgumentNumber())
268 EXPECT_EQ(2, foo_->OverloadedOnArgumentNumber(1));
269 EXPECT_EQ(1, foo_->OverloadedOnArgumentNumber());
274 EXPECT_CALL(mock_foo_, OverloadedOnArgumentType(An<int>()))
276 EXPECT_CALL(mock_foo_, OverloadedOnArgumentType(TypedEq<char>(
'a')))
279 EXPECT_EQ(1, foo_->OverloadedOnArgumentType(0));
280 EXPECT_EQ(
'b', foo_->OverloadedOnArgumentType(
'a'));
289 EXPECT_EQ(0, foo_->OverloadedOnConstness());
294 const std::map<int, string> a_map;
300 EXPECT_EQ(a_map, mock_foo_.ReturnTypeWithComma());
301 EXPECT_EQ(a_map, mock_foo_.ReturnTypeWithComma(42));
306 TEST_F(FunctionMockerTest, MocksNullaryFunctionWithCallType) {
316 TEST_F(FunctionMockerTest, MocksUnaryFunctionWithCallType) {
327 TEST_F(FunctionMockerTest, MocksDecimalFunctionWithCallType) {
328 EXPECT_CALL(mock_foo_, CTDecimal(
true,
'a', 0, 0, 1
L, A<float>(),
329 Lt(100), 5U, NULL,
"hi"))
332 EXPECT_EQ(10, foo_->CTDecimal(
true,
'a', 0, 0, 1, 0, 0, 5, NULL,
"hi"));
336 TEST_F(FunctionMockerTest, MocksFunctionsConstFunctionWithCallType) {
343 TEST_F(FunctionMockerTest, MocksReturnTypeWithCommaAndCallType) {
344 const std::map<int, string> a_map;
348 EXPECT_EQ(a_map, mock_foo_.CTReturnTypeWithComma());
351 #endif // GTEST_OS_WINDOWS
365 TEST(ExpectCallTest, UnmentionedFunctionCanBeCalledAnyNumberOfTimes) {
384 template <
typename T>
391 virtual void Pop() = 0;
392 virtual int GetSize()
const = 0;
394 virtual const T&
GetTop()
const = 0;
397 template <
typename T>
416 TEST(TemplateMockTest, Works) {
438 TEST(TemplateMockTest, MethodWithCommaInReturnTypeWorks) {
441 const std::map<int, int> a_map;
447 EXPECT_EQ(a_map, mock.ReturnTypeWithComma());
448 EXPECT_EQ(a_map, mock.ReturnTypeWithComma(1));
454 template <
typename T>
455 class StackInterfaceWithCallType {
457 virtual ~StackInterfaceWithCallType() {}
460 STDMETHOD_(
void, Push)(
const T&
value) = 0;
461 STDMETHOD_(
void, Pop)() = 0;
462 STDMETHOD_(
int, GetSize)()
const = 0;
464 STDMETHOD_(
const T&, GetTop)()
const = 0;
467 template <
typename T>
468 class MockStackWithCallType :
public StackInterfaceWithCallType<T> {
470 MockStackWithCallType() {}
482 TEST(TemplateMockTestWithCallType, Works) {
483 MockStackWithCallType<int> mock;
503 #endif // GTEST_OS_WINDOWS
505 #define MY_MOCK_METHODS1_ \
506 MOCK_METHOD0(Overloaded, void()); \
507 MOCK_CONST_METHOD1(Overloaded, int(int n)); \
508 MOCK_METHOD2(Overloaded, bool(bool f, int n))
520 TEST(OverloadedMockMethodTest, CanOverloadOnArgNumberInMacroBody) {
531 #define MY_MOCK_METHODS2_ \
532 MOCK_CONST_METHOD1(Overloaded, int(int n)); \
533 MOCK_METHOD1(Overloaded, int(int n));
545 TEST(OverloadedMockMethodTest, CanOverloadOnConstnessInMacroBody) {
555 TEST(MockFunctionTest, WorksForVoidNullary) {
556 MockFunction<void()>
foo;
561 TEST(MockFunctionTest, WorksForNonVoidNullary) {
562 MockFunction<int()>
foo;
570 TEST(MockFunctionTest, WorksForVoidUnary) {
571 MockFunction<void(int)>
foo;
576 TEST(MockFunctionTest, WorksForNonVoidBinary) {
577 MockFunction<int(bool, int)>
foo;
588 TEST(MockFunctionTest, WorksFor10Arguments) {
589 MockFunction<int(
bool a0,
char a1,
int a2,
int a3,
int a4,
590 int a5,
int a6,
char a7,
int a8,
bool a9)>
foo;
591 EXPECT_CALL(foo, Call(
_,
'a',
_,
_,
_,
_,
_,
_,
_,
_))
594 EXPECT_EQ(1, foo.Call(
false,
'a', 0, 0, 0, 0, 0,
'b', 0,
true));
595 EXPECT_EQ(2, foo.Call(
true,
'a', 0, 0, 0, 0, 0,
'b', 1,
false));
598 #if GTEST_HAS_STD_FUNCTION_
599 TEST(MockFunctionTest, AsStdFunction) {
601 auto call = [](
const std::function<int(int)> &
f,
int i) {
606 EXPECT_EQ(-1, call(foo.AsStdFunction(), 1));
607 EXPECT_EQ(-2, call(foo.AsStdFunction(), 2));
610 TEST(MockFunctionTest, AsStdFunctionReturnsReference) {
614 int&
ref = foo.AsStdFunction()();
619 #endif // GTEST_HAS_STD_FUNCTION_