blob: 7200fa1ae916575064549837865924c745eab53b [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;
337
338 EXPECT_FALSE(my_action_impl.IsDoDefault());
339}
340
341TEST(ActionInterfaceTest, MakeAction) {
342 Action<MyFunction> action = MakeAction(new MyActionImpl);
343
344 // When exercising the Perform() method of Action<F>, we must pass
345 // it a tuple whose size and type are compatible with F's argument
346 // types. For example, if F is int(), then Perform() takes a
347 // 0-tuple; if F is void(bool, int), then Perform() takes a
348 // tuple<bool, int>, and so on.
349 EXPECT_EQ(5, action.Perform(make_tuple(true, 5)));
350}
351
352// Tests that Action<F> can be contructed from a pointer to
353// ActionInterface<F>.
354TEST(ActionTest, CanBeConstructedFromActionInterface) {
355 Action<MyFunction> action(new MyActionImpl);
356}
357
358// Tests that Action<F> delegates actual work to ActionInterface<F>.
359TEST(ActionTest, DelegatesWorkToActionInterface) {
360 const Action<MyFunction> action(new MyActionImpl);
361
362 EXPECT_EQ(5, action.Perform(make_tuple(true, 5)));
363 EXPECT_EQ(0, action.Perform(make_tuple(false, 1)));
364}
365
366// Tests that Action<F> can be copied.
367TEST(ActionTest, IsCopyable) {
368 Action<MyFunction> a1(new MyActionImpl);
369 Action<MyFunction> a2(a1); // Tests the copy constructor.
370
371 // a1 should continue to work after being copied from.
372 EXPECT_EQ(5, a1.Perform(make_tuple(true, 5)));
373 EXPECT_EQ(0, a1.Perform(make_tuple(false, 1)));
374
375 // a2 should work like the action it was copied from.
376 EXPECT_EQ(5, a2.Perform(make_tuple(true, 5)));
377 EXPECT_EQ(0, a2.Perform(make_tuple(false, 1)));
378
379 a2 = a1; // Tests the assignment operator.
380
381 // a1 should continue to work after being copied from.
382 EXPECT_EQ(5, a1.Perform(make_tuple(true, 5)));
383 EXPECT_EQ(0, a1.Perform(make_tuple(false, 1)));
384
385 // a2 should work like the action it was copied from.
386 EXPECT_EQ(5, a2.Perform(make_tuple(true, 5)));
387 EXPECT_EQ(0, a2.Perform(make_tuple(false, 1)));
388}
389
390// Tests that an Action<From> object can be converted to a
391// compatible Action<To> object.
392
393class IsNotZero : public ActionInterface<bool(int)> { // NOLINT
394 public:
395 virtual bool Perform(const tuple<int>& arg) {
396 return get<0>(arg) != 0;
397 }
398};
399
zhanyong.wan95b12332009-09-25 18:55:50 +0000400#if !GTEST_OS_SYMBIAN
401// Compiling this test on Nokia's Symbian compiler fails with:
402// 'Result' is not a member of class 'testing::internal::Function<int>'
403// (point of instantiation: '@unnamed@gmock_actions_test_cc@::
404// ActionTest_CanBeConvertedToOtherActionType_Test::TestBody()')
405// with no obvious fix.
shiqiane35fdd92008-12-10 05:08:54 +0000406TEST(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}
zhanyong.wan95b12332009-09-25 18:55:50 +0000412#endif // !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +0000413
414// The following two classes are for testing MakePolymorphicAction().
415
416// Implements a polymorphic action that returns the second of the
417// arguments it receives.
418class ReturnSecondArgumentAction {
419 public:
420 // We want to verify that MakePolymorphicAction() can work with a
421 // polymorphic action whose Perform() method template is either
422 // const or not. This lets us verify the non-const case.
423 template <typename Result, typename ArgumentTuple>
424 Result Perform(const ArgumentTuple& args) { return get<1>(args); }
425};
426
427// Implements a polymorphic action that can be used in a nullary
428// function to return 0.
429class ReturnZeroFromNullaryFunctionAction {
430 public:
431 // For testing that MakePolymorphicAction() works when the
432 // implementation class' Perform() method template takes only one
433 // template parameter.
434 //
435 // We want to verify that MakePolymorphicAction() can work with a
436 // polymorphic action whose Perform() method template is either
437 // const or not. This lets us verify the const case.
438 template <typename Result>
439 Result Perform(const tuple<>&) const { return 0; }
440};
441
442// These functions verify that MakePolymorphicAction() returns a
443// PolymorphicAction<T> where T is the argument's type.
444
445PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() {
446 return MakePolymorphicAction(ReturnSecondArgumentAction());
447}
448
449PolymorphicAction<ReturnZeroFromNullaryFunctionAction>
450ReturnZeroFromNullaryFunction() {
451 return MakePolymorphicAction(ReturnZeroFromNullaryFunctionAction());
452}
453
454// Tests that MakePolymorphicAction() turns a polymorphic action
455// implementation class into a polymorphic action.
456TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) {
457 Action<int(bool, int, double)> a1 = ReturnSecondArgument(); // NOLINT
458 EXPECT_EQ(5, a1.Perform(make_tuple(false, 5, 2.0)));
459}
460
461// Tests that MakePolymorphicAction() works when the implementation
462// class' Perform() method template has only one template parameter.
463TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) {
464 Action<int()> a1 = ReturnZeroFromNullaryFunction();
465 EXPECT_EQ(0, a1.Perform(make_tuple()));
466
467 Action<void*()> a2 = ReturnZeroFromNullaryFunction();
468 EXPECT_TRUE(a2.Perform(make_tuple()) == NULL);
469}
470
471// Tests that Return() works as an action for void-returning
472// functions.
473TEST(ReturnTest, WorksForVoid) {
474 const Action<void(int)> ret = Return(); // NOLINT
475 return ret.Perform(make_tuple(1));
476}
477
478// Tests that Return(v) returns v.
479TEST(ReturnTest, ReturnsGivenValue) {
480 Action<int()> ret = Return(1); // NOLINT
481 EXPECT_EQ(1, ret.Perform(make_tuple()));
482
483 ret = Return(-5);
484 EXPECT_EQ(-5, ret.Perform(make_tuple()));
485}
486
487// Tests that Return("string literal") works.
488TEST(ReturnTest, AcceptsStringLiteral) {
489 Action<const char*()> a1 = Return("Hello");
490 EXPECT_STREQ("Hello", a1.Perform(make_tuple()));
491
492 Action<std::string()> a2 = Return("world");
493 EXPECT_EQ("world", a2.Perform(make_tuple()));
494}
495
496// Tests that Return(v) is covaraint.
497
498struct Base {
499 bool operator==(const Base&) { return true; }
500};
501
502struct Derived : public Base {
503 bool operator==(const Derived&) { return true; }
504};
505
506TEST(ReturnTest, IsCovariant) {
507 Base base;
508 Derived derived;
509 Action<Base*()> ret = Return(&base);
510 EXPECT_EQ(&base, ret.Perform(make_tuple()));
511
512 ret = Return(&derived);
513 EXPECT_EQ(&derived, ret.Perform(make_tuple()));
514}
515
vladloseva070cbd2009-11-18 00:09:28 +0000516// Tests that the type of the value passed into Return is converted into T
517// when the action is cast to Action<T(...)> rather than when the action is
518// performed. See comments on testing::internal::ReturnAction in
519// gmock-actions.h for more information.
520class FromType {
521 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000522 FromType(bool* is_converted) : converted_(is_converted) {}
vladloseva070cbd2009-11-18 00:09:28 +0000523 bool* converted() const { return converted_; }
524
525 private:
526 bool* const converted_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000527
528 GTEST_DISALLOW_ASSIGN_(FromType);
vladloseva070cbd2009-11-18 00:09:28 +0000529};
530
531class ToType {
532 public:
533 ToType(const FromType& x) { *x.converted() = true; }
534};
535
536TEST(ReturnTest, ConvertsArgumentWhenConverted) {
537 bool converted = false;
538 FromType x(&converted);
539 Action<ToType()> action(Return(x));
540 EXPECT_TRUE(converted) << "Return must convert its argument in its own "
541 << "conversion operator.";
542 converted = false;
543 action.Perform(tuple<>());
544 EXPECT_FALSE(converted) << "Action must NOT convert its argument "
545 << "when performed." ;
546}
547
vladloseva070cbd2009-11-18 00:09:28 +0000548class DestinationType {};
549
550class SourceType {
551 public:
552 // Note: a non-const typecast operator.
553 operator DestinationType() { return DestinationType(); }
554};
555
556TEST(ReturnTest, CanConvertArgumentUsingNonConstTypeCastOperator) {
557 SourceType s;
558 Action<DestinationType()> action(Return(s));
559}
vladloseva070cbd2009-11-18 00:09:28 +0000560
shiqiane35fdd92008-12-10 05:08:54 +0000561// Tests that ReturnNull() returns NULL in a pointer-returning function.
562TEST(ReturnNullTest, WorksInPointerReturningFunction) {
563 const Action<int*()> a1 = ReturnNull();
564 EXPECT_TRUE(a1.Perform(make_tuple()) == NULL);
565
566 const Action<const char*(bool)> a2 = ReturnNull(); // NOLINT
567 EXPECT_TRUE(a2.Perform(make_tuple(true)) == NULL);
568}
569
570// Tests that ReturnRef(v) works for reference types.
571TEST(ReturnRefTest, WorksForReference) {
572 const int n = 0;
573 const Action<const int&(bool)> ret = ReturnRef(n); // NOLINT
574
575 EXPECT_EQ(&n, &ret.Perform(make_tuple(true)));
576}
577
578// Tests that ReturnRef(v) is covariant.
579TEST(ReturnRefTest, IsCovariant) {
580 Base base;
581 Derived derived;
582 Action<Base&()> a = ReturnRef(base);
583 EXPECT_EQ(&base, &a.Perform(make_tuple()));
584
585 a = ReturnRef(derived);
586 EXPECT_EQ(&derived, &a.Perform(make_tuple()));
587}
588
zhanyong.wane3bd0982010-07-03 00:16:42 +0000589// Tests that ReturnRefOfCopy(v) works for reference types.
590TEST(ReturnRefOfCopyTest, WorksForReference) {
591 int n = 42;
592 const Action<const int&()> ret = ReturnRefOfCopy(n);
593
594 EXPECT_NE(&n, &ret.Perform(make_tuple()));
595 EXPECT_EQ(42, ret.Perform(make_tuple()));
596
597 n = 43;
598 EXPECT_NE(&n, &ret.Perform(make_tuple()));
599 EXPECT_EQ(42, ret.Perform(make_tuple()));
600}
601
602// Tests that ReturnRefOfCopy(v) is covariant.
603TEST(ReturnRefOfCopyTest, IsCovariant) {
604 Base base;
605 Derived derived;
606 Action<Base&()> a = ReturnRefOfCopy(base);
607 EXPECT_NE(&base, &a.Perform(make_tuple()));
608
609 a = ReturnRefOfCopy(derived);
610 EXPECT_NE(&derived, &a.Perform(make_tuple()));
611}
612
shiqiane35fdd92008-12-10 05:08:54 +0000613// Tests that DoDefault() does the default action for the mock method.
614
615class MyClass {};
616
617class MockClass {
618 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000619 MockClass() {}
620
shiqiane35fdd92008-12-10 05:08:54 +0000621 MOCK_METHOD1(IntFunc, int(bool flag)); // NOLINT
622 MOCK_METHOD0(Foo, MyClass());
zhanyong.wan32de5f52009-12-23 00:13:23 +0000623
624 private:
625 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockClass);
shiqiane35fdd92008-12-10 05:08:54 +0000626};
627
628// Tests that DoDefault() returns the built-in default value for the
629// return type by default.
630TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) {
631 MockClass mock;
632 EXPECT_CALL(mock, IntFunc(_))
633 .WillOnce(DoDefault());
634 EXPECT_EQ(0, mock.IntFunc(true));
635}
636
shiqiane35fdd92008-12-10 05:08:54 +0000637// Tests that DoDefault() aborts the process when there is no built-in
638// default value for the return type.
639TEST(DoDefaultDeathTest, DiesForUnknowType) {
640 MockClass mock;
641 EXPECT_CALL(mock, Foo())
642 .WillRepeatedly(DoDefault());
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000643 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000644 mock.Foo();
645 }, "");
646}
647
648// Tests that using DoDefault() inside a composite action leads to a
649// run-time error.
650
zhanyong.wan32de5f52009-12-23 00:13:23 +0000651void VoidFunc(bool /* flag */) {}
shiqiane35fdd92008-12-10 05:08:54 +0000652
653TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
654 MockClass mock;
655 EXPECT_CALL(mock, IntFunc(_))
656 .WillRepeatedly(DoAll(Invoke(VoidFunc),
657 DoDefault()));
658
659 // Ideally we should verify the error message as well. Sadly,
660 // EXPECT_DEATH() can only capture stderr, while Google Mock's
661 // errors are printed on stdout. Therefore we have to settle for
662 // not verifying the message.
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000663 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000664 mock.IntFunc(true);
665 }, "");
666}
667
shiqiane35fdd92008-12-10 05:08:54 +0000668// Tests that DoDefault() returns the default value set by
669// DefaultValue<T>::Set() when it's not overriden by an ON_CALL().
670TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) {
671 DefaultValue<int>::Set(1);
672 MockClass mock;
673 EXPECT_CALL(mock, IntFunc(_))
674 .WillOnce(DoDefault());
675 EXPECT_EQ(1, mock.IntFunc(false));
676 DefaultValue<int>::Clear();
677}
678
679// Tests that DoDefault() does the action specified by ON_CALL().
680TEST(DoDefaultTest, DoesWhatOnCallSpecifies) {
681 MockClass mock;
682 ON_CALL(mock, IntFunc(_))
683 .WillByDefault(Return(2));
684 EXPECT_CALL(mock, IntFunc(_))
685 .WillOnce(DoDefault());
686 EXPECT_EQ(2, mock.IntFunc(false));
687}
688
689// Tests that using DoDefault() in ON_CALL() leads to a run-time failure.
690TEST(DoDefaultTest, CannotBeUsedInOnCall) {
691 MockClass mock;
692 EXPECT_NONFATAL_FAILURE({ // NOLINT
693 ON_CALL(mock, IntFunc(_))
694 .WillByDefault(DoDefault());
695 }, "DoDefault() cannot be used in ON_CALL()");
696}
697
zhanyong.wan59214832010-10-05 05:58:51 +0000698// Tests that SetArgPointee<N>(v) sets the variable pointed to by
699// the N-th (0-based) argument to v.
700TEST(SetArgPointeeTest, SetsTheNthPointee) {
701 typedef void MyFunction(bool, int*, char*);
702 Action<MyFunction> a = SetArgPointee<1>(2);
703
704 int n = 0;
705 char ch = '\0';
706 a.Perform(make_tuple(true, &n, &ch));
707 EXPECT_EQ(2, n);
708 EXPECT_EQ('\0', ch);
709
710 a = SetArgPointee<2>('a');
711 n = 0;
712 ch = '\0';
713 a.Perform(make_tuple(true, &n, &ch));
714 EXPECT_EQ(0, n);
715 EXPECT_EQ('a', ch);
716}
717
718#if GTEST_HAS_PROTOBUF_
719
720// Tests that SetArgPointee<N>(proto_buffer) sets the v1 protobuf
721// variable pointed to by the N-th (0-based) argument to proto_buffer.
722TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
723 TestMessage* const msg = new TestMessage;
724 msg->set_member("yes");
725 TestMessage orig_msg;
726 orig_msg.CopyFrom(*msg);
727
728 Action<void(bool, TestMessage*)> a = SetArgPointee<1>(*msg);
729 // SetArgPointee<N>(proto_buffer) makes a copy of proto_buffer
730 // s.t. the action works even when the original proto_buffer has
731 // died. We ensure this behavior by deleting msg before using the
732 // action.
733 delete msg;
734
735 TestMessage dest;
736 EXPECT_FALSE(orig_msg.Equals(dest));
737 a.Perform(make_tuple(true, &dest));
738 EXPECT_TRUE(orig_msg.Equals(dest));
739}
740
741// Tests that SetArgPointee<N>(proto_buffer) sets the
742// ::ProtocolMessage variable pointed to by the N-th (0-based)
743// argument to proto_buffer.
744TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
745 TestMessage* const msg = new TestMessage;
746 msg->set_member("yes");
747 TestMessage orig_msg;
748 orig_msg.CopyFrom(*msg);
749
750 Action<void(bool, ::ProtocolMessage*)> a = SetArgPointee<1>(*msg);
751 // SetArgPointee<N>(proto_buffer) makes a copy of proto_buffer
752 // s.t. the action works even when the original proto_buffer has
753 // died. We ensure this behavior by deleting msg before using the
754 // action.
755 delete msg;
756
757 TestMessage dest;
758 ::ProtocolMessage* const dest_base = &dest;
759 EXPECT_FALSE(orig_msg.Equals(dest));
760 a.Perform(make_tuple(true, dest_base));
761 EXPECT_TRUE(orig_msg.Equals(dest));
762}
763
764// Tests that SetArgPointee<N>(proto2_buffer) sets the v2
765// protobuf variable pointed to by the N-th (0-based) argument to
766// proto2_buffer.
767TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
768 using testing::internal::FooMessage;
769 FooMessage* const msg = new FooMessage;
770 msg->set_int_field(2);
771 msg->set_string_field("hi");
772 FooMessage orig_msg;
773 orig_msg.CopyFrom(*msg);
774
775 Action<void(bool, FooMessage*)> a = SetArgPointee<1>(*msg);
776 // SetArgPointee<N>(proto2_buffer) makes a copy of
777 // proto2_buffer s.t. the action works even when the original
778 // proto2_buffer has died. We ensure this behavior by deleting msg
779 // before using the action.
780 delete msg;
781
782 FooMessage dest;
783 dest.set_int_field(0);
784 a.Perform(make_tuple(true, &dest));
785 EXPECT_EQ(2, dest.int_field());
786 EXPECT_EQ("hi", dest.string_field());
787}
788
789// Tests that SetArgPointee<N>(proto2_buffer) sets the
790// proto2::Message variable pointed to by the N-th (0-based) argument
791// to proto2_buffer.
792TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
793 using testing::internal::FooMessage;
794 FooMessage* const msg = new FooMessage;
795 msg->set_int_field(2);
796 msg->set_string_field("hi");
797 FooMessage orig_msg;
798 orig_msg.CopyFrom(*msg);
799
800 Action<void(bool, ::proto2::Message*)> a = SetArgPointee<1>(*msg);
801 // SetArgPointee<N>(proto2_buffer) makes a copy of
802 // proto2_buffer s.t. the action works even when the original
803 // proto2_buffer has died. We ensure this behavior by deleting msg
804 // before using the action.
805 delete msg;
806
807 FooMessage dest;
808 dest.set_int_field(0);
809 ::proto2::Message* const dest_base = &dest;
810 a.Perform(make_tuple(true, dest_base));
811 EXPECT_EQ(2, dest.int_field());
812 EXPECT_EQ("hi", dest.string_field());
813}
814
815#endif // GTEST_HAS_PROTOBUF_
816
shiqiane35fdd92008-12-10 05:08:54 +0000817// Tests that SetArgumentPointee<N>(v) sets the variable pointed to by
818// the N-th (0-based) argument to v.
819TEST(SetArgumentPointeeTest, SetsTheNthPointee) {
820 typedef void MyFunction(bool, int*, char*);
821 Action<MyFunction> a = SetArgumentPointee<1>(2);
822
823 int n = 0;
824 char ch = '\0';
825 a.Perform(make_tuple(true, &n, &ch));
826 EXPECT_EQ(2, n);
827 EXPECT_EQ('\0', ch);
828
829 a = SetArgumentPointee<2>('a');
830 n = 0;
831 ch = '\0';
832 a.Perform(make_tuple(true, &n, &ch));
833 EXPECT_EQ(0, n);
834 EXPECT_EQ('a', ch);
835}
836
zhanyong.wan02f71062010-05-10 17:14:29 +0000837#if GTEST_HAS_PROTOBUF_
shiqiane35fdd92008-12-10 05:08:54 +0000838
zhanyong.wanc6a41232009-05-13 23:38:40 +0000839// Tests that SetArgumentPointee<N>(proto_buffer) sets the v1 protobuf
840// variable pointed to by the N-th (0-based) argument to proto_buffer.
shiqiane35fdd92008-12-10 05:08:54 +0000841TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
shiqiane35fdd92008-12-10 05:08:54 +0000842 TestMessage* const msg = new TestMessage;
843 msg->set_member("yes");
844 TestMessage orig_msg;
845 orig_msg.CopyFrom(*msg);
846
zhanyong.wanc6a41232009-05-13 23:38:40 +0000847 Action<void(bool, TestMessage*)> a = SetArgumentPointee<1>(*msg);
shiqiane35fdd92008-12-10 05:08:54 +0000848 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
849 // s.t. the action works even when the original proto_buffer has
850 // died. We ensure this behavior by deleting msg before using the
851 // action.
852 delete msg;
853
854 TestMessage dest;
855 EXPECT_FALSE(orig_msg.Equals(dest));
856 a.Perform(make_tuple(true, &dest));
857 EXPECT_TRUE(orig_msg.Equals(dest));
858}
859
zhanyong.wanc6a41232009-05-13 23:38:40 +0000860// Tests that SetArgumentPointee<N>(proto_buffer) sets the
861// ::ProtocolMessage variable pointed to by the N-th (0-based)
862// argument to proto_buffer.
863TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
864 TestMessage* const msg = new TestMessage;
865 msg->set_member("yes");
866 TestMessage orig_msg;
867 orig_msg.CopyFrom(*msg);
868
869 Action<void(bool, ::ProtocolMessage*)> a = SetArgumentPointee<1>(*msg);
870 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
871 // s.t. the action works even when the original proto_buffer has
872 // died. We ensure this behavior by deleting msg before using the
873 // action.
874 delete msg;
875
876 TestMessage dest;
877 ::ProtocolMessage* const dest_base = &dest;
878 EXPECT_FALSE(orig_msg.Equals(dest));
879 a.Perform(make_tuple(true, dest_base));
880 EXPECT_TRUE(orig_msg.Equals(dest));
881}
882
883// Tests that SetArgumentPointee<N>(proto2_buffer) sets the v2
884// protobuf variable pointed to by the N-th (0-based) argument to
885// proto2_buffer.
shiqiane35fdd92008-12-10 05:08:54 +0000886TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
887 using testing::internal::FooMessage;
shiqiane35fdd92008-12-10 05:08:54 +0000888 FooMessage* const msg = new FooMessage;
889 msg->set_int_field(2);
890 msg->set_string_field("hi");
891 FooMessage orig_msg;
892 orig_msg.CopyFrom(*msg);
893
zhanyong.wanc6a41232009-05-13 23:38:40 +0000894 Action<void(bool, FooMessage*)> a = SetArgumentPointee<1>(*msg);
shiqiane35fdd92008-12-10 05:08:54 +0000895 // SetArgumentPointee<N>(proto2_buffer) makes a copy of
896 // proto2_buffer s.t. the action works even when the original
897 // proto2_buffer has died. We ensure this behavior by deleting msg
898 // before using the action.
899 delete msg;
900
901 FooMessage dest;
902 dest.set_int_field(0);
903 a.Perform(make_tuple(true, &dest));
904 EXPECT_EQ(2, dest.int_field());
905 EXPECT_EQ("hi", dest.string_field());
906}
907
zhanyong.wanc6a41232009-05-13 23:38:40 +0000908// Tests that SetArgumentPointee<N>(proto2_buffer) sets the
909// proto2::Message variable pointed to by the N-th (0-based) argument
910// to proto2_buffer.
911TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
912 using testing::internal::FooMessage;
913 FooMessage* const msg = new FooMessage;
914 msg->set_int_field(2);
915 msg->set_string_field("hi");
916 FooMessage orig_msg;
917 orig_msg.CopyFrom(*msg);
918
919 Action<void(bool, ::proto2::Message*)> a = SetArgumentPointee<1>(*msg);
920 // SetArgumentPointee<N>(proto2_buffer) makes a copy of
921 // proto2_buffer s.t. the action works even when the original
922 // proto2_buffer has died. We ensure this behavior by deleting msg
923 // before using the action.
924 delete msg;
925
926 FooMessage dest;
927 dest.set_int_field(0);
928 ::proto2::Message* const dest_base = &dest;
929 a.Perform(make_tuple(true, dest_base));
930 EXPECT_EQ(2, dest.int_field());
931 EXPECT_EQ("hi", dest.string_field());
932}
933
zhanyong.wan02f71062010-05-10 17:14:29 +0000934#endif // GTEST_HAS_PROTOBUF_
shiqiane35fdd92008-12-10 05:08:54 +0000935
shiqiane35fdd92008-12-10 05:08:54 +0000936// Sample functions and functors for testing Invoke() and etc.
937int Nullary() { return 1; }
938
939class NullaryFunctor {
940 public:
941 int operator()() { return 2; }
942};
943
944bool g_done = false;
945void VoidNullary() { g_done = true; }
946
947class VoidNullaryFunctor {
948 public:
949 void operator()() { g_done = true; }
950};
951
952bool Unary(int x) { return x < 0; }
953
954const char* Plus1(const char* s) { return s + 1; }
955
zhanyong.wan32de5f52009-12-23 00:13:23 +0000956void VoidUnary(int /* n */) { g_done = true; }
shiqiane35fdd92008-12-10 05:08:54 +0000957
958bool ByConstRef(const std::string& s) { return s == "Hi"; }
959
960const double g_double = 0;
961bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
962
963std::string ByNonConstRef(std::string& s) { return s += "+"; } // NOLINT
964
965struct UnaryFunctor {
966 int operator()(bool x) { return x ? 1 : -1; }
967};
968
969const char* Binary(const char* input, short n) { return input + n; } // NOLINT
970
971void VoidBinary(int, char) { g_done = true; }
972
973int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT
974
975void VoidTernary(int, char, bool) { g_done = true; }
976
977int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
978
979void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; }
980
981int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
982
983struct SumOf5Functor {
984 int operator()(int a, int b, int c, int d, int e) {
985 return a + b + c + d + e;
986 }
987};
988
989int SumOf6(int a, int b, int c, int d, int e, int f) {
990 return a + b + c + d + e + f;
991}
992
993struct SumOf6Functor {
994 int operator()(int a, int b, int c, int d, int e, int f) {
995 return a + b + c + d + e + f;
996 }
997};
998
999class Foo {
1000 public:
1001 Foo() : value_(123) {}
1002
1003 int Nullary() const { return value_; }
1004 short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT
1005 std::string Binary(const std::string& str, char c) const { return str + c; }
1006 int Ternary(int x, bool y, char z) { return value_ + x + y*z; }
1007 int SumOf4(int a, int b, int c, int d) const {
1008 return a + b + c + d + value_;
1009 }
1010 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
1011 int SumOf6(int a, int b, int c, int d, int e, int f) {
1012 return a + b + c + d + e + f;
1013 }
1014 private:
1015 int value_;
1016};
1017
1018// Tests InvokeWithoutArgs(function).
1019TEST(InvokeWithoutArgsTest, Function) {
1020 // As an action that takes one argument.
1021 Action<int(int)> a = InvokeWithoutArgs(Nullary); // NOLINT
1022 EXPECT_EQ(1, a.Perform(make_tuple(2)));
1023
1024 // As an action that takes two arguments.
zhanyong.wan32de5f52009-12-23 00:13:23 +00001025 Action<int(int, double)> a2 = InvokeWithoutArgs(Nullary); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00001026 EXPECT_EQ(1, a2.Perform(make_tuple(2, 3.5)));
1027
1028 // As an action that returns void.
1029 Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary); // NOLINT
1030 g_done = false;
1031 a3.Perform(make_tuple(1));
1032 EXPECT_TRUE(g_done);
1033}
1034
1035// Tests InvokeWithoutArgs(functor).
1036TEST(InvokeWithoutArgsTest, Functor) {
1037 // As an action that takes no argument.
1038 Action<int()> a = InvokeWithoutArgs(NullaryFunctor()); // NOLINT
1039 EXPECT_EQ(2, a.Perform(make_tuple()));
1040
1041 // As an action that takes three arguments.
zhanyong.wan32de5f52009-12-23 00:13:23 +00001042 Action<int(int, double, char)> a2 = // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00001043 InvokeWithoutArgs(NullaryFunctor());
1044 EXPECT_EQ(2, a2.Perform(make_tuple(3, 3.5, 'a')));
1045
1046 // As an action that returns void.
1047 Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor());
1048 g_done = false;
1049 a3.Perform(make_tuple());
1050 EXPECT_TRUE(g_done);
1051}
1052
1053// Tests InvokeWithoutArgs(obj_ptr, method).
1054TEST(InvokeWithoutArgsTest, Method) {
1055 Foo foo;
1056 Action<int(bool, char)> a = // NOLINT
1057 InvokeWithoutArgs(&foo, &Foo::Nullary);
1058 EXPECT_EQ(123, a.Perform(make_tuple(true, 'a')));
1059}
1060
1061// Tests using IgnoreResult() on a polymorphic action.
1062TEST(IgnoreResultTest, PolymorphicAction) {
1063 Action<void(int)> a = IgnoreResult(Return(5)); // NOLINT
1064 a.Perform(make_tuple(1));
1065}
1066
1067// Tests using IgnoreResult() on a monomorphic action.
1068
1069int ReturnOne() {
1070 g_done = true;
1071 return 1;
1072}
1073
1074TEST(IgnoreResultTest, MonomorphicAction) {
1075 g_done = false;
1076 Action<void()> a = IgnoreResult(Invoke(ReturnOne));
1077 a.Perform(make_tuple());
1078 EXPECT_TRUE(g_done);
1079}
1080
1081// Tests using IgnoreResult() on an action that returns a class type.
1082
zhanyong.wan32de5f52009-12-23 00:13:23 +00001083MyClass ReturnMyClass(double /* x */) {
shiqiane35fdd92008-12-10 05:08:54 +00001084 g_done = true;
1085 return MyClass();
1086}
1087
1088TEST(IgnoreResultTest, ActionReturningClass) {
1089 g_done = false;
1090 Action<void(int)> a = IgnoreResult(Invoke(ReturnMyClass)); // NOLINT
1091 a.Perform(make_tuple(2));
1092 EXPECT_TRUE(g_done);
1093}
1094
1095TEST(AssignTest, Int) {
1096 int x = 0;
1097 Action<void(int)> a = Assign(&x, 5);
1098 a.Perform(make_tuple(0));
1099 EXPECT_EQ(5, x);
1100}
1101
1102TEST(AssignTest, String) {
1103 ::std::string x;
1104 Action<void(void)> a = Assign(&x, "Hello, world");
1105 a.Perform(make_tuple());
1106 EXPECT_EQ("Hello, world", x);
1107}
1108
1109TEST(AssignTest, CompatibleTypes) {
1110 double x = 0;
1111 Action<void(int)> a = Assign(&x, 5);
1112 a.Perform(make_tuple(0));
1113 EXPECT_DOUBLE_EQ(5, x);
1114}
1115
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001116#if !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001117
shiqiane35fdd92008-12-10 05:08:54 +00001118class SetErrnoAndReturnTest : public testing::Test {
1119 protected:
1120 virtual void SetUp() { errno = 0; }
1121 virtual void TearDown() { errno = 0; }
1122};
1123
1124TEST_F(SetErrnoAndReturnTest, Int) {
1125 Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5);
1126 EXPECT_EQ(-5, a.Perform(make_tuple()));
1127 EXPECT_EQ(ENOTTY, errno);
1128}
1129
1130TEST_F(SetErrnoAndReturnTest, Ptr) {
1131 int x;
1132 Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x);
1133 EXPECT_EQ(&x, a.Perform(make_tuple()));
1134 EXPECT_EQ(ENOTTY, errno);
1135}
1136
1137TEST_F(SetErrnoAndReturnTest, CompatibleTypes) {
1138 Action<double()> a = SetErrnoAndReturn(EINVAL, 5);
1139 EXPECT_DOUBLE_EQ(5.0, a.Perform(make_tuple()));
1140 EXPECT_EQ(EINVAL, errno);
1141}
1142
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001143#endif // !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001144
zhanyong.wana18423e2009-07-22 23:58:19 +00001145// Tests ByRef().
1146
1147// Tests that ReferenceWrapper<T> is copyable.
1148TEST(ByRefTest, IsCopyable) {
1149 const std::string s1 = "Hi";
1150 const std::string s2 = "Hello";
1151
1152 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper = ByRef(s1);
1153 const std::string& r1 = ref_wrapper;
1154 EXPECT_EQ(&s1, &r1);
1155
1156 // Assigns a new value to ref_wrapper.
1157 ref_wrapper = ByRef(s2);
1158 const std::string& r2 = ref_wrapper;
1159 EXPECT_EQ(&s2, &r2);
1160
1161 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper1 = ByRef(s1);
1162 // Copies ref_wrapper1 to ref_wrapper.
1163 ref_wrapper = ref_wrapper1;
1164 const std::string& r3 = ref_wrapper;
1165 EXPECT_EQ(&s1, &r3);
1166}
1167
1168// Tests using ByRef() on a const value.
1169TEST(ByRefTest, ConstValue) {
1170 const int n = 0;
1171 // int& ref = ByRef(n); // This shouldn't compile - we have a
1172 // negative compilation test to catch it.
1173 const int& const_ref = ByRef(n);
1174 EXPECT_EQ(&n, &const_ref);
1175}
1176
1177// Tests using ByRef() on a non-const value.
1178TEST(ByRefTest, NonConstValue) {
1179 int n = 0;
1180
1181 // ByRef(n) can be used as either an int&,
1182 int& ref = ByRef(n);
1183 EXPECT_EQ(&n, &ref);
1184
1185 // or a const int&.
1186 const int& const_ref = ByRef(n);
1187 EXPECT_EQ(&n, &const_ref);
1188}
1189
1190// Tests explicitly specifying the type when using ByRef().
1191TEST(ByRefTest, ExplicitType) {
1192 int n = 0;
1193 const int& r1 = ByRef<const int>(n);
1194 EXPECT_EQ(&n, &r1);
1195
1196 // ByRef<char>(n); // This shouldn't compile - we have a negative
1197 // compilation test to catch it.
1198
1199 Derived d;
1200 Derived& r2 = ByRef<Derived>(d);
1201 EXPECT_EQ(&d, &r2);
1202
1203 const Derived& r3 = ByRef<const Derived>(d);
1204 EXPECT_EQ(&d, &r3);
1205
1206 Base& r4 = ByRef<Base>(d);
1207 EXPECT_EQ(&d, &r4);
1208
1209 const Base& r5 = ByRef<const Base>(d);
1210 EXPECT_EQ(&d, &r5);
1211
1212 // The following shouldn't compile - we have a negative compilation
1213 // test for it.
1214 //
1215 // Base b;
1216 // ByRef<Derived>(b);
1217}
1218
1219// Tests that Google Mock prints expression ByRef(x) as a reference to x.
1220TEST(ByRefTest, PrintsCorrectly) {
1221 int n = 42;
1222 ::std::stringstream expected, actual;
1223 testing::internal::UniversalPrinter<const int&>::Print(n, &expected);
1224 testing::internal::UniversalPrint(ByRef(n), &actual);
1225 EXPECT_EQ(expected.str(), actual.str());
1226}
1227
shiqiane35fdd92008-12-10 05:08:54 +00001228} // Unnamed namespace