gmock-more-actions_test.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. //
  30. // Author: wan@google.com (Zhanyong Wan)
  31. // Google Mock - a framework for writing C++ mock classes.
  32. //
  33. // This file tests the built-in actions in gmock-more-actions.h.
  34. #include "gmock/gmock-more-actions.h"
  35. #include <functional>
  36. #include <sstream>
  37. #include <string>
  38. #include "gmock/gmock.h"
  39. #include "gtest/gtest.h"
  40. #include "gtest/internal/gtest-linked_ptr.h"
  41. namespace testing {
  42. namespace gmock_more_actions_test {
  43. using ::std::plus;
  44. using ::std::string;
  45. using testing::get;
  46. using testing::make_tuple;
  47. using testing::tuple;
  48. using testing::tuple_element;
  49. using testing::_;
  50. using testing::Action;
  51. using testing::ActionInterface;
  52. using testing::DeleteArg;
  53. using testing::Invoke;
  54. using testing::Return;
  55. using testing::ReturnArg;
  56. using testing::ReturnPointee;
  57. using testing::SaveArg;
  58. using testing::SaveArgPointee;
  59. using testing::SetArgReferee;
  60. using testing::StaticAssertTypeEq;
  61. using testing::Unused;
  62. using testing::WithArg;
  63. using testing::WithoutArgs;
  64. using testing::internal::linked_ptr;
  65. // For suppressing compiler warnings on conversion possibly losing precision.
  66. inline short Short(short n) { return n; } // NOLINT
  67. inline char Char(char ch) { return ch; }
  68. // Sample functions and functors for testing Invoke() and etc.
  69. int Nullary() { return 1; }
  70. class NullaryFunctor {
  71. public:
  72. int operator()() { return 2; }
  73. };
  74. bool g_done = false;
  75. void VoidNullary() { g_done = true; }
  76. class VoidNullaryFunctor {
  77. public:
  78. void operator()() { g_done = true; }
  79. };
  80. bool Unary(int x) { return x < 0; }
  81. const char* Plus1(const char* s) { return s + 1; }
  82. void VoidUnary(int /* n */) { g_done = true; }
  83. bool ByConstRef(const string& s) { return s == "Hi"; }
  84. const double g_double = 0;
  85. bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
  86. string ByNonConstRef(string& s) { return s += "+"; } // NOLINT
  87. struct UnaryFunctor {
  88. int operator()(bool x) { return x ? 1 : -1; }
  89. };
  90. const char* Binary(const char* input, short n) { return input + n; } // NOLINT
  91. void VoidBinary(int, char) { g_done = true; }
  92. int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT
  93. void VoidTernary(int, char, bool) { g_done = true; }
  94. int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
  95. int SumOfFirst2(int a, int b, Unused, Unused) { return a + b; }
  96. void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; }
  97. string Concat4(const char* s1, const char* s2, const char* s3,
  98. const char* s4) {
  99. return string(s1) + s2 + s3 + s4;
  100. }
  101. int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
  102. struct SumOf5Functor {
  103. int operator()(int a, int b, int c, int d, int e) {
  104. return a + b + c + d + e;
  105. }
  106. };
  107. string Concat5(const char* s1, const char* s2, const char* s3,
  108. const char* s4, const char* s5) {
  109. return string(s1) + s2 + s3 + s4 + s5;
  110. }
  111. int SumOf6(int a, int b, int c, int d, int e, int f) {
  112. return a + b + c + d + e + f;
  113. }
  114. struct SumOf6Functor {
  115. int operator()(int a, int b, int c, int d, int e, int f) {
  116. return a + b + c + d + e + f;
  117. }
  118. };
  119. string Concat6(const char* s1, const char* s2, const char* s3,
  120. const char* s4, const char* s5, const char* s6) {
  121. return string(s1) + s2 + s3 + s4 + s5 + s6;
  122. }
  123. string Concat7(const char* s1, const char* s2, const char* s3,
  124. const char* s4, const char* s5, const char* s6,
  125. const char* s7) {
  126. return string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
  127. }
  128. string Concat8(const char* s1, const char* s2, const char* s3,
  129. const char* s4, const char* s5, const char* s6,
  130. const char* s7, const char* s8) {
  131. return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
  132. }
  133. string Concat9(const char* s1, const char* s2, const char* s3,
  134. const char* s4, const char* s5, const char* s6,
  135. const char* s7, const char* s8, const char* s9) {
  136. return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
  137. }
  138. string Concat10(const char* s1, const char* s2, const char* s3,
  139. const char* s4, const char* s5, const char* s6,
  140. const char* s7, const char* s8, const char* s9,
  141. const char* s10) {
  142. return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
  143. }
  144. class Foo {
  145. public:
  146. Foo() : value_(123) {}
  147. int Nullary() const { return value_; }
  148. short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT
  149. string Binary(const string& str, char c) const { return str + c; }
  150. int Ternary(int x, bool y, char z) { return value_ + x + y*z; }
  151. int SumOf4(int a, int b, int c, int d) const {
  152. return a + b + c + d + value_;
  153. }
  154. int SumOfLast2(Unused, Unused, int a, int b) const { return a + b; }
  155. int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
  156. int SumOf6(int a, int b, int c, int d, int e, int f) {
  157. return a + b + c + d + e + f;
  158. }
  159. string Concat7(const char* s1, const char* s2, const char* s3,
  160. const char* s4, const char* s5, const char* s6,
  161. const char* s7) {
  162. return string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
  163. }
  164. string Concat8(const char* s1, const char* s2, const char* s3,
  165. const char* s4, const char* s5, const char* s6,
  166. const char* s7, const char* s8) {
  167. return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
  168. }
  169. string Concat9(const char* s1, const char* s2, const char* s3,
  170. const char* s4, const char* s5, const char* s6,
  171. const char* s7, const char* s8, const char* s9) {
  172. return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
  173. }
  174. string Concat10(const char* s1, const char* s2, const char* s3,
  175. const char* s4, const char* s5, const char* s6,
  176. const char* s7, const char* s8, const char* s9,
  177. const char* s10) {
  178. return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
  179. }
  180. private:
  181. int value_;
  182. };
  183. // Tests using Invoke() with a nullary function.
  184. TEST(InvokeTest, Nullary) {
  185. Action<int()> a = Invoke(Nullary); // NOLINT
  186. EXPECT_EQ(1, a.Perform(make_tuple()));
  187. }
  188. // Tests using Invoke() with a unary function.
  189. TEST(InvokeTest, Unary) {
  190. Action<bool(int)> a = Invoke(Unary); // NOLINT
  191. EXPECT_FALSE(a.Perform(make_tuple(1)));
  192. EXPECT_TRUE(a.Perform(make_tuple(-1)));
  193. }
  194. // Tests using Invoke() with a binary function.
  195. TEST(InvokeTest, Binary) {
  196. Action<const char*(const char*, short)> a = Invoke(Binary); // NOLINT
  197. const char* p = "Hello";
  198. EXPECT_EQ(p + 2, a.Perform(make_tuple(p, Short(2))));
  199. }
  200. // Tests using Invoke() with a ternary function.
  201. TEST(InvokeTest, Ternary) {
  202. Action<int(int, char, short)> a = Invoke(Ternary); // NOLINT
  203. EXPECT_EQ(6, a.Perform(make_tuple(1, '\2', Short(3))));
  204. }
  205. // Tests using Invoke() with a 4-argument function.
  206. TEST(InvokeTest, FunctionThatTakes4Arguments) {
  207. Action<int(int, int, int, int)> a = Invoke(SumOf4); // NOLINT
  208. EXPECT_EQ(1234, a.Perform(make_tuple(1000, 200, 30, 4)));
  209. }
  210. // Tests using Invoke() with a 5-argument function.
  211. TEST(InvokeTest, FunctionThatTakes5Arguments) {
  212. Action<int(int, int, int, int, int)> a = Invoke(SumOf5); // NOLINT
  213. EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5)));
  214. }
  215. // Tests using Invoke() with a 6-argument function.
  216. TEST(InvokeTest, FunctionThatTakes6Arguments) {
  217. Action<int(int, int, int, int, int, int)> a = Invoke(SumOf6); // NOLINT
  218. EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6)));
  219. }
  220. // A helper that turns the type of a C-string literal from const
  221. // char[N] to const char*.
  222. inline const char* CharPtr(const char* s) { return s; }
  223. // Tests using Invoke() with a 7-argument function.
  224. TEST(InvokeTest, FunctionThatTakes7Arguments) {
  225. Action<string(const char*, const char*, const char*, const char*,
  226. const char*, const char*, const char*)> a =
  227. Invoke(Concat7);
  228. EXPECT_EQ("1234567",
  229. a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
  230. CharPtr("4"), CharPtr("5"), CharPtr("6"),
  231. CharPtr("7"))));
  232. }
  233. // Tests using Invoke() with a 8-argument function.
  234. TEST(InvokeTest, FunctionThatTakes8Arguments) {
  235. Action<string(const char*, const char*, const char*, const char*,
  236. const char*, const char*, const char*, const char*)> a =
  237. Invoke(Concat8);
  238. EXPECT_EQ("12345678",
  239. a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
  240. CharPtr("4"), CharPtr("5"), CharPtr("6"),
  241. CharPtr("7"), CharPtr("8"))));
  242. }
  243. // Tests using Invoke() with a 9-argument function.
  244. TEST(InvokeTest, FunctionThatTakes9Arguments) {
  245. Action<string(const char*, const char*, const char*, const char*,
  246. const char*, const char*, const char*, const char*,
  247. const char*)> a = Invoke(Concat9);
  248. EXPECT_EQ("123456789",
  249. a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
  250. CharPtr("4"), CharPtr("5"), CharPtr("6"),
  251. CharPtr("7"), CharPtr("8"), CharPtr("9"))));
  252. }
  253. // Tests using Invoke() with a 10-argument function.
  254. TEST(InvokeTest, FunctionThatTakes10Arguments) {
  255. Action<string(const char*, const char*, const char*, const char*,
  256. const char*, const char*, const char*, const char*,
  257. const char*, const char*)> 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 string&)> a1 =
  267. Invoke(SumOfFirst2);
  268. string s("hi");
  269. EXPECT_EQ(12, a1.Perform(
  270. tuple<int, int, double, const string&>(10, 2, 5.6, s)));
  271. Action<int(int, int, bool, int*)> a2 =
  272. Invoke(SumOfFirst2);
  273. EXPECT_EQ(23, a2.Perform(make_tuple(20, 3, true, static_cast<int*>(NULL))));
  274. }
  275. // Tests using Invoke() with methods with parameters declared as Unused.
  276. TEST(InvokeTest, MethodWithUnusedParameters) {
  277. Foo foo;
  278. Action<int(string, bool, int, int)> a1 =
  279. Invoke(&foo, &Foo::SumOfLast2);
  280. EXPECT_EQ(12, a1.Perform(make_tuple(CharPtr("hi"), true, 10, 2)));
  281. Action<int(char, double, int, int)> a2 =
  282. Invoke(&foo, &Foo::SumOfLast2);
  283. EXPECT_EQ(23, a2.Perform(make_tuple('a', 2.5, 20, 3)));
  284. }
  285. // Tests using Invoke() with a functor.
  286. TEST(InvokeTest, Functor) {
  287. Action<long(long, int)> a = Invoke(plus<long>()); // NOLINT
  288. EXPECT_EQ(3L, a.Perform(make_tuple(1, 2)));
  289. }
  290. // Tests using Invoke(f) as an action of a compatible type.
  291. TEST(InvokeTest, FunctionWithCompatibleType) {
  292. Action<long(int, short, char, bool)> a = Invoke(SumOf4); // NOLINT
  293. EXPECT_EQ(4321, a.Perform(make_tuple(4000, Short(300), Char(20), true)));
  294. }
  295. // Tests using Invoke() with an object pointer and a method pointer.
  296. // Tests using Invoke() with a nullary method.
  297. TEST(InvokeMethodTest, Nullary) {
  298. Foo foo;
  299. Action<int()> a = Invoke(&foo, &Foo::Nullary); // NOLINT
  300. EXPECT_EQ(123, a.Perform(make_tuple()));
  301. }
  302. // Tests using Invoke() with a unary method.
  303. TEST(InvokeMethodTest, Unary) {
  304. Foo foo;
  305. Action<short(long)> a = Invoke(&foo, &Foo::Unary); // NOLINT
  306. EXPECT_EQ(4123, a.Perform(make_tuple(4000)));
  307. }
  308. // Tests using Invoke() with a binary method.
  309. TEST(InvokeMethodTest, Binary) {
  310. Foo foo;
  311. Action<string(const string&, char)> a = Invoke(&foo, &Foo::Binary);
  312. string s("Hell");
  313. EXPECT_EQ("Hello", a.Perform(
  314. tuple<const string&, char>(s, 'o')));
  315. }
  316. // Tests using Invoke() with a ternary method.
  317. TEST(InvokeMethodTest, Ternary) {
  318. Foo foo;
  319. Action<int(int, bool, char)> a = Invoke(&foo, &Foo::Ternary); // NOLINT
  320. EXPECT_EQ(1124, a.Perform(make_tuple(1000, true, Char(1))));
  321. }
  322. // Tests using Invoke() with a 4-argument method.
  323. TEST(InvokeMethodTest, MethodThatTakes4Arguments) {
  324. Foo foo;
  325. Action<int(int, int, int, int)> a = Invoke(&foo, &Foo::SumOf4); // NOLINT
  326. EXPECT_EQ(1357, a.Perform(make_tuple(1000, 200, 30, 4)));
  327. }
  328. // Tests using Invoke() with a 5-argument method.
  329. TEST(InvokeMethodTest, MethodThatTakes5Arguments) {
  330. Foo foo;
  331. Action<int(int, int, int, int, int)> a = Invoke(&foo, &Foo::SumOf5); // NOLINT
  332. EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5)));
  333. }
  334. // Tests using Invoke() with a 6-argument method.
  335. TEST(InvokeMethodTest, MethodThatTakes6Arguments) {
  336. Foo foo;
  337. Action<int(int, int, int, int, int, int)> a = // NOLINT
  338. Invoke(&foo, &Foo::SumOf6);
  339. EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6)));
  340. }
  341. // Tests using Invoke() with a 7-argument method.
  342. TEST(InvokeMethodTest, MethodThatTakes7Arguments) {
  343. Foo foo;
  344. Action<string(const char*, const char*, const char*, const char*,
  345. const char*, const char*, const char*)> a =
  346. Invoke(&foo, &Foo::Concat7);
  347. EXPECT_EQ("1234567",
  348. a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
  349. CharPtr("4"), CharPtr("5"), CharPtr("6"),
  350. CharPtr("7"))));
  351. }
  352. // Tests using Invoke() with a 8-argument method.
  353. TEST(InvokeMethodTest, MethodThatTakes8Arguments) {
  354. Foo foo;
  355. Action<string(const char*, const char*, const char*, const char*,
  356. const char*, const char*, const char*, const char*)> a =
  357. Invoke(&foo, &Foo::Concat8);
  358. EXPECT_EQ("12345678",
  359. a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
  360. CharPtr("4"), CharPtr("5"), CharPtr("6"),
  361. CharPtr("7"), CharPtr("8"))));
  362. }
  363. // Tests using Invoke() with a 9-argument method.
  364. TEST(InvokeMethodTest, MethodThatTakes9Arguments) {
  365. Foo foo;
  366. Action<string(const char*, const char*, const char*, const char*,
  367. const char*, const char*, const char*, const char*,
  368. const char*)> a = Invoke(&foo, &Foo::Concat9);
  369. EXPECT_EQ("123456789",
  370. a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
  371. CharPtr("4"), CharPtr("5"), CharPtr("6"),
  372. CharPtr("7"), CharPtr("8"), CharPtr("9"))));
  373. }
  374. // Tests using Invoke() with a 10-argument method.
  375. TEST(InvokeMethodTest, MethodThatTakes10Arguments) {
  376. Foo foo;
  377. Action<string(const char*, const char*, const char*, const char*,
  378. const char*, const char*, const char*, const char*,
  379. const char*, const char*)> 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<string(int, int, string, int)> a = ReturnArg<2>();
  414. EXPECT_EQ("seven", a.Perform(make_tuple(5, 6, 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