blob: b7803fe92568a34d2b725ea0c83e0ae5df5bd3b0 [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
zhanyong.wan53e08c42010-09-14 05:38:21 +000036#include "gmock/gmock-actions.h"
shiqiane35fdd92008-12-10 05:08:54 +000037#include <algorithm>
38#include <iterator>
39#include <string>
zhanyong.wan53e08c42010-09-14 05:38:21 +000040#include "gmock/gmock.h"
41#include "gmock/internal/gmock-port.h"
42#include "gtest/gtest.h"
43#include "gtest/gtest-spi.h"
shiqiane35fdd92008-12-10 05:08:54 +000044
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;
zhanyong.wane3bd0982010-07-03 00:16:42 +000071using testing::ReturnRefOfCopy;
zhanyong.wan59214832010-10-05 05:58:51 +000072using testing::SetArgPointee;
shiqiane35fdd92008-12-10 05:08:54 +000073using testing::SetArgumentPointee;
zhanyong.wan5b5d62f2009-03-11 23:37:56 +000074
zhanyong.wanf7af24c2009-09-24 21:17:24 +000075#if !GTEST_OS_WINDOWS_MOBILE
shiqiane35fdd92008-12-10 05:08:54 +000076using testing::SetErrnoAndReturn;
zhanyong.wanf7af24c2009-09-24 21:17:24 +000077#endif
shiqiane35fdd92008-12-10 05:08:54 +000078
zhanyong.wan02f71062010-05-10 17:14:29 +000079#if GTEST_HAS_PROTOBUF_
shiqiane35fdd92008-12-10 05:08:54 +000080using testing::internal::TestMessage;
zhanyong.wan02f71062010-05-10 17:14:29 +000081#endif // GTEST_HAS_PROTOBUF_
shiqiane35fdd92008-12-10 05:08:54 +000082
83// Tests that BuiltInDefaultValue<T*>::Get() returns NULL.
84TEST(BuiltInDefaultValueTest, IsNullForPointerTypes) {
85 EXPECT_TRUE(BuiltInDefaultValue<int*>::Get() == NULL);
86 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Get() == NULL);
87 EXPECT_TRUE(BuiltInDefaultValue<void*>::Get() == NULL);
88}
89
zhanyong.wan5b95fa72009-01-27 22:28:45 +000090// Tests that BuiltInDefaultValue<T*>::Exists() return true.
91TEST(BuiltInDefaultValueTest, ExistsForPointerTypes) {
92 EXPECT_TRUE(BuiltInDefaultValue<int*>::Exists());
93 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Exists());
94 EXPECT_TRUE(BuiltInDefaultValue<void*>::Exists());
95}
96
shiqiane35fdd92008-12-10 05:08:54 +000097// Tests that BuiltInDefaultValue<T>::Get() returns 0 when T is a
98// built-in numeric type.
99TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) {
zhanyong.wan32de5f52009-12-23 00:13:23 +0000100 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned char>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000101 EXPECT_EQ(0, BuiltInDefaultValue<signed char>::Get());
102 EXPECT_EQ(0, BuiltInDefaultValue<char>::Get());
zhanyong.wan95b12332009-09-25 18:55:50 +0000103#if GMOCK_HAS_SIGNED_WCHAR_T_
zhanyong.wan32de5f52009-12-23 00:13:23 +0000104 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned wchar_t>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000105 EXPECT_EQ(0, BuiltInDefaultValue<signed wchar_t>::Get());
zhanyong.wan95b12332009-09-25 18:55:50 +0000106#endif
107#if GMOCK_WCHAR_T_IS_NATIVE_
shiqiane35fdd92008-12-10 05:08:54 +0000108 EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get());
zhanyong.wan95b12332009-09-25 18:55:50 +0000109#endif
zhanyong.wan32de5f52009-12-23 00:13:23 +0000110 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned short>::Get()); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +0000111 EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get()); // NOLINT
112 EXPECT_EQ(0, BuiltInDefaultValue<short>::Get()); // NOLINT
zhanyong.wan32de5f52009-12-23 00:13:23 +0000113 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned int>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000114 EXPECT_EQ(0, BuiltInDefaultValue<signed int>::Get());
115 EXPECT_EQ(0, BuiltInDefaultValue<int>::Get());
zhanyong.wan32de5f52009-12-23 00:13:23 +0000116 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned long>::Get()); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +0000117 EXPECT_EQ(0, BuiltInDefaultValue<signed long>::Get()); // NOLINT
118 EXPECT_EQ(0, BuiltInDefaultValue<long>::Get()); // NOLINT
zhanyong.wan32de5f52009-12-23 00:13:23 +0000119 EXPECT_EQ(0U, BuiltInDefaultValue<UInt64>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000120 EXPECT_EQ(0, BuiltInDefaultValue<Int64>::Get());
121 EXPECT_EQ(0, BuiltInDefaultValue<float>::Get());
122 EXPECT_EQ(0, BuiltInDefaultValue<double>::Get());
123}
124
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000125// Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
126// built-in numeric type.
127TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) {
128 EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists());
129 EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists());
130 EXPECT_TRUE(BuiltInDefaultValue<char>::Exists());
zhanyong.wan95b12332009-09-25 18:55:50 +0000131#if GMOCK_HAS_SIGNED_WCHAR_T_
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000132 EXPECT_TRUE(BuiltInDefaultValue<unsigned wchar_t>::Exists());
133 EXPECT_TRUE(BuiltInDefaultValue<signed wchar_t>::Exists());
zhanyong.wan95b12332009-09-25 18:55:50 +0000134#endif
135#if GMOCK_WCHAR_T_IS_NATIVE_
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000136 EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists());
zhanyong.wan95b12332009-09-25 18:55:50 +0000137#endif
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000138 EXPECT_TRUE(BuiltInDefaultValue<unsigned short>::Exists()); // NOLINT
139 EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists()); // NOLINT
140 EXPECT_TRUE(BuiltInDefaultValue<short>::Exists()); // NOLINT
141 EXPECT_TRUE(BuiltInDefaultValue<unsigned int>::Exists());
142 EXPECT_TRUE(BuiltInDefaultValue<signed int>::Exists());
143 EXPECT_TRUE(BuiltInDefaultValue<int>::Exists());
144 EXPECT_TRUE(BuiltInDefaultValue<unsigned long>::Exists()); // NOLINT
145 EXPECT_TRUE(BuiltInDefaultValue<signed long>::Exists()); // NOLINT
146 EXPECT_TRUE(BuiltInDefaultValue<long>::Exists()); // NOLINT
147 EXPECT_TRUE(BuiltInDefaultValue<UInt64>::Exists());
148 EXPECT_TRUE(BuiltInDefaultValue<Int64>::Exists());
149 EXPECT_TRUE(BuiltInDefaultValue<float>::Exists());
150 EXPECT_TRUE(BuiltInDefaultValue<double>::Exists());
151}
152
shiqiane35fdd92008-12-10 05:08:54 +0000153// Tests that BuiltInDefaultValue<bool>::Get() returns false.
154TEST(BuiltInDefaultValueTest, IsFalseForBool) {
155 EXPECT_FALSE(BuiltInDefaultValue<bool>::Get());
156}
157
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000158// Tests that BuiltInDefaultValue<bool>::Exists() returns true.
159TEST(BuiltInDefaultValueTest, BoolExists) {
160 EXPECT_TRUE(BuiltInDefaultValue<bool>::Exists());
161}
162
shiqiane35fdd92008-12-10 05:08:54 +0000163// Tests that BuiltInDefaultValue<T>::Get() returns "" when T is a
164// string type.
165TEST(BuiltInDefaultValueTest, IsEmptyStringForString) {
166#if GTEST_HAS_GLOBAL_STRING
167 EXPECT_EQ("", BuiltInDefaultValue< ::string>::Get());
168#endif // GTEST_HAS_GLOBAL_STRING
169
shiqiane35fdd92008-12-10 05:08:54 +0000170 EXPECT_EQ("", BuiltInDefaultValue< ::std::string>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000171}
172
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000173// Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
174// string type.
175TEST(BuiltInDefaultValueTest, ExistsForString) {
176#if GTEST_HAS_GLOBAL_STRING
177 EXPECT_TRUE(BuiltInDefaultValue< ::string>::Exists());
178#endif // GTEST_HAS_GLOBAL_STRING
179
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000180 EXPECT_TRUE(BuiltInDefaultValue< ::std::string>::Exists());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000181}
182
shiqiane35fdd92008-12-10 05:08:54 +0000183// Tests that BuiltInDefaultValue<const T>::Get() returns the same
184// value as BuiltInDefaultValue<T>::Get() does.
185TEST(BuiltInDefaultValueTest, WorksForConstTypes) {
186 EXPECT_EQ("", BuiltInDefaultValue<const std::string>::Get());
187 EXPECT_EQ(0, BuiltInDefaultValue<const int>::Get());
188 EXPECT_TRUE(BuiltInDefaultValue<char* const>::Get() == NULL);
189 EXPECT_FALSE(BuiltInDefaultValue<const bool>::Get());
190}
191
192// Tests that BuiltInDefaultValue<T>::Get() aborts the program with
193// the correct error message when T is a user-defined type.
194struct UserType {
195 UserType() : value(0) {}
196
197 int value;
198};
199
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000200TEST(BuiltInDefaultValueTest, UserTypeHasNoDefault) {
201 EXPECT_FALSE(BuiltInDefaultValue<UserType>::Exists());
202}
203
shiqiane35fdd92008-12-10 05:08:54 +0000204// Tests that BuiltInDefaultValue<T&>::Get() aborts the program.
205TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000206 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000207 BuiltInDefaultValue<int&>::Get();
208 }, "");
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000209 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000210 BuiltInDefaultValue<const char&>::Get();
211 }, "");
212}
213
214TEST(BuiltInDefaultValueDeathTest, IsUndefinedForUserTypes) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000215 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000216 BuiltInDefaultValue<UserType>::Get();
217 }, "");
218}
219
shiqiane35fdd92008-12-10 05:08:54 +0000220// Tests that DefaultValue<T>::IsSet() is false initially.
221TEST(DefaultValueTest, IsInitiallyUnset) {
222 EXPECT_FALSE(DefaultValue<int>::IsSet());
223 EXPECT_FALSE(DefaultValue<const UserType>::IsSet());
224}
225
226// Tests that DefaultValue<T> can be set and then unset.
227TEST(DefaultValueTest, CanBeSetAndUnset) {
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000228 EXPECT_TRUE(DefaultValue<int>::Exists());
229 EXPECT_FALSE(DefaultValue<const UserType>::Exists());
230
shiqiane35fdd92008-12-10 05:08:54 +0000231 DefaultValue<int>::Set(1);
232 DefaultValue<const UserType>::Set(UserType());
233
234 EXPECT_EQ(1, DefaultValue<int>::Get());
235 EXPECT_EQ(0, DefaultValue<const UserType>::Get().value);
236
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000237 EXPECT_TRUE(DefaultValue<int>::Exists());
238 EXPECT_TRUE(DefaultValue<const UserType>::Exists());
239
shiqiane35fdd92008-12-10 05:08:54 +0000240 DefaultValue<int>::Clear();
241 DefaultValue<const UserType>::Clear();
242
243 EXPECT_FALSE(DefaultValue<int>::IsSet());
244 EXPECT_FALSE(DefaultValue<const UserType>::IsSet());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000245
246 EXPECT_TRUE(DefaultValue<int>::Exists());
247 EXPECT_FALSE(DefaultValue<const UserType>::Exists());
shiqiane35fdd92008-12-10 05:08:54 +0000248}
249
250// Tests that DefaultValue<T>::Get() returns the
251// BuiltInDefaultValue<T>::Get() when DefaultValue<T>::IsSet() is
252// false.
253TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
254 EXPECT_FALSE(DefaultValue<int>::IsSet());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000255 EXPECT_TRUE(DefaultValue<int>::Exists());
shiqiane35fdd92008-12-10 05:08:54 +0000256 EXPECT_FALSE(DefaultValue<UserType>::IsSet());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000257 EXPECT_FALSE(DefaultValue<UserType>::Exists());
shiqiane35fdd92008-12-10 05:08:54 +0000258
259 EXPECT_EQ(0, DefaultValue<int>::Get());
260
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000261 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000262 DefaultValue<UserType>::Get();
263 }, "");
shiqiane35fdd92008-12-10 05:08:54 +0000264}
265
266// Tests that DefaultValue<void>::Get() returns void.
267TEST(DefaultValueTest, GetWorksForVoid) {
268 return DefaultValue<void>::Get();
269}
270
271// Tests using DefaultValue with a reference type.
272
273// Tests that DefaultValue<T&>::IsSet() is false initially.
274TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) {
275 EXPECT_FALSE(DefaultValue<int&>::IsSet());
276 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
277}
278
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000279// Tests that DefaultValue<T&>::Exists is false initiallly.
280TEST(DefaultValueOfReferenceTest, IsInitiallyNotExisting) {
281 EXPECT_FALSE(DefaultValue<int&>::Exists());
282 EXPECT_FALSE(DefaultValue<UserType&>::Exists());
283}
284
shiqiane35fdd92008-12-10 05:08:54 +0000285// Tests that DefaultValue<T&> can be set and then unset.
286TEST(DefaultValueOfReferenceTest, CanBeSetAndUnset) {
287 int n = 1;
288 DefaultValue<const int&>::Set(n);
289 UserType u;
290 DefaultValue<UserType&>::Set(u);
291
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000292 EXPECT_TRUE(DefaultValue<const int&>::Exists());
293 EXPECT_TRUE(DefaultValue<UserType&>::Exists());
294
shiqiane35fdd92008-12-10 05:08:54 +0000295 EXPECT_EQ(&n, &(DefaultValue<const int&>::Get()));
296 EXPECT_EQ(&u, &(DefaultValue<UserType&>::Get()));
297
298 DefaultValue<const int&>::Clear();
299 DefaultValue<UserType&>::Clear();
300
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000301 EXPECT_FALSE(DefaultValue<const int&>::Exists());
302 EXPECT_FALSE(DefaultValue<UserType&>::Exists());
303
shiqiane35fdd92008-12-10 05:08:54 +0000304 EXPECT_FALSE(DefaultValue<const int&>::IsSet());
305 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
306}
307
308// Tests that DefaultValue<T&>::Get() returns the
309// BuiltInDefaultValue<T&>::Get() when DefaultValue<T&>::IsSet() is
310// false.
311TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
312 EXPECT_FALSE(DefaultValue<int&>::IsSet());
313 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
314
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000315 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000316 DefaultValue<int&>::Get();
317 }, "");
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000318 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000319 DefaultValue<UserType>::Get();
320 }, "");
shiqiane35fdd92008-12-10 05:08:54 +0000321}
322
323// Tests that ActionInterface can be implemented by defining the
324// Perform method.
325
326typedef int MyFunction(bool, int);
327
328class MyActionImpl : public ActionInterface<MyFunction> {
329 public:
330 virtual int Perform(const tuple<bool, int>& args) {
331 return get<0>(args) ? get<1>(args) : 0;
332 }
333};
334
335TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) {
336 MyActionImpl my_action_impl;
zhanyong.waned6c9272011-02-23 19:39:27 +0000337 (void)my_action_impl;
shiqiane35fdd92008-12-10 05:08:54 +0000338}
339
340TEST(ActionInterfaceTest, MakeAction) {
341 Action<MyFunction> action = MakeAction(new MyActionImpl);
342
343 // When exercising the Perform() method of Action<F>, we must pass
344 // it a tuple whose size and type are compatible with F's argument
345 // types. For example, if F is int(), then Perform() takes a
346 // 0-tuple; if F is void(bool, int), then Perform() takes a
347 // tuple<bool, int>, and so on.
348 EXPECT_EQ(5, action.Perform(make_tuple(true, 5)));
349}
350
351// Tests that Action<F> can be contructed from a pointer to
352// ActionInterface<F>.
353TEST(ActionTest, CanBeConstructedFromActionInterface) {
354 Action<MyFunction> action(new MyActionImpl);
355}
356
357// Tests that Action<F> delegates actual work to ActionInterface<F>.
358TEST(ActionTest, DelegatesWorkToActionInterface) {
359 const Action<MyFunction> action(new MyActionImpl);
360
361 EXPECT_EQ(5, action.Perform(make_tuple(true, 5)));
362 EXPECT_EQ(0, action.Perform(make_tuple(false, 1)));
363}
364
365// Tests that Action<F> can be copied.
366TEST(ActionTest, IsCopyable) {
367 Action<MyFunction> a1(new MyActionImpl);
368 Action<MyFunction> a2(a1); // Tests the copy constructor.
369
370 // a1 should continue to work after being copied from.
371 EXPECT_EQ(5, a1.Perform(make_tuple(true, 5)));
372 EXPECT_EQ(0, a1.Perform(make_tuple(false, 1)));
373
374 // a2 should work like the action it was copied from.
375 EXPECT_EQ(5, a2.Perform(make_tuple(true, 5)));
376 EXPECT_EQ(0, a2.Perform(make_tuple(false, 1)));
377
378 a2 = a1; // Tests the assignment operator.
379
380 // a1 should continue to work after being copied from.
381 EXPECT_EQ(5, a1.Perform(make_tuple(true, 5)));
382 EXPECT_EQ(0, a1.Perform(make_tuple(false, 1)));
383
384 // a2 should work like the action it was copied from.
385 EXPECT_EQ(5, a2.Perform(make_tuple(true, 5)));
386 EXPECT_EQ(0, a2.Perform(make_tuple(false, 1)));
387}
388
389// Tests that an Action<From> object can be converted to a
390// compatible Action<To> object.
391
392class IsNotZero : public ActionInterface<bool(int)> { // NOLINT
393 public:
394 virtual bool Perform(const tuple<int>& arg) {
395 return get<0>(arg) != 0;
396 }
397};
398
zhanyong.wan95b12332009-09-25 18:55:50 +0000399#if !GTEST_OS_SYMBIAN
400// Compiling this test on Nokia's Symbian compiler fails with:
401// 'Result' is not a member of class 'testing::internal::Function<int>'
402// (point of instantiation: '@unnamed@gmock_actions_test_cc@::
403// ActionTest_CanBeConvertedToOtherActionType_Test::TestBody()')
404// with no obvious fix.
shiqiane35fdd92008-12-10 05:08:54 +0000405TEST(ActionTest, CanBeConvertedToOtherActionType) {
406 const Action<bool(int)> a1(new IsNotZero); // NOLINT
407 const Action<int(char)> a2 = Action<int(char)>(a1); // NOLINT
408 EXPECT_EQ(1, a2.Perform(make_tuple('a')));
409 EXPECT_EQ(0, a2.Perform(make_tuple('\0')));
410}
zhanyong.wan95b12332009-09-25 18:55:50 +0000411#endif // !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +0000412
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
vladloseva070cbd2009-11-18 00:09:28 +0000515// Tests that the type of the value passed into Return is converted into T
516// when the action is cast to Action<T(...)> rather than when the action is
517// performed. See comments on testing::internal::ReturnAction in
518// gmock-actions.h for more information.
519class FromType {
520 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000521 FromType(bool* is_converted) : converted_(is_converted) {}
vladloseva070cbd2009-11-18 00:09:28 +0000522 bool* converted() const { return converted_; }
523
524 private:
525 bool* const converted_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000526
527 GTEST_DISALLOW_ASSIGN_(FromType);
vladloseva070cbd2009-11-18 00:09:28 +0000528};
529
530class ToType {
531 public:
532 ToType(const FromType& x) { *x.converted() = true; }
533};
534
535TEST(ReturnTest, ConvertsArgumentWhenConverted) {
536 bool converted = false;
537 FromType x(&converted);
538 Action<ToType()> action(Return(x));
539 EXPECT_TRUE(converted) << "Return must convert its argument in its own "
540 << "conversion operator.";
541 converted = false;
542 action.Perform(tuple<>());
543 EXPECT_FALSE(converted) << "Action must NOT convert its argument "
544 << "when performed." ;
545}
546
vladloseva070cbd2009-11-18 00:09:28 +0000547class DestinationType {};
548
549class SourceType {
550 public:
551 // Note: a non-const typecast operator.
552 operator DestinationType() { return DestinationType(); }
553};
554
555TEST(ReturnTest, CanConvertArgumentUsingNonConstTypeCastOperator) {
556 SourceType s;
557 Action<DestinationType()> action(Return(s));
558}
vladloseva070cbd2009-11-18 00:09:28 +0000559
shiqiane35fdd92008-12-10 05:08:54 +0000560// Tests that ReturnNull() returns NULL in a pointer-returning function.
561TEST(ReturnNullTest, WorksInPointerReturningFunction) {
562 const Action<int*()> a1 = ReturnNull();
563 EXPECT_TRUE(a1.Perform(make_tuple()) == NULL);
564
565 const Action<const char*(bool)> a2 = ReturnNull(); // NOLINT
566 EXPECT_TRUE(a2.Perform(make_tuple(true)) == NULL);
567}
568
569// Tests that ReturnRef(v) works for reference types.
570TEST(ReturnRefTest, WorksForReference) {
571 const int n = 0;
572 const Action<const int&(bool)> ret = ReturnRef(n); // NOLINT
573
574 EXPECT_EQ(&n, &ret.Perform(make_tuple(true)));
575}
576
577// Tests that ReturnRef(v) is covariant.
578TEST(ReturnRefTest, IsCovariant) {
579 Base base;
580 Derived derived;
581 Action<Base&()> a = ReturnRef(base);
582 EXPECT_EQ(&base, &a.Perform(make_tuple()));
583
584 a = ReturnRef(derived);
585 EXPECT_EQ(&derived, &a.Perform(make_tuple()));
586}
587
zhanyong.wane3bd0982010-07-03 00:16:42 +0000588// Tests that ReturnRefOfCopy(v) works for reference types.
589TEST(ReturnRefOfCopyTest, WorksForReference) {
590 int n = 42;
591 const Action<const int&()> ret = ReturnRefOfCopy(n);
592
593 EXPECT_NE(&n, &ret.Perform(make_tuple()));
594 EXPECT_EQ(42, ret.Perform(make_tuple()));
595
596 n = 43;
597 EXPECT_NE(&n, &ret.Perform(make_tuple()));
598 EXPECT_EQ(42, ret.Perform(make_tuple()));
599}
600
601// Tests that ReturnRefOfCopy(v) is covariant.
602TEST(ReturnRefOfCopyTest, IsCovariant) {
603 Base base;
604 Derived derived;
605 Action<Base&()> a = ReturnRefOfCopy(base);
606 EXPECT_NE(&base, &a.Perform(make_tuple()));
607
608 a = ReturnRefOfCopy(derived);
609 EXPECT_NE(&derived, &a.Perform(make_tuple()));
610}
611
shiqiane35fdd92008-12-10 05:08:54 +0000612// Tests that DoDefault() does the default action for the mock method.
613
614class MyClass {};
615
616class MockClass {
617 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000618 MockClass() {}
619
shiqiane35fdd92008-12-10 05:08:54 +0000620 MOCK_METHOD1(IntFunc, int(bool flag)); // NOLINT
621 MOCK_METHOD0(Foo, MyClass());
zhanyong.wan32de5f52009-12-23 00:13:23 +0000622
623 private:
624 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockClass);
shiqiane35fdd92008-12-10 05:08:54 +0000625};
626
627// Tests that DoDefault() returns the built-in default value for the
628// return type by default.
629TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) {
630 MockClass mock;
631 EXPECT_CALL(mock, IntFunc(_))
632 .WillOnce(DoDefault());
633 EXPECT_EQ(0, mock.IntFunc(true));
634}
635
shiqiane35fdd92008-12-10 05:08:54 +0000636// Tests that DoDefault() aborts the process when there is no built-in
637// default value for the return type.
638TEST(DoDefaultDeathTest, DiesForUnknowType) {
639 MockClass mock;
640 EXPECT_CALL(mock, Foo())
641 .WillRepeatedly(DoDefault());
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000642 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000643 mock.Foo();
644 }, "");
645}
646
647// Tests that using DoDefault() inside a composite action leads to a
648// run-time error.
649
zhanyong.wan32de5f52009-12-23 00:13:23 +0000650void VoidFunc(bool /* flag */) {}
shiqiane35fdd92008-12-10 05:08:54 +0000651
652TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
653 MockClass mock;
654 EXPECT_CALL(mock, IntFunc(_))
655 .WillRepeatedly(DoAll(Invoke(VoidFunc),
656 DoDefault()));
657
658 // Ideally we should verify the error message as well. Sadly,
659 // EXPECT_DEATH() can only capture stderr, while Google Mock's
660 // errors are printed on stdout. Therefore we have to settle for
661 // not verifying the message.
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000662 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000663 mock.IntFunc(true);
664 }, "");
665}
666
shiqiane35fdd92008-12-10 05:08:54 +0000667// Tests that DoDefault() returns the default value set by
668// DefaultValue<T>::Set() when it's not overriden by an ON_CALL().
669TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) {
670 DefaultValue<int>::Set(1);
671 MockClass mock;
672 EXPECT_CALL(mock, IntFunc(_))
673 .WillOnce(DoDefault());
674 EXPECT_EQ(1, mock.IntFunc(false));
675 DefaultValue<int>::Clear();
676}
677
678// Tests that DoDefault() does the action specified by ON_CALL().
679TEST(DoDefaultTest, DoesWhatOnCallSpecifies) {
680 MockClass mock;
681 ON_CALL(mock, IntFunc(_))
682 .WillByDefault(Return(2));
683 EXPECT_CALL(mock, IntFunc(_))
684 .WillOnce(DoDefault());
685 EXPECT_EQ(2, mock.IntFunc(false));
686}
687
688// Tests that using DoDefault() in ON_CALL() leads to a run-time failure.
689TEST(DoDefaultTest, CannotBeUsedInOnCall) {
690 MockClass mock;
691 EXPECT_NONFATAL_FAILURE({ // NOLINT
692 ON_CALL(mock, IntFunc(_))
693 .WillByDefault(DoDefault());
694 }, "DoDefault() cannot be used in ON_CALL()");
695}
696
zhanyong.wan59214832010-10-05 05:58:51 +0000697// Tests that SetArgPointee<N>(v) sets the variable pointed to by
698// the N-th (0-based) argument to v.
699TEST(SetArgPointeeTest, SetsTheNthPointee) {
700 typedef void MyFunction(bool, int*, char*);
701 Action<MyFunction> a = SetArgPointee<1>(2);
702
703 int n = 0;
704 char ch = '\0';
705 a.Perform(make_tuple(true, &n, &ch));
706 EXPECT_EQ(2, n);
707 EXPECT_EQ('\0', ch);
708
709 a = SetArgPointee<2>('a');
710 n = 0;
711 ch = '\0';
712 a.Perform(make_tuple(true, &n, &ch));
713 EXPECT_EQ(0, n);
714 EXPECT_EQ('a', ch);
715}
716
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000717#if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN)
zhanyong.wana684b5a2010-12-02 23:30:50 +0000718// Tests that SetArgPointee<N>() accepts a string literal.
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000719// GCC prior to v4.0 and the Symbian compiler do not support this.
zhanyong.wana684b5a2010-12-02 23:30:50 +0000720TEST(SetArgPointeeTest, AcceptsStringLiteral) {
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000721 typedef void MyFunction(std::string*, const char**);
722 Action<MyFunction> a = SetArgPointee<0>("hi");
zhanyong.wana684b5a2010-12-02 23:30:50 +0000723 std::string str;
724 const char* ptr = NULL;
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000725 a.Perform(make_tuple(&str, &ptr));
zhanyong.wana684b5a2010-12-02 23:30:50 +0000726 EXPECT_EQ("hi", str);
727 EXPECT_TRUE(ptr == NULL);
728
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000729 a = SetArgPointee<1>("world");
zhanyong.wana684b5a2010-12-02 23:30:50 +0000730 str = "";
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000731 a.Perform(make_tuple(&str, &ptr));
zhanyong.wana684b5a2010-12-02 23:30:50 +0000732 EXPECT_EQ("", str);
733 EXPECT_STREQ("world", ptr);
734}
735
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000736TEST(SetArgPointeeTest, AcceptsWideStringLiteral) {
737 typedef void MyFunction(const wchar_t**);
738 Action<MyFunction> a = SetArgPointee<0>(L"world");
739 const wchar_t* ptr = NULL;
740 a.Perform(make_tuple(&ptr));
741 EXPECT_STREQ(L"world", ptr);
742
743# if GTEST_HAS_STD_WSTRING
744
745 typedef void MyStringFunction(std::wstring*);
746 Action<MyStringFunction> a2 = SetArgPointee<0>(L"world");
747 std::wstring str = L"";
748 a2.Perform(make_tuple(&str));
749 EXPECT_EQ(L"world", str);
750
751# endif
752}
753#endif
754
zhanyong.wana684b5a2010-12-02 23:30:50 +0000755// Tests that SetArgPointee<N>() accepts a char pointer.
756TEST(SetArgPointeeTest, AcceptsCharPointer) {
757 typedef void MyFunction(bool, std::string*, const char**);
758 const char* const hi = "hi";
759 Action<MyFunction> a = SetArgPointee<1>(hi);
760 std::string str;
761 const char* ptr = NULL;
762 a.Perform(make_tuple(true, &str, &ptr));
763 EXPECT_EQ("hi", str);
764 EXPECT_TRUE(ptr == NULL);
765
766 char world_array[] = "world";
767 char* const world = world_array;
768 a = SetArgPointee<2>(world);
769 str = "";
770 a.Perform(make_tuple(true, &str, &ptr));
771 EXPECT_EQ("", str);
772 EXPECT_EQ(world, ptr);
773}
774
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000775TEST(SetArgPointeeTest, AcceptsWideCharPointer) {
776 typedef void MyFunction(bool, const wchar_t**);
777 const wchar_t* const hi = L"hi";
778 Action<MyFunction> a = SetArgPointee<1>(hi);
779 const wchar_t* ptr = NULL;
780 a.Perform(make_tuple(true, &ptr));
781 EXPECT_EQ(hi, ptr);
782
783# if GTEST_HAS_STD_WSTRING
784
785 typedef void MyStringFunction(bool, std::wstring*);
786 wchar_t world_array[] = L"world";
787 wchar_t* const world = world_array;
788 Action<MyStringFunction> a2 = SetArgPointee<1>(world);
789 std::wstring str;
790 a2.Perform(make_tuple(true, &str));
791 EXPECT_EQ(world_array, str);
792# endif
793}
794
zhanyong.wan59214832010-10-05 05:58:51 +0000795#if GTEST_HAS_PROTOBUF_
796
797// Tests that SetArgPointee<N>(proto_buffer) sets the v1 protobuf
798// variable pointed to by the N-th (0-based) argument to proto_buffer.
799TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
800 TestMessage* const msg = new TestMessage;
801 msg->set_member("yes");
802 TestMessage orig_msg;
803 orig_msg.CopyFrom(*msg);
804
805 Action<void(bool, TestMessage*)> a = SetArgPointee<1>(*msg);
806 // SetArgPointee<N>(proto_buffer) makes a copy of proto_buffer
807 // s.t. the action works even when the original proto_buffer has
808 // died. We ensure this behavior by deleting msg before using the
809 // action.
810 delete msg;
811
812 TestMessage dest;
813 EXPECT_FALSE(orig_msg.Equals(dest));
814 a.Perform(make_tuple(true, &dest));
815 EXPECT_TRUE(orig_msg.Equals(dest));
816}
817
818// Tests that SetArgPointee<N>(proto_buffer) sets the
819// ::ProtocolMessage variable pointed to by the N-th (0-based)
820// argument to proto_buffer.
821TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
822 TestMessage* const msg = new TestMessage;
823 msg->set_member("yes");
824 TestMessage orig_msg;
825 orig_msg.CopyFrom(*msg);
826
827 Action<void(bool, ::ProtocolMessage*)> a = SetArgPointee<1>(*msg);
828 // SetArgPointee<N>(proto_buffer) makes a copy of proto_buffer
829 // s.t. the action works even when the original proto_buffer has
830 // died. We ensure this behavior by deleting msg before using the
831 // action.
832 delete msg;
833
834 TestMessage dest;
835 ::ProtocolMessage* const dest_base = &dest;
836 EXPECT_FALSE(orig_msg.Equals(dest));
837 a.Perform(make_tuple(true, dest_base));
838 EXPECT_TRUE(orig_msg.Equals(dest));
839}
840
841// Tests that SetArgPointee<N>(proto2_buffer) sets the v2
842// protobuf variable pointed to by the N-th (0-based) argument to
843// proto2_buffer.
844TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
845 using testing::internal::FooMessage;
846 FooMessage* const msg = new FooMessage;
847 msg->set_int_field(2);
848 msg->set_string_field("hi");
849 FooMessage orig_msg;
850 orig_msg.CopyFrom(*msg);
851
852 Action<void(bool, FooMessage*)> a = SetArgPointee<1>(*msg);
853 // SetArgPointee<N>(proto2_buffer) makes a copy of
854 // proto2_buffer s.t. the action works even when the original
855 // proto2_buffer has died. We ensure this behavior by deleting msg
856 // before using the action.
857 delete msg;
858
859 FooMessage dest;
860 dest.set_int_field(0);
861 a.Perform(make_tuple(true, &dest));
862 EXPECT_EQ(2, dest.int_field());
863 EXPECT_EQ("hi", dest.string_field());
864}
865
866// Tests that SetArgPointee<N>(proto2_buffer) sets the
867// proto2::Message variable pointed to by the N-th (0-based) argument
868// to proto2_buffer.
869TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
870 using testing::internal::FooMessage;
871 FooMessage* const msg = new FooMessage;
872 msg->set_int_field(2);
873 msg->set_string_field("hi");
874 FooMessage orig_msg;
875 orig_msg.CopyFrom(*msg);
876
877 Action<void(bool, ::proto2::Message*)> a = SetArgPointee<1>(*msg);
878 // SetArgPointee<N>(proto2_buffer) makes a copy of
879 // proto2_buffer s.t. the action works even when the original
880 // proto2_buffer has died. We ensure this behavior by deleting msg
881 // before using the action.
882 delete msg;
883
884 FooMessage dest;
885 dest.set_int_field(0);
886 ::proto2::Message* const dest_base = &dest;
887 a.Perform(make_tuple(true, dest_base));
888 EXPECT_EQ(2, dest.int_field());
889 EXPECT_EQ("hi", dest.string_field());
890}
891
892#endif // GTEST_HAS_PROTOBUF_
893
shiqiane35fdd92008-12-10 05:08:54 +0000894// Tests that SetArgumentPointee<N>(v) sets the variable pointed to by
895// the N-th (0-based) argument to v.
896TEST(SetArgumentPointeeTest, SetsTheNthPointee) {
897 typedef void MyFunction(bool, int*, char*);
898 Action<MyFunction> a = SetArgumentPointee<1>(2);
899
900 int n = 0;
901 char ch = '\0';
902 a.Perform(make_tuple(true, &n, &ch));
903 EXPECT_EQ(2, n);
904 EXPECT_EQ('\0', ch);
905
906 a = SetArgumentPointee<2>('a');
907 n = 0;
908 ch = '\0';
909 a.Perform(make_tuple(true, &n, &ch));
910 EXPECT_EQ(0, n);
911 EXPECT_EQ('a', ch);
912}
913
zhanyong.wan02f71062010-05-10 17:14:29 +0000914#if GTEST_HAS_PROTOBUF_
shiqiane35fdd92008-12-10 05:08:54 +0000915
zhanyong.wanc6a41232009-05-13 23:38:40 +0000916// Tests that SetArgumentPointee<N>(proto_buffer) sets the v1 protobuf
917// variable pointed to by the N-th (0-based) argument to proto_buffer.
shiqiane35fdd92008-12-10 05:08:54 +0000918TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
shiqiane35fdd92008-12-10 05:08:54 +0000919 TestMessage* const msg = new TestMessage;
920 msg->set_member("yes");
921 TestMessage orig_msg;
922 orig_msg.CopyFrom(*msg);
923
zhanyong.wanc6a41232009-05-13 23:38:40 +0000924 Action<void(bool, TestMessage*)> a = SetArgumentPointee<1>(*msg);
shiqiane35fdd92008-12-10 05:08:54 +0000925 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
926 // s.t. the action works even when the original proto_buffer has
927 // died. We ensure this behavior by deleting msg before using the
928 // action.
929 delete msg;
930
931 TestMessage dest;
932 EXPECT_FALSE(orig_msg.Equals(dest));
933 a.Perform(make_tuple(true, &dest));
934 EXPECT_TRUE(orig_msg.Equals(dest));
935}
936
zhanyong.wanc6a41232009-05-13 23:38:40 +0000937// Tests that SetArgumentPointee<N>(proto_buffer) sets the
938// ::ProtocolMessage variable pointed to by the N-th (0-based)
939// argument to proto_buffer.
940TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
941 TestMessage* const msg = new TestMessage;
942 msg->set_member("yes");
943 TestMessage orig_msg;
944 orig_msg.CopyFrom(*msg);
945
946 Action<void(bool, ::ProtocolMessage*)> a = SetArgumentPointee<1>(*msg);
947 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
948 // s.t. the action works even when the original proto_buffer has
949 // died. We ensure this behavior by deleting msg before using the
950 // action.
951 delete msg;
952
953 TestMessage dest;
954 ::ProtocolMessage* const dest_base = &dest;
955 EXPECT_FALSE(orig_msg.Equals(dest));
956 a.Perform(make_tuple(true, dest_base));
957 EXPECT_TRUE(orig_msg.Equals(dest));
958}
959
960// Tests that SetArgumentPointee<N>(proto2_buffer) sets the v2
961// protobuf variable pointed to by the N-th (0-based) argument to
962// proto2_buffer.
shiqiane35fdd92008-12-10 05:08:54 +0000963TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
964 using testing::internal::FooMessage;
shiqiane35fdd92008-12-10 05:08:54 +0000965 FooMessage* const msg = new FooMessage;
966 msg->set_int_field(2);
967 msg->set_string_field("hi");
968 FooMessage orig_msg;
969 orig_msg.CopyFrom(*msg);
970
zhanyong.wanc6a41232009-05-13 23:38:40 +0000971 Action<void(bool, FooMessage*)> a = SetArgumentPointee<1>(*msg);
shiqiane35fdd92008-12-10 05:08:54 +0000972 // SetArgumentPointee<N>(proto2_buffer) makes a copy of
973 // proto2_buffer s.t. the action works even when the original
974 // proto2_buffer has died. We ensure this behavior by deleting msg
975 // before using the action.
976 delete msg;
977
978 FooMessage dest;
979 dest.set_int_field(0);
980 a.Perform(make_tuple(true, &dest));
981 EXPECT_EQ(2, dest.int_field());
982 EXPECT_EQ("hi", dest.string_field());
983}
984
zhanyong.wanc6a41232009-05-13 23:38:40 +0000985// Tests that SetArgumentPointee<N>(proto2_buffer) sets the
986// proto2::Message variable pointed to by the N-th (0-based) argument
987// to proto2_buffer.
988TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
989 using testing::internal::FooMessage;
990 FooMessage* const msg = new FooMessage;
991 msg->set_int_field(2);
992 msg->set_string_field("hi");
993 FooMessage orig_msg;
994 orig_msg.CopyFrom(*msg);
995
996 Action<void(bool, ::proto2::Message*)> a = SetArgumentPointee<1>(*msg);
997 // SetArgumentPointee<N>(proto2_buffer) makes a copy of
998 // proto2_buffer s.t. the action works even when the original
999 // proto2_buffer has died. We ensure this behavior by deleting msg
1000 // before using the action.
1001 delete msg;
1002
1003 FooMessage dest;
1004 dest.set_int_field(0);
1005 ::proto2::Message* const dest_base = &dest;
1006 a.Perform(make_tuple(true, dest_base));
1007 EXPECT_EQ(2, dest.int_field());
1008 EXPECT_EQ("hi", dest.string_field());
1009}
1010
zhanyong.wan02f71062010-05-10 17:14:29 +00001011#endif // GTEST_HAS_PROTOBUF_
shiqiane35fdd92008-12-10 05:08:54 +00001012
shiqiane35fdd92008-12-10 05:08:54 +00001013// Sample functions and functors for testing Invoke() and etc.
1014int Nullary() { return 1; }
1015
1016class NullaryFunctor {
1017 public:
1018 int operator()() { return 2; }
1019};
1020
1021bool g_done = false;
1022void VoidNullary() { g_done = true; }
1023
1024class VoidNullaryFunctor {
1025 public:
1026 void operator()() { g_done = true; }
1027};
1028
1029bool Unary(int x) { return x < 0; }
1030
1031const char* Plus1(const char* s) { return s + 1; }
1032
zhanyong.wan32de5f52009-12-23 00:13:23 +00001033void VoidUnary(int /* n */) { g_done = true; }
shiqiane35fdd92008-12-10 05:08:54 +00001034
1035bool ByConstRef(const std::string& s) { return s == "Hi"; }
1036
1037const double g_double = 0;
1038bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
1039
1040std::string ByNonConstRef(std::string& s) { return s += "+"; } // NOLINT
1041
1042struct UnaryFunctor {
1043 int operator()(bool x) { return x ? 1 : -1; }
1044};
1045
1046const char* Binary(const char* input, short n) { return input + n; } // NOLINT
1047
1048void VoidBinary(int, char) { g_done = true; }
1049
1050int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT
1051
1052void VoidTernary(int, char, bool) { g_done = true; }
1053
1054int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
1055
1056void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; }
1057
1058int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
1059
1060struct SumOf5Functor {
1061 int operator()(int a, int b, int c, int d, int e) {
1062 return a + b + c + d + e;
1063 }
1064};
1065
1066int SumOf6(int a, int b, int c, int d, int e, int f) {
1067 return a + b + c + d + e + f;
1068}
1069
1070struct SumOf6Functor {
1071 int operator()(int a, int b, int c, int d, int e, int f) {
1072 return a + b + c + d + e + f;
1073 }
1074};
1075
1076class Foo {
1077 public:
1078 Foo() : value_(123) {}
1079
1080 int Nullary() const { return value_; }
1081 short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT
1082 std::string Binary(const std::string& str, char c) const { return str + c; }
1083 int Ternary(int x, bool y, char z) { return value_ + x + y*z; }
1084 int SumOf4(int a, int b, int c, int d) const {
1085 return a + b + c + d + value_;
1086 }
1087 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
1088 int SumOf6(int a, int b, int c, int d, int e, int f) {
1089 return a + b + c + d + e + f;
1090 }
1091 private:
1092 int value_;
1093};
1094
1095// Tests InvokeWithoutArgs(function).
1096TEST(InvokeWithoutArgsTest, Function) {
1097 // As an action that takes one argument.
1098 Action<int(int)> a = InvokeWithoutArgs(Nullary); // NOLINT
1099 EXPECT_EQ(1, a.Perform(make_tuple(2)));
1100
1101 // As an action that takes two arguments.
zhanyong.wan32de5f52009-12-23 00:13:23 +00001102 Action<int(int, double)> a2 = InvokeWithoutArgs(Nullary); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00001103 EXPECT_EQ(1, a2.Perform(make_tuple(2, 3.5)));
1104
1105 // As an action that returns void.
1106 Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary); // NOLINT
1107 g_done = false;
1108 a3.Perform(make_tuple(1));
1109 EXPECT_TRUE(g_done);
1110}
1111
1112// Tests InvokeWithoutArgs(functor).
1113TEST(InvokeWithoutArgsTest, Functor) {
1114 // As an action that takes no argument.
1115 Action<int()> a = InvokeWithoutArgs(NullaryFunctor()); // NOLINT
1116 EXPECT_EQ(2, a.Perform(make_tuple()));
1117
1118 // As an action that takes three arguments.
zhanyong.wan32de5f52009-12-23 00:13:23 +00001119 Action<int(int, double, char)> a2 = // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00001120 InvokeWithoutArgs(NullaryFunctor());
1121 EXPECT_EQ(2, a2.Perform(make_tuple(3, 3.5, 'a')));
1122
1123 // As an action that returns void.
1124 Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor());
1125 g_done = false;
1126 a3.Perform(make_tuple());
1127 EXPECT_TRUE(g_done);
1128}
1129
1130// Tests InvokeWithoutArgs(obj_ptr, method).
1131TEST(InvokeWithoutArgsTest, Method) {
1132 Foo foo;
1133 Action<int(bool, char)> a = // NOLINT
1134 InvokeWithoutArgs(&foo, &Foo::Nullary);
1135 EXPECT_EQ(123, a.Perform(make_tuple(true, 'a')));
1136}
1137
1138// Tests using IgnoreResult() on a polymorphic action.
1139TEST(IgnoreResultTest, PolymorphicAction) {
1140 Action<void(int)> a = IgnoreResult(Return(5)); // NOLINT
1141 a.Perform(make_tuple(1));
1142}
1143
1144// Tests using IgnoreResult() on a monomorphic action.
1145
1146int ReturnOne() {
1147 g_done = true;
1148 return 1;
1149}
1150
1151TEST(IgnoreResultTest, MonomorphicAction) {
1152 g_done = false;
1153 Action<void()> a = IgnoreResult(Invoke(ReturnOne));
1154 a.Perform(make_tuple());
1155 EXPECT_TRUE(g_done);
1156}
1157
1158// Tests using IgnoreResult() on an action that returns a class type.
1159
zhanyong.wan32de5f52009-12-23 00:13:23 +00001160MyClass ReturnMyClass(double /* x */) {
shiqiane35fdd92008-12-10 05:08:54 +00001161 g_done = true;
1162 return MyClass();
1163}
1164
1165TEST(IgnoreResultTest, ActionReturningClass) {
1166 g_done = false;
1167 Action<void(int)> a = IgnoreResult(Invoke(ReturnMyClass)); // NOLINT
1168 a.Perform(make_tuple(2));
1169 EXPECT_TRUE(g_done);
1170}
1171
1172TEST(AssignTest, Int) {
1173 int x = 0;
1174 Action<void(int)> a = Assign(&x, 5);
1175 a.Perform(make_tuple(0));
1176 EXPECT_EQ(5, x);
1177}
1178
1179TEST(AssignTest, String) {
1180 ::std::string x;
1181 Action<void(void)> a = Assign(&x, "Hello, world");
1182 a.Perform(make_tuple());
1183 EXPECT_EQ("Hello, world", x);
1184}
1185
1186TEST(AssignTest, CompatibleTypes) {
1187 double x = 0;
1188 Action<void(int)> a = Assign(&x, 5);
1189 a.Perform(make_tuple(0));
1190 EXPECT_DOUBLE_EQ(5, x);
1191}
1192
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001193#if !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001194
shiqiane35fdd92008-12-10 05:08:54 +00001195class SetErrnoAndReturnTest : public testing::Test {
1196 protected:
1197 virtual void SetUp() { errno = 0; }
1198 virtual void TearDown() { errno = 0; }
1199};
1200
1201TEST_F(SetErrnoAndReturnTest, Int) {
1202 Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5);
1203 EXPECT_EQ(-5, a.Perform(make_tuple()));
1204 EXPECT_EQ(ENOTTY, errno);
1205}
1206
1207TEST_F(SetErrnoAndReturnTest, Ptr) {
1208 int x;
1209 Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x);
1210 EXPECT_EQ(&x, a.Perform(make_tuple()));
1211 EXPECT_EQ(ENOTTY, errno);
1212}
1213
1214TEST_F(SetErrnoAndReturnTest, CompatibleTypes) {
1215 Action<double()> a = SetErrnoAndReturn(EINVAL, 5);
1216 EXPECT_DOUBLE_EQ(5.0, a.Perform(make_tuple()));
1217 EXPECT_EQ(EINVAL, errno);
1218}
1219
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001220#endif // !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001221
zhanyong.wana18423e2009-07-22 23:58:19 +00001222// Tests ByRef().
1223
1224// Tests that ReferenceWrapper<T> is copyable.
1225TEST(ByRefTest, IsCopyable) {
1226 const std::string s1 = "Hi";
1227 const std::string s2 = "Hello";
1228
1229 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper = ByRef(s1);
1230 const std::string& r1 = ref_wrapper;
1231 EXPECT_EQ(&s1, &r1);
1232
1233 // Assigns a new value to ref_wrapper.
1234 ref_wrapper = ByRef(s2);
1235 const std::string& r2 = ref_wrapper;
1236 EXPECT_EQ(&s2, &r2);
1237
1238 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper1 = ByRef(s1);
1239 // Copies ref_wrapper1 to ref_wrapper.
1240 ref_wrapper = ref_wrapper1;
1241 const std::string& r3 = ref_wrapper;
1242 EXPECT_EQ(&s1, &r3);
1243}
1244
1245// Tests using ByRef() on a const value.
1246TEST(ByRefTest, ConstValue) {
1247 const int n = 0;
1248 // int& ref = ByRef(n); // This shouldn't compile - we have a
1249 // negative compilation test to catch it.
1250 const int& const_ref = ByRef(n);
1251 EXPECT_EQ(&n, &const_ref);
1252}
1253
1254// Tests using ByRef() on a non-const value.
1255TEST(ByRefTest, NonConstValue) {
1256 int n = 0;
1257
1258 // ByRef(n) can be used as either an int&,
1259 int& ref = ByRef(n);
1260 EXPECT_EQ(&n, &ref);
1261
1262 // or a const int&.
1263 const int& const_ref = ByRef(n);
1264 EXPECT_EQ(&n, &const_ref);
1265}
1266
1267// Tests explicitly specifying the type when using ByRef().
1268TEST(ByRefTest, ExplicitType) {
1269 int n = 0;
1270 const int& r1 = ByRef<const int>(n);
1271 EXPECT_EQ(&n, &r1);
1272
1273 // ByRef<char>(n); // This shouldn't compile - we have a negative
1274 // compilation test to catch it.
1275
1276 Derived d;
1277 Derived& r2 = ByRef<Derived>(d);
1278 EXPECT_EQ(&d, &r2);
1279
1280 const Derived& r3 = ByRef<const Derived>(d);
1281 EXPECT_EQ(&d, &r3);
1282
1283 Base& r4 = ByRef<Base>(d);
1284 EXPECT_EQ(&d, &r4);
1285
1286 const Base& r5 = ByRef<const Base>(d);
1287 EXPECT_EQ(&d, &r5);
1288
1289 // The following shouldn't compile - we have a negative compilation
1290 // test for it.
1291 //
1292 // Base b;
1293 // ByRef<Derived>(b);
1294}
1295
1296// Tests that Google Mock prints expression ByRef(x) as a reference to x.
1297TEST(ByRefTest, PrintsCorrectly) {
1298 int n = 42;
1299 ::std::stringstream expected, actual;
1300 testing::internal::UniversalPrinter<const int&>::Print(n, &expected);
1301 testing::internal::UniversalPrint(ByRef(n), &actual);
1302 EXPECT_EQ(expected.str(), actual.str());
1303}
1304
shiqiane35fdd92008-12-10 05:08:54 +00001305} // Unnamed namespace