You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

659 lines
20 KiB

  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_generated_function_mockers_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(
  87. const TemplatedCopyable<int>& a_vector) = 0;
  88. #if GTEST_OS_WINDOWS
  89. STDMETHOD_(int, CTNullary)() = 0;
  90. STDMETHOD_(bool, CTUnary)(int x) = 0;
  91. STDMETHOD_(int, CTDecimal)
  92. (bool b, char c, short d, int e, long f, // NOLINT
  93. float g, double h, unsigned i, char* j, const std::string& k) = 0;
  94. STDMETHOD_(char, CTConst)(int x) const = 0;
  95. #endif // GTEST_OS_WINDOWS
  96. };
  97. // Const qualifiers on arguments were once (incorrectly) considered
  98. // significant in determining whether two virtual functions had the same
  99. // signature. This was fixed in Visual Studio 2008. However, the compiler
  100. // still emits a warning that alerts about this change in behavior.
  101. #ifdef _MSC_VER
  102. # pragma warning(push)
  103. # pragma warning(disable : 4373)
  104. #endif
  105. class MockFoo : public FooInterface {
  106. public:
  107. MockFoo() {}
  108. // Makes sure that a mock function parameter can be named.
  109. MOCK_METHOD1(VoidReturning, void(int n)); // NOLINT
  110. MOCK_METHOD0(Nullary, int()); // NOLINT
  111. // Makes sure that a mock function parameter can be unnamed.
  112. MOCK_METHOD1(Unary, bool(int)); // NOLINT
  113. MOCK_METHOD2(Binary, long(short, int)); // NOLINT
  114. MOCK_METHOD10(Decimal, int(bool, char, short, int, long, float, // NOLINT
  115. double, unsigned, char*, const std::string& str));
  116. MOCK_METHOD1(TakesNonConstReference, bool(int&)); // NOLINT
  117. MOCK_METHOD1(TakesConstReference, std::string(const int&));
  118. MOCK_METHOD1(TakesConst, bool(const int)); // NOLINT
  119. // Tests that the function return type can contain unprotected comma.
  120. MOCK_METHOD0(ReturnTypeWithComma, std::map<int, std::string>());
  121. MOCK_CONST_METHOD1(ReturnTypeWithComma,
  122. std::map<int, std::string>(int)); // NOLINT
  123. MOCK_METHOD0(OverloadedOnArgumentNumber, int()); // NOLINT
  124. MOCK_METHOD1(OverloadedOnArgumentNumber, int(int)); // NOLINT
  125. MOCK_METHOD1(OverloadedOnArgumentType, int(int)); // NOLINT
  126. MOCK_METHOD1(OverloadedOnArgumentType, char(char)); // NOLINT
  127. MOCK_METHOD0(OverloadedOnConstness, int()); // NOLINT
  128. MOCK_CONST_METHOD0(OverloadedOnConstness, char()); // NOLINT
  129. MOCK_METHOD1(TypeWithHole, int(int (*)())); // NOLINT
  130. MOCK_METHOD1(TypeWithComma,
  131. int(const std::map<int, std::string>&)); // NOLINT
  132. MOCK_METHOD1(TypeWithTemplatedCopyCtor,
  133. int(const TemplatedCopyable<int>&)); // NOLINT
  134. #if GTEST_OS_WINDOWS
  135. MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, CTNullary, int());
  136. MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, CTUnary, bool(int));
  137. MOCK_METHOD10_WITH_CALLTYPE(STDMETHODCALLTYPE, CTDecimal,
  138. int(bool b, char c, short d, int e, long f,
  139. float g, double h, unsigned i, char* j,
  140. const std::string& k));
  141. MOCK_CONST_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, CTConst, char(int));
  142. // Tests that the function return type can contain unprotected comma.
  143. MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, CTReturnTypeWithComma,
  144. std::map<int, std::string>());
  145. #endif // GTEST_OS_WINDOWS
  146. private:
  147. GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
  148. };
  149. #ifdef _MSC_VER
  150. # pragma warning(pop)
  151. #endif
  152. class FunctionMockerTest : public testing::Test {
  153. protected:
  154. FunctionMockerTest() : foo_(&mock_foo_) {}
  155. FooInterface* const foo_;
  156. MockFoo mock_foo_;
  157. };
  158. // Tests mocking a void-returning function.
  159. TEST_F(FunctionMockerTest, MocksVoidFunction) {
  160. EXPECT_CALL(mock_foo_, VoidReturning(Lt(100)));
  161. foo_->VoidReturning(0);
  162. }
  163. // Tests mocking a nullary function.
  164. TEST_F(FunctionMockerTest, MocksNullaryFunction) {
  165. EXPECT_CALL(mock_foo_, Nullary())
  166. .WillOnce(DoDefault())
  167. .WillOnce(Return(1));
  168. EXPECT_EQ(0, foo_->Nullary());
  169. EXPECT_EQ(1, foo_->Nullary());
  170. }
  171. // Tests mocking a unary function.
  172. TEST_F(FunctionMockerTest, MocksUnaryFunction) {
  173. EXPECT_CALL(mock_foo_, Unary(Eq(2)))
  174. .Times(2)
  175. .WillOnce(Return(true));
  176. EXPECT_TRUE(foo_->Unary(2));
  177. EXPECT_FALSE(foo_->Unary(2));
  178. }
  179. // Tests mocking a binary function.
  180. TEST_F(FunctionMockerTest, MocksBinaryFunction) {
  181. EXPECT_CALL(mock_foo_, Binary(2, _))
  182. .WillOnce(Return(3));
  183. EXPECT_EQ(3, foo_->Binary(2, 1));
  184. }
  185. // Tests mocking a decimal function.
  186. TEST_F(FunctionMockerTest, MocksDecimalFunction) {
  187. EXPECT_CALL(mock_foo_, Decimal(true, 'a', 0, 0, 1L, A<float>(), Lt(100), 5U,
  188. nullptr, "hi"))
  189. .WillOnce(Return(5));
  190. EXPECT_EQ(5, foo_->Decimal(true, 'a', 0, 0, 1, 0, 0, 5, nullptr, "hi"));
  191. }
  192. // Tests mocking a function that takes a non-const reference.
  193. TEST_F(FunctionMockerTest, 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(FunctionMockerTest, 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(FunctionMockerTest, 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(FunctionMockerTest, 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(FunctionMockerTest, 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(FunctionMockerTest, MocksFunctionsOverloadedOnConstnessOfThis) {
  232. EXPECT_CALL(mock_foo_, OverloadedOnConstness());
  233. EXPECT_CALL(Const(mock_foo_), OverloadedOnConstness())
  234. .WillOnce(Return('a'));
  235. EXPECT_EQ(0, foo_->OverloadedOnConstness());
  236. EXPECT_EQ('a', Const(*foo_).OverloadedOnConstness());
  237. }
  238. TEST_F(FunctionMockerTest, MocksReturnTypeWithComma) {
  239. const std::map<int, std::string> a_map;
  240. EXPECT_CALL(mock_foo_, ReturnTypeWithComma())
  241. .WillOnce(Return(a_map));
  242. EXPECT_CALL(mock_foo_, ReturnTypeWithComma(42))
  243. .WillOnce(Return(a_map));
  244. EXPECT_EQ(a_map, mock_foo_.ReturnTypeWithComma());
  245. EXPECT_EQ(a_map, mock_foo_.ReturnTypeWithComma(42));
  246. }
  247. TEST_F(FunctionMockerTest, MocksTypeWithTemplatedCopyCtor) {
  248. EXPECT_CALL(mock_foo_, TypeWithTemplatedCopyCtor(_)).WillOnce(Return(true));
  249. EXPECT_TRUE(foo_->TypeWithTemplatedCopyCtor(TemplatedCopyable<int>()));
  250. }
  251. #if GTEST_OS_WINDOWS
  252. // Tests mocking a nullary function with calltype.
  253. TEST_F(FunctionMockerTest, MocksNullaryFunctionWithCallType) {
  254. EXPECT_CALL(mock_foo_, CTNullary())
  255. .WillOnce(Return(-1))
  256. .WillOnce(Return(0));
  257. EXPECT_EQ(-1, foo_->CTNullary());
  258. EXPECT_EQ(0, foo_->CTNullary());
  259. }
  260. // Tests mocking a unary function with calltype.
  261. TEST_F(FunctionMockerTest, MocksUnaryFunctionWithCallType) {
  262. EXPECT_CALL(mock_foo_, CTUnary(Eq(2)))
  263. .Times(2)
  264. .WillOnce(Return(true))
  265. .WillOnce(Return(false));
  266. EXPECT_TRUE(foo_->CTUnary(2));
  267. EXPECT_FALSE(foo_->CTUnary(2));
  268. }
  269. // Tests mocking a decimal function with calltype.
  270. TEST_F(FunctionMockerTest, MocksDecimalFunctionWithCallType) {
  271. EXPECT_CALL(mock_foo_, CTDecimal(true, 'a', 0, 0, 1L, A<float>(), Lt(100), 5U,
  272. nullptr, "hi"))
  273. .WillOnce(Return(10));
  274. EXPECT_EQ(10, foo_->CTDecimal(true, 'a', 0, 0, 1, 0, 0, 5, nullptr, "hi"));
  275. }
  276. // Tests mocking functions overloaded on the const-ness of this object.
  277. TEST_F(FunctionMockerTest, MocksFunctionsConstFunctionWithCallType) {
  278. EXPECT_CALL(Const(mock_foo_), CTConst(_))
  279. .WillOnce(Return('a'));
  280. EXPECT_EQ('a', Const(*foo_).CTConst(0));
  281. }
  282. TEST_F(FunctionMockerTest, MocksReturnTypeWithCommaAndCallType) {
  283. const std::map<int, std::string> a_map;
  284. EXPECT_CALL(mock_foo_, CTReturnTypeWithComma())
  285. .WillOnce(Return(a_map));
  286. EXPECT_EQ(a_map, mock_foo_.CTReturnTypeWithComma());
  287. }
  288. #endif // GTEST_OS_WINDOWS
  289. class MockB {
  290. public:
  291. MockB() {}
  292. MOCK_METHOD0(DoB, void());
  293. private:
  294. GTEST_DISALLOW_COPY_AND_ASSIGN_(MockB);
  295. };
  296. // Tests that functions with no EXPECT_CALL() ruls can be called any
  297. // number of times.
  298. TEST(ExpectCallTest, UnmentionedFunctionCanBeCalledAnyNumberOfTimes) {
  299. {
  300. MockB b;
  301. }
  302. {
  303. MockB b;
  304. b.DoB();
  305. }
  306. {
  307. MockB b;
  308. b.DoB();
  309. b.DoB();
  310. }
  311. }
  312. // Tests mocking template interfaces.
  313. template <typename T>
  314. class StackInterface {
  315. public:
  316. virtual ~StackInterface() {}
  317. // Template parameter appears in function parameter.
  318. virtual void Push(const T& value) = 0;
  319. virtual void Pop() = 0;
  320. virtual int GetSize() const = 0;
  321. // Template parameter appears in function return type.
  322. virtual const T& GetTop() const = 0;
  323. };
  324. template <typename T>
  325. class MockStack : public StackInterface<T> {
  326. public:
  327. MockStack() {}
  328. MOCK_METHOD1_T(Push, void(const T& elem));
  329. MOCK_METHOD0_T(Pop, void());
  330. MOCK_CONST_METHOD0_T(GetSize, int()); // NOLINT
  331. MOCK_CONST_METHOD0_T(GetTop, const T&());
  332. // Tests that the function return type can contain unprotected comma.
  333. MOCK_METHOD0_T(ReturnTypeWithComma, std::map<int, int>());
  334. MOCK_CONST_METHOD1_T(ReturnTypeWithComma, std::map<int, int>(int)); // NOLINT
  335. private:
  336. GTEST_DISALLOW_COPY_AND_ASSIGN_(MockStack);
  337. };
  338. // Tests that template mock works.
  339. TEST(TemplateMockTest, Works) {
  340. MockStack<int> mock;
  341. EXPECT_CALL(mock, GetSize())
  342. .WillOnce(Return(0))
  343. .WillOnce(Return(1))
  344. .WillOnce(Return(0));
  345. EXPECT_CALL(mock, Push(_));
  346. int n = 5;
  347. EXPECT_CALL(mock, GetTop())
  348. .WillOnce(ReturnRef(n));
  349. EXPECT_CALL(mock, Pop())
  350. .Times(AnyNumber());
  351. EXPECT_EQ(0, mock.GetSize());
  352. mock.Push(5);
  353. EXPECT_EQ(1, mock.GetSize());
  354. EXPECT_EQ(5, mock.GetTop());
  355. mock.Pop();
  356. EXPECT_EQ(0, mock.GetSize());
  357. }
  358. TEST(TemplateMockTest, MethodWithCommaInReturnTypeWorks) {
  359. MockStack<int> mock;
  360. const std::map<int, int> a_map;
  361. EXPECT_CALL(mock, ReturnTypeWithComma())
  362. .WillOnce(Return(a_map));
  363. EXPECT_CALL(mock, ReturnTypeWithComma(1))
  364. .WillOnce(Return(a_map));
  365. EXPECT_EQ(a_map, mock.ReturnTypeWithComma());
  366. EXPECT_EQ(a_map, mock.ReturnTypeWithComma(1));
  367. }
  368. #if GTEST_OS_WINDOWS
  369. // Tests mocking template interfaces with calltype.
  370. template <typename T>
  371. class StackInterfaceWithCallType {
  372. public:
  373. virtual ~StackInterfaceWithCallType() {}
  374. // Template parameter appears in function parameter.
  375. STDMETHOD_(void, Push)(const T& value) = 0;
  376. STDMETHOD_(void, Pop)() = 0;
  377. STDMETHOD_(int, GetSize)() const = 0;
  378. // Template parameter appears in function return type.
  379. STDMETHOD_(const T&, GetTop)() const = 0;
  380. };
  381. template <typename T>
  382. class MockStackWithCallType : public StackInterfaceWithCallType<T> {
  383. public:
  384. MockStackWithCallType() {}
  385. MOCK_METHOD1_T_WITH_CALLTYPE(STDMETHODCALLTYPE, Push, void(const T& elem));
  386. MOCK_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, Pop, void());
  387. MOCK_CONST_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, GetSize, int());
  388. MOCK_CONST_METHOD0_T_WITH_CALLTYPE(STDMETHODCALLTYPE, GetTop, const T&());
  389. private:
  390. GTEST_DISALLOW_COPY_AND_ASSIGN_(MockStackWithCallType);
  391. };
  392. // Tests that template mock with calltype works.
  393. TEST(TemplateMockTestWithCallType, Works) {
  394. MockStackWithCallType<int> mock;
  395. EXPECT_CALL(mock, GetSize())
  396. .WillOnce(Return(0))
  397. .WillOnce(Return(1))
  398. .WillOnce(Return(0));
  399. EXPECT_CALL(mock, Push(_));
  400. int n = 5;
  401. EXPECT_CALL(mock, GetTop())
  402. .WillOnce(ReturnRef(n));
  403. EXPECT_CALL(mock, Pop())
  404. .Times(AnyNumber());
  405. EXPECT_EQ(0, mock.GetSize());
  406. mock.Push(5);
  407. EXPECT_EQ(1, mock.GetSize());
  408. EXPECT_EQ(5, mock.GetTop());
  409. mock.Pop();
  410. EXPECT_EQ(0, mock.GetSize());
  411. }
  412. #endif // GTEST_OS_WINDOWS
  413. #define MY_MOCK_METHODS1_ \
  414. MOCK_METHOD0(Overloaded, void()); \
  415. MOCK_CONST_METHOD1(Overloaded, int(int n)); \
  416. MOCK_METHOD2(Overloaded, bool(bool f, int n))
  417. class MockOverloadedOnArgNumber {
  418. public:
  419. MockOverloadedOnArgNumber() {}
  420. MY_MOCK_METHODS1_;
  421. private:
  422. GTEST_DISALLOW_COPY_AND_ASSIGN_(MockOverloadedOnArgNumber);
  423. };
  424. TEST(OverloadedMockMethodTest, CanOverloadOnArgNumberInMacroBody) {
  425. MockOverloadedOnArgNumber mock;
  426. EXPECT_CALL(mock, Overloaded());
  427. EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2));
  428. EXPECT_CALL(mock, Overloaded(true, 1)).WillOnce(Return(true));
  429. mock.Overloaded();
  430. EXPECT_EQ(2, mock.Overloaded(1));
  431. EXPECT_TRUE(mock.Overloaded(true, 1));
  432. }
  433. #define MY_MOCK_METHODS2_ \
  434. MOCK_CONST_METHOD1(Overloaded, int(int n)); \
  435. MOCK_METHOD1(Overloaded, int(int n))
  436. class MockOverloadedOnConstness {
  437. public:
  438. MockOverloadedOnConstness() {}
  439. MY_MOCK_METHODS2_;
  440. private:
  441. GTEST_DISALLOW_COPY_AND_ASSIGN_(MockOverloadedOnConstness);
  442. };
  443. TEST(OverloadedMockMethodTest, CanOverloadOnConstnessInMacroBody) {
  444. MockOverloadedOnConstness mock;
  445. const MockOverloadedOnConstness* const_mock = &mock;
  446. EXPECT_CALL(mock, Overloaded(1)).WillOnce(Return(2));
  447. EXPECT_CALL(*const_mock, Overloaded(1)).WillOnce(Return(3));
  448. EXPECT_EQ(2, mock.Overloaded(1));
  449. EXPECT_EQ(3, const_mock->Overloaded(1));
  450. }
  451. TEST(MockFunctionTest, WorksForVoidNullary) {
  452. MockFunction<void()> foo;
  453. EXPECT_CALL(foo, Call());
  454. foo.Call();
  455. }
  456. TEST(MockFunctionTest, WorksForNonVoidNullary) {
  457. MockFunction<int()> foo;
  458. EXPECT_CALL(foo, Call())
  459. .WillOnce(Return(1))
  460. .WillOnce(Return(2));
  461. EXPECT_EQ(1, foo.Call());
  462. EXPECT_EQ(2, foo.Call());
  463. }
  464. TEST(MockFunctionTest, WorksForVoidUnary) {
  465. MockFunction<void(int)> foo;
  466. EXPECT_CALL(foo, Call(1));
  467. foo.Call(1);
  468. }
  469. TEST(MockFunctionTest, WorksForNonVoidBinary) {
  470. MockFunction<int(bool, int)> foo;
  471. EXPECT_CALL(foo, Call(false, 42))
  472. .WillOnce(Return(1))
  473. .WillOnce(Return(2));
  474. EXPECT_CALL(foo, Call(true, Ge(100)))
  475. .WillOnce(Return(3));
  476. EXPECT_EQ(1, foo.Call(false, 42));
  477. EXPECT_EQ(2, foo.Call(false, 42));
  478. EXPECT_EQ(3, foo.Call(true, 120));
  479. }
  480. TEST(MockFunctionTest, WorksFor10Arguments) {
  481. MockFunction<int(bool a0, char a1, int a2, int a3, int a4,
  482. int a5, int a6, char a7, int a8, bool a9)> foo;
  483. EXPECT_CALL(foo, Call(_, 'a', _, _, _, _, _, _, _, _))
  484. .WillOnce(Return(1))
  485. .WillOnce(Return(2));
  486. EXPECT_EQ(1, foo.Call(false, 'a', 0, 0, 0, 0, 0, 'b', 0, true));
  487. EXPECT_EQ(2, foo.Call(true, 'a', 0, 0, 0, 0, 0, 'b', 1, false));
  488. }
  489. TEST(MockFunctionTest, AsStdFunction) {
  490. MockFunction<int(int)> foo;
  491. auto call = [](const std::function<int(int)> &f, int i) {
  492. return f(i);
  493. };
  494. EXPECT_CALL(foo, Call(1)).WillOnce(Return(-1));
  495. EXPECT_CALL(foo, Call(2)).WillOnce(Return(-2));
  496. EXPECT_EQ(-1, call(foo.AsStdFunction(), 1));
  497. EXPECT_EQ(-2, call(foo.AsStdFunction(), 2));
  498. }
  499. TEST(MockFunctionTest, AsStdFunctionReturnsReference) {
  500. MockFunction<int&()> foo;
  501. int value = 1;
  502. EXPECT_CALL(foo, Call()).WillOnce(ReturnRef(value));
  503. int& ref = foo.AsStdFunction()();
  504. EXPECT_EQ(1, ref);
  505. value = 2;
  506. EXPECT_EQ(2, ref);
  507. }
  508. TEST(MockFunctionTest, AsStdFunctionWithReferenceParameter) {
  509. MockFunction<int(int &)> foo;
  510. auto call = [](const std::function<int(int& )> &f, int &i) {
  511. return f(i);
  512. };
  513. int i = 42;
  514. EXPECT_CALL(foo, Call(i)).WillOnce(Return(-1));
  515. EXPECT_EQ(-1, call(foo.AsStdFunction(), i));
  516. }
  517. struct MockMethodSizes0 {
  518. MOCK_METHOD0(func, void());
  519. };
  520. struct MockMethodSizes1 {
  521. MOCK_METHOD1(func, void(int));
  522. };
  523. struct MockMethodSizes2 {
  524. MOCK_METHOD2(func, void(int, int));
  525. };
  526. struct MockMethodSizes3 {
  527. MOCK_METHOD3(func, void(int, int, int));
  528. };
  529. struct MockMethodSizes4 {
  530. MOCK_METHOD4(func, void(int, int, int, int));
  531. };
  532. TEST(MockFunctionTest, MockMethodSizeOverhead) {
  533. EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes1));
  534. EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes2));
  535. EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes3));
  536. EXPECT_EQ(sizeof(MockMethodSizes0), sizeof(MockMethodSizes4));
  537. }
  538. } // namespace gmock_generated_function_mockers_test
  539. } // namespace testing