gmock-generated-function-mockers_test.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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. // Google Mock - a framework for writing C++ mock classes.
  30. //
  31. // This file tests the function mocker classes.
  32. #include "gmock/gmock-generated-function-mockers.h"
  33. #if GTEST_OS_WINDOWS
  34. // MSDN says the header file to be included for STDMETHOD is BaseTyps.h but
  35. // we are getting compiler errors if we use basetyps.h, hence including
  36. // objbase.h for definition of STDMETHOD.
  37. # include <objbase.h>
  38. #endif // GTEST_OS_WINDOWS
  39. #include <map>
  40. #include <string>
  41. #include "gmock/gmock.h"
  42. #include "gtest/gtest.h"
  43. // There is a bug in MSVC (fixed in VS 2008) that prevents creating a
  44. // mock for a function with const arguments, so we don't test such
  45. // cases for MSVC versions older than 2008.
  46. #if !GTEST_OS_WINDOWS || (_MSC_VER >= 1500)
  47. # define GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
  48. #endif // !GTEST_OS_WINDOWS || (_MSC_VER >= 1500)
  49. namespace testing {
  50. namespace gmock_generated_function_mockers_test {
  51. using testing::_;
  52. using testing::A;
  53. using testing::An;
  54. using testing::AnyNumber;
  55. using testing::Const;
  56. using testing::DoDefault;
  57. using testing::Eq;
  58. using testing::Lt;
  59. using testing::MockFunction;
  60. using testing::Ref;
  61. using testing::Return;
  62. using testing::ReturnRef;
  63. using testing::TypedEq;
  64. class FooInterface {
  65. public:
  66. virtual ~FooInterface() {}
  67. virtual void VoidReturning(int x) = 0;
  68. virtual int Nullary() = 0;
  69. virtual bool Unary(int x) = 0;
  70. virtual long Binary(short x, int y) = 0; // NOLINT
  71. virtual int Decimal(bool b, char c, short d, int e, long f, // NOLINT
  72. float g, double h, unsigned i, char* j,
  73. const std::string& k) = 0;
  74. virtual bool TakesNonConstReference(int& n) = 0; // NOLINT
  75. virtual std::string TakesConstReference(const int& n) = 0;
  76. #ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
  77. virtual bool TakesConst(const int x) = 0;
  78. #endif // GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
  79. virtual int OverloadedOnArgumentNumber() = 0;
  80. virtual int OverloadedOnArgumentNumber(int n) = 0;
  81. virtual int OverloadedOnArgumentType(int n) = 0;
  82. virtual char OverloadedOnArgumentType(char c) = 0;
  83. virtual int OverloadedOnConstness() = 0;
  84. virtual char OverloadedOnConstness() const = 0;
  85. virtual int TypeWithHole(int (*func)()) = 0;
  86. virtual int TypeWithComma(const std::map<int, std::string>& a_map) = 0;
  87. #if GTEST_OS_WINDOWS
  88. STDMETHOD_(int, CTNullary)() = 0;
  89. STDMETHOD_(bool, CTUnary)(int x) = 0;
  90. STDMETHOD_(int, CTDecimal)
  91. (bool b, char c, short d, int e, long f, // NOLINT
  92. float g, double h, unsigned i, char* j, const std::string& k) = 0;
  93. STDMETHOD_(char, CTConst)(int x) const = 0;
  94. #endif // GTEST_OS_WINDOWS
  95. };
  96. // Const qualifiers on arguments were once (incorrectly) considered
  97. // significant in determining whether two virtual functions had the same
  98. // signature. This was fixed in Visual Studio 2008. However, the compiler
  99. // still emits a warning that alerts about this change in behavior.
  100. #ifdef _MSC_VER
  101. # pragma warning(push)
  102. # pragma warning(disable : 4373)
  103. #endif
  104. class MockFoo : public FooInterface {
  105. public:
  106. MockFoo() {}
  107. // Makes sure that a mock function parameter can be named.
  108. MOCK_METHOD1(VoidReturning, void(int n)); // NOLINT
  109. MOCK_METHOD0(Nullary, int()); // NOLINT
  110. // Makes sure that a mock function parameter can be unnamed.
  111. MOCK_METHOD1(Unary, bool(int)); // NOLINT
  112. MOCK_METHOD2(Binary, long(short, int)); // NOLINT
  113. MOCK_METHOD10(Decimal, int(bool, char, short, int, long, float, // NOLINT
  114. double, unsigned, char*, const std::string& str));
  115. MOCK_METHOD1(TakesNonConstReference, bool(int&)); // NOLINT
  116. MOCK_METHOD1(TakesConstReference, std::string(const int&));
  117. #ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
  118. MOCK_METHOD1(TakesConst, bool(const int)); // NOLINT
  119. #endif
  120. // Tests that the function return type can contain unprotected comma.
  121. MOCK_METHOD0(ReturnTypeWithComma, std::map<int, std::string>());
  122. MOCK_CONST_METHOD1(ReturnTypeWithComma,
  123. std::map<int, std::string>(int)); // NOLINT
  124. MOCK_METHOD0(OverloadedOnArgumentNumber, int()); // NOLINT
  125. MOCK_METHOD1(OverloadedOnArgumentNumber, int(int)); // NOLINT
  126. MOCK_METHOD1(OverloadedOnArgumentType, int(int)); // NOLINT
  127. MOCK_METHOD1(OverloadedOnArgumentType, char(char)); // NOLINT
  128. MOCK_METHOD0(OverloadedOnConstness, int()); // NOLINT
  129. MOCK_CONST_METHOD0(OverloadedOnConstness, char()); // NOLINT
  130. MOCK_METHOD1(TypeWithHole, int(int (*)())); // NOLINT
  131. MOCK_METHOD1(TypeWithComma,
  132. int(const std::map<int, std::string>&)); // NOLINT
  133. #if GTEST_OS_WINDOWS
  134. MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, CTNullary, int());
  135. MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, CTUnary, bool(int));
  136. MOCK_METHOD10_WITH_CALLTYPE(STDMETHODCALLTYPE, CTDecimal,
  137. int(bool b, char c, short d, int e, long f,
  138. float g, double h, unsigned i, char* j,
  139. const std::string& k));
  140. MOCK_CONST_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, CTConst, char(int));
  141. // Tests that the function return type can contain unprotected comma.
  142. MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, CTReturnTypeWithComma,
  143. std::map<int, std::string>());
  144. #endif // GTEST_OS_WINDOWS
  145. private:
  146. GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
  147. };
  148. #ifdef _MSC_VER
  149. # pragma warning(pop)
  150. #endif
  151. class FunctionMockerTest : public testing::Test {
  152. protected:
  153. FunctionMockerTest() : foo_(&mock_foo_) {}
  154. FooInterface* const foo_;
  155. MockFoo mock_foo_;
  156. };
  157. // Tests mocking a void-returning function.
  158. TEST_F(FunctionMockerTest, MocksVoidFunction) {
  159. EXPECT_CALL(mock_foo_, VoidReturning(Lt(100)));
  160. foo_->VoidReturning(0);
  161. }
  162. // Tests mocking a nullary function.
  163. TEST_F(FunctionMockerTest, MocksNullaryFunction) {
  164. EXPECT_CALL(mock_foo_, Nullary())
  165. .WillOnce(DoDefault())
  166. .WillOnce(Return(1));
  167. EXPECT_EQ(0, foo_->Nullary());
  168. EXPECT_EQ(1, foo_->Nullary());
  169. }
  170. // Tests mocking a unary function.
  171. TEST_F(FunctionMockerTest, MocksUnaryFunction) {
  172. EXPECT_CALL(mock_foo_, Unary(Eq(2)))
  173. .Times(2)
  174. .WillOnce(Return(true));
  175. EXPECT_TRUE(foo_->Unary(2));
  176. EXPECT_FALSE(foo_->Unary(2));
  177. }
  178. // Tests mocking a binary function.
  179. TEST_F(FunctionMockerTest, MocksBinaryFunction) {
  180. EXPECT_CALL(mock_foo_, Binary(2, _))
  181. .WillOnce(Return(3));
  182. EXPECT_EQ(3, foo_->Binary(2, 1));
  183. }
  184. // Tests mocking a decimal function.
  185. TEST_F(FunctionMockerTest, MocksDecimalFunction) {
  186. EXPECT_CALL(mock_foo_, Decimal(true, 'a', 0, 0, 1L, A<float>(),
  187. Lt(100), 5U, NULL, "hi"))
  188. .WillOnce(Return(5));
  189. EXPECT_EQ(5, foo_->Decimal(true, 'a', 0, 0, 1, 0, 0, 5, NULL, "hi"));
  190. }
  191. // Tests mocking a function that takes a non-const reference.
  192. TEST_F(FunctionMockerTest, MocksFunctionWithNonConstReferenceArgument) {
  193. int a = 0;
  194. EXPECT_CALL(mock_foo_, TakesNonConstReference(Ref(a)))
  195. .WillOnce(Return(true));
  196. EXPECT_TRUE(foo_->TakesNonConstReference(a));
  197. }
  198. // Tests mocking a function that takes a const reference.
  199. TEST_F(FunctionMockerTest, MocksFunctionWithConstReferenceArgument) {
  200. int a = 0;
  201. EXPECT_CALL(mock_foo_, TakesConstReference(Ref(a)))
  202. .WillOnce(Return("Hello"));
  203. EXPECT_EQ("Hello", foo_->TakesConstReference(a));
  204. }
  205. #ifdef GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
  206. // Tests mocking a function that takes a const variable.
  207. TEST_F(FunctionMockerTest, MocksFunctionWithConstArgument) {
  208. EXPECT_CALL(mock_foo_, TakesConst(Lt(10)))
  209. .WillOnce(DoDefault());
  210. EXPECT_FALSE(foo_->TakesConst(5));
  211. }
  212. #endif // GMOCK_ALLOWS_CONST_PARAM_FUNCTIONS
  213. // Tests mocking functions overloaded on the number of arguments.
  214. TEST_F(FunctionMockerTest, MocksFunctionsOverloadedOnArgumentNumber) {
  215. EXPECT_CALL(mock_foo_, OverloadedOnArgumentNumber())
  216. .WillOnce(Return(1));
  217. EXPECT_CALL(mock_foo_, OverloadedOnArgumentNumber(_))
  218. .WillOnce(Return(2));
  219. EXPECT_EQ(2, foo_->OverloadedOnArgumentNumber(1));
  220. EXPECT_EQ(1, foo_->OverloadedOnArgumentNumber());
  221. }
  222. // Tests mocking functions overloaded on the types of argument.
  223. TEST_F(FunctionMockerTest, MocksFunctionsOverloadedOnArgumentType) {
  224. EXPECT_CALL(mock_foo_, OverloadedOnArgumentType(An<int>()))
  225. .WillOnce(Return(1));
  226. EXPECT_CALL(mock_foo_, OverloadedOnArgumentType(TypedEq<char>('a')))
  227. .WillOnce(Return('b'));
  228. EXPECT_EQ(1, foo_->OverloadedOnArgumentType(0));
  229. EXPECT_EQ('b', foo_->OverloadedOnArgumentType('a'));
  230. }
  231. // Tests mocking functions overloaded on the const-ness of this object.
  232. TEST_F(FunctionMockerTest, MocksFunctionsOverloadedOnConstnessOfThis) {
  233. EXPECT_CALL(mock_foo_, OverloadedOnConstness());
  234. EXPECT_CALL(Const(mock_foo_), OverloadedOnConstness())
  235. .WillOnce(Return('a'));
  236. EXPECT_EQ(0, foo_->OverloadedOnConstness());
  237. EXPECT_EQ('a', Const(*foo_).OverloadedOnConstness());
  238. }
  239. TEST_F(FunctionMockerTest, MocksReturnTypeWithComma) {
  240. const std::map<int, std::string> a_map;
  241. EXPECT_CALL(mock_foo_, ReturnTypeWithComma())
  242. .WillOnce(Return(a_map));
  243. EXPECT_CALL(mock_foo_, ReturnTypeWithComma(42))
  244. .WillOnce(Return(a_map));
  245. EXPECT_EQ(a_map, mock_foo_.ReturnTypeWithComma());
  246. EXPECT_EQ(a_map, mock_foo_.ReturnTypeWithComma(42));
  247. }
  248. #if GTEST_OS_WINDOWS
  249. // Tests mocking a nullary function with calltype.
  250. TEST_F(FunctionMockerTest, MocksNullaryFunctionWithCallType) {
  251. EXPECT_CALL(mock_foo_, CTNullary())
  252. .WillOnce(Return(-1))
  253. .WillOnce(Return(0));
  254. EXPECT_EQ(-1, foo_->CTNullary());
  255. EXPECT_EQ(0, foo_->CTNullary());
  256. }
  257. // Tests mocking a unary function with calltype.
  258. TEST_F(FunctionMockerTest, MocksUnaryFunctionWithCallType) {
  259. EXPECT_CALL(mock_foo_, CTUnary(Eq(2)))
  260. .Times(2)
  261. .WillOnce(Return(true))
  262. .WillOnce(Return(false));
  263. EXPECT_TRUE(foo_->CTUnary(2));
  264. EXPECT_FALSE(foo_->CTUnary(2));
  265. }
  266. // Tests mocking a decimal function with calltype.
  267. TEST_F(FunctionMockerTest, MocksDecimalFunctionWithCallType) {
  268. EXPECT_CALL(mock_foo_, CTDecimal(true, 'a', 0, 0, 1L, A<float>(),
  269. Lt(100), 5U, NULL, "hi"))
  270. .WillOnce(Return(10));
  271. EXPECT_EQ(10, foo_->CTDecimal(true, 'a', 0, 0, 1, 0, 0, 5, NULL, "hi"));
  272. }
  273. // Tests mocking functions overloaded on the const-ness of this object.
  274. TEST_F(FunctionMockerTest, MocksFunctionsConstFunctionWithCallType) {
  275. EXPECT_CALL(Const(mock_foo_), CTConst(_))
  276. .WillOnce(Return('a'));
  277. EXPECT_EQ('a', Const(*foo_).CTConst(0));
  278. }
  279. TEST_F(FunctionMockerTest, MocksReturnTypeWithCommaAndCallType) {
  280. const std::map<int, std::string> a_map;
  281. EXPECT_CALL(mock_foo_, CTReturnTypeWithComma())
  282. .WillOnce(Return(a_map));
  283. EXPECT_EQ(a_map, mock_foo_.CTReturnTypeWithComma());
  284. }
  285. #endif // GTEST_OS_WINDOWS
  286. class MockB {
  287. public:
  288. MockB() {}
  289. MOCK_METHOD0(DoB, void());
  290. private:
  291. GTEST_DISALLOW_COPY_AND_ASSIGN_(MockB);
  292. };
  293. // Tests that functions with no EXPECT_CALL() ruls can be called any
  294. // number of times.
  295. TEST(ExpectCallTest, UnmentionedFunctionCanBeCalledAnyNumberOfTimes) {
  296. {
  297. MockB b;
  298. }
  299. {
  300. MockB b;
  301. b.DoB();
  302. }
  303. {
  304. MockB b;
  305. b.DoB();
  306. b.DoB();
  307. }
  308. }
  309. // Tests mocking template interfaces.
  310. template <typename T>
  311. class StackInterface {
  312. public:
  313. virtual ~StackInterface() {}
  314. // Template parameter appears in function parameter.
  315. virtual void Push(const T& value) = 0;
  316. virtual void Pop() = 0;
  317. virtual int GetSize() const = 0;
  318. // Template parameter appears in function return type.
  319. virtual const T& GetTop() const = 0;
  320. };
  321. template <typename T>
  322. class MockStack : public StackInterface<T> {
  323. public:
  324. MockStack() {}
  325. MOCK_METHOD1_T(Push, void(const T& elem));
  326. MOCK_METHOD0_T(Pop, void());
  327. MOCK_CONST_METHOD0_T(GetSize, int()); // NOLINT
  328. MOCK_CONST_METHOD0_T(GetTop, const T&());
  329. // Tests that the function return type can contain unprotected comma.
  330. MOCK_METHOD0_T(ReturnTypeWithComma, std::map<int, int>());
  331. MOCK_CONST_METHOD1_T(ReturnTypeWithComma, std::map<int, int>(int)); // NOLINT
  332. private:
  333. GTEST_DISALLOW_COPY_AND_ASSIGN_(MockStack);
  334. };
  335. // Tests that template mock works.
  336. TEST(TemplateMockTest, Works) {
  337. MockStack<int> mock;
  338. EXPECT_CALL(mock, GetSize())
  339. .WillOnce(Return(0))
  340. .WillOnce(Return(1))
  341. .WillOnce(Return(0));
  342. EXPECT_CALL(mock, Push(_));
  343. int n = 5;
  344. EXPECT_CALL(mock, GetTop())
  345. .WillOnce(ReturnRef(n));
  346. EXPECT_CALL(mock, Pop())
  347. .Times(AnyNumber());
  348. EXPECT_EQ(0, mock.GetSize());
  349. mock.Push(5);
  350. EXPECT_EQ(1, mock.GetSize());
  351. EXPECT_EQ(5, mock.GetTop());
  352. mock.Pop();
  353. EXPECT_EQ(0, mock.GetSize());
  354. }
  355. TEST(TemplateMockTest, MethodWithCommaInReturnTypeWorks) {
  356. MockStack<int> mock;
  357. const std::map<int, int> a_map;
  358. EXPECT_CALL(mock, ReturnTypeWithComma())
  359. .WillOnce(Return(a_map));
  360. EXPECT_CALL(mock, ReturnTypeWithComma(1))
  361. .WillOnce(Return(a_map));
  362. EXPECT_EQ(a_map, mock.ReturnTypeWithComma());
  363. EXPECT_EQ(a_map, mock.ReturnTypeWithComma(1));
  364. }
  365. #if GTEST_OS_WINDOWS
  366. // Tests mocking template interfaces with calltype.
  367. template <typename T>
  368. class StackInterfaceWithCallType {
  369. public:
  370. virtual ~StackInterfaceWithCallType() {}
  371. // Template parameter appears in function parameter.
  372. STDMETHOD_(void, Push)(const T& value) = 0;
  373. STDMETHOD_(void, Pop)() = 0;
  374. STDMETHOD_(int, GetSize)() const = 0;
  375. // Template parameter appears in function return type.
  376. STDMETHOD_(const T&, GetTop)() const = 0;
  377. };
  378. template <typename T>
  379. class MockStackWithCallType : public StackInterfaceWithCallType<T> {
  380. public:
  381. MockStackWithCallType() {}
  382. MOCK_METHOD1_T_WITH_CALLTYPE(STDMETHODCALLTYPE, Push, void(const T& elem));
  383. MOCK_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, Pop, void());
  384. MOCK_CONST_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, GetSize, int());
  385. MOCK_CONST_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, GetTop, const T&());
  386. private:
  387. GTEST_DISALLOW_COPY_AND_ASSIGN_(MockStackWithCallType);
  388. };
  389. // Tests that template mock with calltype works.
  390. TEST(TemplateMockTestWithCallType, Works) {
  391. MockStackWithCallType<int> mock;
  392. EXPECT_CALL(mock, GetSize())
  393. .WillOnce(Return(0))
  394. .WillOnce(Return(1))
  395. .WillOnce(Return(0));
  396. EXPECT_CALL(mock, Push(_));
  397. int n = 5;
  398. EXPECT_CALL(mock, GetTop())
  399. .WillOnce(ReturnRef(n));
  400. EXPECT_CALL(mock, Pop())
  401. .Times(AnyNumber());
  402. EXPECT_EQ(0, mock.GetSize());
  403. mock.Push(5);
  404. EXPECT_EQ(1, mock.GetSize());
  405. EXPECT_EQ(5, mock.GetTop());
  406. mock.Pop();
  407. EXPECT_EQ(0, mock.GetSize());
  408. }
  409. #endif // GTEST_OS_WINDOWS
  410. #define MY_MOCK_METHODS1_ \
  411. MOCK_METHOD0(Overloaded, void()); \
  412. MOCK_CONST_METHOD1(Overloaded, int(int n)); \
  413. MOCK_METHOD2(Overloaded, bool(bool f, int n))
  414. class MockOverloadedOnArgNumber {
  415. public:
  416. MockOverloadedOnArgNumber() {}
  417. MY_MOCK_METHODS1_;
  418. private:
  419. GTEST_DISALLOW_COPY_AND_ASSIGN_(MockOverloadedOnArgNumber);
  420. };
  421. TEST(OverloadedMockMethodTest, CanOverloadOnArgNumberInMacroBody) {
  422. MockOverloadedOnArgNumber mock;
  423. EXPECT_CALL(mock, Overloaded());
  424. EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2));
  425. EXPECT_CALL(mock, Overloaded(true, 1)).WillOnce(Return(true));
  426. mock.Overloaded();
  427. EXPECT_EQ(2, mock.Overloaded(1));
  428. EXPECT_TRUE(mock.Overloaded(true, 1));
  429. }
  430. #define MY_MOCK_METHODS2_ \
  431. MOCK_CONST_METHOD1(Overloaded, int(int n)); \
  432. MOCK_METHOD1(Overloaded, int(int n));
  433. class MockOverloadedOnConstness {
  434. public:
  435. MockOverloadedOnConstness() {}
  436. MY_MOCK_METHODS2_;
  437. private:
  438. GTEST_DISALLOW_COPY_AND_ASSIGN_(MockOverloadedOnConstness);
  439. };
  440. TEST(OverloadedMockMethodTest, CanOverloadOnConstnessInMacroBody) {
  441. MockOverloadedOnConstness mock;
  442. const MockOverloadedOnConstness* const_mock = &mock;
  443. EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2));
  444. EXPECT_CALL(*const_mock, Overloaded(1)).WillOnce(Return(3));
  445. EXPECT_EQ(2, mock.Overloaded(1));
  446. EXPECT_EQ(3, const_mock->Overloaded(1));
  447. }
  448. TEST(MockFunctionTest, WorksForVoidNullary) {
  449. MockFunction<void()> foo;
  450. EXPECT_CALL(foo, Call());
  451. foo.Call();
  452. }
  453. TEST(MockFunctionTest, WorksForNonVoidNullary) {
  454. MockFunction<int()> foo;
  455. EXPECT_CALL(foo, Call())
  456. .WillOnce(Return(1))
  457. .WillOnce(Return(2));
  458. EXPECT_EQ(1, foo.Call());
  459. EXPECT_EQ(2, foo.Call());
  460. }
  461. TEST(MockFunctionTest, WorksForVoidUnary) {
  462. MockFunction<void(int)> foo;
  463. EXPECT_CALL(foo, Call(1));
  464. foo.Call(1);
  465. }
  466. TEST(MockFunctionTest, WorksForNonVoidBinary) {
  467. MockFunction<int(bool, int)> foo;
  468. EXPECT_CALL(foo, Call(false, 42))
  469. .WillOnce(Return(1))
  470. .WillOnce(Return(2));
  471. EXPECT_CALL(foo, Call(true, Ge(100)))
  472. .WillOnce(Return(3));
  473. EXPECT_EQ(1, foo.Call(false, 42));
  474. EXPECT_EQ(2, foo.Call(false, 42));
  475. EXPECT_EQ(3, foo.Call(true, 120));
  476. }
  477. TEST(MockFunctionTest, WorksFor10Arguments) {
  478. MockFunction<int(bool a0, char a1, int a2, int a3, int a4,
  479. int a5, int a6, char a7, int a8, bool a9)> foo;
  480. EXPECT_CALL(foo, Call(_, 'a', _, _, _, _, _, _, _, _))
  481. .WillOnce(Return(1))
  482. .WillOnce(Return(2));
  483. EXPECT_EQ(1, foo.Call(false, 'a', 0, 0, 0, 0, 0, 'b', 0, true));
  484. EXPECT_EQ(2, foo.Call(true, 'a', 0, 0, 0, 0, 0, 'b', 1, false));
  485. }
  486. #if GTEST_HAS_STD_FUNCTION_
  487. TEST(MockFunctionTest, AsStdFunction) {
  488. MockFunction<int(int)> foo;
  489. auto call = [](const std::function<int(int)> &f, int i) {
  490. return f(i);
  491. };
  492. EXPECT_CALL(foo, Call(1)).WillOnce(Return(-1));
  493. EXPECT_CALL(foo, Call(2)).WillOnce(Return(-2));
  494. EXPECT_EQ(-1, call(foo.AsStdFunction(), 1));
  495. EXPECT_EQ(-2, call(foo.AsStdFunction(), 2));
  496. }
  497. TEST(MockFunctionTest, AsStdFunctionReturnsReference) {
  498. MockFunction<int&()> foo;
  499. int value = 1;
  500. EXPECT_CALL(foo, Call()).WillOnce(ReturnRef(value));
  501. int& ref = foo.AsStdFunction()();
  502. EXPECT_EQ(1, ref);
  503. value = 2;
  504. EXPECT_EQ(2, ref);
  505. }
  506. #endif // GTEST_HAS_STD_FUNCTION_
  507. struct MockMethodSizes0 {
  508. MOCK_METHOD0(func, void());
  509. };
  510. struct MockMethodSizes1 {
  511. MOCK_METHOD1(func, void(int));
  512. };
  513. struct MockMethodSizes2 {
  514. MOCK_METHOD2(func, void(int, int));
  515. };
  516. struct MockMethodSizes3 {
  517. MOCK_METHOD3(func, void(int, int, int));
  518. };
  519. struct MockMethodSizes4 {
  520. MOCK_METHOD4(func, void(int, int, int, int));
  521. };
  522. TEST(MockFunctionTest, MockMethodSizeOverhead) {
  523. EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes1));
  524. EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes2));
  525. EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes3));
  526. EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes4));
  527. }
  528. } // namespace gmock_generated_function_mockers_test
  529. } // namespace testing