You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

698 lines
24 KiB

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