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