gmock-function-mocker_test.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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. namespace testing {
  44. namespace gmock_function_mocker_test {
  45. using testing::_;
  46. using testing::A;
  47. using testing::An;
  48. using testing::AnyNumber;
  49. using testing::Const;
  50. using testing::DoDefault;
  51. using testing::Eq;
  52. using testing::Lt;
  53. using testing::MockFunction;
  54. using testing::Ref;
  55. using testing::Return;
  56. using testing::ReturnRef;
  57. using testing::TypedEq;
  58. template<typename T>
  59. class TemplatedCopyable {
  60. public:
  61. TemplatedCopyable() {}
  62. template <typename U>
  63. TemplatedCopyable(const U& other) {} // NOLINT
  64. };
  65. class FooInterface {
  66. public:
  67. virtual ~FooInterface() {}
  68. virtual void VoidReturning(int x) = 0;
  69. virtual int Nullary() = 0;
  70. virtual bool Unary(int x) = 0;
  71. virtual long Binary(short x, int y) = 0; // NOLINT
  72. virtual int Decimal(bool b, char c, short d, int e, long f, // NOLINT
  73. float g, double h, unsigned i, char* j,
  74. const std::string& k) = 0;
  75. virtual bool TakesNonConstReference(int& n) = 0; // NOLINT
  76. virtual std::string TakesConstReference(const int& n) = 0;
  77. virtual bool TakesConst(const int x) = 0;
  78. virtual int OverloadedOnArgumentNumber() = 0;
  79. virtual int OverloadedOnArgumentNumber(int n) = 0;
  80. virtual int OverloadedOnArgumentType(int n) = 0;
  81. virtual char OverloadedOnArgumentType(char c) = 0;
  82. virtual int OverloadedOnConstness() = 0;
  83. virtual char OverloadedOnConstness() const = 0;
  84. virtual int TypeWithHole(int (*func)()) = 0;
  85. virtual int TypeWithComma(const std::map<int, std::string>& a_map) = 0;
  86. virtual int TypeWithTemplatedCopyCtor(const TemplatedCopyable<int>&) = 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_METHOD(void, VoidReturning, (int n)); // NOLINT
  109. MOCK_METHOD(int, Nullary, ()); // NOLINT
  110. // Makes sure that a mock function parameter can be unnamed.
  111. MOCK_METHOD(bool, Unary, (int)); // NOLINT
  112. MOCK_METHOD(long, Binary, (short, int)); // NOLINT
  113. MOCK_METHOD(int, Decimal,
  114. (bool, char, short, int, long, float, // NOLINT
  115. double, unsigned, char*, const std::string& str),
  116. (override));
  117. MOCK_METHOD(bool, TakesNonConstReference, (int&)); // NOLINT
  118. MOCK_METHOD(std::string, TakesConstReference, (const int&));
  119. MOCK_METHOD(bool, TakesConst, (const int)); // NOLINT
  120. // Tests that the function return type can contain unprotected comma.
  121. MOCK_METHOD((std::map<int, std::string>), ReturnTypeWithComma, (), ());
  122. MOCK_METHOD((std::map<int, std::string>), ReturnTypeWithComma, (int),
  123. (const)); // NOLINT
  124. MOCK_METHOD(int, OverloadedOnArgumentNumber, ()); // NOLINT
  125. MOCK_METHOD(int, OverloadedOnArgumentNumber, (int)); // NOLINT
  126. MOCK_METHOD(int, OverloadedOnArgumentType, (int)); // NOLINT
  127. MOCK_METHOD(char, OverloadedOnArgumentType, (char)); // NOLINT
  128. MOCK_METHOD(int, OverloadedOnConstness, (), (override)); // NOLINT
  129. MOCK_METHOD(char, OverloadedOnConstness, (), (override, const)); // NOLINT
  130. MOCK_METHOD(int, TypeWithHole, (int (*)()), ()); // NOLINT
  131. MOCK_METHOD(int, TypeWithComma, ((const std::map<int, std::string>&)));
  132. MOCK_METHOD(int, TypeWithTemplatedCopyCtor,
  133. (const TemplatedCopyable<int>&)); // NOLINT
  134. #if GTEST_OS_WINDOWS
  135. MOCK_METHOD(int, CTNullary, (), (Calltype(STDMETHODCALLTYPE)));
  136. MOCK_METHOD(bool, CTUnary, (int), (Calltype(STDMETHODCALLTYPE)));
  137. MOCK_METHOD(int, CTDecimal,
  138. (bool b, char c, short d, int e, long f, float g, double h,
  139. unsigned i, char* j, const std::string& k),
  140. (Calltype(STDMETHODCALLTYPE)));
  141. MOCK_METHOD(char, CTConst, (int), (const, Calltype(STDMETHODCALLTYPE)));
  142. MOCK_METHOD((std::map<int, std::string>), CTReturnTypeWithComma, (),
  143. (Calltype(STDMETHODCALLTYPE)));
  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 MockMethodFunctionMockerTest : public testing::Test {
  152. protected:
  153. MockMethodFunctionMockerTest() : foo_(&mock_foo_) {}
  154. FooInterface* const foo_;
  155. MockFoo mock_foo_;
  156. };
  157. // Tests mocking a void-returning function.
  158. TEST_F(MockMethodFunctionMockerTest, MocksVoidFunction) {
  159. EXPECT_CALL(mock_foo_, VoidReturning(Lt(100)));
  160. foo_->VoidReturning(0);
  161. }
  162. // Tests mocking a nullary function.
  163. TEST_F(MockMethodFunctionMockerTest, 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(MockMethodFunctionMockerTest, 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(MockMethodFunctionMockerTest, 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(MockMethodFunctionMockerTest, 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, nullptr, "hi"));
  190. }
  191. // Tests mocking a function that takes a non-const reference.
  192. TEST_F(MockMethodFunctionMockerTest,
  193. MocksFunctionWithNonConstReferenceArgument) {
  194. int a = 0;
  195. EXPECT_CALL(mock_foo_, TakesNonConstReference(Ref(a)))
  196. .WillOnce(Return(true));
  197. EXPECT_TRUE(foo_->TakesNonConstReference(a));
  198. }
  199. // Tests mocking a function that takes a const reference.
  200. TEST_F(MockMethodFunctionMockerTest, MocksFunctionWithConstReferenceArgument) {
  201. int a = 0;
  202. EXPECT_CALL(mock_foo_, TakesConstReference(Ref(a)))
  203. .WillOnce(Return("Hello"));
  204. EXPECT_EQ("Hello", foo_->TakesConstReference(a));
  205. }
  206. // Tests mocking a function that takes a const variable.
  207. TEST_F(MockMethodFunctionMockerTest, MocksFunctionWithConstArgument) {
  208. EXPECT_CALL(mock_foo_, TakesConst(Lt(10)))
  209. .WillOnce(DoDefault());
  210. EXPECT_FALSE(foo_->TakesConst(5));
  211. }
  212. // Tests mocking functions overloaded on the number of arguments.
  213. TEST_F(MockMethodFunctionMockerTest, MocksFunctionsOverloadedOnArgumentNumber) {
  214. EXPECT_CALL(mock_foo_, OverloadedOnArgumentNumber())
  215. .WillOnce(Return(1));
  216. EXPECT_CALL(mock_foo_, OverloadedOnArgumentNumber(_))
  217. .WillOnce(Return(2));
  218. EXPECT_EQ(2, foo_->OverloadedOnArgumentNumber(1));
  219. EXPECT_EQ(1, foo_->OverloadedOnArgumentNumber());
  220. }
  221. // Tests mocking functions overloaded on the types of argument.
  222. TEST_F(MockMethodFunctionMockerTest, MocksFunctionsOverloadedOnArgumentType) {
  223. EXPECT_CALL(mock_foo_, OverloadedOnArgumentType(An<int>()))
  224. .WillOnce(Return(1));
  225. EXPECT_CALL(mock_foo_, OverloadedOnArgumentType(TypedEq<char>('a')))
  226. .WillOnce(Return('b'));
  227. EXPECT_EQ(1, foo_->OverloadedOnArgumentType(0));
  228. EXPECT_EQ('b', foo_->OverloadedOnArgumentType('a'));
  229. }
  230. // Tests mocking functions overloaded on the const-ness of this object.
  231. TEST_F(MockMethodFunctionMockerTest,
  232. 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(MockMethodFunctionMockerTest, 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. TEST_F(MockMethodFunctionMockerTest, MocksTypeWithTemplatedCopyCtor) {
  249. EXPECT_CALL(mock_foo_, TypeWithTemplatedCopyCtor(_)).WillOnce(Return(true));
  250. EXPECT_TRUE(foo_->TypeWithTemplatedCopyCtor(TemplatedCopyable<int>()));
  251. }
  252. #if GTEST_OS_WINDOWS
  253. // Tests mocking a nullary function with calltype.
  254. TEST_F(MockMethodFunctionMockerTest, MocksNullaryFunctionWithCallType) {
  255. EXPECT_CALL(mock_foo_, CTNullary())
  256. .WillOnce(Return(-1))
  257. .WillOnce(Return(0));
  258. EXPECT_EQ(-1, foo_->CTNullary());
  259. EXPECT_EQ(0, foo_->CTNullary());
  260. }
  261. // Tests mocking a unary function with calltype.
  262. TEST_F(MockMethodFunctionMockerTest, MocksUnaryFunctionWithCallType) {
  263. EXPECT_CALL(mock_foo_, CTUnary(Eq(2)))
  264. .Times(2)
  265. .WillOnce(Return(true))
  266. .WillOnce(Return(false));
  267. EXPECT_TRUE(foo_->CTUnary(2));
  268. EXPECT_FALSE(foo_->CTUnary(2));
  269. }
  270. // Tests mocking a decimal function with calltype.
  271. TEST_F(MockMethodFunctionMockerTest, MocksDecimalFunctionWithCallType) {
  272. EXPECT_CALL(mock_foo_, CTDecimal(true, 'a', 0, 0, 1L, A<float>(),
  273. Lt(100), 5U, NULL, "hi"))
  274. .WillOnce(Return(10));
  275. EXPECT_EQ(10, foo_->CTDecimal(true, 'a', 0, 0, 1, 0, 0, 5, NULL, "hi"));
  276. }
  277. // Tests mocking functions overloaded on the const-ness of this object.
  278. TEST_F(MockMethodFunctionMockerTest, MocksFunctionsConstFunctionWithCallType) {
  279. EXPECT_CALL(Const(mock_foo_), CTConst(_))
  280. .WillOnce(Return('a'));
  281. EXPECT_EQ('a', Const(*foo_).CTConst(0));
  282. }
  283. TEST_F(MockMethodFunctionMockerTest, MocksReturnTypeWithCommaAndCallType) {
  284. const std::map<int, std::string> a_map;
  285. EXPECT_CALL(mock_foo_, CTReturnTypeWithComma())
  286. .WillOnce(Return(a_map));
  287. EXPECT_EQ(a_map, mock_foo_.CTReturnTypeWithComma());
  288. }
  289. #endif // GTEST_OS_WINDOWS
  290. class MockB {
  291. public:
  292. MockB() {}
  293. MOCK_METHOD(void, DoB, ());
  294. private:
  295. GTEST_DISALLOW_COPY_AND_ASSIGN_(MockB);
  296. };
  297. // Tests that functions with no EXPECT_CALL() rules can be called any
  298. // number of times.
  299. TEST(MockMethodExpectCallTest, UnmentionedFunctionCanBeCalledAnyNumberOfTimes) {
  300. {
  301. MockB b;
  302. }
  303. {
  304. MockB b;
  305. b.DoB();
  306. }
  307. {
  308. MockB b;
  309. b.DoB();
  310. b.DoB();
  311. }
  312. }
  313. // Tests mocking template interfaces.
  314. template <typename T>
  315. class StackInterface {
  316. public:
  317. virtual ~StackInterface() {}
  318. // Template parameter appears in function parameter.
  319. virtual void Push(const T& value) = 0;
  320. virtual void Pop() = 0;
  321. virtual int GetSize() const = 0;
  322. // Template parameter appears in function return type.
  323. virtual const T& GetTop() const = 0;
  324. };
  325. template <typename T>
  326. class MockStack : public StackInterface<T> {
  327. public:
  328. MockStack() {}
  329. MOCK_METHOD(void, Push, (const T& elem), ());
  330. MOCK_METHOD(void, Pop, (), (final));
  331. MOCK_METHOD(int, GetSize, (), (const, override));
  332. MOCK_METHOD(const T&, GetTop, (), (const));
  333. // Tests that the function return type can contain unprotected comma.
  334. MOCK_METHOD((std::map<int, int>), ReturnTypeWithComma, (), ());
  335. MOCK_METHOD((std::map<int, int>), ReturnTypeWithComma, (int), (const));
  336. private:
  337. GTEST_DISALLOW_COPY_AND_ASSIGN_(MockStack);
  338. };
  339. // Tests that template mock works.
  340. TEST(MockMethodTemplateMockTest, Works) {
  341. MockStack<int> mock;
  342. EXPECT_CALL(mock, GetSize())
  343. .WillOnce(Return(0))
  344. .WillOnce(Return(1))
  345. .WillOnce(Return(0));
  346. EXPECT_CALL(mock, Push(_));
  347. int n = 5;
  348. EXPECT_CALL(mock, GetTop())
  349. .WillOnce(ReturnRef(n));
  350. EXPECT_CALL(mock, Pop())
  351. .Times(AnyNumber());
  352. EXPECT_EQ(0, mock.GetSize());
  353. mock.Push(5);
  354. EXPECT_EQ(1, mock.GetSize());
  355. EXPECT_EQ(5, mock.GetTop());
  356. mock.Pop();
  357. EXPECT_EQ(0, mock.GetSize());
  358. }
  359. TEST(MockMethodTemplateMockTest, MethodWithCommaInReturnTypeWorks) {
  360. MockStack<int> mock;
  361. const std::map<int, int> a_map;
  362. EXPECT_CALL(mock, ReturnTypeWithComma())
  363. .WillOnce(Return(a_map));
  364. EXPECT_CALL(mock, ReturnTypeWithComma(1))
  365. .WillOnce(Return(a_map));
  366. EXPECT_EQ(a_map, mock.ReturnTypeWithComma());
  367. EXPECT_EQ(a_map, mock.ReturnTypeWithComma(1));
  368. }
  369. #if GTEST_OS_WINDOWS
  370. // Tests mocking template interfaces with calltype.
  371. template <typename T>
  372. class StackInterfaceWithCallType {
  373. public:
  374. virtual ~StackInterfaceWithCallType() {}
  375. // Template parameter appears in function parameter.
  376. STDMETHOD_(void, Push)(const T& value) = 0;
  377. STDMETHOD_(void, Pop)() = 0;
  378. STDMETHOD_(int, GetSize)() const = 0;
  379. // Template parameter appears in function return type.
  380. STDMETHOD_(const T&, GetTop)() const = 0;
  381. };
  382. template <typename T>
  383. class MockStackWithCallType : public StackInterfaceWithCallType<T> {
  384. public:
  385. MockStackWithCallType() {}
  386. MOCK_METHOD(void, Push, (const T& elem),
  387. (Calltype(STDMETHODCALLTYPE), override));
  388. MOCK_METHOD(void, Pop, (), (Calltype(STDMETHODCALLTYPE), override));
  389. MOCK_METHOD(int, GetSize, (), (Calltype(STDMETHODCALLTYPE), override, const));
  390. MOCK_METHOD(const T&, GetTop, (),
  391. (Calltype(STDMETHODCALLTYPE), override, const));
  392. private:
  393. GTEST_DISALLOW_COPY_AND_ASSIGN_(MockStackWithCallType);
  394. };
  395. // Tests that template mock with calltype works.
  396. TEST(MockMethodTemplateMockTestWithCallType, Works) {
  397. MockStackWithCallType<int> mock;
  398. EXPECT_CALL(mock, GetSize())
  399. .WillOnce(Return(0))
  400. .WillOnce(Return(1))
  401. .WillOnce(Return(0));
  402. EXPECT_CALL(mock, Push(_));
  403. int n = 5;
  404. EXPECT_CALL(mock, GetTop())
  405. .WillOnce(ReturnRef(n));
  406. EXPECT_CALL(mock, Pop())
  407. .Times(AnyNumber());
  408. EXPECT_EQ(0, mock.GetSize());
  409. mock.Push(5);
  410. EXPECT_EQ(1, mock.GetSize());
  411. EXPECT_EQ(5, mock.GetTop());
  412. mock.Pop();
  413. EXPECT_EQ(0, mock.GetSize());
  414. }
  415. #endif // GTEST_OS_WINDOWS
  416. #define MY_MOCK_METHODS1_ \
  417. MOCK_METHOD(void, Overloaded, ()); \
  418. MOCK_METHOD(int, Overloaded, (int), (const)); \
  419. MOCK_METHOD(bool, Overloaded, (bool f, int n))
  420. class MockOverloadedOnArgNumber {
  421. public:
  422. MockOverloadedOnArgNumber() {}
  423. MY_MOCK_METHODS1_;
  424. private:
  425. GTEST_DISALLOW_COPY_AND_ASSIGN_(MockOverloadedOnArgNumber);
  426. };
  427. TEST(MockMethodOverloadedMockMethodTest, CanOverloadOnArgNumberInMacroBody) {
  428. MockOverloadedOnArgNumber mock;
  429. EXPECT_CALL(mock, Overloaded());
  430. EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2));
  431. EXPECT_CALL(mock, Overloaded(true, 1)).WillOnce(Return(true));
  432. mock.Overloaded();
  433. EXPECT_EQ(2, mock.Overloaded(1));
  434. EXPECT_TRUE(mock.Overloaded(true, 1));
  435. }
  436. #define MY_MOCK_METHODS2_ \
  437. MOCK_CONST_METHOD1(Overloaded, int(int n)); \
  438. MOCK_METHOD1(Overloaded, int(int n))
  439. class MockOverloadedOnConstness {
  440. public:
  441. MockOverloadedOnConstness() {}
  442. MY_MOCK_METHODS2_;
  443. private:
  444. GTEST_DISALLOW_COPY_AND_ASSIGN_(MockOverloadedOnConstness);
  445. };
  446. TEST(MockMethodOverloadedMockMethodTest, CanOverloadOnConstnessInMacroBody) {
  447. MockOverloadedOnConstness mock;
  448. const MockOverloadedOnConstness* const_mock = &mock;
  449. EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2));
  450. EXPECT_CALL(*const_mock, Overloaded(1)).WillOnce(Return(3));
  451. EXPECT_EQ(2, mock.Overloaded(1));
  452. EXPECT_EQ(3, const_mock->Overloaded(1));
  453. }
  454. TEST(MockMethodMockFunctionTest, WorksForVoidNullary) {
  455. MockFunction<void()> foo;
  456. EXPECT_CALL(foo, Call());
  457. foo.Call();
  458. }
  459. TEST(MockMethodMockFunctionTest, WorksForNonVoidNullary) {
  460. MockFunction<int()> foo;
  461. EXPECT_CALL(foo, Call())
  462. .WillOnce(Return(1))
  463. .WillOnce(Return(2));
  464. EXPECT_EQ(1, foo.Call());
  465. EXPECT_EQ(2, foo.Call());
  466. }
  467. TEST(MockMethodMockFunctionTest, WorksForVoidUnary) {
  468. MockFunction<void(int)> foo;
  469. EXPECT_CALL(foo, Call(1));
  470. foo.Call(1);
  471. }
  472. TEST(MockMethodMockFunctionTest, WorksForNonVoidBinary) {
  473. MockFunction<int(bool, int)> foo;
  474. EXPECT_CALL(foo, Call(false, 42))
  475. .WillOnce(Return(1))
  476. .WillOnce(Return(2));
  477. EXPECT_CALL(foo, Call(true, Ge(100)))
  478. .WillOnce(Return(3));
  479. EXPECT_EQ(1, foo.Call(false, 42));
  480. EXPECT_EQ(2, foo.Call(false, 42));
  481. EXPECT_EQ(3, foo.Call(true, 120));
  482. }
  483. TEST(MockMethodMockFunctionTest, WorksFor10Arguments) {
  484. MockFunction<int(bool a0, char a1, int a2, int a3, int a4,
  485. int a5, int a6, char a7, int a8, bool a9)> foo;
  486. EXPECT_CALL(foo, Call(_, 'a', _, _, _, _, _, _, _, _))
  487. .WillOnce(Return(1))
  488. .WillOnce(Return(2));
  489. EXPECT_EQ(1, foo.Call(false, 'a', 0, 0, 0, 0, 0, 'b', 0, true));
  490. EXPECT_EQ(2, foo.Call(true, 'a', 0, 0, 0, 0, 0, 'b', 1, false));
  491. }
  492. TEST(MockMethodMockFunctionTest, AsStdFunction) {
  493. MockFunction<int(int)> foo;
  494. auto call = [](const std::function<int(int)> &f, int i) {
  495. return f(i);
  496. };
  497. EXPECT_CALL(foo, Call(1)).WillOnce(Return(-1));
  498. EXPECT_CALL(foo, Call(2)).WillOnce(Return(-2));
  499. EXPECT_EQ(-1, call(foo.AsStdFunction(), 1));
  500. EXPECT_EQ(-2, call(foo.AsStdFunction(), 2));
  501. }
  502. TEST(MockMethodMockFunctionTest, AsStdFunctionReturnsReference) {
  503. MockFunction<int&()> foo;
  504. int value = 1;
  505. EXPECT_CALL(foo, Call()).WillOnce(ReturnRef(value));
  506. int& ref = foo.AsStdFunction()();
  507. EXPECT_EQ(1, ref);
  508. value = 2;
  509. EXPECT_EQ(2, ref);
  510. }
  511. TEST(MockMethodMockFunctionTest, AsStdFunctionWithReferenceParameter) {
  512. MockFunction<int(int &)> foo;
  513. auto call = [](const std::function<int(int& )> &f, int &i) {
  514. return f(i);
  515. };
  516. int i = 42;
  517. EXPECT_CALL(foo, Call(i)).WillOnce(Return(-1));
  518. EXPECT_EQ(-1, call(foo.AsStdFunction(), i));
  519. }
  520. struct MockMethodSizes0 {
  521. MOCK_METHOD(void, func, ());
  522. };
  523. struct MockMethodSizes1 {
  524. MOCK_METHOD(void, func, (int));
  525. };
  526. struct MockMethodSizes2 {
  527. MOCK_METHOD(void, func, (int, int));
  528. };
  529. struct MockMethodSizes3 {
  530. MOCK_METHOD(void, func, (int, int, int));
  531. };
  532. struct MockMethodSizes4 {
  533. MOCK_METHOD(void, func, (int, int, int, int));
  534. };
  535. TEST(MockMethodMockFunctionTest, MockMethodSizeOverhead) {
  536. EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes1));
  537. EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes2));
  538. EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes3));
  539. EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes4));
  540. }
  541. } // namespace gmock_function_mocker_test
  542. } // namespace testing