blob: 9bf4c32d255ea01cc24847fa8e9e08de89acd750 [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;
zhanyong.wana18423e2009-07-22 23:58:19 +000059using testing::ByRef;
shiqiane35fdd92008-12-10 05:08:54 +000060using testing::DefaultValue;
61using testing::DoDefault;
62using testing::IgnoreResult;
63using testing::Invoke;
64using testing::InvokeWithoutArgs;
65using testing::MakePolymorphicAction;
66using testing::Ne;
67using testing::PolymorphicAction;
68using testing::Return;
69using testing::ReturnNull;
70using testing::ReturnRef;
71using testing::SetArgumentPointee;
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
shiqiane35fdd92008-12-10 05:08:54 +0000746// Sample functions and functors for testing Invoke() and etc.
747int Nullary() { return 1; }
748
749class NullaryFunctor {
750 public:
751 int operator()() { return 2; }
752};
753
754bool g_done = false;
755void VoidNullary() { g_done = true; }
756
757class VoidNullaryFunctor {
758 public:
759 void operator()() { g_done = true; }
760};
761
762bool Unary(int x) { return x < 0; }
763
764const char* Plus1(const char* s) { return s + 1; }
765
766void VoidUnary(int n) { g_done = true; }
767
768bool ByConstRef(const std::string& s) { return s == "Hi"; }
769
770const double g_double = 0;
771bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
772
773std::string ByNonConstRef(std::string& s) { return s += "+"; } // NOLINT
774
775struct UnaryFunctor {
776 int operator()(bool x) { return x ? 1 : -1; }
777};
778
779const char* Binary(const char* input, short n) { return input + n; } // NOLINT
780
781void VoidBinary(int, char) { g_done = true; }
782
783int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT
784
785void VoidTernary(int, char, bool) { g_done = true; }
786
787int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
788
789void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; }
790
791int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
792
793struct SumOf5Functor {
794 int operator()(int a, int b, int c, int d, int e) {
795 return a + b + c + d + e;
796 }
797};
798
799int SumOf6(int a, int b, int c, int d, int e, int f) {
800 return a + b + c + d + e + f;
801}
802
803struct SumOf6Functor {
804 int operator()(int a, int b, int c, int d, int e, int f) {
805 return a + b + c + d + e + f;
806 }
807};
808
809class Foo {
810 public:
811 Foo() : value_(123) {}
812
813 int Nullary() const { return value_; }
814 short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT
815 std::string Binary(const std::string& str, char c) const { return str + c; }
816 int Ternary(int x, bool y, char z) { return value_ + x + y*z; }
817 int SumOf4(int a, int b, int c, int d) const {
818 return a + b + c + d + value_;
819 }
820 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
821 int SumOf6(int a, int b, int c, int d, int e, int f) {
822 return a + b + c + d + e + f;
823 }
824 private:
825 int value_;
826};
827
828// Tests InvokeWithoutArgs(function).
829TEST(InvokeWithoutArgsTest, Function) {
830 // As an action that takes one argument.
831 Action<int(int)> a = InvokeWithoutArgs(Nullary); // NOLINT
832 EXPECT_EQ(1, a.Perform(make_tuple(2)));
833
834 // As an action that takes two arguments.
835 Action<short(int, double)> a2 = InvokeWithoutArgs(Nullary); // NOLINT
836 EXPECT_EQ(1, a2.Perform(make_tuple(2, 3.5)));
837
838 // As an action that returns void.
839 Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary); // NOLINT
840 g_done = false;
841 a3.Perform(make_tuple(1));
842 EXPECT_TRUE(g_done);
843}
844
845// Tests InvokeWithoutArgs(functor).
846TEST(InvokeWithoutArgsTest, Functor) {
847 // As an action that takes no argument.
848 Action<int()> a = InvokeWithoutArgs(NullaryFunctor()); // NOLINT
849 EXPECT_EQ(2, a.Perform(make_tuple()));
850
851 // As an action that takes three arguments.
852 Action<short(int, double, char)> a2 = // NOLINT
853 InvokeWithoutArgs(NullaryFunctor());
854 EXPECT_EQ(2, a2.Perform(make_tuple(3, 3.5, 'a')));
855
856 // As an action that returns void.
857 Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor());
858 g_done = false;
859 a3.Perform(make_tuple());
860 EXPECT_TRUE(g_done);
861}
862
863// Tests InvokeWithoutArgs(obj_ptr, method).
864TEST(InvokeWithoutArgsTest, Method) {
865 Foo foo;
866 Action<int(bool, char)> a = // NOLINT
867 InvokeWithoutArgs(&foo, &Foo::Nullary);
868 EXPECT_EQ(123, a.Perform(make_tuple(true, 'a')));
869}
870
871// Tests using IgnoreResult() on a polymorphic action.
872TEST(IgnoreResultTest, PolymorphicAction) {
873 Action<void(int)> a = IgnoreResult(Return(5)); // NOLINT
874 a.Perform(make_tuple(1));
875}
876
877// Tests using IgnoreResult() on a monomorphic action.
878
879int ReturnOne() {
880 g_done = true;
881 return 1;
882}
883
884TEST(IgnoreResultTest, MonomorphicAction) {
885 g_done = false;
886 Action<void()> a = IgnoreResult(Invoke(ReturnOne));
887 a.Perform(make_tuple());
888 EXPECT_TRUE(g_done);
889}
890
891// Tests using IgnoreResult() on an action that returns a class type.
892
893MyClass ReturnMyClass(double x) {
894 g_done = true;
895 return MyClass();
896}
897
898TEST(IgnoreResultTest, ActionReturningClass) {
899 g_done = false;
900 Action<void(int)> a = IgnoreResult(Invoke(ReturnMyClass)); // NOLINT
901 a.Perform(make_tuple(2));
902 EXPECT_TRUE(g_done);
903}
904
905TEST(AssignTest, Int) {
906 int x = 0;
907 Action<void(int)> a = Assign(&x, 5);
908 a.Perform(make_tuple(0));
909 EXPECT_EQ(5, x);
910}
911
912TEST(AssignTest, String) {
913 ::std::string x;
914 Action<void(void)> a = Assign(&x, "Hello, world");
915 a.Perform(make_tuple());
916 EXPECT_EQ("Hello, world", x);
917}
918
919TEST(AssignTest, CompatibleTypes) {
920 double x = 0;
921 Action<void(int)> a = Assign(&x, 5);
922 a.Perform(make_tuple(0));
923 EXPECT_DOUBLE_EQ(5, x);
924}
925
zhanyong.wan5b5d62f2009-03-11 23:37:56 +0000926#ifndef _WIN32_WCE
927
shiqiane35fdd92008-12-10 05:08:54 +0000928class SetErrnoAndReturnTest : public testing::Test {
929 protected:
930 virtual void SetUp() { errno = 0; }
931 virtual void TearDown() { errno = 0; }
932};
933
934TEST_F(SetErrnoAndReturnTest, Int) {
935 Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5);
936 EXPECT_EQ(-5, a.Perform(make_tuple()));
937 EXPECT_EQ(ENOTTY, errno);
938}
939
940TEST_F(SetErrnoAndReturnTest, Ptr) {
941 int x;
942 Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x);
943 EXPECT_EQ(&x, a.Perform(make_tuple()));
944 EXPECT_EQ(ENOTTY, errno);
945}
946
947TEST_F(SetErrnoAndReturnTest, CompatibleTypes) {
948 Action<double()> a = SetErrnoAndReturn(EINVAL, 5);
949 EXPECT_DOUBLE_EQ(5.0, a.Perform(make_tuple()));
950 EXPECT_EQ(EINVAL, errno);
951}
952
zhanyong.wan5b5d62f2009-03-11 23:37:56 +0000953#endif // _WIN32_WCE
954
zhanyong.wana18423e2009-07-22 23:58:19 +0000955// Tests ByRef().
956
957// Tests that ReferenceWrapper<T> is copyable.
958TEST(ByRefTest, IsCopyable) {
959 const std::string s1 = "Hi";
960 const std::string s2 = "Hello";
961
962 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper = ByRef(s1);
963 const std::string& r1 = ref_wrapper;
964 EXPECT_EQ(&s1, &r1);
965
966 // Assigns a new value to ref_wrapper.
967 ref_wrapper = ByRef(s2);
968 const std::string& r2 = ref_wrapper;
969 EXPECT_EQ(&s2, &r2);
970
971 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper1 = ByRef(s1);
972 // Copies ref_wrapper1 to ref_wrapper.
973 ref_wrapper = ref_wrapper1;
974 const std::string& r3 = ref_wrapper;
975 EXPECT_EQ(&s1, &r3);
976}
977
978// Tests using ByRef() on a const value.
979TEST(ByRefTest, ConstValue) {
980 const int n = 0;
981 // int& ref = ByRef(n); // This shouldn't compile - we have a
982 // negative compilation test to catch it.
983 const int& const_ref = ByRef(n);
984 EXPECT_EQ(&n, &const_ref);
985}
986
987// Tests using ByRef() on a non-const value.
988TEST(ByRefTest, NonConstValue) {
989 int n = 0;
990
991 // ByRef(n) can be used as either an int&,
992 int& ref = ByRef(n);
993 EXPECT_EQ(&n, &ref);
994
995 // or a const int&.
996 const int& const_ref = ByRef(n);
997 EXPECT_EQ(&n, &const_ref);
998}
999
1000// Tests explicitly specifying the type when using ByRef().
1001TEST(ByRefTest, ExplicitType) {
1002 int n = 0;
1003 const int& r1 = ByRef<const int>(n);
1004 EXPECT_EQ(&n, &r1);
1005
1006 // ByRef<char>(n); // This shouldn't compile - we have a negative
1007 // compilation test to catch it.
1008
1009 Derived d;
1010 Derived& r2 = ByRef<Derived>(d);
1011 EXPECT_EQ(&d, &r2);
1012
1013 const Derived& r3 = ByRef<const Derived>(d);
1014 EXPECT_EQ(&d, &r3);
1015
1016 Base& r4 = ByRef<Base>(d);
1017 EXPECT_EQ(&d, &r4);
1018
1019 const Base& r5 = ByRef<const Base>(d);
1020 EXPECT_EQ(&d, &r5);
1021
1022 // The following shouldn't compile - we have a negative compilation
1023 // test for it.
1024 //
1025 // Base b;
1026 // ByRef<Derived>(b);
1027}
1028
1029// Tests that Google Mock prints expression ByRef(x) as a reference to x.
1030TEST(ByRefTest, PrintsCorrectly) {
1031 int n = 42;
1032 ::std::stringstream expected, actual;
1033 testing::internal::UniversalPrinter<const int&>::Print(n, &expected);
1034 testing::internal::UniversalPrint(ByRef(n), &actual);
1035 EXPECT_EQ(expected.str(), actual.str());
1036}
1037
shiqiane35fdd92008-12-10 05:08:54 +00001038} // Unnamed namespace