gmock-actions_test.cc 50.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Copyright 2007, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Gennadiy Civil's avatar
 
Gennadiy Civil committed
29

30
31
32
33
// Google Mock - a framework for writing C++ mock classes.
//
// This file tests the built-in actions.

durandal's avatar
durandal committed
34
// Silence C4100 (unreferenced formal parameter) for MSVC
Gennadiy Civil's avatar
 
Gennadiy Civil committed
35
#ifdef _MSC_VER
36
37
#pragma warning(push)
#pragma warning(disable : 4100)
durandal's avatar
durandal committed
38
39
40
#if _MSC_VER == 1900
// and silence C4800 (C4800: 'int *const ': forcing value
// to bool 'true' or 'false') for MSVC 15
41
#pragma warning(disable : 4800)
Gennadiy Civil's avatar
 
Gennadiy Civil committed
42
43
44
#endif
#endif

45
#include "gmock/gmock-actions.h"
46

47
48
#include <algorithm>
#include <iterator>
49
#include <memory>
50
#include <string>
51
#include <type_traits>
52

53
54
55
#include "gmock/gmock.h"
#include "gmock/internal/gmock-port.h"
#include "gtest/gtest-spi.h"
56
#include "gtest/gtest.h"
57
58
59

namespace {

Abseil Team's avatar
Abseil Team committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using ::testing::_;
using ::testing::Action;
using ::testing::ActionInterface;
using ::testing::Assign;
using ::testing::ByMove;
using ::testing::ByRef;
using ::testing::DefaultValue;
using ::testing::DoAll;
using ::testing::DoDefault;
using ::testing::IgnoreResult;
using ::testing::Invoke;
using ::testing::InvokeWithoutArgs;
using ::testing::MakePolymorphicAction;
using ::testing::PolymorphicAction;
using ::testing::Return;
using ::testing::ReturnNew;
using ::testing::ReturnNull;
using ::testing::ReturnRef;
using ::testing::ReturnRefOfCopy;
using ::testing::ReturnRoundRobin;
using ::testing::SetArgPointee;
using ::testing::SetArgumentPointee;
using ::testing::Unused;
using ::testing::WithArgs;
using ::testing::internal::BuiltInDefaultValue;
85

86
#if !GTEST_OS_WINDOWS_MOBILE
Abseil Team's avatar
Abseil Team committed
87
using ::testing::SetErrnoAndReturn;
88
#endif
89
90
91

// Tests that BuiltInDefaultValue<T*>::Get() returns NULL.
TEST(BuiltInDefaultValueTest, IsNullForPointerTypes) {
92
93
94
  EXPECT_TRUE(BuiltInDefaultValue<int*>::Get() == nullptr);
  EXPECT_TRUE(BuiltInDefaultValue<const char*>::Get() == nullptr);
  EXPECT_TRUE(BuiltInDefaultValue<void*>::Get() == nullptr);
95
96
}

97
98
99
100
101
102
103
// Tests that BuiltInDefaultValue<T*>::Exists() return true.
TEST(BuiltInDefaultValueTest, ExistsForPointerTypes) {
  EXPECT_TRUE(BuiltInDefaultValue<int*>::Exists());
  EXPECT_TRUE(BuiltInDefaultValue<const char*>::Exists());
  EXPECT_TRUE(BuiltInDefaultValue<void*>::Exists());
}

104
105
106
// Tests that BuiltInDefaultValue<T>::Get() returns 0 when T is a
// built-in numeric type.
TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) {
107
  EXPECT_EQ(0U, BuiltInDefaultValue<unsigned char>::Get());
108
109
  EXPECT_EQ(0, BuiltInDefaultValue<signed char>::Get());
  EXPECT_EQ(0, BuiltInDefaultValue<char>::Get());
110
#if GMOCK_WCHAR_T_IS_NATIVE_
111
#if !defined(__WCHAR_UNSIGNED__)
112
  EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get());
113
114
115
#else
  EXPECT_EQ(0U, BuiltInDefaultValue<wchar_t>::Get());
#endif
116
#endif
117
  EXPECT_EQ(0U, BuiltInDefaultValue<unsigned short>::Get());  // NOLINT
118
119
  EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get());     // NOLINT
  EXPECT_EQ(0, BuiltInDefaultValue<short>::Get());            // NOLINT
120
  EXPECT_EQ(0U, BuiltInDefaultValue<unsigned int>::Get());
121
122
  EXPECT_EQ(0, BuiltInDefaultValue<signed int>::Get());
  EXPECT_EQ(0, BuiltInDefaultValue<int>::Get());
123
124
125
  EXPECT_EQ(0U, BuiltInDefaultValue<unsigned long>::Get());       // NOLINT
  EXPECT_EQ(0, BuiltInDefaultValue<signed long>::Get());          // NOLINT
  EXPECT_EQ(0, BuiltInDefaultValue<long>::Get());                 // NOLINT
Abseil Team's avatar
Abseil Team committed
126
  EXPECT_EQ(0U, BuiltInDefaultValue<unsigned long long>::Get());  // NOLINT
127
128
  EXPECT_EQ(0, BuiltInDefaultValue<signed long long>::Get());     // NOLINT
  EXPECT_EQ(0, BuiltInDefaultValue<long long>::Get());            // NOLINT
129
130
131
132
  EXPECT_EQ(0, BuiltInDefaultValue<float>::Get());
  EXPECT_EQ(0, BuiltInDefaultValue<double>::Get());
}

133
134
135
136
137
138
// Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
// built-in numeric type.
TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) {
  EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists());
  EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists());
  EXPECT_TRUE(BuiltInDefaultValue<char>::Exists());
139
#if GMOCK_WCHAR_T_IS_NATIVE_
140
  EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists());
141
#endif
142
  EXPECT_TRUE(BuiltInDefaultValue<unsigned short>::Exists());  // NOLINT
143
144
  EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists());    // NOLINT
  EXPECT_TRUE(BuiltInDefaultValue<short>::Exists());           // NOLINT
145
146
147
  EXPECT_TRUE(BuiltInDefaultValue<unsigned int>::Exists());
  EXPECT_TRUE(BuiltInDefaultValue<signed int>::Exists());
  EXPECT_TRUE(BuiltInDefaultValue<int>::Exists());
148
149
150
  EXPECT_TRUE(BuiltInDefaultValue<unsigned long>::Exists());       // NOLINT
  EXPECT_TRUE(BuiltInDefaultValue<signed long>::Exists());         // NOLINT
  EXPECT_TRUE(BuiltInDefaultValue<long>::Exists());                // NOLINT
Abseil Team's avatar
Abseil Team committed
151
  EXPECT_TRUE(BuiltInDefaultValue<unsigned long long>::Exists());  // NOLINT
152
153
  EXPECT_TRUE(BuiltInDefaultValue<signed long long>::Exists());    // NOLINT
  EXPECT_TRUE(BuiltInDefaultValue<long long>::Exists());           // NOLINT
154
155
156
157
  EXPECT_TRUE(BuiltInDefaultValue<float>::Exists());
  EXPECT_TRUE(BuiltInDefaultValue<double>::Exists());
}

158
159
160
161
162
// Tests that BuiltInDefaultValue<bool>::Get() returns false.
TEST(BuiltInDefaultValueTest, IsFalseForBool) {
  EXPECT_FALSE(BuiltInDefaultValue<bool>::Get());
}

163
164
165
166
167
// Tests that BuiltInDefaultValue<bool>::Exists() returns true.
TEST(BuiltInDefaultValueTest, BoolExists) {
  EXPECT_TRUE(BuiltInDefaultValue<bool>::Exists());
}

168
169
170
// Tests that BuiltInDefaultValue<T>::Get() returns "" when T is a
// string type.
TEST(BuiltInDefaultValueTest, IsEmptyStringForString) {
171
  EXPECT_EQ("", BuiltInDefaultValue<::std::string>::Get());
172
173
}

174
175
176
// Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
// string type.
TEST(BuiltInDefaultValueTest, ExistsForString) {
177
  EXPECT_TRUE(BuiltInDefaultValue<::std::string>::Exists());
178
179
}

180
181
182
183
184
// Tests that BuiltInDefaultValue<const T>::Get() returns the same
// value as BuiltInDefaultValue<T>::Get() does.
TEST(BuiltInDefaultValueTest, WorksForConstTypes) {
  EXPECT_EQ("", BuiltInDefaultValue<const std::string>::Get());
  EXPECT_EQ(0, BuiltInDefaultValue<const int>::Get());
185
  EXPECT_TRUE(BuiltInDefaultValue<char* const>::Get() == nullptr);
186
187
188
  EXPECT_FALSE(BuiltInDefaultValue<const bool>::Get());
}

189
190
191
192
193
194
// A type that's default constructible.
class MyDefaultConstructible {
 public:
  MyDefaultConstructible() : value_(42) {}

  int value() const { return value_; }
195

196
197
 private:
  int value_;
198
199
};

200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// A type that's not default constructible.
class MyNonDefaultConstructible {
 public:
  // Does not have a default ctor.
  explicit MyNonDefaultConstructible(int a_value) : value_(a_value) {}

