gmock-more-actions_test.cc 52 KB

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