gmock-generated-actions_test.cc 40 KB

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