  int value() const { return value_; }

 private:
  int value_;
};

TEST(BuiltInDefaultValueTest, ExistsForDefaultConstructibleType) {
  EXPECT_TRUE(BuiltInDefaultValue<MyDefaultConstructible>::Exists());
}

TEST(BuiltInDefaultValueTest, IsDefaultConstructedForDefaultConstructibleType) {
  EXPECT_EQ(42, BuiltInDefaultValue<MyDefaultConstructible>::Get().value());
}

TEST(BuiltInDefaultValueTest, DoesNotExistForNonDefaultConstructibleType) {
  EXPECT_FALSE(BuiltInDefaultValue<MyNonDefaultConstructible>::Exists());
222
223
}

224
225
// Tests that BuiltInDefaultValue<T&>::Get() aborts the program.
TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) {
226
227
  EXPECT_DEATH_IF_SUPPORTED({ BuiltInDefaultValue<int&>::Get(); }, "");
  EXPECT_DEATH_IF_SUPPORTED({ BuiltInDefaultValue<const char&>::Get(); }, "");
228
229
}

230
TEST(BuiltInDefaultValueDeathTest, IsUndefinedForNonDefaultConstructibleType) {
231
232
  EXPECT_DEATH_IF_SUPPORTED(
      { BuiltInDefaultValue<MyNonDefaultConstructible>::Get(); }, "");
233
234
235
236
237
}

// Tests that DefaultValue<T>::IsSet() is false initially.
TEST(DefaultValueTest, IsInitiallyUnset) {
  EXPECT_FALSE(DefaultValue<int>::IsSet());
238
239
  EXPECT_FALSE(DefaultValue<MyDefaultConstructible>::IsSet());
  EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::IsSet());
240
241
242
243
}

// Tests that DefaultValue<T> can be set and then unset.
TEST(DefaultValueTest, CanBeSetAndUnset) {
244
  EXPECT_TRUE(DefaultValue<int>::Exists());
245
  EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::Exists());
246

247
  DefaultValue<int>::Set(1);
248
249
  DefaultValue<const MyNonDefaultConstructible>::Set(
      MyNonDefaultConstructible(42));
250
251

  EXPECT_EQ(1, DefaultValue<int>::Get());
252
  EXPECT_EQ(42, DefaultValue<const MyNonDefaultConstructible>::Get().value());
253

254
  EXPECT_TRUE(DefaultValue<int>::Exists());
255
  EXPECT_TRUE(DefaultValue<const MyNonDefaultConstructible>::Exists());
256

257
  DefaultValue<int>::Clear();
258
  DefaultValue<const MyNonDefaultConstructible>::Clear();
259
260

  EXPECT_FALSE(DefaultValue<int>::IsSet());
261
  EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::IsSet());
262
263

  EXPECT_TRUE(DefaultValue<int>::Exists());
264
  EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::Exists());
265
266
267
268
269
270
271
}

// Tests that DefaultValue<T>::Get() returns the
// BuiltInDefaultValue<T>::Get() when DefaultValue<T>::IsSet() is
// false.
TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
  EXPECT_FALSE(DefaultValue<int>::IsSet());
272
  EXPECT_TRUE(DefaultValue<int>::Exists());
273
274
  EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible>::IsSet());
  EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible>::Exists());
275
276
277

  EXPECT_EQ(0, DefaultValue<int>::Get());

278
279
  EXPECT_DEATH_IF_SUPPORTED({ DefaultValue<MyNonDefaultConstructible>::Get(); },
                            "");
280
281
}

282
283
TEST(DefaultValueTest, GetWorksForMoveOnlyIfSet) {
  EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Exists());
284
  EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Get() == nullptr);
285
286
  DefaultValue<std::unique_ptr<int>>::SetFactory(
      [] { return std::unique_ptr<int>(new int(42)); });
287
288
289
290
291
  EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Exists());
  std::unique_ptr<int> i = DefaultValue<std::unique_ptr<int>>::Get();
  EXPECT_EQ(42, *i);
}

292
// Tests that DefaultValue<void>::Get() returns void.
293
TEST(DefaultValueTest, GetWorksForVoid) { return DefaultValue<void>::Get(); }
294
295
296
297
298
299

// Tests using DefaultValue with a reference type.

// Tests that DefaultValue<T&>::IsSet() is false initially.
TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) {
  EXPECT_FALSE(DefaultValue<int&>::IsSet());
300
301
  EXPECT_FALSE(DefaultValue<MyDefaultConstructible&>::IsSet());
  EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
302
303
}

304
305
306
// Tests that DefaultValue<T&>::Exists is false initiallly.
TEST(DefaultValueOfReferenceTest, IsInitiallyNotExisting) {
  EXPECT_FALSE(DefaultValue<int&>::Exists());
307
308
  EXPECT_FALSE(DefaultValue<MyDefaultConstructible&>::Exists());
  EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::Exists());
309
310
}

311
312
313
314
// Tests that DefaultValue<T&> can be set and then unset.
TEST(DefaultValueOfReferenceTest, CanBeSetAndUnset) {
  int n = 1;
  DefaultValue<const int&>::Set(n);
315
316
  MyNonDefaultConstructible x(42);
  DefaultValue<MyNonDefaultConstructible&>::Set(x);
317

318
  EXPECT_TRUE(DefaultValue<const int&>::Exists());
319
  EXPECT_TRUE(DefaultValue<MyNonDefaultConstructible&>::Exists());
320

321
  EXPECT_EQ(&n, &(DefaultValue<const int&>::Get()));
322
  EXPECT_EQ(&x, &(DefaultValue<MyNonDefaultConstructible&>::Get()));
323
324

  DefaultValue<const int&>::Clear();
325
  DefaultValue<MyNonDefaultConstructible&>::Clear();
326

327
  EXPECT_FALSE(DefaultValue<const int&>::Exists());
328
  EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::Exists());
329

330
  EXPECT_FALSE(DefaultValue<const int&>::IsSet());
331
  EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
332
333
334
335
336
337
338
}

// Tests that DefaultValue<T&>::Get() returns the
// BuiltInDefaultValue<T&>::Get() when DefaultValue<T&>::IsSet() is
// false.
TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
  EXPECT_FALSE(DefaultValue<int&>::IsSet());
339
  EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
340

341
342
343
  EXPECT_DEATH_IF_SUPPORTED({ DefaultValue<int&>::Get(); }, "");
  EXPECT_DEATH_IF_SUPPORTED({ DefaultValue<MyNonDefaultConstructible>::Get(); },
                            "");
344
345
346
347
348
}

// Tests that ActionInterface can be implemented by defining the
// Perform method.

349
typedef int MyGlobalFunction(bool, int);
350

351
class MyActionImpl : public ActionInterface<MyGlobalFunction> {
352
 public:
Abseil Team's avatar
Abseil Team committed
353
  int Perform(const std::tuple<bool, int>& args) override {
Abseil Team's avatar
Abseil Team committed
354
    return std::get<0>(args) ? std::get<1>(args) : 0;
355
356
357
358
359
  }
};

TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) {
  MyActionImpl my_action_impl;
360
  (void)my_action_impl;
361
362
363
}

TEST(ActionInterfaceTest, MakeAction) {
364
  Action<MyGlobalFunction> action = MakeAction(new MyActionImpl);
365
366
367
368
369

  // When exercising the Perform() method of Action<F>, we must pass
  // it a tuple whose size and type are compatible with F's argument
  // types.  For example, if F is int(), then Perform() takes a
  // 0-tuple; if F is void(bool, int), then Perform() takes a
Abseil Team's avatar
Abseil Team committed
370
371
  // std::tuple<bool, int>, and so on.
  EXPECT_EQ(5, action.Perform(std::make_tuple(true, 5)));
372
373
}

slowy07's avatar
slowy07 committed
374
// Tests that Action<F> can be constructed from a pointer to
375
376
// ActionInterface<F>.
TEST(ActionTest, CanBeConstructedFromActionInterface) {
377
  Action<MyGlobalFunction> action(new MyActionImpl);
378
379
380
381
}

// Tests that Action<F> delegates actual work to ActionInterface<F>.
TEST(ActionTest, DelegatesWorkToActionInterface) {
382
  const Action<MyGlobalFunction> action(new MyActionImpl);
383

Abseil Team's avatar
Abseil Team committed
384
385
  EXPECT_EQ(5, action.Perform(std::make_tuple(true, 5)));
  EXPECT_EQ(0, action.Perform(std::make_tuple(false, 1)));
386
387
388
389
}

