00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "gmock/gmock-generated-function-mockers.h"
00037
00038 #if GTEST_OS_WINDOWS
00039
00040
00041
00042 # include <objbase.h>
00043 #endif // GTEST_OS_WINDOWS
00044
00045 #include <map>
00046 #include <string>
00047 #include "gmock/gmock.h"
00048 #include "gtest/gtest.h"
00049
00050
00051
00052
00053 #if !GTEST_OS_WINDOWS || (_MSC_VER >= 1500)
00054 # define GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
00055 #endif // !GTEST_OS_WINDOWS || (_MSC_VER >= 1500)
00056
00057 namespace testing {
00058 namespace gmock_generated_function_mockers_test {
00059
00060 using testing::internal::string;
00061 using testing::_;
00062 using testing::A;
00063 using testing::An;
00064 using testing::AnyNumber;
00065 using testing::Const;
00066 using testing::DoDefault;
00067 using testing::Eq;
00068 using testing::Lt;
00069 using testing::MockFunction;
00070 using testing::Ref;
00071 using testing::Return;
00072 using testing::ReturnRef;
00073 using testing::TypedEq;
00074
00075 class FooInterface {
00076 public:
00077 virtual ~FooInterface() {}
00078
00079 virtual void VoidReturning(int x) = 0;
00080
00081 virtual int Nullary() = 0;
00082 virtual bool Unary(int x) = 0;
00083 virtual long Binary(short x, int y) = 0;
00084 virtual int Decimal(bool b, char c, short d, int e, long f,
00085 float g, double h, unsigned i, char* j, const string& k)
00086 = 0;
00087
00088 virtual bool TakesNonConstReference(int& n) = 0;
00089 virtual string TakesConstReference(const int& n) = 0;
00090 #ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
00091 virtual bool TakesConst(const int x) = 0;
00092 #endif // GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
00093
00094 virtual int OverloadedOnArgumentNumber() = 0;
00095 virtual int OverloadedOnArgumentNumber(int n) = 0;
00096
00097 virtual int OverloadedOnArgumentType(int n) = 0;
00098 virtual char OverloadedOnArgumentType(char c) = 0;
00099
00100 virtual int OverloadedOnConstness() = 0;
00101 virtual char OverloadedOnConstness() const = 0;
00102
00103 virtual int TypeWithHole(int (*func)()) = 0;
00104 virtual int TypeWithComma(const std::map<int, string>& a_map) = 0;
00105
00106 #if GTEST_OS_WINDOWS
00107 STDMETHOD_(int, CTNullary)() = 0;
00108 STDMETHOD_(bool, CTUnary)(int x) = 0;
00109 STDMETHOD_(int, CTDecimal)(bool b, char c, short d, int e, long f,
00110 float g, double h, unsigned i, char* j, const string& k) = 0;
00111 STDMETHOD_(char, CTConst)(int x) const = 0;
00112 #endif // GTEST_OS_WINDOWS
00113 };
00114
00115 class MockFoo : public FooInterface {
00116 public:
00117 MockFoo() {}
00118
00119
00120 MOCK_METHOD1(VoidReturning, void(int n));
00121
00122 MOCK_METHOD0(Nullary, int());
00123
00124
00125 MOCK_METHOD1(Unary, bool(int));
00126 MOCK_METHOD2(Binary, long(short, int));
00127 MOCK_METHOD10(Decimal, int(bool, char, short, int, long, float,
00128 double, unsigned, char*, const string& str));
00129
00130 MOCK_METHOD1(TakesNonConstReference, bool(int&));
00131 MOCK_METHOD1(TakesConstReference, string(const int&));
00132
00133 #ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
00134 MOCK_METHOD1(TakesConst, bool(const int));
00135 #endif
00136
00137
00138 MOCK_METHOD0(ReturnTypeWithComma, std::map<int, string>());
00139 MOCK_CONST_METHOD1(ReturnTypeWithComma,
00140 std::map<int, string>(int));
00141
00142 MOCK_METHOD0(OverloadedOnArgumentNumber, int());
00143 MOCK_METHOD1(OverloadedOnArgumentNumber, int(int));
00144
00145 MOCK_METHOD1(OverloadedOnArgumentType, int(int));
00146 MOCK_METHOD1(OverloadedOnArgumentType, char(char));
00147
00148 MOCK_METHOD0(OverloadedOnConstness, int());
00149 MOCK_CONST_METHOD0(OverloadedOnConstness, char());
00150
00151 MOCK_METHOD1(TypeWithHole, int(int (*)()));
00152 MOCK_METHOD1(TypeWithComma, int(const std::map<int, string>&));
00153
00154 #if GTEST_OS_WINDOWS
00155 MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, CTNullary, int());
00156 MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, CTUnary, bool(int));
00157 MOCK_METHOD10_WITH_CALLTYPE(STDMETHODCALLTYPE, CTDecimal, int(bool b, char c,
00158 short d, int e, long f, float g, double h, unsigned i, char* j,
00159 const string& k));
00160 MOCK_CONST_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, CTConst, char(int));
00161
00162
00163 MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, CTReturnTypeWithComma,
00164 std::map<int, string>());
00165 #endif // GTEST_OS_WINDOWS
00166
00167 private:
00168 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
00169 };
00170
00171 class FunctionMockerTest : public testing::Test {
00172 protected:
00173 FunctionMockerTest() : foo_(&mock_foo_) {}
00174
00175 FooInterface* const foo_;
00176 MockFoo mock_foo_;
00177 };
00178
00179
00180 TEST_F(FunctionMockerTest, MocksVoidFunction) {
00181 EXPECT_CALL(mock_foo_, VoidReturning(Lt(100)));
00182 foo_->VoidReturning(0);
00183 }
00184
00185
00186 TEST_F(FunctionMockerTest, MocksNullaryFunction) {
00187 EXPECT_CALL(mock_foo_, Nullary())
00188 .WillOnce(DoDefault())
00189 .WillOnce(Return(1));
00190
00191 EXPECT_EQ(0, foo_->Nullary());
00192 EXPECT_EQ(1, foo_->Nullary());
00193 }
00194
00195
00196 TEST_F(FunctionMockerTest, MocksUnaryFunction) {
00197 EXPECT_CALL(mock_foo_, Unary(Eq(2)))
00198 .Times(2)
00199 .WillOnce(Return(true));
00200
00201 EXPECT_TRUE(foo_->Unary(2));
00202 EXPECT_FALSE(foo_->Unary(2));
00203 }
00204
00205
00206 TEST_F(FunctionMockerTest, MocksBinaryFunction) {
00207 EXPECT_CALL(mock_foo_, Binary(2, _))
00208 .WillOnce(Return(3));
00209
00210 EXPECT_EQ(3, foo_->Binary(2, 1));
00211 }
00212
00213
00214 TEST_F(FunctionMockerTest, MocksDecimalFunction) {
00215 EXPECT_CALL(mock_foo_, Decimal(true, 'a', 0, 0, 1L, A<float>(),
00216 Lt(100), 5U, NULL, "hi"))
00217 .WillOnce(Return(5));
00218
00219 EXPECT_EQ(5, foo_->Decimal(true, 'a', 0, 0, 1, 0, 0, 5, NULL, "hi"));
00220 }
00221
00222
00223 TEST_F(FunctionMockerTest, MocksFunctionWithNonConstReferenceArgument) {
00224 int a = 0;
00225 EXPECT_CALL(mock_foo_, TakesNonConstReference(Ref(a)))
00226 .WillOnce(Return(true));
00227
00228 EXPECT_TRUE(foo_->TakesNonConstReference(a));
00229 }
00230
00231
00232 TEST_F(FunctionMockerTest, MocksFunctionWithConstReferenceArgument) {
00233 int a = 0;
00234 EXPECT_CALL(mock_foo_, TakesConstReference(Ref(a)))
00235 .WillOnce(Return("Hello"));
00236
00237 EXPECT_EQ("Hello", foo_->TakesConstReference(a));
00238 }
00239
00240 #ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
00241
00242 TEST_F(FunctionMockerTest, MocksFunctionWithConstArgument) {
00243 EXPECT_CALL(mock_foo_, TakesConst(Lt(10)))
00244 .WillOnce(DoDefault());
00245
00246 EXPECT_FALSE(foo_->TakesConst(5));
00247 }
00248 #endif // GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
00249
00250
00251 TEST_F(FunctionMockerTest, MocksFunctionsOverloadedOnArgumentNumber) {
00252 EXPECT_CALL(mock_foo_, OverloadedOnArgumentNumber())
00253 .WillOnce(Return(1));
00254 EXPECT_CALL(mock_foo_, OverloadedOnArgumentNumber(_))
00255 .WillOnce(Return(2));
00256
00257 EXPECT_EQ(2, foo_->OverloadedOnArgumentNumber(1));
00258 EXPECT_EQ(1, foo_->OverloadedOnArgumentNumber());
00259 }
00260
00261
00262 TEST_F(FunctionMockerTest, MocksFunctionsOverloadedOnArgumentType) {
00263 EXPECT_CALL(mock_foo_, OverloadedOnArgumentType(An<int>()))
00264 .WillOnce(Return(1));
00265 EXPECT_CALL(mock_foo_, OverloadedOnArgumentType(TypedEq<char>('a')))
00266 .WillOnce(Return('b'));
00267
00268 EXPECT_EQ(1, foo_->OverloadedOnArgumentType(0));
00269 EXPECT_EQ('b', foo_->OverloadedOnArgumentType('a'));
00270 }
00271
00272
00273 TEST_F(FunctionMockerTest, MocksFunctionsOverloadedOnConstnessOfThis) {
00274 EXPECT_CALL(mock_foo_, OverloadedOnConstness());
00275 EXPECT_CALL(Const(mock_foo_), OverloadedOnConstness())
00276 .WillOnce(Return('a'));
00277
00278 EXPECT_EQ(0, foo_->OverloadedOnConstness());
00279 EXPECT_EQ('a', Const(*foo_).OverloadedOnConstness());
00280 }
00281
00282 TEST_F(FunctionMockerTest, MocksReturnTypeWithComma) {
00283 const std::map<int, string> a_map;
00284 EXPECT_CALL(mock_foo_, ReturnTypeWithComma())
00285 .WillOnce(Return(a_map));
00286 EXPECT_CALL(mock_foo_, ReturnTypeWithComma(42))
00287 .WillOnce(Return(a_map));
00288
00289 EXPECT_EQ(a_map, mock_foo_.ReturnTypeWithComma());
00290 EXPECT_EQ(a_map, mock_foo_.ReturnTypeWithComma(42));
00291 }
00292
00293 #if GTEST_OS_WINDOWS
00294
00295 TEST_F(FunctionMockerTest, MocksNullaryFunctionWithCallType) {
00296 EXPECT_CALL(mock_foo_, CTNullary())
00297 .WillOnce(Return(-1))
00298 .WillOnce(Return(0));
00299
00300 EXPECT_EQ(-1, foo_->CTNullary());
00301 EXPECT_EQ(0, foo_->CTNullary());
00302 }
00303
00304
00305 TEST_F(FunctionMockerTest, MocksUnaryFunctionWithCallType) {
00306 EXPECT_CALL(mock_foo_, CTUnary(Eq(2)))
00307 .Times(2)
00308 .WillOnce(Return(true))
00309 .WillOnce(Return(false));
00310
00311 EXPECT_TRUE(foo_->CTUnary(2));
00312 EXPECT_FALSE(foo_->CTUnary(2));
00313 }
00314
00315
00316 TEST_F(FunctionMockerTest, MocksDecimalFunctionWithCallType) {
00317 EXPECT_CALL(mock_foo_, CTDecimal(true, 'a', 0, 0, 1L, A<float>(),
00318 Lt(100), 5U, NULL, "hi"))
00319 .WillOnce(Return(10));
00320
00321 EXPECT_EQ(10, foo_->CTDecimal(true, 'a', 0, 0, 1, 0, 0, 5, NULL, "hi"));
00322 }
00323
00324
00325 TEST_F(FunctionMockerTest, MocksFunctionsConstFunctionWithCallType) {
00326 EXPECT_CALL(Const(mock_foo_), CTConst(_))
00327 .WillOnce(Return('a'));
00328
00329 EXPECT_EQ('a', Const(*foo_).CTConst(0));
00330 }
00331
00332 TEST_F(FunctionMockerTest, MocksReturnTypeWithCommaAndCallType) {
00333 const std::map<int, string> a_map;
00334 EXPECT_CALL(mock_foo_, CTReturnTypeWithComma())
00335 .WillOnce(Return(a_map));
00336
00337 EXPECT_EQ(a_map, mock_foo_.CTReturnTypeWithComma());
00338 }
00339
00340 #endif // GTEST_OS_WINDOWS
00341
00342 class MockB {
00343 public:
00344 MockB() {}
00345
00346 MOCK_METHOD0(DoB, void());
00347
00348 private:
00349 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockB);
00350 };
00351
00352
00353
00354 TEST(ExpectCallTest, UnmentionedFunctionCanBeCalledAnyNumberOfTimes) {
00355 {
00356 MockB b;
00357 }
00358
00359 {
00360 MockB b;
00361 b.DoB();
00362 }
00363
00364 {
00365 MockB b;
00366 b.DoB();
00367 b.DoB();
00368 }
00369 }
00370
00371
00372
00373 template <typename T>
00374 class StackInterface {
00375 public:
00376 virtual ~StackInterface() {}
00377
00378
00379 virtual void Push(const T& value) = 0;
00380 virtual void Pop() = 0;
00381 virtual int GetSize() const = 0;
00382
00383 virtual const T& GetTop() const = 0;
00384 };
00385
00386 template <typename T>
00387 class MockStack : public StackInterface<T> {
00388 public:
00389 MockStack() {}
00390
00391 MOCK_METHOD1_T(Push, void(const T& elem));
00392 MOCK_METHOD0_T(Pop, void());
00393 MOCK_CONST_METHOD0_T(GetSize, int());
00394 MOCK_CONST_METHOD0_T(GetTop, const T&());
00395
00396
00397 MOCK_METHOD0_T(ReturnTypeWithComma, std::map<int, int>());
00398 MOCK_CONST_METHOD1_T(ReturnTypeWithComma, std::map<int, int>(int));
00399
00400 private:
00401 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockStack);
00402 };
00403
00404
00405 TEST(TemplateMockTest, Works) {
00406 MockStack<int> mock;
00407
00408 EXPECT_CALL(mock, GetSize())
00409 .WillOnce(Return(0))
00410 .WillOnce(Return(1))
00411 .WillOnce(Return(0));
00412 EXPECT_CALL(mock, Push(_));
00413 int n = 5;
00414 EXPECT_CALL(mock, GetTop())
00415 .WillOnce(ReturnRef(n));
00416 EXPECT_CALL(mock, Pop())
00417 .Times(AnyNumber());
00418
00419 EXPECT_EQ(0, mock.GetSize());
00420 mock.Push(5);
00421 EXPECT_EQ(1, mock.GetSize());
00422 EXPECT_EQ(5, mock.GetTop());
00423 mock.Pop();
00424 EXPECT_EQ(0, mock.GetSize());
00425 }
00426
00427 TEST(TemplateMockTest, MethodWithCommaInReturnTypeWorks) {
00428 MockStack<int> mock;
00429
00430 const std::map<int, int> a_map;
00431 EXPECT_CALL(mock, ReturnTypeWithComma())
00432 .WillOnce(Return(a_map));
00433 EXPECT_CALL(mock, ReturnTypeWithComma(1))
00434 .WillOnce(Return(a_map));
00435
00436 EXPECT_EQ(a_map, mock.ReturnTypeWithComma());
00437 EXPECT_EQ(a_map, mock.ReturnTypeWithComma(1));
00438 }
00439
00440 #if GTEST_OS_WINDOWS
00441
00442
00443 template <typename T>
00444 class StackInterfaceWithCallType {
00445 public:
00446 virtual ~StackInterfaceWithCallType() {}
00447
00448
00449 STDMETHOD_(void, Push)(const T& value) = 0;
00450 STDMETHOD_(void, Pop)() = 0;
00451 STDMETHOD_(int, GetSize)() const = 0;
00452
00453 STDMETHOD_(const T&, GetTop)() const = 0;
00454 };
00455
00456 template <typename T>
00457 class MockStackWithCallType : public StackInterfaceWithCallType<T> {
00458 public:
00459 MockStackWithCallType() {}
00460
00461 MOCK_METHOD1_T_WITH_CALLTYPE(STDMETHODCALLTYPE, Push, void(const T& elem));
00462 MOCK_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, Pop, void());
00463 MOCK_CONST_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, GetSize, int());
00464 MOCK_CONST_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, GetTop, const T&());
00465
00466 private:
00467 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockStackWithCallType);
00468 };
00469
00470
00471 TEST(TemplateMockTestWithCallType, Works) {
00472 MockStackWithCallType<int> mock;
00473
00474 EXPECT_CALL(mock, GetSize())
00475 .WillOnce(Return(0))
00476 .WillOnce(Return(1))
00477 .WillOnce(Return(0));
00478 EXPECT_CALL(mock, Push(_));
00479 int n = 5;
00480 EXPECT_CALL(mock, GetTop())
00481 .WillOnce(ReturnRef(n));
00482 EXPECT_CALL(mock, Pop())
00483 .Times(AnyNumber());
00484
00485 EXPECT_EQ(0, mock.GetSize());
00486 mock.Push(5);
00487 EXPECT_EQ(1, mock.GetSize());
00488 EXPECT_EQ(5, mock.GetTop());
00489 mock.Pop();
00490 EXPECT_EQ(0, mock.GetSize());
00491 }
00492 #endif // GTEST_OS_WINDOWS
00493
00494 #define MY_MOCK_METHODS1_ \
00495 MOCK_METHOD0(Overloaded, void()); \
00496 MOCK_CONST_METHOD1(Overloaded, int(int n)); \
00497 MOCK_METHOD2(Overloaded, bool(bool f, int n))
00498
00499 class MockOverloadedOnArgNumber {
00500 public:
00501 MockOverloadedOnArgNumber() {}
00502
00503 MY_MOCK_METHODS1_;
00504
00505 private:
00506 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockOverloadedOnArgNumber);
00507 };
00508
00509 TEST(OverloadedMockMethodTest, CanOverloadOnArgNumberInMacroBody) {
00510 MockOverloadedOnArgNumber mock;
00511 EXPECT_CALL(mock, Overloaded());
00512 EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2));
00513 EXPECT_CALL(mock, Overloaded(true, 1)).WillOnce(Return(true));
00514
00515 mock.Overloaded();
00516 EXPECT_EQ(2, mock.Overloaded(1));
00517 EXPECT_TRUE(mock.Overloaded(true, 1));
00518 }
00519
00520 #define MY_MOCK_METHODS2_ \
00521 MOCK_CONST_METHOD1(Overloaded, int(int n)); \
00522 MOCK_METHOD1(Overloaded, int(int n));
00523
00524 class MockOverloadedOnConstness {
00525 public:
00526 MockOverloadedOnConstness() {}
00527
00528 MY_MOCK_METHODS2_;
00529
00530 private:
00531 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockOverloadedOnConstness);
00532 };
00533
00534 TEST(OverloadedMockMethodTest, CanOverloadOnConstnessInMacroBody) {
00535 MockOverloadedOnConstness mock;
00536 const MockOverloadedOnConstness* const_mock = &mock;
00537 EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2));
00538 EXPECT_CALL(*const_mock, Overloaded(1)).WillOnce(Return(3));
00539
00540 EXPECT_EQ(2, mock.Overloaded(1));
00541 EXPECT_EQ(3, const_mock->Overloaded(1));
00542 }
00543
00544 TEST(MockFunctionTest, WorksForVoidNullary) {
00545 MockFunction<void()> foo;
00546 EXPECT_CALL(foo, Call());
00547 foo.Call();
00548 }
00549
00550 TEST(MockFunctionTest, WorksForNonVoidNullary) {
00551 MockFunction<int()> foo;
00552 EXPECT_CALL(foo, Call())
00553 .WillOnce(Return(1))
00554 .WillOnce(Return(2));
00555 EXPECT_EQ(1, foo.Call());
00556 EXPECT_EQ(2, foo.Call());
00557 }
00558
00559 TEST(MockFunctionTest, WorksForVoidUnary) {
00560 MockFunction<void(int)> foo;
00561 EXPECT_CALL(foo, Call(1));
00562 foo.Call(1);
00563 }
00564
00565 TEST(MockFunctionTest, WorksForNonVoidBinary) {
00566 MockFunction<int(bool, int)> foo;
00567 EXPECT_CALL(foo, Call(false, 42))
00568 .WillOnce(Return(1))
00569 .WillOnce(Return(2));
00570 EXPECT_CALL(foo, Call(true, Ge(100)))
00571 .WillOnce(Return(3));
00572 EXPECT_EQ(1, foo.Call(false, 42));
00573 EXPECT_EQ(2, foo.Call(false, 42));
00574 EXPECT_EQ(3, foo.Call(true, 120));
00575 }
00576
00577 TEST(MockFunctionTest, WorksFor10Arguments) {
00578 MockFunction<int(bool a0, char a1, int a2, int a3, int a4,
00579 int a5, int a6, char a7, int a8, bool a9)> foo;
00580 EXPECT_CALL(foo, Call(_, 'a', _, _, _, _, _, _, _, _))
00581 .WillOnce(Return(1))
00582 .WillOnce(Return(2));
00583 EXPECT_EQ(1, foo.Call(false, 'a', 0, 0, 0, 0, 0, 'b', 0, true));
00584 EXPECT_EQ(2, foo.Call(true, 'a', 0, 0, 0, 0, 0, 'b', 1, false));
00585 }
00586
00587 }
00588 }