gmock-more-actions_test.cc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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-more-actions.h.
  32. #include "gmock/gmock-more-actions.h"
  33. #include <functional>
  34. #include <sstream>
  35. #include <string>
  36. #include "gmock/gmock.h"
  37. #include "gtest/gtest.h"
  38. #include "gtest/internal/gtest-linked_ptr.h"
  39. namespace testing {
  40. namespace gmock_more_actions_test {
  41. using ::std::plus;
  42. using ::std::string;
  43. using testing::get;
  44. using testing::make_tuple;
  45. using testing::tuple;
  46. using testing::tuple_element;
  47. using testing::_;
  48. using testing::Action;
  49. using testing::ActionInterface;
  50. using testing::DeleteArg;
  51. using testing::Invoke;
  52. using testing::Return;
  53. using testing::ReturnArg;
  54. using testing::ReturnPointee;
  55. using testing::SaveArg;
  56. using testing::SaveArgPointee;
  57. using testing::SetArgReferee;
  58. using testing::StaticAssertTypeEq;
  59. using testing::Unused;
  60. using testing::WithArg;
  61. using testing::WithoutArgs;
  62. using testing::internal::linked_ptr;
  63. // For suppressing compiler warnings on conversion possibly losing precision.
  64. inline short Short(short n) { return n; } // NOLINT
  65. inline char Char(char ch) { return ch; }
  66. // Sample functions and functors for testing Invoke() and etc.
  67. int Nullary() { return 1; }
  68. class NullaryFunctor {
  69. public:
  70. int operator()() { return 2; }
  71. };
  72. bool g_done = false;
  73. void VoidNullary() { g_done = true; }
  74. class VoidNullaryFunctor {
  75. public:
  76. void operator()() { g_done = true; }
  77. };
  78. bool Unary(int x) { return x < 0; }
  79. const char* Plus1(const char* s) { return s + 1; }
  80. void VoidUnary(int /* n */) { g_done = true; }
  81. bool ByConstRef(const std::string& s) { return s == "Hi"; }
  82. const double g_double = 0;
  83. bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
  84. std::string ByNonConstRef(std::string& s) { return s += "+"; } // NOLINT
  85. struct UnaryFunctor {
  86. int operator()(bool x) { return x ? 1 : -1; }
  87. };
  88. const char* Binary(const char* input, short n) { return input + n; } // NOLINT
  89. void VoidBinary(int, char) { g_done = true; }
  90. int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT
  91. void VoidTernary(int, char, bool) { g_done = true; }
  92. int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
  93. int SumOfFirst2(int a, int b, Unused, Unused) { return a + b; }
  94. void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; }
  95. std::string Concat4(const char* s1, const char* s2, const char* s3,
  96. const char* s4) {
  97. return std::string(s1) + s2 + s3 + s4;
  98. }
  99. int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
  100. struct SumOf5Functor {
  101. int operator()(int a, int b, int c, int d, int e) {
  102. return a + b + c + d + e;
  103. }
  104. };
  105. std::string Concat5(const char* s1, const char* s2, const char* s3,
  106. const char* s4, const char* s5) {
  107. return std::string(s1) + s2 + s3 + s4 + s5;
  108. }
  109. int SumOf6(int a, int b, int c, int d, int e, int f) {
  110. return a + b + c + d + e + f;
  111. }
  112. struct SumOf6Functor {
  113. int operator()(int a, int b, int c, int d, int e, int f) {
  114. return a + b + c + d + e + f;
  115. }
  116. };
  117. std::string Concat6(const char* s1, const char* s2, const char* s3,
  118. const char* s4, const char* s5, const char* s6) {
  119. return std::string(s1) + s2 + s3 + s4 + s5 + s6;
  120. }
  121. std::string Concat7(const char* s1, const char* s2, const char* s3,
  122. const char* s4, const char* s5, const char* s6,
  123. const char* s7) {
  124. return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
  125. }
  126. std::string Concat8(const char* s1, const char* s2, const char* s3,
  127. const char* s4, const char* s5, const char* s6,
  128. const char* s7, const char* s8) {
  129. return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
  130. }
  131. std::string Concat9(const char* s1, const char* s2, const char* s3,
  132. const char* s4, const char* s5, const char* s6,
  133. const char* s7, const char* s8, const char* s9) {
  134. return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
  135. }
  136. std::string Concat10(const char* s1, const char* s2, const char* s3,
  137. const char* s4, const char* s5, const char* s6,
  138. const char* s7, const char* s8, const char* s9,
  139. const char* s10) {
  140. return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
  141. }
  142. class Foo {
  143. public:
  144. Foo() : value_(123) {}
  145. int Nullary() const { return value_; }
  146. short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT
  147. std::string Binary(const std::string& str, char c) const { return str + c; }
  148. int Ternary(int x, bool y, char z) { return value_ + x + y*z; }
  149. int SumOf4(int a, int b, int c, int d) const {
  150. return a + b + c + d + value_;
  151. }
  152. int SumOfLast2(Unused, Unused, int a, int b) const { return a + b; }
  153. int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
  154. int SumOf6(int a, int b, int c, int d, int e, int f) {
  155. return a + b + c + d + e + f;
  156. }
  157. std::string Concat7(const char* s1, const char* s2, const char* s3,
  158. const char* s4, const char* s5, const char* s6,
  159. const char* s7) {
  160. return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
  161. }
  162. std::string Concat8(const char* s1, const char* s2, const char* s3,
  163. const char* s4, const char* s5, const char* s6,
  164. const char* s7, const char* s8) {
  165. return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
  166. }
  167. std::string Concat9(const char* s1, const char* s2, const char* s3,
  168. const char* s4, const char* s5, const char* s6,
  169. const char* s7, const char* s8, const char* s9) {
  170. return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
  171. }
  172. std::string Concat10(const char* s1, const char* s2, const char* s3,
  173. const char* s4, const char* s5, const char* s6,
  174. const char* s7, const char* s8, const char* s9,
  175. const char* s10) {
  176. return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
  177. }
  178. private:
  179. int value_;
  180. };
  181. // Tests using Invoke() with a nullary function.
  182. TEST(InvokeTest, Nullary) {
  183. Action<int()> a = Invoke(Nullary); // NOLINT
  184. EXPECT_EQ(1, a.Perform(make_tuple()));
  185. }
  186. // Tests using Invoke() with a unary function.
  187. TEST(InvokeTest, Unary) {
  188. Action<bool(int)> a = Invoke(Unary); // NOLINT
  189. EXPECT_FALSE(a.Perform(make_tuple(1)));
  190. EXPECT_TRUE(a.Perform(make_tuple(-1)));
  191. }
  192. // Tests using Invoke() with a binary function.
  193. TEST(InvokeTest, Binary) {
  194. Action<const char*(const char*, short)> a = Invoke(Binary); // NOLINT
  195. const char* p = "Hello";
  196. EXPECT_EQ(p + 2, a.Perform(make_tuple(p, Short(2))));
  197. }
  198. // Tests using Invoke() with a ternary function.
  199. TEST(InvokeTest, Ternary) {
  200. Action<int(int, char, short)> a = Invoke(Ternary); // NOLINT
  201. EXPECT_EQ(6, a.Perform(make_tuple(1, '\2', Short(3))));
  202. }
  203. // Tests using Invoke() with a 4-argument function.
  204. TEST(InvokeTest, FunctionThatTakes4Arguments) {
  205. Action<int(int, int, int, int)> a = Invoke(SumOf4); // NOLINT
  206. EXPECT_EQ(1234, a.Perform(make_tuple(1000, 200, 30, 4)));
  207. }
  208. // Tests using Invoke() with a 5-argument function.
  209. TEST(InvokeTest, FunctionThatTakes5Arguments) {
  210. Action<int(int, int, int, int, int)> a = Invoke(SumOf5); // NOLINT
  211. EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5)));
  212. }
  213. // Tests using Invoke() with a 6-argument function.
  214. TEST(InvokeTest, FunctionThatTakes6Arguments) {
  215. Action<int(int, int, int, int, int, int)> a = Invoke(SumOf6); // NOLINT
  216. EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6)));
  217. }
  218. // A helper that turns the type of a C-string literal from const
  219. // char[N] to const char*.
  220. inline const char* CharPtr(const char* s) { return s; }
  221. // Tests using Invoke() with a 7-argument function.
  222. TEST(InvokeTest, FunctionThatTakes7Arguments) {
  223. Action<std::string(const char*, const char*, const char*, const char*,
  224. const char*, const char*, const char*)>
  225. a = Invoke(Concat7);
  226. EXPECT_EQ("1234567",
  227. a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
  228. CharPtr("4"), CharPtr("5"), CharPtr("6"),
  229. CharPtr("7"))));
  230. }
  231. // Tests using Invoke() with a 8-argument function.
  232. TEST(InvokeTest, FunctionThatTakes8Arguments) {
  233. Action<std::string(const char*, const char*, const char*, const char*,
  234. const char*, const char*, const char*, const char*)>
  235. a = Invoke(Concat8);
  236. EXPECT_EQ("12345678",
  237. a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
  238. CharPtr("4"), CharPtr("5"), CharPtr("6"),
  239. CharPtr("7"), CharPtr("8"))));
  240. }
  241. // Tests using Invoke() with a 9-argument function.
  242. TEST(InvokeTest, FunctionThatTakes9Arguments) {
  243. Action<std::string(const char*, const char*, const char*, const char*,
  244. const char*, const char*, const char*, const char*,
  245. const char*)>
  246. a = Invoke(Concat9);
  247. EXPECT_EQ("123456789",
  248. a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
  249. CharPtr("4"), CharPtr("5"), CharPtr("6"),
  250. CharPtr("7"), CharPtr("8"), CharPtr("9"))));
  251. }
  252. // Tests using Invoke() with a 10-argument function.
  253. TEST(InvokeTest, FunctionThatTakes10Arguments) {
  254. Action<std::string(const char*, const char*, const char*, const char*,
  255. const char*, const char*, const char*, const char*,
  256. const char*, const char*)>
  257. a = Invoke(Concat10);
  258. EXPECT_EQ("1234567890",
  259. a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
  260. CharPtr("4"), CharPtr("5"), CharPtr("6"),
  261. CharPtr("7"), CharPtr("8"), CharPtr("9"),
  262. CharPtr("0"))));
  263. }
  264. // Tests using Invoke() with functions with parameters declared as Unused.
  265. TEST(InvokeTest, FunctionWithUnusedParameters) {
  266. Action<int(int, int, double, const std::string&)> a1 = Invoke(SumOfFirst2);
  267. tuple<int, int, double, std::string> dummy =
  268. make_tuple(10, 2, 5.6, std::string("hi"));
  269. EXPECT_EQ(12, a1.Perform(dummy));
  270. Action<int(int, int, bool, int*)> a2 =
  271. Invoke(SumOfFirst2);
  272. EXPECT_EQ(23, a2.Perform(make_tuple(20, 3, true, static_cast<int*>(NULL))));
  273. }
  274. // Tests using Invoke() with methods with parameters declared as Unused.
  275. TEST(InvokeTest, MethodWithUnusedParameters) {
  276. Foo foo;
  277. Action<int(std::string, bool, int, int)> a1 = Invoke(&foo, &Foo::SumOfLast2);
  278. EXPECT_EQ(12, a1.Perform(make_tuple(CharPtr("hi"), true, 10, 2)));
  279. Action<int(char, double, int, int)> a2 =
  280. Invoke(&foo, &Foo::SumOfLast2);
  281. EXPECT_EQ(23, a2.Perform(make_tuple('a', 2.5, 20, 3)));
  282. }
  283. // Tests using Invoke() with a functor.
  284. TEST(InvokeTest, Functor) {
  285. Action<long(long, int)> a = Invoke(plus<long>()); // NOLINT
  286. EXPECT_EQ(3L, a.Perform(make_tuple(1, 2)));
  287. }
  288. // Tests using Invoke(f) as an action of a compatible type.
  289. TEST(InvokeTest, FunctionWithCompatibleType) {
  290. Action<long(int, short, char, bool)> a = Invoke(SumOf4); // NOLINT
  291. EXPECT_EQ(4321, a.Perform(make_tuple(4000, Short(300), Char(20), true)));
  292. }
  293. // Tests using Invoke() with an object pointer and a method pointer.
  294. // Tests using Invoke() with a nullary method.
  295. TEST(InvokeMethodTest, Nullary) {
  296. Foo foo;
  297. Action<int()> a = Invoke(&foo, &Foo::Nullary); // NOLINT
  298. EXPECT_EQ(123, a.Perform(make_tuple()));
  299. }
  300. // Tests using Invoke() with a unary method.
  301. TEST(InvokeMethodTest, Unary) {
  302. Foo foo;
  303. Action<short(long)> a = Invoke(&foo, &Foo::Unary); // NOLINT
  304. EXPECT_EQ(4123, a.Perform(make_tuple(4000)));
  305. }
  306. // Tests using Invoke() with a binary method.
  307. TEST(InvokeMethodTest, Binary) {
  308. Foo foo;
  309. Action<std::string(const std::string&, char)> a = Invoke(&foo, &Foo::Binary);
  310. std::string s("Hell");
  311. tuple<std::string, char> dummy = make_tuple(s, 'o');
  312. EXPECT_EQ("Hello", a.Perform(dummy));
  313. }
  314. // Tests using Invoke() with a ternary method.
  315. TEST(InvokeMethodTest, Ternary) {
  316. Foo foo;
  317. Action<int(int, bool, char)> a = Invoke(&foo, &Foo::Ternary); // NOLINT
  318. EXPECT_EQ(1124, a.Perform(make_tuple(1000, true, Char(1))));
  319. }
  320. // Tests using Invoke() with a 4-argument method.
  321. TEST(InvokeMethodTest, MethodThatTakes4Arguments) {
  322. Foo foo;
  323. Action<int(int, int, int, int)> a = Invoke(&foo, &Foo::SumOf4); // NOLINT
  324. EXPECT_EQ(1357, a.Perform(make_tuple(1000, 200, 30, 4)));
  325. }
  326. // Tests using Invoke() with a 5-argument method.
  327. TEST(InvokeMethodTest, MethodThatTakes5Arguments) {
  328. Foo foo;
  329. Action<int(int, int, int, int, int)> a = Invoke(&foo, &Foo::SumOf5); // NOLINT
  330. EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5)));
  331. }
  332. // Tests using Invoke() with a 6-argument method.
  333. TEST(InvokeMethodTest, MethodThatTakes6Arguments) {
  334. Foo foo;
  335. Action<int(int, int, int, int, int, int)> a = // NOLINT
  336. Invoke(&foo, &Foo::SumOf6);
  337. EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6)));
  338. }
  339. // Tests using Invoke() with a 7-argument method.
  340. TEST(InvokeMethodTest, MethodThatTakes7Arguments) {
  341. Foo foo;
  342. Action<std::string(const char*, const char*, const char*, const char*,
  343. const char*, const char*, const char*)>
  344. a = Invoke(&foo, &Foo::Concat7);
  345. EXPECT_EQ("1234567",
  346. a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
  347. CharPtr("4"), CharPtr("5"), CharPtr("6"),
  348. CharPtr("7"))));
  349. }
  350. // Tests using Invoke() with a 8-argument method.
  351. TEST(InvokeMethodTest, MethodThatTakes8Arguments) {
  352. Foo foo;
  353. Action<std::string(const char*, const char*, const char*, const char*,
  354. const char*, const char*, const char*, const char*)>
  355. a = Invoke(&foo, &Foo::Concat8);
  356. EXPECT_EQ("12345678",
  357. a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
  358. CharPtr("4"), CharPtr("5"), CharPtr("6"),
  359. CharPtr("7"), CharPtr("8"))));
  360. }
  361. // Tests using Invoke() with a 9-argument method.
  362. TEST(InvokeMethodTest, MethodThatTakes9Arguments) {
  363. Foo foo;
  364. Action<std::string(const char*, const char*, const char*, const char*,
  365. const char*, const char*, const char*, const char*,
  366. const char*)>
  367. a = Invoke(&foo, &Foo::Concat9);
  368. EXPECT_EQ("123456789",
  369. a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
  370. CharPtr("4"), CharPtr("5"), CharPtr("6"),
  371. CharPtr("7"), CharPtr("8"), CharPtr("9"))));
  372. }
  373. // Tests using Invoke() with a 10-argument method.
  374. TEST(InvokeMethodTest, MethodThatTakes10Arguments) {
  375. Foo foo;
  376. Action<std::string(const char*, const char*, const char*, const char*,
  377. const char*, const char*, const char*, const char*,
  378. const char*, const char*)>
  379. a = Invoke(&foo, &Foo::Concat10);
  380. EXPECT_EQ("1234567890",
  381. a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
  382. CharPtr("4"), CharPtr("5"), CharPtr("6"),
  383. CharPtr("7"), CharPtr("8"), CharPtr("9"),
  384. CharPtr("0"))));
  385. }
  386. // Tests using Invoke(f) as an action of a compatible type.
  387. TEST(InvokeMethodTest, MethodWithCompatibleType) {
  388. Foo foo;
  389. Action<long(int, short, char, bool)> a = // NOLINT
  390. Invoke(&foo, &Foo::SumOf4);
  391. EXPECT_EQ(4444, a.Perform(make_tuple(4000, Short(300), Char(20), true)));
  392. }
  393. // Tests using WithoutArgs with an action that takes no argument.
  394. TEST(WithoutArgsTest, NoArg) {
  395. Action<int(int n)> a = WithoutArgs(Invoke(Nullary)); // NOLINT
  396. EXPECT_EQ(1, a.Perform(make_tuple(2)));
  397. }
  398. // Tests using WithArg with an action that takes 1 argument.
  399. TEST(WithArgTest, OneArg) {
  400. Action<bool(double x, int n)> b = WithArg<1>(Invoke(Unary)); // NOLINT
  401. EXPECT_TRUE(b.Perform(make_tuple(1.5, -1)));
  402. EXPECT_FALSE(b.Perform(make_tuple(1.5, 1)));
  403. }
  404. TEST(ReturnArgActionTest, WorksForOneArgIntArg0) {
  405. const Action<int(int)> a = ReturnArg<0>();
  406. EXPECT_EQ(5, a.Perform(make_tuple(5)));
  407. }
  408. TEST(ReturnArgActionTest, WorksForMultiArgBoolArg0) {
  409. const Action<bool(bool, bool, bool)> a = ReturnArg<0>();
  410. EXPECT_TRUE(a.Perform(make_tuple(true, false, false)));
  411. }
  412. TEST(ReturnArgActionTest, WorksForMultiArgStringArg2) {
  413. const Action<std::string(int, int, std::string, int)> a = ReturnArg<2>();
  414. EXPECT_EQ("seven", a.Perform(make_tuple(5, 6, std::string("seven"), 8)));
  415. }
  416. TEST(SaveArgActionTest, WorksForSameType) {
  417. int result = 0;
  418. const Action<void(int n)> a1 = SaveArg<0>(&result);
  419. a1.Perform(make_tuple(5));
  420. EXPECT_EQ(5, result);
  421. }
  422. TEST(SaveArgActionTest, WorksForCompatibleType) {
  423. int result = 0;
  424. const Action<void(bool, char)> a1 = SaveArg<1>(&result);
  425. a1.Perform(make_tuple(true, 'a'));
  426. EXPECT_EQ('a', result);
  427. }
  428. TEST(SaveArgPointeeActionTest, WorksForSameType) {
  429. int result = 0;
  430. const int value = 5;
  431. const Action<void(const int*)> a1 = SaveArgPointee<0>(&result);
  432. a1.Perform(make_tuple(&value));
  433. EXPECT_EQ(5, result);
  434. }
  435. TEST(SaveArgPointeeActionTest, WorksForCompatibleType) {
  436. int result = 0;
  437. char value = 'a';
  438. const Action<void(bool, char*)> a1 = SaveArgPointee<1>(&result);
  439. a1.Perform(make_tuple(true, &value));
  440. EXPECT_EQ('a', result);
  441. }
  442. TEST(SaveArgPointeeActionTest, WorksForLinkedPtr) {
  443. int result = 0;
  444. linked_ptr<int> value(new int(5));
  445. const Action<void(linked_ptr<int>)> a1 = SaveArgPointee<0>(&result);
  446. a1.Perform(make_tuple(value));
  447. EXPECT_EQ(5, result);
  448. }
  449. TEST(SetArgRefereeActionTest, WorksForSameType) {
  450. int value = 0;
  451. const Action<void(int&)> a1 = SetArgReferee<0>(1);
  452. a1.Perform(tuple<int&>(value));
  453. EXPECT_EQ(1, value);
  454. }
  455. TEST(SetArgRefereeActionTest, WorksForCompatibleType) {
  456. int value = 0;
  457. const Action<void(int, int&)> a1 = SetArgReferee<1>('a');
  458. a1.Perform(tuple<int, int&>(0, value));
  459. EXPECT_EQ('a', value);
  460. }
  461. TEST(SetArgRefereeActionTest, WorksWithExtraArguments) {
  462. int value = 0;
  463. const Action<void(bool, int, int&, const char*)> a1 = SetArgReferee<2>('a');
  464. a1.Perform(tuple<bool, int, int&, const char*>(true, 0, value, "hi"));
  465. EXPECT_EQ('a', value);
  466. }
  467. // A class that can be used to verify that its destructor is called: it will set
  468. // the bool provided to the constructor to true when destroyed.
  469. class DeletionTester {
  470. public:
  471. explicit DeletionTester(bool* is_deleted)
  472. : is_deleted_(is_deleted) {
  473. // Make sure the bit is set to false.
  474. *is_deleted_ = false;
  475. }
  476. ~DeletionTester() {
  477. *is_deleted_ = true;
  478. }
  479. private:
  480. bool* is_deleted_;
  481. };
  482. TEST(DeleteArgActionTest, OneArg) {
  483. bool is_deleted = false;
  484. DeletionTester* t = new DeletionTester(&is_deleted);
  485. const Action<void(DeletionTester*)> a1 = DeleteArg<0>(); // NOLINT
  486. EXPECT_FALSE(is_deleted);
  487. a1.Perform(make_tuple(t));
  488. EXPECT_TRUE(is_deleted);
  489. }
  490. TEST(DeleteArgActionTest, TenArgs) {
  491. bool is_deleted = false;
  492. DeletionTester* t = new DeletionTester(&is_deleted);
  493. const Action<void(bool, int, int, const char*, bool,
  494. int, int, int, int, DeletionTester*)> a1 = DeleteArg<9>();
  495. EXPECT_FALSE(is_deleted);
  496. a1.Perform(make_tuple(true, 5, 6, CharPtr("hi"), false, 7, 8, 9, 10, t));
  497. EXPECT_TRUE(is_deleted);
  498. }
  499. #if GTEST_HAS_EXCEPTIONS
  500. TEST(ThrowActionTest, ThrowsGivenExceptionInVoidFunction) {
  501. const Action<void(int n)> a = Throw('a');
  502. EXPECT_THROW(a.Perform(make_tuple(0)), char);
  503. }
  504. class MyException {};
  505. TEST(ThrowActionTest, ThrowsGivenExceptionInNonVoidFunction) {
  506. const Action<double(char ch)> a = Throw(MyException());
  507. EXPECT_THROW(a.Perform(make_tuple('0')), MyException);
  508. }
  509. TEST(ThrowActionTest, ThrowsGivenExceptionInNullaryFunction) {
  510. const Action<double()> a = Throw(MyException());
  511. EXPECT_THROW(a.Perform(make_tuple()), MyException);
  512. }
  513. #endif // GTEST_HAS_EXCEPTIONS
  514. // Tests that SetArrayArgument<N>(first, last) sets the elements of the array
  515. // pointed to by the N-th (0-based) argument to values in range [first, last).
  516. TEST(SetArrayArgumentTest, SetsTheNthArray) {
  517. typedef void MyFunction(bool, int*, char*);
  518. int numbers[] = { 1, 2, 3 };
  519. Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers + 3);
  520. int n[4] = {};
  521. int* pn = n;
  522. char ch[4] = {};
  523. char* pch = ch;
  524. a.Perform(make_tuple(true, pn, pch));
  525. EXPECT_EQ(1, n[0]);
  526. EXPECT_EQ(2, n[1]);
  527. EXPECT_EQ(3, n[2]);
  528. EXPECT_EQ(0, n[3]);
  529. EXPECT_EQ('\0', ch[0]);
  530. EXPECT_EQ('\0', ch[1]);
  531. EXPECT_EQ('\0', ch[2]);
  532. EXPECT_EQ('\0', ch[3]);
  533. // Tests first and last are iterators.
  534. std::string letters = "abc";
  535. a = SetArrayArgument<2>(letters.begin(), letters.end());
  536. std::fill_n(n, 4, 0);
  537. std::fill_n(ch, 4, '\0');
  538. a.Perform(make_tuple(true, pn, pch));
  539. EXPECT_EQ(0, n[0]);
  540. EXPECT_EQ(0, n[1]);
  541. EXPECT_EQ(0, n[2]);
  542. EXPECT_EQ(0, n[3]);
  543. EXPECT_EQ('a', ch[0]);
  544. EXPECT_EQ('b', ch[1]);
  545. EXPECT_EQ('c', ch[2]);
  546. EXPECT_EQ('\0', ch[3]);
  547. }
  548. // Tests SetArrayArgument<N>(first, last) where first == last.
  549. TEST(SetArrayArgumentTest, SetsTheNthArrayWithEmptyRange) {
  550. typedef void MyFunction(bool, int*);
  551. int numbers[] = { 1, 2, 3 };
  552. Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers);
  553. int n[4] = {};
  554. int* pn = n;
  555. a.Perform(make_tuple(true, pn));
  556. EXPECT_EQ(0, n[0]);
  557. EXPECT_EQ(0, n[1]);
  558. EXPECT_EQ(0, n[2]);
  559. EXPECT_EQ(0, n[3]);
  560. }
  561. // Tests SetArrayArgument<N>(first, last) where *first is convertible
  562. // (but not equal) to the argument type.
  563. TEST(SetArrayArgumentTest, SetsTheNthArrayWithConvertibleType) {
  564. typedef void MyFunction(bool, int*);
  565. char chars[] = { 97, 98, 99 };
  566. Action<MyFunction> a = SetArrayArgument<1>(chars, chars + 3);
  567. int codes[4] = { 111, 222, 333, 444 };
  568. int* pcodes = codes;
  569. a.Perform(make_tuple(true, pcodes));
  570. EXPECT_EQ(97, codes[0]);
  571. EXPECT_EQ(98, codes[1]);
  572. EXPECT_EQ(99, codes[2]);
  573. EXPECT_EQ(444, codes[3]);
  574. }
  575. // Test SetArrayArgument<N>(first, last) with iterator as argument.
  576. TEST(SetArrayArgumentTest, SetsTheNthArrayWithIteratorArgument) {
  577. typedef void MyFunction(bool, std::back_insert_iterator<std::string>);
  578. std::string letters = "abc";
  579. Action<MyFunction> a = SetArrayArgument<1>(letters.begin(), letters.end());
  580. std::string s;
  581. a.Perform(make_tuple(true, back_inserter(s)));
  582. EXPECT_EQ(letters, s);
  583. }
  584. TEST(ReturnPointeeTest, Works) {
  585. int n = 42;
  586. const Action<int()> a = ReturnPointee(&n);
  587. EXPECT_EQ(42, a.Perform(make_tuple()));
  588. n = 43;
  589. EXPECT_EQ(43, a.Perform(make_tuple()));
  590. }
  591. } // namespace gmock_generated_actions_test
  592. } // namespace testing