// Tests that Action<F> can be copied.
TEST(ActionTest, IsCopyable) {
390
391
  Action<MyGlobalFunction> a1(new MyActionImpl);
  Action<MyGlobalFunction> a2(a1);  // Tests the copy constructor.
392
393

  // a1 should continue to work after being copied from.
Abseil Team's avatar
Abseil Team committed
394
395
  EXPECT_EQ(5, a1.Perform(std::make_tuple(true, 5)));
  EXPECT_EQ(0, a1.Perform(std::make_tuple(false, 1)));
396
397

  // a2 should work like the action it was copied from.
Abseil Team's avatar
Abseil Team committed
398
399
  EXPECT_EQ(5, a2.Perform(std::make_tuple(true, 5)));
  EXPECT_EQ(0, a2.Perform(std::make_tuple(false, 1)));
400
401
402
403

  a2 = a1;  // Tests the assignment operator.

  // a1 should continue to work after being copied from.
Abseil Team's avatar
Abseil Team committed
404
405
  EXPECT_EQ(5, a1.Perform(std::make_tuple(true, 5)));
  EXPECT_EQ(0, a1.Perform(std::make_tuple(false, 1)));
406
407

  // a2 should work like the action it was copied from.
Abseil Team's avatar
Abseil Team committed
408
409
  EXPECT_EQ(5, a2.Perform(std::make_tuple(true, 5)));
  EXPECT_EQ(0, a2.Perform(std::make_tuple(false, 1)));
410
411
412
413
414
415
416
}

// Tests that an Action<From> object can be converted to a
// compatible Action<To> object.

class IsNotZero : public ActionInterface<bool(int)> {  // NOLINT
 public:
Abseil Team's avatar
Abseil Team committed
417
  bool Perform(const std::tuple<int>& arg) override {
Abseil Team's avatar
Abseil Team committed
418
    return std::get<0>(arg) != 0;
419
420
421
422
  }
};

TEST(ActionTest, CanBeConvertedToOtherActionType) {
423
  const Action<bool(int)> a1(new IsNotZero);           // NOLINT
424
  const Action<int(char)> a2 = Action<int(char)>(a1);  // NOLINT
Abseil Team's avatar
Abseil Team committed
425
426
  EXPECT_EQ(1, a2.Perform(std::make_tuple('a')));
  EXPECT_EQ(0, a2.Perform(std::make_tuple('\0')));
427
428
429
430
431
432
433
434
435
436
437
438
}

// The following two classes are for testing MakePolymorphicAction().

// Implements a polymorphic action that returns the second of the
// arguments it receives.
class ReturnSecondArgumentAction {
 public:
  // We want to verify that MakePolymorphicAction() can work with a
  // polymorphic action whose Perform() method template is either
  // const or not.  This lets us verify the non-const case.
  template <typename Result, typename ArgumentTuple>
Abseil Team's avatar
Abseil Team committed
439
440
441
  Result Perform(const ArgumentTuple& args) {
    return std::get<1>(args);
  }
442
443
444
445
446
447
448
449
450
451
452
453
454
455
};

// Implements a polymorphic action that can be used in a nullary
// function to return 0.
class ReturnZeroFromNullaryFunctionAction {
 public:
  // For testing that MakePolymorphicAction() works when the
  // implementation class' Perform() method template takes only one
  // template parameter.
  //
  // We want to verify that MakePolymorphicAction() can work with a
  // polymorphic action whose Perform() method template is either
  // const or not.  This lets us verify the const case.
  template <typename Result>
Abseil Team's avatar
Abseil Team committed
456
457
458
  Result Perform(const std::tuple<>&) const {
    return 0;
  }
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
};

// These functions verify that MakePolymorphicAction() returns a
// PolymorphicAction<T> where T is the argument's type.

PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() {
  return MakePolymorphicAction(ReturnSecondArgumentAction());
}

PolymorphicAction<ReturnZeroFromNullaryFunctionAction>
ReturnZeroFromNullaryFunction() {
  return MakePolymorphicAction(ReturnZeroFromNullaryFunctionAction());
}

// Tests that MakePolymorphicAction() turns a polymorphic action
// implementation class into a polymorphic action.
TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) {
  Action<int(bool, int, double)> a1 = ReturnSecondArgument();  // NOLINT
Abseil Team's avatar
Abseil Team committed
477
  EXPECT_EQ(5, a1.Perform(std::make_tuple(false, 5, 2.0)));
478
479
480
481
482
483
}

// Tests that MakePolymorphicAction() works when the implementation
// class' Perform() method template has only one template parameter.
TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) {
  Action<int()> a1 = ReturnZeroFromNullaryFunction();
Abseil Team's avatar
Abseil Team committed
484
  EXPECT_EQ(0, a1.Perform(std::make_tuple()));
485
486

  Action<void*()> a2 = ReturnZeroFromNullaryFunction();
Abseil Team's avatar
Abseil Team committed
487
  EXPECT_TRUE(a2.Perform(std::make_tuple()) == nullptr);
488
489
490
491
492
493
}

// Tests that Return() works as an action for void-returning
// functions.
TEST(ReturnTest, WorksForVoid) {
  const Action<void(int)> ret = Return();  // NOLINT
Abseil Team's avatar
Abseil Team committed
494
  return ret.Perform(std::make_tuple(1));
495
496
497
498
499
}

// Tests that Return(v) returns v.
TEST(ReturnTest, ReturnsGivenValue) {
  Action<int()> ret = Return(1);  // NOLINT
Abseil Team's avatar
Abseil Team committed
500
  EXPECT_EQ(1, ret.Perform(std::make_tuple()));
501
502

  ret = Return(-5);
Abseil Team's avatar
Abseil Team committed
503
  EXPECT_EQ(-5, ret.Perform(std::make_tuple()));
504
505
506
507
508
}

// Tests that Return("string literal") works.
TEST(ReturnTest, AcceptsStringLiteral) {
  Action<const char*()> a1 = Return("Hello");
Abseil Team's avatar
Abseil Team committed
509
  EXPECT_STREQ("Hello", a1.Perform(std::make_tuple()));
510
511

  Action<std::string()> a2 = Return("world");
Abseil Team's avatar
Abseil Team committed
512
  EXPECT_EQ("world", a2.Perform(std::make_tuple()));
513
514
}

515
516
517
// Test struct which wraps a vector of integers. Used in
// 'SupportsWrapperReturnType' test.
struct IntegerVectorWrapper {
518
  std::vector<int>* v;
519
520
521
522
523
524
525
526
527
528
529
530
  IntegerVectorWrapper(std::vector<int>& _v) : v(&_v) {}  // NOLINT
};

// Tests that Return() works when return type is a wrapper type.
TEST(ReturnTest, SupportsWrapperReturnType) {
  // Initialize vector of integers.
  std::vector<int> v;
  for (int i = 0; i < 5; ++i) v.push_back(i);

  // Return() called with 'v' as argument. The Action will return the same data
  // as 'v' (copy) but it will be wrapped in an IntegerVectorWrapper.
  Action<IntegerVectorWrapper()> a = Return(v);
Abseil Team's avatar
Abseil Team committed
531
  const std::vector<int>& result = *(a.Perform(std::make_tuple()).v);
532
533
534
  EXPECT_THAT(result, ::testing::ElementsAre(0, 1, 2, 3, 4));
}

535
536
537
538
539
540
541
542
543
544
545
546
547
548
// Tests that Return(v) is covaraint.

struct Base {
  bool operator==(const Base&) { return true; }
};

struct Derived : public Base {
  bool operator==(const Derived&) { return true; }
};

TEST(ReturnTest, IsCovariant) {
  Base base;
  Derived derived;
  Action<Base*()> ret = Return(&base);
Abseil Team's avatar
Abseil Team committed
549
  EXPECT_EQ(&base, ret.Perform(std::make_tuple()));
550
551

  ret = Return(&derived);
Abseil Team's avatar
Abseil Team committed
552
  EXPECT_EQ(&derived, ret.Perform(std::make_tuple()));
553
554
}

555
556
557
558
559
560
// Tests that the type of the value passed into Return is converted into T
// when the action is cast to Action<T(...)> rather than when the action is
// performed. See comments on testing::internal::ReturnAction in
// gmock-actions.h for more information.
class FromType {
 public:
561
  explicit FromType(bool* is_converted) : converted_(is_converted) {}
562
563
564
565
566
567
568
569
  bool* converted() const { return converted_; }

 private:
  bool* const converted_;
};

class ToType {
 public:
570
571
  // Must allow implicit conversion due to use in ImplicitCast_<T>.
  ToType(const FromType& x) { *x.converted() = true; }  // NOLINT
572
573
574
575
576
577
578
579
580
};

TEST(ReturnTest, ConvertsArgumentWhenConverted) {
  bool converted = false;
  FromType x(&converted);
  Action<ToType()> action(Return(x));
  EXPECT_TRUE(converted) << "Return must convert its argument in its own "
                         << "conversion operator.";
  converted = false;
Abseil Team's avatar
Abseil Team committed
581
  action.Perform(std::tuple<>());
582
  EXPECT_FALSE(converted) << "Action must NOT convert its argument "
583
                          << "when performed.";
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
}

class DestinationType {};

class SourceType {
 public:
  // Note: a non-const typecast operator.
  operator DestinationType() { return DestinationType(); }
};

TEST(ReturnTest, CanConvertArgumentUsingNonConstTypeCastOperator) {
  SourceType s;
  Action<DestinationType()> action(Return(s));
}

