gmock-generated-function-mockers_test.cc
Go to the documentation of this file.
00001 // Copyright 2007, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: wan@google.com (Zhanyong Wan)
00031 
00032 // Google Mock - a framework for writing C++ mock classes.
00033 //
00034 // This file tests the function mocker classes.
00035 
00036 #include "gmock/gmock-generated-function-mockers.h"
00037 
00038 #if GTEST_OS_WINDOWS
00039 // MSDN says the header file to be included for STDMETHOD is BaseTyps.h but
00040 // we are getting compiler errors if we use basetyps.h, hence including
00041 // objbase.h for definition of STDMETHOD.
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 // There is a bug in MSVC (fixed in VS 2008) that prevents creating a
00051 // mock for a function with const arguments, so we don't test such
00052 // cases for MSVC versions older than 2008.
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;  // NOLINT
00084   virtual int Decimal(bool b, char c, short d, int e, long f,  // NOLINT
00085                       float g, double h, unsigned i, char* j, const string& k)
00086       = 0;
00087 
00088   virtual bool TakesNonConstReference(int& n) = 0;  // NOLINT
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,  // NOLINT
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   // Makes sure that a mock function parameter can be named.
00120   MOCK_METHOD1(VoidReturning, void(int n));  // NOLINT
00121 
00122   MOCK_METHOD0(Nullary, int());  // NOLINT
00123 
00124   // Makes sure that a mock function parameter can be unnamed.
00125   MOCK_METHOD1(Unary, bool(int));  // NOLINT
00126   MOCK_METHOD2(Binary, long(short, int));  // NOLINT
00127   MOCK_METHOD10(Decimal, int(bool, char, short, int, long, float,  // NOLINT
00128                              double, unsigned, char*, const string& str));
00129 
00130   MOCK_METHOD1(TakesNonConstReference, bool(int&));  // NOLINT
00131   MOCK_METHOD1(TakesConstReference, string(const int&));
00132 
00133 #ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
00134   MOCK_METHOD1(TakesConst, bool(const int));  // NOLINT
00135 #endif
00136 
00137   // Tests that the function return type can contain unprotected comma.
00138   MOCK_METHOD0(ReturnTypeWithComma, std::map<int, string>());
00139   MOCK_CONST_METHOD1(ReturnTypeWithComma,
00140                      std::map<int, string>(int));  // NOLINT
00141 
00142   MOCK_METHOD0(OverloadedOnArgumentNumber, int());  // NOLINT
00143   MOCK_METHOD1(OverloadedOnArgumentNumber, int(int));  // NOLINT
00144 
00145   MOCK_METHOD1(OverloadedOnArgumentType, int(int));  // NOLINT
00146   MOCK_METHOD1(OverloadedOnArgumentType, char(char));  // NOLINT
00147 
00148   MOCK_METHOD0(OverloadedOnConstness, int());  // NOLINT
00149   MOCK_CONST_METHOD0(OverloadedOnConstness, char());  // NOLINT
00150 
00151   MOCK_METHOD1(TypeWithHole, int(int (*)()));  // NOLINT
00152   MOCK_METHOD1(TypeWithComma, int(const std::map<int, string>&));  // NOLINT
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   // Tests that the function return type can contain unprotected comma.
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 // Tests mocking a void-returning function.
00180 TEST_F(FunctionMockerTest, MocksVoidFunction) {
00181   EXPECT_CALL(mock_foo_, VoidReturning(Lt(100)));
00182   foo_->VoidReturning(0);
00183 }
00184 
00185 // Tests mocking a nullary function.
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 // Tests mocking a unary function.
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 // Tests mocking a binary function.
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 // Tests mocking a decimal function.
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 // Tests mocking a function that takes a non-const reference.
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 // Tests mocking a function that takes a const reference.
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 // Tests mocking a function that takes a const variable.
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 // Tests mocking functions overloaded on the number of arguments.
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 // Tests mocking functions overloaded on the types of argument.
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 // Tests mocking functions overloaded on the const-ness of this object.
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 // Tests mocking a nullary function with calltype.
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 // Tests mocking a unary function with calltype.
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 // Tests mocking a decimal function with calltype.
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 // Tests mocking functions overloaded on the const-ness of this object.
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 // Tests that functions with no EXPECT_CALL() ruls can be called any
00353 // number of times.
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 // Tests mocking template interfaces.
00372 
00373 template <typename T>
00374 class StackInterface {
00375  public:
00376   virtual ~StackInterface() {}
00377 
00378   // Template parameter appears in function parameter.
00379   virtual void Push(const T& value) = 0;
00380   virtual void Pop() = 0;
00381   virtual int GetSize() const = 0;
00382   // Template parameter appears in function return type.
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());  // NOLINT
00394   MOCK_CONST_METHOD0_T(GetTop, const T&());
00395 
00396   // Tests that the function return type can contain unprotected comma.
00397   MOCK_METHOD0_T(ReturnTypeWithComma, std::map<int, int>());
00398   MOCK_CONST_METHOD1_T(ReturnTypeWithComma, std::map<int, int>(int));  // NOLINT
00399 
00400  private:
00401   GTEST_DISALLOW_COPY_AND_ASSIGN_(MockStack);
00402 };
00403 
00404 // Tests that template mock works.
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 // Tests mocking template interfaces with calltype.
00442 
00443 template <typename T>
00444 class StackInterfaceWithCallType {
00445  public:
00446   virtual ~StackInterfaceWithCallType() {}
00447 
00448   // Template parameter appears in function parameter.
00449   STDMETHOD_(void, Push)(const T& value) = 0;
00450   STDMETHOD_(void, Pop)() = 0;
00451   STDMETHOD_(int, GetSize)() const = 0;
00452   // Template parameter appears in function return type.
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 // Tests that template mock with calltype works.
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 }  // namespace gmock_generated_function_mockers_test
00588 }  // namespace testing


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:41