gmock-generated-actions_test.cc 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  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 generated by a script.
  32. #include "gmock/gmock-generated-actions.h"
  33. #include <functional>
  34. #include <sstream>
  35. #include <string>
  36. #include "gmock/gmock.h"
  37. #include "gtest/gtest.h"
  38. namespace testing {
  39. namespace gmock_generated_actions_test {
  40. using ::std::plus;
  41. using ::std::string;
  42. using testing::get;
  43. using testing::make_tuple;
  44. using testing::tuple;
  45. using testing::tuple_element;
  46. using testing::_;
  47. using testing::Action;
  48. using testing::ActionInterface;
  49. using testing::ByRef;
  50. using testing::DoAll;
  51. using testing::Invoke;
  52. using testing::Return;
  53. using testing::ReturnNew;
  54. using testing::SetArgPointee;
  55. using testing::StaticAssertTypeEq;
  56. using testing::Unused;
  57. using testing::WithArgs;
  58. // For suppressing compiler warnings on conversion possibly losing precision.
  59. inline short Short(short n) { return n; } // NOLINT
  60. inline char Char(char ch) { return ch; }
  61. // Sample functions and functors for testing various actions.
  62. int Nullary() { return 1; }
  63. class NullaryFunctor {
  64. public:
  65. int operator()() { return 2; }
  66. };
  67. bool g_done = false;
  68. bool Unary(int x) { return x < 0; }
  69. const char* Plus1(const char* s) { return s + 1; }
  70. bool ByConstRef(const std::string& s) { return s == "Hi"; }
  71. const double g_double = 0;
  72. bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
  73. std::string ByNonConstRef(std::string& s) { return s += "+"; } // NOLINT
  74. struct UnaryFunctor {
  75. int operator()(bool x) { return x ? 1 : -1; }
  76. };
  77. const char* Binary(const char* input, short n) { return input + n; } // NOLINT
  78. void VoidBinary(int, char) { g_done = true; }
  79. int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT
  80. void VoidTernary(int, char, bool) { g_done = true; }
  81. int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
  82. std::string Concat4(const char* s1, const char* s2, const char* s3,
  83. const char* s4) {
  84. return std::string(s1) + s2 + s3 + s4;
  85. }
  86. int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
  87. struct SumOf5Functor {
  88. int operator()(int a, int b, int c, int d, int e) {
  89. return a + b + c + d + e;
  90. }
  91. };
  92. std::string Concat5(const char* s1, const char* s2, const char* s3,
  93. const char* s4, const char* s5) {
  94. return std::string(s1) + s2 + s3 + s4 + s5;
  95. }
  96. int SumOf6(int a, int b, int c, int d, int e, int f) {
  97. return a + b + c + d + e + f;
  98. }
  99. struct SumOf6Functor {
  100. int operator()(int a, int b, int c, int d, int e, int f) {
  101. return a + b + c + d + e + f;
  102. }
  103. };
  104. std::string Concat6(const char* s1, const char* s2, const char* s3,
  105. const char* s4, const char* s5, const char* s6) {
  106. return std::string(s1) + s2 + s3 + s4 + s5 + s6;
  107. }
  108. std::string Concat7(const char* s1, const char* s2, const char* s3,
  109. const char* s4, const char* s5, const char* s6,
  110. const char* s7) {
  111. return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
  112. }
  113. std::string Concat8(const char* s1, const char* s2, const char* s3,
  114. const char* s4, const char* s5, const char* s6,
  115. const char* s7, const char* s8) {
  116. return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
  117. }
  118. std::string Concat9(const char* s1, const char* s2, const char* s3,
  119. const char* s4, const char* s5, const char* s6,
  120. const char* s7, const char* s8, const char* s9) {
  121. return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
  122. }
  123. std::string Concat10(const char* s1, const char* s2, const char* s3,
  124. const char* s4, const char* s5, const char* s6,
  125. const char* s7, const char* s8, const char* s9,
  126. const char* s10) {
  127. return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
  128. }
  129. // A helper that turns the type of a C-string literal from const
  130. // char[N] to const char*.
  131. inline const char* CharPtr(const char* s) { return s; }
  132. // Tests InvokeArgument<N>(...).
  133. // Tests using InvokeArgument with a nullary function.
  134. TEST(InvokeArgumentTest, Function0) {
  135. Action<int(int, int(*)())> a = InvokeArgument<1>(); // NOLINT
  136. EXPECT_EQ(1, a.Perform(make_tuple(2, &Nullary)));
  137. }
  138. // Tests using InvokeArgument with a unary function.
  139. TEST(InvokeArgumentTest, Functor1) {
  140. Action<int(UnaryFunctor)> a = InvokeArgument<0>(true); // NOLINT
  141. EXPECT_EQ(1, a.Perform(make_tuple(UnaryFunctor())));
  142. }
  143. // Tests using InvokeArgument with a 5-ary function.
  144. TEST(InvokeArgumentTest, Function5) {
  145. Action<int(int(*)(int, int, int, int, int))> a = // NOLINT
  146. InvokeArgument<0>(10000, 2000, 300, 40, 5);
  147. EXPECT_EQ(12345, a.Perform(make_tuple(&SumOf5)));
  148. }
  149. // Tests using InvokeArgument with a 5-ary functor.
  150. TEST(InvokeArgumentTest, Functor5) {
  151. Action<int(SumOf5Functor)> a = // NOLINT
  152. InvokeArgument<0>(10000, 2000, 300, 40, 5);
  153. EXPECT_EQ(12345, a.Perform(make_tuple(SumOf5Functor())));
  154. }
  155. // Tests using InvokeArgument with a 6-ary function.
  156. TEST(InvokeArgumentTest, Function6) {
  157. Action<int(int(*)(int, int, int, int, int, int))> a = // NOLINT
  158. InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
  159. EXPECT_EQ(123456, a.Perform(make_tuple(&SumOf6)));
  160. }
  161. // Tests using InvokeArgument with a 6-ary functor.
  162. TEST(InvokeArgumentTest, Functor6) {
  163. Action<int(SumOf6Functor)> a = // NOLINT
  164. InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
  165. EXPECT_EQ(123456, a.Perform(make_tuple(SumOf6Functor())));
  166. }
  167. // Tests using InvokeArgument with a 7-ary function.
  168. TEST(InvokeArgumentTest, Function7) {
  169. Action<std::string(std::string(*)(const char*, const char*, const char*,
  170. const char*, const char*, const char*,
  171. const char*))>
  172. a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7");
  173. EXPECT_EQ("1234567", a.Perform(make_tuple(&Concat7)));
  174. }
  175. // Tests using InvokeArgument with a 8-ary function.
  176. TEST(InvokeArgumentTest, Function8) {
  177. Action<std::string(std::string(*)(const char*, const char*, const char*,
  178. const char*, const char*, const char*,
  179. const char*, const char*))>
  180. a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8");
  181. EXPECT_EQ("12345678", a.Perform(make_tuple(&Concat8)));
  182. }
  183. // Tests using InvokeArgument with a 9-ary function.
  184. TEST(InvokeArgumentTest, Function9) {
  185. Action<std::string(std::string(*)(const char*, const char*, const char*,
  186. const char*, const char*, const char*,
  187. const char*, const char*, const char*))>
  188. a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9");
  189. EXPECT_EQ("123456789", a.Perform(make_tuple(&Concat9)));
  190. }
  191. // Tests using InvokeArgument with a 10-ary function.
  192. TEST(InvokeArgumentTest, Function10) {
  193. Action<std::string(std::string(*)(
  194. const char*, const char*, const char*, const char*, const char*,
  195. const char*, const char*, const char*, const char*, const char*))>
  196. a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
  197. EXPECT_EQ("1234567890", a.Perform(make_tuple(&Concat10)));
  198. }
  199. // Tests using InvokeArgument with a function that takes a pointer argument.
  200. TEST(InvokeArgumentTest, ByPointerFunction) {
  201. Action<const char*(const char*(*)(const char* input, short n))> a = // NOLINT
  202. InvokeArgument<0>(static_cast<const char*>("Hi"), Short(1));
  203. EXPECT_STREQ("i", a.Perform(make_tuple(&Binary)));
  204. }
  205. // Tests using InvokeArgument with a function that takes a const char*
  206. // by passing it a C-string literal.
  207. TEST(InvokeArgumentTest, FunctionWithCStringLiteral) {
  208. Action<const char*(const char*(*)(const char* input, short n))> a = // NOLINT
  209. InvokeArgument<0>("Hi", Short(1));
  210. EXPECT_STREQ("i", a.Perform(make_tuple(&Binary)));
  211. }
  212. // Tests using InvokeArgument with a function that takes a const reference.
  213. TEST(InvokeArgumentTest, ByConstReferenceFunction) {
  214. Action<bool(bool (*function)(const std::string& s))> a = // NOLINT
  215. InvokeArgument<0>(std::string("Hi"));
  216. // When action 'a' is constructed, it makes a copy of the temporary
  217. // string object passed to it, so it's OK to use 'a' later, when the
  218. // temporary object has already died.
  219. EXPECT_TRUE(a.Perform(make_tuple(&ByConstRef)));
  220. }
  221. // Tests using InvokeArgument with ByRef() and a function that takes a
  222. // const reference.
  223. TEST(InvokeArgumentTest, ByExplicitConstReferenceFunction) {
  224. Action<bool(bool(*)(const double& x))> a = // NOLINT
  225. InvokeArgument<0>(ByRef(g_double));
  226. // The above line calls ByRef() on a const value.
  227. EXPECT_TRUE(a.Perform(make_tuple(&ReferencesGlobalDouble)));
  228. double x = 0;
  229. a = InvokeArgument<0>(ByRef(x)); // This calls ByRef() on a non-const.
  230. EXPECT_FALSE(a.Perform(make_tuple(&ReferencesGlobalDouble)));
  231. }
  232. // Tests using WithArgs and with an action that takes 1 argument.
  233. TEST(WithArgsTest, OneArg) {
  234. Action<bool(double x, int n)> a = WithArgs<1>(Invoke(Unary)); // NOLINT
  235. EXPECT_TRUE(a.Perform(make_tuple(1.5, -1)));
  236. EXPECT_FALSE(a.Perform(make_tuple(1.5, 1)));
  237. }
  238. // Tests using WithArgs with an action that takes 2 arguments.
  239. TEST(WithArgsTest, TwoArgs) {
  240. Action<const char*(const char* s, double x, short n)> a =
  241. WithArgs<0, 2>(Invoke(Binary));
  242. const char s[] = "Hello";
  243. EXPECT_EQ(s + 2, a.Perform(make_tuple(CharPtr(s), 0.5, Short(2))));
  244. }
  245. // Tests using WithArgs with an action that takes 3 arguments.
  246. TEST(WithArgsTest, ThreeArgs) {
  247. Action<int(int, double, char, short)> a = // NOLINT
  248. WithArgs<0, 2, 3>(Invoke(Ternary));
  249. EXPECT_EQ(123, a.Perform(make_tuple(100, 6.5, Char(20), Short(3))));
  250. }
  251. // Tests using WithArgs with an action that takes 4 arguments.
  252. TEST(WithArgsTest, FourArgs) {
  253. Action<std::string(const char*, const char*, double, const char*,
  254. const char*)>
  255. a = WithArgs<4, 3, 1, 0>(Invoke(Concat4));
  256. EXPECT_EQ("4310", a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), 2.5,
  257. CharPtr("3"), CharPtr("4"))));
  258. }
  259. // Tests using WithArgs with an action that takes 5 arguments.
  260. TEST(WithArgsTest, FiveArgs) {
  261. Action<std::string(const char*, const char*, const char*, const char*,
  262. const char*)>
  263. a = WithArgs<4, 3, 2, 1, 0>(Invoke(Concat5));
  264. EXPECT_EQ("43210",
  265. a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
  266. CharPtr("3"), CharPtr("4"))));
  267. }
  268. // Tests using WithArgs with an action that takes 6 arguments.
  269. TEST(WithArgsTest, SixArgs) {
  270. Action<std::string(const char*, const char*, const char*)> a =
  271. WithArgs<0, 1, 2, 2, 1, 0>(Invoke(Concat6));
  272. EXPECT_EQ("012210",
  273. a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"))));
  274. }
  275. // Tests using WithArgs with an action that takes 7 arguments.
  276. TEST(WithArgsTest, SevenArgs) {
  277. Action<std::string(const char*, const char*, const char*, const char*)> a =
  278. WithArgs<0, 1, 2, 3, 2, 1, 0>(Invoke(Concat7));
  279. EXPECT_EQ("0123210",
  280. a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
  281. CharPtr("3"))));
  282. }
  283. // Tests using WithArgs with an action that takes 8 arguments.
  284. TEST(WithArgsTest, EightArgs) {
  285. Action<std::string(const char*, const char*, const char*, const char*)> a =
  286. WithArgs<0, 1, 2, 3, 0, 1, 2, 3>(Invoke(Concat8));
  287. EXPECT_EQ("01230123",
  288. a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
  289. CharPtr("3"))));
  290. }
  291. // Tests using WithArgs with an action that takes 9 arguments.
  292. TEST(WithArgsTest, NineArgs) {
  293. Action<std::string(const char*, const char*, const char*, const char*)> a =
  294. WithArgs<0, 1, 2, 3, 1, 2, 3, 2, 3>(Invoke(Concat9));
  295. EXPECT_EQ("012312323",
  296. a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
  297. CharPtr("3"))));
  298. }
  299. // Tests using WithArgs with an action that takes 10 arguments.
  300. TEST(WithArgsTest, TenArgs) {
  301. Action<std::string(const char*, const char*, const char*, const char*)> a =
  302. WithArgs<0, 1, 2, 3, 2, 1, 0, 1, 2, 3>(Invoke(Concat10));
  303. EXPECT_EQ("0123210123",
  304. a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
  305. CharPtr("3"))));
  306. }
  307. // Tests using WithArgs with an action that is not Invoke().
  308. class SubstractAction : public ActionInterface<int(int, int)> { // NOLINT
  309. public:
  310. virtual int Perform(const tuple<int, int>& args) {
  311. return get<0>(args) - get<1>(args);
  312. }
  313. };
  314. TEST(WithArgsTest, NonInvokeAction) {
  315. Action<int(const std::string&, int, int)> a = // NOLINT
  316. WithArgs<2, 1>(MakeAction(new SubstractAction));
  317. tuple<std::string, int, int> dummy = make_tuple(std::string("hi"), 2, 10);
  318. EXPECT_EQ(8, a.Perform(dummy));
  319. }
  320. // Tests using WithArgs to pass all original arguments in the original order.
  321. TEST(WithArgsTest, Identity) {
  322. Action<int(int x, char y, short z)> a = // NOLINT
  323. WithArgs<0, 1, 2>(Invoke(Ternary));
  324. EXPECT_EQ(123, a.Perform(make_tuple(100, Char(20), Short(3))));
  325. }
  326. // Tests using WithArgs with repeated arguments.
  327. TEST(WithArgsTest, RepeatedArguments) {
  328. Action<int(bool, int m, int n)> a = // NOLINT
  329. WithArgs<1, 1, 1, 1>(Invoke(SumOf4));
  330. EXPECT_EQ(4, a.Perform(make_tuple(false, 1, 10)));
  331. }
  332. // Tests using WithArgs with reversed argument order.
  333. TEST(WithArgsTest, ReversedArgumentOrder) {
  334. Action<const char*(short n, const char* input)> a = // NOLINT
  335. WithArgs<1, 0>(Invoke(Binary));
  336. const char s[] = "Hello";
  337. EXPECT_EQ(s + 2, a.Perform(make_tuple(Short(2), CharPtr(s))));
  338. }
  339. // Tests using WithArgs with compatible, but not identical, argument types.
  340. TEST(WithArgsTest, ArgsOfCompatibleTypes) {
  341. Action<long(short x, char y, double z, char c)> a = // NOLINT
  342. WithArgs<0, 1, 3>(Invoke(Ternary));
  343. EXPECT_EQ(123, a.Perform(make_tuple(Short(100), Char(20), 5.6, Char(3))));
  344. }
  345. // Tests using WithArgs with an action that returns void.
  346. TEST(WithArgsTest, VoidAction) {
  347. Action<void(double x, char c, int n)> a = WithArgs<2, 1>(Invoke(VoidBinary));
  348. g_done = false;
  349. a.Perform(make_tuple(1.5, 'a', 3));
  350. EXPECT_TRUE(g_done);
  351. }
  352. // Tests DoAll(a1, a2).
  353. TEST(DoAllTest, TwoActions) {
  354. int n = 0;
  355. Action<int(int*)> a = DoAll(SetArgPointee<0>(1), // NOLINT
  356. Return(2));
  357. EXPECT_EQ(2, a.Perform(make_tuple(&n)));
  358. EXPECT_EQ(1, n);
  359. }
  360. // Tests DoAll(a1, a2, a3).
  361. TEST(DoAllTest, ThreeActions) {
  362. int m = 0, n = 0;
  363. Action<int(int*, int*)> a = DoAll(SetArgPointee<0>(1), // NOLINT
  364. SetArgPointee<1>(2),
  365. Return(3));
  366. EXPECT_EQ(3, a.Perform(make_tuple(&m, &n)));
  367. EXPECT_EQ(1, m);
  368. EXPECT_EQ(2, n);
  369. }
  370. // Tests DoAll(a1, a2, a3, a4).
  371. TEST(DoAllTest, FourActions) {
  372. int m = 0, n = 0;
  373. char ch = '\0';
  374. Action<int(int*, int*, char*)> a = // NOLINT
  375. DoAll(SetArgPointee<0>(1),
  376. SetArgPointee<1>(2),
  377. SetArgPointee<2>('a'),
  378. Return(3));
  379. EXPECT_EQ(3, a.Perform(make_tuple(&m, &n, &ch)));
  380. EXPECT_EQ(1, m);
  381. EXPECT_EQ(2, n);
  382. EXPECT_EQ('a', ch);
  383. }
  384. // Tests DoAll(a1, a2, a3, a4, a5).
  385. TEST(DoAllTest, FiveActions) {
  386. int m = 0, n = 0;
  387. char a = '\0', b = '\0';
  388. Action<int(int*, int*, char*, char*)> action = // NOLINT
  389. DoAll(SetArgPointee<0>(1),
  390. SetArgPointee<1>(2),
  391. SetArgPointee<2>('a'),
  392. SetArgPointee<3>('b'),
  393. Return(3));
  394. EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b)));
  395. EXPECT_EQ(1, m);
  396. EXPECT_EQ(2, n);
  397. EXPECT_EQ('a', a);
  398. EXPECT_EQ('b', b);
  399. }
  400. // Tests DoAll(a1, a2, ..., a6).
  401. TEST(DoAllTest, SixActions) {
  402. int m = 0, n = 0;
  403. char a = '\0', b = '\0', c = '\0';
  404. Action<int(int*, int*, char*, char*, char*)> action = // NOLINT
  405. DoAll(SetArgPointee<0>(1),
  406. SetArgPointee<1>(2),
  407. SetArgPointee<2>('a'),
  408. SetArgPointee<3>('b'),
  409. SetArgPointee<4>('c'),
  410. Return(3));
  411. EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c)));
  412. EXPECT_EQ(1, m);
  413. EXPECT_EQ(2, n);
  414. EXPECT_EQ('a', a);
  415. EXPECT_EQ('b', b);
  416. EXPECT_EQ('c', c);
  417. }
  418. // Tests DoAll(a1, a2, ..., a7).
  419. TEST(DoAllTest, SevenActions) {
  420. int m = 0, n = 0;
  421. char a = '\0', b = '\0', c = '\0', d = '\0';
  422. Action<int(int*, int*, char*, char*, char*, char*)> action = // NOLINT
  423. DoAll(SetArgPointee<0>(1),
  424. SetArgPointee<1>(2),
  425. SetArgPointee<2>('a'),
  426. SetArgPointee<3>('b'),
  427. SetArgPointee<4>('c'),
  428. SetArgPointee<5>('d'),
  429. Return(3));
  430. EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d)));
  431. EXPECT_EQ(1, m);
  432. EXPECT_EQ(2, n);
  433. EXPECT_EQ('a', a);
  434. EXPECT_EQ('b', b);
  435. EXPECT_EQ('c', c);
  436. EXPECT_EQ('d', d);
  437. }
  438. // Tests DoAll(a1, a2, ..., a8).
  439. TEST(DoAllTest, EightActions) {
  440. int m = 0, n = 0;
  441. char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0';
  442. Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
  443. char*)> action =
  444. DoAll(SetArgPointee<0>(1),
  445. SetArgPointee<1>(2),
  446. SetArgPointee<2>('a'),
  447. SetArgPointee<3>('b'),
  448. SetArgPointee<4>('c'),
  449. SetArgPointee<5>('d'),
  450. SetArgPointee<6>('e'),
  451. Return(3));
  452. EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d, &e)));
  453. EXPECT_EQ(1, m);
  454. EXPECT_EQ(2, n);
  455. EXPECT_EQ('a', a);
  456. EXPECT_EQ('b', b);
  457. EXPECT_EQ('c', c);
  458. EXPECT_EQ('d', d);
  459. EXPECT_EQ('e', e);
  460. }
  461. // Tests DoAll(a1, a2, ..., a9).
  462. TEST(DoAllTest, NineActions) {
  463. int m = 0, n = 0;
  464. char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0', f = '\0';
  465. Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
  466. char*, char*)> action =
  467. DoAll(SetArgPointee<0>(1),
  468. SetArgPointee<1>(2),
  469. SetArgPointee<2>('a'),
  470. SetArgPointee<3>('b'),
  471. SetArgPointee<4>('c'),
  472. SetArgPointee<5>('d'),
  473. SetArgPointee<6>('e'),
  474. SetArgPointee<7>('f'),
  475. Return(3));
  476. EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d, &e, &f)));
  477. EXPECT_EQ(1, m);
  478. EXPECT_EQ(2, n);
  479. EXPECT_EQ('a', a);
  480. EXPECT_EQ('b', b);
  481. EXPECT_EQ('c', c);
  482. EXPECT_EQ('d', d);
  483. EXPECT_EQ('e', e);
  484. EXPECT_EQ('f', f);
  485. }
  486. // Tests DoAll(a1, a2, ..., a10).
  487. TEST(DoAllTest, TenActions) {
  488. int m = 0, n = 0;
  489. char a = '\0', b = '\0', c = '\0', d = '\0';
  490. char e = '\0', f = '\0', g = '\0';
  491. Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
  492. char*, char*, char*)> action =
  493. DoAll(SetArgPointee<0>(1),
  494. SetArgPointee<1>(2),
  495. SetArgPointee<2>('a'),
  496. SetArgPointee<3>('b'),
  497. SetArgPointee<4>('c'),
  498. SetArgPointee<5>('d'),
  499. SetArgPointee<6>('e'),
  500. SetArgPointee<7>('f'),
  501. SetArgPointee<8>('g'),
  502. Return(3));
  503. EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d, &e, &f, &g)));
  504. EXPECT_EQ(1, m);
  505. EXPECT_EQ(2, n);
  506. EXPECT_EQ('a', a);
  507. EXPECT_EQ('b', b);
  508. EXPECT_EQ('c', c);
  509. EXPECT_EQ('d', d);
  510. EXPECT_EQ('e', e);
  511. EXPECT_EQ('f', f);
  512. EXPECT_EQ('g', g);
  513. }
  514. // The ACTION*() macros trigger warning C4100 (unreferenced formal
  515. // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
  516. // the macro definition, as the warnings are generated when the macro
  517. // is expanded and macro expansion cannot contain #pragma. Therefore
  518. // we suppress them here.
  519. #ifdef _MSC_VER
  520. # pragma warning(push)
  521. # pragma warning(disable:4100)
  522. #endif
  523. // Tests the ACTION*() macro family.
  524. // Tests that ACTION() can define an action that doesn't reference the
  525. // mock function arguments.
  526. ACTION(Return5) { return 5; }
  527. TEST(ActionMacroTest, WorksWhenNotReferencingArguments) {
  528. Action<double()> a1 = Return5();
  529. EXPECT_DOUBLE_EQ(5, a1.Perform(make_tuple()));
  530. Action<int(double, bool)> a2 = Return5();
  531. EXPECT_EQ(5, a2.Perform(make_tuple(1, true)));
  532. }
  533. // Tests that ACTION() can define an action that returns void.
  534. ACTION(IncrementArg1) { (*arg1)++; }
  535. TEST(ActionMacroTest, WorksWhenReturningVoid) {
  536. Action<void(int, int*)> a1 = IncrementArg1();
  537. int n = 0;
  538. a1.Perform(make_tuple(5, &n));
  539. EXPECT_EQ(1, n);
  540. }
  541. // Tests that the body of ACTION() can reference the type of the
  542. // argument.
  543. ACTION(IncrementArg2) {
  544. StaticAssertTypeEq<int*, arg2_type>();
  545. arg2_type temp = arg2;
  546. (*temp)++;
  547. }
  548. TEST(ActionMacroTest, CanReferenceArgumentType) {
  549. Action<void(int, bool, int*)> a1 = IncrementArg2();
  550. int n = 0;
  551. a1.Perform(make_tuple(5, false, &n));
  552. EXPECT_EQ(1, n);
  553. }
  554. // Tests that the body of ACTION() can reference the argument tuple
  555. // via args_type and args.
  556. ACTION(Sum2) {
  557. StaticAssertTypeEq<tuple<int, char, int*>, args_type>();
  558. args_type args_copy = args;
  559. return get<0>(args_copy) + get<1>(args_copy);
  560. }
  561. TEST(ActionMacroTest, CanReferenceArgumentTuple) {
  562. Action<int(int, char, int*)> a1 = Sum2();
  563. int dummy = 0;
  564. EXPECT_EQ(11, a1.Perform(make_tuple(5, Char(6), &dummy)));
  565. }
  566. // Tests that the body of ACTION() can reference the mock function
  567. // type.
  568. int Dummy(bool flag) { return flag? 1 : 0; }
  569. ACTION(InvokeDummy) {
  570. StaticAssertTypeEq<int(bool), function_type>();
  571. function_type* fp = &Dummy;
  572. return (*fp)(true);
  573. }
  574. TEST(ActionMacroTest, CanReferenceMockFunctionType) {
  575. Action<int(bool)> a1 = InvokeDummy();
  576. EXPECT_EQ(1, a1.Perform(make_tuple(true)));
  577. EXPECT_EQ(1, a1.Perform(make_tuple(false)));
  578. }
  579. // Tests that the body of ACTION() can reference the mock function's
  580. // return type.
  581. ACTION(InvokeDummy2) {
  582. StaticAssertTypeEq<int, return_type>();
  583. return_type result = Dummy(true);
  584. return result;
  585. }
  586. TEST(ActionMacroTest, CanReferenceMockFunctionReturnType) {
  587. Action<int(bool)> a1 = InvokeDummy2();
  588. EXPECT_EQ(1, a1.Perform(make_tuple(true)));
  589. EXPECT_EQ(1, a1.Perform(make_tuple(false)));
  590. }
  591. // Tests that ACTION() works for arguments passed by const reference.
  592. ACTION(ReturnAddrOfConstBoolReferenceArg) {
  593. StaticAssertTypeEq<const bool&, arg1_type>();
  594. return &arg1;
  595. }
  596. TEST(ActionMacroTest, WorksForConstReferenceArg) {
  597. Action<const bool*(int, const bool&)> a = ReturnAddrOfConstBoolReferenceArg();
  598. const bool b = false;
  599. EXPECT_EQ(&b, a.Perform(tuple<int, const bool&>(0, b)));
  600. }
  601. // Tests that ACTION() works for arguments passed by non-const reference.
  602. ACTION(ReturnAddrOfIntReferenceArg) {
  603. StaticAssertTypeEq<int&, arg0_type>();
  604. return &arg0;
  605. }
  606. TEST(ActionMacroTest, WorksForNonConstReferenceArg) {
  607. Action<int*(int&, bool, int)> a = ReturnAddrOfIntReferenceArg();
  608. int n = 0;
  609. EXPECT_EQ(&n, a.Perform(tuple<int&, bool, int>(n, true, 1)));
  610. }
  611. // Tests that ACTION() can be used in a namespace.
  612. namespace action_test {
  613. ACTION(Sum) { return arg0 + arg1; }
  614. } // namespace action_test
  615. TEST(ActionMacroTest, WorksInNamespace) {
  616. Action<int(int, int)> a1 = action_test::Sum();
  617. EXPECT_EQ(3, a1.Perform(make_tuple(1, 2)));
  618. }
  619. // Tests that the same ACTION definition works for mock functions with
  620. // different argument numbers.
  621. ACTION(PlusTwo) { return arg0 + 2; }
  622. TEST(ActionMacroTest, WorksForDifferentArgumentNumbers) {
  623. Action<int(int)> a1 = PlusTwo();
  624. EXPECT_EQ(4, a1.Perform(make_tuple(2)));
  625. Action<double(float, void*)> a2 = PlusTwo();
  626. int dummy;
  627. EXPECT_DOUBLE_EQ(6, a2.Perform(make_tuple(4.0f, &dummy)));
  628. }
  629. // Tests that ACTION_P can define a parameterized action.
  630. ACTION_P(Plus, n) { return arg0 + n; }
  631. TEST(ActionPMacroTest, DefinesParameterizedAction) {
  632. Action<int(int m, bool t)> a1 = Plus(9);
  633. EXPECT_EQ(10, a1.Perform(make_tuple(1, true)));
  634. }
  635. // Tests that the body of ACTION_P can reference the argument types
  636. // and the parameter type.
  637. ACTION_P(TypedPlus, n) {
  638. arg0_type t1 = arg0;
  639. n_type t2 = n;
  640. return t1 + t2;
  641. }
  642. TEST(ActionPMacroTest, CanReferenceArgumentAndParameterTypes) {
  643. Action<int(char m, bool t)> a1 = TypedPlus(9);
  644. EXPECT_EQ(10, a1.Perform(make_tuple(Char(1), true)));
  645. }
  646. // Tests that a parameterized action can be used in any mock function
  647. // whose type is compatible.
  648. TEST(ActionPMacroTest, WorksInCompatibleMockFunction) {
  649. Action<std::string(const std::string& s)> a1 = Plus("tail");
  650. const std::string re = "re";
  651. tuple<const std::string> dummy = make_tuple(re);
  652. EXPECT_EQ("retail", a1.Perform(dummy));
  653. }
  654. // Tests that we can use ACTION*() to define actions overloaded on the
  655. // number of parameters.
  656. ACTION(OverloadedAction) { return arg0 ? arg1 : "hello"; }
  657. ACTION_P(OverloadedAction, default_value) {
  658. return arg0 ? arg1 : default_value;
  659. }
  660. ACTION_P2(OverloadedAction, true_value, false_value) {
  661. return arg0 ? true_value : false_value;
  662. }
  663. TEST(ActionMacroTest, CanDefineOverloadedActions) {
  664. typedef Action<const char*(bool, const char*)> MyAction;
  665. const MyAction a1 = OverloadedAction();
  666. EXPECT_STREQ("hello", a1.Perform(make_tuple(false, CharPtr("world"))));
  667. EXPECT_STREQ("world", a1.Perform(make_tuple(true, CharPtr("world"))));
  668. const MyAction a2 = OverloadedAction("hi");
  669. EXPECT_STREQ("hi", a2.Perform(make_tuple(false, CharPtr("world"))));
  670. EXPECT_STREQ("world", a2.Perform(make_tuple(true, CharPtr("world"))));
  671. const MyAction a3 = OverloadedAction("hi", "you");
  672. EXPECT_STREQ("hi", a3.Perform(make_tuple(true, CharPtr("world"))));
  673. EXPECT_STREQ("you", a3.Perform(make_tuple(false, CharPtr("world"))));
  674. }
  675. // Tests ACTION_Pn where n >= 3.
  676. ACTION_P3(Plus, m, n, k) { return arg0 + m + n + k; }
  677. TEST(ActionPnMacroTest, WorksFor3Parameters) {
  678. Action<double(int m, bool t)> a1 = Plus(100, 20, 3.4);
  679. EXPECT_DOUBLE_EQ(3123.4, a1.Perform(make_tuple(3000, true)));
  680. Action<std::string(const std::string& s)> a2 = Plus("tail", "-", ">");
  681. const std::string re = "re";
  682. tuple<const std::string> dummy = make_tuple(re);
  683. EXPECT_EQ("retail->", a2.Perform(dummy));
  684. }
  685. ACTION_P4(Plus, p0, p1, p2, p3) { return arg0 + p0 + p1 + p2 + p3; }
  686. TEST(ActionPnMacroTest, WorksFor4Parameters) {
  687. Action<int(int)> a1 = Plus(1, 2, 3, 4);
  688. EXPECT_EQ(10 + 1 + 2 + 3 + 4, a1.Perform(make_tuple(10)));
  689. }
  690. ACTION_P5(Plus, p0, p1, p2, p3, p4) { return arg0 + p0 + p1 + p2 + p3 + p4; }
  691. TEST(ActionPnMacroTest, WorksFor5Parameters) {
  692. Action<int(int)> a1 = Plus(1, 2, 3, 4, 5);
  693. EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5, a1.Perform(make_tuple(10)));
  694. }
  695. ACTION_P6(Plus, p0, p1, p2, p3, p4, p5) {
  696. return arg0 + p0 + p1 + p2 + p3 + p4 + p5;
  697. }
  698. TEST(ActionPnMacroTest, WorksFor6Parameters) {
  699. Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6);
  700. EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6, a1.Perform(make_tuple(10)));
  701. }
  702. ACTION_P7(Plus, p0, p1, p2, p3, p4, p5, p6) {
  703. return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6;
  704. }
  705. TEST(ActionPnMacroTest, WorksFor7Parameters) {
  706. Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7);
  707. EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7, a1.Perform(make_tuple(10)));
  708. }
  709. ACTION_P8(Plus, p0, p1, p2, p3, p4, p5, p6, p7) {
  710. return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7;
  711. }
  712. TEST(ActionPnMacroTest, WorksFor8Parameters) {
  713. Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8);
  714. EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8, a1.Perform(make_tuple(10)));
  715. }
  716. ACTION_P9(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8) {
  717. return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8;
  718. }
  719. TEST(ActionPnMacroTest, WorksFor9Parameters) {
  720. Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9);
  721. EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9, a1.Perform(make_tuple(10)));
  722. }
  723. ACTION_P10(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8, last_param) {
  724. arg0_type t0 = arg0;
  725. last_param_type t9 = last_param;
  726. return t0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + t9;
  727. }
  728. TEST(ActionPnMacroTest, WorksFor10Parameters) {
  729. Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
  730. EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10,
  731. a1.Perform(make_tuple(10)));
  732. }
  733. // Tests that the action body can promote the parameter types.
  734. ACTION_P2(PadArgument, prefix, suffix) {
  735. // The following lines promote the two parameters to desired types.
  736. std::string prefix_str(prefix);
  737. char suffix_char = static_cast<char>(suffix);
  738. return prefix_str + arg0 + suffix_char;
  739. }
  740. TEST(ActionPnMacroTest, SimpleTypePromotion) {
  741. Action<std::string(const char*)> no_promo =
  742. PadArgument(std::string("foo"), 'r');
  743. Action<std::string(const char*)> promo =
  744. PadArgument("foo", static_cast<int>('r'));
  745. EXPECT_EQ("foobar", no_promo.Perform(make_tuple(CharPtr("ba"))));
  746. EXPECT_EQ("foobar", promo.Perform(make_tuple(CharPtr("ba"))));
  747. }
  748. // Tests that we can partially restrict parameter types using a
  749. // straight-forward pattern.
  750. // Defines a generic action that doesn't restrict the types of its
  751. // parameters.
  752. ACTION_P3(ConcatImpl, a, b, c) {
  753. std::stringstream ss;
  754. ss << a << b << c;
  755. return ss.str();
  756. }
  757. // Next, we try to restrict that either the first parameter is a
  758. // string, or the second parameter is an int.
  759. // Defines a partially specialized wrapper that restricts the first
  760. // parameter to std::string.
  761. template <typename T1, typename T2>
  762. // ConcatImplActionP3 is the class template ACTION_P3 uses to
  763. // implement ConcatImpl. We shouldn't change the name as this
  764. // pattern requires the user to use it directly.
  765. ConcatImplActionP3<std::string, T1, T2>
  766. Concat(const std::string& a, T1 b, T2 c) {
  767. GTEST_INTENTIONAL_CONST_COND_PUSH_()
  768. if (true) {
  769. GTEST_INTENTIONAL_CONST_COND_POP_()
  770. // This branch verifies that ConcatImpl() can be invoked without
  771. // explicit template arguments.
  772. return ConcatImpl(a, b, c);
  773. } else {
  774. // This branch verifies that ConcatImpl() can also be invoked with
  775. // explicit template arguments. It doesn't really need to be
  776. // executed as this is a compile-time verification.
  777. return ConcatImpl<std::string, T1, T2>(a, b, c);
  778. }
  779. }
  780. // Defines another partially specialized wrapper that restricts the
  781. // second parameter to int.
  782. template <typename T1, typename T2>
  783. ConcatImplActionP3<T1, int, T2>
  784. Concat(T1 a, int b, T2 c) {
  785. return ConcatImpl(a, b, c);
  786. }
  787. TEST(ActionPnMacroTest, CanPartiallyRestrictParameterTypes) {
  788. Action<const std::string()> a1 = Concat("Hello", "1", 2);
  789. EXPECT_EQ("Hello12", a1.Perform(make_tuple()));
  790. a1 = Concat(1, 2, 3);
  791. EXPECT_EQ("123", a1.Perform(make_tuple()));
  792. }
  793. // Verifies the type of an ACTION*.
  794. ACTION(DoFoo) {}
  795. ACTION_P(DoFoo, p) {}
  796. ACTION_P2(DoFoo, p0, p1) {}
  797. TEST(ActionPnMacroTest, TypesAreCorrect) {
  798. // DoFoo() must be assignable to a DoFooAction variable.
  799. DoFooAction a0 = DoFoo();
  800. // DoFoo(1) must be assignable to a DoFooActionP variable.
  801. DoFooActionP<int> a1 = DoFoo(1);
  802. // DoFoo(p1, ..., pk) must be assignable to a DoFooActionPk
  803. // variable, and so on.
  804. DoFooActionP2<int, char> a2 = DoFoo(1, '2');
  805. PlusActionP3<int, int, char> a3 = Plus(1, 2, '3');
  806. PlusActionP4<int, int, int, char> a4 = Plus(1, 2, 3, '4');
  807. PlusActionP5<int, int, int, int, char> a5 = Plus(1, 2, 3, 4, '5');
  808. PlusActionP6<int, int, int, int, int, char> a6 = Plus(1, 2, 3, 4, 5, '6');
  809. PlusActionP7<int, int, int, int, int, int, char> a7 =
  810. Plus(1, 2, 3, 4, 5, 6, '7');
  811. PlusActionP8<int, int, int, int, int, int, int, char> a8 =
  812. Plus(1, 2, 3, 4, 5, 6, 7, '8');
  813. PlusActionP9<int, int, int, int, int, int, int, int, char> a9 =
  814. Plus(1, 2, 3, 4, 5, 6, 7, 8, '9');
  815. PlusActionP10<int, int, int, int, int, int, int, int, int, char> a10 =
  816. Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
  817. // Avoid "unused variable" warnings.
  818. (void)a0;
  819. (void)a1;
  820. (void)a2;
  821. (void)a3;
  822. (void)a4;
  823. (void)a5;
  824. (void)a6;
  825. (void)a7;
  826. (void)a8;
  827. (void)a9;
  828. (void)a10;
  829. }
  830. // Tests that an ACTION_P*() action can be explicitly instantiated
  831. // with reference-typed parameters.
  832. ACTION_P(Plus1, x) { return x; }
  833. ACTION_P2(Plus2, x, y) { return x + y; }
  834. ACTION_P3(Plus3, x, y, z) { return x + y + z; }
  835. ACTION_P10(Plus10, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
  836. return a0 + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9;
  837. }
  838. TEST(ActionPnMacroTest, CanExplicitlyInstantiateWithReferenceTypes) {
  839. int x = 1, y = 2, z = 3;
  840. const tuple<> empty = make_tuple();
  841. Action<int()> a = Plus1<int&>(x);
  842. EXPECT_EQ(1, a.Perform(empty));
  843. a = Plus2<const int&, int&>(x, y);
  844. EXPECT_EQ(3, a.Perform(empty));
  845. a = Plus3<int&, const int&, int&>(x, y, z);
  846. EXPECT_EQ(6, a.Perform(empty));
  847. int n[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  848. a = Plus10<const int&, int&, const int&, int&, const int&, int&, const int&,
  849. int&, const int&, int&>(n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7],
  850. n[8], n[9]);
  851. EXPECT_EQ(55, a.Perform(empty));
  852. }
  853. class NullaryConstructorClass {
  854. public:
  855. NullaryConstructorClass() : value_(123) {}
  856. int value_;
  857. };
  858. // Tests using ReturnNew() with a nullary constructor.
  859. TEST(ReturnNewTest, NoArgs) {
  860. Action<NullaryConstructorClass*()> a = ReturnNew<NullaryConstructorClass>();
  861. NullaryConstructorClass* c = a.Perform(make_tuple());
  862. EXPECT_EQ(123, c->value_);
  863. delete c;
  864. }
  865. class UnaryConstructorClass {
  866. public:
  867. explicit UnaryConstructorClass(int value) : value_(value) {}
  868. int value_;
  869. };
  870. // Tests using ReturnNew() with a unary constructor.
  871. TEST(ReturnNewTest, Unary) {
  872. Action<UnaryConstructorClass*()> a = ReturnNew<UnaryConstructorClass>(4000);
  873. UnaryConstructorClass* c = a.Perform(make_tuple());
  874. EXPECT_EQ(4000, c->value_);
  875. delete c;
  876. }
  877. TEST(ReturnNewTest, UnaryWorksWhenMockMethodHasArgs) {
  878. Action<UnaryConstructorClass*(bool, int)> a =
  879. ReturnNew<UnaryConstructorClass>(4000);
  880. UnaryConstructorClass* c = a.Perform(make_tuple(false, 5));
  881. EXPECT_EQ(4000, c->value_);
  882. delete c;
  883. }
  884. TEST(ReturnNewTest, UnaryWorksWhenMockMethodReturnsPointerToConst) {
  885. Action<const UnaryConstructorClass*()> a =
  886. ReturnNew<UnaryConstructorClass>(4000);
  887. const UnaryConstructorClass* c = a.Perform(make_tuple());
  888. EXPECT_EQ(4000, c->value_);
  889. delete c;
  890. }
  891. class TenArgConstructorClass {
  892. public:
  893. TenArgConstructorClass(int a1, int a2, int a3, int a4, int a5,
  894. int a6, int a7, int a8, int a9, int a10)
  895. : value_(a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10) {
  896. }
  897. int value_;
  898. };
  899. // Tests using ReturnNew() with a 10-argument constructor.
  900. TEST(ReturnNewTest, ConstructorThatTakes10Arguments) {
  901. Action<TenArgConstructorClass*()> a =
  902. ReturnNew<TenArgConstructorClass>(1000000000, 200000000, 30000000,
  903. 4000000, 500000, 60000,
  904. 7000, 800, 90, 0);
  905. TenArgConstructorClass* c = a.Perform(make_tuple());
  906. EXPECT_EQ(1234567890, c->value_);
  907. delete c;
  908. }
  909. // Tests that ACTION_TEMPLATE works when there is no value parameter.
  910. ACTION_TEMPLATE(CreateNew,
  911. HAS_1_TEMPLATE_PARAMS(typename, T),
  912. AND_0_VALUE_PARAMS()) {
  913. return new T;
  914. }
  915. TEST(ActionTemplateTest, WorksWithoutValueParam) {
  916. const Action<int*()> a = CreateNew<int>();
  917. int* p = a.Perform(make_tuple());
  918. delete p;
  919. }
  920. // Tests that ACTION_TEMPLATE works when there are value parameters.
  921. ACTION_TEMPLATE(CreateNew,
  922. HAS_1_TEMPLATE_PARAMS(typename, T),
  923. AND_1_VALUE_PARAMS(a0)) {
  924. return new T(a0);
  925. }
  926. TEST(ActionTemplateTest, WorksWithValueParams) {
  927. const Action<int*()> a = CreateNew<int>(42);
  928. int* p = a.Perform(make_tuple());
  929. EXPECT_EQ(42, *p);
  930. delete p;
  931. }
  932. // Tests that ACTION_TEMPLATE works for integral template parameters.
  933. ACTION_TEMPLATE(MyDeleteArg,
  934. HAS_1_TEMPLATE_PARAMS(int, k),
  935. AND_0_VALUE_PARAMS()) {
  936. delete get<k>(args);
  937. }
  938. // Resets a bool variable in the destructor.
  939. class BoolResetter {
  940. public:
  941. explicit BoolResetter(bool* value) : value_(value) {}
  942. ~BoolResetter() { *value_ = false; }
  943. private:
  944. bool* value_;
  945. };
  946. TEST(ActionTemplateTest, WorksForIntegralTemplateParams) {
  947. const Action<void(int*, BoolResetter*)> a = MyDeleteArg<1>();
  948. int n = 0;
  949. bool b = true;
  950. BoolResetter* resetter = new BoolResetter(&b);
  951. a.Perform(make_tuple(&n, resetter));
  952. EXPECT_FALSE(b); // Verifies that resetter is deleted.
  953. }
  954. // Tests that ACTION_TEMPLATES works for template template parameters.
  955. ACTION_TEMPLATE(ReturnSmartPointer,
  956. HAS_1_TEMPLATE_PARAMS(template <typename Pointee> class,
  957. Pointer),
  958. AND_1_VALUE_PARAMS(pointee)) {
  959. return Pointer<pointee_type>(new pointee_type(pointee));
  960. }
  961. TEST(ActionTemplateTest, WorksForTemplateTemplateParameters) {
  962. using ::testing::internal::linked_ptr;
  963. const Action<linked_ptr<int>()> a = ReturnSmartPointer<linked_ptr>(42);
  964. linked_ptr<int> p = a.Perform(make_tuple());
  965. EXPECT_EQ(42, *p);
  966. }
  967. // Tests that ACTION_TEMPLATE works for 10 template parameters.
  968. template <typename T1, typename T2, typename T3, int k4, bool k5,
  969. unsigned int k6, typename T7, typename T8, typename T9>
  970. struct GiantTemplate {
  971. public:
  972. explicit GiantTemplate(int a_value) : value(a_value) {}
  973. int value;
  974. };
  975. ACTION_TEMPLATE(ReturnGiant,
  976. HAS_10_TEMPLATE_PARAMS(
  977. typename, T1,
  978. typename, T2,
  979. typename, T3,
  980. int, k4,
  981. bool, k5,
  982. unsigned int, k6,
  983. class, T7,
  984. class, T8,
  985. class, T9,
  986. template <typename T> class, T10),
  987. AND_1_VALUE_PARAMS(value)) {
  988. return GiantTemplate<T10<T1>, T2, T3, k4, k5, k6, T7, T8, T9>(value);
  989. }
  990. TEST(ActionTemplateTest, WorksFor10TemplateParameters) {
  991. using ::testing::internal::linked_ptr;
  992. typedef GiantTemplate<linked_ptr<int>, bool, double, 5,
  993. true, 6, char, unsigned, int> Giant;
  994. const Action<Giant()> a = ReturnGiant<
  995. int, bool, double, 5, true, 6, char, unsigned, int, linked_ptr>(42);
  996. Giant giant = a.Perform(make_tuple());
  997. EXPECT_EQ(42, giant.value);
  998. }
  999. // Tests that ACTION_TEMPLATE works for 10 value parameters.
  1000. ACTION_TEMPLATE(ReturnSum,
  1001. HAS_1_TEMPLATE_PARAMS(typename, Number),
  1002. AND_10_VALUE_PARAMS(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10)) {
  1003. return static_cast<Number>(v1) + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10;
  1004. }
  1005. TEST(ActionTemplateTest, WorksFor10ValueParameters) {
  1006. const Action<int()> a = ReturnSum<int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
  1007. EXPECT_EQ(55, a.Perform(make_tuple()));
  1008. }
  1009. // Tests that ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded
  1010. // on the number of value parameters.
  1011. ACTION(ReturnSum) { return 0; }
  1012. ACTION_P(ReturnSum, x) { return x; }
  1013. ACTION_TEMPLATE(ReturnSum,
  1014. HAS_1_TEMPLATE_PARAMS(typename, Number),
  1015. AND_2_VALUE_PARAMS(v1, v2)) {
  1016. return static_cast<Number>(v1) + v2;
  1017. }
  1018. ACTION_TEMPLATE(ReturnSum,
  1019. HAS_1_TEMPLATE_PARAMS(typename, Number),
  1020. AND_3_VALUE_PARAMS(v1, v2, v3)) {
  1021. return static_cast<Number>(v1) + v2 + v3;
  1022. }
  1023. ACTION_TEMPLATE(ReturnSum,
  1024. HAS_2_TEMPLATE_PARAMS(typename, Number, int, k),
  1025. AND_4_VALUE_PARAMS(v1, v2, v3, v4)) {
  1026. return static_cast<Number>(v1) + v2 + v3 + v4 + k;
  1027. }
  1028. TEST(ActionTemplateTest, CanBeOverloadedOnNumberOfValueParameters) {
  1029. const Action<int()> a0 = ReturnSum();
  1030. const Action<int()> a1 = ReturnSum(1);
  1031. const Action<int()> a2 = ReturnSum<int>(1, 2);
  1032. const Action<int()> a3 = ReturnSum<int>(1, 2, 3);
  1033. const Action<int()> a4 = ReturnSum<int, 10000>(2000, 300, 40, 5);
  1034. EXPECT_EQ(0, a0.Perform(make_tuple()));
  1035. EXPECT_EQ(1, a1.Perform(make_tuple()));
  1036. EXPECT_EQ(3, a2.Perform(make_tuple()));
  1037. EXPECT_EQ(6, a3.Perform(make_tuple()));
  1038. EXPECT_EQ(12345, a4.Perform(make_tuple()));
  1039. }
  1040. #ifdef _MSC_VER
  1041. # pragma warning(pop)
  1042. #endif
  1043. } // namespace gmock_generated_actions_test
  1044. } // namespace testing