gmock-more-actions_test.cc 52 KB

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