?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. // Copyright 2007, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. //     * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. //     * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. //     * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  
  30.  
  31. // Google Mock - a framework for writing C++ mock classes.
  32. //
  33. // This file tests the function mocker classes.
  34.  
  35. #include "gmock/gmock-generated-function-mockers.h"
  36.  
  37. #if GTEST_OS_WINDOWS
  38. // MSDN says the header file to be included for STDMETHOD is BaseTyps.h but
  39. // we are getting compiler errors if we use basetyps.h, hence including
  40. // objbase.h for definition of STDMETHOD.
  41. # include <objbase.h>
  42. #endif  // GTEST_OS_WINDOWS
  43.  
  44. #include <map>
  45. #include <string>
  46. #include "gmock/gmock.h"
  47. #include "gtest/gtest.h"
  48.  
  49. // There is a bug in MSVC (fixed in VS 2008) that prevents creating a
  50. // mock for a function with const arguments, so we don't test such
  51. // cases for MSVC versions older than 2008.
  52. #if !GTEST_OS_WINDOWS || (_MSC_VER >= 1500)
  53. # define GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
  54. #endif  // !GTEST_OS_WINDOWS || (_MSC_VER >= 1500)
  55.  
  56. namespace testing {
  57. namespace gmock_generated_function_mockers_test {
  58.  
  59. using testing::_;
  60. using testing::A;
  61. using testing::An;
  62. using testing::AnyNumber;
  63. using testing::Const;
  64. using testing::DoDefault;
  65. using testing::Eq;
  66. using testing::Lt;
  67. using testing::MockFunction;
  68. using testing::Ref;
  69. using testing::Return;
  70. using testing::ReturnRef;
  71. using testing::TypedEq;
  72.  
  73. class FooInterface {
  74.  public:
  75.   virtual ~FooInterface() {}
  76.  
  77.   virtual void VoidReturning(int x) = 0;
  78.  
  79.   virtual int Nullary() = 0;
  80.   virtual bool Unary(int x) = 0;
  81.   virtual long Binary(short x, int y) = 0;  // NOLINT
  82.   virtual int Decimal(bool b, char c, short d, int e, long f,  // NOLINT
  83.                       float g, double h, unsigned i, char* j,
  84.                       const std::string& k) = 0;
  85.  
  86.   virtual bool TakesNonConstReference(int& n) = 0;  // NOLINT
  87.   virtual std::string TakesConstReference(const int& n) = 0;
  88. #ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
  89.   virtual bool TakesConst(const int x) = 0;
  90. #endif  // GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
  91.  
  92.   virtual int OverloadedOnArgumentNumber() = 0;
  93.   virtual int OverloadedOnArgumentNumber(int n) = 0;
  94.  
  95.   virtual int OverloadedOnArgumentType(int n) = 0;
  96.   virtual char OverloadedOnArgumentType(char c) = 0;
  97.  
  98.   virtual int OverloadedOnConstness() = 0;
  99.   virtual char OverloadedOnConstness() const = 0;
  100.  
  101.   virtual int TypeWithHole(int (*func)()) = 0;
  102.   virtual int TypeWithComma(const std::map<int, std::string>& a_map) = 0;
  103.  
  104. #if GTEST_OS_WINDOWS
  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,  // NOLINT
  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
  112. };
  113.  
  114. // Const qualifiers on arguments were once (incorrectly) considered
  115. // significant in determining whether two virtual functions had the same
  116. // signature. This was fixed in Visual Studio 2008. However, the compiler
  117. // still emits a warning that alerts about this change in behavior.
  118. #ifdef _MSC_VER
  119. # pragma warning(push)
  120. # pragma warning(disable : 4373)
  121. #endif
  122. class MockFoo : public FooInterface {
  123.  public:
  124.   MockFoo() {}
  125.  
  126.   // Makes sure that a mock function parameter can be named.
  127.   MOCK_METHOD1(VoidReturning, void(int n));  // NOLINT
  128.  
  129.   MOCK_METHOD0(Nullary, int());  // NOLINT
  130.  
  131.   // Makes sure that a mock function parameter can be unnamed.
  132.   MOCK_METHOD1(Unary, bool(int));  // NOLINT
  133.   MOCK_METHOD2(Binary, long(short, int));  // NOLINT
  134.   MOCK_METHOD10(Decimal, int(bool, char, short, int, long, float,  // NOLINT
  135.                              double, unsigned, char*, const std::string& str));
  136.  
  137.   MOCK_METHOD1(TakesNonConstReference, bool(int&));  // NOLINT
  138.   MOCK_METHOD1(TakesConstReference, std::string(const int&));
  139.  
  140. #ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
  141.   MOCK_METHOD1(TakesConst, bool(const int));  // NOLINT
  142. #endif
  143.  
  144.   // Tests that the function return type can contain unprotected comma.
  145.   MOCK_METHOD0(ReturnTypeWithComma, std::map<int, std::string>());
  146.   MOCK_CONST_METHOD1(ReturnTypeWithComma,
  147.                      std::map<int, std::string>(int));  // NOLINT
  148.  
  149.   MOCK_METHOD0(OverloadedOnArgumentNumber, int());  // NOLINT
  150.   MOCK_METHOD1(OverloadedOnArgumentNumber, int(int));  // NOLINT
  151.  
  152.   MOCK_METHOD1(OverloadedOnArgumentType, int(int));  // NOLINT
  153.   MOCK_METHOD1(OverloadedOnArgumentType, char(char));  // NOLINT
  154.  
  155.   MOCK_METHOD0(OverloadedOnConstness, int());  // NOLINT
  156.   MOCK_CONST_METHOD0(OverloadedOnConstness, char());  // NOLINT
  157.  
  158.   MOCK_METHOD1(TypeWithHole, int(int (*)()));  // NOLINT
  159.   MOCK_METHOD1(TypeWithComma,
  160.                int(const std::map<int, std::string>&));  // NOLINT
  161.  
  162. #if GTEST_OS_WINDOWS
  163.   MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, CTNullary, int());
  164.   MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, CTUnary, bool(int));
  165.   MOCK_METHOD10_WITH_CALLTYPE(STDMETHODCALLTYPE, CTDecimal,
  166.                               int(bool b, char c, short d, int e, long f,
  167.                                   float g, double h, unsigned i, char* j,
  168.                                   const std::string& k));
  169.   MOCK_CONST_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, CTConst, char(int));
  170.  
  171.   // Tests that the function return type can contain unprotected comma.
  172.   MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, CTReturnTypeWithComma,
  173.                              std::map<int, std::string>());
  174. #endif  // GTEST_OS_WINDOWS
  175.  
  176.  private:
  177.   GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
  178. };
  179. #ifdef _MSC_VER
  180. # pragma warning(pop)
  181. #endif
  182.  
  183. class FunctionMockerTest : public testing::Test {
  184.  protected:
  185.   FunctionMockerTest() : foo_(&mock_foo_) {}
  186.  
  187.   FooInterface* const foo_;
  188.   MockFoo mock_foo_;
  189. };
  190.  
  191. // Tests mocking a void-returning function.
  192. TEST_F(FunctionMockerTest, MocksVoidFunction) {
  193.   EXPECT_CALL(mock_foo_, VoidReturning(Lt(100)));
  194.   foo_->VoidReturning(0);
  195. }
  196.  
  197. // Tests mocking a nullary function.
  198. TEST_F(FunctionMockerTest, MocksNullaryFunction) {
  199.   EXPECT_CALL(mock_foo_, Nullary())
  200.       .WillOnce(DoDefault())
  201.       .WillOnce(Return(1));
  202.  
  203.   EXPECT_EQ(0, foo_->Nullary());
  204.   EXPECT_EQ(1, foo_->Nullary());
  205. }
  206.  
  207. // Tests mocking a unary function.
  208. TEST_F(FunctionMockerTest, MocksUnaryFunction) {
  209.   EXPECT_CALL(mock_foo_, Unary(Eq(2)))
  210.       .Times(2)
  211.       .WillOnce(Return(true));
  212.  
  213.   EXPECT_TRUE(foo_->Unary(2));
  214.   EXPECT_FALSE(foo_->Unary(2));
  215. }
  216.  
  217. // Tests mocking a binary function.
  218. TEST_F(FunctionMockerTest, MocksBinaryFunction) {
  219.   EXPECT_CALL(mock_foo_, Binary(2, _))
  220.       .WillOnce(Return(3));
  221.  
  222.   EXPECT_EQ(3, foo_->Binary(2, 1));
  223. }
  224.  
  225. // Tests mocking a decimal function.
  226. TEST_F(FunctionMockerTest, MocksDecimalFunction) {
  227.   EXPECT_CALL(mock_foo_, Decimal(true, 'a', 0, 0, 1L, A<float>(),
  228.                                  Lt(100), 5U, NULL, "hi"))
  229.       .WillOnce(Return(5));
  230.  
  231.   EXPECT_EQ(5, foo_->Decimal(true, 'a', 0, 0, 1, 0, 0, 5, NULL, "hi"));
  232. }
  233.  
  234. // Tests mocking a function that takes a non-const reference.
  235. TEST_F(FunctionMockerTest, MocksFunctionWithNonConstReferenceArgument) {
  236.   int a = 0;
  237.   EXPECT_CALL(mock_foo_, TakesNonConstReference(Ref(a)))
  238.       .WillOnce(Return(true));
  239.  
  240.   EXPECT_TRUE(foo_->TakesNonConstReference(a));
  241. }
  242.  
  243. // Tests mocking a function that takes a const reference.
  244. TEST_F(FunctionMockerTest, MocksFunctionWithConstReferenceArgument) {
  245.   int a = 0;
  246.   EXPECT_CALL(mock_foo_, TakesConstReference(Ref(a)))
  247.       .WillOnce(Return("Hello"));
  248.  
  249.   EXPECT_EQ("Hello", foo_->TakesConstReference(a));
  250. }
  251.  
  252. #ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
  253. // Tests mocking a function that takes a const variable.
  254. TEST_F(FunctionMockerTest, MocksFunctionWithConstArgument) {
  255.   EXPECT_CALL(mock_foo_, TakesConst(Lt(10)))
  256.       .WillOnce(DoDefault());
  257.  
  258.   EXPECT_FALSE(foo_->TakesConst(5));
  259. }
  260. #endif  // GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
  261.  
  262. // Tests mocking functions overloaded on the number of arguments.
  263. TEST_F(FunctionMockerTest, MocksFunctionsOverloadedOnArgumentNumber) {
  264.   EXPECT_CALL(mock_foo_, OverloadedOnArgumentNumber())
  265.       .WillOnce(Return(1));
  266.   EXPECT_CALL(mock_foo_, OverloadedOnArgumentNumber(_))
  267.       .WillOnce(Return(2));
  268.  
  269.   EXPECT_EQ(2, foo_->OverloadedOnArgumentNumber(1));
  270.   EXPECT_EQ(1, foo_->OverloadedOnArgumentNumber());
  271. }
  272.  
  273. // Tests mocking functions overloaded on the types of argument.
  274. TEST_F(FunctionMockerTest, MocksFunctionsOverloadedOnArgumentType) {
  275.   EXPECT_CALL(mock_foo_, OverloadedOnArgumentType(An<int>()))
  276.       .WillOnce(Return(1));
  277.   EXPECT_CALL(mock_foo_, OverloadedOnArgumentType(TypedEq<char>('a')))
  278.       .WillOnce(Return('b'));
  279.  
  280.   EXPECT_EQ(1, foo_->OverloadedOnArgumentType(0));
  281.   EXPECT_EQ('b', foo_->OverloadedOnArgumentType('a'));
  282. }
  283.  
  284. // Tests mocking functions overloaded on the const-ness of this object.
  285. TEST_F(FunctionMockerTest, MocksFunctionsOverloadedOnConstnessOfThis) {
  286.   EXPECT_CALL(mock_foo_, OverloadedOnConstness());
  287.   EXPECT_CALL(Const(mock_foo_), OverloadedOnConstness())
  288.       .WillOnce(Return('a'));
  289.  
  290.   EXPECT_EQ(0, foo_->OverloadedOnConstness());
  291.   EXPECT_EQ('a', Const(*foo_).OverloadedOnConstness());
  292. }
  293.  
  294. TEST_F(FunctionMockerTest, MocksReturnTypeWithComma) {
  295.   const std::map<int, std::string> a_map;
  296.   EXPECT_CALL(mock_foo_, ReturnTypeWithComma())
  297.       .WillOnce(Return(a_map));
  298.   EXPECT_CALL(mock_foo_, ReturnTypeWithComma(42))
  299.       .WillOnce(Return(a_map));
  300.  
  301.   EXPECT_EQ(a_map, mock_foo_.ReturnTypeWithComma());
  302.   EXPECT_EQ(a_map, mock_foo_.ReturnTypeWithComma(42));
  303. }
  304.  
  305. #if GTEST_OS_WINDOWS
  306. // Tests mocking a nullary function with calltype.
  307. TEST_F(FunctionMockerTest, MocksNullaryFunctionWithCallType) {
  308.   EXPECT_CALL(mock_foo_, CTNullary())
  309.       .WillOnce(Return(-1))
  310.       .WillOnce(Return(0));
  311.  
  312.   EXPECT_EQ(-1, foo_->CTNullary());
  313.   EXPECT_EQ(0, foo_->CTNullary());
  314. }
  315.  
  316. // Tests mocking a unary function with calltype.
  317. TEST_F(FunctionMockerTest, MocksUnaryFunctionWithCallType) {
  318.   EXPECT_CALL(mock_foo_, CTUnary(Eq(2)))
  319.       .Times(2)
  320.       .WillOnce(Return(true))
  321.       .WillOnce(Return(false));
  322.  
  323.   EXPECT_TRUE(foo_->CTUnary(2));
  324.   EXPECT_FALSE(foo_->CTUnary(2));
  325. }
  326.  
  327. // Tests mocking a decimal function with calltype.
  328. TEST_F(FunctionMockerTest, MocksDecimalFunctionWithCallType) {
  329.   EXPECT_CALL(mock_foo_, CTDecimal(true, 'a', 0, 0, 1L, A<float>(),
  330.                                    Lt(100), 5U, NULL, "hi"))
  331.       .WillOnce(Return(10));
  332.  
  333.   EXPECT_EQ(10, foo_->CTDecimal(true, 'a', 0, 0, 1, 0, 0, 5, NULL, "hi"));
  334. }
  335.  
  336. // Tests mocking functions overloaded on the const-ness of this object.
  337. TEST_F(FunctionMockerTest, MocksFunctionsConstFunctionWithCallType) {
  338.   EXPECT_CALL(Const(mock_foo_), CTConst(_))
  339.       .WillOnce(Return('a'));
  340.  
  341.   EXPECT_EQ('a', Const(*foo_).CTConst(0));
  342. }
  343.  
  344. TEST_F(FunctionMockerTest, MocksReturnTypeWithCommaAndCallType) {
  345.   const std::map<int, std::string> a_map;
  346.   EXPECT_CALL(mock_foo_, CTReturnTypeWithComma())
  347.       .WillOnce(Return(a_map));
  348.  
  349.   EXPECT_EQ(a_map, mock_foo_.CTReturnTypeWithComma());
  350. }
  351.  
  352. #endif  // GTEST_OS_WINDOWS
  353.  
  354. class MockB {
  355.  public:
  356.   MockB() {}
  357.  
  358.   MOCK_METHOD0(DoB, void());
  359.  
  360.  private:
  361.   GTEST_DISALLOW_COPY_AND_ASSIGN_(MockB);
  362. };
  363.  
  364. // Tests that functions with no EXPECT_CALL() ruls can be called any
  365. // number of times.
  366. TEST(ExpectCallTest, UnmentionedFunctionCanBeCalledAnyNumberOfTimes) {
  367.   {
  368.     MockB b;
  369.   }
  370.  
  371.   {
  372.     MockB b;
  373.     b.DoB();
  374.   }
  375.  
  376.   {
  377.     MockB b;
  378.     b.DoB();
  379.     b.DoB();
  380.   }
  381. }
  382.  
  383. // Tests mocking template interfaces.
  384.  
  385. template <typename T>
  386. class StackInterface {
  387.  public:
  388.   virtual ~StackInterface() {}
  389.  
  390.   // Template parameter appears in function parameter.
  391.   virtual void Push(const T& value) = 0;
  392.   virtual void Pop() = 0;
  393.   virtual int GetSize() const = 0;
  394.   // Template parameter appears in function return type.
  395.   virtual const T& GetTop() const = 0;
  396. };
  397.  
  398. template <typename T>
  399. class MockStack : public StackInterface<T> {
  400.  public:
  401.   MockStack() {}
  402.  
  403.   MOCK_METHOD1_T(Push, void(const T& elem));
  404.   MOCK_METHOD0_T(Pop, void());
  405.   MOCK_CONST_METHOD0_T(GetSize, int());  // NOLINT
  406.   MOCK_CONST_METHOD0_T(GetTop, const T&());
  407.  
  408.   // Tests that the function return type can contain unprotected comma.
  409.   MOCK_METHOD0_T(ReturnTypeWithComma, std::map<int, int>());
  410.   MOCK_CONST_METHOD1_T(ReturnTypeWithComma, std::map<int, int>(int));  // NOLINT
  411.  
  412.  private:
  413.   GTEST_DISALLOW_COPY_AND_ASSIGN_(MockStack);
  414. };
  415.  
  416. // Tests that template mock works.
  417. TEST(TemplateMockTest, Works) {
  418.   MockStack<int> mock;
  419.  
  420.   EXPECT_CALL(mock, GetSize())
  421.       .WillOnce(Return(0))
  422.       .WillOnce(Return(1))
  423.       .WillOnce(Return(0));
  424.   EXPECT_CALL(mock, Push(_));
  425.   int n = 5;
  426.   EXPECT_CALL(mock, GetTop())
  427.       .WillOnce(ReturnRef(n));
  428.   EXPECT_CALL(mock, Pop())
  429.       .Times(AnyNumber());
  430.  
  431.   EXPECT_EQ(0, mock.GetSize());
  432.   mock.Push(5);
  433.   EXPECT_EQ(1, mock.GetSize());
  434.   EXPECT_EQ(5, mock.GetTop());
  435.   mock.Pop();
  436.   EXPECT_EQ(0, mock.GetSize());
  437. }
  438.  
  439. TEST(TemplateMockTest, MethodWithCommaInReturnTypeWorks) {
  440.   MockStack<int> mock;
  441.  
  442.   const std::map<int, int> a_map;
  443.   EXPECT_CALL(mock, ReturnTypeWithComma())
  444.       .WillOnce(Return(a_map));
  445.   EXPECT_CALL(mock, ReturnTypeWithComma(1))
  446.       .WillOnce(Return(a_map));
  447.  
  448.   EXPECT_EQ(a_map, mock.ReturnTypeWithComma());
  449.   EXPECT_EQ(a_map, mock.ReturnTypeWithComma(1));
  450. }
  451.  
  452. #if GTEST_OS_WINDOWS
  453. // Tests mocking template interfaces with calltype.
  454.  
  455. template <typename T>
  456. class StackInterfaceWithCallType {
  457.  public:
  458.   virtual ~StackInterfaceWithCallType() {}
  459.  
  460.   // Template parameter appears in function parameter.
  461.   STDMETHOD_(void, Push)(const T& value) = 0;
  462.   STDMETHOD_(void, Pop)() = 0;
  463.   STDMETHOD_(int, GetSize)() const = 0;
  464.   // Template parameter appears in function return type.
  465.   STDMETHOD_(const T&, GetTop)() const = 0;
  466. };
  467.  
  468. template <typename T>
  469. class MockStackWithCallType : public StackInterfaceWithCallType<T> {
  470.  public:
  471.   MockStackWithCallType() {}
  472.  
  473.   MOCK_METHOD1_T_WITH_CALLTYPE(STDMETHODCALLTYPE, Push, void(const T& elem));
  474.   MOCK_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, Pop, void());
  475.   MOCK_CONST_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, GetSize, int());
  476.   MOCK_CONST_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, GetTop, const T&());
  477.  
  478.  private:
  479.   GTEST_DISALLOW_COPY_AND_ASSIGN_(MockStackWithCallType);
  480. };
  481.  
  482. // Tests that template mock with calltype works.
  483. TEST(TemplateMockTestWithCallType, Works) {
  484.   MockStackWithCallType<int> mock;
  485.  
  486.   EXPECT_CALL(mock, GetSize())
  487.       .WillOnce(Return(0))
  488.       .WillOnce(Return(1))
  489.       .WillOnce(Return(0));
  490.   EXPECT_CALL(mock, Push(_));
  491.   int n = 5;
  492.   EXPECT_CALL(mock, GetTop())
  493.       .WillOnce(ReturnRef(n));
  494.   EXPECT_CALL(mock, Pop())
  495.       .Times(AnyNumber());
  496.  
  497.   EXPECT_EQ(0, mock.GetSize());
  498.   mock.Push(5);
  499.   EXPECT_EQ(1, mock.GetSize());
  500.   EXPECT_EQ(5, mock.GetTop());
  501.   mock.Pop();
  502.   EXPECT_EQ(0, mock.GetSize());
  503. }
  504. #endif  // GTEST_OS_WINDOWS
  505.  
  506. #define MY_MOCK_METHODS1_ \
  507.     MOCK_METHOD0(Overloaded, void()); \
  508.     MOCK_CONST_METHOD1(Overloaded, int(int n)); \
  509.     MOCK_METHOD2(Overloaded, bool(bool f, int n))
  510.  
  511. class MockOverloadedOnArgNumber {
  512.  public:
  513.   MockOverloadedOnArgNumber() {}
  514.  
  515.   MY_MOCK_METHODS1_;
  516.  
  517.  private:
  518.   GTEST_DISALLOW_COPY_AND_ASSIGN_(MockOverloadedOnArgNumber);
  519. };
  520.  
  521. TEST(OverloadedMockMethodTest, CanOverloadOnArgNumberInMacroBody) {
  522.   MockOverloadedOnArgNumber mock;
  523.   EXPECT_CALL(mock, Overloaded());
  524.   EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2));
  525.   EXPECT_CALL(mock, Overloaded(true, 1)).WillOnce(Return(true));
  526.  
  527.   mock.Overloaded();
  528.   EXPECT_EQ(2, mock.Overloaded(1));
  529.   EXPECT_TRUE(mock.Overloaded(true, 1));
  530. }
  531.  
  532. #define MY_MOCK_METHODS2_ \
  533.     MOCK_CONST_METHOD1(Overloaded, int(int n)); \
  534.     MOCK_METHOD1(Overloaded, int(int n));
  535.  
  536. class MockOverloadedOnConstness {
  537.  public:
  538.   MockOverloadedOnConstness() {}
  539.  
  540.   MY_MOCK_METHODS2_;
  541.  
  542.  private:
  543.   GTEST_DISALLOW_COPY_AND_ASSIGN_(MockOverloadedOnConstness);
  544. };
  545.  
  546. TEST(OverloadedMockMethodTest, CanOverloadOnConstnessInMacroBody) {
  547.   MockOverloadedOnConstness mock;
  548.   const MockOverloadedOnConstness* const_mock = &mock;
  549.   EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2));
  550.   EXPECT_CALL(*const_mock, Overloaded(1)).WillOnce(Return(3));
  551.  
  552.   EXPECT_EQ(2, mock.Overloaded(1));
  553.   EXPECT_EQ(3, const_mock->Overloaded(1));
  554. }
  555.  
  556. TEST(MockFunctionTest, WorksForVoidNullary) {
  557.   MockFunction<void()> foo;
  558.   EXPECT_CALL(foo, Call());
  559.   foo.Call();
  560. }
  561.  
  562. TEST(MockFunctionTest, WorksForNonVoidNullary) {
  563.   MockFunction<int()> foo;
  564.   EXPECT_CALL(foo, Call())
  565.       .WillOnce(Return(1))
  566.       .WillOnce(Return(2));
  567.   EXPECT_EQ(1, foo.Call());
  568.   EXPECT_EQ(2, foo.Call());
  569. }
  570.  
  571. TEST(MockFunctionTest, WorksForVoidUnary) {
  572.   MockFunction<void(int)> foo;
  573.   EXPECT_CALL(foo, Call(1));
  574.   foo.Call(1);
  575. }
  576.  
  577. TEST(MockFunctionTest, WorksForNonVoidBinary) {
  578.   MockFunction<int(bool, int)> foo;
  579.   EXPECT_CALL(foo, Call(false, 42))
  580.       .WillOnce(Return(1))
  581.       .WillOnce(Return(2));
  582.   EXPECT_CALL(foo, Call(true, Ge(100)))
  583.       .WillOnce(Return(3));
  584.   EXPECT_EQ(1, foo.Call(false, 42));
  585.   EXPECT_EQ(2, foo.Call(false, 42));
  586.   EXPECT_EQ(3, foo.Call(true, 120));
  587. }
  588.  
  589. TEST(MockFunctionTest, WorksFor10Arguments) {
  590.   MockFunction<int(bool a0, char a1, int a2, int a3, int a4,
  591.                    int a5, int a6, char a7, int a8, bool a9)> foo;
  592.   EXPECT_CALL(foo, Call(_, 'a', _, _, _, _, _, _, _, _))
  593.       .WillOnce(Return(1))
  594.       .WillOnce(Return(2));
  595.   EXPECT_EQ(1, foo.Call(false, 'a', 0, 0, 0, 0, 0, 'b', 0, true));
  596.   EXPECT_EQ(2, foo.Call(true, 'a', 0, 0, 0, 0, 0, 'b', 1, false));
  597. }
  598.  
  599. #if GTEST_HAS_STD_FUNCTION_
  600. TEST(MockFunctionTest, AsStdFunction) {
  601.   MockFunction<int(int)> foo;
  602.   auto call = [](const std::function<int(int)> &f, int i) {
  603.     return f(i);
  604.   };
  605.   EXPECT_CALL(foo, Call(1)).WillOnce(Return(-1));
  606.   EXPECT_CALL(foo, Call(2)).WillOnce(Return(-2));
  607.   EXPECT_EQ(-1, call(foo.AsStdFunction(), 1));
  608.   EXPECT_EQ(-2, call(foo.AsStdFunction(), 2));
  609. }
  610.  
  611. TEST(MockFunctionTest, AsStdFunctionReturnsReference) {
  612.   MockFunction<int&()> foo;
  613.   int value = 1;
  614.   EXPECT_CALL(foo, Call()).WillOnce(ReturnRef(value));
  615.   int& ref = foo.AsStdFunction()();
  616.   EXPECT_EQ(1, ref);
  617.   value = 2;
  618.   EXPECT_EQ(2, ref);
  619. }
  620. #endif  // GTEST_HAS_STD_FUNCTION_
  621.  
  622. struct MockMethodSizes0 {
  623.   MOCK_METHOD0(func, void());
  624. };
  625. struct MockMethodSizes1 {
  626.   MOCK_METHOD1(func, void(int));
  627. };
  628. struct MockMethodSizes2 {
  629.   MOCK_METHOD2(func, void(int, int));
  630. };
  631. struct MockMethodSizes3 {
  632.   MOCK_METHOD3(func, void(int, int, int));
  633. };
  634. struct MockMethodSizes4 {
  635.   MOCK_METHOD4(func, void(int, int, int, int));
  636. };
  637.  
  638. TEST(MockFunctionTest, MockMethodSizeOverhead) {
  639.   EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes1));
  640.   EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes2));
  641.   EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes3));
  642.   EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes4));
  643. }
  644.  
  645. }  // namespace gmock_generated_function_mockers_test
  646. }  // namespace testing
  647.