gmock-more-actions_test.cc 52 KB

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