gmock-generated-actions_test.cc 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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 built-in actions generated by a script.
  32. #include "gmock/gmock-generated-actions.h"
  33. #include <functional>
  34. #include <memory>
  35. #include <sstream>
  36. #include <string>
  37. #include "gmock/gmock.h"
  38. #include "gtest/gtest.h"
  39. namespace testing {
  40. namespace gmock_generated_actions_test {
  41. using ::std::plus;
  42. using ::std::string;
  43. using testing::_;
  44. using testing::Action;
  45. using testing::ActionInterface;
  46. using testing::ByRef;
  47. using testing::DoAll;
  48. using testing::Invoke;
  49. using testing::Return;
  50. using testing::ReturnNew;
  51. using testing::SetArgPointee;
  52. using testing::StaticAssertTypeEq;
  53. using testing::Unused;
  54. // For suppressing compiler warnings on conversion possibly losing precision.
  55. inline short Short(short n) { return n; } // NOLINT
  56. inline char Char(char ch) { return ch; }
  57. // Sample functions and functors for testing various actions.
  58. int Nullary() { return 1; }
  59. bool g_done = false;
  60. bool ByConstRef(const std::string& s) { return s == "Hi"; }
  61. const double g_double = 0;
  62. bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
  63. struct UnaryFunctor {
  64. int operator()(bool x) { return x ? 1 : -1; }
  65. };
  66. const char* Binary(const char* input, short n) { return input + n; } // NOLINT
  67. int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
  68. struct SumOf5Functor {
  69. int operator()(int a, int b, int c, int d, int e) {
  70. return a + b + c + d + e;
  71. }
  72. };
  73. std::string Concat5(const char* s1, const char* s2, const char* s3,
  74. const char* s4, const char* s5) {
  75. return std::string(s1) + s2 + s3 + s4 + s5;
  76. }
  77. int SumOf6(int a, int b, int c, int d, int e, int f) {
  78. return a + b + c + d + e + f;
  79. }
  80. struct SumOf6Functor {
  81. int operator()(int a, int b, int c, int d, int e, int f) {
  82. return a + b + c + d + e + f;
  83. }
  84. };
  85. std::string Concat6(const char* s1, const char* s2, const char* s3,
  86. const char* s4, const char* s5, const char* s6) {
  87. return std::string(s1) + s2 + s3 + s4 + s5 + s6;
  88. }
  89. std::string Concat7(const char* s1, const char* s2, const char* s3,
  90. const char* s4, const char* s5, const char* s6,
  91. const char* s7) {
  92. return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
  93. }
  94. std::string Concat8(const char* s1, const char* s2, const char* s3,
  95. const char* s4, const char* s5, const char* s6,
  96. const char* s7, const char* s8) {
  97. return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
  98. }
  99. std::string Concat9(const char* s1, const char* s2, const char* s3,
  100. const char* s4, const char* s5, const char* s6,
  101. const char* s7, const char* s8, const char* s9) {
  102. return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
  103. }
  104. std::string Concat10(const char* s1, const char* s2, const char* s3,
  105. const char* s4, const char* s5, const char* s6,
  106. const char* s7, const char* s8, const char* s9,
  107. const char* s10) {
  108. return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
  109. }
  110. // A helper that turns the type of a C-string literal from const
  111. // char[N] to const char*.
  112. inline const char* CharPtr(const char* s) { return s; }
  113. // Tests InvokeArgument<N>(...).
  114. // Tests using InvokeArgument with a nullary function.
  115. TEST(InvokeArgumentTest, Function0) {
  116. Action<int(int, int(*)())> a = InvokeArgument<1>(); // NOLINT
  117. EXPECT_EQ(1, a.Perform(std::make_tuple(2, &Nullary)));
  118. }
  119. // Tests using InvokeArgument with a unary function.
  120. TEST(InvokeArgumentTest, Functor1) {
  121. Action<int(UnaryFunctor)> a = InvokeArgument<0>(true); // NOLINT
  122. EXPECT_EQ(1, a.Perform(std::make_tuple(UnaryFunctor())));
  123. }
  124. // Tests using InvokeArgument with a 5-ary function.
  125. TEST(InvokeArgumentTest, Function5) {
  126. Action<int(int(*)(int, int, int, int, int))> a = // NOLINT
  127. InvokeArgument<0>(10000, 2000, 300, 40, 5);
  128. EXPECT_EQ(12345, a.Perform(std::make_tuple(&SumOf5)));
  129. }
  130. // Tests using InvokeArgument with a 5-ary functor.
  131. TEST(InvokeArgumentTest, Functor5) {
  132. Action<int(SumOf5Functor)> a = // NOLINT
  133. InvokeArgument<0>(10000, 2000, 300, 40, 5);
  134. EXPECT_EQ(12345, a.Perform(std::make_tuple(SumOf5Functor())));
  135. }
  136. // Tests using InvokeArgument with a 6-ary function.
  137. TEST(InvokeArgumentTest, Function6) {
  138. Action<int(int(*)(int, int, int, int, int, int))> a = // NOLINT
  139. InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
  140. EXPECT_EQ(123456, a.Perform(std::make_tuple(&SumOf6)));
  141. }
  142. // Tests using InvokeArgument with a 6-ary functor.
  143. TEST(InvokeArgumentTest, Functor6) {
  144. Action<int(SumOf6Functor)> a = // NOLINT
  145. InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
  146. EXPECT_EQ(123456, a.Perform(std::make_tuple(SumOf6Functor())));
  147. }
  148. // Tests using InvokeArgument with a 7-ary function.
  149. TEST(InvokeArgumentTest, Function7) {
  150. Action<std::string(std::string(*)(const char*, const char*, const char*,
  151. const char*, const char*, const char*,
  152. const char*))>
  153. a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7");
  154. EXPECT_EQ("1234567", a.Perform(std::make_tuple(&Concat7)));
  155. }
  156. // Tests using InvokeArgument with a 8-ary function.
  157. TEST(InvokeArgumentTest, Function8) {
  158. Action<std::string(std::string(*)(const char*, const char*, const char*,
  159. const char*, const char*, const char*,
  160. const char*, const char*))>
  161. a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8");
  162. EXPECT_EQ("12345678", a.Perform(std::make_tuple(&Concat8)));
  163. }
  164. // Tests using InvokeArgument with a 9-ary function.
  165. TEST(InvokeArgumentTest, Function9) {
  166. Action<std::string(std::string(*)(const char*, const char*, const char*,
  167. const char*, const char*, const char*,
  168. const char*, const char*, const char*))>
  169. a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9");
  170. EXPECT_EQ("123456789", a.Perform(std::make_tuple(&Concat9)));
  171. }
  172. // Tests using InvokeArgument with a 10-ary function.
  173. TEST(InvokeArgumentTest, Function10) {
  174. Action<std::string(std::string(*)(
  175. const char*, const char*, const char*, const char*, const char*,
  176. const char*, const char*, const char*, const char*, const char*))>
  177. a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
  178. EXPECT_EQ("1234567890", a.Perform(std::make_tuple(&Concat10)));
  179. }
  180. // Tests using InvokeArgument with a function that takes a pointer argument.
  181. TEST(InvokeArgumentTest, ByPointerFunction) {
  182. Action<const char*(const char*(*)(const char* input, short n))> a = // NOLINT
  183. InvokeArgument<0>(static_cast<const char*>("Hi"), Short(1));
  184. EXPECT_STREQ("i", a.Perform(std::make_tuple(&Binary)));
  185. }
  186. // Tests using InvokeArgument with a function that takes a const char*
  187. // by passing it a C-string literal.
  188. TEST(InvokeArgumentTest, FunctionWithCStringLiteral) {
  189. Action<const char*(const char*(*)(const char* input, short n))> a = // NOLINT
  190. InvokeArgument<0>("Hi", Short(1));
  191. EXPECT_STREQ("i", a.Perform(std::make_tuple(&Binary)));
  192. }
  193. // Tests using InvokeArgument with a function that takes a const reference.
  194. TEST(InvokeArgumentTest, ByConstReferenceFunction) {
  195. Action<bool(bool (*function)(const std::string& s))> a = // NOLINT
  196. InvokeArgument<0>(std::string("Hi"));
  197. // When action 'a' is constructed, it makes a copy of the temporary
  198. // string object passed to it, so it's OK to use 'a' later, when the
  199. // temporary object has already died.
  200. EXPECT_TRUE(a.Perform(std::make_tuple(&ByConstRef)));
  201. }
  202. // Tests using InvokeArgument with ByRef() and a function that takes a
  203. // const reference.
  204. TEST(InvokeArgumentTest, ByExplicitConstReferenceFunction) {
  205. Action<bool(bool(*)(const double& x))> a = // NOLINT
  206. InvokeArgument<0>(ByRef(g_double));
  207. // The above line calls ByRef() on a const value.
  208. EXPECT_TRUE(a.Perform(std::make_tuple(&ReferencesGlobalDouble)));
  209. double x = 0;
  210. a = InvokeArgument<0>(ByRef(x)); // This calls ByRef() on a non-const.
  211. EXPECT_FALSE(a.Perform(std::make_tuple(&ReferencesGlobalDouble)));
  212. }
  213. // Tests DoAll(a1, a2).
  214. TEST(DoAllTest, TwoActions) {
  215. int n = 0;
  216. Action<int(int*)> a = DoAll(SetArgPointee<0>(1), // NOLINT
  217. Return(2));
  218. EXPECT_EQ(2, a.Perform(std::make_tuple(&n)));
  219. EXPECT_EQ(1, n);
  220. }
  221. // Tests DoAll(a1, a2, a3).
  222. TEST(DoAllTest, ThreeActions) {
  223. int m = 0, n = 0;
  224. Action<int(int*, int*)> a = DoAll(SetArgPointee<0>(1), // NOLINT
  225. SetArgPointee<1>(2),
  226. Return(3));
  227. EXPECT_EQ(3, a.Perform(std::make_tuple(&m, &n)));
  228. EXPECT_EQ(1, m);
  229. EXPECT_EQ(2, n);
  230. }
  231. // Tests DoAll(a1, a2, a3, a4).
  232. TEST(DoAllTest, FourActions) {
  233. int m = 0, n = 0;
  234. char ch = '\0';
  235. Action<int(int*, int*, char*)> a = // NOLINT
  236. DoAll(SetArgPointee<0>(1),
  237. SetArgPointee<1>(2),
  238. SetArgPointee<2>('a'),
  239. Return(3));
  240. EXPECT_EQ(3, a.Perform(std::make_tuple(&m, &n, &ch)));
  241. EXPECT_EQ(1, m);
  242. EXPECT_EQ(2, n);
  243. EXPECT_EQ('a', ch);
  244. }
  245. // Tests DoAll(a1, a2, a3, a4, a5).
  246. TEST(DoAllTest, FiveActions) {
  247. int m = 0, n = 0;
  248. char a = '\0', b = '\0';
  249. Action<int(int*, int*, char*, char*)> action = // NOLINT
  250. DoAll(SetArgPointee<0>(1),
  251. SetArgPointee<1>(2),
  252. SetArgPointee<2>('a'),
  253. SetArgPointee<3>('b'),
  254. Return(3));
  255. EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b)));
  256. EXPECT_EQ(1, m);
  257. EXPECT_EQ(2, n);
  258. EXPECT_EQ('a', a);
  259. EXPECT_EQ('b', b);
  260. }
  261. // Tests DoAll(a1, a2, ..., a6).
  262. TEST(DoAllTest, SixActions) {
  263. int m = 0, n = 0;
  264. char a = '\0', b = '\0', c = '\0';
  265. Action<int(int*, int*, char*, char*, char*)> action = // NOLINT
  266. DoAll(SetArgPointee<0>(1),
  267. SetArgPointee<1>(2),
  268. SetArgPointee<2>('a'),
  269. SetArgPointee<3>('b'),
  270. SetArgPointee<4>('c'),
  271. Return(3));
  272. EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c)));
  273. EXPECT_EQ(1, m);
  274. EXPECT_EQ(2, n);
  275. EXPECT_EQ('a', a);
  276. EXPECT_EQ('b', b);
  277. EXPECT_EQ('c', c);
  278. }
  279. // Tests DoAll(a1, a2, ..., a7).
  280. TEST(DoAllTest, SevenActions) {
  281. int m = 0, n = 0;
  282. char a = '\0', b = '\0', c = '\0', d = '\0';
  283. Action<int(int*, int*, char*, char*, char*, char*)> action = // NOLINT
  284. DoAll(SetArgPointee<0>(1),
  285. SetArgPointee<1>(2),
  286. SetArgPointee<2>('a'),
  287. SetArgPointee<3>('b'),
  288. SetArgPointee<4>('c'),
  289. SetArgPointee<5>('d'),
  290. Return(3));
  291. EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d)));
  292. EXPECT_EQ(1, m);
  293. EXPECT_EQ(2, n);
  294. EXPECT_EQ('a', a);
  295. EXPECT_EQ('b', b);
  296. EXPECT_EQ('c', c);
  297. EXPECT_EQ('d', d);
  298. }
  299. // Tests DoAll(a1, a2, ..., a8).
  300. TEST(DoAllTest, EightActions) {
  301. int m = 0, n = 0;
  302. char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0';
  303. Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
  304. char*)> action =
  305. DoAll(SetArgPointee<0>(1),
  306. SetArgPointee<1>(2),
  307. SetArgPointee<2>('a'),
  308. SetArgPointee<3>('b'),
  309. SetArgPointee<4>('c'),
  310. SetArgPointee<5>('d'),
  311. SetArgPointee<6>('e'),
  312. Return(3));
  313. EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d, &e)));
  314. EXPECT_EQ(1, m);
  315. EXPECT_EQ(2, n);
  316. EXPECT_EQ('a', a);
  317. EXPECT_EQ('b', b);
  318. EXPECT_EQ('c', c);
  319. EXPECT_EQ('d', d);
  320. EXPECT_EQ('e', e);
  321. }
  322. // Tests DoAll(a1, a2, ..., a9).
  323. TEST(DoAllTest, NineActions) {
  324. int m = 0, n = 0;
  325. char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0', f = '\0';
  326. Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
  327. char*, char*)> action =
  328. DoAll(SetArgPointee<0>(1),
  329. SetArgPointee<1>(2),
  330. SetArgPointee<2>('a'),
  331. SetArgPointee<3>('b'),
  332. SetArgPointee<4>('c'),
  333. SetArgPointee<5>('d'),
  334. SetArgPointee<6>('e'),
  335. SetArgPointee<7>('f'),
  336. Return(3));
  337. EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d, &e, &f)));
  338. EXPECT_EQ(1, m);
  339. EXPECT_EQ(2, n);
  340. EXPECT_EQ('a', a);
  341. EXPECT_EQ('b', b);
  342. EXPECT_EQ('c', c);
  343. EXPECT_EQ('d', d);
  344. EXPECT_EQ('e', e);
  345. EXPECT_EQ('f', f);
  346. }
  347. // Tests DoAll(a1, a2, ..., a10).
  348. TEST(DoAllTest, TenActions) {
  349. int m = 0, n = 0;
  350. char a = '\0', b = '\0', c = '\0', d = '\0';
  351. char e = '\0', f = '\0', g = '\0';
  352. Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
  353. char*, char*, char*)> action =
  354. DoAll(SetArgPointee<0>(1),
  355. SetArgPointee<1>(2),
  356. SetArgPointee<2>('a'),
  357. SetArgPointee<3>('b'),
  358. SetArgPointee<4>('c'),
  359. SetArgPointee<5>('d'),
  360. SetArgPointee<6>('e'),
  361. SetArgPointee<7>('f'),
  362. SetArgPointee<8>('g'),
  363. Return(3));
  364. EXPECT_EQ(
  365. 3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d, &e, &f, &g)));
  366. EXPECT_EQ(1, m);
  367. EXPECT_EQ(2, n);
  368. EXPECT_EQ('a', a);
  369. EXPECT_EQ('b', b);
  370. EXPECT_EQ('c', c);
  371. EXPECT_EQ('d', d);
  372. EXPECT_EQ('e', e);
  373. EXPECT_EQ('f', f);
  374. EXPECT_EQ('g', g);
  375. }
  376. // The ACTION*() macros trigger warning C4100 (unreferenced formal
  377. // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
  378. // the macro definition, as the warnings are generated when the macro
  379. // is expanded and macro expansion cannot contain #pragma. Therefore
  380. // we suppress them here.
  381. // Also suppress C4503 decorated name length exceeded, name was truncated
  382. #ifdef _MSC_VER
  383. # pragma warning(push)
  384. # pragma warning(disable:4100)
  385. # pragma warning(disable:4503)
  386. #endif
  387. // Tests the ACTION*() macro family.
  388. // Tests that ACTION() can define an action that doesn't reference the
  389. // mock function arguments.
  390. ACTION(Return5) { return 5; }
  391. TEST(ActionMacroTest, WorksWhenNotReferencingArguments) {
  392. Action<double()> a1 = Return5();
  393. EXPECT_DOUBLE_EQ(5, a1.Perform(std::make_tuple()));
  394. Action<int(double, bool)> a2 = Return5();
  395. EXPECT_EQ(5, a2.Perform(std::make_tuple(1, true)));
  396. }
  397. // Tests that ACTION() can define an action that returns void.
  398. ACTION(IncrementArg1) { (*arg1)++; }
  399. TEST(ActionMacroTest, WorksWhenReturningVoid) {
  400. Action<void(int, int*)> a1 = IncrementArg1();
  401. int n = 0;
  402. a1.Perform(std::make_tuple(5, &n));
  403. EXPECT_EQ(1, n);
  404. }
  405. // Tests that the body of ACTION() can reference the type of the
  406. // argument.
  407. ACTION(IncrementArg2) {
  408. StaticAssertTypeEq<int*, arg2_type>();
  409. arg2_type temp = arg2;
  410. (*temp)++;
  411. }
  412. TEST(ActionMacroTest, CanReferenceArgumentType) {
  413. Action<void(int, bool, int*)> a1 = IncrementArg2();
  414. int n = 0;
  415. a1.Perform(std::make_tuple(5, false, &n));
  416. EXPECT_EQ(1, n);
  417. }
  418. // Tests that the body of ACTION() can reference the argument tuple
  419. // via args_type and args.
  420. ACTION(Sum2) {
  421. StaticAssertTypeEq<std::tuple<int, char, int*>, args_type>();
  422. args_type args_copy = args;
  423. return std::get<0>(args_copy) + std::get<1>(args_copy);
  424. }
  425. TEST(ActionMacroTest, CanReferenceArgumentTuple) {
  426. Action<int(int, char, int*)> a1 = Sum2();
  427. int dummy = 0;
  428. EXPECT_EQ(11, a1.Perform(std::make_tuple(5, Char(6), &dummy)));
  429. }
  430. // Tests that the body of ACTION() can reference the mock function
  431. // type.
  432. int Dummy(bool flag) { return flag? 1 : 0; }
  433. ACTION(InvokeDummy) {
  434. StaticAssertTypeEq<int(bool), function_type>();
  435. function_type* fp = &Dummy;
  436. return (*fp)(true);
  437. }
  438. TEST(ActionMacroTest, CanReferenceMockFunctionType) {
  439. Action<int(bool)> a1 = InvokeDummy();
  440. EXPECT_EQ(1, a1.Perform(std::make_tuple(true)));
  441. EXPECT_EQ(1, a1.Perform(std::make_tuple(false)));
  442. }
  443. // Tests that the body of ACTION() can reference the mock function's
  444. // return type.
  445. ACTION(InvokeDummy2) {
  446. StaticAssertTypeEq<int, return_type>();
  447. return_type result = Dummy(true);
  448. return result;
  449. }
  450. TEST(ActionMacroTest, CanReferenceMockFunctionReturnType) {
  451. Action<int(bool)> a1 = InvokeDummy2();
  452. EXPECT_EQ(1, a1.Perform(std::make_tuple(true)));
  453. EXPECT_EQ(1, a1.Perform(std::make_tuple(false)));
  454. }
  455. // Tests that ACTION() works for arguments passed by const reference.
  456. ACTION(ReturnAddrOfConstBoolReferenceArg) {
  457. StaticAssertTypeEq<const bool&, arg1_type>();
  458. return &arg1;
  459. }
  460. TEST(ActionMacroTest, WorksForConstReferenceArg) {
  461. Action<const bool*(int, const bool&)> a = ReturnAddrOfConstBoolReferenceArg();
  462. const bool b = false;
  463. EXPECT_EQ(&b, a.Perform(std::tuple<int, const bool&>(0, b)));
  464. }
  465. // Tests that ACTION() works for arguments passed by non-const reference.
  466. ACTION(ReturnAddrOfIntReferenceArg) {
  467. StaticAssertTypeEq<int&, arg0_type>();
  468. return &arg0;
  469. }
  470. TEST(ActionMacroTest, WorksForNonConstReferenceArg) {
  471. Action<int*(int&, bool, int)> a = ReturnAddrOfIntReferenceArg();
  472. int n = 0;
  473. EXPECT_EQ(&n, a.Perform(std::tuple<int&, bool, int>(n, true, 1)));
  474. }
  475. // Tests that ACTION() can be used in a namespace.
  476. namespace action_test {
  477. ACTION(Sum) { return arg0 + arg1; }
  478. } // namespace action_test
  479. TEST(ActionMacroTest, WorksInNamespace) {
  480. Action<int(int, int)> a1 = action_test::Sum();
  481. EXPECT_EQ(3, a1.Perform(std::make_tuple(1, 2)));
  482. }
  483. // Tests that the same ACTION definition works for mock functions with
  484. // different argument numbers.
  485. ACTION(PlusTwo) { return arg0 + 2; }
  486. TEST(ActionMacroTest, WorksForDifferentArgumentNumbers) {
  487. Action<int(int)> a1 = PlusTwo();
  488. EXPECT_EQ(4, a1.Perform(std::make_tuple(2)));
  489. Action<double(float, void*)> a2 = PlusTwo();
  490. int dummy;
  491. EXPECT_DOUBLE_EQ(6, a2.Perform(std::make_tuple(4.0f, &dummy)));
  492. }
  493. // Tests that ACTION_P can define a parameterized action.
  494. ACTION_P(Plus, n) { return arg0 + n; }
  495. TEST(ActionPMacroTest, DefinesParameterizedAction) {
  496. Action<int(int m, bool t)> a1 = Plus(9);
  497. EXPECT_EQ(10, a1.Perform(std::make_tuple(1, true)));
  498. }
  499. // Tests that the body of ACTION_P can reference the argument types
  500. // and the parameter type.
  501. ACTION_P(TypedPlus, n) {
  502. arg0_type t1 = arg0;
  503. n_type t2 = n;
  504. return t1 + t2;
  505. }
  506. TEST(ActionPMacroTest, CanReferenceArgumentAndParameterTypes) {
  507. Action<int(char m, bool t)> a1 = TypedPlus(9);
  508. EXPECT_EQ(10, a1.Perform(std::make_tuple(Char(1), true)));
  509. }
  510. // Tests that a parameterized action can be used in any mock function
  511. // whose type is compatible.
  512. TEST(ActionPMacroTest, WorksInCompatibleMockFunction) {
  513. Action<std::string(const std::string& s)> a1 = Plus("tail");
  514. const std::string re = "re";
  515. std::tuple<const std::string> dummy = std::make_tuple(re);
  516. EXPECT_EQ("retail", a1.Perform(dummy));
  517. }
  518. // Tests that we can use ACTION*() to define actions overloaded on the
  519. // number of parameters.
  520. ACTION(OverloadedAction) { return arg0 ? arg1 : "hello"; }
  521. ACTION_P(OverloadedAction, default_value) {
  522. return arg0 ? arg1 : default_value;
  523. }
  524. ACTION_P2(OverloadedAction, true_value, false_value) {
  525. return arg0 ? true_value : false_value;
  526. }
  527. TEST(ActionMacroTest, CanDefineOverloadedActions) {
  528. typedef Action<const char*(bool, const char*)> MyAction;
  529. const MyAction a1 = OverloadedAction();
  530. EXPECT_STREQ("hello", a1.Perform(std::make_tuple(false, CharPtr("world"))));
  531. EXPECT_STREQ("world", a1.Perform(std::make_tuple(true, CharPtr("world"))));
  532. const MyAction a2 = OverloadedAction("hi");
  533. EXPECT_STREQ("hi", a2.Perform(std::make_tuple(false, CharPtr("world"))));
  534. EXPECT_STREQ("world", a2.Perform(std::make_tuple(true, CharPtr("world"))));
  535. const MyAction a3 = OverloadedAction("hi", "you");
  536. EXPECT_STREQ("hi", a3.Perform(std::make_tuple(true, CharPtr("world"))));
  537. EXPECT_STREQ("you", a3.Perform(std::make_tuple(false, CharPtr("world"))));
  538. }
  539. // Tests ACTION_Pn where n >= 3.
  540. ACTION_P3(Plus, m, n, k) { return arg0 + m + n + k; }
  541. TEST(ActionPnMacroTest, WorksFor3Parameters) {
  542. Action<double(int m, bool t)> a1 = Plus(100, 20, 3.4);
  543. EXPECT_DOUBLE_EQ(3123.4, a1.Perform(std::make_tuple(3000, true)));
  544. Action<std::string(const std::string& s)> a2 = Plus("tail", "-", ">");
  545. const std::string re = "re";
  546. std::tuple<const std::string> dummy = std::make_tuple(re);
  547. EXPECT_EQ("retail->", a2.Perform(dummy));
  548. }
  549. ACTION_P4(Plus, p0, p1, p2, p3) { return arg0 + p0 + p1 + p2 + p3; }
  550. TEST(ActionPnMacroTest, WorksFor4Parameters) {
  551. Action<int(int)> a1 = Plus(1, 2, 3, 4);
  552. EXPECT_EQ(10 + 1 + 2 + 3 + 4, a1.Perform(std::make_tuple(10)));
  553. }
  554. ACTION_P5(Plus, p0, p1, p2, p3, p4) { return arg0 + p0 + p1 + p2 + p3 + p4; }
  555. TEST(ActionPnMacroTest, WorksFor5Parameters) {
  556. Action<int(int)> a1 = Plus(1, 2, 3, 4, 5);
  557. EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5, a1.Perform(std::make_tuple(10)));
  558. }
  559. ACTION_P6(Plus, p0, p1, p2, p3, p4, p5) {
  560. return arg0 + p0 + p1 + p2 + p3 + p4 + p5;
  561. }
  562. TEST(ActionPnMacroTest, WorksFor6Parameters) {
  563. Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6);
  564. EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6, a1.Perform(std::make_tuple(10)));
  565. }
  566. ACTION_P7(Plus, p0, p1, p2, p3, p4, p5, p6) {
  567. return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6;
  568. }
  569. TEST(ActionPnMacroTest, WorksFor7Parameters) {
  570. Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7);
  571. EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7, a1.Perform(std::make_tuple(10)));
  572. }
  573. ACTION_P8(Plus, p0, p1, p2, p3, p4, p5, p6, p7) {
  574. return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7;
  575. }
  576. TEST(ActionPnMacroTest, WorksFor8Parameters) {
  577. Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8);
  578. EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
  579. a1.Perform(std::make_tuple(10)));
  580. }
  581. ACTION_P9(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8) {
  582. return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8;
  583. }
  584. TEST(ActionPnMacroTest, WorksFor9Parameters) {
  585. Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9);
  586. EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9,
  587. a1.Perform(std::make_tuple(10)));
  588. }
  589. ACTION_P10(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8, last_param) {
  590. arg0_type t0 = arg0;
  591. last_param_type t9 = last_param;
  592. return t0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + t9;
  593. }
  594. TEST(ActionPnMacroTest, WorksFor10Parameters) {
  595. Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
  596. EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10,
  597. a1.Perform(std::make_tuple(10)));
  598. }
  599. // Tests that the action body can promote the parameter types.
  600. ACTION_P2(PadArgument, prefix, suffix) {
  601. // The following lines promote the two parameters to desired types.
  602. std::string prefix_str(prefix);
  603. char suffix_char = static_cast<char>(suffix);
  604. return prefix_str + arg0 + suffix_char;
  605. }
  606. TEST(ActionPnMacroTest, SimpleTypePromotion) {
  607. Action<std::string(const char*)> no_promo =
  608. PadArgument(std::string("foo"), 'r');
  609. Action<std::string(const char*)> promo =
  610. PadArgument("foo", static_cast<int>('r'));
  611. EXPECT_EQ("foobar", no_promo.Perform(std::make_tuple(CharPtr("ba"))));
  612. EXPECT_EQ("foobar", promo.Perform(std::make_tuple(CharPtr("ba"))));
  613. }
  614. // Tests that we can partially restrict parameter types using a
  615. // straight-forward pattern.
  616. // Defines a generic action that doesn't restrict the types of its
  617. // parameters.
  618. ACTION_P3(ConcatImpl, a, b, c) {
  619. std::stringstream ss;
  620. ss << a << b << c;
  621. return ss.str();
  622. }
  623. // Next, we try to restrict that either the first parameter is a
  624. // string, or the second parameter is an int.
  625. // Defines a partially specialized wrapper that restricts the first
  626. // parameter to std::string.
  627. template <typename T1, typename T2>
  628. // ConcatImplActionP3 is the class template ACTION_P3 uses to
  629. // implement ConcatImpl. We shouldn't change the name as this
  630. // pattern requires the user to use it directly.
  631. ConcatImplActionP3<std::string, T1, T2>
  632. Concat(const std::string& a, T1 b, T2 c) {
  633. GTEST_INTENTIONAL_CONST_COND_PUSH_()
  634. if (true) {
  635. GTEST_INTENTIONAL_CONST_COND_POP_()
  636. // This branch verifies that ConcatImpl() can be invoked without
  637. // explicit template arguments.
  638. return ConcatImpl(a, b, c);
  639. } else {
  640. // This branch verifies that ConcatImpl() can also be invoked with
  641. // explicit template arguments. It doesn't really need to be
  642. // executed as this is a compile-time verification.
  643. return ConcatImpl<std::string, T1, T2>(a, b, c);
  644. }
  645. }
  646. // Defines another partially specialized wrapper that restricts the
  647. // second parameter to int.
  648. template <typename T1, typename T2>
  649. ConcatImplActionP3<T1, int, T2>
  650. Concat(T1 a, int b, T2 c) {
  651. return ConcatImpl(a, b, c);
  652. }
  653. TEST(ActionPnMacroTest, CanPartiallyRestrictParameterTypes) {
  654. Action<const std::string()> a1 = Concat("Hello", "1", 2);
  655. EXPECT_EQ("Hello12", a1.Perform(std::make_tuple()));
  656. a1 = Concat(1, 2, 3);
  657. EXPECT_EQ("123", a1.Perform(std::make_tuple()));
  658. }
  659. // Verifies the type of an ACTION*.
  660. ACTION(DoFoo) {}
  661. ACTION_P(DoFoo, p) {}
  662. ACTION_P2(DoFoo, p0, p1) {}
  663. TEST(ActionPnMacroTest, TypesAreCorrect) {
  664. // DoFoo() must be assignable to a DoFooAction variable.
  665. DoFooAction a0 = DoFoo();
  666. // DoFoo(1) must be assignable to a DoFooActionP variable.
  667. DoFooActionP<int> a1 = DoFoo(1);
  668. // DoFoo(p1, ..., pk) must be assignable to a DoFooActionPk
  669. // variable, and so on.
  670. DoFooActionP2<int, char> a2 = DoFoo(1, '2');
  671. PlusActionP3<int, int, char> a3 = Plus(1, 2, '3');
  672. PlusActionP4<int, int, int, char> a4 = Plus(1, 2, 3, '4');
  673. PlusActionP5<int, int, int, int, char> a5 = Plus(1, 2, 3, 4, '5');
  674. PlusActionP6<int, int, int, int, int, char> a6 = Plus(1, 2, 3, 4, 5, '6');
  675. PlusActionP7<int, int, int, int, int, int, char> a7 =
  676. Plus(1, 2, 3, 4, 5, 6, '7');
  677. PlusActionP8<int, int, int, int, int, int, int, char> a8 =
  678. Plus(1, 2, 3, 4, 5, 6, 7, '8');
  679. PlusActionP9<int, int, int, int, int, int, int, int, char> a9 =
  680. Plus(1, 2, 3, 4, 5, 6, 7, 8, '9');
  681. PlusActionP10<int, int, int, int, int, int, int, int, int, char> a10 =
  682. Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
  683. // Avoid "unused variable" warnings.
  684. (void)a0;
  685. (void)a1;
  686. (void)a2;
  687. (void)a3;
  688. (void)a4;
  689. (void)a5;
  690. (void)a6;
  691. (void)a7;
  692. (void)a8;
  693. (void)a9;
  694. (void)a10;
  695. }
  696. // Tests that an ACTION_P*() action can be explicitly instantiated
  697. // with reference-typed parameters.
  698. ACTION_P(Plus1, x) { return x; }
  699. ACTION_P2(Plus2, x, y) { return x + y; }
  700. ACTION_P3(Plus3, x, y, z) { return x + y + z; }
  701. ACTION_P10(Plus10, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
  702. return a0 + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9;
  703. }
  704. TEST(ActionPnMacroTest, CanExplicitlyInstantiateWithReferenceTypes) {
  705. int x = 1, y = 2, z = 3;
  706. const std::tuple<> empty = std::make_tuple();
  707. Action<int()> a = Plus1<int&>(x);
  708. EXPECT_EQ(1, a.Perform(empty));
  709. a = Plus2<const int&, int&>(x, y);
  710. EXPECT_EQ(3, a.Perform(empty));
  711. a = Plus3<int&, const int&, int&>(x, y, z);
  712. EXPECT_EQ(6, a.Perform(empty));
  713. int n[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  714. a = Plus10<const int&, int&, const int&, int&, const int&, int&, const int&,
  715. int&, const int&, int&>(n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7],
  716. n[8], n[9]);
  717. EXPECT_EQ(55, a.Perform(empty));
  718. }
  719. class NullaryConstructorClass {
  720. public:
  721. NullaryConstructorClass() : value_(123) {}
  722. int value_;
  723. };
  724. // Tests using ReturnNew() with a nullary constructor.
  725. TEST(ReturnNewTest, NoArgs) {
  726. Action<NullaryConstructorClass*()> a = ReturnNew<NullaryConstructorClass>();
  727. NullaryConstructorClass* c = a.Perform(std::make_tuple());
  728. EXPECT_EQ(123, c->value_);
  729. delete c;
  730. }
  731. class UnaryConstructorClass {
  732. public:
  733. explicit UnaryConstructorClass(int value) : value_(value) {}
  734. int value_;
  735. };
  736. // Tests using ReturnNew() with a unary constructor.
  737. TEST(ReturnNewTest, Unary) {
  738. Action<UnaryConstructorClass*()> a = ReturnNew<UnaryConstructorClass>(4000);
  739. UnaryConstructorClass* c = a.Perform(std::make_tuple());
  740. EXPECT_EQ(4000, c->value_);
  741. delete c;
  742. }
  743. TEST(ReturnNewTest, UnaryWorksWhenMockMethodHasArgs) {
  744. Action<UnaryConstructorClass*(bool, int)> a =
  745. ReturnNew<UnaryConstructorClass>(4000);
  746. UnaryConstructorClass* c = a.Perform(std::make_tuple(false, 5));
  747. EXPECT_EQ(4000, c->value_);
  748. delete c;
  749. }
  750. TEST(ReturnNewTest, UnaryWorksWhenMockMethodReturnsPointerToConst) {
  751. Action<const UnaryConstructorClass*()> a =
  752. ReturnNew<UnaryConstructorClass>(4000);
  753. const UnaryConstructorClass* c = a.Perform(std::make_tuple());
  754. EXPECT_EQ(4000, c->value_);
  755. delete c;
  756. }
  757. class TenArgConstructorClass {
  758. public:
  759. TenArgConstructorClass(int a1, int a2, int a3, int a4, int a5,
  760. int a6, int a7, int a8, int a9, int a10)
  761. : value_(a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10) {
  762. }
  763. int value_;
  764. };
  765. // Tests using ReturnNew() with a 10-argument constructor.
  766. TEST(ReturnNewTest, ConstructorThatTakes10Arguments) {
  767. Action<TenArgConstructorClass*()> a =
  768. ReturnNew<TenArgConstructorClass>(1000000000, 200000000, 30000000,
  769. 4000000, 500000, 60000,
  770. 7000, 800, 90, 0);
  771. TenArgConstructorClass* c = a.Perform(std::make_tuple());
  772. EXPECT_EQ(1234567890, c->value_);
  773. delete c;
  774. }
  775. // Tests that ACTION_TEMPLATE works when there is no value parameter.
  776. ACTION_TEMPLATE(CreateNew,
  777. HAS_1_TEMPLATE_PARAMS(typename, T),
  778. AND_0_VALUE_PARAMS()) {
  779. return new T;
  780. }
  781. TEST(ActionTemplateTest, WorksWithoutValueParam) {
  782. const Action<int*()> a = CreateNew<int>();
  783. int* p = a.Perform(std::make_tuple());
  784. delete p;
  785. }
  786. // Tests that ACTION_TEMPLATE works when there are value parameters.
  787. ACTION_TEMPLATE(CreateNew,
  788. HAS_1_TEMPLATE_PARAMS(typename, T),
  789. AND_1_VALUE_PARAMS(a0)) {
  790. return new T(a0);
  791. }
  792. TEST(ActionTemplateTest, WorksWithValueParams) {
  793. const Action<int*()> a = CreateNew<int>(42);
  794. int* p = a.Perform(std::make_tuple());
  795. EXPECT_EQ(42, *p);
  796. delete p;
  797. }
  798. // Tests that ACTION_TEMPLATE works for integral template parameters.
  799. ACTION_TEMPLATE(MyDeleteArg,
  800. HAS_1_TEMPLATE_PARAMS(int, k),
  801. AND_0_VALUE_PARAMS()) {
  802. delete std::get<k>(args);
  803. }
  804. // Resets a bool variable in the destructor.
  805. class BoolResetter {
  806. public:
  807. explicit BoolResetter(bool* value) : value_(value) {}
  808. ~BoolResetter() { *value_ = false; }
  809. private:
  810. bool* value_;
  811. };
  812. TEST(ActionTemplateTest, WorksForIntegralTemplateParams) {
  813. const Action<void(int*, BoolResetter*)> a = MyDeleteArg<1>();
  814. int n = 0;
  815. bool b = true;
  816. BoolResetter* resetter = new BoolResetter(&b);
  817. a.Perform(std::make_tuple(&n, resetter));
  818. EXPECT_FALSE(b); // Verifies that resetter is deleted.
  819. }
  820. // Tests that ACTION_TEMPLATES works for template template parameters.
  821. ACTION_TEMPLATE(ReturnSmartPointer,
  822. HAS_1_TEMPLATE_PARAMS(template <typename Pointee> class,
  823. Pointer),
  824. AND_1_VALUE_PARAMS(pointee)) {
  825. return Pointer<pointee_type>(new pointee_type(pointee));
  826. }
  827. TEST(ActionTemplateTest, WorksForTemplateTemplateParameters) {
  828. const Action<std::shared_ptr<int>()> a =
  829. ReturnSmartPointer<std::shared_ptr>(42);
  830. std::shared_ptr<int> p = a.Perform(std::make_tuple());
  831. EXPECT_EQ(42, *p);
  832. }
  833. // Tests that ACTION_TEMPLATE works for 10 template parameters.
  834. template <typename T1, typename T2, typename T3, int k4, bool k5,
  835. unsigned int k6, typename T7, typename T8, typename T9>
  836. struct GiantTemplate {
  837. public:
  838. explicit GiantTemplate(int a_value) : value(a_value) {}
  839. int value;
  840. };
  841. ACTION_TEMPLATE(ReturnGiant,
  842. HAS_10_TEMPLATE_PARAMS(
  843. typename, T1,
  844. typename, T2,
  845. typename, T3,
  846. int, k4,
  847. bool, k5,
  848. unsigned int, k6,
  849. class, T7,
  850. class, T8,
  851. class, T9,
  852. template <typename T> class, T10),
  853. AND_1_VALUE_PARAMS(value)) {
  854. return GiantTemplate<T10<T1>, T2, T3, k4, k5, k6, T7, T8, T9>(value);
  855. }
  856. TEST(ActionTemplateTest, WorksFor10TemplateParameters) {
  857. using Giant = GiantTemplate<std::shared_ptr<int>, bool, double, 5, true, 6,
  858. char, unsigned, int>;
  859. const Action<Giant()> a = ReturnGiant<int, bool, double, 5, true, 6, char,
  860. unsigned, int, std::shared_ptr>(42);
  861. Giant giant = a.Perform(std::make_tuple());
  862. EXPECT_EQ(42, giant.value);
  863. }
  864. // Tests that ACTION_TEMPLATE works for 10 value parameters.
  865. ACTION_TEMPLATE(ReturnSum,
  866. HAS_1_TEMPLATE_PARAMS(typename, Number),
  867. AND_10_VALUE_PARAMS(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10)) {
  868. return static_cast<Number>(v1) + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10;
  869. }
  870. TEST(ActionTemplateTest, WorksFor10ValueParameters) {
  871. const Action<int()> a = ReturnSum<int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
  872. EXPECT_EQ(55, a.Perform(std::make_tuple()));
  873. }
  874. // Tests that ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded
  875. // on the number of value parameters.
  876. ACTION(ReturnSum) { return 0; }
  877. ACTION_P(ReturnSum, x) { return x; }
  878. ACTION_TEMPLATE(ReturnSum,
  879. HAS_1_TEMPLATE_PARAMS(typename, Number),
  880. AND_2_VALUE_PARAMS(v1, v2)) {
  881. return static_cast<Number>(v1) + v2;
  882. }
  883. ACTION_TEMPLATE(ReturnSum,
  884. HAS_1_TEMPLATE_PARAMS(typename, Number),
  885. AND_3_VALUE_PARAMS(v1, v2, v3)) {
  886. return static_cast<Number>(v1) + v2 + v3;
  887. }
  888. ACTION_TEMPLATE(ReturnSum,
  889. HAS_2_TEMPLATE_PARAMS(typename, Number, int, k),
  890. AND_4_VALUE_PARAMS(v1, v2, v3, v4)) {
  891. return static_cast<Number>(v1) + v2 + v3 + v4 + k;
  892. }
  893. TEST(ActionTemplateTest, CanBeOverloadedOnNumberOfValueParameters) {
  894. const Action<int()> a0 = ReturnSum();
  895. const Action<int()> a1 = ReturnSum(1);
  896. const Action<int()> a2 = ReturnSum<int>(1, 2);
  897. const Action<int()> a3 = ReturnSum<int>(1, 2, 3);
  898. const Action<int()> a4 = ReturnSum<int, 10000>(2000, 300, 40, 5);
  899. EXPECT_EQ(0, a0.Perform(std::make_tuple()));
  900. EXPECT_EQ(1, a1.Perform(std::make_tuple()));
  901. EXPECT_EQ(3, a2.Perform(std::make_tuple()));
  902. EXPECT_EQ(6, a3.Perform(std::make_tuple()));
  903. EXPECT_EQ(12345, a4.Perform(std::make_tuple()));
  904. }
  905. } // namespace gmock_generated_actions_test
  906. } // namespace testing