599
600
601
// Tests that ReturnNull() returns NULL in a pointer-returning function.
TEST(ReturnNullTest, WorksInPointerReturningFunction) {
  const Action<int*()> a1 = ReturnNull();
Abseil Team's avatar
Abseil Team committed
602
  EXPECT_TRUE(a1.Perform(std::make_tuple()) == nullptr);
603
604

  const Action<const char*(bool)> a2 = ReturnNull();  // NOLINT
Abseil Team's avatar
Abseil Team committed
605
  EXPECT_TRUE(a2.Perform(std::make_tuple(true)) == nullptr);
606
607
}

608
609
610
611
// Tests that ReturnNull() returns NULL for shared_ptr and unique_ptr returning
// functions.
TEST(ReturnNullTest, WorksInSmartPointerReturningFunction) {
  const Action<std::unique_ptr<const int>()> a1 = ReturnNull();
Abseil Team's avatar
Abseil Team committed
612
  EXPECT_TRUE(a1.Perform(std::make_tuple()) == nullptr);
613
614

  const Action<std::shared_ptr<int>(std::string)> a2 = ReturnNull();
Abseil Team's avatar
Abseil Team committed
615
  EXPECT_TRUE(a2.Perform(std::make_tuple("foo")) == nullptr);
616
617
}

618
619
620
621
622
// Tests that ReturnRef(v) works for reference types.
TEST(ReturnRefTest, WorksForReference) {
  const int n = 0;
  const Action<const int&(bool)> ret = ReturnRef(n);  // NOLINT

Abseil Team's avatar
Abseil Team committed
623
  EXPECT_EQ(&n, &ret.Perform(std::make_tuple(true)));
624
625
626
627
628
629
630
}

// Tests that ReturnRef(v) is covariant.
TEST(ReturnRefTest, IsCovariant) {
  Base base;
  Derived derived;
  Action<Base&()> a = ReturnRef(base);
Abseil Team's avatar
Abseil Team committed
631
  EXPECT_EQ(&base, &a.Perform(std::make_tuple()));
632
633

  a = ReturnRef(derived);
Abseil Team's avatar
Abseil Team committed
634
  EXPECT_EQ(&derived, &a.Perform(std::make_tuple()));
635
636
}

637
template <typename T, typename = decltype(ReturnRef(std::declval<T&&>()))>
638
639
640
bool CanCallReturnRef(T&&) {
  return true;
}
641
bool CanCallReturnRef(Unused) { return false; }
642
643

// Tests that ReturnRef(v) is working with non-temporaries (T&)
644
TEST(ReturnRefTest, WorksForNonTemporary) {
645
646
  int scalar_value = 123;
  EXPECT_TRUE(CanCallReturnRef(scalar_value));
647

648
649
  std::string non_scalar_value("ABC");
  EXPECT_TRUE(CanCallReturnRef(non_scalar_value));
650

651
652
  const int const_scalar_value{321};
  EXPECT_TRUE(CanCallReturnRef(const_scalar_value));
653

654
655
  const std::string const_non_scalar_value("CBA");
  EXPECT_TRUE(CanCallReturnRef(const_non_scalar_value));
656
657
658
}

// Tests that ReturnRef(v) is not working with temporaries (T&&)
659
TEST(ReturnRefTest, DoesNotWorkForTemporary) {
660
  auto scalar_value = []() -> int { return 123; };
661
  EXPECT_FALSE(CanCallReturnRef(scalar_value()));
662

663
664
  auto non_scalar_value = []() -> std::string { return "ABC"; };
  EXPECT_FALSE(CanCallReturnRef(non_scalar_value()));
665

Piotr Nycz's avatar
Piotr Nycz committed
666
667
  // cannot use here callable returning "const scalar type",
  // because such const for scalar return type is ignored
668
  EXPECT_FALSE(CanCallReturnRef(static_cast<const int>(321)));
669

670
671
  auto const_non_scalar_value = []() -> const std::string { return "CBA"; };
  EXPECT_FALSE(CanCallReturnRef(const_non_scalar_value()));
672
}
673

674
675
676
677
678
// Tests that ReturnRefOfCopy(v) works for reference types.
TEST(ReturnRefOfCopyTest, WorksForReference) {
  int n = 42;
  const Action<const int&()> ret = ReturnRefOfCopy(n);

Abseil Team's avatar
Abseil Team committed
679
680
  EXPECT_NE(&n, &ret.Perform(std::make_tuple()));
  EXPECT_EQ(42, ret.Perform(std::make_tuple()));
681
682

  n = 43;
Abseil Team's avatar
Abseil Team committed
683
684
  EXPECT_NE(&n, &ret.Perform(std::make_tuple()));
  EXPECT_EQ(42, ret.Perform(std::make_tuple()));
685
686
687
688
689
690
691
}

// Tests that ReturnRefOfCopy(v) is covariant.
TEST(ReturnRefOfCopyTest, IsCovariant) {
  Base base;
  Derived derived;
  Action<Base&()> a = ReturnRefOfCopy(base);
Abseil Team's avatar
Abseil Team committed
692
  EXPECT_NE(&base, &a.Perform(std::make_tuple()));
693
694

  a = ReturnRefOfCopy(derived);
Abseil Team's avatar
Abseil Team committed
695
  EXPECT_NE(&derived, &a.Perform(std::make_tuple()));
696
697
}

Abseil Team's avatar
Abseil Team committed
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
// Tests that ReturnRoundRobin(v) works with initializer lists
TEST(ReturnRoundRobinTest, WorksForInitList) {
  Action<int()> ret = ReturnRoundRobin({1, 2, 3});

  EXPECT_EQ(1, ret.Perform(std::make_tuple()));
  EXPECT_EQ(2, ret.Perform(std::make_tuple()));
  EXPECT_EQ(3, ret.Perform(std::make_tuple()));
  EXPECT_EQ(1, ret.Perform(std::make_tuple()));
  EXPECT_EQ(2, ret.Perform(std::make_tuple()));
  EXPECT_EQ(3, ret.Perform(std::make_tuple()));
}

// Tests that ReturnRoundRobin(v) works with vectors
TEST(ReturnRoundRobinTest, WorksForVector) {
  std::vector<double> v = {4.4, 5.5, 6.6};
  Action<double()> ret = ReturnRoundRobin(v);

  EXPECT_EQ(4.4, ret.Perform(std::make_tuple()));
  EXPECT_EQ(5.5, ret.Perform(std::make_tuple()));
  EXPECT_EQ(6.6, ret.Perform(std::make_tuple()));
  EXPECT_EQ(4.4, ret.Perform(std::make_tuple()));
  EXPECT_EQ(5.5, ret.Perform(std::make_tuple()));
  EXPECT_EQ(6.6, ret.Perform(std::make_tuple()));
}

723
724
725
726
// Tests that DoDefault() does the default action for the mock method.

class MockClass {
 public:
727
728
  MockClass() {}

729
  MOCK_METHOD1(IntFunc, int(bool flag));  // NOLINT
730
  MOCK_METHOD0(Foo, MyNonDefaultConstructible());
731
  MOCK_METHOD0(MakeUnique, std::unique_ptr<int>());
732
  MOCK_METHOD0(MakeUniqueBase, std::unique_ptr<Base>());
733
  MOCK_METHOD0(MakeVectorUnique, std::vector<std::unique_ptr<int>>());
Gennadiy Civil's avatar
Gennadiy Civil committed
734
  MOCK_METHOD1(TakeUnique, int(std::unique_ptr<int>));
Gennadiy Civil's avatar
 
Gennadiy Civil committed
735
736
  MOCK_METHOD2(TakeUnique,
               int(const std::unique_ptr<int>&, std::unique_ptr<int>));
737
738
739

 private:
  GTEST_DISALLOW_COPY_AND_ASSIGN_(MockClass);
740
741
742
743
744
745
};

// Tests that DoDefault() returns the built-in default value for the
// return type by default.
TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) {
  MockClass mock;
746
  EXPECT_CALL(mock, IntFunc(_)).WillOnce(DoDefault());
747
748
749
  EXPECT_EQ(0, mock.IntFunc(true));
}

750
751
// Tests that DoDefault() throws (when exceptions are enabled) or aborts
// the process when there is no built-in default value for the return type.
752
753
TEST(DoDefaultDeathTest, DiesForUnknowType) {
  MockClass mock;
754
  EXPECT_CALL(mock, Foo()).WillRepeatedly(DoDefault());
755
756
757
#if GTEST_HAS_EXCEPTIONS
  EXPECT_ANY_THROW(mock.Foo());
#else
758
  EXPECT_DEATH_IF_SUPPORTED({ mock.Foo(); }, "");
759
#endif
760
761
762
763
764
}

// Tests that using DoDefault() inside a composite action leads to a
// run-time error.

765
void VoidFunc(bool /* flag */) {}
766
767
768
769

TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
  MockClass mock;
  EXPECT_CALL(mock, IntFunc(_))
770
      .WillRepeatedly(DoAll(Invoke(VoidFunc), DoDefault()));
771
772
773
774
775

  // Ideally we should verify the error message as well.  Sadly,
  // EXPECT_DEATH() can only capture stderr, while Google Mock's
  // errors are printed on stdout.  Therefore we have to settle for
  // not verifying the message.
776
  EXPECT_DEATH_IF_SUPPORTED({ mock.IntFunc(true); }, "");
777
778
779
}

