blob: 2e6fa0b6cfa3218b89835bea212150231278208d [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +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 generated by a script.
35
36#include <gmock/gmock-generated-actions.h>
37
38#include <functional>
zhanyong.wan387bdd52009-07-20 21:16:35 +000039#include <sstream>
shiqiane35fdd92008-12-10 05:08:54 +000040#include <string>
41#include <gmock/gmock.h>
42#include <gtest/gtest.h>
43
44namespace testing {
45namespace gmock_generated_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::ByRef;
57using testing::DoAll;
58using testing::Invoke;
shiqiane35fdd92008-12-10 05:08:54 +000059using testing::Return;
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +000060using testing::ReturnNew;
shiqiane35fdd92008-12-10 05:08:54 +000061using testing::SetArgumentPointee;
shiqian326aa562009-01-09 21:43:57 +000062using testing::StaticAssertTypeEq;
shiqiane35fdd92008-12-10 05:08:54 +000063using testing::Unused;
shiqiane35fdd92008-12-10 05:08:54 +000064using testing::WithArgs;
shiqiane35fdd92008-12-10 05:08:54 +000065
zhanyong.wana18423e2009-07-22 23:58:19 +000066// Sample functions and functors for testing various actions.
shiqiane35fdd92008-12-10 05:08:54 +000067int Nullary() { return 1; }
68
69class NullaryFunctor {
70 public:
71 int operator()() { return 2; }
72};
73
74bool g_done = false;
shiqiane35fdd92008-12-10 05:08:54 +000075
76bool Unary(int x) { return x < 0; }
77
78const char* Plus1(const char* s) { return s + 1; }
79
shiqiane35fdd92008-12-10 05:08:54 +000080bool ByConstRef(const string& s) { return s == "Hi"; }
81
82const double g_double = 0;
83bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
84
85string ByNonConstRef(string& s) { return s += "+"; } // NOLINT
86
87struct UnaryFunctor {
88 int operator()(bool x) { return x ? 1 : -1; }
89};
90
91const char* Binary(const char* input, short n) { return input + n; } // NOLINT
92
93void VoidBinary(int, char) { g_done = true; }
94
95int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT
96
97void VoidTernary(int, char, bool) { g_done = true; }
98
99int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
100
shiqiane35fdd92008-12-10 05:08:54 +0000101string Concat4(const char* s1, const char* s2, const char* s3,
102 const char* s4) {
103 return string(s1) + s2 + s3 + s4;
104}
105
106int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
107
108struct SumOf5Functor {
109 int operator()(int a, int b, int c, int d, int e) {
110 return a + b + c + d + e;
111 }
112};
113
114string Concat5(const char* s1, const char* s2, const char* s3,
115 const char* s4, const char* s5) {
116 return string(s1) + s2 + s3 + s4 + s5;
117}
118
119int SumOf6(int a, int b, int c, int d, int e, int f) {
120 return a + b + c + d + e + f;
121}
122
123struct SumOf6Functor {
124 int operator()(int a, int b, int c, int d, int e, int f) {
125 return a + b + c + d + e + f;
126 }
127};
128
129string Concat6(const char* s1, const char* s2, const char* s3,
130 const char* s4, const char* s5, const char* s6) {
131 return string(s1) + s2 + s3 + s4 + s5 + s6;
132}
133
134string Concat7(const char* s1, const char* s2, const char* s3,
135 const char* s4, const char* s5, const char* s6,
136 const char* s7) {
137 return string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
138}
139
140string Concat8(const char* s1, const char* s2, const char* s3,
141 const char* s4, const char* s5, const char* s6,
142 const char* s7, const char* s8) {
143 return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
144}
145
146string Concat9(const char* s1, const char* s2, const char* s3,
147 const char* s4, const char* s5, const char* s6,
148 const char* s7, const char* s8, const char* s9) {
149 return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
150}
151
152string Concat10(const char* s1, const char* s2, const char* s3,
153 const char* s4, const char* s5, const char* s6,
154 const char* s7, const char* s8, const char* s9,
155 const char* s10) {
156 return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
157}
158
zhanyong.wan90c90f92009-06-17 22:11:04 +0000159// A helper that turns the type of a C-string literal from const
160// char[N] to const char*.
161inline const char* CharPtr(const char* s) { return s; }
162
shiqiane35fdd92008-12-10 05:08:54 +0000163// Tests InvokeArgument<N>(...).
164
165// Tests using InvokeArgument with a nullary function.
166TEST(InvokeArgumentTest, Function0) {
167 Action<int(int, int(*)())> a = InvokeArgument<1>(); // NOLINT
168 EXPECT_EQ(1, a.Perform(make_tuple(2, &Nullary)));
169}
170
171// Tests using InvokeArgument with a unary function.
172TEST(InvokeArgumentTest, Functor1) {
173 Action<int(UnaryFunctor)> a = InvokeArgument<0>(true); // NOLINT
174 EXPECT_EQ(1, a.Perform(make_tuple(UnaryFunctor())));
175}
176
177// Tests using InvokeArgument with a 5-ary function.
178TEST(InvokeArgumentTest, Function5) {
179 Action<int(int(*)(int, int, int, int, int))> a = // NOLINT
180 InvokeArgument<0>(10000, 2000, 300, 40, 5);
181 EXPECT_EQ(12345, a.Perform(make_tuple(&SumOf5)));
182}
183
184// Tests using InvokeArgument with a 5-ary functor.
185TEST(InvokeArgumentTest, Functor5) {
186 Action<int(SumOf5Functor)> a = // NOLINT
187 InvokeArgument<0>(10000, 2000, 300, 40, 5);
188 EXPECT_EQ(12345, a.Perform(make_tuple(SumOf5Functor())));
189}
190
191// Tests using InvokeArgument with a 6-ary function.
192TEST(InvokeArgumentTest, Function6) {
193 Action<int(int(*)(int, int, int, int, int, int))> a = // NOLINT
194 InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
195 EXPECT_EQ(123456, a.Perform(make_tuple(&SumOf6)));
196}
197
198// Tests using InvokeArgument with a 6-ary functor.
199TEST(InvokeArgumentTest, Functor6) {
200 Action<int(SumOf6Functor)> a = // NOLINT
201 InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
202 EXPECT_EQ(123456, a.Perform(make_tuple(SumOf6Functor())));
203}
204
205// Tests using InvokeArgument with a 7-ary function.
206TEST(InvokeArgumentTest, Function7) {
207 Action<string(string(*)(const char*, const char*, const char*,
208 const char*, const char*, const char*,
209 const char*))> a =
210 InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7");
211 EXPECT_EQ("1234567", a.Perform(make_tuple(&Concat7)));
212}
213
214// Tests using InvokeArgument with a 8-ary function.
215TEST(InvokeArgumentTest, Function8) {
216 Action<string(string(*)(const char*, const char*, const char*,
217 const char*, const char*, const char*,
218 const char*, const char*))> a =
219 InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8");
220 EXPECT_EQ("12345678", a.Perform(make_tuple(&Concat8)));
221}
222
223// Tests using InvokeArgument with a 9-ary function.
224TEST(InvokeArgumentTest, Function9) {
225 Action<string(string(*)(const char*, const char*, const char*,
226 const char*, const char*, const char*,
227 const char*, const char*, const char*))> a =
228 InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9");
229 EXPECT_EQ("123456789", a.Perform(make_tuple(&Concat9)));
230}
231
232// Tests using InvokeArgument with a 10-ary function.
233TEST(InvokeArgumentTest, Function10) {
234 Action<string(string(*)(const char*, const char*, const char*,
235 const char*, const char*, const char*,
236 const char*, const char*, const char*,
237 const char*))> a =
238 InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
239 EXPECT_EQ("1234567890", a.Perform(make_tuple(&Concat10)));
240}
241
242// Tests using InvokeArgument with a function that takes a pointer argument.
243TEST(InvokeArgumentTest, ByPointerFunction) {
244 Action<const char*(const char*(*)(const char* input, short n))> a = // NOLINT
245 InvokeArgument<0>(static_cast<const char*>("Hi"), 1);
246 EXPECT_STREQ("i", a.Perform(make_tuple(&Binary)));
247}
248
249// Tests using InvokeArgument with a function that takes a const char*
250// by passing it a C-string literal.
251TEST(InvokeArgumentTest, FunctionWithCStringLiteral) {
252 Action<const char*(const char*(*)(const char* input, short n))> a = // NOLINT
253 InvokeArgument<0>("Hi", 1);
254 EXPECT_STREQ("i", a.Perform(make_tuple(&Binary)));
255}
256
257// Tests using InvokeArgument with a function that takes a const reference.
258TEST(InvokeArgumentTest, ByConstReferenceFunction) {
259 Action<bool(bool(*function)(const string& s))> a = // NOLINT
260 InvokeArgument<0>(string("Hi"));
261 // When action 'a' is constructed, it makes a copy of the temporary
262 // string object passed to it, so it's OK to use 'a' later, when the
263 // temporary object has already died.
264 EXPECT_TRUE(a.Perform(make_tuple(&ByConstRef)));
265}
266
267// Tests using InvokeArgument with ByRef() and a function that takes a
268// const reference.
269TEST(InvokeArgumentTest, ByExplicitConstReferenceFunction) {
270 Action<bool(bool(*)(const double& x))> a = // NOLINT
271 InvokeArgument<0>(ByRef(g_double));
272 // The above line calls ByRef() on a const value.
273 EXPECT_TRUE(a.Perform(make_tuple(&ReferencesGlobalDouble)));
274
275 double x = 0;
276 a = InvokeArgument<0>(ByRef(x)); // This calls ByRef() on a non-const.
277 EXPECT_FALSE(a.Perform(make_tuple(&ReferencesGlobalDouble)));
278}
279
zhanyong.wana18423e2009-07-22 23:58:19 +0000280// Tests using WithArgs and with an action that takes 1 argument.
shiqiane35fdd92008-12-10 05:08:54 +0000281TEST(WithArgsTest, OneArg) {
282 Action<bool(double x, int n)> a = WithArgs<1>(Invoke(Unary)); // NOLINT
283 EXPECT_TRUE(a.Perform(make_tuple(1.5, -1)));
284 EXPECT_FALSE(a.Perform(make_tuple(1.5, 1)));
shiqiane35fdd92008-12-10 05:08:54 +0000285}
286
287// Tests using WithArgs with an action that takes 2 arguments.
288TEST(WithArgsTest, TwoArgs) {
289 Action<const char*(const char* s, double x, int n)> a =
290 WithArgs<0, 2>(Invoke(Binary));
291 const char s[] = "Hello";
zhanyong.wan90c90f92009-06-17 22:11:04 +0000292 EXPECT_EQ(s + 2, a.Perform(make_tuple(CharPtr(s), 0.5, 2)));
shiqiane35fdd92008-12-10 05:08:54 +0000293}
294
295// Tests using WithArgs with an action that takes 3 arguments.
296TEST(WithArgsTest, ThreeArgs) {
297 Action<int(int, double, char, short)> a = // NOLINT
298 WithArgs<0, 2, 3>(Invoke(Ternary));
299 EXPECT_EQ(123, a.Perform(make_tuple(100, 6.5, 20, 3)));
300}
301
302// Tests using WithArgs with an action that takes 4 arguments.
303TEST(WithArgsTest, FourArgs) {
304 Action<string(const char*, const char*, double, const char*, const char*)> a =
305 WithArgs<4, 3, 1, 0>(Invoke(Concat4));
zhanyong.wan90c90f92009-06-17 22:11:04 +0000306 EXPECT_EQ("4310", a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), 2.5,
307 CharPtr("3"), CharPtr("4"))));
shiqiane35fdd92008-12-10 05:08:54 +0000308}
309
310// Tests using WithArgs with an action that takes 5 arguments.
311TEST(WithArgsTest, FiveArgs) {
312 Action<string(const char*, const char*, const char*,
313 const char*, const char*)> a =
314 WithArgs<4, 3, 2, 1, 0>(Invoke(Concat5));
zhanyong.wan90c90f92009-06-17 22:11:04 +0000315 EXPECT_EQ("43210",
316 a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
317 CharPtr("3"), CharPtr("4"))));
shiqiane35fdd92008-12-10 05:08:54 +0000318}
319
320// Tests using WithArgs with an action that takes 6 arguments.
321TEST(WithArgsTest, SixArgs) {
322 Action<string(const char*, const char*, const char*)> a =
323 WithArgs<0, 1, 2, 2, 1, 0>(Invoke(Concat6));
zhanyong.wan90c90f92009-06-17 22:11:04 +0000324 EXPECT_EQ("012210",
325 a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"))));
shiqiane35fdd92008-12-10 05:08:54 +0000326}
327
328// Tests using WithArgs with an action that takes 7 arguments.
329TEST(WithArgsTest, SevenArgs) {
330 Action<string(const char*, const char*, const char*, const char*)> a =
331 WithArgs<0, 1, 2, 3, 2, 1, 0>(Invoke(Concat7));
zhanyong.wan90c90f92009-06-17 22:11:04 +0000332 EXPECT_EQ("0123210",
333 a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
334 CharPtr("3"))));
shiqiane35fdd92008-12-10 05:08:54 +0000335}
336
337// Tests using WithArgs with an action that takes 8 arguments.
338TEST(WithArgsTest, EightArgs) {
339 Action<string(const char*, const char*, const char*, const char*)> a =
340 WithArgs<0, 1, 2, 3, 0, 1, 2, 3>(Invoke(Concat8));
zhanyong.wan90c90f92009-06-17 22:11:04 +0000341 EXPECT_EQ("01230123",
342 a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
343 CharPtr("3"))));
shiqiane35fdd92008-12-10 05:08:54 +0000344}
345
346// Tests using WithArgs with an action that takes 9 arguments.
347TEST(WithArgsTest, NineArgs) {
348 Action<string(const char*, const char*, const char*, const char*)> a =
349 WithArgs<0, 1, 2, 3, 1, 2, 3, 2, 3>(Invoke(Concat9));
zhanyong.wan90c90f92009-06-17 22:11:04 +0000350 EXPECT_EQ("012312323",
351 a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
352 CharPtr("3"))));
shiqiane35fdd92008-12-10 05:08:54 +0000353}
354
355// Tests using WithArgs with an action that takes 10 arguments.
356TEST(WithArgsTest, TenArgs) {
357 Action<string(const char*, const char*, const char*, const char*)> a =
358 WithArgs<0, 1, 2, 3, 2, 1, 0, 1, 2, 3>(Invoke(Concat10));
zhanyong.wan90c90f92009-06-17 22:11:04 +0000359 EXPECT_EQ("0123210123",
360 a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
361 CharPtr("3"))));
shiqiane35fdd92008-12-10 05:08:54 +0000362}
363
364// Tests using WithArgs with an action that is not Invoke().
365class SubstractAction : public ActionInterface<int(int, int)> { // NOLINT
366 public:
367 virtual int Perform(const tuple<int, int>& args) {
368 return get<0>(args) - get<1>(args);
369 }
370};
371
372TEST(WithArgsTest, NonInvokeAction) {
373 Action<int(const string&, int, int)> a = // NOLINT
374 WithArgs<2, 1>(MakeAction(new SubstractAction));
zhanyong.wan90c90f92009-06-17 22:11:04 +0000375 EXPECT_EQ(8, a.Perform(make_tuple(CharPtr("hi"), 2, 10)));
shiqiane35fdd92008-12-10 05:08:54 +0000376}
377
378// Tests using WithArgs to pass all original arguments in the original order.
379TEST(WithArgsTest, Identity) {
380 Action<int(int x, char y, short z)> a = // NOLINT
381 WithArgs<0, 1, 2>(Invoke(Ternary));
382 EXPECT_EQ(123, a.Perform(make_tuple(100, 20, 3)));
383}
384
385// Tests using WithArgs with repeated arguments.
386TEST(WithArgsTest, RepeatedArguments) {
387 Action<int(bool, int m, int n)> a = // NOLINT
388 WithArgs<1, 1, 1, 1>(Invoke(SumOf4));
389 EXPECT_EQ(4, a.Perform(make_tuple(false, 1, 10)));
390}
391
392// Tests using WithArgs with reversed argument order.
393TEST(WithArgsTest, ReversedArgumentOrder) {
394 Action<const char*(short n, const char* input)> a = // NOLINT
395 WithArgs<1, 0>(Invoke(Binary));
396 const char s[] = "Hello";
zhanyong.wan90c90f92009-06-17 22:11:04 +0000397 EXPECT_EQ(s + 2, a.Perform(make_tuple(2, CharPtr(s))));
shiqiane35fdd92008-12-10 05:08:54 +0000398}
399
400// Tests using WithArgs with compatible, but not identical, argument types.
401TEST(WithArgsTest, ArgsOfCompatibleTypes) {
402 Action<long(short x, int y, double z, char c)> a = // NOLINT
403 WithArgs<0, 1, 3>(Invoke(Ternary));
404 EXPECT_EQ(123, a.Perform(make_tuple(100, 20, 5.6, 3)));
405}
406
407// Tests using WithArgs with an action that returns void.
408TEST(WithArgsTest, VoidAction) {
409 Action<void(double x, char c, int n)> a = WithArgs<2, 1>(Invoke(VoidBinary));
410 g_done = false;
411 a.Perform(make_tuple(1.5, 'a', 3));
412 EXPECT_TRUE(g_done);
413}
414
415// Tests DoAll(a1, a2).
416TEST(DoAllTest, TwoActions) {
417 int n = 0;
418 Action<int(int*)> a = DoAll(SetArgumentPointee<0>(1), // NOLINT
419 Return(2));
420 EXPECT_EQ(2, a.Perform(make_tuple(&n)));
421 EXPECT_EQ(1, n);
422}
423
424// Tests DoAll(a1, a2, a3).
425TEST(DoAllTest, ThreeActions) {
426 int m = 0, n = 0;
427 Action<int(int*, int*)> a = DoAll(SetArgumentPointee<0>(1), // NOLINT
428 SetArgumentPointee<1>(2),
429 Return(3));
430 EXPECT_EQ(3, a.Perform(make_tuple(&m, &n)));
431 EXPECT_EQ(1, m);
432 EXPECT_EQ(2, n);
433}
434
435// Tests DoAll(a1, a2, a3, a4).
436TEST(DoAllTest, FourActions) {
437 int m = 0, n = 0;
438 char ch = '\0';
439 Action<int(int*, int*, char*)> a = // NOLINT
440 DoAll(SetArgumentPointee<0>(1),
441 SetArgumentPointee<1>(2),
442 SetArgumentPointee<2>('a'),
443 Return(3));
444 EXPECT_EQ(3, a.Perform(make_tuple(&m, &n, &ch)));
445 EXPECT_EQ(1, m);
446 EXPECT_EQ(2, n);
447 EXPECT_EQ('a', ch);
448}
449
450// Tests DoAll(a1, a2, a3, a4, a5).
451TEST(DoAllTest, FiveActions) {
452 int m = 0, n = 0;
453 char a = '\0', b = '\0';
454 Action<int(int*, int*, char*, char*)> action = // NOLINT
455 DoAll(SetArgumentPointee<0>(1),
456 SetArgumentPointee<1>(2),
457 SetArgumentPointee<2>('a'),
458 SetArgumentPointee<3>('b'),
459 Return(3));
460 EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b)));
461 EXPECT_EQ(1, m);
462 EXPECT_EQ(2, n);
463 EXPECT_EQ('a', a);
464 EXPECT_EQ('b', b);
465}
466
467// Tests DoAll(a1, a2, ..., a6).
468TEST(DoAllTest, SixActions) {
469 int m = 0, n = 0;
470 char a = '\0', b = '\0', c = '\0';
471 Action<int(int*, int*, char*, char*, char*)> action = // NOLINT
472 DoAll(SetArgumentPointee<0>(1),
473 SetArgumentPointee<1>(2),
474 SetArgumentPointee<2>('a'),
475 SetArgumentPointee<3>('b'),
476 SetArgumentPointee<4>('c'),
477 Return(3));
478 EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c)));
479 EXPECT_EQ(1, m);
480 EXPECT_EQ(2, n);
481 EXPECT_EQ('a', a);
482 EXPECT_EQ('b', b);
483 EXPECT_EQ('c', c);
484}
485
486// Tests DoAll(a1, a2, ..., a7).
487TEST(DoAllTest, SevenActions) {
488 int m = 0, n = 0;
489 char a = '\0', b = '\0', c = '\0', d = '\0';
490 Action<int(int*, int*, char*, char*, char*, char*)> action = // NOLINT
491 DoAll(SetArgumentPointee<0>(1),
492 SetArgumentPointee<1>(2),
493 SetArgumentPointee<2>('a'),
494 SetArgumentPointee<3>('b'),
495 SetArgumentPointee<4>('c'),
496 SetArgumentPointee<5>('d'),
497 Return(3));
498 EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d)));
499 EXPECT_EQ(1, m);
500 EXPECT_EQ(2, n);
501 EXPECT_EQ('a', a);
502 EXPECT_EQ('b', b);
503 EXPECT_EQ('c', c);
504 EXPECT_EQ('d', d);
505}
506
507// Tests DoAll(a1, a2, ..., a8).
508TEST(DoAllTest, EightActions) {
509 int m = 0, n = 0;
510 char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0';
511 Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
512 char*)> action =
513 DoAll(SetArgumentPointee<0>(1),
514 SetArgumentPointee<1>(2),
515 SetArgumentPointee<2>('a'),
516 SetArgumentPointee<3>('b'),
517 SetArgumentPointee<4>('c'),
518 SetArgumentPointee<5>('d'),
519 SetArgumentPointee<6>('e'),
520 Return(3));
521 EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d, &e)));
522 EXPECT_EQ(1, m);
523 EXPECT_EQ(2, n);
524 EXPECT_EQ('a', a);
525 EXPECT_EQ('b', b);
526 EXPECT_EQ('c', c);
527 EXPECT_EQ('d', d);
528 EXPECT_EQ('e', e);
529}
530
531// Tests DoAll(a1, a2, ..., a9).
532TEST(DoAllTest, NineActions) {
533 int m = 0, n = 0;
534 char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0', f = '\0';
535 Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
536 char*, char*)> action =
537 DoAll(SetArgumentPointee<0>(1),
538 SetArgumentPointee<1>(2),
539 SetArgumentPointee<2>('a'),
540 SetArgumentPointee<3>('b'),
541 SetArgumentPointee<4>('c'),
542 SetArgumentPointee<5>('d'),
543 SetArgumentPointee<6>('e'),
544 SetArgumentPointee<7>('f'),
545 Return(3));
546 EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d, &e, &f)));
547 EXPECT_EQ(1, m);
548 EXPECT_EQ(2, n);
549 EXPECT_EQ('a', a);
550 EXPECT_EQ('b', b);
551 EXPECT_EQ('c', c);
552 EXPECT_EQ('d', d);
553 EXPECT_EQ('e', e);
554 EXPECT_EQ('f', f);
555}
556
557// Tests DoAll(a1, a2, ..., a10).
558TEST(DoAllTest, TenActions) {
559 int m = 0, n = 0;
560 char a = '\0', b = '\0', c = '\0', d = '\0';
561 char e = '\0', f = '\0', g = '\0';
562 Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
563 char*, char*, char*)> action =
564 DoAll(SetArgumentPointee<0>(1),
565 SetArgumentPointee<1>(2),
566 SetArgumentPointee<2>('a'),
567 SetArgumentPointee<3>('b'),
568 SetArgumentPointee<4>('c'),
569 SetArgumentPointee<5>('d'),
570 SetArgumentPointee<6>('e'),
571 SetArgumentPointee<7>('f'),
572 SetArgumentPointee<8>('g'),
573 Return(3));
574 EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d, &e, &f, &g)));
575 EXPECT_EQ(1, m);
576 EXPECT_EQ(2, n);
577 EXPECT_EQ('a', a);
578 EXPECT_EQ('b', b);
579 EXPECT_EQ('c', c);
580 EXPECT_EQ('d', d);
581 EXPECT_EQ('e', e);
582 EXPECT_EQ('f', f);
583 EXPECT_EQ('g', g);
584}
585
shiqian326aa562009-01-09 21:43:57 +0000586// Tests the ACTION*() macro family.
587
588// Tests that ACTION() can define an action that doesn't reference the
589// mock function arguments.
590ACTION(Return5) { return 5; }
591
592TEST(ActionMacroTest, WorksWhenNotReferencingArguments) {
593 Action<double()> a1 = Return5();
594 EXPECT_DOUBLE_EQ(5, a1.Perform(make_tuple()));
595
596 Action<int(double, bool)> a2 = Return5();
597 EXPECT_EQ(5, a2.Perform(make_tuple(1, true)));
598}
599
600// Tests that ACTION() can define an action that returns void.
601ACTION(IncrementArg1) { (*arg1)++; }
602
603TEST(ActionMacroTest, WorksWhenReturningVoid) {
604 Action<void(int, int*)> a1 = IncrementArg1();
605 int n = 0;
606 a1.Perform(make_tuple(5, &n));
607 EXPECT_EQ(1, n);
608}
609
610// Tests that the body of ACTION() can reference the type of the
611// argument.
612ACTION(IncrementArg2) {
613 StaticAssertTypeEq<int*, arg2_type>();
614 arg2_type temp = arg2;
615 (*temp)++;
616}
617
618TEST(ActionMacroTest, CanReferenceArgumentType) {
619 Action<void(int, bool, int*)> a1 = IncrementArg2();
620 int n = 0;
621 a1.Perform(make_tuple(5, false, &n));
622 EXPECT_EQ(1, n);
623}
624
625// Tests that the body of ACTION() can reference the argument tuple
626// via args_type and args.
627ACTION(Sum2) {
628 StaticAssertTypeEq< ::std::tr1::tuple<int, char, int*>, args_type>();
629 args_type args_copy = args;
630 return get<0>(args_copy) + get<1>(args_copy);
631}
632
633TEST(ActionMacroTest, CanReferenceArgumentTuple) {
634 Action<int(int, char, int*)> a1 = Sum2();
635 int dummy = 0;
636 EXPECT_EQ(11, a1.Perform(make_tuple(5, static_cast<char>(6), &dummy)));
637}
638
639// Tests that the body of ACTION() can reference the mock function
640// type.
641int Dummy(bool flag) { return flag? 1 : 0; }
642
643ACTION(InvokeDummy) {
644 StaticAssertTypeEq<int(bool), function_type>();
645 function_type* fp = &Dummy;
646 return (*fp)(true);
647}
648
649TEST(ActionMacroTest, CanReferenceMockFunctionType) {
650 Action<int(bool)> a1 = InvokeDummy();
651 EXPECT_EQ(1, a1.Perform(make_tuple(true)));
652 EXPECT_EQ(1, a1.Perform(make_tuple(false)));
653}
654
655// Tests that the body of ACTION() can reference the mock function's
656// return type.
657ACTION(InvokeDummy2) {
658 StaticAssertTypeEq<int, return_type>();
659 return_type result = Dummy(true);
660 return result;
661}
662
663TEST(ActionMacroTest, CanReferenceMockFunctionReturnType) {
664 Action<int(bool)> a1 = InvokeDummy2();
665 EXPECT_EQ(1, a1.Perform(make_tuple(true)));
666 EXPECT_EQ(1, a1.Perform(make_tuple(false)));
667}
668
zhanyong.wan7f4c2c02009-02-19 22:38:27 +0000669// Tests that ACTION() works for arguments passed by const reference.
670ACTION(ReturnAddrOfConstBoolReferenceArg) {
671 StaticAssertTypeEq<const bool&, arg1_type>();
672 return &arg1;
673}
674
675TEST(ActionMacroTest, WorksForConstReferenceArg) {
676 Action<const bool*(int, const bool&)> a = ReturnAddrOfConstBoolReferenceArg();
677 const bool b = false;
678 EXPECT_EQ(&b, a.Perform(tuple<int, const bool&>(0, b)));
679}
680
681// Tests that ACTION() works for arguments passed by non-const reference.
682ACTION(ReturnAddrOfIntReferenceArg) {
683 StaticAssertTypeEq<int&, arg0_type>();
684 return &arg0;
685}
686
687TEST(ActionMacroTest, WorksForNonConstReferenceArg) {
688 Action<int*(int&, bool, int)> a = ReturnAddrOfIntReferenceArg();
689 int n = 0;
690 EXPECT_EQ(&n, a.Perform(tuple<int&, bool, int>(n, true, 1)));
691}
692
shiqian326aa562009-01-09 21:43:57 +0000693// Tests that ACTION() can be used in a namespace.
694namespace action_test {
695ACTION(Sum) { return arg0 + arg1; }
696} // namespace action_test
697
698TEST(ActionMacroTest, WorksInNamespace) {
699 Action<int(int, int)> a1 = action_test::Sum();
700 EXPECT_EQ(3, a1.Perform(make_tuple(1, 2)));
701}
702
703// Tests that the same ACTION definition works for mock functions with
704// different argument numbers.
705ACTION(PlusTwo) { return arg0 + 2; }
706
707TEST(ActionMacroTest, WorksForDifferentArgumentNumbers) {
708 Action<int(int)> a1 = PlusTwo();
709 EXPECT_EQ(4, a1.Perform(make_tuple(2)));
710
711 Action<double(float, void*)> a2 = PlusTwo();
712 int dummy;
713 EXPECT_DOUBLE_EQ(6, a2.Perform(make_tuple(4.0f, &dummy)));
714}
715
716// Tests that ACTION_P can define a parameterized action.
717ACTION_P(Plus, n) { return arg0 + n; }
718
719TEST(ActionPMacroTest, DefinesParameterizedAction) {
720 Action<int(int m, bool t)> a1 = Plus(9);
721 EXPECT_EQ(10, a1.Perform(make_tuple(1, true)));
722}
723
724// Tests that the body of ACTION_P can reference the argument types
725// and the parameter type.
726ACTION_P(TypedPlus, n) {
727 arg0_type t1 = arg0;
728 n_type t2 = n;
729 return t1 + t2;
730}
731
732TEST(ActionPMacroTest, CanReferenceArgumentAndParameterTypes) {
733 Action<int(char m, bool t)> a1 = TypedPlus(9);
734 EXPECT_EQ(10, a1.Perform(make_tuple(static_cast<char>(1), true)));
735}
736
737// Tests that a parameterized action can be used in any mock function
738// whose type is compatible.
739TEST(ActionPMacroTest, WorksInCompatibleMockFunction) {
740 Action<std::string(const std::string& s)> a1 = Plus("tail");
741 const std::string re = "re";
742 EXPECT_EQ("retail", a1.Perform(make_tuple(re)));
743}
744
745// Tests that we can use ACTION*() to define actions overloaded on the
746// number of parameters.
747
748ACTION(OverloadedAction) { return arg0 ? arg1 : "hello"; }
749
750ACTION_P(OverloadedAction, default_value) {
751 return arg0 ? arg1 : default_value;
752}
753
754ACTION_P2(OverloadedAction, true_value, false_value) {
755 return arg0 ? true_value : false_value;
756}
757
758TEST(ActionMacroTest, CanDefineOverloadedActions) {
759 typedef Action<const char*(bool, const char*)> MyAction;
760
761 const MyAction a1 = OverloadedAction();
zhanyong.wan90c90f92009-06-17 22:11:04 +0000762 EXPECT_STREQ("hello", a1.Perform(make_tuple(false, CharPtr("world"))));
763 EXPECT_STREQ("world", a1.Perform(make_tuple(true, CharPtr("world"))));
shiqian326aa562009-01-09 21:43:57 +0000764
765 const MyAction a2 = OverloadedAction("hi");
zhanyong.wan90c90f92009-06-17 22:11:04 +0000766 EXPECT_STREQ("hi", a2.Perform(make_tuple(false, CharPtr("world"))));
767 EXPECT_STREQ("world", a2.Perform(make_tuple(true, CharPtr("world"))));
shiqian326aa562009-01-09 21:43:57 +0000768
769 const MyAction a3 = OverloadedAction("hi", "you");
zhanyong.wan90c90f92009-06-17 22:11:04 +0000770 EXPECT_STREQ("hi", a3.Perform(make_tuple(true, CharPtr("world"))));
771 EXPECT_STREQ("you", a3.Perform(make_tuple(false, CharPtr("world"))));
shiqian326aa562009-01-09 21:43:57 +0000772}
773
774// Tests ACTION_Pn where n >= 3.
775
776ACTION_P3(Plus, m, n, k) { return arg0 + m + n + k; }
777
778TEST(ActionPnMacroTest, WorksFor3Parameters) {
779 Action<double(int m, bool t)> a1 = Plus(100, 20, 3.4);
780 EXPECT_DOUBLE_EQ(3123.4, a1.Perform(make_tuple(3000, true)));
781
782 Action<std::string(const std::string& s)> a2 = Plus("tail", "-", ">");
783 const std::string re = "re";
784 EXPECT_EQ("retail->", a2.Perform(make_tuple(re)));
785}
786
787ACTION_P4(Plus, p0, p1, p2, p3) { return arg0 + p0 + p1 + p2 + p3; }
788
789TEST(ActionPnMacroTest, WorksFor4Parameters) {
790 Action<int(int)> a1 = Plus(1, 2, 3, 4);
791 EXPECT_EQ(10 + 1 + 2 + 3 + 4, a1.Perform(make_tuple(10)));
792}
793
794ACTION_P5(Plus, p0, p1, p2, p3, p4) { return arg0 + p0 + p1 + p2 + p3 + p4; }
795
796TEST(ActionPnMacroTest, WorksFor5Parameters) {
797 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5);
798 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5, a1.Perform(make_tuple(10)));
799}
800
801ACTION_P6(Plus, p0, p1, p2, p3, p4, p5) {
802 return arg0 + p0 + p1 + p2 + p3 + p4 + p5;
803}
804
805TEST(ActionPnMacroTest, WorksFor6Parameters) {
806 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6);
807 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6, a1.Perform(make_tuple(10)));
808}
809
810ACTION_P7(Plus, p0, p1, p2, p3, p4, p5, p6) {
811 return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6;
812}
813
814TEST(ActionPnMacroTest, WorksFor7Parameters) {
815 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7);
816 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7, a1.Perform(make_tuple(10)));
817}
818
819ACTION_P8(Plus, p0, p1, p2, p3, p4, p5, p6, p7) {
820 return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7;
821}
822
823TEST(ActionPnMacroTest, WorksFor8Parameters) {
824 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8);
825 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8, a1.Perform(make_tuple(10)));
826}
827
828ACTION_P9(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8) {
829 return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8;
830}
831
832TEST(ActionPnMacroTest, WorksFor9Parameters) {
833 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9);
834 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9, a1.Perform(make_tuple(10)));
835}
836
837ACTION_P10(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8, last_param) {
838 arg0_type t0 = arg0;
839 last_param_type t9 = last_param;
840 return t0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + t9;
841}
842
843TEST(ActionPnMacroTest, WorksFor10Parameters) {
844 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
845 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10,
846 a1.Perform(make_tuple(10)));
847}
848
849// Tests that the action body can promote the parameter types.
850
851ACTION_P2(PadArgument, prefix, suffix) {
852 // The following lines promote the two parameters to desired types.
853 std::string prefix_str(prefix);
854 char suffix_char(suffix);
855 return prefix_str + arg0 + suffix_char;
856}
857
858TEST(ActionPnMacroTest, SimpleTypePromotion) {
859 Action<std::string(const char*)> no_promo =
860 PadArgument(std::string("foo"), 'r');
861 Action<std::string(const char*)> promo =
862 PadArgument("foo", static_cast<int>('r'));
zhanyong.wan90c90f92009-06-17 22:11:04 +0000863 EXPECT_EQ("foobar", no_promo.Perform(make_tuple(CharPtr("ba"))));
864 EXPECT_EQ("foobar", promo.Perform(make_tuple(CharPtr("ba"))));
shiqian326aa562009-01-09 21:43:57 +0000865}
866
867// Tests that we can partially restrict parameter types using a
868// straight-forward pattern.
869
870// Defines a generic action that doesn't restrict the types of its
871// parameters.
872ACTION_P3(ConcatImpl, a, b, c) {
873 std::stringstream ss;
874 ss << a << b << c;
875 return ss.str();
876}
877
878// Next, we try to restrict that either the first parameter is a
879// string, or the second parameter is an int.
880
881// Defines a partially specialized wrapper that restricts the first
882// parameter to std::string.
883template <typename T1, typename T2>
884// ConcatImplActionP3 is the class template ACTION_P3 uses to
885// implement ConcatImpl. We shouldn't change the name as this
886// pattern requires the user to use it directly.
887ConcatImplActionP3<std::string, T1, T2>
888Concat(const std::string& a, T1 b, T2 c) {
889 if (true) {
890 // This branch verifies that ConcatImpl() can be invoked without
891 // explicit template arguments.
892 return ConcatImpl(a, b, c);
893 } else {
894 // This branch verifies that ConcatImpl() can also be invoked with
895 // explicit template arguments. It doesn't really need to be
896 // executed as this is a compile-time verification.
897 return ConcatImpl<std::string, T1, T2>(a, b, c);
898 }
899}
900
901// Defines another partially specialized wrapper that restricts the
902// second parameter to int.
903template <typename T1, typename T2>
904ConcatImplActionP3<T1, int, T2>
905Concat(T1 a, int b, T2 c) {
906 return ConcatImpl(a, b, c);
907}
908
909TEST(ActionPnMacroTest, CanPartiallyRestrictParameterTypes) {
910 Action<const std::string()> a1 = Concat("Hello", "1", 2);
911 EXPECT_EQ("Hello12", a1.Perform(make_tuple()));
912
913 a1 = Concat(1, 2, 3);
914 EXPECT_EQ("123", a1.Perform(make_tuple()));
915}
916
917// Verifies the type of an ACTION*.
918
919ACTION(DoFoo) {}
920ACTION_P(DoFoo, p) {}
921ACTION_P2(DoFoo, p0, p1) {}
922
923TEST(ActionPnMacroTest, TypesAreCorrect) {
924 // DoFoo() must be assignable to a DoFooAction variable.
925 DoFooAction a0 = DoFoo();
926
927 // DoFoo(1) must be assignable to a DoFooActionP variable.
928 DoFooActionP<int> a1 = DoFoo(1);
929
930 // DoFoo(p1, ..., pk) must be assignable to a DoFooActionPk
931 // variable, and so on.
932 DoFooActionP2<int, char> a2 = DoFoo(1, '2');
933 PlusActionP3<int, int, char> a3 = Plus(1, 2, '3');
934 PlusActionP4<int, int, int, char> a4 = Plus(1, 2, 3, '4');
935 PlusActionP5<int, int, int, int, char> a5 = Plus(1, 2, 3, 4, '5');
936 PlusActionP6<int, int, int, int, int, char> a6 = Plus(1, 2, 3, 4, 5, '6');
937 PlusActionP7<int, int, int, int, int, int, char> a7 =
938 Plus(1, 2, 3, 4, 5, 6, '7');
939 PlusActionP8<int, int, int, int, int, int, int, char> a8 =
940 Plus(1, 2, 3, 4, 5, 6, 7, '8');
941 PlusActionP9<int, int, int, int, int, int, int, int, char> a9 =
942 Plus(1, 2, 3, 4, 5, 6, 7, 8, '9');
943 PlusActionP10<int, int, int, int, int, int, int, int, int, char> a10 =
944 Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
945}
946
zhanyong.wanc069d7f2009-02-02 20:51:53 +0000947// Tests that an ACTION_P*() action can be explicitly instantiated
948// with reference-typed parameters.
949
950ACTION_P(Plus1, x) { return x; }
951ACTION_P2(Plus2, x, y) { return x + y; }
952ACTION_P3(Plus3, x, y, z) { return x + y + z; }
953ACTION_P10(Plus10, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
954 return a0 + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9;
955}
956
957TEST(ActionPnMacroTest, CanExplicitlyInstantiateWithReferenceTypes) {
958 int x = 1, y = 2, z = 3;
959 const tuple<> empty = make_tuple();
960
961 Action<int()> a = Plus1<int&>(x);
962 EXPECT_EQ(1, a.Perform(empty));
963
964 a = Plus2<const int&, int&>(x, y);
965 EXPECT_EQ(3, a.Perform(empty));
966
967 a = Plus3<int&, const int&, int&>(x, y, z);
968 EXPECT_EQ(6, a.Perform(empty));
969
970 int n[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
971 a = Plus10<const int&, int&, const int&, int&, const int&, int&, const int&,
972 int&, const int&, int&>(n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7],
973 n[8], n[9]);
974 EXPECT_EQ(55, a.Perform(empty));
975}
976
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +0000977class NullaryConstructorClass {
978 public:
979 NullaryConstructorClass() : value_(123) {}
980 int value_;
981};
982
983// Tests using ReturnNew() with a nullary constructor.
984TEST(ReturnNewTest, NoArgs) {
985 Action<NullaryConstructorClass*()> a = ReturnNew<NullaryConstructorClass>();
986 NullaryConstructorClass* c = a.Perform(make_tuple());
987 EXPECT_EQ(123, c->value_);
988 delete c;
989}
990
991class UnaryConstructorClass {
992 public:
993 explicit UnaryConstructorClass(int value) : value_(value) {}
994 int value_;
995};
996
997// Tests using ReturnNew() with a unary constructor.
998TEST(ReturnNewTest, Unary) {
999 Action<UnaryConstructorClass*()> a = ReturnNew<UnaryConstructorClass>(4000);
1000 UnaryConstructorClass* c = a.Perform(make_tuple());
1001 EXPECT_EQ(4000, c->value_);
1002 delete c;
1003}
1004
1005TEST(ReturnNewTest, UnaryWorksWhenMockMethodHasArgs) {
1006 Action<UnaryConstructorClass*(bool, int)> a =
1007 ReturnNew<UnaryConstructorClass>(4000);
1008 UnaryConstructorClass* c = a.Perform(make_tuple(false, 5));
1009 EXPECT_EQ(4000, c->value_);
1010 delete c;
1011}
1012
1013TEST(ReturnNewTest, UnaryWorksWhenMockMethodReturnsPointerToConst) {
1014 Action<const UnaryConstructorClass*()> a =
1015 ReturnNew<UnaryConstructorClass>(4000);
1016 const UnaryConstructorClass* c = a.Perform(make_tuple());
1017 EXPECT_EQ(4000, c->value_);
1018 delete c;
1019}
1020
1021class TenArgConstructorClass {
1022 public:
1023 TenArgConstructorClass(int a1, int a2, int a3, int a4, int a5,
1024 int a6, int a7, int a8, int a9, int a10)
1025 : value_(a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10) {
1026 }
1027 int value_;
1028};
1029
1030// Tests using ReturnNew() with a 10-argument constructor.
1031TEST(ReturnNewTest, ConstructorThatTakes10Arguments) {
1032 Action<TenArgConstructorClass*()> a =
1033 ReturnNew<TenArgConstructorClass>(1000000000, 200000000, 30000000,
1034 4000000, 500000, 60000,
1035 7000, 800, 90, 0);
1036 TenArgConstructorClass* c = a.Perform(make_tuple());
1037 EXPECT_EQ(1234567890, c->value_);
1038 delete c;
1039}
1040
zhanyong.wan18490652009-05-11 18:54:08 +00001041// Tests that ACTION_TEMPLATE works when there is no value parameter.
1042ACTION_TEMPLATE(CreateNew,
1043 HAS_1_TEMPLATE_PARAMS(typename, T),
1044 AND_0_VALUE_PARAMS()) {
1045 return new T;
1046}
1047
1048TEST(ActionTemplateTest, WorksWithoutValueParam) {
1049 const Action<int*()> a = CreateNew<int>();
1050 int* p = a.Perform(make_tuple());
1051 delete p;
1052}
1053
1054// Tests that ACTION_TEMPLATE works when there are value parameters.
1055ACTION_TEMPLATE(CreateNew,
1056 HAS_1_TEMPLATE_PARAMS(typename, T),
1057 AND_1_VALUE_PARAMS(a0)) {
1058 return new T(a0);
1059}
1060
1061TEST(ActionTemplateTest, WorksWithValueParams) {
1062 const Action<int*()> a = CreateNew<int>(42);
1063 int* p = a.Perform(make_tuple());
1064 EXPECT_EQ(42, *p);
1065 delete p;
1066}
1067
1068// Tests that ACTION_TEMPLATE works for integral template parameters.
1069ACTION_TEMPLATE(MyDeleteArg,
1070 HAS_1_TEMPLATE_PARAMS(int, k),
1071 AND_0_VALUE_PARAMS()) {
1072 delete std::tr1::get<k>(args);
1073}
1074
1075// Resets a bool variable in the destructor.
1076class BoolResetter {
1077 public:
1078 explicit BoolResetter(bool* value) : value_(value) {}
1079 ~BoolResetter() { *value_ = false; }
1080 private:
1081 bool* const value_;
1082};
1083
1084TEST(ActionTemplateTest, WorksForIntegralTemplateParams) {
1085 const Action<void(int*, BoolResetter*)> a = MyDeleteArg<1>();
1086 int n = 0;
1087 bool b = true;
1088 BoolResetter* resetter = new BoolResetter(&b);
1089 a.Perform(make_tuple(&n, resetter));
1090 EXPECT_FALSE(b); // Verifies that resetter is deleted.
1091}
1092
1093// Tests that ACTION_TEMPLATES works for template template parameters.
1094ACTION_TEMPLATE(ReturnSmartPointer,
1095 HAS_1_TEMPLATE_PARAMS(template <typename Pointee> class,
1096 Pointer),
1097 AND_1_VALUE_PARAMS(pointee)) {
1098 return Pointer<pointee_type>(new pointee_type(pointee));
1099}
1100
1101TEST(ActionTemplateTest, WorksForTemplateTemplateParameters) {
1102 using ::testing::internal::linked_ptr;
1103 const Action<linked_ptr<int>()> a = ReturnSmartPointer<linked_ptr>(42);
1104 linked_ptr<int> p = a.Perform(make_tuple());
1105 EXPECT_EQ(42, *p);
1106}
1107
1108// Tests that ACTION_TEMPLATE works for 10 template parameters.
1109template <typename T1, typename T2, typename T3, int k4, bool k5,
1110 unsigned int k6, typename T7, typename T8, typename T9>
1111struct GiantTemplate {
1112 public:
1113 explicit GiantTemplate(int a_value) : value(a_value) {}
1114 int value;
1115};
1116
1117ACTION_TEMPLATE(ReturnGiant,
1118 HAS_10_TEMPLATE_PARAMS(
1119 typename, T1,
1120 typename, T2,
1121 typename, T3,
1122 int, k4,
1123 bool, k5,
1124 unsigned int, k6,
1125 class, T7,
1126 class, T8,
1127 class, T9,
1128 template <typename T> class, T10),
1129 AND_1_VALUE_PARAMS(value)) {
1130 return GiantTemplate<T10<T1>, T2, T3, k4, k5, k6, T7, T8, T9>(value);
1131}
1132
1133TEST(ActionTemplateTest, WorksFor10TemplateParameters) {
1134 using ::testing::internal::linked_ptr;
1135 typedef GiantTemplate<linked_ptr<int>, bool, double, 5,
1136 true, 6, char, unsigned, int> Giant;
1137 const Action<Giant()> a = ReturnGiant<
1138 int, bool, double, 5, true, 6, char, unsigned, int, linked_ptr>(42);
1139 Giant giant = a.Perform(make_tuple());
1140 EXPECT_EQ(42, giant.value);
1141}
1142
1143// Tests that ACTION_TEMPLATE works for 10 value parameters.
1144ACTION_TEMPLATE(ReturnSum,
1145 HAS_1_TEMPLATE_PARAMS(typename, Number),
1146 AND_10_VALUE_PARAMS(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10)) {
1147 return static_cast<Number>(v1) + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10;
1148}
1149
1150TEST(ActionTemplateTest, WorksFor10ValueParameters) {
1151 const Action<int()> a = ReturnSum<int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
1152 EXPECT_EQ(55, a.Perform(make_tuple()));
1153}
1154
1155// Tests that ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded
1156// on the number of value parameters.
1157
1158ACTION(ReturnSum) { return 0; }
1159
1160ACTION_P(ReturnSum, x) { return x; }
1161
1162ACTION_TEMPLATE(ReturnSum,
1163 HAS_1_TEMPLATE_PARAMS(typename, Number),
1164 AND_2_VALUE_PARAMS(v1, v2)) {
1165 return static_cast<Number>(v1) + v2;
1166}
1167
1168ACTION_TEMPLATE(ReturnSum,
1169 HAS_1_TEMPLATE_PARAMS(typename, Number),
1170 AND_3_VALUE_PARAMS(v1, v2, v3)) {
1171 return static_cast<Number>(v1) + v2 + v3;
1172}
1173
1174ACTION_TEMPLATE(ReturnSum,
1175 HAS_2_TEMPLATE_PARAMS(typename, Number, int, k),
1176 AND_4_VALUE_PARAMS(v1, v2, v3, v4)) {
1177 return static_cast<Number>(v1) + v2 + v3 + v4 + k;
1178}
1179
1180TEST(ActionTemplateTest, CanBeOverloadedOnNumberOfValueParameters) {
1181 const Action<int()> a0 = ReturnSum();
1182 const Action<int()> a1 = ReturnSum(1);
1183 const Action<int()> a2 = ReturnSum<int>(1, 2);
1184 const Action<int()> a3 = ReturnSum<int>(1, 2, 3);
1185 const Action<int()> a4 = ReturnSum<int, 10000>(2000, 300, 40, 5);
1186 EXPECT_EQ(0, a0.Perform(make_tuple()));
1187 EXPECT_EQ(1, a1.Perform(make_tuple()));
1188 EXPECT_EQ(3, a2.Perform(make_tuple()));
1189 EXPECT_EQ(6, a3.Perform(make_tuple()));
1190 EXPECT_EQ(12345, a4.Perform(make_tuple()));
1191}
1192
shiqiane35fdd92008-12-10 05:08:54 +00001193} // namespace gmock_generated_actions_test
1194} // namespace testing