gmock-generated-actions_test.cc 33 KB

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