blob: f63c8c5af9263f14c9da41fc8ed0cf5c63dce63d [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_WCHAR_T_IS_NATIVE_
Scott Graham567b40e2018-02-23 12:28:09 -0800109#if !defined(__WCHAR_UNSIGNED__)
shiqiane35fdd92008-12-10 05:08:54 +0000110 EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get());
Scott Graham567b40e2018-02-23 12:28:09 -0800111#else
112 EXPECT_EQ(0U, BuiltInDefaultValue<wchar_t>::Get());
113#endif
zhanyong.wan95b12332009-09-25 18:55:50 +0000114#endif
zhanyong.wan32de5f52009-12-23 00:13:23 +0000115 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned short>::Get()); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +0000116 EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get()); // NOLINT
117 EXPECT_EQ(0, BuiltInDefaultValue<short>::Get()); // NOLINT
zhanyong.wan32de5f52009-12-23 00:13:23 +0000118 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned int>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000119 EXPECT_EQ(0, BuiltInDefaultValue<signed int>::Get());
120 EXPECT_EQ(0, BuiltInDefaultValue<int>::Get());
zhanyong.wan32de5f52009-12-23 00:13:23 +0000121 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned long>::Get()); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +0000122 EXPECT_EQ(0, BuiltInDefaultValue<signed long>::Get()); // NOLINT
123 EXPECT_EQ(0, BuiltInDefaultValue<long>::Get()); // NOLINT
zhanyong.wan32de5f52009-12-23 00:13:23 +0000124 EXPECT_EQ(0U, BuiltInDefaultValue<UInt64>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000125 EXPECT_EQ(0, BuiltInDefaultValue<Int64>::Get());
126 EXPECT_EQ(0, BuiltInDefaultValue<float>::Get());
127 EXPECT_EQ(0, BuiltInDefaultValue<double>::Get());
128}
129
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000130// Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
131// built-in numeric type.
132TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) {
133 EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists());
134 EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists());
135 EXPECT_TRUE(BuiltInDefaultValue<char>::Exists());
zhanyong.wan95b12332009-09-25 18:55:50 +0000136#if GMOCK_WCHAR_T_IS_NATIVE_
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000137 EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists());
zhanyong.wan95b12332009-09-25 18:55:50 +0000138#endif
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000139 EXPECT_TRUE(BuiltInDefaultValue<unsigned short>::Exists()); // NOLINT
140 EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists()); // NOLINT
141 EXPECT_TRUE(BuiltInDefaultValue<short>::Exists()); // NOLINT
142 EXPECT_TRUE(BuiltInDefaultValue<unsigned int>::Exists());
143 EXPECT_TRUE(BuiltInDefaultValue<signed int>::Exists());
144 EXPECT_TRUE(BuiltInDefaultValue<int>::Exists());
145 EXPECT_TRUE(BuiltInDefaultValue<unsigned long>::Exists()); // NOLINT
146 EXPECT_TRUE(BuiltInDefaultValue<signed long>::Exists()); // NOLINT
147 EXPECT_TRUE(BuiltInDefaultValue<long>::Exists()); // NOLINT
148 EXPECT_TRUE(BuiltInDefaultValue<UInt64>::Exists());
149 EXPECT_TRUE(BuiltInDefaultValue<Int64>::Exists());
150 EXPECT_TRUE(BuiltInDefaultValue<float>::Exists());
151 EXPECT_TRUE(BuiltInDefaultValue<double>::Exists());
152}
153
shiqiane35fdd92008-12-10 05:08:54 +0000154// Tests that BuiltInDefaultValue<bool>::Get() returns false.
155TEST(BuiltInDefaultValueTest, IsFalseForBool) {
156 EXPECT_FALSE(BuiltInDefaultValue<bool>::Get());
157}
158
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000159// Tests that BuiltInDefaultValue<bool>::Exists() returns true.
160TEST(BuiltInDefaultValueTest, BoolExists) {
161 EXPECT_TRUE(BuiltInDefaultValue<bool>::Exists());
162}
163
shiqiane35fdd92008-12-10 05:08:54 +0000164// Tests that BuiltInDefaultValue<T>::Get() returns "" when T is a
165// string type.
166TEST(BuiltInDefaultValueTest, IsEmptyStringForString) {
shiqiane35fdd92008-12-10 05:08:54 +0000167 EXPECT_EQ("", BuiltInDefaultValue< ::std::string>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000168}
169
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000170// Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
171// string type.
172TEST(BuiltInDefaultValueTest, ExistsForString) {
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000173 EXPECT_TRUE(BuiltInDefaultValue< ::std::string>::Exists());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000174}
175
shiqiane35fdd92008-12-10 05:08:54 +0000176// Tests that BuiltInDefaultValue<const T>::Get() returns the same
177// value as BuiltInDefaultValue<T>::Get() does.
178TEST(BuiltInDefaultValueTest, WorksForConstTypes) {
179 EXPECT_EQ("", BuiltInDefaultValue<const std::string>::Get());
180 EXPECT_EQ(0, BuiltInDefaultValue<const int>::Get());
Abseil Team4bb49ed2018-10-04 18:28:05 -0400181 EXPECT_TRUE(BuiltInDefaultValue<char* const>::Get() == nullptr);
shiqiane35fdd92008-12-10 05:08:54 +0000182 EXPECT_FALSE(BuiltInDefaultValue<const bool>::Get());
183}
184
kosakd478a1f2015-02-14 02:45:40 +0000185// A type that's default constructible.
186class MyDefaultConstructible {
187 public:
188 MyDefaultConstructible() : value_(42) {}
shiqiane35fdd92008-12-10 05:08:54 +0000189
kosakd478a1f2015-02-14 02:45:40 +0000190 int value() const { return value_; }
191
192 private:
193 int value_;
shiqiane35fdd92008-12-10 05:08:54 +0000194};
195
kosakd478a1f2015-02-14 02:45:40 +0000196// A type that's not default constructible.
197class MyNonDefaultConstructible {
198 public:
199 // Does not have a default ctor.
200 explicit MyNonDefaultConstructible(int a_value) : value_(a_value) {}
201
202 int value() const { return value_; }
203
204 private:
205 int value_;
206};
207
kosakd478a1f2015-02-14 02:45:40 +0000208
209TEST(BuiltInDefaultValueTest, ExistsForDefaultConstructibleType) {
210 EXPECT_TRUE(BuiltInDefaultValue<MyDefaultConstructible>::Exists());
211}
212
213TEST(BuiltInDefaultValueTest, IsDefaultConstructedForDefaultConstructibleType) {
214 EXPECT_EQ(42, BuiltInDefaultValue<MyDefaultConstructible>::Get().value());
215}
216
kosakd478a1f2015-02-14 02:45:40 +0000217
218TEST(BuiltInDefaultValueTest, DoesNotExistForNonDefaultConstructibleType) {
219 EXPECT_FALSE(BuiltInDefaultValue<MyNonDefaultConstructible>::Exists());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000220}
221
shiqiane35fdd92008-12-10 05:08:54 +0000222// Tests that BuiltInDefaultValue<T&>::Get() aborts the program.
223TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000224 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000225 BuiltInDefaultValue<int&>::Get();
226 }, "");
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000227 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000228 BuiltInDefaultValue<const char&>::Get();
229 }, "");
230}
231
kosakd478a1f2015-02-14 02:45:40 +0000232TEST(BuiltInDefaultValueDeathTest, IsUndefinedForNonDefaultConstructibleType) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000233 EXPECT_DEATH_IF_SUPPORTED({
kosakd478a1f2015-02-14 02:45:40 +0000234 BuiltInDefaultValue<MyNonDefaultConstructible>::Get();
shiqiane35fdd92008-12-10 05:08:54 +0000235 }, "");
236}
237
shiqiane35fdd92008-12-10 05:08:54 +0000238// Tests that DefaultValue<T>::IsSet() is false initially.
239TEST(DefaultValueTest, IsInitiallyUnset) {
240 EXPECT_FALSE(DefaultValue<int>::IsSet());
kosakd478a1f2015-02-14 02:45:40 +0000241 EXPECT_FALSE(DefaultValue<MyDefaultConstructible>::IsSet());
242 EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::IsSet());
shiqiane35fdd92008-12-10 05:08:54 +0000243}
244
245// Tests that DefaultValue<T> can be set and then unset.
246TEST(DefaultValueTest, CanBeSetAndUnset) {
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000247 EXPECT_TRUE(DefaultValue<int>::Exists());
kosakd478a1f2015-02-14 02:45:40 +0000248 EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::Exists());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000249
shiqiane35fdd92008-12-10 05:08:54 +0000250 DefaultValue<int>::Set(1);
kosakd478a1f2015-02-14 02:45:40 +0000251 DefaultValue<const MyNonDefaultConstructible>::Set(
252 MyNonDefaultConstructible(42));
shiqiane35fdd92008-12-10 05:08:54 +0000253
254 EXPECT_EQ(1, DefaultValue<int>::Get());
kosakd478a1f2015-02-14 02:45:40 +0000255 EXPECT_EQ(42, DefaultValue<const MyNonDefaultConstructible>::Get().value());
shiqiane35fdd92008-12-10 05:08:54 +0000256
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000257 EXPECT_TRUE(DefaultValue<int>::Exists());
kosakd478a1f2015-02-14 02:45:40 +0000258 EXPECT_TRUE(DefaultValue<const MyNonDefaultConstructible>::Exists());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000259
shiqiane35fdd92008-12-10 05:08:54 +0000260 DefaultValue<int>::Clear();
kosakd478a1f2015-02-14 02:45:40 +0000261 DefaultValue<const MyNonDefaultConstructible>::Clear();
shiqiane35fdd92008-12-10 05:08:54 +0000262
263 EXPECT_FALSE(DefaultValue<int>::IsSet());
kosakd478a1f2015-02-14 02:45:40 +0000264 EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::IsSet());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000265
266 EXPECT_TRUE(DefaultValue<int>::Exists());
kosakd478a1f2015-02-14 02:45:40 +0000267 EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::Exists());
shiqiane35fdd92008-12-10 05:08:54 +0000268}
269
270// Tests that DefaultValue<T>::Get() returns the
271// BuiltInDefaultValue<T>::Get() when DefaultValue<T>::IsSet() is
272// false.
273TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
274 EXPECT_FALSE(DefaultValue<int>::IsSet());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000275 EXPECT_TRUE(DefaultValue<int>::Exists());
kosakd478a1f2015-02-14 02:45:40 +0000276 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible>::IsSet());
277 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible>::Exists());
shiqiane35fdd92008-12-10 05:08:54 +0000278
279 EXPECT_EQ(0, DefaultValue<int>::Get());
280
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000281 EXPECT_DEATH_IF_SUPPORTED({
kosakd478a1f2015-02-14 02:45:40 +0000282 DefaultValue<MyNonDefaultConstructible>::Get();
shiqiane35fdd92008-12-10 05:08:54 +0000283 }, "");
shiqiane35fdd92008-12-10 05:08:54 +0000284}
285
kosakd478a1f2015-02-14 02:45:40 +0000286TEST(DefaultValueTest, GetWorksForMoveOnlyIfSet) {
287 EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Exists());
Abseil Team4bb49ed2018-10-04 18:28:05 -0400288 EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Get() == nullptr);
kosakb5c81092014-01-29 06:41:44 +0000289 DefaultValue<std::unique_ptr<int>>::SetFactory([] {
290 return std::unique_ptr<int>(new int(42));
291 });
292 EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Exists());
293 std::unique_ptr<int> i = DefaultValue<std::unique_ptr<int>>::Get();
294 EXPECT_EQ(42, *i);
295}
kosakb5c81092014-01-29 06:41:44 +0000296
shiqiane35fdd92008-12-10 05:08:54 +0000297// Tests that DefaultValue<void>::Get() returns void.
298TEST(DefaultValueTest, GetWorksForVoid) {
299 return DefaultValue<void>::Get();
300}
301
302// Tests using DefaultValue with a reference type.
303
304// Tests that DefaultValue<T&>::IsSet() is false initially.
305TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) {
306 EXPECT_FALSE(DefaultValue<int&>::IsSet());
kosakd478a1f2015-02-14 02:45:40 +0000307 EXPECT_FALSE(DefaultValue<MyDefaultConstructible&>::IsSet());
308 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
shiqiane35fdd92008-12-10 05:08:54 +0000309}
310
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000311// Tests that DefaultValue<T&>::Exists is false initiallly.
312TEST(DefaultValueOfReferenceTest, IsInitiallyNotExisting) {
313 EXPECT_FALSE(DefaultValue<int&>::Exists());
kosakd478a1f2015-02-14 02:45:40 +0000314 EXPECT_FALSE(DefaultValue<MyDefaultConstructible&>::Exists());
315 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::Exists());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000316}
317
shiqiane35fdd92008-12-10 05:08:54 +0000318// Tests that DefaultValue<T&> can be set and then unset.
319TEST(DefaultValueOfReferenceTest, CanBeSetAndUnset) {
320 int n = 1;
321 DefaultValue<const int&>::Set(n);
kosakd478a1f2015-02-14 02:45:40 +0000322 MyNonDefaultConstructible x(42);
323 DefaultValue<MyNonDefaultConstructible&>::Set(x);
shiqiane35fdd92008-12-10 05:08:54 +0000324
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000325 EXPECT_TRUE(DefaultValue<const int&>::Exists());
kosakd478a1f2015-02-14 02:45:40 +0000326 EXPECT_TRUE(DefaultValue<MyNonDefaultConstructible&>::Exists());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000327
shiqiane35fdd92008-12-10 05:08:54 +0000328 EXPECT_EQ(&n, &(DefaultValue<const int&>::Get()));
kosakd478a1f2015-02-14 02:45:40 +0000329 EXPECT_EQ(&x, &(DefaultValue<MyNonDefaultConstructible&>::Get()));
shiqiane35fdd92008-12-10 05:08:54 +0000330
331 DefaultValue<const int&>::Clear();
kosakd478a1f2015-02-14 02:45:40 +0000332 DefaultValue<MyNonDefaultConstructible&>::Clear();
shiqiane35fdd92008-12-10 05:08:54 +0000333
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000334 EXPECT_FALSE(DefaultValue<const int&>::Exists());
kosakd478a1f2015-02-14 02:45:40 +0000335 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::Exists());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000336
shiqiane35fdd92008-12-10 05:08:54 +0000337 EXPECT_FALSE(DefaultValue<const int&>::IsSet());
kosakd478a1f2015-02-14 02:45:40 +0000338 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
shiqiane35fdd92008-12-10 05:08:54 +0000339}
340
341// Tests that DefaultValue<T&>::Get() returns the
342// BuiltInDefaultValue<T&>::Get() when DefaultValue<T&>::IsSet() is
343// false.
344TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
345 EXPECT_FALSE(DefaultValue<int&>::IsSet());
kosakd478a1f2015-02-14 02:45:40 +0000346 EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
shiqiane35fdd92008-12-10 05:08:54 +0000347
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000348 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000349 DefaultValue<int&>::Get();
350 }, "");
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000351 EXPECT_DEATH_IF_SUPPORTED({
kosakd478a1f2015-02-14 02:45:40 +0000352 DefaultValue<MyNonDefaultConstructible>::Get();
shiqiane35fdd92008-12-10 05:08:54 +0000353 }, "");
shiqiane35fdd92008-12-10 05:08:54 +0000354}
355
356// Tests that ActionInterface can be implemented by defining the
357// Perform method.
358
zhanyong.wana1a98f82013-03-01 21:28:40 +0000359typedef int MyGlobalFunction(bool, int);
shiqiane35fdd92008-12-10 05:08:54 +0000360
zhanyong.wana1a98f82013-03-01 21:28:40 +0000361class MyActionImpl : public ActionInterface<MyGlobalFunction> {
shiqiane35fdd92008-12-10 05:08:54 +0000362 public:
Abseil Team26743362018-12-03 11:30:02 -0500363 int Perform(const std::tuple<bool, int>& args) override {
Abseil Team7d3b73c2018-10-09 14:50:26 -0400364 return std::get<0>(args) ? std::get<1>(args) : 0;
shiqiane35fdd92008-12-10 05:08:54 +0000365 }
366};
367
368TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) {
369 MyActionImpl my_action_impl;
zhanyong.waned6c9272011-02-23 19:39:27 +0000370 (void)my_action_impl;
shiqiane35fdd92008-12-10 05:08:54 +0000371}
372
373TEST(ActionInterfaceTest, MakeAction) {
zhanyong.wana1a98f82013-03-01 21:28:40 +0000374 Action<MyGlobalFunction> action = MakeAction(new MyActionImpl);
shiqiane35fdd92008-12-10 05:08:54 +0000375
376 // When exercising the Perform() method of Action<F>, we must pass
377 // it a tuple whose size and type are compatible with F's argument
378 // types. For example, if F is int(), then Perform() takes a
379 // 0-tuple; if F is void(bool, int), then Perform() takes a
Abseil Team7d3b73c2018-10-09 14:50:26 -0400380 // std::tuple<bool, int>, and so on.
381 EXPECT_EQ(5, action.Perform(std::make_tuple(true, 5)));
shiqiane35fdd92008-12-10 05:08:54 +0000382}
383
384// Tests that Action<F> can be contructed from a pointer to
385// ActionInterface<F>.
386TEST(ActionTest, CanBeConstructedFromActionInterface) {
zhanyong.wana1a98f82013-03-01 21:28:40 +0000387 Action<MyGlobalFunction> action(new MyActionImpl);
shiqiane35fdd92008-12-10 05:08:54 +0000388}
389
390// Tests that Action<F> delegates actual work to ActionInterface<F>.
391TEST(ActionTest, DelegatesWorkToActionInterface) {
zhanyong.wana1a98f82013-03-01 21:28:40 +0000392 const Action<MyGlobalFunction> action(new MyActionImpl);
shiqiane35fdd92008-12-10 05:08:54 +0000393
Abseil Team7d3b73c2018-10-09 14:50:26 -0400394 EXPECT_EQ(5, action.Perform(std::make_tuple(true, 5)));
395 EXPECT_EQ(0, action.Perform(std::make_tuple(false, 1)));
shiqiane35fdd92008-12-10 05:08:54 +0000396}
397
398// Tests that Action<F> can be copied.
399TEST(ActionTest, IsCopyable) {
zhanyong.wana1a98f82013-03-01 21:28:40 +0000400 Action<MyGlobalFunction> a1(new MyActionImpl);
401 Action<MyGlobalFunction> a2(a1); // Tests the copy constructor.
shiqiane35fdd92008-12-10 05:08:54 +0000402
403 // a1 should continue to work after being copied from.
Abseil Team7d3b73c2018-10-09 14:50:26 -0400404 EXPECT_EQ(5, a1.Perform(std::make_tuple(true, 5)));
405 EXPECT_EQ(0, a1.Perform(std::make_tuple(false, 1)));
shiqiane35fdd92008-12-10 05:08:54 +0000406
407 // a2 should work like the action it was copied from.
Abseil Team7d3b73c2018-10-09 14:50:26 -0400408 EXPECT_EQ(5, a2.Perform(std::make_tuple(true, 5)));
409 EXPECT_EQ(0, a2.Perform(std::make_tuple(false, 1)));
shiqiane35fdd92008-12-10 05:08:54 +0000410
411 a2 = a1; // Tests the assignment operator.
412
413 // a1 should continue to work after being copied from.
Abseil Team7d3b73c2018-10-09 14:50:26 -0400414 EXPECT_EQ(5, a1.Perform(std::make_tuple(true, 5)));
415 EXPECT_EQ(0, a1.Perform(std::make_tuple(false, 1)));
shiqiane35fdd92008-12-10 05:08:54 +0000416
417 // a2 should work like the action it was copied from.
Abseil Team7d3b73c2018-10-09 14:50:26 -0400418 EXPECT_EQ(5, a2.Perform(std::make_tuple(true, 5)));
419 EXPECT_EQ(0, a2.Perform(std::make_tuple(false, 1)));
shiqiane35fdd92008-12-10 05:08:54 +0000420}
421
422// Tests that an Action<From> object can be converted to a
423// compatible Action<To> object.
424
425class IsNotZero : public ActionInterface<bool(int)> { // NOLINT
426 public:
Abseil Team26743362018-12-03 11:30:02 -0500427 bool Perform(const std::tuple<int>& arg) override {
Abseil Team7d3b73c2018-10-09 14:50:26 -0400428 return std::get<0>(arg) != 0;
shiqiane35fdd92008-12-10 05:08:54 +0000429 }
430};
431
432TEST(ActionTest, CanBeConvertedToOtherActionType) {
433 const Action<bool(int)> a1(new IsNotZero); // NOLINT
434 const Action<int(char)> a2 = Action<int(char)>(a1); // NOLINT
Abseil Team7d3b73c2018-10-09 14:50:26 -0400435 EXPECT_EQ(1, a2.Perform(std::make_tuple('a')));
436 EXPECT_EQ(0, a2.Perform(std::make_tuple('\0')));
shiqiane35fdd92008-12-10 05:08:54 +0000437}
438
439// The following two classes are for testing MakePolymorphicAction().
440
441// Implements a polymorphic action that returns the second of the
442// arguments it receives.
443class ReturnSecondArgumentAction {
444 public:
445 // We want to verify that MakePolymorphicAction() can work with a
446 // polymorphic action whose Perform() method template is either
447 // const or not. This lets us verify the non-const case.
448 template <typename Result, typename ArgumentTuple>
Abseil Team7d3b73c2018-10-09 14:50:26 -0400449 Result Perform(const ArgumentTuple& args) {
450 return std::get<1>(args);
451 }
shiqiane35fdd92008-12-10 05:08:54 +0000452};
453
454// Implements a polymorphic action that can be used in a nullary
455// function to return 0.
456class ReturnZeroFromNullaryFunctionAction {
457 public:
458 // For testing that MakePolymorphicAction() works when the
459 // implementation class' Perform() method template takes only one
460 // template parameter.
461 //
462 // We want to verify that MakePolymorphicAction() can work with a
463 // polymorphic action whose Perform() method template is either
464 // const or not. This lets us verify the const case.
465 template <typename Result>
Abseil Team7d3b73c2018-10-09 14:50:26 -0400466 Result Perform(const std::tuple<>&) const {
467 return 0;
468 }
shiqiane35fdd92008-12-10 05:08:54 +0000469};
470
471// These functions verify that MakePolymorphicAction() returns a
472// PolymorphicAction<T> where T is the argument's type.
473
474PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() {
475 return MakePolymorphicAction(ReturnSecondArgumentAction());
476}
477
478PolymorphicAction<ReturnZeroFromNullaryFunctionAction>
479ReturnZeroFromNullaryFunction() {
480 return MakePolymorphicAction(ReturnZeroFromNullaryFunctionAction());
481}
482
483// Tests that MakePolymorphicAction() turns a polymorphic action
484// implementation class into a polymorphic action.
485TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) {
486 Action<int(bool, int, double)> a1 = ReturnSecondArgument(); // NOLINT
Abseil Team7d3b73c2018-10-09 14:50:26 -0400487 EXPECT_EQ(5, a1.Perform(std::make_tuple(false, 5, 2.0)));
shiqiane35fdd92008-12-10 05:08:54 +0000488}
489
490// Tests that MakePolymorphicAction() works when the implementation
491// class' Perform() method template has only one template parameter.
492TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) {
493 Action<int()> a1 = ReturnZeroFromNullaryFunction();
Abseil Team7d3b73c2018-10-09 14:50:26 -0400494 EXPECT_EQ(0, a1.Perform(std::make_tuple()));
shiqiane35fdd92008-12-10 05:08:54 +0000495
496 Action<void*()> a2 = ReturnZeroFromNullaryFunction();
Abseil Team7d3b73c2018-10-09 14:50:26 -0400497 EXPECT_TRUE(a2.Perform(std::make_tuple()) == nullptr);
shiqiane35fdd92008-12-10 05:08:54 +0000498}
499
500// Tests that Return() works as an action for void-returning
501// functions.
502TEST(ReturnTest, WorksForVoid) {
503 const Action<void(int)> ret = Return(); // NOLINT
Abseil Team7d3b73c2018-10-09 14:50:26 -0400504 return ret.Perform(std::make_tuple(1));
shiqiane35fdd92008-12-10 05:08:54 +0000505}
506
507// Tests that Return(v) returns v.
508TEST(ReturnTest, ReturnsGivenValue) {
509 Action<int()> ret = Return(1); // NOLINT
Abseil Team7d3b73c2018-10-09 14:50:26 -0400510 EXPECT_EQ(1, ret.Perform(std::make_tuple()));
shiqiane35fdd92008-12-10 05:08:54 +0000511
512 ret = Return(-5);
Abseil Team7d3b73c2018-10-09 14:50:26 -0400513 EXPECT_EQ(-5, ret.Perform(std::make_tuple()));
shiqiane35fdd92008-12-10 05:08:54 +0000514}
515
516// Tests that Return("string literal") works.
517TEST(ReturnTest, AcceptsStringLiteral) {
518 Action<const char*()> a1 = Return("Hello");
Abseil Team7d3b73c2018-10-09 14:50:26 -0400519 EXPECT_STREQ("Hello", a1.Perform(std::make_tuple()));
shiqiane35fdd92008-12-10 05:08:54 +0000520
521 Action<std::string()> a2 = Return("world");
Abseil Team7d3b73c2018-10-09 14:50:26 -0400522 EXPECT_EQ("world", a2.Perform(std::make_tuple()));
shiqiane35fdd92008-12-10 05:08:54 +0000523}
524
kosak7123d832014-11-17 02:04:46 +0000525// Test struct which wraps a vector of integers. Used in
526// 'SupportsWrapperReturnType' test.
527struct IntegerVectorWrapper {
528 std::vector<int> * v;
529 IntegerVectorWrapper(std::vector<int>& _v) : v(&_v) {} // NOLINT
530};
531
532// Tests that Return() works when return type is a wrapper type.
533TEST(ReturnTest, SupportsWrapperReturnType) {
534 // Initialize vector of integers.
535 std::vector<int> v;
536 for (int i = 0; i < 5; ++i) v.push_back(i);
537
538 // Return() called with 'v' as argument. The Action will return the same data
539 // as 'v' (copy) but it will be wrapped in an IntegerVectorWrapper.
540 Action<IntegerVectorWrapper()> a = Return(v);
Abseil Team7d3b73c2018-10-09 14:50:26 -0400541 const std::vector<int>& result = *(a.Perform(std::make_tuple()).v);
kosak7123d832014-11-17 02:04:46 +0000542 EXPECT_THAT(result, ::testing::ElementsAre(0, 1, 2, 3, 4));
543}
544
shiqiane35fdd92008-12-10 05:08:54 +0000545// Tests that Return(v) is covaraint.
546
547struct Base {
548 bool operator==(const Base&) { return true; }
549};
550
551struct Derived : public Base {
552 bool operator==(const Derived&) { return true; }
553};
554
555TEST(ReturnTest, IsCovariant) {
556 Base base;
557 Derived derived;
558 Action<Base*()> ret = Return(&base);
Abseil Team7d3b73c2018-10-09 14:50:26 -0400559 EXPECT_EQ(&base, ret.Perform(std::make_tuple()));
shiqiane35fdd92008-12-10 05:08:54 +0000560
561 ret = Return(&derived);
Abseil Team7d3b73c2018-10-09 14:50:26 -0400562 EXPECT_EQ(&derived, ret.Perform(std::make_tuple()));
shiqiane35fdd92008-12-10 05:08:54 +0000563}
564
vladloseva070cbd2009-11-18 00:09:28 +0000565// Tests that the type of the value passed into Return is converted into T
566// when the action is cast to Action<T(...)> rather than when the action is
567// performed. See comments on testing::internal::ReturnAction in
568// gmock-actions.h for more information.
569class FromType {
570 public:
jgm79a367e2012-04-10 16:02:11 +0000571 explicit FromType(bool* is_converted) : converted_(is_converted) {}
vladloseva070cbd2009-11-18 00:09:28 +0000572 bool* converted() const { return converted_; }
573
574 private:
575 bool* const converted_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000576
577 GTEST_DISALLOW_ASSIGN_(FromType);
vladloseva070cbd2009-11-18 00:09:28 +0000578};
579
580class ToType {
581 public:
jgm79a367e2012-04-10 16:02:11 +0000582 // Must allow implicit conversion due to use in ImplicitCast_<T>.
583 ToType(const FromType& x) { *x.converted() = true; } // NOLINT
vladloseva070cbd2009-11-18 00:09:28 +0000584};
585
586TEST(ReturnTest, ConvertsArgumentWhenConverted) {
587 bool converted = false;
588 FromType x(&converted);
589 Action<ToType()> action(Return(x));
590 EXPECT_TRUE(converted) << "Return must convert its argument in its own "
591 << "conversion operator.";
592 converted = false;
Abseil Team7d3b73c2018-10-09 14:50:26 -0400593 action.Perform(std::tuple<>());
vladloseva070cbd2009-11-18 00:09:28 +0000594 EXPECT_FALSE(converted) << "Action must NOT convert its argument "
jgm79a367e2012-04-10 16:02:11 +0000595 << "when performed.";
vladloseva070cbd2009-11-18 00:09:28 +0000596}
597
vladloseva070cbd2009-11-18 00:09:28 +0000598class DestinationType {};
599
600class SourceType {
601 public:
602 // Note: a non-const typecast operator.
603 operator DestinationType() { return DestinationType(); }
604};
605
606TEST(ReturnTest, CanConvertArgumentUsingNonConstTypeCastOperator) {
607 SourceType s;
608 Action<DestinationType()> action(Return(s));
609}
vladloseva070cbd2009-11-18 00:09:28 +0000610
shiqiane35fdd92008-12-10 05:08:54 +0000611// Tests that ReturnNull() returns NULL in a pointer-returning function.
612TEST(ReturnNullTest, WorksInPointerReturningFunction) {
613 const Action<int*()> a1 = ReturnNull();
Abseil Team7d3b73c2018-10-09 14:50:26 -0400614 EXPECT_TRUE(a1.Perform(std::make_tuple()) == nullptr);
shiqiane35fdd92008-12-10 05:08:54 +0000615
616 const Action<const char*(bool)> a2 = ReturnNull(); // NOLINT
Abseil Team7d3b73c2018-10-09 14:50:26 -0400617 EXPECT_TRUE(a2.Perform(std::make_tuple(true)) == nullptr);
shiqiane35fdd92008-12-10 05:08:54 +0000618}
619
kosak53d49dc2015-01-08 03:03:09 +0000620// Tests that ReturnNull() returns NULL for shared_ptr and unique_ptr returning
621// functions.
622TEST(ReturnNullTest, WorksInSmartPointerReturningFunction) {
623 const Action<std::unique_ptr<const int>()> a1 = ReturnNull();
Abseil Team7d3b73c2018-10-09 14:50:26 -0400624 EXPECT_TRUE(a1.Perform(std::make_tuple()) == nullptr);
kosak53d49dc2015-01-08 03:03:09 +0000625
626 const Action<std::shared_ptr<int>(std::string)> a2 = ReturnNull();
Abseil Team7d3b73c2018-10-09 14:50:26 -0400627 EXPECT_TRUE(a2.Perform(std::make_tuple("foo")) == nullptr);
kosak53d49dc2015-01-08 03:03:09 +0000628}
kosak53d49dc2015-01-08 03:03:09 +0000629
shiqiane35fdd92008-12-10 05:08:54 +0000630// Tests that ReturnRef(v) works for reference types.
631TEST(ReturnRefTest, WorksForReference) {
632 const int n = 0;
633 const Action<const int&(bool)> ret = ReturnRef(n); // NOLINT
634
Abseil Team7d3b73c2018-10-09 14:50:26 -0400635 EXPECT_EQ(&n, &ret.Perform(std::make_tuple(true)));
shiqiane35fdd92008-12-10 05:08:54 +0000636}
637
638// Tests that ReturnRef(v) is covariant.
639TEST(ReturnRefTest, IsCovariant) {
640 Base base;
641 Derived derived;
642 Action<Base&()> a = ReturnRef(base);
Abseil Team7d3b73c2018-10-09 14:50:26 -0400643 EXPECT_EQ(&base, &a.Perform(std::make_tuple()));
shiqiane35fdd92008-12-10 05:08:54 +0000644
645 a = ReturnRef(derived);
Abseil Team7d3b73c2018-10-09 14:50:26 -0400646 EXPECT_EQ(&derived, &a.Perform(std::make_tuple()));
shiqiane35fdd92008-12-10 05:08:54 +0000647}
648
zhanyong.wane3bd0982010-07-03 00:16:42 +0000649// Tests that ReturnRefOfCopy(v) works for reference types.
650TEST(ReturnRefOfCopyTest, WorksForReference) {
651 int n = 42;
652 const Action<const int&()> ret = ReturnRefOfCopy(n);
653
Abseil Team7d3b73c2018-10-09 14:50:26 -0400654 EXPECT_NE(&n, &ret.Perform(std::make_tuple()));
655 EXPECT_EQ(42, ret.Perform(std::make_tuple()));
zhanyong.wane3bd0982010-07-03 00:16:42 +0000656
657 n = 43;
Abseil Team7d3b73c2018-10-09 14:50:26 -0400658 EXPECT_NE(&n, &ret.Perform(std::make_tuple()));
659 EXPECT_EQ(42, ret.Perform(std::make_tuple()));
zhanyong.wane3bd0982010-07-03 00:16:42 +0000660}
661
662// Tests that ReturnRefOfCopy(v) is covariant.
663TEST(ReturnRefOfCopyTest, IsCovariant) {
664 Base base;
665 Derived derived;
666 Action<Base&()> a = ReturnRefOfCopy(base);
Abseil Team7d3b73c2018-10-09 14:50:26 -0400667 EXPECT_NE(&base, &a.Perform(std::make_tuple()));
zhanyong.wane3bd0982010-07-03 00:16:42 +0000668
669 a = ReturnRefOfCopy(derived);
Abseil Team7d3b73c2018-10-09 14:50:26 -0400670 EXPECT_NE(&derived, &a.Perform(std::make_tuple()));
zhanyong.wane3bd0982010-07-03 00:16:42 +0000671}
672
shiqiane35fdd92008-12-10 05:08:54 +0000673// Tests that DoDefault() does the default action for the mock method.
674
shiqiane35fdd92008-12-10 05:08:54 +0000675class MockClass {
676 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000677 MockClass() {}
678
shiqiane35fdd92008-12-10 05:08:54 +0000679 MOCK_METHOD1(IntFunc, int(bool flag)); // NOLINT
kosakd478a1f2015-02-14 02:45:40 +0000680 MOCK_METHOD0(Foo, MyNonDefaultConstructible());
kosakb5c81092014-01-29 06:41:44 +0000681 MOCK_METHOD0(MakeUnique, std::unique_ptr<int>());
kosak3d1c78b2014-11-17 00:56:52 +0000682 MOCK_METHOD0(MakeUniqueBase, std::unique_ptr<Base>());
kosakb5c81092014-01-29 06:41:44 +0000683 MOCK_METHOD0(MakeVectorUnique, std::vector<std::unique_ptr<int>>());
Gennadiy Civilfe402c22018-04-05 16:09:17 -0400684 MOCK_METHOD1(TakeUnique, int(std::unique_ptr<int>));
Gennadiy Civil0bfa8232018-04-13 11:02:25 -0400685 MOCK_METHOD2(TakeUnique,
686 int(const std::unique_ptr<int>&, std::unique_ptr<int>));
zhanyong.wan32de5f52009-12-23 00:13:23 +0000687
688 private:
689 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockClass);
shiqiane35fdd92008-12-10 05:08:54 +0000690};
691
692// Tests that DoDefault() returns the built-in default value for the
693// return type by default.
694TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) {
695 MockClass mock;
696 EXPECT_CALL(mock, IntFunc(_))
697 .WillOnce(DoDefault());
698 EXPECT_EQ(0, mock.IntFunc(true));
699}
700
zhanyong.wanedd4ab42013-02-28 22:58:51 +0000701// Tests that DoDefault() throws (when exceptions are enabled) or aborts
702// the process when there is no built-in default value for the return type.
shiqiane35fdd92008-12-10 05:08:54 +0000703TEST(DoDefaultDeathTest, DiesForUnknowType) {
704 MockClass mock;
705 EXPECT_CALL(mock, Foo())
706 .WillRepeatedly(DoDefault());
zhanyong.wanedd4ab42013-02-28 22:58:51 +0000707#if GTEST_HAS_EXCEPTIONS
708 EXPECT_ANY_THROW(mock.Foo());
709#else
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000710 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000711 mock.Foo();
712 }, "");
zhanyong.wanedd4ab42013-02-28 22:58:51 +0000713#endif
shiqiane35fdd92008-12-10 05:08:54 +0000714}
715
716// Tests that using DoDefault() inside a composite action leads to a
717// run-time error.
718
zhanyong.wan32de5f52009-12-23 00:13:23 +0000719void VoidFunc(bool /* flag */) {}
shiqiane35fdd92008-12-10 05:08:54 +0000720
721TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
722 MockClass mock;
723 EXPECT_CALL(mock, IntFunc(_))
724 .WillRepeatedly(DoAll(Invoke(VoidFunc),
725 DoDefault()));
726
727 // Ideally we should verify the error message as well. Sadly,
728 // EXPECT_DEATH() can only capture stderr, while Google Mock's
729 // errors are printed on stdout. Therefore we have to settle for
730 // not verifying the message.
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000731 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000732 mock.IntFunc(true);
733 }, "");
734}
735
shiqiane35fdd92008-12-10 05:08:54 +0000736// Tests that DoDefault() returns the default value set by
Gennadiy Civil0bfa8232018-04-13 11:02:25 -0400737// DefaultValue<T>::Set() when it's not overriden by an ON_CALL().
shiqiane35fdd92008-12-10 05:08:54 +0000738TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) {
739 DefaultValue<int>::Set(1);
740 MockClass mock;
741 EXPECT_CALL(mock, IntFunc(_))
742 .WillOnce(DoDefault());
743 EXPECT_EQ(1, mock.IntFunc(false));
744 DefaultValue<int>::Clear();
745}
746
747// Tests that DoDefault() does the action specified by ON_CALL().
748TEST(DoDefaultTest, DoesWhatOnCallSpecifies) {
749 MockClass mock;
750 ON_CALL(mock, IntFunc(_))
751 .WillByDefault(Return(2));
752 EXPECT_CALL(mock, IntFunc(_))
753 .WillOnce(DoDefault());
754 EXPECT_EQ(2, mock.IntFunc(false));
755}
756
757// Tests that using DoDefault() in ON_CALL() leads to a run-time failure.
758TEST(DoDefaultTest, CannotBeUsedInOnCall) {
759 MockClass mock;
760 EXPECT_NONFATAL_FAILURE({ // NOLINT
761 ON_CALL(mock, IntFunc(_))
762 .WillByDefault(DoDefault());
763 }, "DoDefault() cannot be used in ON_CALL()");
764}
765
zhanyong.wan59214832010-10-05 05:58:51 +0000766// Tests that SetArgPointee<N>(v) sets the variable pointed to by
767// the N-th (0-based) argument to v.
768TEST(SetArgPointeeTest, SetsTheNthPointee) {
769 typedef void MyFunction(bool, int*, char*);
770 Action<MyFunction> a = SetArgPointee<1>(2);
771
772 int n = 0;
773 char ch = '\0';
Abseil Team7d3b73c2018-10-09 14:50:26 -0400774 a.Perform(std::make_tuple(true, &n, &ch));
zhanyong.wan59214832010-10-05 05:58:51 +0000775 EXPECT_EQ(2, n);
776 EXPECT_EQ('\0', ch);
777
778 a = SetArgPointee<2>('a');
779 n = 0;
780 ch = '\0';
Abseil Team7d3b73c2018-10-09 14:50:26 -0400781 a.Perform(std::make_tuple(true, &n, &ch));
zhanyong.wan59214832010-10-05 05:58:51 +0000782 EXPECT_EQ(0, n);
783 EXPECT_EQ('a', ch);
784}
785
zhanyong.wana684b5a2010-12-02 23:30:50 +0000786// Tests that SetArgPointee<N>() accepts a string literal.
787TEST(SetArgPointeeTest, AcceptsStringLiteral) {
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000788 typedef void MyFunction(std::string*, const char**);
789 Action<MyFunction> a = SetArgPointee<0>("hi");
zhanyong.wana684b5a2010-12-02 23:30:50 +0000790 std::string str;
Abseil Team4bb49ed2018-10-04 18:28:05 -0400791 const char* ptr = nullptr;
Abseil Team7d3b73c2018-10-09 14:50:26 -0400792 a.Perform(std::make_tuple(&str, &ptr));
zhanyong.wana684b5a2010-12-02 23:30:50 +0000793 EXPECT_EQ("hi", str);
Abseil Team4bb49ed2018-10-04 18:28:05 -0400794 EXPECT_TRUE(ptr == nullptr);
zhanyong.wana684b5a2010-12-02 23:30:50 +0000795
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000796 a = SetArgPointee<1>("world");
zhanyong.wana684b5a2010-12-02 23:30:50 +0000797 str = "";
Abseil Team7d3b73c2018-10-09 14:50:26 -0400798 a.Perform(std::make_tuple(&str, &ptr));
zhanyong.wana684b5a2010-12-02 23:30:50 +0000799 EXPECT_EQ("", str);
800 EXPECT_STREQ("world", ptr);
801}
802
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000803TEST(SetArgPointeeTest, AcceptsWideStringLiteral) {
804 typedef void MyFunction(const wchar_t**);
805 Action<MyFunction> a = SetArgPointee<0>(L"world");
Abseil Team4bb49ed2018-10-04 18:28:05 -0400806 const wchar_t* ptr = nullptr;
Abseil Team7d3b73c2018-10-09 14:50:26 -0400807 a.Perform(std::make_tuple(&ptr));
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000808 EXPECT_STREQ(L"world", ptr);
809
810# if GTEST_HAS_STD_WSTRING
811
812 typedef void MyStringFunction(std::wstring*);
813 Action<MyStringFunction> a2 = SetArgPointee<0>(L"world");
814 std::wstring str = L"";
Abseil Team7d3b73c2018-10-09 14:50:26 -0400815 a2.Perform(std::make_tuple(&str));
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000816 EXPECT_EQ(L"world", str);
817
818# endif
819}
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000820
zhanyong.wana684b5a2010-12-02 23:30:50 +0000821// Tests that SetArgPointee<N>() accepts a char pointer.
822TEST(SetArgPointeeTest, AcceptsCharPointer) {
823 typedef void MyFunction(bool, std::string*, const char**);
824 const char* const hi = "hi";
825 Action<MyFunction> a = SetArgPointee<1>(hi);
826 std::string str;
Abseil Team4bb49ed2018-10-04 18:28:05 -0400827 const char* ptr = nullptr;
Abseil Team7d3b73c2018-10-09 14:50:26 -0400828 a.Perform(std::make_tuple(true, &str, &ptr));
zhanyong.wana684b5a2010-12-02 23:30:50 +0000829 EXPECT_EQ("hi", str);
Abseil Team4bb49ed2018-10-04 18:28:05 -0400830 EXPECT_TRUE(ptr == nullptr);
zhanyong.wana684b5a2010-12-02 23:30:50 +0000831
832 char world_array[] = "world";
833 char* const world = world_array;
834 a = SetArgPointee<2>(world);
835 str = "";
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("", str);
838 EXPECT_EQ(world, ptr);
839}
840
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000841TEST(SetArgPointeeTest, AcceptsWideCharPointer) {
842 typedef void MyFunction(bool, const wchar_t**);
843 const wchar_t* const hi = L"hi";
844 Action<MyFunction> a = SetArgPointee<1>(hi);
Abseil Team4bb49ed2018-10-04 18:28:05 -0400845 const wchar_t* ptr = nullptr;
Abseil Team7d3b73c2018-10-09 14:50:26 -0400846 a.Perform(std::make_tuple(true, &ptr));
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000847 EXPECT_EQ(hi, ptr);
848
849# if GTEST_HAS_STD_WSTRING
850
851 typedef void MyStringFunction(bool, std::wstring*);
852 wchar_t world_array[] = L"world";
853 wchar_t* const world = world_array;
854 Action<MyStringFunction> a2 = SetArgPointee<1>(world);
855 std::wstring str;
Abseil Team7d3b73c2018-10-09 14:50:26 -0400856 a2.Perform(std::make_tuple(true, &str));
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000857 EXPECT_EQ(world_array, str);
858# endif
859}
860
shiqiane35fdd92008-12-10 05:08:54 +0000861// Tests that SetArgumentPointee<N>(v) sets the variable pointed to by
862// the N-th (0-based) argument to v.
863TEST(SetArgumentPointeeTest, SetsTheNthPointee) {
864 typedef void MyFunction(bool, int*, char*);
865 Action<MyFunction> a = SetArgumentPointee<1>(2);
866
867 int n = 0;
868 char ch = '\0';
Abseil Team7d3b73c2018-10-09 14:50:26 -0400869 a.Perform(std::make_tuple(true, &n, &ch));
shiqiane35fdd92008-12-10 05:08:54 +0000870 EXPECT_EQ(2, n);
871 EXPECT_EQ('\0', ch);
872
873 a = SetArgumentPointee<2>('a');
874 n = 0;
875 ch = '\0';
Abseil Team7d3b73c2018-10-09 14:50:26 -0400876 a.Perform(std::make_tuple(true, &n, &ch));
shiqiane35fdd92008-12-10 05:08:54 +0000877 EXPECT_EQ(0, n);
878 EXPECT_EQ('a', ch);
879}
880
shiqiane35fdd92008-12-10 05:08:54 +0000881// Sample functions and functors for testing Invoke() and etc.
882int Nullary() { return 1; }
883
884class NullaryFunctor {
885 public:
886 int operator()() { return 2; }
887};
888
889bool g_done = false;
890void VoidNullary() { g_done = true; }
891
892class VoidNullaryFunctor {
893 public:
894 void operator()() { g_done = true; }
895};
896
Abseil Teamaac18182018-11-15 15:43:19 -0500897short Short(short n) { return n; } // NOLINT
898char Char(char ch) { return ch; }
899
900const char* CharPtr(const char* s) { return s; }
901
902bool Unary(int x) { return x < 0; }
903
904const char* Binary(const char* input, short n) { return input + n; } // NOLINT
905
906void VoidBinary(int, char) { g_done = true; }
907
908int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT
909
910int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
911
shiqiane35fdd92008-12-10 05:08:54 +0000912class Foo {
913 public:
914 Foo() : value_(123) {}
915
916 int Nullary() const { return value_; }
zhanyong.wan29be9232013-03-01 06:53:35 +0000917
shiqiane35fdd92008-12-10 05:08:54 +0000918 private:
919 int value_;
920};
921
922// Tests InvokeWithoutArgs(function).
923TEST(InvokeWithoutArgsTest, Function) {
924 // As an action that takes one argument.
925 Action<int(int)> a = InvokeWithoutArgs(Nullary); // NOLINT
Abseil Team7d3b73c2018-10-09 14:50:26 -0400926 EXPECT_EQ(1, a.Perform(std::make_tuple(2)));
shiqiane35fdd92008-12-10 05:08:54 +0000927
928 // As an action that takes two arguments.
zhanyong.wan32de5f52009-12-23 00:13:23 +0000929 Action<int(int, double)> a2 = InvokeWithoutArgs(Nullary); // NOLINT
Abseil Team7d3b73c2018-10-09 14:50:26 -0400930 EXPECT_EQ(1, a2.Perform(std::make_tuple(2, 3.5)));
shiqiane35fdd92008-12-10 05:08:54 +0000931
932 // As an action that returns void.
933 Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary); // NOLINT
934 g_done = false;
Abseil Team7d3b73c2018-10-09 14:50:26 -0400935 a3.Perform(std::make_tuple(1));
shiqiane35fdd92008-12-10 05:08:54 +0000936 EXPECT_TRUE(g_done);
937}
938
939// Tests InvokeWithoutArgs(functor).
940TEST(InvokeWithoutArgsTest, Functor) {
941 // As an action that takes no argument.
942 Action<int()> a = InvokeWithoutArgs(NullaryFunctor()); // NOLINT
Abseil Team7d3b73c2018-10-09 14:50:26 -0400943 EXPECT_EQ(2, a.Perform(std::make_tuple()));
shiqiane35fdd92008-12-10 05:08:54 +0000944
945 // As an action that takes three arguments.
zhanyong.wan32de5f52009-12-23 00:13:23 +0000946 Action<int(int, double, char)> a2 = // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +0000947 InvokeWithoutArgs(NullaryFunctor());
Abseil Team7d3b73c2018-10-09 14:50:26 -0400948 EXPECT_EQ(2, a2.Perform(std::make_tuple(3, 3.5, 'a')));
shiqiane35fdd92008-12-10 05:08:54 +0000949
950 // As an action that returns void.
951 Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor());
952 g_done = false;
Abseil Team7d3b73c2018-10-09 14:50:26 -0400953 a3.Perform(std::make_tuple());
shiqiane35fdd92008-12-10 05:08:54 +0000954 EXPECT_TRUE(g_done);
955}
956
957// Tests InvokeWithoutArgs(obj_ptr, method).
958TEST(InvokeWithoutArgsTest, Method) {
959 Foo foo;
960 Action<int(bool, char)> a = // NOLINT
961 InvokeWithoutArgs(&foo, &Foo::Nullary);
Abseil Team7d3b73c2018-10-09 14:50:26 -0400962 EXPECT_EQ(123, a.Perform(std::make_tuple(true, 'a')));
shiqiane35fdd92008-12-10 05:08:54 +0000963}
964
965// Tests using IgnoreResult() on a polymorphic action.
966TEST(IgnoreResultTest, PolymorphicAction) {
967 Action<void(int)> a = IgnoreResult(Return(5)); // NOLINT
Abseil Team7d3b73c2018-10-09 14:50:26 -0400968 a.Perform(std::make_tuple(1));
shiqiane35fdd92008-12-10 05:08:54 +0000969}
970
971// Tests using IgnoreResult() on a monomorphic action.
972
973int ReturnOne() {
974 g_done = true;
975 return 1;
976}
977
978TEST(IgnoreResultTest, MonomorphicAction) {
979 g_done = false;
980 Action<void()> a = IgnoreResult(Invoke(ReturnOne));
Abseil Team7d3b73c2018-10-09 14:50:26 -0400981 a.Perform(std::make_tuple());
shiqiane35fdd92008-12-10 05:08:54 +0000982 EXPECT_TRUE(g_done);
983}
984
985// Tests using IgnoreResult() on an action that returns a class type.
986
kosakd478a1f2015-02-14 02:45:40 +0000987MyNonDefaultConstructible ReturnMyNonDefaultConstructible(double /* x */) {
shiqiane35fdd92008-12-10 05:08:54 +0000988 g_done = true;
kosakd478a1f2015-02-14 02:45:40 +0000989 return MyNonDefaultConstructible(42);
shiqiane35fdd92008-12-10 05:08:54 +0000990}
991
992TEST(IgnoreResultTest, ActionReturningClass) {
993 g_done = false;
kosakd478a1f2015-02-14 02:45:40 +0000994 Action<void(int)> a =
995 IgnoreResult(Invoke(ReturnMyNonDefaultConstructible)); // NOLINT
Abseil Team7d3b73c2018-10-09 14:50:26 -0400996 a.Perform(std::make_tuple(2));
shiqiane35fdd92008-12-10 05:08:54 +0000997 EXPECT_TRUE(g_done);
998}
999
1000TEST(AssignTest, Int) {
1001 int x = 0;
1002 Action<void(int)> a = Assign(&x, 5);
Abseil Team7d3b73c2018-10-09 14:50:26 -04001003 a.Perform(std::make_tuple(0));
shiqiane35fdd92008-12-10 05:08:54 +00001004 EXPECT_EQ(5, x);
1005}
1006
1007TEST(AssignTest, String) {
1008 ::std::string x;
1009 Action<void(void)> a = Assign(&x, "Hello, world");
Abseil Team7d3b73c2018-10-09 14:50:26 -04001010 a.Perform(std::make_tuple());
shiqiane35fdd92008-12-10 05:08:54 +00001011 EXPECT_EQ("Hello, world", x);
1012}
1013
1014TEST(AssignTest, CompatibleTypes) {
1015 double x = 0;
1016 Action<void(int)> a = Assign(&x, 5);
Abseil Team7d3b73c2018-10-09 14:50:26 -04001017 a.Perform(std::make_tuple(0));
shiqiane35fdd92008-12-10 05:08:54 +00001018 EXPECT_DOUBLE_EQ(5, x);
1019}
1020
Abseil Teamaac18182018-11-15 15:43:19 -05001021
1022// Tests using WithArgs and with an action that takes 1 argument.
1023TEST(WithArgsTest, OneArg) {
1024 Action<bool(double x, int n)> a = WithArgs<1>(Invoke(Unary)); // NOLINT
1025 EXPECT_TRUE(a.Perform(std::make_tuple(1.5, -1)));
1026 EXPECT_FALSE(a.Perform(std::make_tuple(1.5, 1)));
1027}
1028
1029// Tests using WithArgs with an action that takes 2 arguments.
1030TEST(WithArgsTest, TwoArgs) {
1031 Action<const char*(const char* s, double x, short n)> a = // NOLINT
1032 WithArgs<0, 2>(Invoke(Binary));
1033 const char s[] = "Hello";
1034 EXPECT_EQ(s + 2, a.Perform(std::make_tuple(CharPtr(s), 0.5, Short(2))));
1035}
1036
1037struct ConcatAll {
1038 std::string operator()() const { return {}; }
1039 template <typename... I>
1040 std::string operator()(const char* a, I... i) const {
1041 return a + ConcatAll()(i...);
1042 }
1043};
1044
1045// Tests using WithArgs with an action that takes 10 arguments.
1046TEST(WithArgsTest, TenArgs) {
1047 Action<std::string(const char*, const char*, const char*, const char*)> a =
1048 WithArgs<0, 1, 2, 3, 2, 1, 0, 1, 2, 3>(Invoke(ConcatAll{}));
1049 EXPECT_EQ("0123210123",
1050 a.Perform(std::make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
1051 CharPtr("3"))));
1052}
1053
1054// Tests using WithArgs with an action that is not Invoke().
1055class SubtractAction : public ActionInterface<int(int, int)> {
1056 public:
Abseil Team26743362018-12-03 11:30:02 -05001057 int Perform(const std::tuple<int, int>& args) override {
Abseil Teamaac18182018-11-15 15:43:19 -05001058 return std::get<0>(args) - std::get<1>(args);
1059 }
1060};
1061
1062TEST(WithArgsTest, NonInvokeAction) {
1063 Action<int(const std::string&, int, int)> a =
1064 WithArgs<2, 1>(MakeAction(new SubtractAction));
1065 std::tuple<std::string, int, int> dummy =
1066 std::make_tuple(std::string("hi"), 2, 10);
1067 EXPECT_EQ(8, a.Perform(dummy));
1068}
1069
1070// Tests using WithArgs to pass all original arguments in the original order.
1071TEST(WithArgsTest, Identity) {
1072 Action<int(int x, char y, short z)> a = // NOLINT
1073 WithArgs<0, 1, 2>(Invoke(Ternary));
1074 EXPECT_EQ(123, a.Perform(std::make_tuple(100, Char(20), Short(3))));
1075}
1076
1077// Tests using WithArgs with repeated arguments.
1078TEST(WithArgsTest, RepeatedArguments) {
1079 Action<int(bool, int m, int n)> a = // NOLINT
1080 WithArgs<1, 1, 1, 1>(Invoke(SumOf4));
1081 EXPECT_EQ(4, a.Perform(std::make_tuple(false, 1, 10)));
1082}
1083
1084// Tests using WithArgs with reversed argument order.
1085TEST(WithArgsTest, ReversedArgumentOrder) {
1086 Action<const char*(short n, const char* input)> a = // NOLINT
1087 WithArgs<1, 0>(Invoke(Binary));
1088 const char s[] = "Hello";
1089 EXPECT_EQ(s + 2, a.Perform(std::make_tuple(Short(2), CharPtr(s))));
1090}
1091
1092// Tests using WithArgs with compatible, but not identical, argument types.
1093TEST(WithArgsTest, ArgsOfCompatibleTypes) {
1094 Action<long(short x, char y, double z, char c)> a = // NOLINT
1095 WithArgs<0, 1, 3>(Invoke(Ternary));
1096 EXPECT_EQ(123,
1097 a.Perform(std::make_tuple(Short(100), Char(20), 5.6, Char(3))));
1098}
1099
1100// Tests using WithArgs with an action that returns void.
1101TEST(WithArgsTest, VoidAction) {
1102 Action<void(double x, char c, int n)> a = WithArgs<2, 1>(Invoke(VoidBinary));
1103 g_done = false;
1104 a.Perform(std::make_tuple(1.5, 'a', 3));
1105 EXPECT_TRUE(g_done);
1106}
1107
1108TEST(WithArgsTest, ReturnReference) {
misterga3013cc2018-11-20 10:42:17 -05001109 Action<int&(int&, void*)> aa = WithArgs<0>([](int& a) -> int& { return a; });
Abseil Teamaac18182018-11-15 15:43:19 -05001110 int i = 0;
misterga3013cc2018-11-20 10:42:17 -05001111 const int& res = aa.Perform(std::forward_as_tuple(i, nullptr));
Abseil Teamaac18182018-11-15 15:43:19 -05001112 EXPECT_EQ(&i, &res);
1113}
1114
1115TEST(WithArgsTest, InnerActionWithConversion) {
Abseil Teamaac18182018-11-15 15:43:19 -05001116 Action<Derived*()> inner = [] { return nullptr; };
1117 Action<Base*(double)> a = testing::WithoutArgs(inner);
1118 EXPECT_EQ(nullptr, a.Perform(std::make_tuple(1.1)));
1119}
1120
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001121#if !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001122
shiqiane35fdd92008-12-10 05:08:54 +00001123class SetErrnoAndReturnTest : public testing::Test {
1124 protected:
Abseil Team26743362018-12-03 11:30:02 -05001125 void SetUp() override { errno = 0; }
1126 void TearDown() override { errno = 0; }
shiqiane35fdd92008-12-10 05:08:54 +00001127};
1128
1129TEST_F(SetErrnoAndReturnTest, Int) {
1130 Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5);
Abseil Team7d3b73c2018-10-09 14:50:26 -04001131 EXPECT_EQ(-5, a.Perform(std::make_tuple()));
shiqiane35fdd92008-12-10 05:08:54 +00001132 EXPECT_EQ(ENOTTY, errno);
1133}
1134
1135TEST_F(SetErrnoAndReturnTest, Ptr) {
1136 int x;
1137 Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x);
Abseil Team7d3b73c2018-10-09 14:50:26 -04001138 EXPECT_EQ(&x, a.Perform(std::make_tuple()));
shiqiane35fdd92008-12-10 05:08:54 +00001139 EXPECT_EQ(ENOTTY, errno);
1140}
1141
1142TEST_F(SetErrnoAndReturnTest, CompatibleTypes) {
1143 Action<double()> a = SetErrnoAndReturn(EINVAL, 5);
Abseil Team7d3b73c2018-10-09 14:50:26 -04001144 EXPECT_DOUBLE_EQ(5.0, a.Perform(std::make_tuple()));
shiqiane35fdd92008-12-10 05:08:54 +00001145 EXPECT_EQ(EINVAL, errno);
1146}
1147
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001148#endif // !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001149
zhanyong.wana18423e2009-07-22 23:58:19 +00001150// Tests ByRef().
1151
Abseil Team097407f2019-01-12 12:26:37 -05001152// Tests that the result of ByRef() is copyable.
zhanyong.wana18423e2009-07-22 23:58:19 +00001153TEST(ByRefTest, IsCopyable) {
1154 const std::string s1 = "Hi";
1155 const std::string s2 = "Hello";
1156
Abseil Team097407f2019-01-12 12:26:37 -05001157 auto ref_wrapper = ByRef(s1);
zhanyong.wana18423e2009-07-22 23:58:19 +00001158 const std::string& r1 = ref_wrapper;
1159 EXPECT_EQ(&s1, &r1);
1160
1161 // Assigns a new value to ref_wrapper.
1162 ref_wrapper = ByRef(s2);
1163 const std::string& r2 = ref_wrapper;
1164 EXPECT_EQ(&s2, &r2);
1165
Abseil Team097407f2019-01-12 12:26:37 -05001166 auto ref_wrapper1 = ByRef(s1);
zhanyong.wana18423e2009-07-22 23:58:19 +00001167 // Copies ref_wrapper1 to ref_wrapper.
1168 ref_wrapper = ref_wrapper1;
1169 const std::string& r3 = ref_wrapper;
1170 EXPECT_EQ(&s1, &r3);
1171}
1172
1173// Tests using ByRef() on a const value.
1174TEST(ByRefTest, ConstValue) {
1175 const int n = 0;
1176 // int& ref = ByRef(n); // This shouldn't compile - we have a
1177 // negative compilation test to catch it.
1178 const int& const_ref = ByRef(n);
1179 EXPECT_EQ(&n, &const_ref);
1180}
1181
1182// Tests using ByRef() on a non-const value.
1183TEST(ByRefTest, NonConstValue) {
1184 int n = 0;
1185
1186 // ByRef(n) can be used as either an int&,
1187 int& ref = ByRef(n);
1188 EXPECT_EQ(&n, &ref);
1189
1190 // or a const int&.
1191 const int& const_ref = ByRef(n);
1192 EXPECT_EQ(&n, &const_ref);
1193}
1194
1195// Tests explicitly specifying the type when using ByRef().
1196TEST(ByRefTest, ExplicitType) {
1197 int n = 0;
1198 const int& r1 = ByRef<const int>(n);
1199 EXPECT_EQ(&n, &r1);
1200
1201 // ByRef<char>(n); // This shouldn't compile - we have a negative
1202 // compilation test to catch it.
1203
1204 Derived d;
1205 Derived& r2 = ByRef<Derived>(d);
1206 EXPECT_EQ(&d, &r2);
1207
1208 const Derived& r3 = ByRef<const Derived>(d);
1209 EXPECT_EQ(&d, &r3);
1210
1211 Base& r4 = ByRef<Base>(d);
1212 EXPECT_EQ(&d, &r4);
1213
1214 const Base& r5 = ByRef<const Base>(d);
1215 EXPECT_EQ(&d, &r5);
1216
1217 // The following shouldn't compile - we have a negative compilation
1218 // test for it.
1219 //
1220 // Base b;
1221 // ByRef<Derived>(b);
1222}
1223
1224// Tests that Google Mock prints expression ByRef(x) as a reference to x.
1225TEST(ByRefTest, PrintsCorrectly) {
1226 int n = 42;
1227 ::std::stringstream expected, actual;
1228 testing::internal::UniversalPrinter<const int&>::Print(n, &expected);
1229 testing::internal::UniversalPrint(ByRef(n), &actual);
1230 EXPECT_EQ(expected.str(), actual.str());
1231}
1232
kosakb5c81092014-01-29 06:41:44 +00001233
1234std::unique_ptr<int> UniquePtrSource() {
1235 return std::unique_ptr<int>(new int(19));
1236}
1237
1238std::vector<std::unique_ptr<int>> VectorUniquePtrSource() {
1239 std::vector<std::unique_ptr<int>> out;
1240 out.emplace_back(new int(7));
1241 return out;
1242}
1243
kosak3d1c78b2014-11-17 00:56:52 +00001244TEST(MockMethodTest, CanReturnMoveOnlyValue_Return) {
1245 MockClass mock;
1246 std::unique_ptr<int> i(new int(19));
1247 EXPECT_CALL(mock, MakeUnique()).WillOnce(Return(ByMove(std::move(i))));
1248 EXPECT_CALL(mock, MakeVectorUnique())
1249 .WillOnce(Return(ByMove(VectorUniquePtrSource())));
1250 Derived* d = new Derived;
1251 EXPECT_CALL(mock, MakeUniqueBase())
1252 .WillOnce(Return(ByMove(std::unique_ptr<Derived>(d))));
1253
1254 std::unique_ptr<int> result1 = mock.MakeUnique();
1255 EXPECT_EQ(19, *result1);
1256
1257 std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
kosak389bad62014-11-17 01:08:51 +00001258 EXPECT_EQ(1u, vresult.size());
kosak3d1c78b2014-11-17 00:56:52 +00001259 EXPECT_NE(nullptr, vresult[0]);
1260 EXPECT_EQ(7, *vresult[0]);
1261
1262 std::unique_ptr<Base> result2 = mock.MakeUniqueBase();
1263 EXPECT_EQ(d, result2.get());
1264}
1265
1266TEST(MockMethodTest, CanReturnMoveOnlyValue_DoAllReturn) {
1267 testing::MockFunction<void()> mock_function;
1268 MockClass mock;
1269 std::unique_ptr<int> i(new int(19));
1270 EXPECT_CALL(mock_function, Call());
1271 EXPECT_CALL(mock, MakeUnique()).WillOnce(DoAll(
1272 InvokeWithoutArgs(&mock_function, &testing::MockFunction<void()>::Call),
1273 Return(ByMove(std::move(i)))));
1274
1275 std::unique_ptr<int> result1 = mock.MakeUnique();
1276 EXPECT_EQ(19, *result1);
1277}
1278
1279TEST(MockMethodTest, CanReturnMoveOnlyValue_Invoke) {
kosakb5c81092014-01-29 06:41:44 +00001280 MockClass mock;
1281
1282 // Check default value
1283 DefaultValue<std::unique_ptr<int>>::SetFactory([] {
1284 return std::unique_ptr<int>(new int(42));
1285 });
1286 EXPECT_EQ(42, *mock.MakeUnique());
1287
kosak3d1c78b2014-11-17 00:56:52 +00001288 EXPECT_CALL(mock, MakeUnique()).WillRepeatedly(Invoke(UniquePtrSource));
kosakb5c81092014-01-29 06:41:44 +00001289 EXPECT_CALL(mock, MakeVectorUnique())
1290 .WillRepeatedly(Invoke(VectorUniquePtrSource));
1291 std::unique_ptr<int> result1 = mock.MakeUnique();
1292 EXPECT_EQ(19, *result1);
1293 std::unique_ptr<int> result2 = mock.MakeUnique();
1294 EXPECT_EQ(19, *result2);
1295 EXPECT_NE(result1, result2);
1296
1297 std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
kosak389bad62014-11-17 01:08:51 +00001298 EXPECT_EQ(1u, vresult.size());
kosakb5c81092014-01-29 06:41:44 +00001299 EXPECT_NE(nullptr, vresult[0]);
1300 EXPECT_EQ(7, *vresult[0]);
1301}
1302
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001303TEST(MockMethodTest, CanTakeMoveOnlyValue) {
1304 MockClass mock;
1305 auto make = [](int i) { return std::unique_ptr<int>(new int(i)); };
1306
1307 EXPECT_CALL(mock, TakeUnique(_)).WillRepeatedly([](std::unique_ptr<int> i) {
1308 return *i;
1309 });
1310 // DoAll() does not compile, since it would move from its arguments twice.
1311 // EXPECT_CALL(mock, TakeUnique(_, _))
1312 // .WillRepeatedly(DoAll(Invoke([](std::unique_ptr<int> j) {}),
1313 // Return(1)));
1314 EXPECT_CALL(mock, TakeUnique(testing::Pointee(7)))
1315 .WillOnce(Return(-7))
1316 .RetiresOnSaturation();
1317 EXPECT_CALL(mock, TakeUnique(testing::IsNull()))
1318 .WillOnce(Return(-1))
1319 .RetiresOnSaturation();
1320
1321 EXPECT_EQ(5, mock.TakeUnique(make(5)));
1322 EXPECT_EQ(-7, mock.TakeUnique(make(7)));
1323 EXPECT_EQ(7, mock.TakeUnique(make(7)));
1324 EXPECT_EQ(7, mock.TakeUnique(make(7)));
1325 EXPECT_EQ(-1, mock.TakeUnique({}));
1326
1327 // Some arguments are moved, some passed by reference.
1328 auto lvalue = make(6);
1329 EXPECT_CALL(mock, TakeUnique(_, _))
1330 .WillOnce([](const std::unique_ptr<int>& i, std::unique_ptr<int> j) {
1331 return *i * *j;
1332 });
1333 EXPECT_EQ(42, mock.TakeUnique(lvalue, make(7)));
1334
1335 // The unique_ptr can be saved by the action.
1336 std::unique_ptr<int> saved;
1337 EXPECT_CALL(mock, TakeUnique(_)).WillOnce([&saved](std::unique_ptr<int> i) {
1338 saved = std::move(i);
1339 return 0;
1340 });
1341 EXPECT_EQ(0, mock.TakeUnique(make(42)));
1342 EXPECT_EQ(42, *saved);
1343}
1344
kosakb5c81092014-01-29 06:41:44 +00001345
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001346// Tests for std::function based action.
1347
1348int Add(int val, int& ref, int* ptr) { // NOLINT
1349 int result = val + ref + *ptr;
1350 ref = 42;
1351 *ptr = 43;
1352 return result;
1353}
1354
1355int Deref(std::unique_ptr<int> ptr) { return *ptr; }
1356
1357struct Double {
1358 template <typename T>
1359 T operator()(T t) { return 2 * t; }
1360};
1361
1362std::unique_ptr<int> UniqueInt(int i) {
1363 return std::unique_ptr<int>(new int(i));
1364}
1365
1366TEST(FunctorActionTest, ActionFromFunction) {
1367 Action<int(int, int&, int*)> a = &Add;
1368 int x = 1, y = 2, z = 3;
1369 EXPECT_EQ(6, a.Perform(std::forward_as_tuple(x, y, &z)));
1370 EXPECT_EQ(42, y);
1371 EXPECT_EQ(43, z);
1372
1373 Action<int(std::unique_ptr<int>)> a1 = &Deref;
1374 EXPECT_EQ(7, a1.Perform(std::make_tuple(UniqueInt(7))));
1375}
1376
1377TEST(FunctorActionTest, ActionFromLambda) {
1378 Action<int(bool, int)> a1 = [](bool b, int i) { return b ? i : 0; };
Abseil Team7d3b73c2018-10-09 14:50:26 -04001379 EXPECT_EQ(5, a1.Perform(std::make_tuple(true, 5)));
1380 EXPECT_EQ(0, a1.Perform(std::make_tuple(false, 5)));
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001381
1382 std::unique_ptr<int> saved;
1383 Action<void(std::unique_ptr<int>)> a2 = [&saved](std::unique_ptr<int> p) {
1384 saved = std::move(p);
1385 };
Abseil Team7d3b73c2018-10-09 14:50:26 -04001386 a2.Perform(std::make_tuple(UniqueInt(5)));
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001387 EXPECT_EQ(5, *saved);
1388}
1389
1390TEST(FunctorActionTest, PolymorphicFunctor) {
1391 Action<int(int)> ai = Double();
Abseil Team7d3b73c2018-10-09 14:50:26 -04001392 EXPECT_EQ(2, ai.Perform(std::make_tuple(1)));
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001393 Action<double(double)> ad = Double(); // Double? Double double!
Abseil Team7d3b73c2018-10-09 14:50:26 -04001394 EXPECT_EQ(3.0, ad.Perform(std::make_tuple(1.5)));
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001395}
1396
1397TEST(FunctorActionTest, TypeConversion) {
1398 // Numeric promotions are allowed.
1399 const Action<bool(int)> a1 = [](int i) { return i > 1; };
1400 const Action<int(bool)> a2 = Action<int(bool)>(a1);
Abseil Team7d3b73c2018-10-09 14:50:26 -04001401 EXPECT_EQ(1, a1.Perform(std::make_tuple(42)));
1402 EXPECT_EQ(0, a2.Perform(std::make_tuple(42)));
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001403
1404 // Implicit constructors are allowed.
1405 const Action<bool(std::string)> s1 = [](std::string s) { return !s.empty(); };
1406 const Action<int(const char*)> s2 = Action<int(const char*)>(s1);
Abseil Team7d3b73c2018-10-09 14:50:26 -04001407 EXPECT_EQ(0, s2.Perform(std::make_tuple("")));
1408 EXPECT_EQ(1, s2.Perform(std::make_tuple("hello")));
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001409
1410 // Also between the lambda and the action itself.
1411 const Action<bool(std::string)> x = [](Unused) { return 42; };
Abseil Team7d3b73c2018-10-09 14:50:26 -04001412 EXPECT_TRUE(x.Perform(std::make_tuple("hello")));
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001413}
1414
1415TEST(FunctorActionTest, UnusedArguments) {
1416 // Verify that users can ignore uninteresting arguments.
Gennadiy Civil1c6e68c2018-04-16 10:34:07 -04001417 Action<int(int, double y, double z)> a =
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001418 [](int i, Unused, Unused) { return 2 * i; };
Abseil Team7d3b73c2018-10-09 14:50:26 -04001419 std::tuple<int, double, double> dummy = std::make_tuple(3, 7.3, 9.44);
Gennadiy Civilb74a1af2018-04-13 11:49:37 -04001420 EXPECT_EQ(6, a.Perform(dummy));
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001421}
1422
1423// Test that basic built-in actions work with move-only arguments.
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001424TEST(MoveOnlyArgumentsTest, ReturningActions) {
1425 Action<int(std::unique_ptr<int>)> a = Return(1);
Abseil Team7d3b73c2018-10-09 14:50:26 -04001426 EXPECT_EQ(1, a.Perform(std::make_tuple(nullptr)));
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001427
1428 a = testing::WithoutArgs([]() { return 7; });
Abseil Team7d3b73c2018-10-09 14:50:26 -04001429 EXPECT_EQ(7, a.Perform(std::make_tuple(nullptr)));
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001430
1431 Action<void(std::unique_ptr<int>, int*)> a2 = testing::SetArgPointee<1>(3);
1432 int x = 0;
Abseil Team7d3b73c2018-10-09 14:50:26 -04001433 a2.Perform(std::make_tuple(nullptr, &x));
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001434 EXPECT_EQ(x, 3);
1435}
1436
Gennadiy Civil0bfa8232018-04-13 11:02:25 -04001437
shiqiane35fdd92008-12-10 05:08:54 +00001438} // Unnamed namespace
Gennadiy Civile1071eb2018-04-10 15:57:16 -04001439
1440#ifdef _MSC_VER
1441#if _MSC_VER == 1900
1442# pragma warning(pop)
1443#endif
1444#endif
1445