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