blob: 24462609aefaa6b200f2f0619bb8f18c042a847b [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.
35
36#include <gmock/gmock-actions.h>
37#include <algorithm>
38#include <iterator>
39#include <string>
40#include <gmock/gmock.h>
41#include <gmock/internal/gmock-port.h>
42#include <gtest/gtest.h>
43#include <gtest/gtest-spi.h>
44
45namespace {
46
47using ::std::tr1::get;
48using ::std::tr1::make_tuple;
49using ::std::tr1::tuple;
50using ::std::tr1::tuple_element;
51using testing::internal::BuiltInDefaultValue;
52using testing::internal::Int64;
53using testing::internal::UInt64;
54// This list should be kept sorted.
55using testing::_;
56using testing::Action;
57using testing::ActionInterface;
58using testing::Assign;
59using testing::DefaultValue;
60using testing::DoDefault;
61using testing::IgnoreResult;
62using testing::Invoke;
63using testing::InvokeWithoutArgs;
64using testing::MakePolymorphicAction;
65using testing::Ne;
66using testing::PolymorphicAction;
67using testing::Return;
68using testing::ReturnNull;
69using testing::ReturnRef;
70using testing::SetArgumentPointee;
71using testing::SetArrayArgument;
zhanyong.wan5b5d62f2009-03-11 23:37:56 +000072
73#ifndef _WIN32_WCE
shiqiane35fdd92008-12-10 05:08:54 +000074using testing::SetErrnoAndReturn;
zhanyong.wan5b5d62f2009-03-11 23:37:56 +000075#endif // _WIN32_WCE
shiqiane35fdd92008-12-10 05:08:54 +000076
77#if GMOCK_HAS_PROTOBUF_
78using testing::internal::TestMessage;
79#endif // GMOCK_HAS_PROTOBUF_
80
81// Tests that BuiltInDefaultValue<T*>::Get() returns NULL.
82TEST(BuiltInDefaultValueTest, IsNullForPointerTypes) {
83 EXPECT_TRUE(BuiltInDefaultValue<int*>::Get() == NULL);
84 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Get() == NULL);
85 EXPECT_TRUE(BuiltInDefaultValue<void*>::Get() == NULL);
86}
87
zhanyong.wan5b95fa72009-01-27 22:28:45 +000088// Tests that BuiltInDefaultValue<T*>::Exists() return true.
89TEST(BuiltInDefaultValueTest, ExistsForPointerTypes) {
90 EXPECT_TRUE(BuiltInDefaultValue<int*>::Exists());
91 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Exists());
92 EXPECT_TRUE(BuiltInDefaultValue<void*>::Exists());
93}
94
shiqiane35fdd92008-12-10 05:08:54 +000095// Tests that BuiltInDefaultValue<T>::Get() returns 0 when T is a
96// built-in numeric type.
97TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) {
98 EXPECT_EQ(0, BuiltInDefaultValue<unsigned char>::Get());
99 EXPECT_EQ(0, BuiltInDefaultValue<signed char>::Get());
100 EXPECT_EQ(0, BuiltInDefaultValue<char>::Get());
zhanyong.wan652540a2009-02-23 23:37:29 +0000101#if !GTEST_OS_WINDOWS
shiqiane35fdd92008-12-10 05:08:54 +0000102 EXPECT_EQ(0, BuiltInDefaultValue<unsigned wchar_t>::Get());
103 EXPECT_EQ(0, BuiltInDefaultValue<signed wchar_t>::Get());
zhanyong.wan652540a2009-02-23 23:37:29 +0000104#endif // !GTEST_OS_WINDOWS
shiqiane35fdd92008-12-10 05:08:54 +0000105 EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get());
106 EXPECT_EQ(0, BuiltInDefaultValue<unsigned short>::Get()); // NOLINT
107 EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get()); // NOLINT
108 EXPECT_EQ(0, BuiltInDefaultValue<short>::Get()); // NOLINT
109 EXPECT_EQ(0, BuiltInDefaultValue<unsigned int>::Get());
110 EXPECT_EQ(0, BuiltInDefaultValue<signed int>::Get());
111 EXPECT_EQ(0, BuiltInDefaultValue<int>::Get());
112 EXPECT_EQ(0, BuiltInDefaultValue<unsigned long>::Get()); // NOLINT
113 EXPECT_EQ(0, BuiltInDefaultValue<signed long>::Get()); // NOLINT
114 EXPECT_EQ(0, BuiltInDefaultValue<long>::Get()); // NOLINT
115 EXPECT_EQ(0, BuiltInDefaultValue<UInt64>::Get());
116 EXPECT_EQ(0, BuiltInDefaultValue<Int64>::Get());
117 EXPECT_EQ(0, BuiltInDefaultValue<float>::Get());
118 EXPECT_EQ(0, BuiltInDefaultValue<double>::Get());
119}
120
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000121// Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
122// built-in numeric type.
123TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) {
124 EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists());
125 EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists());
126 EXPECT_TRUE(BuiltInDefaultValue<char>::Exists());
zhanyong.wan652540a2009-02-23 23:37:29 +0000127#if !GTEST_OS_WINDOWS
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000128 EXPECT_TRUE(BuiltInDefaultValue<unsigned wchar_t>::Exists());
129 EXPECT_TRUE(BuiltInDefaultValue<signed wchar_t>::Exists());
zhanyong.wan652540a2009-02-23 23:37:29 +0000130#endif // !GTEST_OS_WINDOWS
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000131 EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists());
132 EXPECT_TRUE(BuiltInDefaultValue<unsigned short>::Exists()); // NOLINT
133 EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists()); // NOLINT
134 EXPECT_TRUE(BuiltInDefaultValue<short>::Exists()); // NOLINT
135 EXPECT_TRUE(BuiltInDefaultValue<unsigned int>::Exists());
136 EXPECT_TRUE(BuiltInDefaultValue<signed int>::Exists());
137 EXPECT_TRUE(BuiltInDefaultValue<int>::Exists());
138 EXPECT_TRUE(BuiltInDefaultValue<unsigned long>::Exists()); // NOLINT
139 EXPECT_TRUE(BuiltInDefaultValue<signed long>::Exists()); // NOLINT
140 EXPECT_TRUE(BuiltInDefaultValue<long>::Exists()); // NOLINT
141 EXPECT_TRUE(BuiltInDefaultValue<UInt64>::Exists());
142 EXPECT_TRUE(BuiltInDefaultValue<Int64>::Exists());
143 EXPECT_TRUE(BuiltInDefaultValue<float>::Exists());
144 EXPECT_TRUE(BuiltInDefaultValue<double>::Exists());
145}
146
shiqiane35fdd92008-12-10 05:08:54 +0000147// Tests that BuiltInDefaultValue<bool>::Get() returns false.
148TEST(BuiltInDefaultValueTest, IsFalseForBool) {
149 EXPECT_FALSE(BuiltInDefaultValue<bool>::Get());
150}
151
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000152// Tests that BuiltInDefaultValue<bool>::Exists() returns true.
153TEST(BuiltInDefaultValueTest, BoolExists) {
154 EXPECT_TRUE(BuiltInDefaultValue<bool>::Exists());
155}
156
shiqiane35fdd92008-12-10 05:08:54 +0000157// Tests that BuiltInDefaultValue<T>::Get() returns "" when T is a
158// string type.
159TEST(BuiltInDefaultValueTest, IsEmptyStringForString) {
160#if GTEST_HAS_GLOBAL_STRING
161 EXPECT_EQ("", BuiltInDefaultValue< ::string>::Get());
162#endif // GTEST_HAS_GLOBAL_STRING
163
164#if GTEST_HAS_STD_STRING
165 EXPECT_EQ("", BuiltInDefaultValue< ::std::string>::Get());
166#endif // GTEST_HAS_STD_STRING
167}
168
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000169// Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
170// string type.
171TEST(BuiltInDefaultValueTest, ExistsForString) {
172#if GTEST_HAS_GLOBAL_STRING
173 EXPECT_TRUE(BuiltInDefaultValue< ::string>::Exists());
174#endif // GTEST_HAS_GLOBAL_STRING
175
176#if GTEST_HAS_STD_STRING
177 EXPECT_TRUE(BuiltInDefaultValue< ::std::string>::Exists());
178#endif // GTEST_HAS_STD_STRING
179}
180
shiqiane35fdd92008-12-10 05:08:54 +0000181// Tests that BuiltInDefaultValue<const T>::Get() returns the same
182// value as BuiltInDefaultValue<T>::Get() does.
183TEST(BuiltInDefaultValueTest, WorksForConstTypes) {
184 EXPECT_EQ("", BuiltInDefaultValue<const std::string>::Get());
185 EXPECT_EQ(0, BuiltInDefaultValue<const int>::Get());
186 EXPECT_TRUE(BuiltInDefaultValue<char* const>::Get() == NULL);
187 EXPECT_FALSE(BuiltInDefaultValue<const bool>::Get());
188}
189
190// Tests that BuiltInDefaultValue<T>::Get() aborts the program with
191// the correct error message when T is a user-defined type.
192struct UserType {
193 UserType() : value(0) {}
194
195 int value;
196};
197
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000198TEST(BuiltInDefaultValueTest, UserTypeHasNoDefault) {
199 EXPECT_FALSE(BuiltInDefaultValue<UserType>::Exists());
200}
201
zhanyong.wan652540a2009-02-23 23:37:29 +0000202#if GTEST_HAS_DEATH_TEST
shiqiane35fdd92008-12-10 05:08:54 +0000203
204// Tests that BuiltInDefaultValue<T&>::Get() aborts the program.
205TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) {
206 EXPECT_DEATH({ // NOLINT
207 BuiltInDefaultValue<int&>::Get();
208 }, "");
209 EXPECT_DEATH({ // NOLINT
210 BuiltInDefaultValue<const char&>::Get();
211 }, "");
212}
213
214TEST(BuiltInDefaultValueDeathTest, IsUndefinedForUserTypes) {
215 EXPECT_DEATH({ // NOLINT
216 BuiltInDefaultValue<UserType>::Get();
217 }, "");
218}
219
220#endif // GTEST_HAS_DEATH_TEST
221
222// Tests that DefaultValue<T>::IsSet() is false initially.
223TEST(DefaultValueTest, IsInitiallyUnset) {
224 EXPECT_FALSE(DefaultValue<int>::IsSet());
225 EXPECT_FALSE(DefaultValue<const UserType>::IsSet());
226}
227
228// Tests that DefaultValue<T> can be set and then unset.
229TEST(DefaultValueTest, CanBeSetAndUnset) {
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000230 EXPECT_TRUE(DefaultValue<int>::Exists());
231 EXPECT_FALSE(DefaultValue<const UserType>::Exists());
232
shiqiane35fdd92008-12-10 05:08:54 +0000233 DefaultValue<int>::Set(1);
234 DefaultValue<const UserType>::Set(UserType());
235
236 EXPECT_EQ(1, DefaultValue<int>::Get());
237 EXPECT_EQ(0, DefaultValue<const UserType>::Get().value);
238
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000239 EXPECT_TRUE(DefaultValue<int>::Exists());
240 EXPECT_TRUE(DefaultValue<const UserType>::Exists());
241
shiqiane35fdd92008-12-10 05:08:54 +0000242 DefaultValue<int>::Clear();
243 DefaultValue<const UserType>::Clear();
244
245 EXPECT_FALSE(DefaultValue<int>::IsSet());
246 EXPECT_FALSE(DefaultValue<const UserType>::IsSet());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000247
248 EXPECT_TRUE(DefaultValue<int>::Exists());
249 EXPECT_FALSE(DefaultValue<const UserType>::Exists());
shiqiane35fdd92008-12-10 05:08:54 +0000250}
251
252// Tests that DefaultValue<T>::Get() returns the
253// BuiltInDefaultValue<T>::Get() when DefaultValue<T>::IsSet() is
254// false.
255TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
256 EXPECT_FALSE(DefaultValue<int>::IsSet());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000257 EXPECT_TRUE(DefaultValue<int>::Exists());
shiqiane35fdd92008-12-10 05:08:54 +0000258 EXPECT_FALSE(DefaultValue<UserType>::IsSet());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000259 EXPECT_FALSE(DefaultValue<UserType>::Exists());
shiqiane35fdd92008-12-10 05:08:54 +0000260
261 EXPECT_EQ(0, DefaultValue<int>::Get());
262
zhanyong.wan652540a2009-02-23 23:37:29 +0000263#if GTEST_HAS_DEATH_TEST
shiqiane35fdd92008-12-10 05:08:54 +0000264 EXPECT_DEATH({ // NOLINT
265 DefaultValue<UserType>::Get();
266 }, "");
267#endif // GTEST_HAS_DEATH_TEST
268}
269
270// Tests that DefaultValue<void>::Get() returns void.
271TEST(DefaultValueTest, GetWorksForVoid) {
272 return DefaultValue<void>::Get();
273}
274
275// Tests using DefaultValue with a reference type.
276
277// Tests that DefaultValue<T&>::IsSet() is false initially.
278TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) {
279 EXPECT_FALSE(DefaultValue<int&>::IsSet());
280 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
281}
282
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000283// Tests that DefaultValue<T&>::Exists is false initiallly.
284TEST(DefaultValueOfReferenceTest, IsInitiallyNotExisting) {
285 EXPECT_FALSE(DefaultValue<int&>::Exists());
286 EXPECT_FALSE(DefaultValue<UserType&>::Exists());
287}
288
shiqiane35fdd92008-12-10 05:08:54 +0000289// Tests that DefaultValue<T&> can be set and then unset.
290TEST(DefaultValueOfReferenceTest, CanBeSetAndUnset) {
291 int n = 1;
292 DefaultValue<const int&>::Set(n);
293 UserType u;
294 DefaultValue<UserType&>::Set(u);
295
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000296 EXPECT_TRUE(DefaultValue<const int&>::Exists());
297 EXPECT_TRUE(DefaultValue<UserType&>::Exists());
298
shiqiane35fdd92008-12-10 05:08:54 +0000299 EXPECT_EQ(&n, &(DefaultValue<const int&>::Get()));
300 EXPECT_EQ(&u, &(DefaultValue<UserType&>::Get()));
301
302 DefaultValue<const int&>::Clear();
303 DefaultValue<UserType&>::Clear();
304
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000305 EXPECT_FALSE(DefaultValue<const int&>::Exists());
306 EXPECT_FALSE(DefaultValue<UserType&>::Exists());
307
shiqiane35fdd92008-12-10 05:08:54 +0000308 EXPECT_FALSE(DefaultValue<const int&>::IsSet());
309 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
310}
311
312// Tests that DefaultValue<T&>::Get() returns the
313// BuiltInDefaultValue<T&>::Get() when DefaultValue<T&>::IsSet() is
314// false.
315TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
316 EXPECT_FALSE(DefaultValue<int&>::IsSet());
317 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
318
zhanyong.wan652540a2009-02-23 23:37:29 +0000319#if GTEST_HAS_DEATH_TEST
shiqiane35fdd92008-12-10 05:08:54 +0000320 EXPECT_DEATH({ // NOLINT
321 DefaultValue<int&>::Get();
322 }, "");
323 EXPECT_DEATH({ // NOLINT
324 DefaultValue<UserType>::Get();
325 }, "");
326#endif // GTEST_HAS_DEATH_TEST
327}
328
329// Tests that ActionInterface can be implemented by defining the
330// Perform method.
331
332typedef int MyFunction(bool, int);
333
334class MyActionImpl : public ActionInterface<MyFunction> {
335 public:
336 virtual int Perform(const tuple<bool, int>& args) {
337 return get<0>(args) ? get<1>(args) : 0;
338 }
339};
340
341TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) {
342 MyActionImpl my_action_impl;
343
344 EXPECT_FALSE(my_action_impl.IsDoDefault());
345}
346
347TEST(ActionInterfaceTest, MakeAction) {
348 Action<MyFunction> action = MakeAction(new MyActionImpl);
349
350 // When exercising the Perform() method of Action<F>, we must pass
351 // it a tuple whose size and type are compatible with F's argument
352 // types. For example, if F is int(), then Perform() takes a
353 // 0-tuple; if F is void(bool, int), then Perform() takes a
354 // tuple<bool, int>, and so on.
355 EXPECT_EQ(5, action.Perform(make_tuple(true, 5)));
356}
357
358// Tests that Action<F> can be contructed from a pointer to
359// ActionInterface<F>.
360TEST(ActionTest, CanBeConstructedFromActionInterface) {
361 Action<MyFunction> action(new MyActionImpl);
362}
363
364// Tests that Action<F> delegates actual work to ActionInterface<F>.
365TEST(ActionTest, DelegatesWorkToActionInterface) {
366 const Action<MyFunction> action(new MyActionImpl);
367
368 EXPECT_EQ(5, action.Perform(make_tuple(true, 5)));
369 EXPECT_EQ(0, action.Perform(make_tuple(false, 1)));
370}
371
372// Tests that Action<F> can be copied.
373TEST(ActionTest, IsCopyable) {
374 Action<MyFunction> a1(new MyActionImpl);
375 Action<MyFunction> a2(a1); // Tests the copy constructor.
376
377 // a1 should continue to work after being copied from.
378 EXPECT_EQ(5, a1.Perform(make_tuple(true, 5)));
379 EXPECT_EQ(0, a1.Perform(make_tuple(false, 1)));
380
381 // a2 should work like the action it was copied from.
382 EXPECT_EQ(5, a2.Perform(make_tuple(true, 5)));
383 EXPECT_EQ(0, a2.Perform(make_tuple(false, 1)));
384
385 a2 = a1; // Tests the assignment operator.
386
387 // a1 should continue to work after being copied from.
388 EXPECT_EQ(5, a1.Perform(make_tuple(true, 5)));
389 EXPECT_EQ(0, a1.Perform(make_tuple(false, 1)));
390
391 // a2 should work like the action it was copied from.
392 EXPECT_EQ(5, a2.Perform(make_tuple(true, 5)));
393 EXPECT_EQ(0, a2.Perform(make_tuple(false, 1)));
394}
395
396// Tests that an Action<From> object can be converted to a
397// compatible Action<To> object.
398
399class IsNotZero : public ActionInterface<bool(int)> { // NOLINT
400 public:
401 virtual bool Perform(const tuple<int>& arg) {
402 return get<0>(arg) != 0;
403 }
404};
405
406TEST(ActionTest, CanBeConvertedToOtherActionType) {
407 const Action<bool(int)> a1(new IsNotZero); // NOLINT
408 const Action<int(char)> a2 = Action<int(char)>(a1); // NOLINT
409 EXPECT_EQ(1, a2.Perform(make_tuple('a')));
410 EXPECT_EQ(0, a2.Perform(make_tuple('\0')));
411}
412
413// The following two classes are for testing MakePolymorphicAction().
414
415// Implements a polymorphic action that returns the second of the
416// arguments it receives.
417class ReturnSecondArgumentAction {
418 public:
419 // We want to verify that MakePolymorphicAction() can work with a
420 // polymorphic action whose Perform() method template is either
421 // const or not. This lets us verify the non-const case.
422 template <typename Result, typename ArgumentTuple>
423 Result Perform(const ArgumentTuple& args) { return get<1>(args); }
424};
425
426// Implements a polymorphic action that can be used in a nullary
427// function to return 0.
428class ReturnZeroFromNullaryFunctionAction {
429 public:
430 // For testing that MakePolymorphicAction() works when the
431 // implementation class' Perform() method template takes only one
432 // template parameter.
433 //
434 // We want to verify that MakePolymorphicAction() can work with a
435 // polymorphic action whose Perform() method template is either
436 // const or not. This lets us verify the const case.
437 template <typename Result>
438 Result Perform(const tuple<>&) const { return 0; }
439};
440
441// These functions verify that MakePolymorphicAction() returns a
442// PolymorphicAction<T> where T is the argument's type.
443
444PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() {
445 return MakePolymorphicAction(ReturnSecondArgumentAction());
446}
447
448PolymorphicAction<ReturnZeroFromNullaryFunctionAction>
449ReturnZeroFromNullaryFunction() {
450 return MakePolymorphicAction(ReturnZeroFromNullaryFunctionAction());
451}
452
453// Tests that MakePolymorphicAction() turns a polymorphic action
454// implementation class into a polymorphic action.
455TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) {
456 Action<int(bool, int, double)> a1 = ReturnSecondArgument(); // NOLINT
457 EXPECT_EQ(5, a1.Perform(make_tuple(false, 5, 2.0)));
458}
459
460// Tests that MakePolymorphicAction() works when the implementation
461// class' Perform() method template has only one template parameter.
462TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) {
463 Action<int()> a1 = ReturnZeroFromNullaryFunction();
464 EXPECT_EQ(0, a1.Perform(make_tuple()));
465
466 Action<void*()> a2 = ReturnZeroFromNullaryFunction();
467 EXPECT_TRUE(a2.Perform(make_tuple()) == NULL);
468}
469
470// Tests that Return() works as an action for void-returning
471// functions.
472TEST(ReturnTest, WorksForVoid) {
473 const Action<void(int)> ret = Return(); // NOLINT
474 return ret.Perform(make_tuple(1));
475}
476
477// Tests that Return(v) returns v.
478TEST(ReturnTest, ReturnsGivenValue) {
479 Action<int()> ret = Return(1); // NOLINT
480 EXPECT_EQ(1, ret.Perform(make_tuple()));
481
482 ret = Return(-5);
483 EXPECT_EQ(-5, ret.Perform(make_tuple()));
484}
485
486// Tests that Return("string literal") works.
487TEST(ReturnTest, AcceptsStringLiteral) {
488 Action<const char*()> a1 = Return("Hello");
489 EXPECT_STREQ("Hello", a1.Perform(make_tuple()));
490
491 Action<std::string()> a2 = Return("world");
492 EXPECT_EQ("world", a2.Perform(make_tuple()));
493}
494
495// Tests that Return(v) is covaraint.
496
497struct Base {
498 bool operator==(const Base&) { return true; }
499};
500
501struct Derived : public Base {
502 bool operator==(const Derived&) { return true; }
503};
504
505TEST(ReturnTest, IsCovariant) {
506 Base base;
507 Derived derived;
508 Action<Base*()> ret = Return(&base);
509 EXPECT_EQ(&base, ret.Perform(make_tuple()));
510
511 ret = Return(&derived);
512 EXPECT_EQ(&derived, ret.Perform(make_tuple()));
513}
514
515// Tests that ReturnNull() returns NULL in a pointer-returning function.
516TEST(ReturnNullTest, WorksInPointerReturningFunction) {
517 const Action<int*()> a1 = ReturnNull();
518 EXPECT_TRUE(a1.Perform(make_tuple()) == NULL);
519
520 const Action<const char*(bool)> a2 = ReturnNull(); // NOLINT
521 EXPECT_TRUE(a2.Perform(make_tuple(true)) == NULL);
522}
523
524// Tests that ReturnRef(v) works for reference types.
525TEST(ReturnRefTest, WorksForReference) {
526 const int n = 0;
527 const Action<const int&(bool)> ret = ReturnRef(n); // NOLINT
528
529 EXPECT_EQ(&n, &ret.Perform(make_tuple(true)));
530}
531
532// Tests that ReturnRef(v) is covariant.
533TEST(ReturnRefTest, IsCovariant) {
534 Base base;
535 Derived derived;
536 Action<Base&()> a = ReturnRef(base);
537 EXPECT_EQ(&base, &a.Perform(make_tuple()));
538
539 a = ReturnRef(derived);
540 EXPECT_EQ(&derived, &a.Perform(make_tuple()));
541}
542
543// Tests that DoDefault() does the default action for the mock method.
544
545class MyClass {};
546
547class MockClass {
548 public:
549 MOCK_METHOD1(IntFunc, int(bool flag)); // NOLINT
550 MOCK_METHOD0(Foo, MyClass());
551};
552
553// Tests that DoDefault() returns the built-in default value for the
554// return type by default.
555TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) {
556 MockClass mock;
557 EXPECT_CALL(mock, IntFunc(_))
558 .WillOnce(DoDefault());
559 EXPECT_EQ(0, mock.IntFunc(true));
560}
561
zhanyong.wan652540a2009-02-23 23:37:29 +0000562#if GTEST_HAS_DEATH_TEST
shiqiane35fdd92008-12-10 05:08:54 +0000563
564// Tests that DoDefault() aborts the process when there is no built-in
565// default value for the return type.
566TEST(DoDefaultDeathTest, DiesForUnknowType) {
567 MockClass mock;
568 EXPECT_CALL(mock, Foo())
569 .WillRepeatedly(DoDefault());
570 EXPECT_DEATH({ // NOLINT
571 mock.Foo();
572 }, "");
573}
574
575// Tests that using DoDefault() inside a composite action leads to a
576// run-time error.
577
578void VoidFunc(bool flag) {}
579
580TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
581 MockClass mock;
582 EXPECT_CALL(mock, IntFunc(_))
583 .WillRepeatedly(DoAll(Invoke(VoidFunc),
584 DoDefault()));
585
586 // Ideally we should verify the error message as well. Sadly,
587 // EXPECT_DEATH() can only capture stderr, while Google Mock's
588 // errors are printed on stdout. Therefore we have to settle for
589 // not verifying the message.
590 EXPECT_DEATH({ // NOLINT
591 mock.IntFunc(true);
592 }, "");
593}
594
595#endif // GTEST_HAS_DEATH_TEST
596
597// Tests that DoDefault() returns the default value set by
598// DefaultValue<T>::Set() when it's not overriden by an ON_CALL().
599TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) {
600 DefaultValue<int>::Set(1);
601 MockClass mock;
602 EXPECT_CALL(mock, IntFunc(_))
603 .WillOnce(DoDefault());
604 EXPECT_EQ(1, mock.IntFunc(false));
605 DefaultValue<int>::Clear();
606}
607
608// Tests that DoDefault() does the action specified by ON_CALL().
609TEST(DoDefaultTest, DoesWhatOnCallSpecifies) {
610 MockClass mock;
611 ON_CALL(mock, IntFunc(_))
612 .WillByDefault(Return(2));
613 EXPECT_CALL(mock, IntFunc(_))
614 .WillOnce(DoDefault());
615 EXPECT_EQ(2, mock.IntFunc(false));
616}
617
618// Tests that using DoDefault() in ON_CALL() leads to a run-time failure.
619TEST(DoDefaultTest, CannotBeUsedInOnCall) {
620 MockClass mock;
621 EXPECT_NONFATAL_FAILURE({ // NOLINT
622 ON_CALL(mock, IntFunc(_))
623 .WillByDefault(DoDefault());
624 }, "DoDefault() cannot be used in ON_CALL()");
625}
626
627// Tests that SetArgumentPointee<N>(v) sets the variable pointed to by
628// the N-th (0-based) argument to v.
629TEST(SetArgumentPointeeTest, SetsTheNthPointee) {
630 typedef void MyFunction(bool, int*, char*);
631 Action<MyFunction> a = SetArgumentPointee<1>(2);
632
633 int n = 0;
634 char ch = '\0';
635 a.Perform(make_tuple(true, &n, &ch));
636 EXPECT_EQ(2, n);
637 EXPECT_EQ('\0', ch);
638
639 a = SetArgumentPointee<2>('a');
640 n = 0;
641 ch = '\0';
642 a.Perform(make_tuple(true, &n, &ch));
643 EXPECT_EQ(0, n);
644 EXPECT_EQ('a', ch);
645}
646
647#if GMOCK_HAS_PROTOBUF_
648
zhanyong.wanc6a41232009-05-13 23:38:40 +0000649// Tests that SetArgumentPointee<N>(proto_buffer) sets the v1 protobuf
650// variable pointed to by the N-th (0-based) argument to proto_buffer.
shiqiane35fdd92008-12-10 05:08:54 +0000651TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
shiqiane35fdd92008-12-10 05:08:54 +0000652 TestMessage* const msg = new TestMessage;
653 msg->set_member("yes");
654 TestMessage orig_msg;
655 orig_msg.CopyFrom(*msg);
656
zhanyong.wanc6a41232009-05-13 23:38:40 +0000657 Action<void(bool, TestMessage*)> a = SetArgumentPointee<1>(*msg);
shiqiane35fdd92008-12-10 05:08:54 +0000658 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
659 // s.t. the action works even when the original proto_buffer has
660 // died. We ensure this behavior by deleting msg before using the
661 // action.
662 delete msg;
663
664 TestMessage dest;
665 EXPECT_FALSE(orig_msg.Equals(dest));
666 a.Perform(make_tuple(true, &dest));
667 EXPECT_TRUE(orig_msg.Equals(dest));
668}
669
zhanyong.wanc6a41232009-05-13 23:38:40 +0000670// Tests that SetArgumentPointee<N>(proto_buffer) sets the
671// ::ProtocolMessage variable pointed to by the N-th (0-based)
672// argument to proto_buffer.
673TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
674 TestMessage* const msg = new TestMessage;
675 msg->set_member("yes");
676 TestMessage orig_msg;
677 orig_msg.CopyFrom(*msg);
678
679 Action<void(bool, ::ProtocolMessage*)> a = SetArgumentPointee<1>(*msg);
680 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
681 // s.t. the action works even when the original proto_buffer has
682 // died. We ensure this behavior by deleting msg before using the
683 // action.
684 delete msg;
685
686 TestMessage dest;
687 ::ProtocolMessage* const dest_base = &dest;
688 EXPECT_FALSE(orig_msg.Equals(dest));
689 a.Perform(make_tuple(true, dest_base));
690 EXPECT_TRUE(orig_msg.Equals(dest));
691}
692
693// Tests that SetArgumentPointee<N>(proto2_buffer) sets the v2
694// protobuf variable pointed to by the N-th (0-based) argument to
695// proto2_buffer.
shiqiane35fdd92008-12-10 05:08:54 +0000696TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
697 using testing::internal::FooMessage;
shiqiane35fdd92008-12-10 05:08:54 +0000698 FooMessage* const msg = new FooMessage;
699 msg->set_int_field(2);
700 msg->set_string_field("hi");
701 FooMessage orig_msg;
702 orig_msg.CopyFrom(*msg);
703
zhanyong.wanc6a41232009-05-13 23:38:40 +0000704 Action<void(bool, FooMessage*)> a = SetArgumentPointee<1>(*msg);
shiqiane35fdd92008-12-10 05:08:54 +0000705 // SetArgumentPointee<N>(proto2_buffer) makes a copy of
706 // proto2_buffer s.t. the action works even when the original
707 // proto2_buffer has died. We ensure this behavior by deleting msg
708 // before using the action.
709 delete msg;
710
711 FooMessage dest;
712 dest.set_int_field(0);
713 a.Perform(make_tuple(true, &dest));
714 EXPECT_EQ(2, dest.int_field());
715 EXPECT_EQ("hi", dest.string_field());
716}
717
zhanyong.wanc6a41232009-05-13 23:38:40 +0000718// Tests that SetArgumentPointee<N>(proto2_buffer) sets the
719// proto2::Message variable pointed to by the N-th (0-based) argument
720// to proto2_buffer.
721TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
722 using testing::internal::FooMessage;
723 FooMessage* const msg = new FooMessage;
724 msg->set_int_field(2);
725 msg->set_string_field("hi");
726 FooMessage orig_msg;
727 orig_msg.CopyFrom(*msg);
728
729 Action<void(bool, ::proto2::Message*)> a = SetArgumentPointee<1>(*msg);
730 // SetArgumentPointee<N>(proto2_buffer) makes a copy of
731 // proto2_buffer s.t. the action works even when the original
732 // proto2_buffer has died. We ensure this behavior by deleting msg
733 // before using the action.
734 delete msg;
735
736 FooMessage dest;
737 dest.set_int_field(0);
738 ::proto2::Message* const dest_base = &dest;
739 a.Perform(make_tuple(true, dest_base));
740 EXPECT_EQ(2, dest.int_field());
741 EXPECT_EQ("hi", dest.string_field());
742}
743
shiqiane35fdd92008-12-10 05:08:54 +0000744#endif // GMOCK_HAS_PROTOBUF_
745
746// Tests that SetArrayArgument<N>(first, last) sets the elements of the array
747// pointed to by the N-th (0-based) argument to values in range [first, last).
748TEST(SetArrayArgumentTest, SetsTheNthArray) {
749 typedef void MyFunction(bool, int*, char*);
750 int numbers[] = { 1, 2, 3 };
751 Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers + 3);
752
753 int n[4] = {};
754 int* pn = n;
755 char ch[4] = {};
756 char* pch = ch;
757 a.Perform(make_tuple(true, pn, pch));
758 EXPECT_EQ(1, n[0]);
759 EXPECT_EQ(2, n[1]);
760 EXPECT_EQ(3, n[2]);
761 EXPECT_EQ(0, n[3]);
762 EXPECT_EQ('\0', ch[0]);
763 EXPECT_EQ('\0', ch[1]);
764 EXPECT_EQ('\0', ch[2]);
765 EXPECT_EQ('\0', ch[3]);
766
767 // Tests first and last are iterators.
768 std::string letters = "abc";
769 a = SetArrayArgument<2>(letters.begin(), letters.end());
770 std::fill_n(n, 4, 0);
771 std::fill_n(ch, 4, '\0');
772 a.Perform(make_tuple(true, pn, pch));
773 EXPECT_EQ(0, n[0]);
774 EXPECT_EQ(0, n[1]);
775 EXPECT_EQ(0, n[2]);
776 EXPECT_EQ(0, n[3]);
777 EXPECT_EQ('a', ch[0]);
778 EXPECT_EQ('b', ch[1]);
779 EXPECT_EQ('c', ch[2]);
780 EXPECT_EQ('\0', ch[3]);
781}
782
783// Tests SetArrayArgument<N>(first, last) where first == last.
784TEST(SetArrayArgumentTest, SetsTheNthArrayWithEmptyRange) {
785 typedef void MyFunction(bool, int*);
786 int numbers[] = { 1, 2, 3 };
787 Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers);
788
789 int n[4] = {};
790 int* pn = n;
791 a.Perform(make_tuple(true, pn));
792 EXPECT_EQ(0, n[0]);
793 EXPECT_EQ(0, n[1]);
794 EXPECT_EQ(0, n[2]);
795 EXPECT_EQ(0, n[3]);
796}
797
798// Tests SetArrayArgument<N>(first, last) where *first is convertible
799// (but not equal) to the argument type.
800TEST(SetArrayArgumentTest, SetsTheNthArrayWithConvertibleType) {
801 typedef void MyFunction(bool, char*);
802 int codes[] = { 97, 98, 99 };
803 Action<MyFunction> a = SetArrayArgument<1>(codes, codes + 3);
804
805 char ch[4] = {};
806 char* pch = ch;
807 a.Perform(make_tuple(true, pch));
808 EXPECT_EQ('a', ch[0]);
809 EXPECT_EQ('b', ch[1]);
810 EXPECT_EQ('c', ch[2]);
811 EXPECT_EQ('\0', ch[3]);
812}
813
814// Test SetArrayArgument<N>(first, last) with iterator as argument.
815TEST(SetArrayArgumentTest, SetsTheNthArrayWithIteratorArgument) {
816 typedef void MyFunction(bool, std::back_insert_iterator<std::string>);
817 std::string letters = "abc";
818 Action<MyFunction> a = SetArrayArgument<1>(letters.begin(), letters.end());
819
820 std::string s;
821 a.Perform(make_tuple(true, back_inserter(s)));
822 EXPECT_EQ(letters, s);
823}
824
825// Sample functions and functors for testing Invoke() and etc.
826int Nullary() { return 1; }
827
828class NullaryFunctor {
829 public:
830 int operator()() { return 2; }
831};
832
833bool g_done = false;
834void VoidNullary() { g_done = true; }
835
836class VoidNullaryFunctor {
837 public:
838 void operator()() { g_done = true; }
839};
840
841bool Unary(int x) { return x < 0; }
842
843const char* Plus1(const char* s) { return s + 1; }
844
845void VoidUnary(int n) { g_done = true; }
846
847bool ByConstRef(const std::string& s) { return s == "Hi"; }
848
849const double g_double = 0;
850bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
851
852std::string ByNonConstRef(std::string& s) { return s += "+"; } // NOLINT
853
854struct UnaryFunctor {
855 int operator()(bool x) { return x ? 1 : -1; }
856};
857
858const char* Binary(const char* input, short n) { return input + n; } // NOLINT
859
860void VoidBinary(int, char) { g_done = true; }
861
862int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT
863
864void VoidTernary(int, char, bool) { g_done = true; }
865
866int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
867
868void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; }
869
870int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
871
872struct SumOf5Functor {
873 int operator()(int a, int b, int c, int d, int e) {
874 return a + b + c + d + e;
875 }
876};
877
878int SumOf6(int a, int b, int c, int d, int e, int f) {
879 return a + b + c + d + e + f;
880}
881
882struct SumOf6Functor {
883 int operator()(int a, int b, int c, int d, int e, int f) {
884 return a + b + c + d + e + f;
885 }
886};
887
888class Foo {
889 public:
890 Foo() : value_(123) {}
891
892 int Nullary() const { return value_; }
893 short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT
894 std::string Binary(const std::string& str, char c) const { return str + c; }
895 int Ternary(int x, bool y, char z) { return value_ + x + y*z; }
896 int SumOf4(int a, int b, int c, int d) const {
897 return a + b + c + d + value_;
898 }
899 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
900 int SumOf6(int a, int b, int c, int d, int e, int f) {
901 return a + b + c + d + e + f;
902 }
903 private:
904 int value_;
905};
906
907// Tests InvokeWithoutArgs(function).
908TEST(InvokeWithoutArgsTest, Function) {
909 // As an action that takes one argument.
910 Action<int(int)> a = InvokeWithoutArgs(Nullary); // NOLINT
911 EXPECT_EQ(1, a.Perform(make_tuple(2)));
912
913 // As an action that takes two arguments.
914 Action<short(int, double)> a2 = InvokeWithoutArgs(Nullary); // NOLINT
915 EXPECT_EQ(1, a2.Perform(make_tuple(2, 3.5)));
916
917 // As an action that returns void.
918 Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary); // NOLINT
919 g_done = false;
920 a3.Perform(make_tuple(1));
921 EXPECT_TRUE(g_done);
922}
923
924// Tests InvokeWithoutArgs(functor).
925TEST(InvokeWithoutArgsTest, Functor) {
926 // As an action that takes no argument.
927 Action<int()> a = InvokeWithoutArgs(NullaryFunctor()); // NOLINT
928 EXPECT_EQ(2, a.Perform(make_tuple()));
929
930 // As an action that takes three arguments.
931 Action<short(int, double, char)> a2 = // NOLINT
932 InvokeWithoutArgs(NullaryFunctor());
933 EXPECT_EQ(2, a2.Perform(make_tuple(3, 3.5, 'a')));
934
935 // As an action that returns void.
936 Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor());
937 g_done = false;
938 a3.Perform(make_tuple());
939 EXPECT_TRUE(g_done);
940}
941
942// Tests InvokeWithoutArgs(obj_ptr, method).
943TEST(InvokeWithoutArgsTest, Method) {
944 Foo foo;
945 Action<int(bool, char)> a = // NOLINT
946 InvokeWithoutArgs(&foo, &Foo::Nullary);
947 EXPECT_EQ(123, a.Perform(make_tuple(true, 'a')));
948}
949
950// Tests using IgnoreResult() on a polymorphic action.
951TEST(IgnoreResultTest, PolymorphicAction) {
952 Action<void(int)> a = IgnoreResult(Return(5)); // NOLINT
953 a.Perform(make_tuple(1));
954}
955
956// Tests using IgnoreResult() on a monomorphic action.
957
958int ReturnOne() {
959 g_done = true;
960 return 1;
961}
962
963TEST(IgnoreResultTest, MonomorphicAction) {
964 g_done = false;
965 Action<void()> a = IgnoreResult(Invoke(ReturnOne));
966 a.Perform(make_tuple());
967 EXPECT_TRUE(g_done);
968}
969
970// Tests using IgnoreResult() on an action that returns a class type.
971
972MyClass ReturnMyClass(double x) {
973 g_done = true;
974 return MyClass();
975}
976
977TEST(IgnoreResultTest, ActionReturningClass) {
978 g_done = false;
979 Action<void(int)> a = IgnoreResult(Invoke(ReturnMyClass)); // NOLINT
980 a.Perform(make_tuple(2));
981 EXPECT_TRUE(g_done);
982}
983
984TEST(AssignTest, Int) {
985 int x = 0;
986 Action<void(int)> a = Assign(&x, 5);
987 a.Perform(make_tuple(0));
988 EXPECT_EQ(5, x);
989}
990
991TEST(AssignTest, String) {
992 ::std::string x;
993 Action<void(void)> a = Assign(&x, "Hello, world");
994 a.Perform(make_tuple());
995 EXPECT_EQ("Hello, world", x);
996}
997
998TEST(AssignTest, CompatibleTypes) {
999 double x = 0;
1000 Action<void(int)> a = Assign(&x, 5);
1001 a.Perform(make_tuple(0));
1002 EXPECT_DOUBLE_EQ(5, x);
1003}
1004
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001005#ifndef _WIN32_WCE
1006
shiqiane35fdd92008-12-10 05:08:54 +00001007class SetErrnoAndReturnTest : public testing::Test {
1008 protected:
1009 virtual void SetUp() { errno = 0; }
1010 virtual void TearDown() { errno = 0; }
1011};
1012
1013TEST_F(SetErrnoAndReturnTest, Int) {
1014 Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5);
1015 EXPECT_EQ(-5, a.Perform(make_tuple()));
1016 EXPECT_EQ(ENOTTY, errno);
1017}
1018
1019TEST_F(SetErrnoAndReturnTest, Ptr) {
1020 int x;
1021 Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x);
1022 EXPECT_EQ(&x, a.Perform(make_tuple()));
1023 EXPECT_EQ(ENOTTY, errno);
1024}
1025
1026TEST_F(SetErrnoAndReturnTest, CompatibleTypes) {
1027 Action<double()> a = SetErrnoAndReturn(EINVAL, 5);
1028 EXPECT_DOUBLE_EQ(5.0, a.Perform(make_tuple()));
1029 EXPECT_EQ(EINVAL, errno);
1030}
1031
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001032#endif // _WIN32_WCE
1033
shiqiane35fdd92008-12-10 05:08:54 +00001034} // Unnamed namespace