gmock-more-actions_test.cc
Go to the documentation of this file.
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 
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 
36 
37 #include <functional>
38 #include <memory>
39 #include <sstream>
40 #include <string>
41 #include "gmock/gmock.h"
42 #include "gtest/gtest.h"
43 
44 namespace testing {
45 namespace gmock_more_actions_test {
46 
47 using ::std::plus;
49 using testing::_;
50 using testing::Action;
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;
61 using testing::Unused;
62 using testing::WithArg;
64 
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 
69 // Sample functions and functors for testing Invoke() and etc.
70 int Nullary() { return 1; }
71 
73  public:
74  int operator()() { return 2; }
75 };
76 
77 bool g_done = false;
78 void VoidNullary() { g_done = true; }
79 
81  public:
82  void operator()() { g_done = true; }
83 };
84 
85 bool Unary(int x) { return x < 0; }
86 
87 const char* Plus1(const char* s) { return s + 1; }
88 
89 void VoidUnary(int /* n */) { g_done = true; }
90 
91 bool ByConstRef(const std::string& s) { return s == "Hi"; }
92 
93 const double g_double = 0;
94 bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
95 
96 std::string ByNonConstRef(std::string& s) { return s += "+"; } // NOLINT
97 
98 struct UnaryFunctor {
99  int operator()(bool x) { return x ? 1 : -1; }
100 };
101 
102 const char* Binary(const char* input, short n) { return input + n; } // NOLINT
103 
104 void VoidBinary(int, char) { g_done = true; }
105 
106 int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT
107 
108 void VoidTernary(int, char, bool) { g_done = true; }
109 
110 int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
111 
112 int SumOfFirst2(int a, int b, Unused, Unused) { return a + b; }
113 
114 void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; }
115 
116 std::string Concat4(const char* s1, const char* s2, const char* s3,
117  const char* s4) {
118  return std::string(s1) + s2 + s3 + s4;
119 }
120 
121 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
122 
124  int operator()(int a, int b, int c, int d, int e) {
125  return a + b + c + d + e;
126  }
127 };
128 
129 std::string Concat5(const char* s1, const char* s2, const char* s3,
130  const char* s4, const char* s5) {
131  return std::string(s1) + s2 + s3 + s4 + s5;
132 }
133 
134 int SumOf6(int a, int b, int c, int d, int e, int f) {
135  return a + b + c + d + e + f;
136 }
137 
139  int operator()(int a, int b, int c, int d, int e, int f) {
140  return a + b + c + d + e + f;
141  }
142 };
143 
144 std::string Concat6(const char* s1, const char* s2, const char* s3,
145  const char* s4, const char* s5, const char* s6) {
146  return std::string(s1) + s2 + s3 + s4 + s5 + s6;
147 }
148 
149 std::string Concat7(const char* s1, const char* s2, const char* s3,
150  const char* s4, const char* s5, const char* s6,
151  const char* s7) {
152  return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
153 }
154 
155 std::string Concat8(const char* s1, const char* s2, const char* s3,
156  const char* s4, const char* s5, const char* s6,
157  const char* s7, const char* s8) {
158  return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
159 }
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 
167 std::string Concat10(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  const char* s10) {
171  return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
172 }
173 
174 class Foo {
175  public:
176  Foo() : value_(123) {}
177 
178  int Nullary() const { return value_; }
179 
180  short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT
181 
182  std::string Binary(const std::string& str, char c) const { return str + c; }
183 
184  int Ternary(int x, bool y, char z) { return value_ + x + y*z; }
185 
186  int SumOf4(int a, int b, int c, int d) const {
187  return a + b + c + d + value_;
188  }
189 
190  int SumOfLast2(Unused, Unused, int a, int b) const { return a + b; }
191 
192  int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
193 
194  int SumOf6(int a, int b, int c, int d, int e, int f) {
195  return a + b + c + d + e + f;
196  }
197 
198  std::string Concat7(const char* s1, const char* s2, const char* s3,
199  const char* s4, const char* s5, const char* s6,
200  const char* s7) {
201  return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
202  }
203 
204  std::string Concat8(const char* s1, const char* s2, const char* s3,
205  const char* s4, const char* s5, const char* s6,
206  const char* s7, const char* s8) {
207  return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
208  }
209 
210  std::string Concat9(const char* s1, const char* s2, const char* s3,
211  const char* s4, const char* s5, const char* s6,
212  const char* s7, const char* s8, const char* s9) {
213  return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
214  }
215 
216  std::string Concat10(const char* s1, const char* s2, const char* s3,
217  const char* s4, const char* s5, const char* s6,
218  const char* s7, const char* s8, const char* s9,
219  const char* s10) {
220  return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
221  }
222 
223  private:
224  int value_;
225 };
226 
227 // Tests using Invoke() with a nullary function.
228 TEST(InvokeTest, Nullary) {
229  Action<int()> a = Invoke(Nullary); // NOLINT
230  EXPECT_EQ(1, a.Perform(std::make_tuple()));
231 }
232 
233 // Tests using Invoke() with a unary function.
234 TEST(InvokeTest, Unary) {
235  Action<bool(int)> a = Invoke(Unary); // NOLINT
236  EXPECT_FALSE(a.Perform(std::make_tuple(1)));
237  EXPECT_TRUE(a.Perform(std::make_tuple(-1)));
238 }
239 
240 // Tests using Invoke() with a binary function.
241 TEST(InvokeTest, Binary) {
242  Action<const char*(const char*, short)> a = Invoke(Binary); // NOLINT
243  const char* p = "Hello";
244  EXPECT_EQ(p + 2, a.Perform(std::make_tuple(p, Short(2))));
245 }
246 
247 // Tests using Invoke() with a ternary function.
248 TEST(InvokeTest, Ternary) {
249  Action<int(int, char, short)> a = Invoke(Ternary); // NOLINT
250  EXPECT_EQ(6, a.Perform(std::make_tuple(1, '\2', Short(3))));
251 }
252 
253 // Tests using Invoke() with a 4-argument function.
254 TEST(InvokeTest, FunctionThatTakes4Arguments) {
255  Action<int(int, int, int, int)> a = Invoke(SumOf4); // NOLINT
256  EXPECT_EQ(1234, a.Perform(std::make_tuple(1000, 200, 30, 4)));
257 }
258 
259 // Tests using Invoke() with a 5-argument function.
260 TEST(InvokeTest, FunctionThatTakes5Arguments) {
261  Action<int(int, int, int, int, int)> a = Invoke(SumOf5); // NOLINT
262  EXPECT_EQ(12345, a.Perform(std::make_tuple(10000, 2000, 300, 40, 5)));
263 }
264 
265 // Tests using Invoke() with a 6-argument function.
266 TEST(InvokeTest, FunctionThatTakes6Arguments) {
267  Action<int(int, int, int, int, int, int)> a = Invoke(SumOf6); // NOLINT
268  EXPECT_EQ(123456,
269  a.Perform(std::make_tuple(100000, 20000, 3000, 400, 50, 6)));
270 }
271 
272 // A helper that turns the type of a C-string literal from const
273 // char[N] to const char*.
274 inline const char* CharPtr(const char* s) { return s; }
275 
276 // Tests using Invoke() with a 7-argument function.
277 TEST(InvokeTest, FunctionThatTakes7Arguments) {
278  Action<std::string(const char*, const char*, const char*, const char*,
279  const char*, const char*, const char*)>
280  a = Invoke(Concat7);
281  EXPECT_EQ("1234567",
282  a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
283  CharPtr("4"), CharPtr("5"), CharPtr("6"),
284  CharPtr("7"))));
285 }
286 
287 // Tests using Invoke() with a 8-argument function.
288 TEST(InvokeTest, FunctionThatTakes8Arguments) {
289  Action<std::string(const char*, const char*, const char*, const char*,
290  const char*, const char*, const char*, const char*)>
291  a = Invoke(Concat8);
292  EXPECT_EQ("12345678",
293  a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
294  CharPtr("4"), CharPtr("5"), CharPtr("6"),
295  CharPtr("7"), CharPtr("8"))));
296 }
297 
298 // Tests using Invoke() with a 9-argument function.
299 TEST(InvokeTest, FunctionThatTakes9Arguments) {
300  Action<std::string(const char*, const char*, const char*, const char*,
301  const char*, const char*, const char*, const char*,
302  const char*)>
303  a = Invoke(Concat9);
304  EXPECT_EQ("123456789", a.Perform(std::make_tuple(
305  CharPtr("1"), CharPtr("2"), CharPtr("3"),
306  CharPtr("4"), CharPtr("5"), CharPtr("6"),
307  CharPtr("7"), CharPtr("8"), CharPtr("9"))));
308 }
309 
310 // Tests using Invoke() with a 10-argument function.
311 TEST(InvokeTest, FunctionThatTakes10Arguments) {
312  Action<std::string(const char*, const char*, const char*, const char*,
313  const char*, const char*, const char*, const char*,
314  const char*, const char*)>
315  a = Invoke(Concat10);
316  EXPECT_EQ("1234567890",
317  a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
318  CharPtr("4"), CharPtr("5"), CharPtr("6"),
319  CharPtr("7"), CharPtr("8"), CharPtr("9"),
320  CharPtr("0"))));
321 }
322 
323 // Tests using Invoke() with functions with parameters declared as Unused.
324 TEST(InvokeTest, FunctionWithUnusedParameters) {
325  Action<int(int, int, double, const std::string&)> a1 = Invoke(SumOfFirst2);
326  std::tuple<int, int, double, std::string> dummy =
327  std::make_tuple(10, 2, 5.6, std::string("hi"));
328  EXPECT_EQ(12, a1.Perform(dummy));
329 
330  Action<int(int, int, bool, int*)> a2 =
332  EXPECT_EQ(
333  23, a2.Perform(std::make_tuple(20, 3, true, static_cast<int*>(nullptr))));
334 }
335 
336 // Tests using Invoke() with methods with parameters declared as Unused.
337 TEST(InvokeTest, MethodWithUnusedParameters) {
338  Foo foo;
339  Action<int(std::string, bool, int, int)> a1 = Invoke(&foo, &Foo::SumOfLast2);
340  EXPECT_EQ(12, a1.Perform(std::make_tuple(CharPtr("hi"), true, 10, 2)));
341 
342  Action<int(char, double, int, int)> a2 =
344  EXPECT_EQ(23, a2.Perform(std::make_tuple('a', 2.5, 20, 3)));
345 }
346 
347 // Tests using Invoke() with a functor.
348 TEST(InvokeTest, Functor) {
349  Action<long(long, int)> a = Invoke(plus<long>()); // NOLINT
350  EXPECT_EQ(3L, a.Perform(std::make_tuple(1, 2)));
351 }
352 
353 // Tests using Invoke(f) as an action of a compatible type.
354 TEST(InvokeTest, FunctionWithCompatibleType) {
355  Action<long(int, short, char, bool)> a = Invoke(SumOf4); // NOLINT
356  EXPECT_EQ(4321, a.Perform(std::make_tuple(4000, Short(300), Char(20), true)));
357 }
358 
359 // Tests using Invoke() with an object pointer and a method pointer.
360 
361 // Tests using Invoke() with a nullary method.
362 TEST(InvokeMethodTest, Nullary) {
363  Foo foo;
364  Action<int()> a = Invoke(&foo, &Foo::Nullary); // NOLINT
365  EXPECT_EQ(123, a.Perform(std::make_tuple()));
366 }
367 
368 // Tests using Invoke() with a unary method.
369 TEST(InvokeMethodTest, Unary) {
370  Foo foo;
371  Action<short(long)> a = Invoke(&foo, &Foo::Unary); // NOLINT
372  EXPECT_EQ(4123, a.Perform(std::make_tuple(4000)));
373 }
374 
375 // Tests using Invoke() with a binary method.
376 TEST(InvokeMethodTest, Binary) {
377  Foo foo;
378  Action<std::string(const std::string&, char)> a = Invoke(&foo, &Foo::Binary);
379  std::string s("Hell");
380  std::tuple<std::string, char> dummy = std::make_tuple(s, 'o');
381  EXPECT_EQ("Hello", a.Perform(dummy));
382 }
383 
384 // Tests using Invoke() with a ternary method.
385 TEST(InvokeMethodTest, Ternary) {
386  Foo foo;
387  Action<int(int, bool, char)> a = Invoke(&foo, &Foo::Ternary); // NOLINT
388  EXPECT_EQ(1124, a.Perform(std::make_tuple(1000, true, Char(1))));
389 }
390 
391 // Tests using Invoke() with a 4-argument method.
392 TEST(InvokeMethodTest, MethodThatTakes4Arguments) {
393  Foo foo;
394  Action<int(int, int, int, int)> a = Invoke(&foo, &Foo::SumOf4); // NOLINT
395  EXPECT_EQ(1357, a.Perform(std::make_tuple(1000, 200, 30, 4)));
396 }
397 
398 // Tests using Invoke() with a 5-argument method.
399 TEST(InvokeMethodTest, MethodThatTakes5Arguments) {
400  Foo foo;
401  Action<int(int, int, int, int, int)> a = Invoke(&foo, &Foo::SumOf5); // NOLINT
402  EXPECT_EQ(12345, a.Perform(std::make_tuple(10000, 2000, 300, 40, 5)));
403 }
404 
405 // Tests using Invoke() with a 6-argument method.
406 TEST(InvokeMethodTest, MethodThatTakes6Arguments) {
407  Foo foo;
408  Action<int(int, int, int, int, int, int)> a = // NOLINT
409  Invoke(&foo, &Foo::SumOf6);
410  EXPECT_EQ(123456,
411  a.Perform(std::make_tuple(100000, 20000, 3000, 400, 50, 6)));
412 }
413 
414 // Tests using Invoke() with a 7-argument method.
415 TEST(InvokeMethodTest, MethodThatTakes7Arguments) {
416  Foo foo;
417  Action<std::string(const char*, const char*, const char*, const char*,
418  const char*, const char*, const char*)>
419  a = Invoke(&foo, &Foo::Concat7);
420  EXPECT_EQ("1234567",
421  a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
422  CharPtr("4"), CharPtr("5"), CharPtr("6"),
423  CharPtr("7"))));
424 }
425 
426 // Tests using Invoke() with a 8-argument method.
427 TEST(InvokeMethodTest, MethodThatTakes8Arguments) {
428  Foo foo;
429  Action<std::string(const char*, const char*, const char*, const char*,
430  const char*, const char*, const char*, const char*)>
431  a = Invoke(&foo, &Foo::Concat8);
432  EXPECT_EQ("12345678",
433  a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
434  CharPtr("4"), CharPtr("5"), CharPtr("6"),
435  CharPtr("7"), CharPtr("8"))));
436 }
437 
438 // Tests using Invoke() with a 9-argument method.
439 TEST(InvokeMethodTest, MethodThatTakes9Arguments) {
440  Foo foo;
441  Action<std::string(const char*, const char*, const char*, const char*,
442  const char*, const char*, const char*, const char*,
443  const char*)>
444  a = Invoke(&foo, &Foo::Concat9);
445  EXPECT_EQ("123456789", a.Perform(std::make_tuple(
446  CharPtr("1"), CharPtr("2"), CharPtr("3"),
447  CharPtr("4"), CharPtr("5"), CharPtr("6"),
448  CharPtr("7"), CharPtr("8"), CharPtr("9"))));
449 }
450 
451 // Tests using Invoke() with a 10-argument method.
452 TEST(InvokeMethodTest, MethodThatTakes10Arguments) {
453  Foo foo;
454  Action<std::string(const char*, const char*, const char*, const char*,
455  const char*, const char*, const char*, const char*,
456  const char*, const char*)>
457  a = Invoke(&foo, &Foo::Concat10);
458  EXPECT_EQ("1234567890",
459  a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
460  CharPtr("4"), CharPtr("5"), CharPtr("6"),
461  CharPtr("7"), CharPtr("8"), CharPtr("9"),
462  CharPtr("0"))));
463 }
464 
465 // Tests using Invoke(f) as an action of a compatible type.
466 TEST(InvokeMethodTest, MethodWithCompatibleType) {
467  Foo foo;
468  Action<long(int, short, char, bool)> a = // NOLINT
469  Invoke(&foo, &Foo::SumOf4);
470  EXPECT_EQ(4444, a.Perform(std::make_tuple(4000, Short(300), Char(20), true)));
471 }
472 
473 // Tests using WithoutArgs with an action that takes no argument.
474 TEST(WithoutArgsTest, NoArg) {
475  Action<int(int n)> a = WithoutArgs(Invoke(Nullary)); // NOLINT
476  EXPECT_EQ(1, a.Perform(std::make_tuple(2)));
477 }
478 
479 // Tests using WithArg with an action that takes 1 argument.
480 TEST(WithArgTest, OneArg) {
481  Action<bool(double x, int n)> b = WithArg<1>(Invoke(Unary)); // NOLINT
482  EXPECT_TRUE(b.Perform(std::make_tuple(1.5, -1)));
483  EXPECT_FALSE(b.Perform(std::make_tuple(1.5, 1)));
484 }
485 
486 TEST(ReturnArgActionTest, WorksForOneArgIntArg0) {
487  const Action<int(int)> a = ReturnArg<0>();
488  EXPECT_EQ(5, a.Perform(std::make_tuple(5)));
489 }
490 
491 TEST(ReturnArgActionTest, WorksForMultiArgBoolArg0) {
492  const Action<bool(bool, bool, bool)> a = ReturnArg<0>();
493  EXPECT_TRUE(a.Perform(std::make_tuple(true, false, false)));
494 }
495 
496 TEST(ReturnArgActionTest, WorksForMultiArgStringArg2) {
497  const Action<std::string(int, int, std::string, int)> a = ReturnArg<2>();
498  EXPECT_EQ("seven", a.Perform(std::make_tuple(5, 6, std::string("seven"), 8)));
499 }
500 
501 TEST(SaveArgActionTest, WorksForSameType) {
502  int result = 0;
503  const Action<void(int n)> a1 = SaveArg<0>(&result);
504  a1.Perform(std::make_tuple(5));
505  EXPECT_EQ(5, result);
506 }
507 
508 TEST(SaveArgActionTest, WorksForCompatibleType) {
509  int result = 0;
510  const Action<void(bool, char)> a1 = SaveArg<1>(&result);
511  a1.Perform(std::make_tuple(true, 'a'));
512  EXPECT_EQ('a', result);
513 }
514 
515 TEST(SaveArgPointeeActionTest, WorksForSameType) {
516  int result = 0;
517  const int value = 5;
518  const Action<void(const int*)> a1 = SaveArgPointee<0>(&result);
519  a1.Perform(std::make_tuple(&value));
520  EXPECT_EQ(5, result);
521 }
522 
523 TEST(SaveArgPointeeActionTest, WorksForCompatibleType) {
524  int result = 0;
525  char value = 'a';
526  const Action<void(bool, char*)> a1 = SaveArgPointee<1>(&result);
527  a1.Perform(std::make_tuple(true, &value));
528  EXPECT_EQ('a', result);
529 }
530 
531 TEST(SetArgRefereeActionTest, WorksForSameType) {
532  int value = 0;
533  const Action<void(int&)> a1 = SetArgReferee<0>(1);
534  a1.Perform(std::tuple<int&>(value));
535  EXPECT_EQ(1, value);
536 }
537 
538 TEST(SetArgRefereeActionTest, WorksForCompatibleType) {
539  int value = 0;
540  const Action<void(int, int&)> a1 = SetArgReferee<1>('a');
541  a1.Perform(std::tuple<int, int&>(0, value));
542  EXPECT_EQ('a', value);
543 }
544 
545 TEST(SetArgRefereeActionTest, WorksWithExtraArguments) {
546  int value = 0;
547  const Action<void(bool, int, int&, const char*)> a1 = SetArgReferee<2>('a');
548  a1.Perform(std::tuple<bool, int, int&, const char*>(true, 0, value, "hi"));
549  EXPECT_EQ('a', value);
550 }
551 
552 // A class that can be used to verify that its destructor is called: it will set
553 // the bool provided to the constructor to true when destroyed.
555  public:
556  explicit DeletionTester(bool* is_deleted)
557  : is_deleted_(is_deleted) {
558  // Make sure the bit is set to false.
559  *is_deleted_ = false;
560  }
561 
563  *is_deleted_ = true;
564  }
565 
566  private:
567  bool* is_deleted_;
568 };
569 
570 TEST(DeleteArgActionTest, OneArg) {
571  bool is_deleted = false;
572  DeletionTester* t = new DeletionTester(&is_deleted);
573  const Action<void(DeletionTester*)> a1 = DeleteArg<0>(); // NOLINT
574  EXPECT_FALSE(is_deleted);
575  a1.Perform(std::make_tuple(t));
576  EXPECT_TRUE(is_deleted);
577 }
578 
579 TEST(DeleteArgActionTest, TenArgs) {
580  bool is_deleted = false;
581  DeletionTester* t = new DeletionTester(&is_deleted);
582  const Action<void(bool, int, int, const char*, bool,
583  int, int, int, int, DeletionTester*)> a1 = DeleteArg<9>();
584  EXPECT_FALSE(is_deleted);
585  a1.Perform(std::make_tuple(true, 5, 6, CharPtr("hi"), false, 7, 8, 9, 10, t));
586  EXPECT_TRUE(is_deleted);
587 }
588 
589 #if GTEST_HAS_EXCEPTIONS
590 
591 TEST(ThrowActionTest, ThrowsGivenExceptionInVoidFunction) {
592  const Action<void(int n)> a = Throw('a');
593  EXPECT_THROW(a.Perform(std::make_tuple(0)), char);
594 }
595 
596 class MyException {};
597 
598 TEST(ThrowActionTest, ThrowsGivenExceptionInNonVoidFunction) {
599  const Action<double(char ch)> a = Throw(MyException());
600  EXPECT_THROW(a.Perform(std::make_tuple('0')), MyException);
601 }
602 
603 TEST(ThrowActionTest, ThrowsGivenExceptionInNullaryFunction) {
604  const Action<double()> a = Throw(MyException());
605  EXPECT_THROW(a.Perform(std::make_tuple()), MyException);
606 }
607 
608 #endif // GTEST_HAS_EXCEPTIONS
609 
610 // Tests that SetArrayArgument<N>(first, last) sets the elements of the array
611 // pointed to by the N-th (0-based) argument to values in range [first, last).
612 TEST(SetArrayArgumentTest, SetsTheNthArray) {
613  typedef void MyFunction(bool, int*, char*);
614  int numbers[] = { 1, 2, 3 };
615  Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers + 3);
616 
617  int n[4] = {};
618  int* pn = n;
619  char ch[4] = {};
620  char* pch = ch;
621  a.Perform(std::make_tuple(true, pn, pch));
622  EXPECT_EQ(1, n[0]);
623  EXPECT_EQ(2, n[1]);
624  EXPECT_EQ(3, n[2]);
625  EXPECT_EQ(0, n[3]);
626  EXPECT_EQ('\0', ch[0]);
627  EXPECT_EQ('\0', ch[1]);
628  EXPECT_EQ('\0', ch[2]);
629  EXPECT_EQ('\0', ch[3]);
630 
631  // Tests first and last are iterators.
632  std::string letters = "abc";
633  a = SetArrayArgument<2>(letters.begin(), letters.end());
634  std::fill_n(n, 4, 0);
635  std::fill_n(ch, 4, '\0');
636  a.Perform(std::make_tuple(true, pn, pch));
637  EXPECT_EQ(0, n[0]);
638  EXPECT_EQ(0, n[1]);
639  EXPECT_EQ(0, n[2]);
640  EXPECT_EQ(0, n[3]);
641  EXPECT_EQ('a', ch[0]);
642  EXPECT_EQ('b', ch[1]);
643  EXPECT_EQ('c', ch[2]);
644  EXPECT_EQ('\0', ch[3]);
645 }
646 
647 // Tests SetArrayArgument<N>(first, last) where first == last.
648 TEST(SetArrayArgumentTest, SetsTheNthArrayWithEmptyRange) {
649  typedef void MyFunction(bool, int*);
650  int numbers[] = { 1, 2, 3 };
651  Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers);
652 
653  int n[4] = {};
654  int* pn = n;
655  a.Perform(std::make_tuple(true, pn));
656  EXPECT_EQ(0, n[0]);
657  EXPECT_EQ(0, n[1]);
658  EXPECT_EQ(0, n[2]);
659  EXPECT_EQ(0, n[3]);
660 }
661 
662 // Tests SetArrayArgument<N>(first, last) where *first is convertible
663 // (but not equal) to the argument type.
664 TEST(SetArrayArgumentTest, SetsTheNthArrayWithConvertibleType) {
665  typedef void MyFunction(bool, int*);
666  char chars[] = { 97, 98, 99 };
667  Action<MyFunction> a = SetArrayArgument<1>(chars, chars + 3);
668 
669  int codes[4] = { 111, 222, 333, 444 };
670  int* pcodes = codes;
671  a.Perform(std::make_tuple(true, pcodes));
672  EXPECT_EQ(97, codes[0]);
673  EXPECT_EQ(98, codes[1]);
674  EXPECT_EQ(99, codes[2]);
675  EXPECT_EQ(444, codes[3]);
676 }
677 
678 // Test SetArrayArgument<N>(first, last) with iterator as argument.
679 TEST(SetArrayArgumentTest, SetsTheNthArrayWithIteratorArgument) {
680  typedef void MyFunction(bool, std::back_insert_iterator<std::string>);
681  std::string letters = "abc";
682  Action<MyFunction> a = SetArrayArgument<1>(letters.begin(), letters.end());
683 
684  std::string s;
685  a.Perform(std::make_tuple(true, back_inserter(s)));
686  EXPECT_EQ(letters, s);
687 }
688 
689 TEST(ReturnPointeeTest, Works) {
690  int n = 42;
691  const Action<int()> a = ReturnPointee(&n);
692  EXPECT_EQ(42, a.Perform(std::make_tuple()));
693 
694  n = 43;
695  EXPECT_EQ(43, a.Perform(std::make_tuple()));
696 }
697 
698 } // namespace gmock_generated_actions_test
699 } // namespace testing
testing::gmock_more_actions_test::VoidBinary
void VoidBinary(int, char)
Definition: gmock-more-actions_test.cc:104
testing
Definition: gmock-actions.h:59
testing::gmock_more_actions_test::ByNonConstRef
std::string ByNonConstRef(std::string &s)
Definition: gmock-more-actions_test.cc:96
testing::gmock_more_actions_test::Short
short Short(short n)
Definition: gmock-more-actions_test.cc:66
testing::gmock_more_actions_test::VoidNullaryFunctor::operator()
void operator()()
Definition: gmock-more-actions_test.cc:82
testing::gmock_more_actions_test::DeletionTester
Definition: gmock-more-actions_test.cc:554
testing::gmock_more_actions_test::Foo::SumOf5
int SumOf5(int a, int b, int c, int d, int e)
Definition: gmock-more-actions_test.cc:192
testing::gmock_more_actions_test::SumOf5Functor::operator()
int operator()(int a, int b, int c, int d, int e)
Definition: gmock-more-actions_test.cc:124
input
std::string input
Definition: tokenizer_unittest.cc:197
testing::gmock_more_actions_test::Foo::Concat7
std::string Concat7(const char *s1, const char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7)
Definition: gmock-more-actions_test.cc:198
gtest.h
testing::gmock_more_actions_test::SumOf6
int SumOf6(int a, int b, int c, int d, int e, int f)
Definition: gmock-more-actions_test.cc:134
s
XmlRpcServer s
testing::gmock_more_actions_test::Foo::Concat10
std::string Concat10(const char *s1, const char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7, const char *s8, const char *s9, const char *s10)
Definition: gmock-more-actions_test.cc:216
EXPECT_EQ
#define EXPECT_EQ(val1, val2)
Definition: glog/src/googletest.h:155
testing::gmock_more_actions_test::Unary
bool Unary(int x)
Definition: gmock-more-actions_test.cc:85
foo
Definition: googletest-output-test_.cc:534
testing::gmock_more_actions_test::SumOf6Functor
Definition: gmock-more-actions_test.cc:138
testing::gmock_more_actions_test::UnaryFunctor::operator()
int operator()(bool x)
Definition: gmock-more-actions_test.cc:99
testing::gmock_more_actions_test::Foo::Nullary
int Nullary() const
Definition: gmock-more-actions_test.cc:178
testing::gmock_more_actions_test::SumOf5
int SumOf5(int a, int b, int c, int d, int e)
Definition: gmock-more-actions_test.cc:121
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
testing::gmock_more_actions_test::NullaryFunctor
Definition: gmock-more-actions_test.cc:72
foo
int foo
Definition: statusor_test.cc:66
y
GLint y
Definition: glcorearb.h:2768
gmock.h
x
GLint GLenum GLint x
Definition: glcorearb.h:2834
dummy
ReturnVal dummy
Definition: register_benchmark_test.cc:68
testing::gmock_more_actions_test::NullaryFunctor::operator()
int operator()()
Definition: gmock-more-actions_test.cc:74
testing::gmock_more_actions_test::DeletionTester::~DeletionTester
~DeletionTester()
Definition: gmock-more-actions_test.cc:562
testing::gmock_more_actions_test::Plus1
const char * Plus1(const char *s)
Definition: gmock-more-actions_test.cc:87
b
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:3228
testing::gmock_more_actions_test::Concat7
std::string Concat7(const char *s1, const char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7)
Definition: gmock-more-actions_test.cc:149
testing::WithoutArgs
internal::WithArgsAction< typename std::decay< InnerAction >::type > WithoutArgs(InnerAction &&action)
Definition: gmock-actions.h:1033
testing::gmock_more_actions_test::VoidUnary
void VoidUnary(int)
Definition: gmock-more-actions_test.cc:89
testing::Invoke
std::decay< FunctionImpl >::type Invoke(FunctionImpl &&function_impl)
Definition: gmock-actions.h:1145
testing::gmock_more_actions_test::DeletionTester::is_deleted_
bool * is_deleted_
Definition: gmock-more-actions_test.cc:567
testing::gmock_more_actions_test::g_double
const double g_double
Definition: gmock-more-actions_test.cc:93
testing::gmock_more_actions_test::VoidNullaryFunctor
Definition: gmock-more-actions_test.cc:80
testing::gmock_more_actions_test::Foo::Ternary
int Ternary(int x, bool y, char z)
Definition: gmock-more-actions_test.cc:184
testing::gmock_more_actions_test::Foo::Unary
short Unary(long x)
Definition: gmock-more-actions_test.cc:180
testing::gmock_more_actions_test::Concat10
std::string Concat10(const char *s1, const char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7, const char *s8, const char *s9, const char *s10)
Definition: gmock-more-actions_test.cc:167
gmock_output_test._
_
Definition: gmock_output_test.py:173
testing::gmock_more_actions_test::Nullary
int Nullary()
Definition: gmock-more-actions_test.cc:70
update_failure_list.str
str
Definition: update_failure_list.py:41
testing::gmock_more_actions_test::VoidFunctionWithFourArguments
void VoidFunctionWithFourArguments(char, int, float, double)
Definition: gmock-more-actions_test.cc:114
p
const char * p
Definition: gmock-matchers_test.cc:3863
testing::gtest_printers_test::MyFunction
void MyFunction(int)
Definition: googletest-printers-test.cc:522
testing::gmock_more_actions_test::Foo::Concat8
std::string Concat8(const char *s1, const char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7, const char *s8)
Definition: gmock-more-actions_test.cc:204
testing::gmock_more_actions_test::VoidNullary
void VoidNullary()
Definition: gmock-more-actions_test.cc:78
EXPECT_TRUE
#define EXPECT_TRUE(cond)
Definition: glog/src/googletest.h:137
d
d
testing::Action
Definition: gmock-actions.h:341
testing::gmock_more_actions_test::SumOf5Functor
Definition: gmock-more-actions_test.cc:123
testing::gmock_more_actions_test::g_done
bool g_done
Definition: gmock-more-actions_test.cc:77
testing::gmock_more_actions_test::UnaryFunctor
Definition: gmock-more-actions_test.cc:98
testing::gmock_more_actions_test::Concat9
std::string Concat9(const char *s1, const char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7, const char *s8, const char *s9)
Definition: gmock-more-actions_test.cc:161
void
typedef void(APIENTRY *GLDEBUGPROCARB)(GLenum source
n
GLdouble n
Definition: glcorearb.h:4153
testing::gmock_more_actions_test::Foo::SumOf6
int SumOf6(int a, int b, int c, int d, int e, int f)
Definition: gmock-more-actions_test.cc:194
testing::StaticAssertTypeEq
bool StaticAssertTypeEq()
Definition: gtest.h:2332
testing::WithArg
internal::WithArgsAction< typename std::decay< InnerAction >::type, k > WithArg(InnerAction &&action)
Definition: gmock-actions.h:1013
testing::gmock_more_actions_test::Concat5
std::string Concat5(const char *s1, const char *s2, const char *s3, const char *s4, const char *s5)
Definition: gmock-more-actions_test.cc:129
z
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:3117
testing::gmock_more_actions_test::VoidTernary
void VoidTernary(int, char, bool)
Definition: gmock-more-actions_test.cc:108
testing::gmock_more_actions_test::Foo::SumOfLast2
int SumOfLast2(Unused, Unused, int a, int b) const
Definition: gmock-more-actions_test.cc:190
testing::gmock_more_actions_test::Foo::Concat9
std::string Concat9(const char *s1, const char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7, const char *s8, const char *s9)
Definition: gmock-more-actions_test.cc:210
testing::gmock_more_actions_test::Ternary
int Ternary(int x, char y, short z)
Definition: gmock-more-actions_test.cc:106
ch
char ch
Definition: gmock-matchers_test.cc:3871
testing::gmock_more_actions_test::Concat8
std::string Concat8(const char *s1, const char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7, const char *s8)
Definition: gmock-more-actions_test.cc:155
testing::gmock_more_actions_test::Foo::Foo
Foo()
Definition: gmock-more-actions_test.cc:176
testing::gmock_more_actions_test::Binary
const char * Binary(const char *input, short n)
Definition: gmock-more-actions_test.cc:102
EXPECT_FALSE
#define EXPECT_FALSE(cond)
Definition: glog/src/googletest.h:145
testing::gmock_more_actions_test::TEST
TEST(InvokeTest, Nullary)
Definition: gmock-more-actions_test.cc:228
testing::Action::Perform
Result Perform(ArgumentTuple args) const
Definition: gmock-actions.h:390
testing::gmock_more_actions_test::Foo::SumOf4
int SumOf4(int a, int b, int c, int d) const
Definition: gmock-more-actions_test.cc:186
testing::internal::IgnoredValue
Definition: gtest-internal.h:111
EXPECT_THROW
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1973
testing::gmock_more_actions_test::SumOf6Functor::operator()
int operator()(int a, int b, int c, int d, int e, int f)
Definition: gmock-more-actions_test.cc:139
testing::gmock_more_actions_test::Char
char Char(char ch)
Definition: gmock-more-actions_test.cc:67
gmock-more-actions.h
testing::Return
internal::ReturnAction< R > Return(R value)
Definition: gmock-actions.h:1041
testing::gmock_more_actions_test::ReferencesGlobalDouble
bool ReferencesGlobalDouble(const double &x)
Definition: gmock-more-actions_test.cc:94
testing::gmock_more_actions_test::Concat6
std::string Concat6(const char *s1, const char *s2, const char *s3, const char *s4, const char *s5, const char *s6)
Definition: gmock-more-actions_test.cc:144
testing::gmock_more_actions_test::Foo
Definition: gmock-more-actions_test.cc:174
testing::gmock_more_actions_test::DeletionTester::DeletionTester
DeletionTester(bool *is_deleted)
Definition: gmock-more-actions_test.cc:556
f
GLfloat f
Definition: glcorearb.h:3964
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
testing::gmock_more_actions_test::CharPtr
const char * CharPtr(const char *s)
Definition: gmock-more-actions_test.cc:274
testing::gmock_more_actions_test::ByConstRef
bool ByConstRef(const std::string &s)
Definition: gmock-more-actions_test.cc:91
a
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:3228
testing::Unused
internal::IgnoredValue Unused
Definition: gmock-actions.h:996
testing::gmock_more_actions_test::Concat4
std::string Concat4(const char *s1, const char *s2, const char *s3, const char *s4)
Definition: gmock-more-actions_test.cc:116
testing::ActionInterface
Definition: gmock-actions.h:315
testing::gmock_more_actions_test::SumOf4
int SumOf4(int a, int b, int c, int d)
Definition: gmock-more-actions_test.cc:110
testing::gmock_more_actions_test::Foo::Binary
std::string Binary(const std::string &str, char c) const
Definition: gmock-more-actions_test.cc:182
testing::gmock_more_actions_test::Foo::value_
int value_
Definition: gmock-more-actions_test.cc:224
google::protobuf.internal.decoder.long
long
Definition: decoder.py:89
testing::gmock_more_actions_test::SumOfFirst2
int SumOfFirst2(int a, int b, Unused, Unused)
Definition: gmock-more-actions_test.cc:112


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:52