// Tests that DoDefault() returns the default value set by
John Bampton's avatar
John Bampton committed
780
// DefaultValue<T>::Set() when it's not overridden by an ON_CALL().
781
782
783
TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) {
  DefaultValue<int>::Set(1);
  MockClass mock;
784
  EXPECT_CALL(mock, IntFunc(_)).WillOnce(DoDefault());
785
786
787
788
789
790
791
  EXPECT_EQ(1, mock.IntFunc(false));
  DefaultValue<int>::Clear();
}

// Tests that DoDefault() does the action specified by ON_CALL().
TEST(DoDefaultTest, DoesWhatOnCallSpecifies) {
  MockClass mock;
792
793
  ON_CALL(mock, IntFunc(_)).WillByDefault(Return(2));
  EXPECT_CALL(mock, IntFunc(_)).WillOnce(DoDefault());
794
795
796
797
798
799
  EXPECT_EQ(2, mock.IntFunc(false));
}

// Tests that using DoDefault() in ON_CALL() leads to a run-time failure.
TEST(DoDefaultTest, CannotBeUsedInOnCall) {
  MockClass mock;
800
801
802
803
804
  EXPECT_NONFATAL_FAILURE(
      {  // NOLINT
        ON_CALL(mock, IntFunc(_)).WillByDefault(DoDefault());
      },
      "DoDefault() cannot be used in ON_CALL()");
805
806
}

807
808
809
810
811
812
813
814
// Tests that SetArgPointee<N>(v) sets the variable pointed to by
// the N-th (0-based) argument to v.
TEST(SetArgPointeeTest, SetsTheNthPointee) {
  typedef void MyFunction(bool, int*, char*);
  Action<MyFunction> a = SetArgPointee<1>(2);

  int n = 0;
  char ch = '\0';
Abseil Team's avatar
Abseil Team committed
815
  a.Perform(std::make_tuple(true, &n, &ch));
816
817
818
819
820
821
  EXPECT_EQ(2, n);
  EXPECT_EQ('\0', ch);

  a = SetArgPointee<2>('a');
  n = 0;
  ch = '\0';
Abseil Team's avatar
Abseil Team committed
822
  a.Perform(std::make_tuple(true, &n, &ch));
823
824
825
826
  EXPECT_EQ(0, n);
  EXPECT_EQ('a', ch);
}

827
828
// Tests that SetArgPointee<N>() accepts a string literal.
TEST(SetArgPointeeTest, AcceptsStringLiteral) {
829
830
  typedef void MyFunction(std::string*, const char**);
  Action<MyFunction> a = SetArgPointee<0>("hi");
831
  std::string str;
832
  const char* ptr = nullptr;
Abseil Team's avatar
Abseil Team committed
833
  a.Perform(std::make_tuple(&str, &ptr));
834
  EXPECT_EQ("hi", str);
835
  EXPECT_TRUE(ptr == nullptr);
836

837
  a = SetArgPointee<1>("world");
838
  str = "";
Abseil Team's avatar
Abseil Team committed
839
  a.Perform(std::make_tuple(&str, &ptr));
840
841
842
843
  EXPECT_EQ("", str);
  EXPECT_STREQ("world", ptr);
}

844
845
846
TEST(SetArgPointeeTest, AcceptsWideStringLiteral) {
  typedef void MyFunction(const wchar_t**);
  Action<MyFunction> a = SetArgPointee<0>(L"world");
847
  const wchar_t* ptr = nullptr;
Abseil Team's avatar
Abseil Team committed
848
  a.Perform(std::make_tuple(&ptr));
849
850
  EXPECT_STREQ(L"world", ptr);

851
#if GTEST_HAS_STD_WSTRING
852
853
854
855

  typedef void MyStringFunction(std::wstring*);
  Action<MyStringFunction> a2 = SetArgPointee<0>(L"world");
  std::wstring str = L"";
Abseil Team's avatar
Abseil Team committed
856
  a2.Perform(std::make_tuple(&str));
857
858
  EXPECT_EQ(L"world", str);

859
#endif
860
861
}

862
863
864
865
866
867
// Tests that SetArgPointee<N>() accepts a char pointer.
TEST(SetArgPointeeTest, AcceptsCharPointer) {
  typedef void MyFunction(bool, std::string*, const char**);
  const char* const hi = "hi";
  Action<MyFunction> a = SetArgPointee<1>(hi);
  std::string str;
868
  const char* ptr = nullptr;
Abseil Team's avatar
Abseil Team committed
869
  a.Perform(std::make_tuple(true, &str, &ptr));
870
  EXPECT_EQ("hi", str);
871
  EXPECT_TRUE(ptr == nullptr);
872
873
874
875
876

  char world_array[] = "world";
  char* const world = world_array;
  a = SetArgPointee<2>(world);
  str = "";
Abseil Team's avatar
Abseil Team committed
877
  a.Perform(std::make_tuple(true, &str, &ptr));
878
879
880
881
  EXPECT_EQ("", str);
  EXPECT_EQ(world, ptr);
}

882
883
884
885
TEST(SetArgPointeeTest, AcceptsWideCharPointer) {
  typedef void MyFunction(bool, const wchar_t**);
  const wchar_t* const hi = L"hi";
  Action<MyFunction> a = SetArgPointee<1>(hi);
886
  const wchar_t* ptr = nullptr;
Abseil Team's avatar
Abseil Team committed
887
  a.Perform(std::make_tuple(true, &ptr));
888
889
  EXPECT_EQ(hi, ptr);

890
#if GTEST_HAS_STD_WSTRING
891
892
893
894
895
896

  typedef void MyStringFunction(bool, std::wstring*);
  wchar_t world_array[] = L"world";
  wchar_t* const world = world_array;
  Action<MyStringFunction> a2 = SetArgPointee<1>(world);
  std::wstring str;
Abseil Team's avatar
Abseil Team committed
897
  a2.Perform(std::make_tuple(true, &str));
898
  EXPECT_EQ(world_array, str);
899
#endif
900
901
}

902
903
904
905
906
907
908
909
// Tests that SetArgumentPointee<N>(v) sets the variable pointed to by
// the N-th (0-based) argument to v.
TEST(SetArgumentPointeeTest, SetsTheNthPointee) {
  typedef void MyFunction(bool, int*, char*);
  Action<MyFunction> a = SetArgumentPointee<1>(2);

  int n = 0;
  char ch = '\0';
Abseil Team's avatar
Abseil Team committed
910
  a.Perform(std::make_tuple(true, &n, &ch));
911
912
913
914
915
916
  EXPECT_EQ(2, n);
  EXPECT_EQ('\0', ch);

  a = SetArgumentPointee<2>('a');
  n = 0;
  ch = '\0';
Abseil Team's avatar
Abseil Team committed
917
  a.Perform(std::make_tuple(true, &n, &ch));
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
  EXPECT_EQ(0, n);
  EXPECT_EQ('a', ch);
}

// Sample functions and functors for testing Invoke() and etc.
int Nullary() { return 1; }

class NullaryFunctor {
 public:
  int operator()() { return 2; }
};

bool g_done = false;
void VoidNullary() { g_done = true; }

class VoidNullaryFunctor {
 public:
  void operator()() { g_done = true; }
};

Abseil Team's avatar
Abseil Team committed
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
short Short(short n) { return n; }  // NOLINT
char Char(char ch) { return ch; }

const char* CharPtr(const char* s) { return s; }

bool Unary(int x) { return x < 0; }

const char* Binary(const char* input, short n) { return input + n; }  // NOLINT

void VoidBinary(int, char) { g_done = true; }

int Ternary(int x, char y, short z) { return x + y + z; }  // NOLINT

int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }

953
954
955
956
957
class Foo {
 public:
  Foo() : value_(123) {}

  int Nullary() const { return value_; }
958

959
960
961
962
963
964
965
966
 private:
  int value_;
};

// Tests InvokeWithoutArgs(function).
TEST(InvokeWithoutArgsTest, Function) {
  // As an action that takes one argument.
  Action<int(int)> a = InvokeWithoutArgs(Nullary);  // NOLINT
Abseil Team's avatar
Abseil Team committed
967
  EXPECT_EQ(1, a.Perform(std::make_tuple(2)));
968
969

  // As an action that takes two arguments.
970
  Action<int(int, double)> a2 = InvokeWithoutArgs(Nullary);  // NOLINT
Abseil Team's avatar
Abseil Team committed
971
  EXPECT_EQ(1, a2.Perform(std::make_tuple(2, 3.5)));
972
973
974
975

  // As an action that returns void.
  Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary);  // NOLINT
  g_done = false;
Abseil Team's avatar
Abseil Team committed
976
  a3.Perform(std::make_tuple(1));
977
978
979
980
981
982
983
  EXPECT_TRUE(g_done);
}

