blob: f761b44600c742b310bcbfc2faccc47f5c5f2e81 [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.
Gennadiy Civila3c0dd02018-08-14 14:04:07 -040029
shiqiane35fdd92008-12-10 05:08:54 +000030
31// Google Mock - a framework for writing C++ mock classes.
32//
33// This file tests the built-in actions.
34
Gennadiy Civile1071eb2018-04-10 15:57:16 -040035// Silence C4800 (C4800: 'int *const ': forcing value
Robin Lindén826656b2018-11-10 15:05:55 +010036// to bool 'true' or 'false') for MSVC 15
Gennadiy Civile1071eb2018-04-10 15:57:16 -040037#ifdef _MSC_VER
Robin Lindén826656b2018-11-10 15:05:55 +010038#if _MSC_VER == 1900
Gennadiy Civile1071eb2018-04-10 15:57:16 -040039# pragma warning(push)
40# pragma warning(disable:4800)
41#endif
42#endif
43
zhanyong.wan53e08c42010-09-14 05:38:21 +000044#include "gmock/gmock-actions.h"
shiqiane35fdd92008-12-10 05:08:54 +000045#include <algorithm>
46#include <iterator>
kosakb5c81092014-01-29 06:41:44 +000047#include <memory>
shiqiane35fdd92008-12-10 05:08:54 +000048#include <string>
zhanyong.wan53e08c42010-09-14 05:38:21 +000049#include "gmock/gmock.h"
50#include "gmock/internal/gmock-port.h"
51#include "gtest/gtest.h"
52#include "gtest/gtest-spi.h"
shiqiane35fdd92008-12-10 05:08:54 +000053
54namespace {
55
shiqiane35fdd92008-12-10 05:08:54 +000056// This list should be kept sorted.
Abseil Team0adeadd2019-01-16 15:23:44 -050057using testing::_;
shiqiane35fdd92008-12-10 05:08:54 +000058using testing::Action;
59using testing::ActionInterface;
60using testing::Assign;
kosak3d1c78b2014-11-17 00:56:52 +000061using testing::ByMove;
zhanyong.wana18423e2009-07-22 23:58:19 +000062using testing::ByRef;
shiqiane35fdd92008-12-10 05:08:54 +000063using testing::DefaultValue;
Abseil Team0adeadd2019-01-16 15:23:44 -050064using testing::DoAll;
shiqiane35fdd92008-12-10 05:08:54 +000065using testing::DoDefault;
66using testing::IgnoreResult;
67using testing::Invoke;
68using testing::InvokeWithoutArgs;
69using testing::MakePolymorphicAction;
70using testing::Ne;
71using testing::PolymorphicAction;
72using testing::Return;
73using testing::ReturnNull;
74using testing::ReturnRef;
zhanyong.wane3bd0982010-07-03 00:16:42 +000075using testing::ReturnRefOfCopy;
zhanyong.wan59214832010-10-05 05:58:51 +000076using testing::SetArgPointee;
shiqiane35fdd92008-12-10 05:08:54 +000077using testing::SetArgumentPointee;
Gennadiy Civilf9bd6182018-04-13 11:02:55 -040078using testing::Unused;
Abseil Teamaac18182018-11-15 15:43:19 -050079using testing::WithArgs;
kosakd478a1f2015-02-14 02:45:40 +000080using testing::internal::BuiltInDefaultValue;
81using testing::internal::Int64;
82using testing::internal::UInt64;
zhanyong.wan5b5d62f2009-03-11 23:37:56 +000083
zhanyong.wanf7af24c2009-09-24 21:17:24 +000084#if !GTEST_OS_WINDOWS_MOBILE
shiqiane35fdd92008-12-10 05:08:54 +000085using testing::SetErrnoAndReturn;
zhanyong.wanf7af24c2009-09-24 21:17:24 +000086#endif
shiqiane35fdd92008-12-10 05:08:54 +000087
shiqiane35fdd92008-12-10 05:08:54 +000088// Tests that BuiltInDefaultValue<T*>::Get() returns NULL.
89TEST(BuiltInDefaultValueTest, IsNullForPointerTypes) {
Abseil Team4bb49ed2018-10-04 18:28:05 -040090 EXPECT_TRUE(BuiltInDefaultValue<int*>::Get() == nullptr);
91 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Get() == nullptr);
92 EXPECT_TRUE(BuiltInDefaultValue<void*>::Get() == nullptr);
shiqiane35fdd92008-12-10 05:08:54 +000093}
94
zhanyong.wan5b95fa72009-01-27 22:28:45 +000095// Tests that BuiltInDefaultValue<T*>::Exists() return true.
96TEST(BuiltInDefaultValueTest, ExistsForPointerTypes) {
97 EXPECT_TRUE(BuiltInDefaultValue<int*>::Exists());
98 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Exists());
99 EXPECT_TRUE(BuiltInDefaultValue<void*>::Exists());
100}
101
shiqiane35fdd92008-12-10 05:08:54 +0000102// Tests that BuiltInDefaultValue<T>::Get() returns 0 when T is a
103// built-in numeric type.
104TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) {
zhanyong.wan32de5f52009-12-23 00:13:23 +0000105 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned char>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000106 EXPECT_EQ(0, BuiltInDefaultValue<signed char>::Get());
107 EXPECT_EQ(0, BuiltInDefaultValue<char>::Get());
zhanyong.wan95b12332009-09-25 18:55:50 +0000108#if GMOCK_HAS_SIGNED_WCHAR_T_
zhanyong.wan32de5f52009-12-23 00:13:23 +0000109 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned wchar_t>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000110 EXPECT_EQ(0, BuiltInDefaultValue<signed wchar_t>::Get());
zhanyong.wan95b12332009-09-25 18:55:50 +0000111#endif
112#if GMOCK_WCHAR_T_IS_NATIVE_
Scott Graham567b40e2018-02-23 12:28:09 -0800113#if !defined(__WCHAR_UNSIGNED__)
shiqiane35fdd92008-12-10 05:08:54 +0000114 EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get());
Scott Graham567b40e2018-02-23 12:28:09 -0800115#else
116 EXPECT_EQ(0U, BuiltInDefaultValue<wchar_t>::Get());
117#endif
zhanyong.wan95b12332009-09-25 18:55:50 +0000118#endif
zhanyong.wan32de5f52009-12-23 00:13:23 +0000119 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned short>::Get()); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +0000120 EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get()); // NOLINT
121 EXPECT_EQ(0, BuiltInDefaultValue<short>::Get()); // NOLINT
zhanyong.wan32de5f52009-12-23 00:13:23 +0000122 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned int>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000123 EXPECT_EQ(0, BuiltInDefaultValue<signed int>::Get());
124 EXPECT_EQ(0, BuiltInDefaultValue<int>::Get());
zhanyong.wan32de5f52009-12-23 00:13:23 +0000125 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned long>::Get()); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +0000126 EXPECT_EQ(0, BuiltInDefaultValue<signed long>::Get()); // NOLINT
127 EXPECT_EQ(0, BuiltInDefaultValue<long>::Get()); // NOLINT
zhanyong.wan32de5f52009-12-23 00:13:23 +0000128 EXPECT_EQ(0U, BuiltInDefaultValue<UInt64>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000129 EXPECT_EQ(0, BuiltInDefaultValue<Int64>::Get());
130 EXPECT_EQ(0, BuiltInDefaultValue<float>::Get());
131 EXPECT_EQ(0, BuiltInDefaultValue<double>::Get());
132}
133
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000134// Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
135// built-in numeric type.
136TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) {
137 EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists());
138 EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists());
139 EXPECT_TRUE(BuiltInDefaultValue<char>::Exists());
zhanyong.wan95b12332009-09-25 18:55:50 +0000140#if GMOCK_HAS_SIGNED_WCHAR_T_
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000141 EXPECT_TRUE(BuiltInDefaultValue<unsigned wchar_t>::Exists());
142 EXPECT_TRUE(BuiltInDefaultValue<signed wchar_t>::Exists());
zhanyong.wan95b12332009-09-25 18:55:50 +0000143#endif
144#if GMOCK_WCHAR_T_IS_NATIVE_
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000145 EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists());
zhanyong.wan95b12332009-09-25 18:55:50 +0000146#endif
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000147 EXPECT_TRUE(BuiltInDefaultValue<unsigned short>::Exists()); // NOLINT
148 EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists()); // NOLINT
149 EXPECT_TRUE(BuiltInDefaultValue<short>::Exists()); // NOLINT
150 EXPECT_TRUE(BuiltInDefaultValue<unsigned int>::Exists());
151 EXPECT_TRUE(BuiltInDefaultValue<signed int>::Exists());
152 EXPECT_TRUE(BuiltInDefaultValue<int>::Exists());
153 EXPECT_TRUE(BuiltInDefaultValue<unsigned long>::Exists()); // NOLINT
154 EXPECT_TRUE(BuiltInDefaultValue<signed long>::Exists()); // NOLINT
155 EXPECT_TRUE(BuiltInDefaultValue<long>::Exists()); // NOLINT
156 EXPECT_TRUE(BuiltInDefaultValue<UInt64>::Exists());
157 EXPECT_TRUE(BuiltInDefaultValue<Int64>::Exists());
158 EXPECT_TRUE(BuiltInDefaultValue<float>::Exists());
159 EXPECT_TRUE(BuiltInDefaultValue<double>::Exists());
160}
161
shiqiane35fdd92008-12-10 05:08:54 +0000162// Tests that BuiltInDefaultValue<bool>::Get() returns false.
163TEST(BuiltInDefaultValueTest, IsFalseForBool) {
164 EXPECT_FALSE(BuiltInDefaultValue<bool>::Get());
165}
166
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000167// Tests that BuiltInDefaultValue<bool>::Exists() returns true.
168TEST(BuiltInDefaultValueTest, BoolExists) {
169 EXPECT_TRUE(BuiltInDefaultValue<bool>::Exists());
170}
171
shiqiane35fdd92008-12-10 05:08:54 +0000172// Tests that BuiltInDefaultValue<T>::Get() returns "" when T is a
173// string type.
174TEST(BuiltInDefaultValueTest, IsEmptyStringForString) {
shiqiane35fdd92008-12-10 05:08:54 +0000175 EXPECT_EQ("", BuiltInDefaultValue< ::std::string>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000176}
177
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000178// Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
179// string type.
180TEST(BuiltInDefaultValueTest, ExistsForString) {
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000181 EXPECT_TRUE(BuiltInDefaultValue< ::std::string>::Exists());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000182}
183
shiqiane35fdd92008-12-10 05:08:54 +0000184// Tests that BuiltInDefaultValue<const T>::Get() returns the same
185// value as BuiltInDefaultValue<T>::Get() does.
186TEST(BuiltInDefaultValueTest, WorksForConstTypes) {
187 EXPECT_EQ("", BuiltInDefaultValue<const std::string>::Get());
188 EXPECT_EQ(0, BuiltInDefaultValue<const int>::Get());
Abseil Team4bb49ed2018-10-04 18:28:05 -0400189 EXPECT_TRUE(BuiltInDefaultValue<char* const>::Get() == nullptr);
shiqiane35fdd92008-12-10 05:08:54 +0000190 EXPECT_FALSE(BuiltInDefaultValue<const bool>::Get());
191}
192
kosakd478a1f2015-02-14 02:45:40 +0000193// A type that's default constructible.
194class MyDefaultConstructible {
195 public:
196 MyDefaultConstructible() : value_(42) {}
shiqiane35fdd92008-12-10 05:08:54 +0000197
kosakd478a1f2015-02-14 02:45:40 +0000198 int value() const { return value_; }
199
200 private:
201 int value_;
shiqiane35fdd92008-12-10 05:08:54 +0000202};
203
kosakd478a1f2015-02-14 02:45:40 +0000204// A type that's not default constructible.
205class MyNonDefaultConstructible {
206 public:
207 // Does not have a default ctor.
208 explicit MyNonDefaultConstructible(int a_value) : value_(a_value) {}
209
210 int value() const { return value_; }
211
212 private:
213 int value_;
214};
215
kosakd478a1f2015-02-14 02:45:40 +0000216
217TEST(BuiltInDefaultValueTest, ExistsForDefaultConstructibleType) {
218 EXPECT_TRUE(BuiltInDefaultValue<MyDefaultConstructible>::Exists());
219}
220
221TEST(BuiltInDefaultValueTest, IsDefaultConstructedForDefaultConstructibleType) {
222 EXPECT_EQ(42, BuiltInDefaultValue<MyDefaultConstructible>::Get().value());
223}
224
kosakd478a1f2015-02-14 02:45:40 +0000225
226TEST(BuiltInDefaultValueTest, DoesNotExistForNonDefaultConstructibleType) {
227 EXPECT_FALSE(BuiltInDefaultValue<MyNonDefaultConstructible>::Exists());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000228}
229
shiqiane35fdd92008-12-10 05:08:54 +0000230// Tests that BuiltInDefaultValue<T&>::Get() aborts the program.
231TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000232 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000233 BuiltInDefaultValue<int&>::Get();
234 }, "");
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000235 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000236 BuiltInDefaultValue<const char&>::Get();
237 }, "");
238}
239
kosakd478a1f2015-02-14 02:45:40 +0000240TEST(BuiltInDefaultValueDeathTest, IsUndefinedForNonDefaultConstructibleType) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000241 EXPECT_DEATH_IF_SUPPORTED({
kosakd478a1f2015-02-14 02:45:40 +0000242 BuiltInDefaultValue<MyNonDefaultConstructible>::Get();
shiqiane35fdd92008-12-10 05:08:54 +0000243 }, "");
244}
245
shiqiane35fdd92008-12-10 05:08:54 +0000246// Tests that DefaultValue<T>::IsSet() is false initially.
247TEST(DefaultValueTest, IsInitiallyUnset) {
248 EXPECT_FALSE(DefaultValue<int>::IsSet());
kosakd478a1f2015-02-14 02:45:40 +0000249 EXPECT_FALSE(DefaultValue<MyDefaultConstructible>::IsSet());
250 EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::IsSet());
shiqiane35fdd92008-12-10 05:08:54 +0000251}
252
253// Tests that DefaultValue<T> can be set and then unset.
254TEST(DefaultValueTest, CanBeSetAndUnset) {
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000255 EXPECT_TRUE(DefaultValue<int>::Exists());
kosakd478a1f2015-02-14 02:45:40 +0000256 EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::Exists());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000257
shiqiane35fdd92008-12-10 05:08:54 +0000258 DefaultValue<int>::Set(1);
kosakd478a1f2015-02-14 02:45:40 +0000259 DefaultValue<const MyNonDefaultConstructible>::Set(
260 MyNonDefaultConstructible(42));
shiqiane35fdd92008-12-10 05:08:54 +0000261
262 EXPECT_EQ(1, DefaultValue<int>::Get());
kosakd478a1f2015-02-14 02:45:40 +0000263 EXPECT_EQ(42, DefaultValue<const MyNonDefaultConstructible>::Get().value());
shiqiane35fdd92008-12-10 05:08:54 +0000264
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000265 EXPECT_TRUE(DefaultValue<int>::Exists());
kosakd478a1f2015-02-14 02:45:40 +0000266 EXPECT_TRUE(DefaultValue<const MyNonDefaultConstructible>::Exists());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000267
shiqiane35fdd92008-12-10 05:08:54 +0000268 DefaultValue<int>::Clear();
kosakd478a1f2015-02-14 02:45:40 +0000269 DefaultValue<const MyNonDefaultConstructible>::Clear();
shiqiane35fdd92008-12-10 05:08:54 +0000270
271 EXPECT_FALSE(DefaultValue<int>::IsSet());
kosakd478a1f2015-02-14 02:45:40 +0000272 EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::IsSet());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000273
274 EXPECT_TRUE(DefaultValue<int>::Exists());
kosakd478a1f2015-02-14 02:45:40 +0000275 EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::Exists());
shiqiane35fdd92008-12-10 05:08:54 +0000276}
277
278// Tests that DefaultValue<T>::Get() returns the
279// BuiltInDefaultValue<T>::Get() when DefaultValue<T>::IsSet() is
280// false.
281TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
282 EXPECT_FALSE(DefaultValue<int>::IsSet());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000283 EXPECT_TRUE(DefaultValue<int>::Exists());
kosakd478a1f2015-02-14 02:45:40 +0000284 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible>::IsSet());
285 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible>::Exists());
shiqiane35fdd92008-12-10 05:08:54 +0000286
287 EXPECT_EQ(0, DefaultValue<int>::Get());
288
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000289 EXPECT_DEATH_IF_SUPPORTED({
kosakd478a1f2015-02-14 02:45:40 +0000290 DefaultValue<MyNonDefaultConstructible>::Get();
shiqiane35fdd92008-12-10 05:08:54 +0000291 }, "");
shiqiane35fdd92008-12-10 05:08:54 +0000292}
293
kosakd478a1f2015-02-14 02:45:40 +0000294TEST(DefaultValueTest, GetWorksForMoveOnlyIfSet) {
295 EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Exists());
Abseil Team4bb49ed2018-10-04 18:28:05 -0400296 EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Get() == nullptr);
kosakb5c81092014-01-29 06:41:44 +0000297 DefaultValue<std::unique_ptr<int>>::SetFactory([] {
298 return std::unique_ptr<int>(new int(42));
299 });
300 EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Exists());
301 std::unique_ptr<int> i = DefaultValue<std::unique_ptr<int>>::Get();
302 EXPECT_EQ(42, *i);
303}
kosakb5c81092014-01-29 06:41:44 +0000304
shiqiane35fdd92008-12-10 05:08:54 +0000305// Tests that DefaultValue<void>::Get() returns void.
306TEST(DefaultValueTest, GetWorksForVoid) {
307 return DefaultValue<void>::Get();
308}
309
310// Tests using DefaultValue with a reference type.
311
312// Tests that DefaultValue<T&>::IsSet() is false initially.
313TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) {
314 EXPECT_FALSE(DefaultValue<int&>::IsSet());
kosakd478a1f2015-02-14 02:45:40 +0000315 EXPECT_FALSE(DefaultValue<MyDefaultConstructible&>::IsSet());
316 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
shiqiane35fdd92008-12-10 05:08:54 +0000317}
318
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000319// Tests that DefaultValue<T&>::Exists is false initiallly.
320TEST(DefaultValueOfReferenceTest, IsInitiallyNotExisting) {
321 EXPECT_FALSE(DefaultValue<int&>::Exists());
kosakd478a1f2015-02-14 02:45:40 +0000322 EXPECT_FALSE(DefaultValue<MyDefaultConstructible&>::Exists());
323 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::Exists());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000324}
325
shiqiane35fdd92008-12-10 05:08:54 +0000326// Tests that DefaultValue<T&> can be set and then unset.
327TEST(DefaultValueOfReferenceTest, CanBeSetAndUnset) {
328 int n = 1;
329 DefaultValue<const int&>::Set(n);
kosakd478a1f2015-02-14 02:45:40 +0000330 MyNonDefaultConstructible x(42);
331 DefaultValue<MyNonDefaultConstructible&>::Set(x);
shiqiane35fdd92008-12-10 05:08:54 +0000332
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000333 EXPECT_TRUE(DefaultValue<const int&>::Exists());
kosakd478a1f2015-02-14 02:45:40 +0000334 EXPECT_TRUE(DefaultValue<MyNonDefaultConstructible&>::Exists());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000335
shiqiane35fdd92008-12-10 05:08:54 +0000336 EXPECT_EQ(&n, &(DefaultValue<const int&>::Get()));
kosakd478a1f2015-02-14 02:45:40 +0000337 EXPECT_EQ(&x, &(DefaultValue<MyNonDefaultConstructible&>::Get()));
shiqiane35fdd92008-12-10 05:08:54 +0000338
339 DefaultValue<const int&>::Clear();
kosakd478a1f2015-02-14 02:45:40 +0000340 DefaultValue<MyNonDefaultConstructible&>::Clear();
shiqiane35fdd92008-12-10 05:08:54 +0000341
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000342 EXPECT_FALSE(DefaultValue<const int&>::Exists());
kosakd478a1f2015-02-14 02:45:40 +0000343 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::Exists());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000344
shiqiane35fdd92008-12-10 05:08:54 +0000345 EXPECT_FALSE(DefaultValue<const int&>::IsSet());
kosakd478a1f2015-02-14 02:45:40 +0000346 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
shiqiane35fdd92008-12-10 05:08:54 +0000347}
348
349// Tests that DefaultValue<T&>::Get() returns the
350// BuiltInDefaultValue<T&>::Get() when DefaultValue<T&>::IsSet() is
351// false.
352TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
353 EXPECT_FALSE(DefaultValue<int&>::IsSet());
kosakd478a1f2015-02-14 02:45:40 +0000354 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
shiqiane35fdd92008-12-10 05:08:54 +0000355
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000356 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000357 DefaultValue<int&>::Get();
358 }, "");
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000359 EXPECT_DEATH_IF_SUPPORTED({
kosakd478a1f2015-02-14 02:45:40 +0000360 DefaultValue<MyNonDefaultConstructible>::Get();
shiqiane35fdd92008-12-10 05:08:54 +0000361 }, "");
shiqiane35fdd92008-12-10 05:08:54 +0000362}
363
364// Tests that ActionInterface can be implemented by defining the
365// Perform method.
366
zhanyong.wana1a98f82013-03-01 21:28:40 +0000367typedef int MyGlobalFunction(bool, int);
shiqiane35fdd92008-12-10 05:08:54 +0000368
zhanyong.wana1a98f82013-03-01 21:28:40 +0000369class MyActionImpl : public ActionInterface<MyGlobalFunction> {
shiqiane35fdd92008-12-10 05:08:54 +0000370 public:
Abseil Team26743362018-12-03 11:30:02 -0500371 int Perform(const std::tuple<bool, int>& args) override {
Abseil Team7d3b73c2018-10-09 14:50:26 -0400372 return std::get<0>(args) ? std::get<1>(args) : 0;
shiqiane35fdd92008-12-10 05:08:54 +0000373 }
374};
375
376TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) {
377 MyActionImpl my_action_impl;
zhanyong.waned6c9272011-02-23 19:39:27 +0000378 (void)my_action_impl;
shiqiane35fdd92008-12-10 05:08:54 +0000379}
380
381TEST(ActionInterfaceTest, MakeAction) {
zhanyong.wana1a98f82013-03-01 21:28:40 +0000382 Action<MyGlobalFunction> action = MakeAction(new MyActionImpl);
shiqiane35fdd92008-12-10 05:08:54 +0000383
384 // When exercising the Perform() method of Action<F>, we must pass
385 // it a tuple whose size and type are compatible with F's argument
386 // types. For example, if F is int(), then Perform() takes a
387 // 0-tuple; if F is void(bool, int), then Perform() takes a
Abseil Team7d3b73c2018-10-09 14:50:26 -0400388 // std::tuple<bool, int>, and so on.
389 EXPECT_EQ(5, action.Perform(std::make_tuple(true, 5)));
shiqiane35fdd92008-12-10 05:08:54 +0000390}
391
392// Tests that Action<F> can be contructed from a pointer to
393// ActionInterface<F>.
394TEST(ActionTest, CanBeConstructedFromActionInterface) {
zhanyong.wana1a98f82013-03-01 21:28:40 +0000395 Action<MyGlobalFunction> action(new MyActionImpl);
shiqiane35fdd92008-12-10 05:08:54 +0000396}
397
398// Tests that Action<F> delegates actual work to ActionInterface<F>.
399TEST(ActionTest, DelegatesWorkToActionInterface) {
zhanyong.wana1a98f82013-03-01 21:28:40 +0000400 const Action<MyGlobalFunction> action(new MyActionImpl);
shiqiane35fdd92008-12-10 05:08:54 +0000401
Abseil Team7d3b73c2018-10-09 14:50:26 -0400402 EXPECT_EQ(5, action.Perform(std::make_tuple(true, 5)));
403 EXPECT_EQ(0, action.Perform(std::make_tuple(false, 1)));
shiqiane35fdd92008-12-10 05:08:54 +0000404}
405
406// Tests that Action<F> can be copied.
407TEST(ActionTest, IsCopyable) {
zhanyong.wana1a98f82013-03-01 21:28:40 +0000408 Action<MyGlobalFunction> a1(new MyActionImpl);
409 Action<MyGlobalFunction> a2(a1); // Tests the copy constructor.
shiqiane35fdd92008-12-10 05:08:54 +0000410
411 // a1 should continue to work after being copied from.
Abseil Team7d3b73c2018-10-09 14:50:26 -0400412 EXPECT_EQ(5, a1.Perform(std::make_tuple(true, 5)));
413 EXPECT_EQ(0, a1.Perform(std::make_tuple(false, 1)));
shiqiane35fdd92008-12-10 05:08:54 +0000414
415 // a2 should work like the action it was copied from.
Abseil Team7d3b73c2018-10-09 14:50:26 -0400416 EXPECT_EQ(5, a2.Perform(std::make_tuple(true, 5)));
417 EXPECT_EQ(0, a2.Perform(std::make_tuple(false, 1)));
shiqiane35fdd92008-12-10 05:08:54 +0000418
419 a2 = a1; // Tests the assignment operator.
420
421 // a1 should continue to work after being copied from.
Abseil Team7d3b73c2018-10-09 14:50:26 -0400422 EXPECT_EQ(5, a1.Perform(std::make_tuple(true, 5)));
423 EXPECT_EQ(0, a1.Perform(std::make_tuple(false, 1)));
shiqiane35fdd92008-12-10 05:08:54 +0000424
425 // a2 should work like the action it was copied from.
Abseil Team7d3b73c2018-10-09 14:50:26 -0400426 EXPECT_EQ(5, a2.Perform(std::make_tuple(true, 5)));
427 EXPECT_EQ(0, a2.Perform(std::make_tuple(false, 1)));
shiqiane35fdd92008-12-10 05:08:54 +0000428}
429
430// Tests that an Action<From> object can be converted to a
431// compatible Action<To> object.
432
433class IsNotZero : public ActionInterface<bool(int)> { // NOLINT
434 public:
Abseil Team26743362018-12-03 11:30:02 -0500435 bool Perform(const std::tuple<int>& arg) override {
Abseil Team7d3b73c2018-10-09 14:50:26 -0400436 return std::get<0>(arg) != 0;
shiqiane35fdd92008-12-10 05:08:54 +0000437 }
438};
439
440TEST(ActionTest, CanBeConvertedToOtherActionType) {
441 const Action<bool(int)> a1(new IsNotZero); // NOLINT
442 const Action<int(char)> a2 = Action<int(char)>(a1); // NOLINT
Abseil Team7d3b73c2018-10-09 14:50:26 -0400443 EXPECT_EQ(1, a2.Perform(std::make_tuple('a')));
444 EXPECT_EQ(0, a2.Perform(std::make_tuple('\0')));
shiqiane35fdd92008-12-10 05:08:54 +0000445}
446
447// The following two classes are for testing MakePolymorphicAction().
448
449// Implements a polymorphic action that returns the second of the
450// arguments it receives.
451class ReturnSecondArgumentAction {
452 public:
453 // We want to verify that MakePolymorphicAction() can work with a
454 // polymorphic action whose Perform() method template is either
455 // const or not. This lets us verify the non-const case.
456 template <typename Result, typename ArgumentTuple>
Abseil Team7d3b73c2018-10-09 14:50:26 -0400457 Result Perform(const ArgumentTuple& args) {
458 return std::get<1>(args);
459 }
shiqiane35fdd92008-12-10 05:08:54 +0000460};
461
462// Implements a polymorphic action that can be used in a nullary
463// function to return 0.
464class ReturnZeroFromNullaryFunctionAction {
465 public:
466 // For testing that MakePolymorphicAction() works when the
467 // implementation class' Perform() method template takes only one
468 // template parameter.
469 //
470 // We want to verify that MakePolymorphicAction() can work with a
471 // polymorphic action whose Perform() method template is either
472 // const or not. This lets us verify the const case.
473 template <typename Result>
Abseil Team7d3b73c2018-10-09 14:50:26 -0400474 Result Perform(const std::tuple<>&) const {
475 return 0;
476 }
shiqiane35fdd92008-12-10 05:08:54 +0000477};
478
479// These functions verify that MakePolymorphicAction() returns a
480// PolymorphicAction<T> where T is the argument's type.
481
482PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() {
483 return MakePolymorphicAction(ReturnSecondArgumentAction());
484}
485
486PolymorphicAction<ReturnZeroFromNullaryFunctionAction>
487ReturnZeroFromNullaryFunction() {
488 return MakePolymorphicAction(ReturnZeroFromNullaryFunctionAction());
489}
490
491// Tests that MakePolymorphicAction() turns a polymorphic action
492// implementation class into a polymorphic action.
493TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) {
494 Action<int(bool, int, double)> a1 = ReturnSecondArgument(); // NOLINT
Abseil Team7d3b73c2018-10-09 14:50:26 -0400495 EXPECT_EQ(5, a1.Perform(std::make_tuple(false, 5, 2.0)));
shiqiane35fdd92008-12-10 05:08:54 +0000496}
497
498// Tests that MakePolymorphicAction() works when the implementation
499// class' Perform() method template has only one template parameter.
500TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) {
501 Action<int()> a1 = ReturnZeroFromNullaryFunction();
Abseil Team7d3b73c2018-10-09 14:50:26 -0400502 EXPECT_EQ(0, a1.Perform(std::make_tuple()));
shiqiane35fdd92008-12-10 05:08:54 +0000503
504 Action<void*()> a2 = ReturnZeroFromNullaryFunction();
Abseil Team7d3b73c2018-10-09 14:50:26 -0400505 EXPECT_TRUE(a2.Perform(std::make_tuple()) == nullptr);
shiqiane35fdd92008-12-10 05:08:54 +0000506}
507
508// Tests that Return() works as an action for void-returning
509// functions.
510TEST(ReturnTest, WorksForVoid) {
511 const Action<void(int)> ret = Return(); // NOLINT
Abseil Team7d3b73c2018-10-09 14:50:26 -0400512 return ret.Perform(std::make_tuple(1));
shiqiane35fdd92008-12-10 05:08:54 +0000513}
514
515// Tests that Return(v) returns v.
516TEST(ReturnTest, ReturnsGivenValue) {
517 Action<int()> ret = Return(1); // NOLINT
Abseil Team7d3b73c2018-10-09 14:50:26 -0400518 EXPECT_EQ(1, ret.Perform(std::make_tuple()));
shiqiane35fdd92008-12-10 05:08:54 +0000519
520 ret = Return(-5);
Abseil Team7d3b73c2018-10-09 14:50:26 -0400521 EXPECT_EQ(-5, ret.Perform(std::make_tuple()));
shiqiane35fdd92008-12-10 05:08:54 +0000522}
523
524// Tests that Return("string literal") works.
525TEST(ReturnTest, AcceptsStringLiteral) {
526 Action<const char*()> a1 = Return("Hello");
Abseil Team7d3b73c2018-10-09 14:50:26 -0400527 EXPECT_STREQ("Hello", a1.Perform(std::make_tuple()));
shiqiane35fdd92008-12-10 05:08:54 +0000528
529 Action<std::string()> a2 = Return("world");
Abseil Team7d3b73c2018-10-09 14:50:26 -0400530 EXPECT_EQ("world", a2.Perform(std::make_tuple()));
shiqiane35fdd92008-12-10 05:08:54 +0000531}
532
kosak7123d832014-11-17 02:04:46 +0000533// Test struct which wraps a vector of integers. Used in
534// 'SupportsWrapperReturnType' test.
535struct IntegerVectorWrapper {
536 std::vector<int> * v;
537 IntegerVectorWrapper(std::vector<int>& _v) : v(&_v) {} // NOLINT
538};
539
540// Tests that Return() works when return type is a wrapper type.
541TEST(ReturnTest, SupportsWrapperReturnType) {
542 // Initialize vector of integers.
543 std::vector<int> v;
544 for (int i = 0; i < 5; ++i) v.push_back(i);
545
546 // Return() called with 'v' as argument. The Action will return the same data
547 // as 'v' (copy) but it will be wrapped in an IntegerVectorWrapper.
548 Action<IntegerVectorWrapper()> a = Return(v);
Abseil Team7d3b73c2018-10-09 14:50:26 -0400549 const std::vector<int>& result = *(a.Perform(std::make_tuple()).v);
kosak7123d832014-11-17 02:04:46 +0000550 EXPECT_THAT(result, ::testing::ElementsAre(0, 1, 2, 3, 4));
551}
552
shiqiane35fdd92008-12-10 05:08:54 +0000553// Tests that Return(v) is covaraint.
554
555struct Base {
556 bool operator==(const Base&) { return true; }
557};
558
559struct Derived : public Base {
560 bool operator==(const Derived&) { return true; }
561};
562
563TEST(ReturnTest, IsCovariant) {
564 Base base;
565 Derived derived;
566 Action<Base*()> ret = Return(&base);
Abseil Team7d3b73c2018-10-09 14:50:26 -0400567 EXPECT_EQ(&base, ret.Perform(std::make_tuple()));
shiqiane35fdd92008-12-10 05:08:54 +0000568
569 ret = Return(&derived);
Abseil Team7d3b73c2018-10-09 14:50:26 -0400570 EXPECT_EQ(&derived, ret.Perform(std::make_tuple()));
shiqiane35fdd92008-12-10 05:08:54 +0000571}
572
vladloseva070cbd2009-11-18 00:09:28 +0000573// Tests that the type of the value passed into Return is converted into T
574// when the action is cast to Action<T(...)> rather than when the action is
575// performed. See comments on testing::internal::ReturnAction in
576// gmock-actions.h for more information.
577class FromType {
578 public:
jgm79a367e2012-04-10 16:02:11 +0000579 explicit FromType(bool* is_converted) : converted_(is_converted) {}
vladloseva070cbd2009-11-18 00:09:28 +0000580 bool* converted() const { return converted_; }
581
582 private:
583 bool* const converted_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000584
585 GTEST_DISALLOW_ASSIGN_(FromType);
vladloseva070cbd2009-11-18 00:09:28 +0000586};
587
588class ToType {
589 public:
jgm79a367e2012-04-10 16:02:11 +0000590 // Must allow implicit conversion due to use in ImplicitCast_<T>.
591 ToType(const FromType& x) { *x.converted() = true; } // NOLINT
vladloseva070cbd2009-11-18 00:09:28 +0000592};
593
594TEST(ReturnTest, ConvertsArgumentWhenConverted) {
595 bool converted = false;
596 FromType x(&converted);
597 Action<ToType()> action(Return(x));
598 EXPECT_TRUE(converted) << "Return must convert its argument in its own "
599 << "conversion operator.";
600 converted = false;
Abseil Team7d3b73c2018-10-09 14:50:26 -0400601 action.Perform(std::tuple<>());
vladloseva070cbd2009-11-18 00:09:28 +0000602 EXPECT_FALSE(converted) << "Action must NOT convert its argument "
jgm79a367e2012-04-10 16:02:11 +0000603 << "when performed.";
vladloseva070cbd2009-11-18 00:09:28 +0000604}
605
vladloseva070cbd2009-11-18 00:09:28 +0000606class DestinationType {};
607
608class SourceType {
609 public:
610 // Note: a non-const typecast operator.
611 operator DestinationType() { return DestinationType(); }
612};
613
614TEST(ReturnTest, CanConvertArgumentUsingNonConstTypeCastOperator) {
615 SourceType s;
616 Action<DestinationType()> action(Return(s));
617}
vladloseva070cbd2009-11-18 00:09:28 +0000618
shiqiane35fdd92008-12-10 05:08:54 +0000619// Tests that ReturnNull() returns NULL in a pointer-returning function.
620TEST(ReturnNullTest, WorksInPointerReturningFunction) {
621 const Action<int*()> a1 = ReturnNull();
Abseil Team7d3b73c2018-10-09 14:50:26 -0400622 EXPECT_TRUE(a1.Perform(std::make_tuple()) == nullptr);
shiqiane35fdd92008-12-10 05:08:54 +0000623
624 const Action<const char*(bool)> a2 = ReturnNull(); // NOLINT
Abseil Team7d3b73c2018-10-09 14:50:26 -0400625 EXPECT_TRUE(a2.Perform(std::make_tuple(true)) == nullptr);
shiqiane35fdd92008-12-10 05:08:54 +0000626}
627
kosak53d49dc2015-01-08 03:03:09 +0000628// Tests that ReturnNull() returns NULL for shared_ptr and unique_ptr returning
629// functions.
630TEST(ReturnNullTest, WorksInSmartPointerReturningFunction) {
631 const Action<std::unique_ptr<const int>()> a1 = ReturnNull();
Abseil Team7d3b73c2018-10-09 14:50:26 -0400632 EXPECT_TRUE(a1.Perform(std::make_tuple()) == nullptr);
kosak53d49dc2015-01-08 03:03:09 +0000633
634 const Action<std::shared_ptr<int>(std::string)> a2 = ReturnNull();
Abseil Team7d3b73c2018-10-09 14:50:26 -0400635 EXPECT_TRUE(a2.Perform(std::make_tuple("foo")) == nullptr);
kosak53d49dc2015-01-08 03:03:09 +0000636}
kosak53d49dc2015-01-08 03:03:09 +0000637
shiqiane35fdd92008-12-10 05:08:54 +0000638// Tests that ReturnRef(v) works for reference types.
639TEST(ReturnRefTest, WorksForReference) {
640 const int n = 0;
641 const Action<const int&(bool)> ret = ReturnRef(n); // NOLINT
642
Abseil Team7d3b73c2018-10-09 14:50:26 -0400643 EXPECT_EQ(&n, &ret.Perform(std::make_tuple(true)));
shiqiane35fdd92008-12-10 05:08:54 +0000644}
645
646// Tests that ReturnRef(v) is covariant.
647TEST(ReturnRefTest, IsCovariant) {
648 Base base;
649 Derived derived;
650 Action<Base&()> a = ReturnRef(base);
Abseil Team7d3b73c2018-10-09 14:50:26 -0400651 EXPECT_EQ(&base, &a.Perform(std::make_tuple()));
shiqiane35fdd92008-12-10 05:08:54 +0000652
653 a = ReturnRef(derived);
Abseil Team7d3b73c2018-10-09 14:50:26 -0400654 EXPECT_EQ(&derived, &a.Perform(std::make_tuple()));
shiqiane35fdd92008-12-10 05:08:54 +0000655}
656
zhanyong.wane3bd0982010-07-03 00:16:42 +0000657// Tests that ReturnRefOfCopy(v) works for reference types.
658TEST(ReturnRefOfCopyTest, WorksForReference) {
659 int n = 42;
660 const Action<const int&()> ret = ReturnRefOfCopy(n);
661
Abseil Team7d3b73c2018-10-09 14:50:26 -0400662 EXPECT_NE(&n, &ret.Perform(std::make_tuple()));
663 EXPECT_EQ(42, ret.Perform(std::make_tuple()));
zhanyong.wane3bd0982010-07-03 00:16:42 +0000664
665 n = 43;
Abseil Team7d3b73c2018-10-09 14:50:26 -0400666 EXPECT_NE(&n, &ret.Perform(std::make_tuple()));
667 EXPECT_EQ(42, ret.Perform(std::make_tuple()));
zhanyong.wane3bd0982010-07-03 00:16:42 +0000668}
669
670// Tests that ReturnRefOfCopy(v) is covariant.
671TEST(ReturnRefOfCopyTest, IsCovariant) {
672 Base base;
673 Derived derived;
674 Action<Base&()> a = ReturnRefOfCopy(base);
Abseil Team7d3b73c2018-10-09 14:50:26 -0400675 EXPECT_NE(&base, &a.Perform(std::make_tuple()));
zhanyong.wane3bd0982010-07-03 00:16:42 +0000676
677 a = ReturnRefOfCopy(derived);
Abseil Team7d3b73c2018-10-09 14:50:26 -0400678 EXPECT_NE(&derived, &a.Perform(std::make_tuple()));
zhanyong.wane3bd0982010-07-03 00:16:42 +0000679}
680
shiqiane35fdd92008-12-10 05:08:54 +0000681// Tests that DoDefault() does the default action for the mock method.
682
shiqiane35fdd92008-12-10 05:08:54 +0000683class MockClass {
684 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000685 MockClass() {}
686
shiqiane35fdd92008-12-10 05:08:54 +0000687 MOCK_METHOD1(IntFunc, int(bool flag)); // NOLINT
kosakd478a1f2015-02-14 02:45:40 +0000688 MOCK_METHOD0(Foo, MyNonDefaultConstructible());
kosakb5c81092014-01-29 06:41:44 +0000689 MOCK_METHOD0(MakeUnique, std::unique_ptr<int>());
kosak3d1c78b2014-11-17 00:56:52 +0000690 MOCK_METHOD0(MakeUniqueBase, std::unique_ptr<Base>());
kosakb5c81092014-01-29 06:41:44 +0000691 MOCK_METHOD0(MakeVectorUnique, std::vector<std::unique_ptr<int>>());
Gennadiy Civilfe402c22018-04-05 16:09:17 -0400692 MOCK_METHOD1(TakeUnique, int(std::unique_ptr<int>));
Gennadiy Civil0bfa8232018-04-13 11:02:25 -0400693 MOCK_METHOD2(TakeUnique,
694 int(const std::unique_ptr<int>&, std::unique_ptr<int>));
zhanyong.wan32de5f52009-12-23 00:13:23 +0000695
696 private:
697 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockClass);
shiqiane35fdd92008-12-10 05:08:54 +0000698};
699
700// Tests that DoDefault() returns the built-in default value for the
701// return type by default.
702TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) {
703 MockClass mock;
704 EXPECT_CALL(mock, IntFunc(_))
705 .WillOnce(DoDefault());
706 EXPECT_EQ(0, mock.IntFunc(true));
707}
708
zhanyong.wanedd4ab42013-02-28 22:58:51 +0000709// Tests that DoDefault() throws (when exceptions are enabled) or aborts
710// the process when there is no built-in default value for the return type.
shiqiane35fdd92008-12-10 05:08:54 +0000711TEST(DoDefaultDeathTest, DiesForUnknowType) {
712 MockClass mock;
713 EXPECT_CALL(mock, Foo())
714 .WillRepeatedly(DoDefault());
zhanyong.wanedd4ab42013-02-28 22:58:51 +0000715#if GTEST_HAS_EXCEPTIONS
716 EXPECT_ANY_THROW(mock.Foo());
717#else
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000718 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000719 mock.Foo();
720 }, "");
zhanyong.wanedd4ab42013-02-28 22:58:51 +0000721#endif
shiqiane35fdd92008-12-10 05:08:54 +0000722}
723
724// Tests that using DoDefault() inside a composite action leads to a
725// run-time error.
726
zhanyong.wan32de5f52009-12-23 00:13:23 +0000727void VoidFunc(bool /* flag */) {}
shiqiane35fdd92008-12-10 05:08:54 +0000728
729TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
730 MockClass mock;
731 EXPECT_CALL(mock, IntFunc(_))
732 .WillRepeatedly(DoAll(Invoke(VoidFunc),
733 DoDefault()));
734
735 // Ideally we should verify the error message as well. Sadly,
736 // EXPECT_DEATH() can only capture stderr, while Google Mock's
737 // errors are printed on stdout. Therefore we have to settle for
738 // not verifying the message.
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000739 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000740 mock.IntFunc(true);
741 }, "");
742}
743
shiqiane35fdd92008-12-10 05:08:54 +0000744// Tests that DoDefault() returns the default value set by
Gennadiy Civil0bfa8232018-04-13 11:02:25 -0400745// DefaultValue<T>::Set() when it's not overriden by an ON_CALL().
shiqiane35fdd92008-12-10 05:08:54 +0000746TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) {
747 DefaultValue<int>::Set(1);
748 MockClass mock;
749 EXPECT_CALL(mock, IntFunc(_))
750 .WillOnce(DoDefault());
751 EXPECT_EQ(1, mock.IntFunc(false));
752 DefaultValue<int>::Clear();
753}
754
755// Tests that DoDefault() does the action specified by ON_CALL().
756TEST(DoDefaultTest, DoesWhatOnCallSpecifies) {
757 MockClass mock;
758 ON_CALL(mock, IntFunc(_))
759 .WillByDefault(Return(2));
760 EXPECT_CALL(mock, IntFunc(_))
761 .WillOnce(DoDefault());
762 EXPECT_EQ(2, mock.IntFunc(false));
763}
764
765// Tests that using DoDefault() in ON_CALL() leads to a run-time failure.
766TEST(DoDefaultTest, CannotBeUsedInOnCall) {
767 MockClass mock;
768 EXPECT_NONFATAL_FAILURE({ // NOLINT
769 ON_CALL(mock, IntFunc(_))
770 .WillByDefault(DoDefault());
771 }, "DoDefault() cannot be used in ON_CALL()");
772}
773
zhanyong.wan59214832010-10-05 05:58:51 +0000774// Tests that SetArgPointee<N>(v) sets the variable pointed to by
775// the N-th (0-based) argument to v.
776TEST(SetArgPointeeTest, SetsTheNthPointee) {
777 typedef void MyFunction(bool, int*, char*);
778 Action<MyFunction> a = SetArgPointee<1>(2);
779
780 int n = 0;
781 char ch = '\0';
Abseil Team7d3b73c2018-10-09 14:50:26 -0400782 a.Perform(std::make_tuple(true, &n, &ch));
zhanyong.wan59214832010-10-05 05:58:51 +0000783 EXPECT_EQ(2, n);
784 EXPECT_EQ('\0', ch);
785
786 a = SetArgPointee<2>('a');
787 n = 0;
788 ch = '\0';
Abseil Team7d3b73c2018-10-09 14:50:26 -0400789 a.Perform(std::make_tuple(true, &n, &ch));
zhanyong.wan59214832010-10-05 05:58:51 +0000790 EXPECT_EQ(0, n);
791 EXPECT_EQ('a', ch);
792}
793
zhanyong.wana684b5a2010-12-02 23:30:50 +0000794// Tests that SetArgPointee<N>() accepts a string literal.
795TEST(SetArgPointeeTest, AcceptsStringLiteral) {
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000796 typedef void MyFunction(std::string*, const char**);
797 Action<MyFunction> a = SetArgPointee<0>("hi");
zhanyong.wana684b5a2010-12-02 23:30:50 +0000798 std::string str;
Abseil Team4bb49ed2018-10-04 18:28:05 -0400799 const char* ptr = nullptr;
Abseil Team7d3b73c2018-10-09 14:50:26 -0400800 a.Perform(std::make_tuple(&str, &ptr));
zhanyong.wana684b5a2010-12-02 23:30:50 +0000801 EXPECT_EQ("hi", str);
Abseil Team4bb49ed2018-10-04 18:28:05 -0400802 EXPECT_TRUE(ptr == nullptr);
zhanyong.wana684b5a2010-12-02 23:30:50 +0000803
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000804 a = SetArgPointee<1>("world");
zhanyong.wana684b5a2010-12-02 23:30:50 +0000805 str = "";
Abseil Team7d3b73c2018-10-09 14:50:26 -0400806 a.Perform(std::make_tuple(&str, &ptr));
zhanyong.wana684b5a2010-12-02 23:30:50 +0000807 EXPECT_EQ("", str);
808 EXPECT_STREQ("world", ptr);
809}
810
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000811TEST(SetArgPointeeTest, AcceptsWideStringLiteral) {
812 typedef void MyFunction(const wchar_t**);
813 Action<MyFunction> a = SetArgPointee<0>(L"world");
Abseil Team4bb49ed2018-10-04 18:28:05 -0400814 const wchar_t* ptr = nullptr;
Abseil Team7d3b73c2018-10-09 14:50:26 -0400815 a.Perform(std::make_tuple(&ptr));
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000816 EXPECT_STREQ(L"world", ptr);
817
818# if GTEST_HAS_STD_WSTRING
819
820 typedef void MyStringFunction(std::wstring*);
821 Action<MyStringFunction> a2 = SetArgPointee<0>(L"world");
822 std::wstring str = L"";
Abseil Team7d3b73c2018-10-09 14:50:26 -0400823 a2.Perform(std::make_tuple(&str));
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000824 EXPECT_EQ(L"world", str);
825
826# endif
827}
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000828
zhanyong.wana684b5a2010-12-02 23:30:50 +0000829// Tests that SetArgPointee<N>() accepts a char pointer.
830TEST(SetArgPointeeTest, AcceptsCharPointer) {
831 typedef void MyFunction(bool, std::string*, const char**);
832 const char* const hi = "hi";
833 Action<MyFunction> a = SetArgPointee<1>(hi);
834 std::string str;
Abseil Team4bb49ed2018-10-04 18:28:05 -0400835 const char* ptr = nullptr;
Abseil Team7d3b73c2018-10-09 14:50:26 -0400836 a.Perform(std::make_tuple(true, &str, &ptr));
zhanyong.wana684b5a2010-12-02 23:30:50 +0000837 EXPECT_EQ("hi", str);
Abseil Team4bb49ed2018-10-04 18:28:05 -0400838 EXPECT_TRUE(ptr == nullptr);
zhanyong.wana684b5a2010-12-02 23:30:50 +0000839
840 char world_array[] = "world";
841 char* const world = world_array;
842 a = SetArgPointee<2>(world);
843 str = "";
Abseil Team7d3b73c2018-10-09 14:50:26 -0400844 a.Perform(std::make_tuple(true, &str, &ptr));
zhanyong.wana684b5a2010-12-02 23:30:50 +0000845 EXPECT_EQ("", str);
846 EXPECT_EQ(world, ptr);
847}
848
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000849TEST(SetArgPointeeTest, AcceptsWideCharPointer) {
850 typedef void MyFunction(bool, const wchar_t**);
851 const wchar_t* const hi = L"hi";
852 Action<MyFunction> a = SetArgPointee<1>(hi);
Abseil Team4bb49ed2018-10-04 18:28:05 -0400853 const wchar_t* ptr = nullptr;
Abseil Team7d3b73c2018-10-09 14:50:26 -0400854 a.Perform(std::make_tuple(true, &ptr));
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000855 EXPECT_EQ(hi, ptr);
856
857# if GTEST_HAS_STD_WSTRING
858
859 typedef void MyStringFunction(bool, std::wstring*);
860 wchar_t world_array[] = L"world";
861 wchar_t* const world = world_array;
862 Action<MyStringFunction> a2 = SetArgPointee<1>(world);
863 std::wstring str;
Abseil Team7d3b73c2018-10-09 14:50:26 -0400864 a2.Perform(std::make_tuple(true, &str));
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000865 EXPECT_EQ(world_array, str);
866# endif
867}
868
shiqiane35fdd92008-12-10 05:08:54 +0000869// Tests that SetArgumentPointee<N>(v) sets the variable pointed to by
870// the N-th (0-based) argument to v.
871TEST(SetArgumentPointeeTest, SetsTheNthPointee) {
872 typedef void MyFunction(bool, int*, char*);
873 Action<MyFunction> a = SetArgumentPointee<1>(2);
874
875 int n = 0;
876 char ch = '\0';
Abseil Team7d3b73c2018-10-09 14:50:26 -0400877 a.Perform(std::make_tuple(true, &n, &ch));
shiqiane35fdd92008-12-10 05:08:54 +0000878 EXPECT_EQ(2, n);
879 EXPECT_EQ('\0', ch);
880
881 a = SetArgumentPointee<2>('a');
882 n = 0;
883 ch = '\0';
Abseil Team7d3b73c2018-10-09 14:50:26 -0400884 a.Perform(std::make_tuple(true, &n, &ch));
shiqiane35fdd92008-12-10 05:08:54 +0000885 EXPECT_EQ(0, n);
886 EXPECT_EQ('a', ch);
887}
888
shiqiane35fdd92008-12-10 05:08:54 +0000889// Sample functions and functors for testing Invoke() and etc.
890int Nullary() { return 1; }
891
892class NullaryFunctor {
893 public:
894 int operator()() { return 2; }
895};
896
897bool g_done = false;
898void VoidNullary() { g_done = true; }
899
900class VoidNullaryFunctor {
901 public:
902 void operator()() { g_done = true; }
903};
904
Abseil Teamaac18182018-11-15 15:43:19 -0500905short Short(short n) { return n; } // NOLINT
906char Char(char ch) { return ch; }
907
908const char* CharPtr(const char* s) { return s; }
909
910bool Unary(int x) { return x < 0; }
911
912const char* Binary(const char* input, short n) { return input + n; } // NOLINT
913
914void VoidBinary(int, char) { g_done = true; }
915
916int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT
917
918int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
919
shiqiane35fdd92008-12-10 05:08:54 +0000920class Foo {
921 public:
922 Foo() : value_(123) {}
923
924 int Nullary() const { return value_; }
zhanyong.wan29be9232013-03-01 06:53:35 +0000925
shiqiane35fdd92008-12-10 05:08:54 +0000926 private:
927 int value_;
928};
929
930// Tests InvokeWithoutArgs(function).
931TEST(InvokeWithoutArgsTest, Function) {
932 // As an action that takes one argument.
933 Action<int(int)> a = InvokeWithoutArgs(Nullary); // NOLINT
Abseil Team7d3b73c2018-10-09 14:50:26 -0400934 EXPECT_EQ(1, a.Perform(std::make_tuple(2)));
shiqiane35fdd92008-12-10 05:08:54 +0000935
936 // As an action that takes two arguments.
zhanyong.wan32de5f52009-12-23 00:13:23 +0000937 Action<int(int, double)> a2 = InvokeWithoutArgs(Nullary); // NOLINT
Abseil Team7d3b73c2018-10-09 14:50:26 -0400938 EXPECT_EQ(1, a2.Perform(std::make_tuple(2, 3.5)));
shiqiane35fdd92008-12-10 05:08:54 +0000939
940 // As an action that returns void.
941 Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary); // NOLINT
942 g_done = false;
Abseil Team7d3b73c2018-10-09 14:50:26 -0400943 a3.Perform(std::make_tuple(1));
shiqiane35fdd92008-12-10 05:08:54 +0000944 EXPECT_TRUE(g_done);
945}
946
947// Tests InvokeWithoutArgs(functor).
948TEST(InvokeWithoutArgsTest, Functor) {
949 // As an action that takes no argument.
950 Action<int()> a = InvokeWithoutArgs(NullaryFunctor()); // NOLINT
Abseil Team7d3b73c2018-10-09 14:50:26 -0400951 EXPECT_EQ(2, a.Perform(std::make_tuple()));
shiqiane35fdd92008-12-10 05:08:54 +0000952
953 // As an action that takes three arguments.
zhanyong.wan32de5f52009-12-23 00:13:23 +0000954 Action<int(int, double, char)> a2 = // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +0000955 InvokeWithoutArgs(NullaryFunctor());
Abseil Team7d3b73c2018-10-09 14:50:26 -0400956 EXPECT_EQ(2, a2.Perform(std::make_tuple(3, 3.5, 'a')));
shiqiane35fdd92008-12-10 05:08:54 +0000957
958 // As an action that returns void.
959 Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor());
960 g_done = false;
Abseil Team7d3b73c2018-10-09 14:50:26 -0400961 a3.Perform(std::make_tuple());
shiqiane35fdd92008-12-10 05:08:54 +0000962 EXPECT_TRUE(g_done);
963}
964
965// Tests InvokeWithoutArgs(obj_ptr, method).
966TEST(InvokeWithoutArgsTest, Method) {
967 Foo foo;
968 Action<int(bool, char)> a = // NOLINT
969 InvokeWithoutArgs(&foo, &Foo::Nullary);
Abseil Team7d3b73c2018-10-09 14:50:26 -0400970 EXPECT_EQ(123, a.Perform(std::make_tuple(true, 'a')));
shiqiane35fdd92008-12-10 05:08:54 +0000971}
972
973// Tests using IgnoreResult() on a polymorphic action.
974TEST(IgnoreResultTest, PolymorphicAction) {
975 Action<void(int)> a = IgnoreResult(Return(5)); // NOLINT
Abseil Team7d3b73c2018-10-09 14:50:26 -0400976 a.Perform(std::make_tuple(1));
shiqiane35fdd92008-12-10 05:08:54 +0000977}
978
979// Tests using IgnoreResult() on a monomorphic action.
980
981int ReturnOne() {
982 g_done = true;
983 return 1;
984}
985
986TEST(IgnoreResultTest, MonomorphicAction) {
987 g_done = false;
988 Action<void()> a = IgnoreResult(Invoke(ReturnOne));
Abseil Team7d3b73c2018-10-09 14:50:26 -0400989 a.Perform(std::make_tuple());
shiqiane35fdd92008-12-10 05:08:54 +0000990 EXPECT_TRUE(g_done);
991}
992
993// Tests using IgnoreResult() on an action that returns a class type.
994
kosakd478a1f2015-02-14 02:45:40 +0000995MyNonDefaultConstructible ReturnMyNonDefaultConstructible(double /* x */) {
shiqiane35fdd92008-12-10 05:08:54 +0000996 g_done = true;
kosakd478a1f2015-02-14 02:45:40 +0000997 return MyNonDefaultConstructible(42);
shiqiane35fdd92008-12-10 05:08:54 +0000998}
999
1000TEST(IgnoreResultTest, ActionReturningClass) {
1001 g_done = false;
kosakd478a1f2015-02-14 02:45:40 +00001002 Action<void(int)> a =
1003 IgnoreResult(Invoke(ReturnMyNonDefaultConstructible)); // NOLINT
Abseil Team7d3b73c2018-10-09 14:50:26 -04001004 a.Perform(std::make_tuple(2));
shiqiane35fdd92008-12-10 05:08:54 +00001005 EXPECT_TRUE(g_done);
1006}
1007
1008TEST(AssignTest, Int) {
1009 int x = 0;
1010 Action<void(int)> a = Assign(&x, 5);
Abseil Team7d3b73c2018-10-09 14:50:26 -04001011 a.Perform(std::make_tuple(0));
shiqiane35fdd92008-12-10 05:08:54 +00001012 EXPECT_EQ(5, x);
1013}
1014
1015TEST(AssignTest, String) {
1016 ::std::string x;
1017 Action<void(void)> a = Assign(&x, "Hello, world");
Abseil Team7d3b73c2018-10-09 14:50:26 -04001018 a.Perform(std::make_tuple());
shiqiane35fdd92008-12-10 05:08:54 +00001019 EXPECT_EQ("Hello, world", x);
1020}
1021
1022TEST(AssignTest, CompatibleTypes) {
1023 double x = 0;
1024 Action<void(int)> a = Assign(&x, 5);
Abseil Team7d3b73c2018-10-09 14:50:26 -04001025 a.Perform(std::make_tuple(0));
shiqiane35fdd92008-12-10 05:08:54 +00001026 EXPECT_DOUBLE_EQ(5, x);
1027}
1028
Abseil Teamaac18182018-11-15 15:43:19 -05001029
1030// Tests using WithArgs and with an action that takes 1 argument.
1031TEST(WithArgsTest, OneArg) {
1032 Action<bool(double x, int n)> a = WithArgs<1>(Invoke(Unary)); // NOLINT
1033 EXPECT_TRUE(a.Perform(std::make_tuple(1.5, -1)));
1034 EXPECT_FALSE(a.Perform(std::make_tuple(1.5, 1)));
1035}
1036
1037// Tests using WithArgs with an action that takes 2 arguments.
1038TEST(WithArgsTest, TwoArgs) {
1039 Action<const char*(const char* s, double x, short n)> a = // NOLINT
1040 WithArgs<0, 2>(Invoke(Binary));
1041 const char s[] = "Hello";
1042 EXPECT_EQ(s + 2, a.Perform(std::make_tuple(CharPtr(s), 0.5, Short(2))));
1043}
1044
1045struct ConcatAll {
1046 std::string operator()() const { return {}; }
1047 template <typename... I>
1048 std::string operator()(const char* a, I... i) const {
1049 return a + ConcatAll()(i...);
1050 }
1051};
1052
1053// Tests using WithArgs with an action that takes 10 arguments.
1054TEST(WithArgsTest, TenArgs) {
1055 Action<std::string(const char*, const char*, const char*, const char*)> a =
1056 WithArgs<0, 1, 2, 3, 2, 1, 0, 1, 2, 3>(Invoke(ConcatAll{}));
1057 EXPECT_EQ("0123210123",
1058 a.Perform(std::make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
1059 CharPtr("3"))));
1060}
1061
1062// Tests using WithArgs with an action that is not Invoke().
1063class SubtractAction : public ActionInterface<int(int, int)> {
1064 public:
Abseil Team26743362018-12-03 11:30:02 -05001065 int Perform(const std::tuple<int, int>& args) override {
Abseil Teamaac18182018-11-15 15:43:19 -05001066 return std::get<0>(args) - std::get<1>(args);
1067 }
1068};
1069
1070TEST(WithArgsTest, NonInvokeAction) {
1071 Action<int(const std::string&, int, int)> a =
1072 WithArgs<2, 1>(MakeAction(new SubtractAction));
1073 std::tuple<std::string, int, int> dummy =
1074 std::make_tuple(std::string("hi"), 2, 10);
1075 EXPECT_EQ(8, a.Perform(dummy));
1076}
1077
1078// Tests using WithArgs to pass all original arguments in the original order.
1079TEST(WithArgsTest, Identity) {
1080 Action<int(int x, char y, short z)> a = // NOLINT
1081 WithArgs<0, 1, 2>(Invoke(Ternary));
1082 EXPECT_EQ(123, a.Perform(std::make_tuple(100, Char(20), Short(3))));
1083}
1084
1085// Tests using WithArgs with repeated arguments.
1086TEST(WithArgsTest, RepeatedArguments) {
1087 Action<int(bool, int m, int n)> a = // NOLINT
1088 WithArgs<1, 1, 1, 1>(Invoke(SumOf4));
1089 EXPECT_EQ(4, a.Perform(std::make_tuple(false, 1, 10)));
1090}
1091
1092// Tests using WithArgs with reversed argument order.
1093TEST(WithArgsTest, ReversedArgumentOrder) {
1094 Action<const char*(short n, const char* input)> a = // NOLINT
1095 WithArgs<1, 0>(Invoke(Binary));
1096 const char s[] = "Hello";
1097 EXPECT_EQ(s + 2, a.Perform(std::make_tuple(Short(2), CharPtr(s))));
1098}
1099
1100// Tests using WithArgs with compatible, but not identical, argument types.
1101TEST(WithArgsTest, ArgsOfCompatibleTypes) {
1102 Action<long(short x, char y, double z, char c)> a = // NOLINT
1103 WithArgs<0, 1, 3>(Invoke(Ternary));
1104 EXPECT_EQ(123,
1105 a.Perform(std::make_tuple(Short(100), Char(20), 5.6, Char(3))));
1106}
1107
1108// Tests using WithArgs with an action that returns void.
1109TEST(WithArgsTest, VoidAction) {
1110 Action<void(double x, char c, int n)> a = WithArgs<2, 1>(Invoke(VoidBinary));
1111 g_done = false;
1112 a.Perform(std::make_tuple(1.5, 'a', 3));
1113 EXPECT_TRUE(g_done);
1114}
1115
1116TEST(WithArgsTest, ReturnReference) {
misterga3013cc2018-11-20 10:42:17 -05001117 Action<int&(int&, void*)> aa = WithArgs<0>([](int& a) -> int& { return a; });
Abseil Teamaac18182018-11-15 15:43:19 -05001118 int i = 0;
misterga3013cc2018-11-20 10:42:17 -05001119 const int& res = aa.Perform(std::forward_as_tuple(i, nullptr));
Abseil Teamaac18182018-11-15 15:43:19 -05001120 EXPECT_EQ(&i, &res);
1121}
1122
1123TEST(WithArgsTest, InnerActionWithConversion) {
Abseil Teamaac18182018-11-15 15:43:19 -05001124 Action<Derived*()> inner = [] { return nullptr; };
1125 Action<Base*(double)> a = testing::WithoutArgs(inner);
1126 EXPECT_EQ(nullptr, a.Perform(std::make_tuple(1.1)));
1127}
1128
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001129#if !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001130
shiqiane35fdd92008-12-10 05:08:54 +00001131class SetErrnoAndReturnTest : public testing::Test {
1132 protected:
Abseil Team26743362018-12-03 11:30:02 -05001133 void SetUp() override { errno = 0; }
1134 void TearDown() override { errno = 0; }
shiqiane35fdd92008-12-10 05:08:54 +00001135};
1136
1137TEST_F(SetErrnoAndReturnTest, Int) {
1138 Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5);
Abseil Team7d3b73c2018-10-09 14:50:26 -04001139 EXPECT_EQ(-5, a.Perform(std::make_tuple()));
shiqiane35fdd92008-12-10 05:08:54 +00001140 EXPECT_EQ(ENOTTY, errno);
1141}
1142
1143TEST_F(SetErrnoAndReturnTest, Ptr) {
1144 int x;
1145 Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x);
Abseil Team7d3b73c2018-10-09 14:50:26 -04001146 EXPECT_EQ(&x, a.Perform(std::make_tuple()));
shiqiane35fdd92008-12-10 05:08:54 +00001147 EXPECT_EQ(ENOTTY, errno);
1148}
1149
1150TEST_F(SetErrnoAndReturnTest, CompatibleTypes) {
1151 Action<double()> a = SetErrnoAndReturn(EINVAL, 5);
Abseil Team7d3b73c2018-10-09 14:50:26 -04001152 EXPECT_DOUBLE_EQ(5.0, a.Perform(std::make_tuple()));
shiqiane35fdd92008-12-10 05:08:54 +00001153 EXPECT_EQ(EINVAL, errno);
1154}
1155
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001156#endif // !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001157
zhanyong.wana18423e2009-07-22 23:58:19 +00001158// Tests ByRef().
1159
Abseil Team097407f2019-01-12 12:26:37 -05001160// Tests that the result of ByRef() is copyable.
zhanyong.wana18423e2009-07-22 23:58:19 +00001161TEST(ByRefTest, IsCopyable) {
1162 const std::string s1 = "Hi";
1163 const std::string s2 = "Hello";
1164
Abseil Team097407f2019-01-12 12:26:37 -05001165 auto ref_wrapper = ByRef(s1);
zhanyong.wana18423e2009-07-22 23:58:19 +00001166 const std::string& r1 = ref_wrapper;
1167 EXPECT_EQ(&s1, &r1);
1168
1169 // Assigns a new value to ref_wrapper.
1170 ref_wrapper = ByRef(s2);
1171 const std::string& r2 = ref_wrapper;
1172 EXPECT_EQ(&s2, &r2);
1173
Abseil Team097407f2019-01-12 12:26:37 -05001174 auto ref_wrapper1 = ByRef(s1);
zhanyong.wana18423e2009-07-22 23:58:19 +00001175 // Copies ref_wrapper1 to ref_wrapper.
1176 ref_wrapper = ref_wrapper1;
1177 const std::string& r3 = ref_wrapper;
1178 EXPECT_EQ(&s1, &r3);
1179}
1180
1181// Tests using ByRef() on a const value.
1182TEST(ByRefTest, ConstValue) {
1183 const int n = 0;
1184 // int& ref = ByRef(n); // This shouldn't compile - we have a
1185 // negative compilation test to catch it.
1186 const int& const_ref = ByRef(n);
1187 EXPECT_EQ(&n, &const_ref);
1188}
1189
1190// Tests using ByRef() on a non-const value.
1191TEST(ByRefTest, NonConstValue) {
1192 int n = 0;
1193
1194 // ByRef(n) can be used as either an int&,
1195 int& ref = ByRef(n);
1196 EXPECT_EQ(&n, &ref);
1197
1198 // or a const int&.
1199 const int& const_ref = ByRef(n);
1200 EXPECT_EQ(&n, &const_ref);
1201}
1202
1203// Tests explicitly specifying the type when using ByRef().
1204TEST(ByRefTest, ExplicitType) {
1205 int n = 0;
1206 const int& r1 = ByRef<const int>(n);
1207 EXPECT_EQ(&n, &r1);
1208
1209 // ByRef<char>(n); // This shouldn't compile - we have a negative
1210 // compilation test to catch it.
1211
1212 Derived d;
1213 Derived& r2 = ByRef<Derived>(d);
1214 EXPECT_EQ(&d, &r2);
1215
1216 const Derived& r3 = ByRef<const Derived>(d);
1217 EXPECT_EQ(&d, &r3);
1218
1219 Base& r4 = ByRef<Base>(d);
1220 EXPECT_EQ(&d, &r4);
1221
1222 const Base& r5 = ByRef<const Base>(d);
1223 EXPECT_EQ(&d, &r5);
1224
1225 // The following shouldn't compile - we have a negative compilation
1226 // test for it.
1227 //
1228 // Base b;
1229 // ByRef<Derived>(b);
1230}
1231
1232// Tests that Google Mock prints expression ByRef(x) as a reference to x.
1233TEST(ByRefTest, PrintsCorrectly) {
1234 int n = 42;
1235 ::std::stringstream expected, actual;
1236 testing::internal::UniversalPrinter<const int&>::Print(n, &expected);
1237 testing::internal::UniversalPrint(ByRef(n), &actual);
1238 EXPECT_EQ(expected.str(), actual.str());
1239}
1240
kosakb5c81092014-01-29 06:41:44 +00001241
1242std::unique_ptr<int> UniquePtrSource() {
1243 return std::unique_ptr<int>(new int(19));
1244}
1245
1246std::vector<std::unique_ptr<int>> VectorUniquePtrSource() {
1247 std::vector<std::unique_ptr<int>> out;
1248 out.emplace_back(new int(7));
1249 return out;
1250}
1251
kosak3d1c78b2014-11-17 00:56:52 +00001252TEST(MockMethodTest, CanReturnMoveOnlyValue_Return) {
1253 MockClass mock;
1254 std::unique_ptr<int> i(new int(19));
1255 EXPECT_CALL(mock, MakeUnique()).WillOnce(Return(ByMove(std::move(i))));
1256 EXPECT_CALL(mock, MakeVectorUnique())
1257 .WillOnce(Return(ByMove(VectorUniquePtrSource())));
1258 Derived* d = new Derived;
1259 EXPECT_CALL(mock, MakeUniqueBase())
1260 .WillOnce(Return(ByMove(std::unique_ptr<Derived>(d))));
1261
1262 std::unique_ptr<int> result1 = mock.MakeUnique();
1263 EXPECT_EQ(19, *result1);
1264
1265 std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
kosak389bad62014-11-17 01:08:51 +00001266 EXPECT_EQ(1u, vresult.size());
kosak3d1c78b2014-11-17 00:56:52 +00001267 EXPECT_NE(nullptr, vresult[0]);
1268 EXPECT_EQ(7, *vresult[0]);
1269
1270 std::unique_ptr<Base> result2 = mock.MakeUniqueBase();
1271 EXPECT_EQ(d, result2.get());
1272}
1273
1274TEST(MockMethodTest, CanReturnMoveOnlyValue_DoAllReturn) {
1275 testing::MockFunction<void()> mock_function;
1276 MockClass mock;
1277 std::unique_ptr<int> i(new int(19));
1278 EXPECT_CALL(mock_function, Call());
1279 EXPECT_CALL(mock, MakeUnique()).WillOnce(DoAll(
1280 InvokeWithoutArgs(&mock_function, &testing::MockFunction<void()>::Call),
1281 Return(ByMove(std::move(i)))));
1282
1283 std::unique_ptr<int> result1 = mock.MakeUnique();
1284 EXPECT_EQ(19, *result1);
1285}
1286
1287TEST(MockMethodTest, CanReturnMoveOnlyValue_Invoke) {
kosakb5c81092014-01-29 06:41:44 +00001288 MockClass mock;
1289
1290 // Check default value
1291 DefaultValue<std::unique_ptr<int>>::SetFactory([] {
1292 return std::unique_ptr<int>(new int(42));
1293 });
1294 EXPECT_EQ(42, *mock.MakeUnique());
1295
kosak3d1c78b2014-11-17 00:56:52 +00001296 EXPECT_CALL(mock, MakeUnique()).WillRepeatedly(Invoke(UniquePtrSource));
kosakb5c81092014-01-29 06:41:44 +00001297 EXPECT_CALL(mock, MakeVectorUnique())
1298 .WillRepeatedly(Invoke(VectorUniquePtrSource));
1299 std::unique_ptr<int> result1 = mock.MakeUnique();
1300 EXPECT_EQ(19, *result1);
1301 std::unique_ptr<int> result2 = mock.MakeUnique();
1302 EXPECT_EQ(19, *result2);
1303 EXPECT_NE(result1, result2);
1304
1305 std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
kosak389bad62014-11-17 01:08:51 +00001306 EXPECT_EQ(1u, vresult.size());
kosakb5c81092014-01-29 06:41:44 +00001307 EXPECT_NE(nullptr, vresult[0]);
1308 EXPECT_EQ(7, *vresult[0]);
1309}
1310
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001311TEST(MockMethodTest, CanTakeMoveOnlyValue) {
1312 MockClass mock;
1313 auto make = [](int i) { return std::unique_ptr<int>(new int(i)); };
1314
1315 EXPECT_CALL(mock, TakeUnique(_)).WillRepeatedly([](std::unique_ptr<int> i) {
1316 return *i;
1317 });
1318 // DoAll() does not compile, since it would move from its arguments twice.
1319 // EXPECT_CALL(mock, TakeUnique(_, _))
1320 // .WillRepeatedly(DoAll(Invoke([](std::unique_ptr<int> j) {}),
1321 // Return(1)));
1322 EXPECT_CALL(mock, TakeUnique(testing::Pointee(7)))
1323 .WillOnce(Return(-7))
1324 .RetiresOnSaturation();
1325 EXPECT_CALL(mock, TakeUnique(testing::IsNull()))
1326 .WillOnce(Return(-1))
1327 .RetiresOnSaturation();
1328
1329 EXPECT_EQ(5, mock.TakeUnique(make(5)));
1330 EXPECT_EQ(-7, mock.TakeUnique(make(7)));
1331 EXPECT_EQ(7, mock.TakeUnique(make(7)));
1332 EXPECT_EQ(7, mock.TakeUnique(make(7)));
1333 EXPECT_EQ(-1, mock.TakeUnique({}));
1334
1335 // Some arguments are moved, some passed by reference.
1336 auto lvalue = make(6);
1337 EXPECT_CALL(mock, TakeUnique(_, _))
1338 .WillOnce([](const std::unique_ptr<int>& i, std::unique_ptr<int> j) {
1339 return *i * *j;
1340 });
1341 EXPECT_EQ(42, mock.TakeUnique(lvalue, make(7)));
1342
1343 // The unique_ptr can be saved by the action.
1344 std::unique_ptr<int> saved;
1345 EXPECT_CALL(mock, TakeUnique(_)).WillOnce([&saved](std::unique_ptr<int> i) {
1346 saved = std::move(i);
1347 return 0;
1348 });
1349 EXPECT_EQ(0, mock.TakeUnique(make(42)));
1350 EXPECT_EQ(42, *saved);
1351}
1352
kosakb5c81092014-01-29 06:41:44 +00001353
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001354// Tests for std::function based action.
1355
1356int Add(int val, int& ref, int* ptr) { // NOLINT
1357 int result = val + ref + *ptr;
1358 ref = 42;
1359 *ptr = 43;
1360 return result;
1361}
1362
1363int Deref(std::unique_ptr<int> ptr) { return *ptr; }
1364
1365struct Double {
1366 template <typename T>
1367 T operator()(T t) { return 2 * t; }
1368};
1369
1370std::unique_ptr<int> UniqueInt(int i) {
1371 return std::unique_ptr<int>(new int(i));
1372}
1373
1374TEST(FunctorActionTest, ActionFromFunction) {
1375 Action<int(int, int&, int*)> a = &Add;
1376 int x = 1, y = 2, z = 3;
1377 EXPECT_EQ(6, a.Perform(std::forward_as_tuple(x, y, &z)));
1378 EXPECT_EQ(42, y);
1379 EXPECT_EQ(43, z);
1380
1381 Action<int(std::unique_ptr<int>)> a1 = &Deref;
1382 EXPECT_EQ(7, a1.Perform(std::make_tuple(UniqueInt(7))));
1383}
1384
1385TEST(FunctorActionTest, ActionFromLambda) {
1386 Action<int(bool, int)> a1 = [](bool b, int i) { return b ? i : 0; };
Abseil Team7d3b73c2018-10-09 14:50:26 -04001387 EXPECT_EQ(5, a1.Perform(std::make_tuple(true, 5)));
1388 EXPECT_EQ(0, a1.Perform(std::make_tuple(false, 5)));
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001389
1390 std::unique_ptr<int> saved;
1391 Action<void(std::unique_ptr<int>)> a2 = [&saved](std::unique_ptr<int> p) {
1392 saved = std::move(p);
1393 };
Abseil Team7d3b73c2018-10-09 14:50:26 -04001394 a2.Perform(std::make_tuple(UniqueInt(5)));
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001395 EXPECT_EQ(5, *saved);
1396}
1397
1398TEST(FunctorActionTest, PolymorphicFunctor) {
1399 Action<int(int)> ai = Double();
Abseil Team7d3b73c2018-10-09 14:50:26 -04001400 EXPECT_EQ(2, ai.Perform(std::make_tuple(1)));
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001401 Action<double(double)> ad = Double(); // Double? Double double!
Abseil Team7d3b73c2018-10-09 14:50:26 -04001402 EXPECT_EQ(3.0, ad.Perform(std::make_tuple(1.5)));
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001403}
1404
1405TEST(FunctorActionTest, TypeConversion) {
1406 // Numeric promotions are allowed.
1407 const Action<bool(int)> a1 = [](int i) { return i > 1; };
1408 const Action<int(bool)> a2 = Action<int(bool)>(a1);
Abseil Team7d3b73c2018-10-09 14:50:26 -04001409 EXPECT_EQ(1, a1.Perform(std::make_tuple(42)));
1410 EXPECT_EQ(0, a2.Perform(std::make_tuple(42)));
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001411
1412 // Implicit constructors are allowed.
1413 const Action<bool(std::string)> s1 = [](std::string s) { return !s.empty(); };
1414 const Action<int(const char*)> s2 = Action<int(const char*)>(s1);
Abseil Team7d3b73c2018-10-09 14:50:26 -04001415 EXPECT_EQ(0, s2.Perform(std::make_tuple("")));
1416 EXPECT_EQ(1, s2.Perform(std::make_tuple("hello")));
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001417
1418 // Also between the lambda and the action itself.
1419 const Action<bool(std::string)> x = [](Unused) { return 42; };
Abseil Team7d3b73c2018-10-09 14:50:26 -04001420 EXPECT_TRUE(x.Perform(std::make_tuple("hello")));
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001421}
1422
1423TEST(FunctorActionTest, UnusedArguments) {
1424 // Verify that users can ignore uninteresting arguments.
Gennadiy Civil1c6e68c2018-04-16 10:34:07 -04001425 Action<int(int, double y, double z)> a =
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001426 [](int i, Unused, Unused) { return 2 * i; };
Abseil Team7d3b73c2018-10-09 14:50:26 -04001427 std::tuple<int, double, double> dummy = std::make_tuple(3, 7.3, 9.44);
Gennadiy Civilb74a1af2018-04-13 11:49:37 -04001428 EXPECT_EQ(6, a.Perform(dummy));
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001429}
1430
1431// Test that basic built-in actions work with move-only arguments.
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001432TEST(MoveOnlyArgumentsTest, ReturningActions) {
1433 Action<int(std::unique_ptr<int>)> a = Return(1);
Abseil Team7d3b73c2018-10-09 14:50:26 -04001434 EXPECT_EQ(1, a.Perform(std::make_tuple(nullptr)));
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001435
1436 a = testing::WithoutArgs([]() { return 7; });
Abseil Team7d3b73c2018-10-09 14:50:26 -04001437 EXPECT_EQ(7, a.Perform(std::make_tuple(nullptr)));
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001438
1439 Action<void(std::unique_ptr<int>, int*)> a2 = testing::SetArgPointee<1>(3);
1440 int x = 0;
Abseil Team7d3b73c2018-10-09 14:50:26 -04001441 a2.Perform(std::make_tuple(nullptr, &x));
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001442 EXPECT_EQ(x, 3);
1443}
1444
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001445
shiqiane35fdd92008-12-10 05:08:54 +00001446} // Unnamed namespace
Gennadiy Civile1071eb2018-04-10 15:57:16 -04001447
1448#ifdef _MSC_VER
1449#if _MSC_VER == 1900
1450# pragma warning(pop)
1451#endif
1452#endif
1453