blob: 885097c74d8f2b6a0860ad2cd04d55287227d692 [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;
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +000057using testing::DeleteArg;
shiqiane35fdd92008-12-10 05:08:54 +000058using testing::DoAll;
59using testing::Invoke;
60using testing::InvokeArgument;
61using testing::Return;
zhanyong.wan1afe1c72009-07-21 23:26:31 +000062using testing::ReturnArg;
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +000063using testing::ReturnNew;
zhanyong.wan7f4c2c02009-02-19 22:38:27 +000064using testing::SaveArg;
65using testing::SetArgReferee;
shiqiane35fdd92008-12-10 05:08:54 +000066using testing::SetArgumentPointee;
shiqian326aa562009-01-09 21:43:57 +000067using testing::StaticAssertTypeEq;
shiqiane35fdd92008-12-10 05:08:54 +000068using testing::Unused;
69using testing::WithArg;
70using testing::WithArgs;
71using testing::WithoutArgs;
72
73// 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
93void VoidUnary(int n) { g_done = true; }
94
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";
247 EXPECT_EQ(p + 2, a.Perform(make_tuple(p, 2)));
248}
249
250// Tests using Invoke() with a ternary function.
251TEST(InvokeTest, Ternary) {
252 Action<int(int, char, short)> a = Invoke(Ternary); // NOLINT
253 EXPECT_EQ(6, a.Perform(make_tuple(1, '\2', 3)));
254}
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
zhanyong.wan90c90f92009-06-17 22:11:04 +0000274// 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
shiqiane35fdd92008-12-10 05:08:54 +0000278// 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",
zhanyong.wan90c90f92009-06-17 22:11:04 +0000284 a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
285 CharPtr("4"), CharPtr("5"), CharPtr("6"),
286 CharPtr("7"))));
shiqiane35fdd92008-12-10 05:08:54 +0000287}
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",
zhanyong.wan90c90f92009-06-17 22:11:04 +0000295 a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
296 CharPtr("4"), CharPtr("5"), CharPtr("6"),
297 CharPtr("7"), CharPtr("8"))));
shiqiane35fdd92008-12-10 05:08:54 +0000298}
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",
zhanyong.wan90c90f92009-06-17 22:11:04 +0000306 a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
307 CharPtr("4"), CharPtr("5"), CharPtr("6"),
308 CharPtr("7"), CharPtr("8"), CharPtr("9"))));
shiqiane35fdd92008-12-10 05:08:54 +0000309}
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);
zhanyong.wan90c90f92009-06-17 22:11:04 +0000316 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"))));
shiqiane35fdd92008-12-10 05:08:54 +0000321}
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);
zhanyong.wan90c90f92009-06-17 22:11:04 +0000327 EXPECT_EQ(12, a1.Perform(make_tuple(10, 2, 5.6, CharPtr("hi"))));
shiqiane35fdd92008-12-10 05:08:54 +0000328
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);
zhanyong.wan90c90f92009-06-17 22:11:04 +0000339 EXPECT_EQ(12, a1.Perform(make_tuple(CharPtr("hi"), true, 10, 2)));
shiqiane35fdd92008-12-10 05:08:54 +0000340
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) {
348 Action<int(short, char)> a = Invoke(plus<short>()); // NOLINT
349 EXPECT_EQ(3, a.Perform(make_tuple(1, 2)));
350}
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
355 EXPECT_EQ(4321, a.Perform(make_tuple(4000, 300, 20, true)));
356}
357
358// 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
386 EXPECT_EQ(1124, a.Perform(make_tuple(1000, true, 1)));
387}
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",
zhanyong.wan90c90f92009-06-17 22:11:04 +0000418 a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
419 CharPtr("4"), CharPtr("5"), CharPtr("6"),
420 CharPtr("7"))));
shiqiane35fdd92008-12-10 05:08:54 +0000421}
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",
zhanyong.wan90c90f92009-06-17 22:11:04 +0000430 a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
431 CharPtr("4"), CharPtr("5"), CharPtr("6"),
432 CharPtr("7"), CharPtr("8"))));
shiqiane35fdd92008-12-10 05:08:54 +0000433}
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",
zhanyong.wan90c90f92009-06-17 22:11:04 +0000442 a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
443 CharPtr("4"), CharPtr("5"), CharPtr("6"),
444 CharPtr("7"), CharPtr("8"), CharPtr("9"))));
shiqiane35fdd92008-12-10 05:08:54 +0000445}
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);
zhanyong.wan90c90f92009-06-17 22:11:04 +0000453 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"))));
shiqiane35fdd92008-12-10 05:08:54 +0000458}
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);
465 EXPECT_EQ(4444, a.Perform(make_tuple(4000, 300, 20, true)));
466}
467
468// Tests ByRef().
469
470// Tests that ReferenceWrapper<T> is copyable.
471TEST(ByRefTest, IsCopyable) {
472 const string s1 = "Hi";
473 const string s2 = "Hello";
474
475 ::testing::internal::ReferenceWrapper<const string> ref_wrapper = ByRef(s1);
476 const string& r1 = ref_wrapper;
477 EXPECT_EQ(&s1, &r1);
478
479 // Assigns a new value to ref_wrapper.
480 ref_wrapper = ByRef(s2);
481 const string& r2 = ref_wrapper;
482 EXPECT_EQ(&s2, &r2);
483
484 ::testing::internal::ReferenceWrapper<const string> ref_wrapper1 = ByRef(s1);
485 // Copies ref_wrapper1 to ref_wrapper.
486 ref_wrapper = ref_wrapper1;
487 const string& r3 = ref_wrapper;
488 EXPECT_EQ(&s1, &r3);
489}
490
491// Tests using ByRef() on a const value.
492TEST(ByRefTest, ConstValue) {
493 const int n = 0;
494 // int& ref = ByRef(n); // This shouldn't compile - we have a
495 // negative compilation test to catch it.
496 const int& const_ref = ByRef(n);
497 EXPECT_EQ(&n, &const_ref);
498}
499
500// Tests using ByRef() on a non-const value.
501TEST(ByRefTest, NonConstValue) {
502 int n = 0;
503
504 // ByRef(n) can be used as either an int&,
505 int& ref = ByRef(n);
506 EXPECT_EQ(&n, &ref);
507
508 // or a const int&.
509 const int& const_ref = ByRef(n);
510 EXPECT_EQ(&n, &const_ref);
511}
512
513struct Base {
514 bool operator==(const Base&) { return true; }
515};
516
517struct Derived : public Base {
518 bool operator==(const Derived&) { return true; }
519};
520
521// Tests explicitly specifying the type when using ByRef().
522TEST(ByRefTest, ExplicitType) {
523 int n = 0;
524 const int& r1 = ByRef<const int>(n);
525 EXPECT_EQ(&n, &r1);
526
527 // ByRef<char>(n); // This shouldn't compile - we have a negative
528 // compilation test to catch it.
529
530
531 Derived d;
532 Derived& r2 = ByRef<Derived>(d);
533 EXPECT_EQ(&d, &r2);
534
535 const Derived& r3 = ByRef<const Derived>(d);
536 EXPECT_EQ(&d, &r3);
537
538 Base& r4 = ByRef<Base>(d);
539 EXPECT_EQ(&d, &r4);
540
541 const Base& r5 = ByRef<const Base>(d);
542 EXPECT_EQ(&d, &r5);
543
544 // The following shouldn't compile - we have a negative compilation
545 // test for it.
546 //
547 // Base b;
548 // ByRef<Derived>(b);
549}
550
zhanyong.wan387bdd52009-07-20 21:16:35 +0000551// Tests that Google Mock prints expression ByRef(x) as a reference to x.
552TEST(ByRefTest, PrintsCorrectly) {
553 int n = 42;
554 ::std::stringstream expected, actual;
555 testing::internal::UniversalPrinter<const int&>::Print(n, &expected);
556 testing::internal::UniversalPrint(ByRef(n), &actual);
557 EXPECT_EQ(expected.str(), actual.str());
558}
559
shiqiane35fdd92008-12-10 05:08:54 +0000560// Tests InvokeArgument<N>(...).
561
562// Tests using InvokeArgument with a nullary function.
563TEST(InvokeArgumentTest, Function0) {
564 Action<int(int, int(*)())> a = InvokeArgument<1>(); // NOLINT
565 EXPECT_EQ(1, a.Perform(make_tuple(2, &Nullary)));
566}
567
568// Tests using InvokeArgument with a unary function.
569TEST(InvokeArgumentTest, Functor1) {
570 Action<int(UnaryFunctor)> a = InvokeArgument<0>(true); // NOLINT
571 EXPECT_EQ(1, a.Perform(make_tuple(UnaryFunctor())));
572}
573
574// Tests using InvokeArgument with a 5-ary function.
575TEST(InvokeArgumentTest, Function5) {
576 Action<int(int(*)(int, int, int, int, int))> a = // NOLINT
577 InvokeArgument<0>(10000, 2000, 300, 40, 5);
578 EXPECT_EQ(12345, a.Perform(make_tuple(&SumOf5)));
579}
580
581// Tests using InvokeArgument with a 5-ary functor.
582TEST(InvokeArgumentTest, Functor5) {
583 Action<int(SumOf5Functor)> a = // NOLINT
584 InvokeArgument<0>(10000, 2000, 300, 40, 5);
585 EXPECT_EQ(12345, a.Perform(make_tuple(SumOf5Functor())));
586}
587
588// Tests using InvokeArgument with a 6-ary function.
589TEST(InvokeArgumentTest, Function6) {
590 Action<int(int(*)(int, int, int, int, int, int))> a = // NOLINT
591 InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
592 EXPECT_EQ(123456, a.Perform(make_tuple(&SumOf6)));
593}
594
595// Tests using InvokeArgument with a 6-ary functor.
596TEST(InvokeArgumentTest, Functor6) {
597 Action<int(SumOf6Functor)> a = // NOLINT
598 InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
599 EXPECT_EQ(123456, a.Perform(make_tuple(SumOf6Functor())));
600}
601
602// Tests using InvokeArgument with a 7-ary function.
603TEST(InvokeArgumentTest, Function7) {
604 Action<string(string(*)(const char*, const char*, const char*,
605 const char*, const char*, const char*,
606 const char*))> a =
607 InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7");
608 EXPECT_EQ("1234567", a.Perform(make_tuple(&Concat7)));
609}
610
611// Tests using InvokeArgument with a 8-ary function.
612TEST(InvokeArgumentTest, Function8) {
613 Action<string(string(*)(const char*, const char*, const char*,
614 const char*, const char*, const char*,
615 const char*, const char*))> a =
616 InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8");
617 EXPECT_EQ("12345678", a.Perform(make_tuple(&Concat8)));
618}
619
620// Tests using InvokeArgument with a 9-ary function.
621TEST(InvokeArgumentTest, Function9) {
622 Action<string(string(*)(const char*, const char*, const char*,
623 const char*, const char*, const char*,
624 const char*, const char*, const char*))> a =
625 InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9");
626 EXPECT_EQ("123456789", a.Perform(make_tuple(&Concat9)));
627}
628
629// Tests using InvokeArgument with a 10-ary function.
630TEST(InvokeArgumentTest, Function10) {
631 Action<string(string(*)(const char*, const char*, const char*,
632 const char*, const char*, const char*,
633 const char*, const char*, const char*,
634 const char*))> a =
635 InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
636 EXPECT_EQ("1234567890", a.Perform(make_tuple(&Concat10)));
637}
638
639// Tests using InvokeArgument with a function that takes a pointer argument.
640TEST(InvokeArgumentTest, ByPointerFunction) {
641 Action<const char*(const char*(*)(const char* input, short n))> a = // NOLINT
642 InvokeArgument<0>(static_cast<const char*>("Hi"), 1);
643 EXPECT_STREQ("i", a.Perform(make_tuple(&Binary)));
644}
645
646// Tests using InvokeArgument with a function that takes a const char*
647// by passing it a C-string literal.
648TEST(InvokeArgumentTest, FunctionWithCStringLiteral) {
649 Action<const char*(const char*(*)(const char* input, short n))> a = // NOLINT
650 InvokeArgument<0>("Hi", 1);
651 EXPECT_STREQ("i", a.Perform(make_tuple(&Binary)));
652}
653
654// Tests using InvokeArgument with a function that takes a const reference.
655TEST(InvokeArgumentTest, ByConstReferenceFunction) {
656 Action<bool(bool(*function)(const string& s))> a = // NOLINT
657 InvokeArgument<0>(string("Hi"));
658 // When action 'a' is constructed, it makes a copy of the temporary
659 // string object passed to it, so it's OK to use 'a' later, when the
660 // temporary object has already died.
661 EXPECT_TRUE(a.Perform(make_tuple(&ByConstRef)));
662}
663
664// Tests using InvokeArgument with ByRef() and a function that takes a
665// const reference.
666TEST(InvokeArgumentTest, ByExplicitConstReferenceFunction) {
667 Action<bool(bool(*)(const double& x))> a = // NOLINT
668 InvokeArgument<0>(ByRef(g_double));
669 // The above line calls ByRef() on a const value.
670 EXPECT_TRUE(a.Perform(make_tuple(&ReferencesGlobalDouble)));
671
672 double x = 0;
673 a = InvokeArgument<0>(ByRef(x)); // This calls ByRef() on a non-const.
674 EXPECT_FALSE(a.Perform(make_tuple(&ReferencesGlobalDouble)));
675}
676
677// Tests using WithoutArgs with an action that takes no argument.
678TEST(WithoutArgsTest, NoArg) {
679 Action<int(int n)> a = WithoutArgs(Invoke(Nullary)); // NOLINT
680 EXPECT_EQ(1, a.Perform(make_tuple(2)));
681}
682
683// Tests using WithArgs and WithArg with an action that takes 1 argument.
684TEST(WithArgsTest, OneArg) {
685 Action<bool(double x, int n)> a = WithArgs<1>(Invoke(Unary)); // NOLINT
686 EXPECT_TRUE(a.Perform(make_tuple(1.5, -1)));
687 EXPECT_FALSE(a.Perform(make_tuple(1.5, 1)));
688
689 // Also tests the synonym WithArg.
690 Action<bool(double x, int n)> b = WithArg<1>(Invoke(Unary)); // NOLINT
691 EXPECT_TRUE(a.Perform(make_tuple(1.5, -1)));
692 EXPECT_FALSE(a.Perform(make_tuple(1.5, 1)));
693
694}
695
696// Tests using WithArgs with an action that takes 2 arguments.
697TEST(WithArgsTest, TwoArgs) {
698 Action<const char*(const char* s, double x, int n)> a =
699 WithArgs<0, 2>(Invoke(Binary));
700 const char s[] = "Hello";
zhanyong.wan90c90f92009-06-17 22:11:04 +0000701 EXPECT_EQ(s + 2, a.Perform(make_tuple(CharPtr(s), 0.5, 2)));
shiqiane35fdd92008-12-10 05:08:54 +0000702}
703
704// Tests using WithArgs with an action that takes 3 arguments.
705TEST(WithArgsTest, ThreeArgs) {
706 Action<int(int, double, char, short)> a = // NOLINT
707 WithArgs<0, 2, 3>(Invoke(Ternary));
708 EXPECT_EQ(123, a.Perform(make_tuple(100, 6.5, 20, 3)));
709}
710
711// Tests using WithArgs with an action that takes 4 arguments.
712TEST(WithArgsTest, FourArgs) {
713 Action<string(const char*, const char*, double, const char*, const char*)> a =
714 WithArgs<4, 3, 1, 0>(Invoke(Concat4));
zhanyong.wan90c90f92009-06-17 22:11:04 +0000715 EXPECT_EQ("4310", a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), 2.5,
716 CharPtr("3"), CharPtr("4"))));
shiqiane35fdd92008-12-10 05:08:54 +0000717}
718
719// Tests using WithArgs with an action that takes 5 arguments.
720TEST(WithArgsTest, FiveArgs) {
721 Action<string(const char*, const char*, const char*,
722 const char*, const char*)> a =
723 WithArgs<4, 3, 2, 1, 0>(Invoke(Concat5));
zhanyong.wan90c90f92009-06-17 22:11:04 +0000724 EXPECT_EQ("43210",
725 a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
726 CharPtr("3"), CharPtr("4"))));
shiqiane35fdd92008-12-10 05:08:54 +0000727}
728
729// Tests using WithArgs with an action that takes 6 arguments.
730TEST(WithArgsTest, SixArgs) {
731 Action<string(const char*, const char*, const char*)> a =
732 WithArgs<0, 1, 2, 2, 1, 0>(Invoke(Concat6));
zhanyong.wan90c90f92009-06-17 22:11:04 +0000733 EXPECT_EQ("012210",
734 a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"))));
shiqiane35fdd92008-12-10 05:08:54 +0000735}
736
737// Tests using WithArgs with an action that takes 7 arguments.
738TEST(WithArgsTest, SevenArgs) {
739 Action<string(const char*, const char*, const char*, const char*)> a =
740 WithArgs<0, 1, 2, 3, 2, 1, 0>(Invoke(Concat7));
zhanyong.wan90c90f92009-06-17 22:11:04 +0000741 EXPECT_EQ("0123210",
742 a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
743 CharPtr("3"))));
shiqiane35fdd92008-12-10 05:08:54 +0000744}
745
746// Tests using WithArgs with an action that takes 8 arguments.
747TEST(WithArgsTest, EightArgs) {
748 Action<string(const char*, const char*, const char*, const char*)> a =
749 WithArgs<0, 1, 2, 3, 0, 1, 2, 3>(Invoke(Concat8));
zhanyong.wan90c90f92009-06-17 22:11:04 +0000750 EXPECT_EQ("01230123",
751 a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
752 CharPtr("3"))));
shiqiane35fdd92008-12-10 05:08:54 +0000753}
754
755// Tests using WithArgs with an action that takes 9 arguments.
756TEST(WithArgsTest, NineArgs) {
757 Action<string(const char*, const char*, const char*, const char*)> a =
758 WithArgs<0, 1, 2, 3, 1, 2, 3, 2, 3>(Invoke(Concat9));
zhanyong.wan90c90f92009-06-17 22:11:04 +0000759 EXPECT_EQ("012312323",
760 a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
761 CharPtr("3"))));
shiqiane35fdd92008-12-10 05:08:54 +0000762}
763
764// Tests using WithArgs with an action that takes 10 arguments.
765TEST(WithArgsTest, TenArgs) {
766 Action<string(const char*, const char*, const char*, const char*)> a =
767 WithArgs<0, 1, 2, 3, 2, 1, 0, 1, 2, 3>(Invoke(Concat10));
zhanyong.wan90c90f92009-06-17 22:11:04 +0000768 EXPECT_EQ("0123210123",
769 a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
770 CharPtr("3"))));
shiqiane35fdd92008-12-10 05:08:54 +0000771}
772
773// Tests using WithArgs with an action that is not Invoke().
774class SubstractAction : public ActionInterface<int(int, int)> { // NOLINT
775 public:
776 virtual int Perform(const tuple<int, int>& args) {
777 return get<0>(args) - get<1>(args);
778 }
779};
780
781TEST(WithArgsTest, NonInvokeAction) {
782 Action<int(const string&, int, int)> a = // NOLINT
783 WithArgs<2, 1>(MakeAction(new SubstractAction));
zhanyong.wan90c90f92009-06-17 22:11:04 +0000784 EXPECT_EQ(8, a.Perform(make_tuple(CharPtr("hi"), 2, 10)));
shiqiane35fdd92008-12-10 05:08:54 +0000785}
786
787// Tests using WithArgs to pass all original arguments in the original order.
788TEST(WithArgsTest, Identity) {
789 Action<int(int x, char y, short z)> a = // NOLINT
790 WithArgs<0, 1, 2>(Invoke(Ternary));
791 EXPECT_EQ(123, a.Perform(make_tuple(100, 20, 3)));
792}
793
794// Tests using WithArgs with repeated arguments.
795TEST(WithArgsTest, RepeatedArguments) {
796 Action<int(bool, int m, int n)> a = // NOLINT
797 WithArgs<1, 1, 1, 1>(Invoke(SumOf4));
798 EXPECT_EQ(4, a.Perform(make_tuple(false, 1, 10)));
799}
800
801// Tests using WithArgs with reversed argument order.
802TEST(WithArgsTest, ReversedArgumentOrder) {
803 Action<const char*(short n, const char* input)> a = // NOLINT
804 WithArgs<1, 0>(Invoke(Binary));
805 const char s[] = "Hello";
zhanyong.wan90c90f92009-06-17 22:11:04 +0000806 EXPECT_EQ(s + 2, a.Perform(make_tuple(2, CharPtr(s))));
shiqiane35fdd92008-12-10 05:08:54 +0000807}
808
809// Tests using WithArgs with compatible, but not identical, argument types.
810TEST(WithArgsTest, ArgsOfCompatibleTypes) {
811 Action<long(short x, int y, double z, char c)> a = // NOLINT
812 WithArgs<0, 1, 3>(Invoke(Ternary));
813 EXPECT_EQ(123, a.Perform(make_tuple(100, 20, 5.6, 3)));
814}
815
816// Tests using WithArgs with an action that returns void.
817TEST(WithArgsTest, VoidAction) {
818 Action<void(double x, char c, int n)> a = WithArgs<2, 1>(Invoke(VoidBinary));
819 g_done = false;
820 a.Perform(make_tuple(1.5, 'a', 3));
821 EXPECT_TRUE(g_done);
822}
823
824// Tests DoAll(a1, a2).
825TEST(DoAllTest, TwoActions) {
826 int n = 0;
827 Action<int(int*)> a = DoAll(SetArgumentPointee<0>(1), // NOLINT
828 Return(2));
829 EXPECT_EQ(2, a.Perform(make_tuple(&n)));
830 EXPECT_EQ(1, n);
831}
832
833// Tests DoAll(a1, a2, a3).
834TEST(DoAllTest, ThreeActions) {
835 int m = 0, n = 0;
836 Action<int(int*, int*)> a = DoAll(SetArgumentPointee<0>(1), // NOLINT
837 SetArgumentPointee<1>(2),
838 Return(3));
839 EXPECT_EQ(3, a.Perform(make_tuple(&m, &n)));
840 EXPECT_EQ(1, m);
841 EXPECT_EQ(2, n);
842}
843
844// Tests DoAll(a1, a2, a3, a4).
845TEST(DoAllTest, FourActions) {
846 int m = 0, n = 0;
847 char ch = '\0';
848 Action<int(int*, int*, char*)> a = // NOLINT
849 DoAll(SetArgumentPointee<0>(1),
850 SetArgumentPointee<1>(2),
851 SetArgumentPointee<2>('a'),
852 Return(3));
853 EXPECT_EQ(3, a.Perform(make_tuple(&m, &n, &ch)));
854 EXPECT_EQ(1, m);
855 EXPECT_EQ(2, n);
856 EXPECT_EQ('a', ch);
857}
858
859// Tests DoAll(a1, a2, a3, a4, a5).
860TEST(DoAllTest, FiveActions) {
861 int m = 0, n = 0;
862 char a = '\0', b = '\0';
863 Action<int(int*, int*, char*, char*)> action = // NOLINT
864 DoAll(SetArgumentPointee<0>(1),
865 SetArgumentPointee<1>(2),
866 SetArgumentPointee<2>('a'),
867 SetArgumentPointee<3>('b'),
868 Return(3));
869 EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b)));
870 EXPECT_EQ(1, m);
871 EXPECT_EQ(2, n);
872 EXPECT_EQ('a', a);
873 EXPECT_EQ('b', b);
874}
875
876// Tests DoAll(a1, a2, ..., a6).
877TEST(DoAllTest, SixActions) {
878 int m = 0, n = 0;
879 char a = '\0', b = '\0', c = '\0';
880 Action<int(int*, int*, char*, char*, char*)> action = // NOLINT
881 DoAll(SetArgumentPointee<0>(1),
882 SetArgumentPointee<1>(2),
883 SetArgumentPointee<2>('a'),
884 SetArgumentPointee<3>('b'),
885 SetArgumentPointee<4>('c'),
886 Return(3));
887 EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c)));
888 EXPECT_EQ(1, m);
889 EXPECT_EQ(2, n);
890 EXPECT_EQ('a', a);
891 EXPECT_EQ('b', b);
892 EXPECT_EQ('c', c);
893}
894
895// Tests DoAll(a1, a2, ..., a7).
896TEST(DoAllTest, SevenActions) {
897 int m = 0, n = 0;
898 char a = '\0', b = '\0', c = '\0', d = '\0';
899 Action<int(int*, int*, char*, char*, char*, char*)> action = // NOLINT
900 DoAll(SetArgumentPointee<0>(1),
901 SetArgumentPointee<1>(2),
902 SetArgumentPointee<2>('a'),
903 SetArgumentPointee<3>('b'),
904 SetArgumentPointee<4>('c'),
905 SetArgumentPointee<5>('d'),
906 Return(3));
907 EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d)));
908 EXPECT_EQ(1, m);
909 EXPECT_EQ(2, n);
910 EXPECT_EQ('a', a);
911 EXPECT_EQ('b', b);
912 EXPECT_EQ('c', c);
913 EXPECT_EQ('d', d);
914}
915
916// Tests DoAll(a1, a2, ..., a8).
917TEST(DoAllTest, EightActions) {
918 int m = 0, n = 0;
919 char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0';
920 Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
921 char*)> action =
922 DoAll(SetArgumentPointee<0>(1),
923 SetArgumentPointee<1>(2),
924 SetArgumentPointee<2>('a'),
925 SetArgumentPointee<3>('b'),
926 SetArgumentPointee<4>('c'),
927 SetArgumentPointee<5>('d'),
928 SetArgumentPointee<6>('e'),
929 Return(3));
930 EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d, &e)));
931 EXPECT_EQ(1, m);
932 EXPECT_EQ(2, n);
933 EXPECT_EQ('a', a);
934 EXPECT_EQ('b', b);
935 EXPECT_EQ('c', c);
936 EXPECT_EQ('d', d);
937 EXPECT_EQ('e', e);
938}
939
940// Tests DoAll(a1, a2, ..., a9).
941TEST(DoAllTest, NineActions) {
942 int m = 0, n = 0;
943 char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0', f = '\0';
944 Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
945 char*, char*)> action =
946 DoAll(SetArgumentPointee<0>(1),
947 SetArgumentPointee<1>(2),
948 SetArgumentPointee<2>('a'),
949 SetArgumentPointee<3>('b'),
950 SetArgumentPointee<4>('c'),
951 SetArgumentPointee<5>('d'),
952 SetArgumentPointee<6>('e'),
953 SetArgumentPointee<7>('f'),
954 Return(3));
955 EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d, &e, &f)));
956 EXPECT_EQ(1, m);
957 EXPECT_EQ(2, n);
958 EXPECT_EQ('a', a);
959 EXPECT_EQ('b', b);
960 EXPECT_EQ('c', c);
961 EXPECT_EQ('d', d);
962 EXPECT_EQ('e', e);
963 EXPECT_EQ('f', f);
964}
965
966// Tests DoAll(a1, a2, ..., a10).
967TEST(DoAllTest, TenActions) {
968 int m = 0, n = 0;
969 char a = '\0', b = '\0', c = '\0', d = '\0';
970 char e = '\0', f = '\0', g = '\0';
971 Action<int(int*, int*, char*, char*, char*, char*, // NOLINT
972 char*, char*, char*)> action =
973 DoAll(SetArgumentPointee<0>(1),
974 SetArgumentPointee<1>(2),
975 SetArgumentPointee<2>('a'),
976 SetArgumentPointee<3>('b'),
977 SetArgumentPointee<4>('c'),
978 SetArgumentPointee<5>('d'),
979 SetArgumentPointee<6>('e'),
980 SetArgumentPointee<7>('f'),
981 SetArgumentPointee<8>('g'),
982 Return(3));
983 EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d, &e, &f, &g)));
984 EXPECT_EQ(1, m);
985 EXPECT_EQ(2, n);
986 EXPECT_EQ('a', a);
987 EXPECT_EQ('b', b);
988 EXPECT_EQ('c', c);
989 EXPECT_EQ('d', d);
990 EXPECT_EQ('e', e);
991 EXPECT_EQ('f', f);
992 EXPECT_EQ('g', g);
993}
994
shiqian326aa562009-01-09 21:43:57 +0000995// Tests the ACTION*() macro family.
996
997// Tests that ACTION() can define an action that doesn't reference the
998// mock function arguments.
999ACTION(Return5) { return 5; }
1000
1001TEST(ActionMacroTest, WorksWhenNotReferencingArguments) {
1002 Action<double()> a1 = Return5();
1003 EXPECT_DOUBLE_EQ(5, a1.Perform(make_tuple()));
1004
1005 Action<int(double, bool)> a2 = Return5();
1006 EXPECT_EQ(5, a2.Perform(make_tuple(1, true)));
1007}
1008
1009// Tests that ACTION() can define an action that returns void.
1010ACTION(IncrementArg1) { (*arg1)++; }
1011
1012TEST(ActionMacroTest, WorksWhenReturningVoid) {
1013 Action<void(int, int*)> a1 = IncrementArg1();
1014 int n = 0;
1015 a1.Perform(make_tuple(5, &n));
1016 EXPECT_EQ(1, n);
1017}
1018
1019// Tests that the body of ACTION() can reference the type of the
1020// argument.
1021ACTION(IncrementArg2) {
1022 StaticAssertTypeEq<int*, arg2_type>();
1023 arg2_type temp = arg2;
1024 (*temp)++;
1025}
1026
1027TEST(ActionMacroTest, CanReferenceArgumentType) {
1028 Action<void(int, bool, int*)> a1 = IncrementArg2();
1029 int n = 0;
1030 a1.Perform(make_tuple(5, false, &n));
1031 EXPECT_EQ(1, n);
1032}
1033
1034// Tests that the body of ACTION() can reference the argument tuple
1035// via args_type and args.
1036ACTION(Sum2) {
1037 StaticAssertTypeEq< ::std::tr1::tuple<int, char, int*>, args_type>();
1038 args_type args_copy = args;
1039 return get<0>(args_copy) + get<1>(args_copy);
1040}
1041
1042TEST(ActionMacroTest, CanReferenceArgumentTuple) {
1043 Action<int(int, char, int*)> a1 = Sum2();
1044 int dummy = 0;
1045 EXPECT_EQ(11, a1.Perform(make_tuple(5, static_cast<char>(6), &dummy)));
1046}
1047
1048// Tests that the body of ACTION() can reference the mock function
1049// type.
1050int Dummy(bool flag) { return flag? 1 : 0; }
1051
1052ACTION(InvokeDummy) {
1053 StaticAssertTypeEq<int(bool), function_type>();
1054 function_type* fp = &Dummy;
1055 return (*fp)(true);
1056}
1057
1058TEST(ActionMacroTest, CanReferenceMockFunctionType) {
1059 Action<int(bool)> a1 = InvokeDummy();
1060 EXPECT_EQ(1, a1.Perform(make_tuple(true)));
1061 EXPECT_EQ(1, a1.Perform(make_tuple(false)));
1062}
1063
1064// Tests that the body of ACTION() can reference the mock function's
1065// return type.
1066ACTION(InvokeDummy2) {
1067 StaticAssertTypeEq<int, return_type>();
1068 return_type result = Dummy(true);
1069 return result;
1070}
1071
1072TEST(ActionMacroTest, CanReferenceMockFunctionReturnType) {
1073 Action<int(bool)> a1 = InvokeDummy2();
1074 EXPECT_EQ(1, a1.Perform(make_tuple(true)));
1075 EXPECT_EQ(1, a1.Perform(make_tuple(false)));
1076}
1077
zhanyong.wan7f4c2c02009-02-19 22:38:27 +00001078// Tests that ACTION() works for arguments passed by const reference.
1079ACTION(ReturnAddrOfConstBoolReferenceArg) {
1080 StaticAssertTypeEq<const bool&, arg1_type>();
1081 return &arg1;
1082}
1083
1084TEST(ActionMacroTest, WorksForConstReferenceArg) {
1085 Action<const bool*(int, const bool&)> a = ReturnAddrOfConstBoolReferenceArg();
1086 const bool b = false;
1087 EXPECT_EQ(&b, a.Perform(tuple<int, const bool&>(0, b)));
1088}
1089
1090// Tests that ACTION() works for arguments passed by non-const reference.
1091ACTION(ReturnAddrOfIntReferenceArg) {
1092 StaticAssertTypeEq<int&, arg0_type>();
1093 return &arg0;
1094}
1095
1096TEST(ActionMacroTest, WorksForNonConstReferenceArg) {
1097 Action<int*(int&, bool, int)> a = ReturnAddrOfIntReferenceArg();
1098 int n = 0;
1099 EXPECT_EQ(&n, a.Perform(tuple<int&, bool, int>(n, true, 1)));
1100}
1101
shiqian326aa562009-01-09 21:43:57 +00001102// Tests that ACTION() can be used in a namespace.
1103namespace action_test {
1104ACTION(Sum) { return arg0 + arg1; }
1105} // namespace action_test
1106
1107TEST(ActionMacroTest, WorksInNamespace) {
1108 Action<int(int, int)> a1 = action_test::Sum();
1109 EXPECT_EQ(3, a1.Perform(make_tuple(1, 2)));
1110}
1111
1112// Tests that the same ACTION definition works for mock functions with
1113// different argument numbers.
1114ACTION(PlusTwo) { return arg0 + 2; }
1115
1116TEST(ActionMacroTest, WorksForDifferentArgumentNumbers) {
1117 Action<int(int)> a1 = PlusTwo();
1118 EXPECT_EQ(4, a1.Perform(make_tuple(2)));
1119
1120 Action<double(float, void*)> a2 = PlusTwo();
1121 int dummy;
1122 EXPECT_DOUBLE_EQ(6, a2.Perform(make_tuple(4.0f, &dummy)));
1123}
1124
1125// Tests that ACTION_P can define a parameterized action.
1126ACTION_P(Plus, n) { return arg0 + n; }
1127
1128TEST(ActionPMacroTest, DefinesParameterizedAction) {
1129 Action<int(int m, bool t)> a1 = Plus(9);
1130 EXPECT_EQ(10, a1.Perform(make_tuple(1, true)));
1131}
1132
1133// Tests that the body of ACTION_P can reference the argument types
1134// and the parameter type.
1135ACTION_P(TypedPlus, n) {
1136 arg0_type t1 = arg0;
1137 n_type t2 = n;
1138 return t1 + t2;
1139}
1140
1141TEST(ActionPMacroTest, CanReferenceArgumentAndParameterTypes) {
1142 Action<int(char m, bool t)> a1 = TypedPlus(9);
1143 EXPECT_EQ(10, a1.Perform(make_tuple(static_cast<char>(1), true)));
1144}
1145
1146// Tests that a parameterized action can be used in any mock function
1147// whose type is compatible.
1148TEST(ActionPMacroTest, WorksInCompatibleMockFunction) {
1149 Action<std::string(const std::string& s)> a1 = Plus("tail");
1150 const std::string re = "re";
1151 EXPECT_EQ("retail", a1.Perform(make_tuple(re)));
1152}
1153
1154// Tests that we can use ACTION*() to define actions overloaded on the
1155// number of parameters.
1156
1157ACTION(OverloadedAction) { return arg0 ? arg1 : "hello"; }
1158
1159ACTION_P(OverloadedAction, default_value) {
1160 return arg0 ? arg1 : default_value;
1161}
1162
1163ACTION_P2(OverloadedAction, true_value, false_value) {
1164 return arg0 ? true_value : false_value;
1165}
1166
1167TEST(ActionMacroTest, CanDefineOverloadedActions) {
1168 typedef Action<const char*(bool, const char*)> MyAction;
1169
1170 const MyAction a1 = OverloadedAction();
zhanyong.wan90c90f92009-06-17 22:11:04 +00001171 EXPECT_STREQ("hello", a1.Perform(make_tuple(false, CharPtr("world"))));
1172 EXPECT_STREQ("world", a1.Perform(make_tuple(true, CharPtr("world"))));
shiqian326aa562009-01-09 21:43:57 +00001173
1174 const MyAction a2 = OverloadedAction("hi");
zhanyong.wan90c90f92009-06-17 22:11:04 +00001175 EXPECT_STREQ("hi", a2.Perform(make_tuple(false, CharPtr("world"))));
1176 EXPECT_STREQ("world", a2.Perform(make_tuple(true, CharPtr("world"))));
shiqian326aa562009-01-09 21:43:57 +00001177
1178 const MyAction a3 = OverloadedAction("hi", "you");
zhanyong.wan90c90f92009-06-17 22:11:04 +00001179 EXPECT_STREQ("hi", a3.Perform(make_tuple(true, CharPtr("world"))));
1180 EXPECT_STREQ("you", a3.Perform(make_tuple(false, CharPtr("world"))));
shiqian326aa562009-01-09 21:43:57 +00001181}
1182
1183// Tests ACTION_Pn where n >= 3.
1184
1185ACTION_P3(Plus, m, n, k) { return arg0 + m + n + k; }
1186
1187TEST(ActionPnMacroTest, WorksFor3Parameters) {
1188 Action<double(int m, bool t)> a1 = Plus(100, 20, 3.4);
1189 EXPECT_DOUBLE_EQ(3123.4, a1.Perform(make_tuple(3000, true)));
1190
1191 Action<std::string(const std::string& s)> a2 = Plus("tail", "-", ">");
1192 const std::string re = "re";
1193 EXPECT_EQ("retail->", a2.Perform(make_tuple(re)));
1194}
1195
1196ACTION_P4(Plus, p0, p1, p2, p3) { return arg0 + p0 + p1 + p2 + p3; }
1197
1198TEST(ActionPnMacroTest, WorksFor4Parameters) {
1199 Action<int(int)> a1 = Plus(1, 2, 3, 4);
1200 EXPECT_EQ(10 + 1 + 2 + 3 + 4, a1.Perform(make_tuple(10)));
1201}
1202
1203ACTION_P5(Plus, p0, p1, p2, p3, p4) { return arg0 + p0 + p1 + p2 + p3 + p4; }
1204
1205TEST(ActionPnMacroTest, WorksFor5Parameters) {
1206 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5);
1207 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5, a1.Perform(make_tuple(10)));
1208}
1209
1210ACTION_P6(Plus, p0, p1, p2, p3, p4, p5) {
1211 return arg0 + p0 + p1 + p2 + p3 + p4 + p5;
1212}
1213
1214TEST(ActionPnMacroTest, WorksFor6Parameters) {
1215 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6);
1216 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6, a1.Perform(make_tuple(10)));
1217}
1218
1219ACTION_P7(Plus, p0, p1, p2, p3, p4, p5, p6) {
1220 return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6;
1221}
1222
1223TEST(ActionPnMacroTest, WorksFor7Parameters) {
1224 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7);
1225 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7, a1.Perform(make_tuple(10)));
1226}
1227
1228ACTION_P8(Plus, p0, p1, p2, p3, p4, p5, p6, p7) {
1229 return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7;
1230}
1231
1232TEST(ActionPnMacroTest, WorksFor8Parameters) {
1233 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8);
1234 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8, a1.Perform(make_tuple(10)));
1235}
1236
1237ACTION_P9(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8) {
1238 return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8;
1239}
1240
1241TEST(ActionPnMacroTest, WorksFor9Parameters) {
1242 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9);
1243 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9, a1.Perform(make_tuple(10)));
1244}
1245
1246ACTION_P10(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8, last_param) {
1247 arg0_type t0 = arg0;
1248 last_param_type t9 = last_param;
1249 return t0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + t9;
1250}
1251
1252TEST(ActionPnMacroTest, WorksFor10Parameters) {
1253 Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
1254 EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10,
1255 a1.Perform(make_tuple(10)));
1256}
1257
1258// Tests that the action body can promote the parameter types.
1259
1260ACTION_P2(PadArgument, prefix, suffix) {
1261 // The following lines promote the two parameters to desired types.
1262 std::string prefix_str(prefix);
1263 char suffix_char(suffix);
1264 return prefix_str + arg0 + suffix_char;
1265}
1266
1267TEST(ActionPnMacroTest, SimpleTypePromotion) {
1268 Action<std::string(const char*)> no_promo =
1269 PadArgument(std::string("foo"), 'r');
1270 Action<std::string(const char*)> promo =
1271 PadArgument("foo", static_cast<int>('r'));
zhanyong.wan90c90f92009-06-17 22:11:04 +00001272 EXPECT_EQ("foobar", no_promo.Perform(make_tuple(CharPtr("ba"))));
1273 EXPECT_EQ("foobar", promo.Perform(make_tuple(CharPtr("ba"))));
shiqian326aa562009-01-09 21:43:57 +00001274}
1275
1276// Tests that we can partially restrict parameter types using a
1277// straight-forward pattern.
1278
1279// Defines a generic action that doesn't restrict the types of its
1280// parameters.
1281ACTION_P3(ConcatImpl, a, b, c) {
1282 std::stringstream ss;
1283 ss << a << b << c;
1284 return ss.str();
1285}
1286
1287// Next, we try to restrict that either the first parameter is a
1288// string, or the second parameter is an int.
1289
1290// Defines a partially specialized wrapper that restricts the first
1291// parameter to std::string.
1292template <typename T1, typename T2>
1293// ConcatImplActionP3 is the class template ACTION_P3 uses to
1294// implement ConcatImpl. We shouldn't change the name as this
1295// pattern requires the user to use it directly.
1296ConcatImplActionP3<std::string, T1, T2>
1297Concat(const std::string& a, T1 b, T2 c) {
1298 if (true) {
1299 // This branch verifies that ConcatImpl() can be invoked without
1300 // explicit template arguments.
1301 return ConcatImpl(a, b, c);
1302 } else {
1303 // This branch verifies that ConcatImpl() can also be invoked with
1304 // explicit template arguments. It doesn't really need to be
1305 // executed as this is a compile-time verification.
1306 return ConcatImpl<std::string, T1, T2>(a, b, c);
1307 }
1308}
1309
1310// Defines another partially specialized wrapper that restricts the
1311// second parameter to int.
1312template <typename T1, typename T2>
1313ConcatImplActionP3<T1, int, T2>
1314Concat(T1 a, int b, T2 c) {
1315 return ConcatImpl(a, b, c);
1316}
1317
1318TEST(ActionPnMacroTest, CanPartiallyRestrictParameterTypes) {
1319 Action<const std::string()> a1 = Concat("Hello", "1", 2);
1320 EXPECT_EQ("Hello12", a1.Perform(make_tuple()));
1321
1322 a1 = Concat(1, 2, 3);
1323 EXPECT_EQ("123", a1.Perform(make_tuple()));
1324}
1325
1326// Verifies the type of an ACTION*.
1327
1328ACTION(DoFoo) {}
1329ACTION_P(DoFoo, p) {}
1330ACTION_P2(DoFoo, p0, p1) {}
1331
1332TEST(ActionPnMacroTest, TypesAreCorrect) {
1333 // DoFoo() must be assignable to a DoFooAction variable.
1334 DoFooAction a0 = DoFoo();
1335
1336 // DoFoo(1) must be assignable to a DoFooActionP variable.
1337 DoFooActionP<int> a1 = DoFoo(1);
1338
1339 // DoFoo(p1, ..., pk) must be assignable to a DoFooActionPk
1340 // variable, and so on.
1341 DoFooActionP2<int, char> a2 = DoFoo(1, '2');
1342 PlusActionP3<int, int, char> a3 = Plus(1, 2, '3');
1343 PlusActionP4<int, int, int, char> a4 = Plus(1, 2, 3, '4');
1344 PlusActionP5<int, int, int, int, char> a5 = Plus(1, 2, 3, 4, '5');
1345 PlusActionP6<int, int, int, int, int, char> a6 = Plus(1, 2, 3, 4, 5, '6');
1346 PlusActionP7<int, int, int, int, int, int, char> a7 =
1347 Plus(1, 2, 3, 4, 5, 6, '7');
1348 PlusActionP8<int, int, int, int, int, int, int, char> a8 =
1349 Plus(1, 2, 3, 4, 5, 6, 7, '8');
1350 PlusActionP9<int, int, int, int, int, int, int, int, char> a9 =
1351 Plus(1, 2, 3, 4, 5, 6, 7, 8, '9');
1352 PlusActionP10<int, int, int, int, int, int, int, int, int, char> a10 =
1353 Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
1354}
1355
zhanyong.wanc069d7f2009-02-02 20:51:53 +00001356// Tests that an ACTION_P*() action can be explicitly instantiated
1357// with reference-typed parameters.
1358
1359ACTION_P(Plus1, x) { return x; }
1360ACTION_P2(Plus2, x, y) { return x + y; }
1361ACTION_P3(Plus3, x, y, z) { return x + y + z; }
1362ACTION_P10(Plus10, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
1363 return a0 + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9;
1364}
1365
1366TEST(ActionPnMacroTest, CanExplicitlyInstantiateWithReferenceTypes) {
1367 int x = 1, y = 2, z = 3;
1368 const tuple<> empty = make_tuple();
1369
1370 Action<int()> a = Plus1<int&>(x);
1371 EXPECT_EQ(1, a.Perform(empty));
1372
1373 a = Plus2<const int&, int&>(x, y);
1374 EXPECT_EQ(3, a.Perform(empty));
1375
1376 a = Plus3<int&, const int&, int&>(x, y, z);
1377 EXPECT_EQ(6, a.Perform(empty));
1378
1379 int n[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
1380 a = Plus10<const int&, int&, const int&, int&, const int&, int&, const int&,
1381 int&, const int&, int&>(n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7],
1382 n[8], n[9]);
1383 EXPECT_EQ(55, a.Perform(empty));
1384}
1385
zhanyong.wan1afe1c72009-07-21 23:26:31 +00001386TEST(ReturnArgActionTest, WorksForOneArgIntArg0) {
1387 const Action<int(int)> a = ReturnArg<0>();
1388 EXPECT_EQ(5, a.Perform(make_tuple(5)));
1389}
1390
1391TEST(ReturnArgActionTest, WorksForMultiArgBoolArg0) {
1392 const Action<bool(bool, bool, bool)> a = ReturnArg<0>();
1393 EXPECT_TRUE(a.Perform(make_tuple(true, false, false)));
1394}
1395
1396TEST(ReturnArgActionTest, WorksForMultiArgStringArg2) {
1397 const Action<string(int, int, string, int)> a = ReturnArg<2>();
1398 EXPECT_EQ("seven", a.Perform(make_tuple(5, 6, string("seven"), 8)));
1399}
1400
zhanyong.wan7f4c2c02009-02-19 22:38:27 +00001401TEST(SaveArgActionTest, WorksForSameType) {
1402 int result = 0;
1403 const Action<void(int n)> a1 = SaveArg<0>(&result);
1404 a1.Perform(make_tuple(5));
1405 EXPECT_EQ(5, result);
1406}
1407
1408TEST(SaveArgActionTest, WorksForCompatibleType) {
1409 int result = 0;
1410 const Action<void(bool, char)> a1 = SaveArg<1>(&result);
1411 a1.Perform(make_tuple(true, 'a'));
1412 EXPECT_EQ('a', result);
1413}
1414
1415TEST(SetArgRefereeActionTest, WorksForSameType) {
1416 int value = 0;
1417 const Action<void(int&)> a1 = SetArgReferee<0>(1);
1418 a1.Perform(tuple<int&>(value));
1419 EXPECT_EQ(1, value);
1420}
1421
1422TEST(SetArgRefereeActionTest, WorksForCompatibleType) {
1423 int value = 0;
1424 const Action<void(int, int&)> a1 = SetArgReferee<1>('a');
1425 a1.Perform(tuple<int, int&>(0, value));
1426 EXPECT_EQ('a', value);
1427}
1428
1429TEST(SetArgRefereeActionTest, WorksWithExtraArguments) {
1430 int value = 0;
1431 const Action<void(bool, int, int&, const char*)> a1 = SetArgReferee<2>('a');
1432 a1.Perform(tuple<bool, int, int&, const char*>(true, 0, value, "hi"));
1433 EXPECT_EQ('a', value);
1434}
1435
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +00001436class NullaryConstructorClass {
1437 public:
1438 NullaryConstructorClass() : value_(123) {}
1439 int value_;
1440};
1441
1442// Tests using ReturnNew() with a nullary constructor.
1443TEST(ReturnNewTest, NoArgs) {
1444 Action<NullaryConstructorClass*()> a = ReturnNew<NullaryConstructorClass>();
1445 NullaryConstructorClass* c = a.Perform(make_tuple());
1446 EXPECT_EQ(123, c->value_);
1447 delete c;
1448}
1449
1450class UnaryConstructorClass {
1451 public:
1452 explicit UnaryConstructorClass(int value) : value_(value) {}
1453 int value_;
1454};
1455
1456// Tests using ReturnNew() with a unary constructor.
1457TEST(ReturnNewTest, Unary) {
1458 Action<UnaryConstructorClass*()> a = ReturnNew<UnaryConstructorClass>(4000);
1459 UnaryConstructorClass* c = a.Perform(make_tuple());
1460 EXPECT_EQ(4000, c->value_);
1461 delete c;
1462}
1463
1464TEST(ReturnNewTest, UnaryWorksWhenMockMethodHasArgs) {
1465 Action<UnaryConstructorClass*(bool, int)> a =
1466 ReturnNew<UnaryConstructorClass>(4000);
1467 UnaryConstructorClass* c = a.Perform(make_tuple(false, 5));
1468 EXPECT_EQ(4000, c->value_);
1469 delete c;
1470}
1471
1472TEST(ReturnNewTest, UnaryWorksWhenMockMethodReturnsPointerToConst) {
1473 Action<const UnaryConstructorClass*()> a =
1474 ReturnNew<UnaryConstructorClass>(4000);
1475 const UnaryConstructorClass* c = a.Perform(make_tuple());
1476 EXPECT_EQ(4000, c->value_);
1477 delete c;
1478}
1479
1480class TenArgConstructorClass {
1481 public:
1482 TenArgConstructorClass(int a1, int a2, int a3, int a4, int a5,
1483 int a6, int a7, int a8, int a9, int a10)
1484 : value_(a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10) {
1485 }
1486 int value_;
1487};
1488
1489// Tests using ReturnNew() with a 10-argument constructor.
1490TEST(ReturnNewTest, ConstructorThatTakes10Arguments) {
1491 Action<TenArgConstructorClass*()> a =
1492 ReturnNew<TenArgConstructorClass>(1000000000, 200000000, 30000000,
1493 4000000, 500000, 60000,
1494 7000, 800, 90, 0);
1495 TenArgConstructorClass* c = a.Perform(make_tuple());
1496 EXPECT_EQ(1234567890, c->value_);
1497 delete c;
1498}
1499
1500// A class that can be used to verify that its destructor is called: it will set
1501// the bool provided to the constructor to true when destroyed.
1502class DeletionTester {
1503 public:
1504 explicit DeletionTester(bool* is_deleted)
1505 : is_deleted_(is_deleted) {
1506 // Make sure the bit is set to false.
1507 *is_deleted_ = false;
1508 }
1509
1510 ~DeletionTester() {
1511 *is_deleted_ = true;
1512 }
1513
1514 private:
1515 bool* is_deleted_;
1516};
1517
1518TEST(DeleteArgActionTest, OneArg) {
1519 bool is_deleted = false;
1520 DeletionTester* t = new DeletionTester(&is_deleted);
1521 const Action<void(DeletionTester*)> a1 = DeleteArg<0>(); // NOLINT
1522 EXPECT_FALSE(is_deleted);
1523 a1.Perform(make_tuple(t));
1524 EXPECT_TRUE(is_deleted);
1525}
1526
1527TEST(DeleteArgActionTest, TenArgs) {
1528 bool is_deleted = false;
1529 DeletionTester* t = new DeletionTester(&is_deleted);
1530 const Action<void(bool, int, int, const char*, bool,
1531 int, int, int, int, DeletionTester*)> a1 = DeleteArg<9>();
1532 EXPECT_FALSE(is_deleted);
zhanyong.wan90c90f92009-06-17 22:11:04 +00001533 a1.Perform(make_tuple(true, 5, 6, CharPtr("hi"), false, 7, 8, 9, 10, t));
zhanyong.wan1c8eb1c2009-04-09 07:29:58 +00001534 EXPECT_TRUE(is_deleted);
1535}
1536
zhanyong.wane1cdce52009-02-06 01:09:43 +00001537#if GTEST_HAS_EXCEPTIONS
1538
1539TEST(ThrowActionTest, ThrowsGivenExceptionInVoidFunction) {
1540 const Action<void(int n)> a = Throw('a');
1541 EXPECT_THROW(a.Perform(make_tuple(0)), char);
1542}
1543
1544class MyException {};
1545
1546TEST(ThrowActionTest, ThrowsGivenExceptionInNonVoidFunction) {
1547 const Action<double(char ch)> a = Throw(MyException());
1548 EXPECT_THROW(a.Perform(make_tuple('0')), MyException);
1549}
1550
1551TEST(ThrowActionTest, ThrowsGivenExceptionInNullaryFunction) {
1552 const Action<double()> a = Throw(MyException());
1553 EXPECT_THROW(a.Perform(make_tuple()), MyException);
1554}
1555
1556#endif // GTEST_HAS_EXCEPTIONS
1557
zhanyong.wan18490652009-05-11 18:54:08 +00001558// Tests that ACTION_TEMPLATE works when there is no value parameter.
1559ACTION_TEMPLATE(CreateNew,
1560 HAS_1_TEMPLATE_PARAMS(typename, T),
1561 AND_0_VALUE_PARAMS()) {
1562 return new T;
1563}
1564
1565TEST(ActionTemplateTest, WorksWithoutValueParam) {
1566 const Action<int*()> a = CreateNew<int>();
1567 int* p = a.Perform(make_tuple());
1568 delete p;
1569}
1570
1571// Tests that ACTION_TEMPLATE works when there are value parameters.
1572ACTION_TEMPLATE(CreateNew,
1573 HAS_1_TEMPLATE_PARAMS(typename, T),
1574 AND_1_VALUE_PARAMS(a0)) {
1575 return new T(a0);
1576}
1577
1578TEST(ActionTemplateTest, WorksWithValueParams) {
1579 const Action<int*()> a = CreateNew<int>(42);
1580 int* p = a.Perform(make_tuple());
1581 EXPECT_EQ(42, *p);
1582 delete p;
1583}
1584
1585// Tests that ACTION_TEMPLATE works for integral template parameters.
1586ACTION_TEMPLATE(MyDeleteArg,
1587 HAS_1_TEMPLATE_PARAMS(int, k),
1588 AND_0_VALUE_PARAMS()) {
1589 delete std::tr1::get<k>(args);
1590}
1591
1592// Resets a bool variable in the destructor.
1593class BoolResetter {
1594 public:
1595 explicit BoolResetter(bool* value) : value_(value) {}
1596 ~BoolResetter() { *value_ = false; }
1597 private:
1598 bool* const value_;
1599};
1600
1601TEST(ActionTemplateTest, WorksForIntegralTemplateParams) {
1602 const Action<void(int*, BoolResetter*)> a = MyDeleteArg<1>();
1603 int n = 0;
1604 bool b = true;
1605 BoolResetter* resetter = new BoolResetter(&b);
1606 a.Perform(make_tuple(&n, resetter));
1607 EXPECT_FALSE(b); // Verifies that resetter is deleted.
1608}
1609
1610// Tests that ACTION_TEMPLATES works for template template parameters.
1611ACTION_TEMPLATE(ReturnSmartPointer,
1612 HAS_1_TEMPLATE_PARAMS(template <typename Pointee> class,
1613 Pointer),
1614 AND_1_VALUE_PARAMS(pointee)) {
1615 return Pointer<pointee_type>(new pointee_type(pointee));
1616}
1617
1618TEST(ActionTemplateTest, WorksForTemplateTemplateParameters) {
1619 using ::testing::internal::linked_ptr;
1620 const Action<linked_ptr<int>()> a = ReturnSmartPointer<linked_ptr>(42);
1621 linked_ptr<int> p = a.Perform(make_tuple());
1622 EXPECT_EQ(42, *p);
1623}
1624
1625// Tests that ACTION_TEMPLATE works for 10 template parameters.
1626template <typename T1, typename T2, typename T3, int k4, bool k5,
1627 unsigned int k6, typename T7, typename T8, typename T9>
1628struct GiantTemplate {
1629 public:
1630 explicit GiantTemplate(int a_value) : value(a_value) {}
1631 int value;
1632};
1633
1634ACTION_TEMPLATE(ReturnGiant,
1635 HAS_10_TEMPLATE_PARAMS(
1636 typename, T1,
1637 typename, T2,
1638 typename, T3,
1639 int, k4,
1640 bool, k5,
1641 unsigned int, k6,
1642 class, T7,
1643 class, T8,
1644 class, T9,
1645 template <typename T> class, T10),
1646 AND_1_VALUE_PARAMS(value)) {
1647 return GiantTemplate<T10<T1>, T2, T3, k4, k5, k6, T7, T8, T9>(value);
1648}
1649
1650TEST(ActionTemplateTest, WorksFor10TemplateParameters) {
1651 using ::testing::internal::linked_ptr;
1652 typedef GiantTemplate<linked_ptr<int>, bool, double, 5,
1653 true, 6, char, unsigned, int> Giant;
1654 const Action<Giant()> a = ReturnGiant<
1655 int, bool, double, 5, true, 6, char, unsigned, int, linked_ptr>(42);
1656 Giant giant = a.Perform(make_tuple());
1657 EXPECT_EQ(42, giant.value);
1658}
1659
1660// Tests that ACTION_TEMPLATE works for 10 value parameters.
1661ACTION_TEMPLATE(ReturnSum,
1662 HAS_1_TEMPLATE_PARAMS(typename, Number),
1663 AND_10_VALUE_PARAMS(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10)) {
1664 return static_cast<Number>(v1) + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10;
1665}
1666
1667TEST(ActionTemplateTest, WorksFor10ValueParameters) {
1668 const Action<int()> a = ReturnSum<int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
1669 EXPECT_EQ(55, a.Perform(make_tuple()));
1670}
1671
1672// Tests that ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded
1673// on the number of value parameters.
1674
1675ACTION(ReturnSum) { return 0; }
1676
1677ACTION_P(ReturnSum, x) { return x; }
1678
1679ACTION_TEMPLATE(ReturnSum,
1680 HAS_1_TEMPLATE_PARAMS(typename, Number),
1681 AND_2_VALUE_PARAMS(v1, v2)) {
1682 return static_cast<Number>(v1) + v2;
1683}
1684
1685ACTION_TEMPLATE(ReturnSum,
1686 HAS_1_TEMPLATE_PARAMS(typename, Number),
1687 AND_3_VALUE_PARAMS(v1, v2, v3)) {
1688 return static_cast<Number>(v1) + v2 + v3;
1689}
1690
1691ACTION_TEMPLATE(ReturnSum,
1692 HAS_2_TEMPLATE_PARAMS(typename, Number, int, k),
1693 AND_4_VALUE_PARAMS(v1, v2, v3, v4)) {
1694 return static_cast<Number>(v1) + v2 + v3 + v4 + k;
1695}
1696
1697TEST(ActionTemplateTest, CanBeOverloadedOnNumberOfValueParameters) {
1698 const Action<int()> a0 = ReturnSum();
1699 const Action<int()> a1 = ReturnSum(1);
1700 const Action<int()> a2 = ReturnSum<int>(1, 2);
1701 const Action<int()> a3 = ReturnSum<int>(1, 2, 3);
1702 const Action<int()> a4 = ReturnSum<int, 10000>(2000, 300, 40, 5);
1703 EXPECT_EQ(0, a0.Perform(make_tuple()));
1704 EXPECT_EQ(1, a1.Perform(make_tuple()));
1705 EXPECT_EQ(3, a2.Perform(make_tuple()));
1706 EXPECT_EQ(6, a3.Perform(make_tuple()));
1707 EXPECT_EQ(12345, a4.Perform(make_tuple()));
1708}
1709
shiqiane35fdd92008-12-10 05:08:54 +00001710} // namespace gmock_generated_actions_test
1711} // namespace testing