// Tests InvokeWithoutArgs(functor).
TEST(InvokeWithoutArgsTest, Functor) {
  // As an action that takes no argument.
  Action<int()> a = InvokeWithoutArgs(NullaryFunctor());  // NOLINT
Abseil Team's avatar
Abseil Team committed
984
  EXPECT_EQ(2, a.Perform(std::make_tuple()));
985
986

  // As an action that takes three arguments.
987
  Action<int(int, double, char)> a2 =  // NOLINT
988
      InvokeWithoutArgs(NullaryFunctor());
Abseil Team's avatar
Abseil Team committed
989
  EXPECT_EQ(2, a2.Perform(std::make_tuple(3, 3.5, 'a')));
990
991
992
993

  // As an action that returns void.
  Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor());
  g_done = false;
Abseil Team's avatar
Abseil Team committed
994
  a3.Perform(std::make_tuple());
995
996
997
998
999
1000
1001
1002
  EXPECT_TRUE(g_done);
}

// Tests InvokeWithoutArgs(obj_ptr, method).
TEST(InvokeWithoutArgsTest, Method) {
  Foo foo;
  Action<int(bool, char)> a =  // NOLINT
      InvokeWithoutArgs(&foo, &Foo::Nullary);
Abseil Team's avatar
Abseil Team committed
1003
  EXPECT_EQ(123, a.Perform(std::make_tuple(true, 'a')));
1004
1005
1006
1007
1008
}

// Tests using IgnoreResult() on a polymorphic action.
TEST(IgnoreResultTest, PolymorphicAction) {
  Action<void(int)> a = IgnoreResult(Return(5));  // NOLINT
Abseil Team's avatar
Abseil Team committed
1009
  a.Perform(std::make_tuple(1));
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
}

// Tests using IgnoreResult() on a monomorphic action.

int ReturnOne() {
  g_done = true;
  return 1;
}

TEST(IgnoreResultTest, MonomorphicAction) {
  g_done = false;
  Action<void()> a = IgnoreResult(Invoke(ReturnOne));
Abseil Team's avatar
Abseil Team committed
1022
  a.Perform(std::make_tuple());
1023
1024
1025
1026
1027
  EXPECT_TRUE(g_done);
}

// Tests using IgnoreResult() on an action that returns a class type.

1028
MyNonDefaultConstructible ReturnMyNonDefaultConstructible(double /* x */) {
1029
  g_done = true;
1030
  return MyNonDefaultConstructible(42);
1031
1032
1033
1034
}

TEST(IgnoreResultTest, ActionReturningClass) {
  g_done = false;
1035
1036
  Action<void(int)> a =
      IgnoreResult(Invoke(ReturnMyNonDefaultConstructible));  // NOLINT
Abseil Team's avatar
Abseil Team committed
1037
  a.Perform(std::make_tuple(2));
1038
1039
1040
1041
1042
1043
  EXPECT_TRUE(g_done);
}

TEST(AssignTest, Int) {
  int x = 0;
  Action<void(int)> a = Assign(&x, 5);
Abseil Team's avatar
Abseil Team committed
1044
  a.Perform(std::make_tuple(0));
1045
1046
1047
1048
1049
1050
  EXPECT_EQ(5, x);
}

TEST(AssignTest, String) {
  ::std::string x;
  Action<void(void)> a = Assign(&x, "Hello, world");
Abseil Team's avatar
Abseil Team committed
1051
  a.Perform(std::make_tuple());
1052
1053
1054
1055
1056
1057
  EXPECT_EQ("Hello, world", x);
}

TEST(AssignTest, CompatibleTypes) {
  double x = 0;
  Action<void(int)> a = Assign(&x, 5);
Abseil Team's avatar
Abseil Team committed
1058
  a.Perform(std::make_tuple(0));
1059
1060
1061
  EXPECT_DOUBLE_EQ(5, x);
}

Abseil Team's avatar
Abseil Team committed
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
// Tests using WithArgs and with an action that takes 1 argument.
TEST(WithArgsTest, OneArg) {
  Action<bool(double x, int n)> a = WithArgs<1>(Invoke(Unary));  // NOLINT
  EXPECT_TRUE(a.Perform(std::make_tuple(1.5, -1)));
  EXPECT_FALSE(a.Perform(std::make_tuple(1.5, 1)));
}

// Tests using WithArgs with an action that takes 2 arguments.
TEST(WithArgsTest, TwoArgs) {
  Action<const char*(const char* s, double x, short n)> a =  // NOLINT
      WithArgs<0, 2>(Invoke(Binary));
  const char s[] = "Hello";
  EXPECT_EQ(s + 2, a.Perform(std::make_tuple(CharPtr(s), 0.5, Short(2))));
}

struct ConcatAll {
  std::string operator()() const { return {}; }
  template <typename... I>
  std::string operator()(const char* a, I... i) const {
    return a + ConcatAll()(i...);
  }
};

// Tests using WithArgs with an action that takes 10 arguments.
TEST(WithArgsTest, TenArgs) {
  Action<std::string(const char*, const char*, const char*, const char*)> a =
      WithArgs<0, 1, 2, 3, 2, 1, 0, 1, 2, 3>(Invoke(ConcatAll{}));
  EXPECT_EQ("0123210123",
            a.Perform(std::make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
                                      CharPtr("3"))));
}

// Tests using WithArgs with an action that is not Invoke().
class SubtractAction : public ActionInterface<int(int, int)> {
 public:
Abseil Team's avatar
Abseil Team committed
1097
  int Perform(const std::tuple<int, int>& args) override {
Abseil Team's avatar
Abseil Team committed
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
    return std::get<0>(args) - std::get<1>(args);
  }
};

TEST(WithArgsTest, NonInvokeAction) {
  Action<int(const std::string&, int, int)> a =
      WithArgs<2, 1>(MakeAction(new SubtractAction));
  std::tuple<std::string, int, int> dummy =
      std::make_tuple(std::string("hi"), 2, 10);
  EXPECT_EQ(8, a.Perform(dummy));
}

// Tests using WithArgs to pass all original arguments in the original order.
TEST(WithArgsTest, Identity) {
  Action<int(int x, char y, short z)> a =  // NOLINT
      WithArgs<0, 1, 2>(Invoke(Ternary));
  EXPECT_EQ(123, a.Perform(std::make_tuple(100, Char(20), Short(3))));
}

// Tests using WithArgs with repeated arguments.
TEST(WithArgsTest, RepeatedArguments) {
  Action<int(bool, int m, int n)> a =  // NOLINT
      WithArgs<1, 1, 1, 1>(Invoke(SumOf4));
  EXPECT_EQ(4, a.Perform(std::make_tuple(false, 1, 10)));
}

// Tests using WithArgs with reversed argument order.
TEST(WithArgsTest, ReversedArgumentOrder) {
  Action<const char*(short n, const char* input)> a =  // NOLINT
      WithArgs<1, 0>(Invoke(Binary));
  const char s[] = "Hello";
  EXPECT_EQ(s + 2, a.Perform(std::make_tuple(Short(2), CharPtr(s))));
}

// Tests using WithArgs with compatible, but not identical, argument types.
TEST(WithArgsTest, ArgsOfCompatibleTypes) {
  Action<long(short x, char y, double z, char c)> a =  // NOLINT
      WithArgs<0, 1, 3>(Invoke(Ternary));
  EXPECT_EQ(123,
            a.Perform(std::make_tuple(Short(100), Char(20), 5.6, Char(3))));
}

// Tests using WithArgs with an action that returns void.
TEST(WithArgsTest, VoidAction) {
  Action<void(double x, char c, int n)> a = WithArgs<2, 1>(Invoke(VoidBinary));
  g_done = false;
  a.Perform(std::make_tuple(1.5, 'a', 3));
  EXPECT_TRUE(g_done);
}

TEST(WithArgsTest, ReturnReference) {
misterg's avatar
misterg committed
1149
  Action<int&(int&, void*)> aa = WithArgs<0>([](int& a) -> int& { return a; });
Abseil Team's avatar
Abseil Team committed
1150
  int i = 0;
misterg's avatar
misterg committed
1151
  const int& res = aa.Perform(std::forward_as_tuple(i, nullptr));
Abseil Team's avatar
Abseil Team committed
1152
1153
1154
1155
1156
1157
1158
1159
1160
  EXPECT_EQ(&i, &res);
}

TEST(WithArgsTest, InnerActionWithConversion) {
  Action<Derived*()> inner = [] { return nullptr; };
  Action<Base*(double)> a = testing::WithoutArgs(inner);
  EXPECT_EQ(nullptr, a.Perform(std::make_tuple(1.1)));
}

1161
#if !GTEST_OS_WINDOWS_MOBILE
1162

1163
1164
class SetErrnoAndReturnTest : public testing::Test {
 protected:
Abseil Team's avatar
Abseil Team committed
1165
1166
  void SetUp() override { errno = 0; }
  void TearDown() override { errno = 0; }
1167
1168
1169
1170
};

TEST_F(SetErrnoAndReturnTest, Int) {
  Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5);
Abseil Team's avatar
Abseil Team committed
1171
  EXPECT_EQ(-5, a.Perform(std::make_tuple()));
1172
1173
1174
1175
1176
1177
  EXPECT_EQ(ENOTTY, errno);
}

TEST_F(SetErrnoAndReturnTest, Ptr) {
  int x;
  Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x);
Abseil Team's avatar
Abseil Team committed
1178
  EXPECT_EQ(&x, a.Perform(std::make_tuple()));
