gmock-more-actions_test.cc 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  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 in gmock-actions.h.
  32. #include "gmock/gmock-more-actions.h"
  33. #include <algorithm>
  34. #include <functional>
  35. #include <iterator>
  36. #include <memory>
  37. #include <sstream>
  38. #include <string>
  39. #include <tuple>
  40. #include <vector>
  41. #include "gmock/gmock.h"
  42. #include "gtest/gtest-spi.h"
  43. #include "gtest/gtest.h"
  44. GTEST_DISABLE_MSC_WARNINGS_PUSH_(4577)
  45. namespace testing {
  46. namespace gmock_more_actions_test {
  47. using ::std::plus;
  48. using ::std::string;
  49. using testing::Action;
  50. using testing::DeleteArg;
  51. using testing::Invoke;
  52. using testing::ReturnArg;
  53. using testing::ReturnPointee;
  54. using testing::SaveArg;
  55. using testing::SaveArgPointee;
  56. using testing::SetArgReferee;
  57. using testing::Unused;
  58. using testing::WithArg;
  59. using testing::WithoutArgs;
  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 Invoke() and etc.
  64. int Nullary() { return 1; }
  65. bool g_done = false;
  66. bool Unary(int x) { return x < 0; }
  67. bool ByConstRef(const std::string& s) { return s == "Hi"; }
  68. const double g_double = 0;
  69. bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
  70. struct UnaryFunctor {
  71. int operator()(bool x) { return x ? 1 : -1; }
  72. };
  73. struct UnaryMoveOnlyFunctor : UnaryFunctor {
  74. UnaryMoveOnlyFunctor() = default;
  75. UnaryMoveOnlyFunctor(const UnaryMoveOnlyFunctor&) = delete;
  76. UnaryMoveOnlyFunctor(UnaryMoveOnlyFunctor&&) = default;
  77. };
  78. struct OneShotUnaryFunctor {
  79. int operator()(bool x) && { return x ? 1 : -1; }
  80. };
  81. const char* Binary(const char* input, short n) { return input + n; } // NOLINT
  82. int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT
  83. int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
  84. int SumOfFirst2(int a, int b, Unused, Unused) { return a + b; }
  85. int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
  86. struct SumOf5Functor {
  87. int operator()(int a, int b, int c, int d, int e) {
  88. return a + b + c + d + e;
  89. }
  90. };
  91. int SumOf6(int a, int b, int c, int d, int e, int f) {
  92. return a + b + c + d + e + f;
  93. }
  94. struct SumOf6Functor {
  95. int operator()(int a, int b, int c, int d, int e, int f) {
  96. return a + b + c + d + e + f;
  97. }
  98. };
  99. std::string Concat7(const char* s1, const char* s2, const char* s3,
  100. const char* s4, const char* s5, const char* s6,
  101. const char* s7) {
  102. return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
  103. }
  104. std::string Concat8(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) {
  107. return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
  108. }
  109. std::string Concat9(const char* s1, const char* s2, const char* s3,
  110. const char* s4, const char* s5, const char* s6,
  111. const char* s7, const char* s8, const char* s9) {
  112. return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
  113. }
  114. std::string Concat10(const char* s1, const char* s2, const char* s3,
  115. const char* s4, const char* s5, const char* s6,
  116. const char* s7, const char* s8, const char* s9,
  117. const char* s10) {
  118. return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
  119. }
  120. class Foo {
  121. public:
  122. Foo() : value_(123) {}
  123. int Nullary() const { return value_; }
  124. short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT
  125. std::string Binary(const std::string& str, char c) const { return str + c; }
  126. int Ternary(int x, bool y, char z) { return value_ + x + y * z; }
  127. int SumOf4(int a, int b, int c, int d) const {
  128. return a + b + c + d + value_;
  129. }
  130. int SumOfLast2(Unused, Unused, int a, int b) const { return a + b; }
  131. int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
  132. int SumOf6(int a, int b, int c, int d, int e, int f) {
  133. return a + b + c + d + e + f;
  134. }
  135. std::string Concat7(const char* s1, const char* s2, const char* s3,
  136. const char* s4, const char* s5, const char* s6,
  137. const char* s7) {
  138. return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
  139. }
  140. std::string Concat8(const char* s1, const char* s2, const char* s3,
  141. const char* s4, const char* s5, const char* s6,
  142. const char* s7, const char* s8) {
  143. return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
  144. }
  145. std::string Concat9(const char* s1, const char* s2, const char* s3,
  146. const char* s4, const char* s5, const char* s6,
  147. const char* s7, const char* s8, const char* s9) {
  148. return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
  149. }
  150. std::string Concat10(const char* s1, const char* s2, const char* s3,
  151. const char* s4, const char* s5, const char* s6,
  152. const char* s7, const char* s8, const char* s9,
  153. const char* s10) {
  154. return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
  155. }
  156. private:
  157. int value_;
  158. };
  159. // Tests using Invoke() with a nullary function.
  160. TEST(InvokeTest, Nullary) {
  161. Action<int()> a = Invoke(Nullary); // NOLINT
  162. EXPECT_EQ(1, a.Perform(std::make_tuple()));
  163. }
  164. // Tests using Invoke() with a unary function.
  165. TEST(InvokeTest, Unary) {
  166. Action<bool(int)> a = Invoke(Unary); // NOLINT
  167. EXPECT_FALSE(a.Perform(std::make_tuple(1)));
  168. EXPECT_TRUE(a.Perform(std::make_tuple(-1)));
  169. }
  170. // Tests using Invoke() with a binary function.
  171. TEST(InvokeTest, Binary) {
  172. Action<const char*(const char*, short)> a = Invoke(Binary); // NOLINT
  173. const char* p = "Hello";
  174. EXPECT_EQ(p + 2, a.Perform(std::make_tuple(p, Short(2))));
  175. }
  176. // Tests using Invoke() with a ternary function.
  177. TEST(InvokeTest, Ternary) {
  178. Action<int(int, char, short)> a = Invoke(Ternary); // NOLINT
  179. EXPECT_EQ(6, a.Perform(std::make_tuple(1, '\2', Short(3))));
  180. }
  181. // Tests using Invoke() with a 4-argument function.
  182. TEST(InvokeTest, FunctionThatTakes4Arguments) {
  183. Action<int(int, int, int, int)> a = Invoke(SumOf4); // NOLINT
  184. EXPECT_EQ(1234, a.Perform(std::make_tuple(1000, 200, 30, 4)));
  185. }
  186. // Tests using Invoke() with a 5-argument function.
  187. TEST(InvokeTest, FunctionThatTakes5Arguments) {
  188. Action<int(int, int, int, int, int)> a = Invoke(SumOf5); // NOLINT
  189. EXPECT_EQ(12345, a.Perform(std::make_tuple(10000, 2000, 300, 40, 5)));
  190. }
  191. // Tests using Invoke() with a 6-argument function.
  192. TEST(InvokeTest, FunctionThatTakes6Arguments) {
  193. Action<int(int, int, int, int, int, int)> a = Invoke(SumOf6); // NOLINT
  194. EXPECT_EQ(123456,
  195. a.Perform(std::make_tuple(100000, 20000, 3000, 400, 50, 6)));
  196. }
  197. // A helper that turns the type of a C-string literal from const
  198. // char[N] to const char*.
  199. inline const char* CharPtr(const char* s) { return s; }
  200. // Tests using Invoke() with a 7-argument function.
  201. TEST(InvokeTest, FunctionThatTakes7Arguments) {
  202. Action<std::string(const char*, const char*, const char*, const char*,
  203. const char*, const char*, const char*)>
  204. a = Invoke(Concat7);
  205. EXPECT_EQ("1234567",
  206. a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
  207. CharPtr("4"), CharPtr("5"), CharPtr("6"),
  208. CharPtr("7"))));
  209. }
  210. // Tests using Invoke() with a 8-argument function.
  211. TEST(InvokeTest, FunctionThatTakes8Arguments) {
  212. Action<std::string(const char*, const char*, const char*, const char*,
  213. const char*, const char*, const char*, const char*)>
  214. a = Invoke(Concat8);
  215. EXPECT_EQ("12345678",
  216. a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
  217. CharPtr("4"), CharPtr("5"), CharPtr("6"),
  218. CharPtr("7"), CharPtr("8"))));
  219. }
  220. // Tests using Invoke() with a 9-argument function.
  221. TEST(InvokeTest, FunctionThatTakes9Arguments) {
  222. Action<std::string(const char*, const char*, const char*, const char*,
  223. const char*, const char*, const char*, const char*,
  224. const char*)>
  225. a = Invoke(Concat9);
  226. EXPECT_EQ("123456789", a.Perform(std::make_tuple(
  227. CharPtr("1"), CharPtr("2"), CharPtr("3"),
  228. CharPtr("4"), CharPtr("5"), CharPtr("6"),
  229. CharPtr("7"), CharPtr("8"), CharPtr("9"))));
  230. }
  231. // Tests using Invoke() with a 10-argument function.
  232. TEST(InvokeTest, FunctionThatTakes10Arguments) {
  233. Action<std::string(const char*, const char*, const char*, const char*,
  234. const char*, const char*, const char*, const char*,
  235. const char*, const char*)>
  236. a = Invoke(Concat10);
  237. EXPECT_EQ("1234567890",
  238. a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
  239. CharPtr("4"), CharPtr("5"), CharPtr("6"),
  240. CharPtr("7"), CharPtr("8"), CharPtr("9"),
  241. CharPtr("0"))));
  242. }
  243. // Tests using Invoke() with functions with parameters declared as Unused.
  244. TEST(InvokeTest, FunctionWithUnusedParameters) {
  245. Action<int(int, int, double, const std::string&)> a1 = Invoke(SumOfFirst2);
  246. std::tuple<int, int, double, std::string> dummy =
  247. std::make_tuple(10, 2, 5.6, std::string("hi"));
  248. EXPECT_EQ(12, a1.Perform(dummy));
  249. Action<int(int, int, bool, int*)> a2 = Invoke(SumOfFirst2);
  250. EXPECT_EQ(
  251. 23, a2.Perform(std::make_tuple(20, 3, true, static_cast<int*>(nullptr))));
  252. }
  253. // Tests using Invoke() with methods with parameters declared as Unused.
  254. TEST(InvokeTest, MethodWithUnusedParameters) {
  255. Foo foo;
  256. Action<int(std::string, bool, int, int)> a1 = Invoke(&foo, &Foo::SumOfLast2);
  257. EXPECT_EQ(12, a1.Perform(std::make_tuple(CharPtr("hi"), true, 10, 2)));
  258. Action<int(char, double, int, int)> a2 = Invoke(&foo, &Foo::SumOfLast2);
  259. EXPECT_EQ(23, a2.Perform(std::make_tuple('a', 2.5, 20, 3)));
  260. }
  261. // Tests using Invoke() with a functor.
  262. TEST(InvokeTest, Functor) {
  263. Action<long(long, int)> a = Invoke(plus<long>()); // NOLINT
  264. EXPECT_EQ(3L, a.Perform(std::make_tuple(1, 2)));
  265. }
  266. // Tests using Invoke(f) as an action of a compatible type.
  267. TEST(InvokeTest, FunctionWithCompatibleType) {
  268. Action<long(int, short, char, bool)> a = Invoke(SumOf4); // NOLINT
  269. EXPECT_EQ(4321, a.Perform(std::make_tuple(4000, Short(300), Char(20), true)));
  270. }
  271. // Tests using Invoke() with an object pointer and a method pointer.
  272. // Tests using Invoke() with a nullary method.
  273. TEST(InvokeMethodTest, Nullary) {
  274. Foo foo;
  275. Action<int()> a = Invoke(&foo, &Foo::Nullary); // NOLINT
  276. EXPECT_EQ(123, a.Perform(std::make_tuple()));
  277. }
  278. // Tests using Invoke() with a unary method.
  279. TEST(InvokeMethodTest, Unary) {
  280. Foo foo;
  281. Action<short(long)> a = Invoke(&foo, &Foo::Unary); // NOLINT
  282. EXPECT_EQ(4123, a.Perform(std::make_tuple(4000)));
  283. }
  284. // Tests using Invoke() with a binary method.
  285. TEST(InvokeMethodTest, Binary) {
  286. Foo foo;
  287. Action<std::string(const std::string&, char)> a = Invoke(&foo, &Foo::Binary);
  288. std::string s("Hell");
  289. std::tuple<std::string, char> dummy = std::make_tuple(s, 'o');
  290. EXPECT_EQ("Hello", a.Perform(dummy));
  291. }
  292. // Tests using Invoke() with a ternary method.
  293. TEST(InvokeMethodTest, Ternary) {
  294. Foo foo;
  295. Action<int(int, bool, char)> a = Invoke(&foo, &Foo::Ternary); // NOLINT
  296. EXPECT_EQ(1124, a.Perform(std::make_tuple(1000, true, Char(1))));
  297. }
  298. // Tests using Invoke() with a 4-argument method.
  299. TEST(InvokeMethodTest, MethodThatTakes4Arguments) {
  300. Foo foo;
  301. Action<int(int, int, int, int)> a = Invoke(&foo, &Foo::SumOf4); // NOLINT
  302. EXPECT_EQ(1357, a.Perform(std::make_tuple(1000, 200, 30, 4)));
  303. }
  304. // Tests using Invoke() with a 5-argument method.
  305. TEST(InvokeMethodTest, MethodThatTakes5Arguments) {
  306. Foo foo;
  307. Action<int(int, int, int, int, int)> a =
  308. Invoke(&foo, &Foo::SumOf5); // NOLINT
  309. EXPECT_EQ(12345, a.Perform(std::make_tuple(10000, 2000, 300, 40, 5)));
  310. }
  311. // Tests using Invoke() with a 6-argument method.
  312. TEST(InvokeMethodTest, MethodThatTakes6Arguments) {
  313. Foo foo;
  314. Action<int(int, int, int, int, int, int)> a = // NOLINT
  315. Invoke(&foo, &Foo::SumOf6);
  316. EXPECT_EQ(123456,
  317. a.Perform(std::make_tuple(100000, 20000, 3000, 400, 50, 6)));
  318. }
  319. // Tests using Invoke() with a 7-argument method.
  320. TEST(InvokeMethodTest, MethodThatTakes7Arguments) {
  321. Foo foo;
  322. Action<std::string(const char*, const char*, const char*, const char*,
  323. const char*, const char*, const char*)>
  324. a = Invoke(&foo, &Foo::Concat7);
  325. EXPECT_EQ("1234567",
  326. a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
  327. CharPtr("4"), CharPtr("5"), CharPtr("6"),
  328. CharPtr("7"))));
  329. }
  330. // Tests using Invoke() with a 8-argument method.
  331. TEST(InvokeMethodTest, MethodThatTakes8Arguments) {
  332. Foo foo;
  333. Action<std::string(const char*, const char*, const char*, const char*,
  334. const char*, const char*, const char*, const char*)>
  335. a = Invoke(&foo, &Foo::Concat8);
  336. EXPECT_EQ("12345678",
  337. a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
  338. CharPtr("4"), CharPtr("5"), CharPtr("6"),
  339. CharPtr("7"), CharPtr("8"))));
  340. }
  341. // Tests using Invoke() with a 9-argument method.
  342. TEST(InvokeMethodTest, MethodThatTakes9Arguments) {
  343. Foo foo;
  344. Action<std::string(const char*, const char*, const char*, const char*,
  345. const char*, const char*, const char*, const char*,
  346. const char*)>
  347. a = Invoke(&foo, &Foo::Concat9);
  348. EXPECT_EQ("123456789", a.Perform(std::make_tuple(
  349. CharPtr("1"), CharPtr("2"), CharPtr("3"),
  350. CharPtr("4"), CharPtr("5"), CharPtr("6"),
  351. CharPtr("7"), CharPtr("8"), CharPtr("9"))));
  352. }
  353. // Tests using Invoke() with a 10-argument method.
  354. TEST(InvokeMethodTest, MethodThatTakes10Arguments) {
  355. Foo foo;
  356. Action<std::string(const char*, const char*, const char*, const char*,
  357. const char*, const char*, const char*, const char*,
  358. const char*, const char*)>
  359. a = Invoke(&foo, &Foo::Concat10);
  360. EXPECT_EQ("1234567890",
  361. a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
  362. CharPtr("4"), CharPtr("5"), CharPtr("6"),
  363. CharPtr("7"), CharPtr("8"), CharPtr("9"),
  364. CharPtr("0"))));
  365. }
  366. // Tests using Invoke(f) as an action of a compatible type.
  367. TEST(InvokeMethodTest, MethodWithCompatibleType) {
  368. Foo foo;
  369. Action<long(int, short, char, bool)> a = // NOLINT
  370. Invoke(&foo, &Foo::SumOf4);
  371. EXPECT_EQ(4444, a.Perform(std::make_tuple(4000, Short(300), Char(20), true)));
  372. }
  373. // Tests using WithoutArgs with an action that takes no argument.
  374. TEST(WithoutArgsTest, NoArg) {
  375. Action<int(int n)> a = WithoutArgs(Invoke(Nullary)); // NOLINT
  376. EXPECT_EQ(1, a.Perform(std::make_tuple(2)));
  377. }
  378. // Tests using WithArg with an action that takes 1 argument.
  379. TEST(WithArgTest, OneArg) {
  380. Action<bool(double x, int n)> b = WithArg<1>(Invoke(Unary)); // NOLINT
  381. EXPECT_TRUE(b.Perform(std::make_tuple(1.5, -1)));
  382. EXPECT_FALSE(b.Perform(std::make_tuple(1.5, 1)));
  383. }
  384. TEST(ReturnArgActionTest, WorksForOneArgIntArg0) {
  385. const Action<int(int)> a = ReturnArg<0>();
  386. EXPECT_EQ(5, a.Perform(std::make_tuple(5)));
  387. }
  388. TEST(ReturnArgActionTest, WorksForMultiArgBoolArg0) {
  389. const Action<bool(bool, bool, bool)> a = ReturnArg<0>();
  390. EXPECT_TRUE(a.Perform(std::make_tuple(true, false, false)));
  391. }
  392. TEST(ReturnArgActionTest, WorksForMultiArgStringArg2) {
  393. const Action<std::string(int, int, std::string, int)> a = ReturnArg<2>();
  394. EXPECT_EQ("seven", a.Perform(std::make_tuple(5, 6, std::string("seven"), 8)));
  395. }
  396. TEST(ReturnArgActionTest, WorksForNonConstRefArg0) {
  397. const Action<std::string&(std::string&)> a = ReturnArg<0>();
  398. std::string s = "12345";
  399. EXPECT_EQ(&s, &a.Perform(std::forward_as_tuple(s)));
  400. }
  401. TEST(SaveArgActionTest, WorksForSameType) {
  402. int result = 0;
  403. const Action<void(int n)> a1 = SaveArg<0>(&result);
  404. a1.Perform(std::make_tuple(5));
  405. EXPECT_EQ(5, result);
  406. }
  407. TEST(SaveArgActionTest, WorksForCompatibleType) {
  408. int result = 0;
  409. const Action<void(bool, char)> a1 = SaveArg<1>(&result);
  410. a1.Perform(std::make_tuple(true, 'a'));
  411. EXPECT_EQ('a', result);
  412. }
  413. TEST(SaveArgPointeeActionTest, WorksForSameType) {
  414. int result = 0;
  415. const int value = 5;
  416. const Action<void(const int*)> a1 = SaveArgPointee<0>(&result);
  417. a1.Perform(std::make_tuple(&value));
  418. EXPECT_EQ(5, result);
  419. }
  420. TEST(SaveArgPointeeActionTest, WorksForCompatibleType) {
  421. int result = 0;
  422. char value = 'a';
  423. const Action<void(bool, char*)> a1 = SaveArgPointee<1>(&result);
  424. a1.Perform(std::make_tuple(true, &value));
  425. EXPECT_EQ('a', result);
  426. }
  427. TEST(SetArgRefereeActionTest, WorksForSameType) {
  428. int value = 0;
  429. const Action<void(int&)> a1 = SetArgReferee<0>(1);
  430. a1.Perform(std::tuple<int&>(value));
  431. EXPECT_EQ(1, value);
  432. }
  433. TEST(SetArgRefereeActionTest, WorksForCompatibleType) {
  434. int value = 0;
  435. const Action<void(int, int&)> a1 = SetArgReferee<1>('a');
  436. a1.Perform(std::tuple<int, int&>(0, value));
  437. EXPECT_EQ('a', value);
  438. }
  439. TEST(SetArgRefereeActionTest, WorksWithExtraArguments) {
  440. int value = 0;
  441. const Action<void(bool, int, int&, const char*)> a1 = SetArgReferee<2>('a');
  442. a1.Perform(std::tuple<bool, int, int&, const char*>(true, 0, value, "hi"));
  443. EXPECT_EQ('a', value);
  444. }
  445. // A class that can be used to verify that its destructor is called: it will set
  446. // the bool provided to the constructor to true when destroyed.
  447. class DeletionTester {
  448. public:
  449. explicit DeletionTester(bool* is_deleted) : is_deleted_(is_deleted) {
  450. // Make sure the bit is set to false.
  451. *is_deleted_ = false;
  452. }
  453. ~DeletionTester() { *is_deleted_ = true; }
  454. private:
  455. bool* is_deleted_;
  456. };
  457. TEST(DeleteArgActionTest, OneArg) {
  458. bool is_deleted = false;
  459. DeletionTester* t = new DeletionTester(&is_deleted);
  460. const Action<void(DeletionTester*)> a1 = DeleteArg<0>(); // NOLINT
  461. EXPECT_FALSE(is_deleted);
  462. a1.Perform(std::make_tuple(t));
  463. EXPECT_TRUE(is_deleted);
  464. }
  465. TEST(DeleteArgActionTest, TenArgs) {
  466. bool is_deleted = false;
  467. DeletionTester* t = new DeletionTester(&is_deleted);
  468. const Action<void(bool, int, int, const char*, bool, int, int, int, int,
  469. DeletionTester*)>
  470. a1 = DeleteArg<9>();
  471. EXPECT_FALSE(is_deleted);
  472. a1.Perform(std::make_tuple(true, 5, 6, CharPtr("hi"), false, 7, 8, 9, 10, t));
  473. EXPECT_TRUE(is_deleted);
  474. }
  475. #if GTEST_HAS_EXCEPTIONS
  476. TEST(ThrowActionTest, ThrowsGivenExceptionInVoidFunction) {
  477. const Action<void(int n)> a = Throw('a');
  478. EXPECT_THROW(a.Perform(std::make_tuple(0)), char);
  479. }
  480. class MyException {};
  481. TEST(ThrowActionTest, ThrowsGivenExceptionInNonVoidFunction) {
  482. const Action<double(char ch)> a = Throw(MyException());
  483. EXPECT_THROW(a.Perform(std::make_tuple('0')), MyException);
  484. }
  485. TEST(ThrowActionTest, ThrowsGivenExceptionInNullaryFunction) {
  486. const Action<double()> a = Throw(MyException());
  487. EXPECT_THROW(a.Perform(std::make_tuple()), MyException);
  488. }
  489. class Object {
  490. public:
  491. virtual ~Object() {}
  492. virtual void Func() {}
  493. };
  494. class MockObject : public Object {
  495. public:
  496. ~MockObject() override {}
  497. MOCK_METHOD(void, Func, (), (override));
  498. };
  499. TEST(ThrowActionTest, Times0) {
  500. EXPECT_NONFATAL_FAILURE(
  501. [] {
  502. try {
  503. MockObject m;
  504. ON_CALL(m, Func()).WillByDefault([] { throw "something"; });
  505. EXPECT_CALL(m, Func()).Times(0);
  506. m.Func();
  507. } catch (...) {
  508. // Exception is caught but Times(0) still triggers a failure.
  509. }
  510. }(),
  511. "");
  512. }
  513. #endif // GTEST_HAS_EXCEPTIONS
  514. // Tests that SetArrayArgument<N>(first, last) sets the elements of the array
  515. // pointed to by the N-th (0-based) argument to values in range [first, last).
  516. TEST(SetArrayArgumentTest, SetsTheNthArray) {
  517. using MyFunction = void(bool, int*, char*);
  518. int numbers[] = {1, 2, 3};
  519. Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers + 3);
  520. int n[4] = {};
  521. int* pn = n;
  522. char ch[4] = {};
  523. char* pch = ch;
  524. a.Perform(std::make_tuple(true, pn, pch));
  525. EXPECT_EQ(1, n[0]);
  526. EXPECT_EQ(2, n[1]);
  527. EXPECT_EQ(3, n[2]);
  528. EXPECT_EQ(0, n[3]);
  529. EXPECT_EQ('\0', ch[0]);
  530. EXPECT_EQ('\0', ch[1]);
  531. EXPECT_EQ('\0', ch[2]);
  532. EXPECT_EQ('\0', ch[3]);
  533. // Tests first and last are iterators.
  534. std::string letters = "abc";
  535. a = SetArrayArgument<2>(letters.begin(), letters.end());
  536. std::fill_n(n, 4, 0);
  537. std::fill_n(ch, 4, '\0');
  538. a.Perform(std::make_tuple(true, pn, pch));
  539. EXPECT_EQ(0, n[0]);
  540. EXPECT_EQ(0, n[1]);
  541. EXPECT_EQ(0, n[2]);
  542. EXPECT_EQ(0, n[3]);
  543. EXPECT_EQ('a', ch[0]);
  544. EXPECT_EQ('b', ch[1]);
  545. EXPECT_EQ('c', ch[2]);
  546. EXPECT_EQ('\0', ch[3]);
  547. }
  548. // Tests SetArrayArgument<N>(first, last) where first == last.
  549. TEST(SetArrayArgumentTest, SetsTheNthArrayWithEmptyRange) {
  550. using MyFunction = void(bool, int*);
  551. int numbers[] = {1, 2, 3};
  552. Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers);
  553. int n[4] = {};
  554. int* pn = n;
  555. a.Perform(std::make_tuple(true, pn));
  556. EXPECT_EQ(0, n[0]);
  557. EXPECT_EQ(0, n[1]);
  558. EXPECT_EQ(0, n[2]);
  559. EXPECT_EQ(0, n[3]);
  560. }
  561. // Tests SetArrayArgument<N>(first, last) where *first is convertible
  562. // (but not equal) to the argument type.
  563. TEST(SetArrayArgumentTest, SetsTheNthArrayWithConvertibleType) {
  564. using MyFunction = void(bool, int*);
  565. char chars[] = {97, 98, 99};
  566. Action<MyFunction> a = SetArrayArgument<1>(chars, chars + 3);
  567. int codes[4] = {111, 222, 333, 444};
  568. int* pcodes = codes;
  569. a.Perform(std::make_tuple(true, pcodes));
  570. EXPECT_EQ(97, codes[0]);
  571. EXPECT_EQ(98, codes[1]);
  572. EXPECT_EQ(99, codes[2]);
  573. EXPECT_EQ(444, codes[3]);
  574. }
  575. // Test SetArrayArgument<N>(first, last) with iterator as argument.
  576. TEST(SetArrayArgumentTest, SetsTheNthArrayWithIteratorArgument) {
  577. using MyFunction = void(bool, std::back_insert_iterator<std::string>);
  578. std::string letters = "abc";
  579. Action<MyFunction> a = SetArrayArgument<1>(letters.begin(), letters.end());
  580. std::string s;
  581. a.Perform(std::make_tuple(true, std::back_inserter(s)));
  582. EXPECT_EQ(letters, s);
  583. }
  584. TEST(ReturnPointeeTest, Works) {
  585. int n = 42;
  586. const Action<int()> a = ReturnPointee(&n);
  587. EXPECT_EQ(42, a.Perform(std::make_tuple()));
  588. n = 43;
  589. EXPECT_EQ(43, a.Perform(std::make_tuple()));
  590. }
  591. // Tests InvokeArgument<N>(...).
  592. // Tests using InvokeArgument with a nullary function.
  593. TEST(InvokeArgumentTest, Function0) {
  594. Action<int(int, int (*)())> a = InvokeArgument<1>(); // NOLINT
  595. EXPECT_EQ(1, a.Perform(std::make_tuple(2, &Nullary)));
  596. }
  597. // Tests using InvokeArgument with a unary functor.
  598. TEST(InvokeArgumentTest, Functor1) {
  599. Action<int(UnaryFunctor)> a = InvokeArgument<0>(true); // NOLINT
  600. EXPECT_EQ(1, a.Perform(std::make_tuple(UnaryFunctor())));
  601. }
  602. // Tests using InvokeArgument with a unary move-only functor.
  603. TEST(InvokeArgumentTest, Functor1MoveOnly) {
  604. Action<int(UnaryMoveOnlyFunctor)> a = InvokeArgument<0>(true); // NOLINT
  605. EXPECT_EQ(1, a.Perform(std::make_tuple(UnaryMoveOnlyFunctor())));
  606. }
  607. // Tests using InvokeArgument with a one-shot unary functor.
  608. TEST(InvokeArgumentTest, OneShotFunctor1) {
  609. Action<int(OneShotUnaryFunctor)> a = InvokeArgument<0>(true); // NOLINT
  610. EXPECT_EQ(1, a.Perform(std::make_tuple(OneShotUnaryFunctor())));
  611. }
  612. // Tests using InvokeArgument with a 5-ary function.
  613. TEST(InvokeArgumentTest, Function5) {
  614. Action<int(int (*)(int, int, int, int, int))> a = // NOLINT
  615. InvokeArgument<0>(10000, 2000, 300, 40, 5);
  616. EXPECT_EQ(12345, a.Perform(std::make_tuple(&SumOf5)));
  617. }
  618. // Tests using InvokeArgument with a 5-ary functor.
  619. TEST(InvokeArgumentTest, Functor5) {
  620. Action<int(SumOf5Functor)> a = // NOLINT
  621. InvokeArgument<0>(10000, 2000, 300, 40, 5);
  622. EXPECT_EQ(12345, a.Perform(std::make_tuple(SumOf5Functor())));
  623. }
  624. // Tests using InvokeArgument with a 6-ary function.
  625. TEST(InvokeArgumentTest, Function6) {
  626. Action<int(int (*)(int, int, int, int, int, int))> a = // NOLINT
  627. InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
  628. EXPECT_EQ(123456, a.Perform(std::make_tuple(&SumOf6)));
  629. }
  630. // Tests using InvokeArgument with a 6-ary functor.
  631. TEST(InvokeArgumentTest, Functor6) {
  632. Action<int(SumOf6Functor)> a = // NOLINT
  633. InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
  634. EXPECT_EQ(123456, a.Perform(std::make_tuple(SumOf6Functor())));
  635. }
  636. // Tests using InvokeArgument with a 7-ary function.
  637. TEST(InvokeArgumentTest, Function7) {
  638. Action<std::string(std::string(*)(const char*, const char*, const char*,
  639. const char*, const char*, const char*,
  640. const char*))>
  641. a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7");
  642. EXPECT_EQ("1234567", a.Perform(std::make_tuple(&Concat7)));
  643. }
  644. // Tests using InvokeArgument with a 8-ary function.
  645. TEST(InvokeArgumentTest, Function8) {
  646. Action<std::string(std::string(*)(const char*, const char*, const char*,
  647. const char*, const char*, const char*,
  648. const char*, const char*))>
  649. a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8");
  650. EXPECT_EQ("12345678", a.Perform(std::make_tuple(&Concat8)));
  651. }
  652. // Tests using InvokeArgument with a 9-ary function.
  653. TEST(InvokeArgumentTest, Function9) {
  654. Action<std::string(std::string(*)(const char*, const char*, const char*,
  655. const char*, const char*, const char*,
  656. const char*, const char*, const char*))>
  657. a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9");
  658. EXPECT_EQ("123456789", a.Perform(std::make_tuple(&Concat9)));
  659. }
  660. // Tests using InvokeArgument with a 10-ary function.
  661. TEST(InvokeArgumentTest, Function10) {
  662. Action<std::string(std::string(*)(
  663. const char*, const char*, const char*, const char*, const char*,
  664. const char*, const char*, const char*, const char*, const char*))>
  665. a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
  666. EXPECT_EQ("1234567890", a.Perform(std::make_tuple(&Concat10)));
  667. }
  668. // Tests using InvokeArgument with a function that takes a pointer argument.
  669. TEST(InvokeArgumentTest, ByPointerFunction) {
  670. Action<const char*(const char* (*)(const char* input, short n))> // NOLINT
  671. a = InvokeArgument<0>(static_cast<const char*>("Hi"), Short(1));
  672. EXPECT_STREQ("i", a.Perform(std::make_tuple(&Binary)));
  673. }
  674. // Tests using InvokeArgument with a function that takes a const char*
  675. // by passing it a C-string literal.
  676. TEST(InvokeArgumentTest, FunctionWithCStringLiteral) {
  677. Action<const char*(const char* (*)(const char* input, short n))> // NOLINT
  678. a = InvokeArgument<0>("Hi", Short(1));
  679. EXPECT_STREQ("i", a.Perform(std::make_tuple(&Binary)));
  680. }
  681. // Tests using InvokeArgument with a function that takes a const reference.
  682. TEST(InvokeArgumentTest, ByConstReferenceFunction) {
  683. Action<bool(bool (*function)(const std::string& s))> a = // NOLINT
  684. InvokeArgument<0>(std::string("Hi"));
  685. // When action 'a' is constructed, it makes a copy of the temporary
  686. // string object passed to it, so it's OK to use 'a' later, when the
  687. // temporary object has already died.
  688. EXPECT_TRUE(a.Perform(std::make_tuple(&ByConstRef)));
  689. }
  690. // Tests using InvokeArgument with ByRef() and a function that takes a
  691. // const reference.
  692. TEST(InvokeArgumentTest, ByExplicitConstReferenceFunction) {
  693. Action<bool(bool (*)(const double& x))> a = // NOLINT
  694. InvokeArgument<0>(ByRef(g_double));
  695. // The above line calls ByRef() on a const value.
  696. EXPECT_TRUE(a.Perform(std::make_tuple(&ReferencesGlobalDouble)));
  697. double x = 0;
  698. a = InvokeArgument<0>(ByRef(x)); // This calls ByRef() on a non-const.
  699. EXPECT_FALSE(a.Perform(std::make_tuple(&ReferencesGlobalDouble)));
  700. }
  701. TEST(InvokeArgumentTest, MoveOnlyType) {
  702. struct Marker {};
  703. struct {
  704. // Method takes a unique_ptr (to a type we don't care about), and an
  705. // invocable type.
  706. MOCK_METHOD(bool, MockMethod,
  707. (std::unique_ptr<Marker>, std::function<int()>), ());
  708. } mock;
  709. ON_CALL(mock, MockMethod(_, _)).WillByDefault(InvokeArgument<1>());
  710. // This compiles, but is a little opaque as a workaround:
  711. ON_CALL(mock, MockMethod(_, _))
  712. .WillByDefault(WithArg<1>(InvokeArgument<0>()));
  713. }
  714. // Tests DoAll(a1, a2).
  715. TEST(DoAllTest, TwoActions) {
  716. int n = 0;
  717. Action<int(int*)> a = DoAll(SetArgPointee<0>(1), // NOLINT
  718. Return(2));
  719. EXPECT_EQ(2, a.Perform(std::make_tuple(&n)));
  720. EXPECT_EQ(1, n);
  721. }
  722. // Tests DoAll(a1, a2, a3).
  723. TEST(DoAllTest, ThreeActions) {
  724. int m = 0, n = 0;
  725. Action<int(int*, int*)> a = DoAll(SetArgPointee<0>(1), // NOLINT
  726. SetArgPointee<1>(2), Return(3));
  727. EXPECT_EQ(3, a.Perform(std::make_tuple(&m, &n)));
  728. EXPECT_EQ(1, m);
  729. EXPECT_EQ(2, n);
  730. }
  731. // Tests DoAll(a1, a2, a3, a4).
  732. TEST(DoAllTest, FourActions) {
  733. int m = 0, n = 0;
  734. char ch = '\0';
  735. Action<int(int*, int*, char*)> a = // NOLINT
  736. DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2), SetArgPointee<2>('a'),
  737. Return(3));
  738. EXPECT_EQ(3, a.Perform(std::make_tuple(&m, &n, &ch)));
  739. EXPECT_EQ(1, m);
  740. EXPECT_EQ(2, n);
  741. EXPECT_EQ('a', ch);
  742. }
  743. // Tests DoAll(a1, a2, a3, a4, a5).
  744. TEST(DoAllTest, FiveActions) {
  745. int m = 0, n = 0;
  746. char a = '\0', b = '\0';
  747. Action<int(int*, int*, char*, char*)> action = // NOLINT
  748. DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2), SetArgPointee<2>('a'),
  749. SetArgPointee<3>('b'), Return(3));
  750. EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b)));
  751. EXPECT_EQ(1, m);
  752. EXPECT_EQ(2, n);
  753. EXPECT_EQ('a', a);
  754. EXPECT_EQ('b', b);
  755. }
  756. // Tests DoAll(a1, a2, ..., a6).
  757. TEST(DoAllTest, SixActions) {
  758. int m = 0, n = 0;
  759. char a = '\0', b = '\0', c = '\0';
  760. Action<int(int*, int*, char*, char*, char*)> action = // NOLINT
  761. DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2), SetArgPointee<2>('a'),
  762. SetArgPointee<3>('b'), SetArgPointee<4>('c'), Return(3));
  763. EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c)));
  764. EXPECT_EQ(1, m);
  765. EXPECT_EQ(2, n);
  766. EXPECT_EQ('a', a);
  767. EXPECT_EQ('b', b);
  768. EXPECT_EQ('c', c);
  769. }
  770. // Tests DoAll(a1, a2, ..., a7).
  771. TEST(DoAllTest, SevenActions) {
  772. int m = 0, n = 0;
  773. char a = '\0', b = '\0', c = '\0', d = '\0';
  774. Action<int(int*, int*, char*, char*, char*, char*)> action = // NOLINT
  775. DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2), SetArgPointee<2>('a'),
  776. SetArgPointee<3>('b'), SetArgPointee<4>('c'), SetArgPointee<5>('d'),
  777. Return(3));
  778. EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d)));
  779. EXPECT_EQ(1, m);
  780. EXPECT_EQ(2, n);
  781. EXPECT_EQ('a', a);
  782. EXPECT_EQ('b', b);
  783. EXPECT_EQ('c', c);
  784. EXPECT_EQ('d', d);
  785. }
  786. // Tests DoAll(a1, a2, ..., a8).
  787. TEST(DoAllTest, EightActions) {
  788. int m = 0, n = 0;
  789. char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0';
  790. Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
  791. char*)>
  792. action =
  793. DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2), SetArgPointee<2>('a'),
  794. SetArgPointee<3>('b'), SetArgPointee<4>('c'),
  795. SetArgPointee<5>('d'), SetArgPointee<6>('e'), Return(3));
  796. EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d, &e)));
  797. EXPECT_EQ(1, m);
  798. EXPECT_EQ(2, n);
  799. EXPECT_EQ('a', a);
  800. EXPECT_EQ('b', b);
  801. EXPECT_EQ('c', c);
  802. EXPECT_EQ('d', d);
  803. EXPECT_EQ('e', e);
  804. }
  805. // Tests DoAll(a1, a2, ..., a9).
  806. TEST(DoAllTest, NineActions) {
  807. int m = 0, n = 0;
  808. char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0', f = '\0';
  809. Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
  810. char*, char*)>
  811. action = DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2),
  812. SetArgPointee<2>('a'), SetArgPointee<3>('b'),
  813. SetArgPointee<4>('c'), SetArgPointee<5>('d'),
  814. SetArgPointee<6>('e'), SetArgPointee<7>('f'), Return(3));
  815. EXPECT_EQ(3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d, &e, &f)));
  816. EXPECT_EQ(1, m);
  817. EXPECT_EQ(2, n);
  818. EXPECT_EQ('a', a);
  819. EXPECT_EQ('b', b);
  820. EXPECT_EQ('c', c);
  821. EXPECT_EQ('d', d);
  822. EXPECT_EQ('e', e);
  823. EXPECT_EQ('f', f);
  824. }
  825. // Tests DoAll(a1, a2, ..., a10).
  826. TEST(DoAllTest, TenActions) {
  827. int m = 0, n = 0;
  828. char a = '\0', b = '\0', c = '\0', d = '\0';
  829. char e = '\0', f = '\0', g = '\0';
  830. Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
  831. char*, char*, char*)>
  832. action =
  833. DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2), SetArgPointee<2>('a'),
  834. SetArgPointee<3>('b'), SetArgPointee<4>('c'),
  835. SetArgPointee<5>('d'), SetArgPointee<6>('e'),
  836. SetArgPointee<7>('f'), SetArgPointee<8>('g'), Return(3));
  837. EXPECT_EQ(
  838. 3, action.Perform(std::make_tuple(&m, &n, &a, &b, &c, &d, &e, &f, &g)));
  839. EXPECT_EQ(1, m);
  840. EXPECT_EQ(2, n);
  841. EXPECT_EQ('a', a);
  842. EXPECT_EQ('b', b);
  843. EXPECT_EQ('c', c);
  844. EXPECT_EQ('d', d);
  845. EXPECT_EQ('e', e);
  846. EXPECT_EQ('f', f);
  847. EXPECT_EQ('g', g);
  848. }
  849. TEST(DoAllTest, NoArgs) {
  850. bool ran_first = false;
  851. Action<bool()> a =
  852. DoAll([&] { ran_first = true; }, [&] { return ran_first; });
  853. EXPECT_TRUE(a.Perform({}));
  854. }
  855. TEST(DoAllTest, MoveOnlyArgs) {
  856. bool ran_first = false;
  857. Action<int(std::unique_ptr<int>)> a =
  858. DoAll(InvokeWithoutArgs([&] { ran_first = true; }),
  859. [](std::unique_ptr<int> p) { return *p; });
  860. EXPECT_EQ(7, a.Perform(std::make_tuple(std::unique_ptr<int>(new int(7)))));
  861. EXPECT_TRUE(ran_first);
  862. }
  863. TEST(DoAllTest, ImplicitlyConvertsActionArguments) {
  864. bool ran_first = false;
  865. // Action<void(std::vector<int>)> isn't an
  866. // Action<void(const std::vector<int>&) but can be converted.
  867. Action<void(std::vector<int>)> first = [&] { ran_first = true; };
  868. Action<int(std::vector<int>)> a =
  869. DoAll(first, [](std::vector<int> arg) { return arg.front(); });
  870. EXPECT_EQ(7, a.Perform(std::make_tuple(std::vector<int>{7})));
  871. EXPECT_TRUE(ran_first);
  872. }
  873. // The ACTION*() macros trigger warning C4100 (unreferenced formal
  874. // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
  875. // the macro definition, as the warnings are generated when the macro
  876. // is expanded and macro expansion cannot contain #pragma. Therefore
  877. // we suppress them here.
  878. // Also suppress C4503 decorated name length exceeded, name was truncated
  879. GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100 4503)
  880. // Tests the ACTION*() macro family.
  881. // Tests that ACTION() can define an action that doesn't reference the
  882. // mock function arguments.
  883. ACTION(Return5) { return 5; }
  884. TEST(ActionMacroTest, WorksWhenNotReferencingArguments) {
  885. Action<double()> a1 = Return5();
  886. EXPECT_DOUBLE_EQ(5, a1.Perform(std::make_tuple()));
  887. Action<int(double, bool)> a2 = Return5();
  888. EXPECT_EQ(5, a2.Perform(std::make_tuple(1, true)));
  889. }
  890. // Tests that ACTION() can define an action that returns void.
  891. ACTION(IncrementArg1) { (*arg1)++; }
  892. TEST(ActionMacroTest, WorksWhenReturningVoid) {
  893. Action<void(int, int*)> a1 = IncrementArg1();
  894. int n = 0;
  895. a1.Perform(std::make_tuple(5, &n));
  896. EXPECT_EQ(1, n);
  897. }
  898. // Tests that the body of ACTION() can reference the type of the
  899. // argument.
  900. ACTION(IncrementArg2) {
  901. StaticAssertTypeEq<int*, arg2_type>();
  902. arg2_type temp = arg2;
  903. (*temp)++;
  904. }
  905. TEST(ActionMacroTest, CanReferenceArgumentType) {
  906. Action<void(int, bool, int*)> a1 = IncrementArg2();
  907. int n = 0;
  908. a1.Perform(std::make_tuple(5, false, &n));
  909. EXPECT_EQ(1, n);
  910. }
  911. // Tests that the body of ACTION() can reference the argument tuple
  912. // via args_type and args.
  913. ACTION(Sum2) {
  914. StaticAssertTypeEq<std::tuple<int, char, int*>, args_type>();
  915. args_type args_copy = args;
  916. return std::get<0>(args_copy) + std::get<1>(args_copy);
  917. }
  918. TEST(ActionMacroTest, CanReferenceArgumentTuple) {
  919. Action<int(int, char, int*)> a1 = Sum2();
  920. int dummy = 0;
  921. EXPECT_EQ(11, a1.Perform(std::make_tuple(5, Char(6), &dummy)));
  922. }
  923. namespace {
  924. // Tests that the body of ACTION() can reference the mock function
  925. // type.
  926. int Dummy(bool flag) { return flag ? 1 : 0; }
  927. } // namespace
  928. ACTION(InvokeDummy) {
  929. StaticAssertTypeEq<int(bool), function_type>();
  930. function_type* fp = &Dummy;
  931. return (*fp)(true);
  932. }
  933. TEST(ActionMacroTest, CanReferenceMockFunctionType) {
  934. Action<int(bool)> a1 = InvokeDummy();
  935. EXPECT_EQ(1, a1.Perform(std::make_tuple(true)));
  936. EXPECT_EQ(1, a1.Perform(std::make_tuple(false)));
  937. }
  938. // Tests that the body of ACTION() can reference the mock function's
  939. // return type.
  940. ACTION(InvokeDummy2) {
  941. StaticAssertTypeEq<int, return_type>();
  942. return_type result = Dummy(true);
  943. return result;
  944. }
  945. TEST(ActionMacroTest, CanReferenceMockFunctionReturnType) {
  946. Action<int(bool)> a1 = InvokeDummy2();
  947. EXPECT_EQ(1, a1.Perform(std::make_tuple(true)));
  948. EXPECT_EQ(1, a1.Perform(std::make_tuple(false)));
  949. }
  950. // Tests that ACTION() works for arguments passed by const reference.
  951. ACTION(ReturnAddrOfConstBoolReferenceArg) {
  952. StaticAssertTypeEq<const bool&, arg1_type>();
  953. return &arg1;
  954. }
  955. TEST(ActionMacroTest, WorksForConstReferenceArg) {
  956. Action<const bool*(int, const bool&)> a = ReturnAddrOfConstBoolReferenceArg();
  957. const bool b = false;
  958. EXPECT_EQ(&b, a.Perform(std::tuple<int, const bool&>(0, b)));
  959. }
  960. // Tests that ACTION() works for arguments passed by non-const reference.
  961. ACTION(ReturnAddrOfIntReferenceArg) {
  962. StaticAssertTypeEq<int&, arg0_type>();
  963. return &arg0;
  964. }
  965. TEST(ActionMacroTest, WorksForNonConstReferenceArg) {
  966. Action<int*(int&, bool, int)> a = ReturnAddrOfIntReferenceArg();
  967. int n = 0;
  968. EXPECT_EQ(&n, a.Perform(std::tuple<int&, bool, int>(n, true, 1)));
  969. }
  970. // Tests that ACTION() can be used in a namespace.
  971. namespace action_test {
  972. ACTION(Sum) { return arg0 + arg1; }
  973. } // namespace action_test
  974. TEST(ActionMacroTest, WorksInNamespace) {
  975. Action<int(int, int)> a1 = action_test::Sum();
  976. EXPECT_EQ(3, a1.Perform(std::make_tuple(1, 2)));
  977. }
  978. // Tests that the same ACTION definition works for mock functions with
  979. // different argument numbers.
  980. ACTION(PlusTwo) { return arg0 + 2; }
  981. TEST(ActionMacroTest, WorksForDifferentArgumentNumbers) {
  982. Action<int(int)> a1 = PlusTwo();
  983. EXPECT_EQ(4, a1.Perform(std::make_tuple(2)));
  984. Action<double(float, void*)> a2 = PlusTwo();
  985. int dummy;
  986. EXPECT_DOUBLE_EQ(6, a2.Perform(std::make_tuple(4.0f, &dummy)));
  987. }
  988. // Tests that ACTION_P can define a parameterized action.
  989. ACTION_P(Plus, n) { return arg0 + n; }
  990. TEST(ActionPMacroTest, DefinesParameterizedAction) {
  991. Action<int(int m, bool t)> a1 = Plus(9);
  992. EXPECT_EQ(10, a1.Perform(std::make_tuple(1, true)));
  993. }
  994. // Tests that the body of ACTION_P can reference the argument types
  995. // and the parameter type.
  996. ACTION_P(TypedPlus, n) {
  997. arg0_type t1 = arg0;
  998. n_type t2 = n;
  999. return t1 + t2;
  1000. }
  1001. TEST(ActionPMacroTest, CanReferenceArgumentAndParameterTypes) {
  1002. Action<int(char m, bool t)> a1 = TypedPlus(9);
  1003. EXPECT_EQ(10, a1.Perform(std::make_tuple(Char(1), true)));
  1004. }
  1005. // Tests that a parameterized action can be used in any mock function
  1006. // whose type is compatible.
  1007. TEST(ActionPMacroTest, WorksInCompatibleMockFunction) {
  1008. Action<std::string(const std::string& s)> a1 = Plus("tail");
  1009. const std::string re = "re";
  1010. std::tuple<const std::string> dummy = std::make_tuple(re);
  1011. EXPECT_EQ("retail", a1.Perform(dummy));
  1012. }
  1013. // Tests that we can use ACTION*() to define actions overloaded on the
  1014. // number of parameters.
  1015. ACTION(OverloadedAction) { return arg0 ? arg1 : "hello"; }
  1016. ACTION_P(OverloadedAction, default_value) {
  1017. return arg0 ? arg1 : default_value;
  1018. }
  1019. ACTION_P2(OverloadedAction, true_value, false_value) {
  1020. return arg0 ? true_value : false_value;
  1021. }
  1022. TEST(ActionMacroTest, CanDefineOverloadedActions) {
  1023. using MyAction = Action<const char*(bool, const char*)>;
  1024. const MyAction a1 = OverloadedAction();
  1025. EXPECT_STREQ("hello", a1.Perform(std::make_tuple(false, CharPtr("world"))));
  1026. EXPECT_STREQ("world", a1.Perform(std::make_tuple(true, CharPtr("world"))));
  1027. const MyAction a2 = OverloadedAction("hi");
  1028. EXPECT_STREQ("hi", a2.Perform(std::make_tuple(false, CharPtr("world"))));
  1029. EXPECT_STREQ("world", a2.Perform(std::make_tuple(true, CharPtr("world"))));
  1030. const MyAction a3 = OverloadedAction("hi", "you");
  1031. EXPECT_STREQ("hi", a3.Perform(std::make_tuple(true, CharPtr("world"))));
  1032. EXPECT_STREQ("you", a3.Perform(std::make_tuple(false, CharPtr("world"))));
  1033. }
  1034. // Tests ACTION_Pn where n >= 3.
  1035. ACTION_P3(Plus, m, n, k) { return arg0 + m + n + k; }
  1036. TEST(ActionPnMacroTest, WorksFor3Parameters) {
  1037. Action<double(int m, bool t)> a1 = Plus(100, 20, 3.4);
  1038. EXPECT_DOUBLE_EQ(3123.4, a1.Perform(std::make_tuple(3000, true)));
  1039. Action<std::string(const std::string& s)> a2 = Plus("tail", "-", ">");
  1040. const std::string re = "re";
  1041. std::tuple<const std::string> dummy = std::make_tuple(re);
  1042. EXPECT_EQ("retail->", a2.Perform(dummy));
  1043. }
  1044. ACTION_P4(Plus, p0, p1, p2, p3) { return arg0 + p0 + p1 + p2 + p3; }
  1045. TEST(ActionPnMacroTest, WorksFor4Parameters) {
  1046. Action<int(int)> a1 = Plus(1, 2, 3, 4);
  1047. EXPECT_EQ(10 + 1 + 2 + 3 + 4, a1.Perform(std::make_tuple(10)));
  1048. }
  1049. ACTION_P5(Plus, p0, p1, p2, p3, p4) { return arg0 + p0 + p1 + p2 + p3 + p4; }
  1050. TEST(ActionPnMacroTest, WorksFor5Parameters) {
  1051. Action<int(int)> a1 = Plus(1, 2, 3, 4, 5);
  1052. EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5, a1.Perform(std::make_tuple(10)));
  1053. }
  1054. ACTION_P6(Plus, p0, p1, p2, p3, p4, p5) {
  1055. return arg0 + p0 + p1 + p2 + p3 + p4 + p5;
  1056. }
  1057. TEST(ActionPnMacroTest, WorksFor6Parameters) {
  1058. Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6);
  1059. EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6, a1.Perform(std::make_tuple(10)));
  1060. }
  1061. ACTION_P7(Plus, p0, p1, p2, p3, p4, p5, p6) {
  1062. return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6;
  1063. }
  1064. TEST(ActionPnMacroTest, WorksFor7Parameters) {
  1065. Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7);
  1066. EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7, a1.Perform(std::make_tuple(10)));
  1067. }
  1068. ACTION_P8(Plus, p0, p1, p2, p3, p4, p5, p6, p7) {
  1069. return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7;
  1070. }
  1071. TEST(ActionPnMacroTest, WorksFor8Parameters) {
  1072. Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8);
  1073. EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
  1074. a1.Perform(std::make_tuple(10)));
  1075. }
  1076. ACTION_P9(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8) {
  1077. return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8;
  1078. }
  1079. TEST(ActionPnMacroTest, WorksFor9Parameters) {
  1080. Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9);
  1081. EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9,
  1082. a1.Perform(std::make_tuple(10)));
  1083. }
  1084. ACTION_P10(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8, last_param) {
  1085. arg0_type t0 = arg0;
  1086. last_param_type t9 = last_param;
  1087. return t0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + t9;
  1088. }
  1089. TEST(ActionPnMacroTest, WorksFor10Parameters) {
  1090. Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
  1091. EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10,
  1092. a1.Perform(std::make_tuple(10)));
  1093. }
  1094. // Tests that the action body can promote the parameter types.
  1095. ACTION_P2(PadArgument, prefix, suffix) {
  1096. // The following lines promote the two parameters to desired types.
  1097. std::string prefix_str(prefix);
  1098. char suffix_char = static_cast<char>(suffix);
  1099. return prefix_str + arg0 + suffix_char;
  1100. }
  1101. TEST(ActionPnMacroTest, SimpleTypePromotion) {
  1102. Action<std::string(const char*)> no_promo =
  1103. PadArgument(std::string("foo"), 'r');
  1104. Action<std::string(const char*)> promo =
  1105. PadArgument("foo", static_cast<int>('r'));
  1106. EXPECT_EQ("foobar", no_promo.Perform(std::make_tuple(CharPtr("ba"))));
  1107. EXPECT_EQ("foobar", promo.Perform(std::make_tuple(CharPtr("ba"))));
  1108. }
  1109. // Tests that we can partially restrict parameter types using a
  1110. // straight-forward pattern.
  1111. // Defines a generic action that doesn't restrict the types of its
  1112. // parameters.
  1113. ACTION_P3(ConcatImpl, a, b, c) {
  1114. std::stringstream ss;
  1115. ss << a << b << c;
  1116. return ss.str();
  1117. }
  1118. // Next, we try to restrict that either the first parameter is a
  1119. // string, or the second parameter is an int.
  1120. // Defines a partially specialized wrapper that restricts the first
  1121. // parameter to std::string.
  1122. template <typename T1, typename T2>
  1123. // ConcatImplActionP3 is the class template ACTION_P3 uses to
  1124. // implement ConcatImpl. We shouldn't change the name as this
  1125. // pattern requires the user to use it directly.
  1126. ConcatImplActionP3<std::string, T1, T2> Concat(const std::string& a, T1 b,
  1127. T2 c) {
  1128. GTEST_INTENTIONAL_CONST_COND_PUSH_()
  1129. if (true) {
  1130. GTEST_INTENTIONAL_CONST_COND_POP_()
  1131. // This branch verifies that ConcatImpl() can be invoked without
  1132. // explicit template arguments.
  1133. return ConcatImpl(a, b, c);
  1134. } else {
  1135. // This branch verifies that ConcatImpl() can also be invoked with
  1136. // explicit template arguments. It doesn't really need to be
  1137. // executed as this is a compile-time verification.
  1138. return ConcatImpl<std::string, T1, T2>(a, b, c);
  1139. }
  1140. }
  1141. // Defines another partially specialized wrapper that restricts the
  1142. // second parameter to int.
  1143. template <typename T1, typename T2>
  1144. ConcatImplActionP3<T1, int, T2> Concat(T1 a, int b, T2 c) {
  1145. return ConcatImpl(a, b, c);
  1146. }
  1147. TEST(ActionPnMacroTest, CanPartiallyRestrictParameterTypes) {
  1148. Action<const std::string()> a1 = Concat("Hello", "1", 2);
  1149. EXPECT_EQ("Hello12", a1.Perform(std::make_tuple()));
  1150. a1 = Concat(1, 2, 3);
  1151. EXPECT_EQ("123", a1.Perform(std::make_tuple()));
  1152. }
  1153. // Verifies the type of an ACTION*.
  1154. ACTION(DoFoo) {}
  1155. ACTION_P(DoFoo, p) {}
  1156. ACTION_P2(DoFoo, p0, p1) {}
  1157. TEST(ActionPnMacroTest, TypesAreCorrect) {
  1158. // DoFoo() must be assignable to a DoFooAction variable.
  1159. DoFooAction a0 = DoFoo();
  1160. // DoFoo(1) must be assignable to a DoFooActionP variable.
  1161. DoFooActionP<int> a1 = DoFoo(1);
  1162. // DoFoo(p1, ..., pk) must be assignable to a DoFooActionPk
  1163. // variable, and so on.
  1164. DoFooActionP2<int, char> a2 = DoFoo(1, '2');
  1165. PlusActionP3<int, int, char> a3 = Plus(1, 2, '3');
  1166. PlusActionP4<int, int, int, char> a4 = Plus(1, 2, 3, '4');
  1167. PlusActionP5<int, int, int, int, char> a5 = Plus(1, 2, 3, 4, '5');
  1168. PlusActionP6<int, int, int, int, int, char> a6 = Plus(1, 2, 3, 4, 5, '6');
  1169. PlusActionP7<int, int, int, int, int, int, char> a7 =
  1170. Plus(1, 2, 3, 4, 5, 6, '7');
  1171. PlusActionP8<int, int, int, int, int, int, int, char> a8 =
  1172. Plus(1, 2, 3, 4, 5, 6, 7, '8');
  1173. PlusActionP9<int, int, int, int, int, int, int, int, char> a9 =
  1174. Plus(1, 2, 3, 4, 5, 6, 7, 8, '9');
  1175. PlusActionP10<int, int, int, int, int, int, int, int, int, char> a10 =
  1176. Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
  1177. // Avoid "unused variable" warnings.
  1178. (void)a0;
  1179. (void)a1;
  1180. (void)a2;
  1181. (void)a3;
  1182. (void)a4;
  1183. (void)a5;
  1184. (void)a6;
  1185. (void)a7;
  1186. (void)a8;
  1187. (void)a9;
  1188. (void)a10;
  1189. }
  1190. // Tests that an ACTION_P*() action can be explicitly instantiated
  1191. // with reference-typed parameters.
  1192. ACTION_P(Plus1, x) { return x; }
  1193. ACTION_P2(Plus2, x, y) { return x + y; }
  1194. ACTION_P3(Plus3, x, y, z) { return x + y + z; }
  1195. ACTION_P10(Plus10, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
  1196. return a0 + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9;
  1197. }
  1198. TEST(ActionPnMacroTest, CanExplicitlyInstantiateWithReferenceTypes) {
  1199. int x = 1, y = 2, z = 3;
  1200. const std::tuple<> empty = std::make_tuple();
  1201. Action<int()> a = Plus1<int&>(x);
  1202. EXPECT_EQ(1, a.Perform(empty));
  1203. a = Plus2<const int&, int&>(x, y);
  1204. EXPECT_EQ(3, a.Perform(empty));
  1205. a = Plus3<int&, const int&, int&>(x, y, z);
  1206. EXPECT_EQ(6, a.Perform(empty));
  1207. int n[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  1208. a = Plus10<const int&, int&, const int&, int&, const int&, int&, const int&,
  1209. int&, const int&, int&>(n[0], n[1], n[2], n[3], n[4], n[5], n[6],
  1210. n[7], n[8], n[9]);
  1211. EXPECT_EQ(55, a.Perform(empty));
  1212. }
  1213. class TenArgConstructorClass {
  1214. public:
  1215. TenArgConstructorClass(int a1, int a2, int a3, int a4, int a5, int a6, int a7,
  1216. int a8, int a9, int a10)
  1217. : value_(a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10) {}
  1218. int value_;
  1219. };
  1220. // Tests that ACTION_TEMPLATE works when there is no value parameter.
  1221. ACTION_TEMPLATE(CreateNew, HAS_1_TEMPLATE_PARAMS(typename, T),
  1222. AND_0_VALUE_PARAMS()) {
  1223. return new T;
  1224. }
  1225. TEST(ActionTemplateTest, WorksWithoutValueParam) {
  1226. const Action<int*()> a = CreateNew<int>();
  1227. int* p = a.Perform(std::make_tuple());
  1228. delete p;
  1229. }
  1230. // Tests that ACTION_TEMPLATE works when there are value parameters.
  1231. ACTION_TEMPLATE(CreateNew, HAS_1_TEMPLATE_PARAMS(typename, T),
  1232. AND_1_VALUE_PARAMS(a0)) {
  1233. return new T(a0);
  1234. }
  1235. TEST(ActionTemplateTest, WorksWithValueParams) {
  1236. const Action<int*()> a = CreateNew<int>(42);
  1237. int* p = a.Perform(std::make_tuple());
  1238. EXPECT_EQ(42, *p);
  1239. delete p;
  1240. }
  1241. // Tests that ACTION_TEMPLATE works for integral template parameters.
  1242. ACTION_TEMPLATE(MyDeleteArg, HAS_1_TEMPLATE_PARAMS(int, k),
  1243. AND_0_VALUE_PARAMS()) {
  1244. delete std::get<k>(args);
  1245. }
  1246. // Resets a bool variable in the destructor.
  1247. class BoolResetter {
  1248. public:
  1249. explicit BoolResetter(bool* value) : value_(value) {}
  1250. ~BoolResetter() { *value_ = false; }
  1251. private:
  1252. bool* value_;
  1253. };
  1254. TEST(ActionTemplateTest, WorksForIntegralTemplateParams) {
  1255. const Action<void(int*, BoolResetter*)> a = MyDeleteArg<1>();
  1256. int n = 0;
  1257. bool b = true;
  1258. auto* resetter = new BoolResetter(&b);
  1259. a.Perform(std::make_tuple(&n, resetter));
  1260. EXPECT_FALSE(b); // Verifies that resetter is deleted.
  1261. }
  1262. // Tests that ACTION_TEMPLATES works for template template parameters.
  1263. ACTION_TEMPLATE(ReturnSmartPointer,
  1264. HAS_1_TEMPLATE_PARAMS(template <typename Pointee> class,
  1265. Pointer),
  1266. AND_1_VALUE_PARAMS(pointee)) {
  1267. return Pointer<pointee_type>(new pointee_type(pointee));
  1268. }
  1269. TEST(ActionTemplateTest, WorksForTemplateTemplateParameters) {
  1270. const Action<std::shared_ptr<int>()> a =
  1271. ReturnSmartPointer<std::shared_ptr>(42);
  1272. std::shared_ptr<int> p = a.Perform(std::make_tuple());
  1273. EXPECT_EQ(42, *p);
  1274. }
  1275. // Tests that ACTION_TEMPLATE works for 10 template parameters.
  1276. template <typename T1, typename T2, typename T3, int k4, bool k5,
  1277. unsigned int k6, typename T7, typename T8, typename T9>
  1278. struct GiantTemplate {
  1279. public:
  1280. explicit GiantTemplate(int a_value) : value(a_value) {}
  1281. int value;
  1282. };
  1283. ACTION_TEMPLATE(ReturnGiant,
  1284. HAS_10_TEMPLATE_PARAMS(typename, T1, typename, T2, typename, T3,
  1285. int, k4, bool, k5, unsigned int, k6,
  1286. class, T7, class, T8, class, T9,
  1287. template <typename T> class, T10),
  1288. AND_1_VALUE_PARAMS(value)) {
  1289. return GiantTemplate<T10<T1>, T2, T3, k4, k5, k6, T7, T8, T9>(value);
  1290. }
  1291. TEST(ActionTemplateTest, WorksFor10TemplateParameters) {
  1292. using Giant = GiantTemplate<std::shared_ptr<int>, bool, double, 5, true, 6,
  1293. char, unsigned, int>;
  1294. const Action<Giant()> a = ReturnGiant<int, bool, double, 5, true, 6, char,
  1295. unsigned, int, std::shared_ptr>(42);
  1296. Giant giant = a.Perform(std::make_tuple());
  1297. EXPECT_EQ(42, giant.value);
  1298. }
  1299. // Tests that ACTION_TEMPLATE works for 10 value parameters.
  1300. ACTION_TEMPLATE(ReturnSum, HAS_1_TEMPLATE_PARAMS(typename, Number),
  1301. AND_10_VALUE_PARAMS(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10)) {
  1302. return static_cast<Number>(v1) + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10;
  1303. }
  1304. TEST(ActionTemplateTest, WorksFor10ValueParameters) {
  1305. const Action<int()> a = ReturnSum<int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
  1306. EXPECT_EQ(55, a.Perform(std::make_tuple()));
  1307. }
  1308. // Tests that ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded
  1309. // on the number of value parameters.
  1310. ACTION(ReturnSum) { return 0; }
  1311. ACTION_P(ReturnSum, x) { return x; }
  1312. ACTION_TEMPLATE(ReturnSum, HAS_1_TEMPLATE_PARAMS(typename, Number),
  1313. AND_2_VALUE_PARAMS(v1, v2)) {
  1314. return static_cast<Number>(v1) + v2;
  1315. }
  1316. ACTION_TEMPLATE(ReturnSum, HAS_1_TEMPLATE_PARAMS(typename, Number),
  1317. AND_3_VALUE_PARAMS(v1, v2, v3)) {
  1318. return static_cast<Number>(v1) + v2 + v3;
  1319. }
  1320. ACTION_TEMPLATE(ReturnSum, HAS_2_TEMPLATE_PARAMS(typename, Number, int, k),
  1321. AND_4_VALUE_PARAMS(v1, v2, v3, v4)) {
  1322. return static_cast<Number>(v1) + v2 + v3 + v4 + k;
  1323. }
  1324. TEST(ActionTemplateTest, CanBeOverloadedOnNumberOfValueParameters) {
  1325. const Action<int()> a0 = ReturnSum();
  1326. const Action<int()> a1 = ReturnSum(1);
  1327. const Action<int()> a2 = ReturnSum<int>(1, 2);
  1328. const Action<int()> a3 = ReturnSum<int>(1, 2, 3);
  1329. const Action<int()> a4 = ReturnSum<int, 10000>(2000, 300, 40, 5);
  1330. EXPECT_EQ(0, a0.Perform(std::make_tuple()));
  1331. EXPECT_EQ(1, a1.Perform(std::make_tuple()));
  1332. EXPECT_EQ(3, a2.Perform(std::make_tuple()));
  1333. EXPECT_EQ(6, a3.Perform(std::make_tuple()));
  1334. EXPECT_EQ(12345, a4.Perform(std::make_tuple()));
  1335. }
  1336. } // namespace gmock_more_actions_test
  1337. } // namespace testing
  1338. GTEST_DISABLE_MSC_WARNINGS_POP_() // 4100 4503
  1339. GTEST_DISABLE_MSC_WARNINGS_POP_() // 4577