1179
1180
1181
1182
1183
  EXPECT_EQ(ENOTTY, errno);
}

TEST_F(SetErrnoAndReturnTest, CompatibleTypes) {
  Action<double()> a = SetErrnoAndReturn(EINVAL, 5);
Abseil Team's avatar
Abseil Team committed
1184
  EXPECT_DOUBLE_EQ(5.0, a.Perform(std::make_tuple()));
1185
1186
1187
  EXPECT_EQ(EINVAL, errno);
}

1188
#endif  // !GTEST_OS_WINDOWS_MOBILE
1189

1190
1191
// Tests ByRef().

Abseil Team's avatar
Abseil Team committed
1192
// Tests that the result of ByRef() is copyable.
1193
1194
1195
1196
TEST(ByRefTest, IsCopyable) {
  const std::string s1 = "Hi";
  const std::string s2 = "Hello";

Abseil Team's avatar
Abseil Team committed
1197
  auto ref_wrapper = ByRef(s1);
1198
1199
1200
1201
1202
1203
1204
1205
  const std::string& r1 = ref_wrapper;
  EXPECT_EQ(&s1, &r1);

  // Assigns a new value to ref_wrapper.
  ref_wrapper = ByRef(s2);
  const std::string& r2 = ref_wrapper;
  EXPECT_EQ(&s2, &r2);

Abseil Team's avatar
Abseil Team committed
1206
  auto ref_wrapper1 = ByRef(s1);
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
  // Copies ref_wrapper1 to ref_wrapper.
  ref_wrapper = ref_wrapper1;
  const std::string& r3 = ref_wrapper;
  EXPECT_EQ(&s1, &r3);
}

// Tests using ByRef() on a const value.
TEST(ByRefTest, ConstValue) {
  const int n = 0;
  // int& ref = ByRef(n);  // This shouldn't compile - we have a
1217
  // negative compilation test to catch it.
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
  const int& const_ref = ByRef(n);
  EXPECT_EQ(&n, &const_ref);
}

// Tests using ByRef() on a non-const value.
TEST(ByRefTest, NonConstValue) {
  int n = 0;

  // ByRef(n) can be used as either an int&,
  int& ref = ByRef(n);
  EXPECT_EQ(&n, &ref);

  // or a const int&.
  const int& const_ref = ByRef(n);
  EXPECT_EQ(&n, &const_ref);
}

// Tests explicitly specifying the type when using ByRef().
TEST(ByRefTest, ExplicitType) {
  int n = 0;
  const int& r1 = ByRef<const int>(n);
  EXPECT_EQ(&n, &r1);

  // ByRef<char>(n);  // This shouldn't compile - we have a negative
1242
  // compilation test to catch it.
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272

  Derived d;
  Derived& r2 = ByRef<Derived>(d);
  EXPECT_EQ(&d, &r2);

  const Derived& r3 = ByRef<const Derived>(d);
  EXPECT_EQ(&d, &r3);

  Base& r4 = ByRef<Base>(d);
  EXPECT_EQ(&d, &r4);

  const Base& r5 = ByRef<const Base>(d);
  EXPECT_EQ(&d, &r5);

  // The following shouldn't compile - we have a negative compilation
  // test for it.
  //
  // Base b;
  // ByRef<Derived>(b);
}

// Tests that Google Mock prints expression ByRef(x) as a reference to x.
TEST(ByRefTest, PrintsCorrectly) {
  int n = 42;
  ::std::stringstream expected, actual;
  testing::internal::UniversalPrinter<const int&>::Print(n, &expected);
  testing::internal::UniversalPrint(ByRef(n), &actual);
  EXPECT_EQ(expected.str(), actual.str());
}

Abseil Team's avatar
Abseil Team committed
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
struct UnaryConstructorClass {
  explicit UnaryConstructorClass(int v) : value(v) {}
  int value;
};

// Tests using ReturnNew() with a unary constructor.
TEST(ReturnNewTest, Unary) {
  Action<UnaryConstructorClass*()> a = ReturnNew<UnaryConstructorClass>(4000);
  UnaryConstructorClass* c = a.Perform(std::make_tuple());
  EXPECT_EQ(4000, c->value);
  delete c;
}

TEST(ReturnNewTest, UnaryWorksWhenMockMethodHasArgs) {
  Action<UnaryConstructorClass*(bool, int)> a =
      ReturnNew<UnaryConstructorClass>(4000);
  UnaryConstructorClass* c = a.Perform(std::make_tuple(false, 5));
  EXPECT_EQ(4000, c->value);
  delete c;
}

TEST(ReturnNewTest, UnaryWorksWhenMockMethodReturnsPointerToConst) {
  Action<const UnaryConstructorClass*()> a =
      ReturnNew<UnaryConstructorClass>(4000);
  const UnaryConstructorClass* c = a.Perform(std::make_tuple());
  EXPECT_EQ(4000, c->value);
  delete c;
}

class TenArgConstructorClass {
 public:
  TenArgConstructorClass(int a1, int a2, int a3, int a4, int a5, int a6, int a7,
                         int a8, int a9, int a10)
      : value_(a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10) {}
  int value_;
};

// Tests using ReturnNew() with a 10-argument constructor.
TEST(ReturnNewTest, ConstructorThatTakes10Arguments) {
  Action<TenArgConstructorClass*()> a = ReturnNew<TenArgConstructorClass>(
      1000000000, 200000000, 30000000, 4000000, 500000, 60000, 7000, 800, 90,
      0);
  TenArgConstructorClass* c = a.Perform(std::make_tuple());
  EXPECT_EQ(1234567890, c->value_);
  delete c;
}
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329

std::unique_ptr<int> UniquePtrSource() {
  return std::unique_ptr<int>(new int(19));
}

std::vector<std::unique_ptr<int>> VectorUniquePtrSource() {
  std::vector<std::unique_ptr<int>> out;
  out.emplace_back(new int(7));
  return out;
}

1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
TEST(MockMethodTest, CanReturnMoveOnlyValue_Return) {
  MockClass mock;
  std::unique_ptr<int> i(new int(19));
  EXPECT_CALL(mock, MakeUnique()).WillOnce(Return(ByMove(std::move(i))));
  EXPECT_CALL(mock, MakeVectorUnique())
      .WillOnce(Return(ByMove(VectorUniquePtrSource())));
  Derived* d = new Derived;
  EXPECT_CALL(mock, MakeUniqueBase())
      .WillOnce(Return(ByMove(std::unique_ptr<Derived>(d))));

  std::unique_ptr<int> result1 = mock.MakeUnique();
  EXPECT_EQ(19, *result1);

  std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
1344
  EXPECT_EQ(1u, vresult.size());
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
  EXPECT_NE(nullptr, vresult[0]);
  EXPECT_EQ(7, *vresult[0]);

  std::unique_ptr<Base> result2 = mock.MakeUniqueBase();
  EXPECT_EQ(d, result2.get());
}

TEST(MockMethodTest, CanReturnMoveOnlyValue_DoAllReturn) {
  testing::MockFunction<void()> mock_function;
  MockClass mock;
  std::unique_ptr<int> i(new int(19));
  EXPECT_CALL(mock_function, Call());
1357
1358
1359
1360
  EXPECT_CALL(mock, MakeUnique())
      .WillOnce(DoAll(InvokeWithoutArgs(&mock_function,
                                        &testing::MockFunction<void()>::Call),
                      Return(ByMove(std::move(i)))));
1361
1362
1363
1364
1365
1366

  std::unique_ptr<int> result1 = mock.MakeUnique();
  EXPECT_EQ(19, *result1);
}

TEST(MockMethodTest, CanReturnMoveOnlyValue_Invoke) {
1367
1368
1369
  MockClass mock;

  // Check default value
1370
1371
  DefaultValue<std::unique_ptr<int>>::SetFactory(
      [] { return std::unique_ptr<int>(new int(42)); });
1372
1373
  EXPECT_EQ(42, *mock.MakeUnique());

1374
  EXPECT_CALL(mock, MakeUnique()).WillRepeatedly(Invoke(UniquePtrSource));
1375
1376
1377
1378
1379
1380
1381
1382
1383
  EXPECT_CALL(mock, MakeVectorUnique())
      .WillRepeatedly(Invoke(VectorUniquePtrSource));
  std::unique_ptr<int> result1 = mock.MakeUnique();
  EXPECT_EQ(19, *result1);
  std::unique_ptr<int> result2 = mock.MakeUnique();
  EXPECT_EQ(19, *result2);
  EXPECT_NE(result1, result2);

  std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
1384
  EXPECT_EQ(1u, vresult.size());
1385
1386
1387
1388
  EXPECT_NE(nullptr, vresult[0]);
  EXPECT_EQ(7, *vresult[0]);
}

Gennadiy Civil's avatar
 
Gennadiy Civil committed
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
TEST(MockMethodTest, CanTakeMoveOnlyValue) {
  MockClass mock;
  auto make = [](int i) { return std::unique_ptr<int>(new int(i)); };

  EXPECT_CALL(mock, TakeUnique(_)).WillRepeatedly([](std::unique_ptr<int> i) {
    return *i;
  });
  // DoAll() does not compile, since it would move from its arguments twice.
  // EXPECT_CALL(mock, TakeUnique(_, _))
  //     .WillRepeatedly(DoAll(Invoke([](std::unique_ptr<int> j) {}),
  //     Return(1)));
  EXPECT_CALL(mock, TakeUnique(testing::Pointee(7)))
      .WillOnce(Return(-7))
      .RetiresOnSaturation();
  EXPECT_CALL(mock, TakeUnique(testing::IsNull()))
      .WillOnce(Return(-1))
      .RetiresOnSaturation();

  EXPECT_EQ(5, mock.TakeUnique(make(5)));
  EXPECT_EQ(-7, mock.TakeUnique(make(7)));
  EXPECT_EQ(7, mock.TakeUnique(make(7)));
  EXPECT_EQ(7, mock.TakeUnique(make(7)));
  EXPECT_EQ(-1, mock.TakeUnique({}));

  // Some arguments are moved, some passed by reference.
  auto lvalue = make(6);
  EXPECT_CALL(mock, TakeUnique(_, _))
      .WillOnce([](const std::unique_ptr<int>& i, std::unique_ptr<int> j) {
        return *i * *j;
      });
  EXPECT_EQ(42, mock.TakeUnique(lvalue, make(7)));

  // The unique_ptr can be saved by the action.
  std::unique_ptr<int> saved;
  EXPECT_CALL(mock, TakeUnique(_)).WillOnce([&saved](std::unique_ptr<int> i) {
    saved = std::move(i);
    return 0;
  });
  EXPECT_EQ(0, mock.TakeUnique(make(42)));
  EXPECT_EQ(42, *saved);
}

// Tests for std::function based action.

int Add(int val, int& ref, int* ptr) {  // NOLINT
  int result = val + ref + *ptr;
  ref = 42;
  *ptr = 43;
  return result;
}

int Deref(std::unique_ptr<int> ptr) { return *ptr; }

struct Double {
  template <typename T>
1444
1445
1446
  T operator()(T t) {
    return 2 * t;
  }
Gennadiy Civil's avatar
 
Gennadiy Civil committed
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
};

std::unique_ptr<int> UniqueInt(int i) {
  return std::unique_ptr<int>(new int(i));
}

TEST(FunctorActionTest, ActionFromFunction) {
  Action<int(int, int&, int*)> a = &Add;
  int x = 1, y = 2, z = 3;
  EXPECT_EQ(6, a.Perform(std::forward_as_tuple(x, y, &z)));
  EXPECT_EQ(42, y);
  EXPECT_EQ(43, z);

  Action<int(std::unique_ptr<int>)> a1 = &Deref;
  EXPECT_EQ(7, a1.Perform(std::make_tuple(UniqueInt(7))));
}

TEST(FunctorActionTest, ActionFromLambda) {
  Action<int(bool, int)> a1 = [](bool b, int i) { return b ? i : 0; };
Abseil Team's avatar
Abseil Team committed
1466
1467
  EXPECT_EQ(5, a1.Perform(std::make_tuple(true, 5)));
  EXPECT_EQ(0, a1.Perform(std::make_tuple(false, 5)));
Gennadiy Civil's avatar
 
Gennadiy Civil committed
1468
1469
1470
1471
1472

  std::unique_ptr<int> saved;
  Action<void(std::unique_ptr<int>)> a2 = [&saved](std::unique_ptr<int> p) {
    saved = std::move(p);
  };
Abseil Team's avatar
Abseil Team committed
1473
  a2.Perform(std::make_tuple(UniqueInt(5)));
Gennadiy Civil's avatar
 
Gennadiy Civil committed
1474
1475
1476
1477
1478
  EXPECT_EQ(5, *saved);
}

TEST(FunctorActionTest, PolymorphicFunctor) {
  Action<int(int)> ai = Double();
Abseil Team's avatar
Abseil Team committed
1479
  EXPECT_EQ(2, ai.Perform(std::make_tuple(1)));
Gennadiy Civil's avatar
 
Gennadiy Civil committed
1480
  Action<double(double)> ad = Double();  // Double? Double double!
Abseil Team's avatar
Abseil Team committed
1481
  EXPECT_EQ(3.0, ad.Perform(std::make_tuple(1.5)));
Gennadiy Civil's avatar
 
Gennadiy Civil committed
1482
1483
1484
1485
1486
1487
}

TEST(FunctorActionTest, TypeConversion) {
  // Numeric promotions are allowed.
  const Action<bool(int)> a1 = [](int i) { return i > 1; };
  const Action<int(bool)> a2 = Action<int(bool)>(a1);
Abseil Team's avatar
Abseil Team committed
1488
1489
  EXPECT_EQ(1, a1.Perform(std::make_tuple(42)));
  EXPECT_EQ(0, a2.Perform(std::make_tuple(42)));
Gennadiy Civil's avatar
 
Gennadiy Civil committed
1490
1491
1492
1493

  // Implicit constructors are allowed.
  const Action<bool(std::string)> s1 = [](std::string s) { return !s.empty(); };
  const Action<int(const char*)> s2 = Action<int(const char*)>(s1);
Abseil Team's avatar
Abseil Team committed
1494
1495
  EXPECT_EQ(0, s2.Perform(std::make_tuple("")));
  EXPECT_EQ(1, s2.Perform(std::make_tuple("hello")));
Gennadiy Civil's avatar
 
Gennadiy Civil committed
1496
1497

  // Also between the lambda and the action itself.
Abseil Team's avatar
Abseil Team committed
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
  const Action<bool(std::string)> x1 = [](Unused) { return 42; };
  const Action<bool(std::string)> x2 = [] { return 42; };
  EXPECT_TRUE(x1.Perform(std::make_tuple("hello")));
  EXPECT_TRUE(x2.Perform(std::make_tuple("hello")));

  // Ensure decay occurs where required.
  std::function<int()> f = [] { return 7; };
  Action<int(int)> d = f;
  f = nullptr;
  EXPECT_EQ(7, d.Perform(std::make_tuple(1)));

  // Ensure creation of an empty action succeeds.
  Action<void(int)>(nullptr);
Gennadiy Civil's avatar
 
Gennadiy Civil committed
1511
1512
1513
1514
}

TEST(FunctorActionTest, UnusedArguments) {
  // Verify that users can ignore uninteresting arguments.
1515
1516
1517
  Action<int(int, double y, double z)> a = [](int i, Unused, Unused) {
    return 2 * i;
  };
Abseil Team's avatar
Abseil Team committed
1518
  std::tuple<int, double, double> dummy = std::make_tuple(3, 7.3, 9.44);
Gennadiy Civil's avatar
 
Gennadiy Civil committed
1519
  EXPECT_EQ(6, a.Perform(dummy));
Gennadiy Civil's avatar
 
Gennadiy Civil committed
1520
1521
1522
1523
1524
}

// Test that basic built-in actions work with move-only arguments.
TEST(MoveOnlyArgumentsTest, ReturningActions) {
  Action<int(std::unique_ptr<int>)> a = Return(1);
Abseil Team's avatar
Abseil Team committed
1525
  EXPECT_EQ(1, a.Perform(std::make_tuple(nullptr)));
Gennadiy Civil's avatar
 
Gennadiy Civil committed
1526
1527

  a = testing::WithoutArgs([]() { return 7; });
Abseil Team's avatar
Abseil Team committed
1528
  EXPECT_EQ(7, a.Perform(std::make_tuple(nullptr)));
Gennadiy Civil's avatar
 
Gennadiy Civil committed
1529
1530
1531

  Action<void(std::unique_ptr<int>, int*)> a2 = testing::SetArgPointee<1>(3);
  int x = 0;
Abseil Team's avatar
Abseil Team committed
1532
  a2.Perform(std::make_tuple(nullptr, &x));
Gennadiy Civil's avatar
 
Gennadiy Civil committed
1533
1534
1535
  EXPECT_EQ(x, 3);
}

1536
ACTION(ReturnArity) { return std::tuple_size<args_type>::value; }
Abseil Team's avatar
Abseil Team committed
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553

TEST(ActionMacro, LargeArity) {
  EXPECT_EQ(
      1, testing::Action<int(int)>(ReturnArity()).Perform(std::make_tuple(0)));
  EXPECT_EQ(
      10,
      testing::Action<int(int, int, int, int, int, int, int, int, int, int)>(
          ReturnArity())
          .Perform(std::make_tuple(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)));
  EXPECT_EQ(
      20,
      testing::Action<int(int, int, int, int, int, int, int, int, int, int, int,
                          int, int, int, int, int, int, int, int, int)>(
          ReturnArity())
          .Perform(std::make_tuple(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
                                   14, 15, 16, 17, 18, 19)));
}
Gennadiy Civil's avatar
 
Gennadiy Civil committed
1554

1555
}  // Unnamed namespace
Gennadiy Civil's avatar
 
Gennadiy Civil committed
1556
1557
1558

#ifdef _MSC_VER
#if _MSC_VER == 1900
1559
#pragma warning(pop)
Gennadiy Civil's avatar
 
Gennadiy Civil committed
1560
1561
#endif
#endif