blob: a05519411c3af37b0e4e62cc04fbfa059e00fca8 [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// Copyright 2007, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Author: wan@google.com (Zhanyong Wan)
31
32// Google Mock - a framework for writing C++ mock classes.
33//
34// This file tests the built-in actions.
35
zhanyong.wan53e08c42010-09-14 05:38:21 +000036#include "gmock/gmock-actions.h"
shiqiane35fdd92008-12-10 05:08:54 +000037#include <algorithm>
38#include <iterator>
kosakb5c81092014-01-29 06:41:44 +000039#include <memory>
shiqiane35fdd92008-12-10 05:08:54 +000040#include <string>
zhanyong.wan53e08c42010-09-14 05:38:21 +000041#include "gmock/gmock.h"
42#include "gmock/internal/gmock-port.h"
43#include "gtest/gtest.h"
44#include "gtest/gtest-spi.h"
shiqiane35fdd92008-12-10 05:08:54 +000045
46namespace {
47
kosakbd018832014-04-02 20:30:00 +000048using testing::get;
49using testing::make_tuple;
50using testing::tuple;
51using testing::tuple_element;
shiqiane35fdd92008-12-10 05:08:54 +000052using testing::internal::BuiltInDefaultValue;
53using testing::internal::Int64;
54using testing::internal::UInt64;
55// This list should be kept sorted.
56using testing::_;
57using testing::Action;
58using testing::ActionInterface;
59using testing::Assign;
kosak3d1c78b2014-11-17 00:56:52 +000060using testing::ByMove;
zhanyong.wana18423e2009-07-22 23:58:19 +000061using testing::ByRef;
shiqiane35fdd92008-12-10 05:08:54 +000062using testing::DefaultValue;
63using testing::DoDefault;
64using testing::IgnoreResult;
65using testing::Invoke;
66using testing::InvokeWithoutArgs;
67using testing::MakePolymorphicAction;
68using testing::Ne;
69using testing::PolymorphicAction;
70using testing::Return;
71using testing::ReturnNull;
72using testing::ReturnRef;
zhanyong.wane3bd0982010-07-03 00:16:42 +000073using testing::ReturnRefOfCopy;
zhanyong.wan59214832010-10-05 05:58:51 +000074using testing::SetArgPointee;
shiqiane35fdd92008-12-10 05:08:54 +000075using testing::SetArgumentPointee;
zhanyong.wan5b5d62f2009-03-11 23:37:56 +000076
zhanyong.wanf7af24c2009-09-24 21:17:24 +000077#if !GTEST_OS_WINDOWS_MOBILE
shiqiane35fdd92008-12-10 05:08:54 +000078using testing::SetErrnoAndReturn;
zhanyong.wanf7af24c2009-09-24 21:17:24 +000079#endif
shiqiane35fdd92008-12-10 05:08:54 +000080
zhanyong.wan02f71062010-05-10 17:14:29 +000081#if GTEST_HAS_PROTOBUF_
shiqiane35fdd92008-12-10 05:08:54 +000082using testing::internal::TestMessage;
zhanyong.wan02f71062010-05-10 17:14:29 +000083#endif // GTEST_HAS_PROTOBUF_
shiqiane35fdd92008-12-10 05:08:54 +000084
85// Tests that BuiltInDefaultValue<T*>::Get() returns NULL.
86TEST(BuiltInDefaultValueTest, IsNullForPointerTypes) {
87 EXPECT_TRUE(BuiltInDefaultValue<int*>::Get() == NULL);
88 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Get() == NULL);
89 EXPECT_TRUE(BuiltInDefaultValue<void*>::Get() == NULL);
90}
91
zhanyong.wan5b95fa72009-01-27 22:28:45 +000092// Tests that BuiltInDefaultValue<T*>::Exists() return true.
93TEST(BuiltInDefaultValueTest, ExistsForPointerTypes) {
94 EXPECT_TRUE(BuiltInDefaultValue<int*>::Exists());
95 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Exists());
96 EXPECT_TRUE(BuiltInDefaultValue<void*>::Exists());
97}
98
shiqiane35fdd92008-12-10 05:08:54 +000099// Tests that BuiltInDefaultValue<T>::Get() returns 0 when T is a
100// built-in numeric type.
101TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) {
zhanyong.wan32de5f52009-12-23 00:13:23 +0000102 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned char>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000103 EXPECT_EQ(0, BuiltInDefaultValue<signed char>::Get());
104 EXPECT_EQ(0, BuiltInDefaultValue<char>::Get());
zhanyong.wan95b12332009-09-25 18:55:50 +0000105#if GMOCK_HAS_SIGNED_WCHAR_T_
zhanyong.wan32de5f52009-12-23 00:13:23 +0000106 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned wchar_t>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000107 EXPECT_EQ(0, BuiltInDefaultValue<signed wchar_t>::Get());
zhanyong.wan95b12332009-09-25 18:55:50 +0000108#endif
109#if GMOCK_WCHAR_T_IS_NATIVE_
shiqiane35fdd92008-12-10 05:08:54 +0000110 EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get());
zhanyong.wan95b12332009-09-25 18:55:50 +0000111#endif
zhanyong.wan32de5f52009-12-23 00:13:23 +0000112 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned short>::Get()); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +0000113 EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get()); // NOLINT
114 EXPECT_EQ(0, BuiltInDefaultValue<short>::Get()); // NOLINT
zhanyong.wan32de5f52009-12-23 00:13:23 +0000115 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned int>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000116 EXPECT_EQ(0, BuiltInDefaultValue<signed int>::Get());
117 EXPECT_EQ(0, BuiltInDefaultValue<int>::Get());
zhanyong.wan32de5f52009-12-23 00:13:23 +0000118 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned long>::Get()); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +0000119 EXPECT_EQ(0, BuiltInDefaultValue<signed long>::Get()); // NOLINT
120 EXPECT_EQ(0, BuiltInDefaultValue<long>::Get()); // NOLINT
zhanyong.wan32de5f52009-12-23 00:13:23 +0000121 EXPECT_EQ(0U, BuiltInDefaultValue<UInt64>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000122 EXPECT_EQ(0, BuiltInDefaultValue<Int64>::Get());
123 EXPECT_EQ(0, BuiltInDefaultValue<float>::Get());
124 EXPECT_EQ(0, BuiltInDefaultValue<double>::Get());
125}
126
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000127// Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
128// built-in numeric type.
129TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) {
130 EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists());
131 EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists());
132 EXPECT_TRUE(BuiltInDefaultValue<char>::Exists());
zhanyong.wan95b12332009-09-25 18:55:50 +0000133#if GMOCK_HAS_SIGNED_WCHAR_T_
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000134 EXPECT_TRUE(BuiltInDefaultValue<unsigned wchar_t>::Exists());
135 EXPECT_TRUE(BuiltInDefaultValue<signed wchar_t>::Exists());
zhanyong.wan95b12332009-09-25 18:55:50 +0000136#endif
137#if GMOCK_WCHAR_T_IS_NATIVE_
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000138 EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists());
zhanyong.wan95b12332009-09-25 18:55:50 +0000139#endif
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000140 EXPECT_TRUE(BuiltInDefaultValue<unsigned short>::Exists()); // NOLINT
141 EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists()); // NOLINT
142 EXPECT_TRUE(BuiltInDefaultValue<short>::Exists()); // NOLINT
143 EXPECT_TRUE(BuiltInDefaultValue<unsigned int>::Exists());
144 EXPECT_TRUE(BuiltInDefaultValue<signed int>::Exists());
145 EXPECT_TRUE(BuiltInDefaultValue<int>::Exists());
146 EXPECT_TRUE(BuiltInDefaultValue<unsigned long>::Exists()); // NOLINT
147 EXPECT_TRUE(BuiltInDefaultValue<signed long>::Exists()); // NOLINT
148 EXPECT_TRUE(BuiltInDefaultValue<long>::Exists()); // NOLINT
149 EXPECT_TRUE(BuiltInDefaultValue<UInt64>::Exists());
150 EXPECT_TRUE(BuiltInDefaultValue<Int64>::Exists());
151 EXPECT_TRUE(BuiltInDefaultValue<float>::Exists());
152 EXPECT_TRUE(BuiltInDefaultValue<double>::Exists());
153}
154
shiqiane35fdd92008-12-10 05:08:54 +0000155// Tests that BuiltInDefaultValue<bool>::Get() returns false.
156TEST(BuiltInDefaultValueTest, IsFalseForBool) {
157 EXPECT_FALSE(BuiltInDefaultValue<bool>::Get());
158}
159
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000160// Tests that BuiltInDefaultValue<bool>::Exists() returns true.
161TEST(BuiltInDefaultValueTest, BoolExists) {
162 EXPECT_TRUE(BuiltInDefaultValue<bool>::Exists());
163}
164
shiqiane35fdd92008-12-10 05:08:54 +0000165// Tests that BuiltInDefaultValue<T>::Get() returns "" when T is a
166// string type.
167TEST(BuiltInDefaultValueTest, IsEmptyStringForString) {
168#if GTEST_HAS_GLOBAL_STRING
169 EXPECT_EQ("", BuiltInDefaultValue< ::string>::Get());
170#endif // GTEST_HAS_GLOBAL_STRING
171
shiqiane35fdd92008-12-10 05:08:54 +0000172 EXPECT_EQ("", BuiltInDefaultValue< ::std::string>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000173}
174
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000175// Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
176// string type.
177TEST(BuiltInDefaultValueTest, ExistsForString) {
178#if GTEST_HAS_GLOBAL_STRING
179 EXPECT_TRUE(BuiltInDefaultValue< ::string>::Exists());
180#endif // GTEST_HAS_GLOBAL_STRING
181
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000182 EXPECT_TRUE(BuiltInDefaultValue< ::std::string>::Exists());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000183}
184
shiqiane35fdd92008-12-10 05:08:54 +0000185// Tests that BuiltInDefaultValue<const T>::Get() returns the same
186// value as BuiltInDefaultValue<T>::Get() does.
187TEST(BuiltInDefaultValueTest, WorksForConstTypes) {
188 EXPECT_EQ("", BuiltInDefaultValue<const std::string>::Get());
189 EXPECT_EQ(0, BuiltInDefaultValue<const int>::Get());
190 EXPECT_TRUE(BuiltInDefaultValue<char* const>::Get() == NULL);
191 EXPECT_FALSE(BuiltInDefaultValue<const bool>::Get());
192}
193
194// Tests that BuiltInDefaultValue<T>::Get() aborts the program with
195// the correct error message when T is a user-defined type.
196struct UserType {
197 UserType() : value(0) {}
198
199 int value;
200};
201
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000202TEST(BuiltInDefaultValueTest, UserTypeHasNoDefault) {
203 EXPECT_FALSE(BuiltInDefaultValue<UserType>::Exists());
204}
205
shiqiane35fdd92008-12-10 05:08:54 +0000206// Tests that BuiltInDefaultValue<T&>::Get() aborts the program.
207TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000208 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000209 BuiltInDefaultValue<int&>::Get();
210 }, "");
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000211 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000212 BuiltInDefaultValue<const char&>::Get();
213 }, "");
214}
215
216TEST(BuiltInDefaultValueDeathTest, IsUndefinedForUserTypes) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000217 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000218 BuiltInDefaultValue<UserType>::Get();
219 }, "");
220}
221
shiqiane35fdd92008-12-10 05:08:54 +0000222// Tests that DefaultValue<T>::IsSet() is false initially.
223TEST(DefaultValueTest, IsInitiallyUnset) {
224 EXPECT_FALSE(DefaultValue<int>::IsSet());
225 EXPECT_FALSE(DefaultValue<const UserType>::IsSet());
226}
227
228// Tests that DefaultValue<T> can be set and then unset.
229TEST(DefaultValueTest, CanBeSetAndUnset) {
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000230 EXPECT_TRUE(DefaultValue<int>::Exists());
231 EXPECT_FALSE(DefaultValue<const UserType>::Exists());
232
shiqiane35fdd92008-12-10 05:08:54 +0000233 DefaultValue<int>::Set(1);
234 DefaultValue<const UserType>::Set(UserType());
235
236 EXPECT_EQ(1, DefaultValue<int>::Get());
237 EXPECT_EQ(0, DefaultValue<const UserType>::Get().value);
238
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000239 EXPECT_TRUE(DefaultValue<int>::Exists());
240 EXPECT_TRUE(DefaultValue<const UserType>::Exists());
241
shiqiane35fdd92008-12-10 05:08:54 +0000242 DefaultValue<int>::Clear();
243 DefaultValue<const UserType>::Clear();
244
245 EXPECT_FALSE(DefaultValue<int>::IsSet());
246 EXPECT_FALSE(DefaultValue<const UserType>::IsSet());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000247
248 EXPECT_TRUE(DefaultValue<int>::Exists());
249 EXPECT_FALSE(DefaultValue<const UserType>::Exists());
shiqiane35fdd92008-12-10 05:08:54 +0000250}
251
252// Tests that DefaultValue<T>::Get() returns the
253// BuiltInDefaultValue<T>::Get() when DefaultValue<T>::IsSet() is
254// false.
255TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
256 EXPECT_FALSE(DefaultValue<int>::IsSet());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000257 EXPECT_TRUE(DefaultValue<int>::Exists());
shiqiane35fdd92008-12-10 05:08:54 +0000258 EXPECT_FALSE(DefaultValue<UserType>::IsSet());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000259 EXPECT_FALSE(DefaultValue<UserType>::Exists());
shiqiane35fdd92008-12-10 05:08:54 +0000260
261 EXPECT_EQ(0, DefaultValue<int>::Get());
262
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000263 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000264 DefaultValue<UserType>::Get();
265 }, "");
shiqiane35fdd92008-12-10 05:08:54 +0000266}
267
kosak5b9cbbb2014-11-17 00:28:55 +0000268#if GTEST_HAS_STD_UNIQUE_PTR_
kosakb5c81092014-01-29 06:41:44 +0000269TEST(DefaultValueDeathTest, GetWorksForMoveOnlyIfSet) {
270 EXPECT_FALSE(DefaultValue<std::unique_ptr<int>>::Exists());
271 EXPECT_DEATH_IF_SUPPORTED({
272 DefaultValue<std::unique_ptr<int>>::Get();
273 }, "");
274 DefaultValue<std::unique_ptr<int>>::SetFactory([] {
275 return std::unique_ptr<int>(new int(42));
276 });
277 EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Exists());
278 std::unique_ptr<int> i = DefaultValue<std::unique_ptr<int>>::Get();
279 EXPECT_EQ(42, *i);
280}
kosak5b9cbbb2014-11-17 00:28:55 +0000281#endif // GTEST_HAS_STD_UNIQUE_PTR_
kosakb5c81092014-01-29 06:41:44 +0000282
shiqiane35fdd92008-12-10 05:08:54 +0000283// Tests that DefaultValue<void>::Get() returns void.
284TEST(DefaultValueTest, GetWorksForVoid) {
285 return DefaultValue<void>::Get();
286}
287
288// Tests using DefaultValue with a reference type.
289
290// Tests that DefaultValue<T&>::IsSet() is false initially.
291TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) {
292 EXPECT_FALSE(DefaultValue<int&>::IsSet());
293 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
294}
295
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000296// Tests that DefaultValue<T&>::Exists is false initiallly.
297TEST(DefaultValueOfReferenceTest, IsInitiallyNotExisting) {
298 EXPECT_FALSE(DefaultValue<int&>::Exists());
299 EXPECT_FALSE(DefaultValue<UserType&>::Exists());
300}
301
shiqiane35fdd92008-12-10 05:08:54 +0000302// Tests that DefaultValue<T&> can be set and then unset.
303TEST(DefaultValueOfReferenceTest, CanBeSetAndUnset) {
304 int n = 1;
305 DefaultValue<const int&>::Set(n);
306 UserType u;
307 DefaultValue<UserType&>::Set(u);
308
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000309 EXPECT_TRUE(DefaultValue<const int&>::Exists());
310 EXPECT_TRUE(DefaultValue<UserType&>::Exists());
311
shiqiane35fdd92008-12-10 05:08:54 +0000312 EXPECT_EQ(&n, &(DefaultValue<const int&>::Get()));
313 EXPECT_EQ(&u, &(DefaultValue<UserType&>::Get()));
314
315 DefaultValue<const int&>::Clear();
316 DefaultValue<UserType&>::Clear();
317
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000318 EXPECT_FALSE(DefaultValue<const int&>::Exists());
319 EXPECT_FALSE(DefaultValue<UserType&>::Exists());
320
shiqiane35fdd92008-12-10 05:08:54 +0000321 EXPECT_FALSE(DefaultValue<const int&>::IsSet());
322 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
323}
324
325// Tests that DefaultValue<T&>::Get() returns the
326// BuiltInDefaultValue<T&>::Get() when DefaultValue<T&>::IsSet() is
327// false.
328TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
329 EXPECT_FALSE(DefaultValue<int&>::IsSet());
330 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
331
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000332 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000333 DefaultValue<int&>::Get();
334 }, "");
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000335 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000336 DefaultValue<UserType>::Get();
337 }, "");
shiqiane35fdd92008-12-10 05:08:54 +0000338}
339
340// Tests that ActionInterface can be implemented by defining the
341// Perform method.
342
zhanyong.wana1a98f82013-03-01 21:28:40 +0000343typedef int MyGlobalFunction(bool, int);
shiqiane35fdd92008-12-10 05:08:54 +0000344
zhanyong.wana1a98f82013-03-01 21:28:40 +0000345class MyActionImpl : public ActionInterface<MyGlobalFunction> {
shiqiane35fdd92008-12-10 05:08:54 +0000346 public:
347 virtual int Perform(const tuple<bool, int>& args) {
348 return get<0>(args) ? get<1>(args) : 0;
349 }
350};
351
352TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) {
353 MyActionImpl my_action_impl;
zhanyong.waned6c9272011-02-23 19:39:27 +0000354 (void)my_action_impl;
shiqiane35fdd92008-12-10 05:08:54 +0000355}
356
357TEST(ActionInterfaceTest, MakeAction) {
zhanyong.wana1a98f82013-03-01 21:28:40 +0000358 Action<MyGlobalFunction> action = MakeAction(new MyActionImpl);
shiqiane35fdd92008-12-10 05:08:54 +0000359
360 // When exercising the Perform() method of Action<F>, we must pass
361 // it a tuple whose size and type are compatible with F's argument
362 // types. For example, if F is int(), then Perform() takes a
363 // 0-tuple; if F is void(bool, int), then Perform() takes a
364 // tuple<bool, int>, and so on.
365 EXPECT_EQ(5, action.Perform(make_tuple(true, 5)));
366}
367
368// Tests that Action<F> can be contructed from a pointer to
369// ActionInterface<F>.
370TEST(ActionTest, CanBeConstructedFromActionInterface) {
zhanyong.wana1a98f82013-03-01 21:28:40 +0000371 Action<MyGlobalFunction> action(new MyActionImpl);
shiqiane35fdd92008-12-10 05:08:54 +0000372}
373
374// Tests that Action<F> delegates actual work to ActionInterface<F>.
375TEST(ActionTest, DelegatesWorkToActionInterface) {
zhanyong.wana1a98f82013-03-01 21:28:40 +0000376 const Action<MyGlobalFunction> action(new MyActionImpl);
shiqiane35fdd92008-12-10 05:08:54 +0000377
378 EXPECT_EQ(5, action.Perform(make_tuple(true, 5)));
379 EXPECT_EQ(0, action.Perform(make_tuple(false, 1)));
380}
381
382// Tests that Action<F> can be copied.
383TEST(ActionTest, IsCopyable) {
zhanyong.wana1a98f82013-03-01 21:28:40 +0000384 Action<MyGlobalFunction> a1(new MyActionImpl);
385 Action<MyGlobalFunction> a2(a1); // Tests the copy constructor.
shiqiane35fdd92008-12-10 05:08:54 +0000386
387 // a1 should continue to work after being copied from.
388 EXPECT_EQ(5, a1.Perform(make_tuple(true, 5)));
389 EXPECT_EQ(0, a1.Perform(make_tuple(false, 1)));
390
391 // a2 should work like the action it was copied from.
392 EXPECT_EQ(5, a2.Perform(make_tuple(true, 5)));
393 EXPECT_EQ(0, a2.Perform(make_tuple(false, 1)));
394
395 a2 = a1; // Tests the assignment operator.
396
397 // a1 should continue to work after being copied from.
398 EXPECT_EQ(5, a1.Perform(make_tuple(true, 5)));
399 EXPECT_EQ(0, a1.Perform(make_tuple(false, 1)));
400
401 // a2 should work like the action it was copied from.
402 EXPECT_EQ(5, a2.Perform(make_tuple(true, 5)));
403 EXPECT_EQ(0, a2.Perform(make_tuple(false, 1)));
404}
405
406// Tests that an Action<From> object can be converted to a
407// compatible Action<To> object.
408
409class IsNotZero : public ActionInterface<bool(int)> { // NOLINT
410 public:
411 virtual bool Perform(const tuple<int>& arg) {
412 return get<0>(arg) != 0;
413 }
414};
415
zhanyong.wan95b12332009-09-25 18:55:50 +0000416#if !GTEST_OS_SYMBIAN
417// Compiling this test on Nokia's Symbian compiler fails with:
418// 'Result' is not a member of class 'testing::internal::Function<int>'
419// (point of instantiation: '@unnamed@gmock_actions_test_cc@::
420// ActionTest_CanBeConvertedToOtherActionType_Test::TestBody()')
421// with no obvious fix.
shiqiane35fdd92008-12-10 05:08:54 +0000422TEST(ActionTest, CanBeConvertedToOtherActionType) {
423 const Action<bool(int)> a1(new IsNotZero); // NOLINT
424 const Action<int(char)> a2 = Action<int(char)>(a1); // NOLINT
425 EXPECT_EQ(1, a2.Perform(make_tuple('a')));
426 EXPECT_EQ(0, a2.Perform(make_tuple('\0')));
427}
zhanyong.wan95b12332009-09-25 18:55:50 +0000428#endif // !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +0000429
430// The following two classes are for testing MakePolymorphicAction().
431
432// Implements a polymorphic action that returns the second of the
433// arguments it receives.
434class ReturnSecondArgumentAction {
435 public:
436 // We want to verify that MakePolymorphicAction() can work with a
437 // polymorphic action whose Perform() method template is either
438 // const or not. This lets us verify the non-const case.
439 template <typename Result, typename ArgumentTuple>
440 Result Perform(const ArgumentTuple& args) { return get<1>(args); }
441};
442
443// Implements a polymorphic action that can be used in a nullary
444// function to return 0.
445class ReturnZeroFromNullaryFunctionAction {
446 public:
447 // For testing that MakePolymorphicAction() works when the
448 // implementation class' Perform() method template takes only one
449 // template parameter.
450 //
451 // We want to verify that MakePolymorphicAction() can work with a
452 // polymorphic action whose Perform() method template is either
453 // const or not. This lets us verify the const case.
454 template <typename Result>
455 Result Perform(const tuple<>&) const { return 0; }
456};
457
458// These functions verify that MakePolymorphicAction() returns a
459// PolymorphicAction<T> where T is the argument's type.
460
461PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() {
462 return MakePolymorphicAction(ReturnSecondArgumentAction());
463}
464
465PolymorphicAction<ReturnZeroFromNullaryFunctionAction>
466ReturnZeroFromNullaryFunction() {
467 return MakePolymorphicAction(ReturnZeroFromNullaryFunctionAction());
468}
469
470// Tests that MakePolymorphicAction() turns a polymorphic action
471// implementation class into a polymorphic action.
472TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) {
473 Action<int(bool, int, double)> a1 = ReturnSecondArgument(); // NOLINT
474 EXPECT_EQ(5, a1.Perform(make_tuple(false, 5, 2.0)));
475}
476
477// Tests that MakePolymorphicAction() works when the implementation
478// class' Perform() method template has only one template parameter.
479TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) {
480 Action<int()> a1 = ReturnZeroFromNullaryFunction();
481 EXPECT_EQ(0, a1.Perform(make_tuple()));
482
483 Action<void*()> a2 = ReturnZeroFromNullaryFunction();
484 EXPECT_TRUE(a2.Perform(make_tuple()) == NULL);
485}
486
487// Tests that Return() works as an action for void-returning
488// functions.
489TEST(ReturnTest, WorksForVoid) {
490 const Action<void(int)> ret = Return(); // NOLINT
491 return ret.Perform(make_tuple(1));
492}
493
494// Tests that Return(v) returns v.
495TEST(ReturnTest, ReturnsGivenValue) {
496 Action<int()> ret = Return(1); // NOLINT
497 EXPECT_EQ(1, ret.Perform(make_tuple()));
498
499 ret = Return(-5);
500 EXPECT_EQ(-5, ret.Perform(make_tuple()));
501}
502
503// Tests that Return("string literal") works.
504TEST(ReturnTest, AcceptsStringLiteral) {
505 Action<const char*()> a1 = Return("Hello");
506 EXPECT_STREQ("Hello", a1.Perform(make_tuple()));
507
508 Action<std::string()> a2 = Return("world");
509 EXPECT_EQ("world", a2.Perform(make_tuple()));
510}
511
kosak7123d832014-11-17 02:04:46 +0000512// Test struct which wraps a vector of integers. Used in
513// 'SupportsWrapperReturnType' test.
514struct IntegerVectorWrapper {
515 std::vector<int> * v;
516 IntegerVectorWrapper(std::vector<int>& _v) : v(&_v) {} // NOLINT
517};
518
519// Tests that Return() works when return type is a wrapper type.
520TEST(ReturnTest, SupportsWrapperReturnType) {
521 // Initialize vector of integers.
522 std::vector<int> v;
523 for (int i = 0; i < 5; ++i) v.push_back(i);
524
525 // Return() called with 'v' as argument. The Action will return the same data
526 // as 'v' (copy) but it will be wrapped in an IntegerVectorWrapper.
527 Action<IntegerVectorWrapper()> a = Return(v);
528 const std::vector<int>& result = *(a.Perform(make_tuple()).v);
529 EXPECT_THAT(result, ::testing::ElementsAre(0, 1, 2, 3, 4));
530}
531
shiqiane35fdd92008-12-10 05:08:54 +0000532// Tests that Return(v) is covaraint.
533
534struct Base {
535 bool operator==(const Base&) { return true; }
536};
537
538struct Derived : public Base {
539 bool operator==(const Derived&) { return true; }
540};
541
542TEST(ReturnTest, IsCovariant) {
543 Base base;
544 Derived derived;
545 Action<Base*()> ret = Return(&base);
546 EXPECT_EQ(&base, ret.Perform(make_tuple()));
547
548 ret = Return(&derived);
549 EXPECT_EQ(&derived, ret.Perform(make_tuple()));
550}
551
vladloseva070cbd2009-11-18 00:09:28 +0000552// Tests that the type of the value passed into Return is converted into T
553// when the action is cast to Action<T(...)> rather than when the action is
554// performed. See comments on testing::internal::ReturnAction in
555// gmock-actions.h for more information.
556class FromType {
557 public:
jgm79a367e2012-04-10 16:02:11 +0000558 explicit FromType(bool* is_converted) : converted_(is_converted) {}
vladloseva070cbd2009-11-18 00:09:28 +0000559 bool* converted() const { return converted_; }
560
561 private:
562 bool* const converted_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000563
564 GTEST_DISALLOW_ASSIGN_(FromType);
vladloseva070cbd2009-11-18 00:09:28 +0000565};
566
567class ToType {
568 public:
jgm79a367e2012-04-10 16:02:11 +0000569 // Must allow implicit conversion due to use in ImplicitCast_<T>.
570 ToType(const FromType& x) { *x.converted() = true; } // NOLINT
vladloseva070cbd2009-11-18 00:09:28 +0000571};
572
573TEST(ReturnTest, ConvertsArgumentWhenConverted) {
574 bool converted = false;
575 FromType x(&converted);
576 Action<ToType()> action(Return(x));
577 EXPECT_TRUE(converted) << "Return must convert its argument in its own "
578 << "conversion operator.";
579 converted = false;
580 action.Perform(tuple<>());
581 EXPECT_FALSE(converted) << "Action must NOT convert its argument "
jgm79a367e2012-04-10 16:02:11 +0000582 << "when performed.";
vladloseva070cbd2009-11-18 00:09:28 +0000583}
584
vladloseva070cbd2009-11-18 00:09:28 +0000585class DestinationType {};
586
587class SourceType {
588 public:
589 // Note: a non-const typecast operator.
590 operator DestinationType() { return DestinationType(); }
591};
592
593TEST(ReturnTest, CanConvertArgumentUsingNonConstTypeCastOperator) {
594 SourceType s;
595 Action<DestinationType()> action(Return(s));
596}
vladloseva070cbd2009-11-18 00:09:28 +0000597
shiqiane35fdd92008-12-10 05:08:54 +0000598// Tests that ReturnNull() returns NULL in a pointer-returning function.
599TEST(ReturnNullTest, WorksInPointerReturningFunction) {
600 const Action<int*()> a1 = ReturnNull();
601 EXPECT_TRUE(a1.Perform(make_tuple()) == NULL);
602
603 const Action<const char*(bool)> a2 = ReturnNull(); // NOLINT
604 EXPECT_TRUE(a2.Perform(make_tuple(true)) == NULL);
605}
606
kosak53d49dc2015-01-08 03:03:09 +0000607#if GTEST_HAS_STD_UNIQUE_PTR_
608// Tests that ReturnNull() returns NULL for shared_ptr and unique_ptr returning
609// functions.
610TEST(ReturnNullTest, WorksInSmartPointerReturningFunction) {
611 const Action<std::unique_ptr<const int>()> a1 = ReturnNull();
612 EXPECT_TRUE(a1.Perform(make_tuple()) == nullptr);
613
614 const Action<std::shared_ptr<int>(std::string)> a2 = ReturnNull();
615 EXPECT_TRUE(a2.Perform(make_tuple("foo")) == nullptr);
616}
617#endif // GTEST_HAS_STD_UNIQUE_PTR_
618
shiqiane35fdd92008-12-10 05:08:54 +0000619// Tests that ReturnRef(v) works for reference types.
620TEST(ReturnRefTest, WorksForReference) {
621 const int n = 0;
622 const Action<const int&(bool)> ret = ReturnRef(n); // NOLINT
623
624 EXPECT_EQ(&n, &ret.Perform(make_tuple(true)));
625}
626
627// Tests that ReturnRef(v) is covariant.
628TEST(ReturnRefTest, IsCovariant) {
629 Base base;
630 Derived derived;
631 Action<Base&()> a = ReturnRef(base);
632 EXPECT_EQ(&base, &a.Perform(make_tuple()));
633
634 a = ReturnRef(derived);
635 EXPECT_EQ(&derived, &a.Perform(make_tuple()));
636}
637
zhanyong.wane3bd0982010-07-03 00:16:42 +0000638// Tests that ReturnRefOfCopy(v) works for reference types.
639TEST(ReturnRefOfCopyTest, WorksForReference) {
640 int n = 42;
641 const Action<const int&()> ret = ReturnRefOfCopy(n);
642
643 EXPECT_NE(&n, &ret.Perform(make_tuple()));
644 EXPECT_EQ(42, ret.Perform(make_tuple()));
645
646 n = 43;
647 EXPECT_NE(&n, &ret.Perform(make_tuple()));
648 EXPECT_EQ(42, ret.Perform(make_tuple()));
649}
650
651// Tests that ReturnRefOfCopy(v) is covariant.
652TEST(ReturnRefOfCopyTest, IsCovariant) {
653 Base base;
654 Derived derived;
655 Action<Base&()> a = ReturnRefOfCopy(base);
656 EXPECT_NE(&base, &a.Perform(make_tuple()));
657
658 a = ReturnRefOfCopy(derived);
659 EXPECT_NE(&derived, &a.Perform(make_tuple()));
660}
661
shiqiane35fdd92008-12-10 05:08:54 +0000662// Tests that DoDefault() does the default action for the mock method.
663
664class MyClass {};
665
666class MockClass {
667 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000668 MockClass() {}
669
shiqiane35fdd92008-12-10 05:08:54 +0000670 MOCK_METHOD1(IntFunc, int(bool flag)); // NOLINT
671 MOCK_METHOD0(Foo, MyClass());
kosak5b9cbbb2014-11-17 00:28:55 +0000672#if GTEST_HAS_STD_UNIQUE_PTR_
kosakb5c81092014-01-29 06:41:44 +0000673 MOCK_METHOD0(MakeUnique, std::unique_ptr<int>());
kosak3d1c78b2014-11-17 00:56:52 +0000674 MOCK_METHOD0(MakeUniqueBase, std::unique_ptr<Base>());
kosakb5c81092014-01-29 06:41:44 +0000675 MOCK_METHOD0(MakeVectorUnique, std::vector<std::unique_ptr<int>>());
676#endif
zhanyong.wan32de5f52009-12-23 00:13:23 +0000677
678 private:
679 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockClass);
shiqiane35fdd92008-12-10 05:08:54 +0000680};
681
682// Tests that DoDefault() returns the built-in default value for the
683// return type by default.
684TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) {
685 MockClass mock;
686 EXPECT_CALL(mock, IntFunc(_))
687 .WillOnce(DoDefault());
688 EXPECT_EQ(0, mock.IntFunc(true));
689}
690
zhanyong.wanedd4ab42013-02-28 22:58:51 +0000691// Tests that DoDefault() throws (when exceptions are enabled) or aborts
692// the process when there is no built-in default value for the return type.
shiqiane35fdd92008-12-10 05:08:54 +0000693TEST(DoDefaultDeathTest, DiesForUnknowType) {
694 MockClass mock;
695 EXPECT_CALL(mock, Foo())
696 .WillRepeatedly(DoDefault());
zhanyong.wanedd4ab42013-02-28 22:58:51 +0000697#if GTEST_HAS_EXCEPTIONS
698 EXPECT_ANY_THROW(mock.Foo());
699#else
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000700 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000701 mock.Foo();
702 }, "");
zhanyong.wanedd4ab42013-02-28 22:58:51 +0000703#endif
shiqiane35fdd92008-12-10 05:08:54 +0000704}
705
706// Tests that using DoDefault() inside a composite action leads to a
707// run-time error.
708
zhanyong.wan32de5f52009-12-23 00:13:23 +0000709void VoidFunc(bool /* flag */) {}
shiqiane35fdd92008-12-10 05:08:54 +0000710
711TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
712 MockClass mock;
713 EXPECT_CALL(mock, IntFunc(_))
714 .WillRepeatedly(DoAll(Invoke(VoidFunc),
715 DoDefault()));
716
717 // Ideally we should verify the error message as well. Sadly,
718 // EXPECT_DEATH() can only capture stderr, while Google Mock's
719 // errors are printed on stdout. Therefore we have to settle for
720 // not verifying the message.
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000721 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000722 mock.IntFunc(true);
723 }, "");
724}
725
shiqiane35fdd92008-12-10 05:08:54 +0000726// Tests that DoDefault() returns the default value set by
727// DefaultValue<T>::Set() when it's not overriden by an ON_CALL().
728TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) {
729 DefaultValue<int>::Set(1);
730 MockClass mock;
731 EXPECT_CALL(mock, IntFunc(_))
732 .WillOnce(DoDefault());
733 EXPECT_EQ(1, mock.IntFunc(false));
734 DefaultValue<int>::Clear();
735}
736
737// Tests that DoDefault() does the action specified by ON_CALL().
738TEST(DoDefaultTest, DoesWhatOnCallSpecifies) {
739 MockClass mock;
740 ON_CALL(mock, IntFunc(_))
741 .WillByDefault(Return(2));
742 EXPECT_CALL(mock, IntFunc(_))
743 .WillOnce(DoDefault());
744 EXPECT_EQ(2, mock.IntFunc(false));
745}
746
747// Tests that using DoDefault() in ON_CALL() leads to a run-time failure.
748TEST(DoDefaultTest, CannotBeUsedInOnCall) {
749 MockClass mock;
750 EXPECT_NONFATAL_FAILURE({ // NOLINT
751 ON_CALL(mock, IntFunc(_))
752 .WillByDefault(DoDefault());
753 }, "DoDefault() cannot be used in ON_CALL()");
754}
755
zhanyong.wan59214832010-10-05 05:58:51 +0000756// Tests that SetArgPointee<N>(v) sets the variable pointed to by
757// the N-th (0-based) argument to v.
758TEST(SetArgPointeeTest, SetsTheNthPointee) {
759 typedef void MyFunction(bool, int*, char*);
760 Action<MyFunction> a = SetArgPointee<1>(2);
761
762 int n = 0;
763 char ch = '\0';
764 a.Perform(make_tuple(true, &n, &ch));
765 EXPECT_EQ(2, n);
766 EXPECT_EQ('\0', ch);
767
768 a = SetArgPointee<2>('a');
769 n = 0;
770 ch = '\0';
771 a.Perform(make_tuple(true, &n, &ch));
772 EXPECT_EQ(0, n);
773 EXPECT_EQ('a', ch);
774}
775
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000776#if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN)
zhanyong.wana684b5a2010-12-02 23:30:50 +0000777// Tests that SetArgPointee<N>() accepts a string literal.
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000778// GCC prior to v4.0 and the Symbian compiler do not support this.
zhanyong.wana684b5a2010-12-02 23:30:50 +0000779TEST(SetArgPointeeTest, AcceptsStringLiteral) {
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000780 typedef void MyFunction(std::string*, const char**);
781 Action<MyFunction> a = SetArgPointee<0>("hi");
zhanyong.wana684b5a2010-12-02 23:30:50 +0000782 std::string str;
783 const char* ptr = NULL;
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000784 a.Perform(make_tuple(&str, &ptr));
zhanyong.wana684b5a2010-12-02 23:30:50 +0000785 EXPECT_EQ("hi", str);
786 EXPECT_TRUE(ptr == NULL);
787
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000788 a = SetArgPointee<1>("world");
zhanyong.wana684b5a2010-12-02 23:30:50 +0000789 str = "";
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000790 a.Perform(make_tuple(&str, &ptr));
zhanyong.wana684b5a2010-12-02 23:30:50 +0000791 EXPECT_EQ("", str);
792 EXPECT_STREQ("world", ptr);
793}
794
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000795TEST(SetArgPointeeTest, AcceptsWideStringLiteral) {
796 typedef void MyFunction(const wchar_t**);
797 Action<MyFunction> a = SetArgPointee<0>(L"world");
798 const wchar_t* ptr = NULL;
799 a.Perform(make_tuple(&ptr));
800 EXPECT_STREQ(L"world", ptr);
801
802# if GTEST_HAS_STD_WSTRING
803
804 typedef void MyStringFunction(std::wstring*);
805 Action<MyStringFunction> a2 = SetArgPointee<0>(L"world");
806 std::wstring str = L"";
807 a2.Perform(make_tuple(&str));
808 EXPECT_EQ(L"world", str);
809
810# endif
811}
812#endif
813
zhanyong.wana684b5a2010-12-02 23:30:50 +0000814// Tests that SetArgPointee<N>() accepts a char pointer.
815TEST(SetArgPointeeTest, AcceptsCharPointer) {
816 typedef void MyFunction(bool, std::string*, const char**);
817 const char* const hi = "hi";
818 Action<MyFunction> a = SetArgPointee<1>(hi);
819 std::string str;
820 const char* ptr = NULL;
821 a.Perform(make_tuple(true, &str, &ptr));
822 EXPECT_EQ("hi", str);
823 EXPECT_TRUE(ptr == NULL);
824
825 char world_array[] = "world";
826 char* const world = world_array;
827 a = SetArgPointee<2>(world);
828 str = "";
829 a.Perform(make_tuple(true, &str, &ptr));
830 EXPECT_EQ("", str);
831 EXPECT_EQ(world, ptr);
832}
833
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000834TEST(SetArgPointeeTest, AcceptsWideCharPointer) {
835 typedef void MyFunction(bool, const wchar_t**);
836 const wchar_t* const hi = L"hi";
837 Action<MyFunction> a = SetArgPointee<1>(hi);
838 const wchar_t* ptr = NULL;
839 a.Perform(make_tuple(true, &ptr));
840 EXPECT_EQ(hi, ptr);
841
842# if GTEST_HAS_STD_WSTRING
843
844 typedef void MyStringFunction(bool, std::wstring*);
845 wchar_t world_array[] = L"world";
846 wchar_t* const world = world_array;
847 Action<MyStringFunction> a2 = SetArgPointee<1>(world);
848 std::wstring str;
849 a2.Perform(make_tuple(true, &str));
850 EXPECT_EQ(world_array, str);
851# endif
852}
853
zhanyong.wan59214832010-10-05 05:58:51 +0000854#if GTEST_HAS_PROTOBUF_
855
856// Tests that SetArgPointee<N>(proto_buffer) sets the v1 protobuf
857// variable pointed to by the N-th (0-based) argument to proto_buffer.
858TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
859 TestMessage* const msg = new TestMessage;
860 msg->set_member("yes");
861 TestMessage orig_msg;
862 orig_msg.CopyFrom(*msg);
863
864 Action<void(bool, TestMessage*)> a = SetArgPointee<1>(*msg);
865 // SetArgPointee<N>(proto_buffer) makes a copy of proto_buffer
866 // s.t. the action works even when the original proto_buffer has
867 // died. We ensure this behavior by deleting msg before using the
868 // action.
869 delete msg;
870
871 TestMessage dest;
872 EXPECT_FALSE(orig_msg.Equals(dest));
873 a.Perform(make_tuple(true, &dest));
874 EXPECT_TRUE(orig_msg.Equals(dest));
875}
876
877// Tests that SetArgPointee<N>(proto_buffer) sets the
878// ::ProtocolMessage variable pointed to by the N-th (0-based)
879// argument to proto_buffer.
880TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
881 TestMessage* const msg = new TestMessage;
882 msg->set_member("yes");
883 TestMessage orig_msg;
884 orig_msg.CopyFrom(*msg);
885
886 Action<void(bool, ::ProtocolMessage*)> a = SetArgPointee<1>(*msg);
887 // SetArgPointee<N>(proto_buffer) makes a copy of proto_buffer
888 // s.t. the action works even when the original proto_buffer has
889 // died. We ensure this behavior by deleting msg before using the
890 // action.
891 delete msg;
892
893 TestMessage dest;
894 ::ProtocolMessage* const dest_base = &dest;
895 EXPECT_FALSE(orig_msg.Equals(dest));
896 a.Perform(make_tuple(true, dest_base));
897 EXPECT_TRUE(orig_msg.Equals(dest));
898}
899
900// Tests that SetArgPointee<N>(proto2_buffer) sets the v2
901// protobuf variable pointed to by the N-th (0-based) argument to
902// proto2_buffer.
903TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
904 using testing::internal::FooMessage;
905 FooMessage* const msg = new FooMessage;
906 msg->set_int_field(2);
907 msg->set_string_field("hi");
908 FooMessage orig_msg;
909 orig_msg.CopyFrom(*msg);
910
911 Action<void(bool, FooMessage*)> a = SetArgPointee<1>(*msg);
912 // SetArgPointee<N>(proto2_buffer) makes a copy of
913 // proto2_buffer s.t. the action works even when the original
914 // proto2_buffer has died. We ensure this behavior by deleting msg
915 // before using the action.
916 delete msg;
917
918 FooMessage dest;
919 dest.set_int_field(0);
920 a.Perform(make_tuple(true, &dest));
921 EXPECT_EQ(2, dest.int_field());
922 EXPECT_EQ("hi", dest.string_field());
923}
924
925// Tests that SetArgPointee<N>(proto2_buffer) sets the
926// proto2::Message variable pointed to by the N-th (0-based) argument
927// to proto2_buffer.
928TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
929 using testing::internal::FooMessage;
930 FooMessage* const msg = new FooMessage;
931 msg->set_int_field(2);
932 msg->set_string_field("hi");
933 FooMessage orig_msg;
934 orig_msg.CopyFrom(*msg);
935
936 Action<void(bool, ::proto2::Message*)> a = SetArgPointee<1>(*msg);
937 // SetArgPointee<N>(proto2_buffer) makes a copy of
938 // proto2_buffer s.t. the action works even when the original
939 // proto2_buffer has died. We ensure this behavior by deleting msg
940 // before using the action.
941 delete msg;
942
943 FooMessage dest;
944 dest.set_int_field(0);
945 ::proto2::Message* const dest_base = &dest;
946 a.Perform(make_tuple(true, dest_base));
947 EXPECT_EQ(2, dest.int_field());
948 EXPECT_EQ("hi", dest.string_field());
949}
950
951#endif // GTEST_HAS_PROTOBUF_
952
shiqiane35fdd92008-12-10 05:08:54 +0000953// Tests that SetArgumentPointee<N>(v) sets the variable pointed to by
954// the N-th (0-based) argument to v.
955TEST(SetArgumentPointeeTest, SetsTheNthPointee) {
956 typedef void MyFunction(bool, int*, char*);
957 Action<MyFunction> a = SetArgumentPointee<1>(2);
958
959 int n = 0;
960 char ch = '\0';
961 a.Perform(make_tuple(true, &n, &ch));
962 EXPECT_EQ(2, n);
963 EXPECT_EQ('\0', ch);
964
965 a = SetArgumentPointee<2>('a');
966 n = 0;
967 ch = '\0';
968 a.Perform(make_tuple(true, &n, &ch));
969 EXPECT_EQ(0, n);
970 EXPECT_EQ('a', ch);
971}
972
zhanyong.wan02f71062010-05-10 17:14:29 +0000973#if GTEST_HAS_PROTOBUF_
shiqiane35fdd92008-12-10 05:08:54 +0000974
zhanyong.wanc6a41232009-05-13 23:38:40 +0000975// Tests that SetArgumentPointee<N>(proto_buffer) sets the v1 protobuf
976// variable pointed to by the N-th (0-based) argument to proto_buffer.
shiqiane35fdd92008-12-10 05:08:54 +0000977TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
shiqiane35fdd92008-12-10 05:08:54 +0000978 TestMessage* const msg = new TestMessage;
979 msg->set_member("yes");
980 TestMessage orig_msg;
981 orig_msg.CopyFrom(*msg);
982
zhanyong.wanc6a41232009-05-13 23:38:40 +0000983 Action<void(bool, TestMessage*)> a = SetArgumentPointee<1>(*msg);
shiqiane35fdd92008-12-10 05:08:54 +0000984 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
985 // s.t. the action works even when the original proto_buffer has
986 // died. We ensure this behavior by deleting msg before using the
987 // action.
988 delete msg;
989
990 TestMessage dest;
991 EXPECT_FALSE(orig_msg.Equals(dest));
992 a.Perform(make_tuple(true, &dest));
993 EXPECT_TRUE(orig_msg.Equals(dest));
994}
995
zhanyong.wanc6a41232009-05-13 23:38:40 +0000996// Tests that SetArgumentPointee<N>(proto_buffer) sets the
997// ::ProtocolMessage variable pointed to by the N-th (0-based)
998// argument to proto_buffer.
999TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
1000 TestMessage* const msg = new TestMessage;
1001 msg->set_member("yes");
1002 TestMessage orig_msg;
1003 orig_msg.CopyFrom(*msg);
1004
1005 Action<void(bool, ::ProtocolMessage*)> a = SetArgumentPointee<1>(*msg);
1006 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
1007 // s.t. the action works even when the original proto_buffer has
1008 // died. We ensure this behavior by deleting msg before using the
1009 // action.
1010 delete msg;
1011
1012 TestMessage dest;
1013 ::ProtocolMessage* const dest_base = &dest;
1014 EXPECT_FALSE(orig_msg.Equals(dest));
1015 a.Perform(make_tuple(true, dest_base));
1016 EXPECT_TRUE(orig_msg.Equals(dest));
1017}
1018
1019// Tests that SetArgumentPointee<N>(proto2_buffer) sets the v2
1020// protobuf variable pointed to by the N-th (0-based) argument to
1021// proto2_buffer.
shiqiane35fdd92008-12-10 05:08:54 +00001022TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
1023 using testing::internal::FooMessage;
shiqiane35fdd92008-12-10 05:08:54 +00001024 FooMessage* const msg = new FooMessage;
1025 msg->set_int_field(2);
1026 msg->set_string_field("hi");
1027 FooMessage orig_msg;
1028 orig_msg.CopyFrom(*msg);
1029
zhanyong.wanc6a41232009-05-13 23:38:40 +00001030 Action<void(bool, FooMessage*)> a = SetArgumentPointee<1>(*msg);
shiqiane35fdd92008-12-10 05:08:54 +00001031 // SetArgumentPointee<N>(proto2_buffer) makes a copy of
1032 // proto2_buffer s.t. the action works even when the original
1033 // proto2_buffer has died. We ensure this behavior by deleting msg
1034 // before using the action.
1035 delete msg;
1036
1037 FooMessage dest;
1038 dest.set_int_field(0);
1039 a.Perform(make_tuple(true, &dest));
1040 EXPECT_EQ(2, dest.int_field());
1041 EXPECT_EQ("hi", dest.string_field());
1042}
1043
zhanyong.wanc6a41232009-05-13 23:38:40 +00001044// Tests that SetArgumentPointee<N>(proto2_buffer) sets the
1045// proto2::Message variable pointed to by the N-th (0-based) argument
1046// to proto2_buffer.
1047TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
1048 using testing::internal::FooMessage;
1049 FooMessage* const msg = new FooMessage;
1050 msg->set_int_field(2);
1051 msg->set_string_field("hi");
1052 FooMessage orig_msg;
1053 orig_msg.CopyFrom(*msg);
1054
1055 Action<void(bool, ::proto2::Message*)> a = SetArgumentPointee<1>(*msg);
1056 // SetArgumentPointee<N>(proto2_buffer) makes a copy of
1057 // proto2_buffer s.t. the action works even when the original
1058 // proto2_buffer has died. We ensure this behavior by deleting msg
1059 // before using the action.
1060 delete msg;
1061
1062 FooMessage dest;
1063 dest.set_int_field(0);
1064 ::proto2::Message* const dest_base = &dest;
1065 a.Perform(make_tuple(true, dest_base));
1066 EXPECT_EQ(2, dest.int_field());
1067 EXPECT_EQ("hi", dest.string_field());
1068}
1069
zhanyong.wan02f71062010-05-10 17:14:29 +00001070#endif // GTEST_HAS_PROTOBUF_
shiqiane35fdd92008-12-10 05:08:54 +00001071
shiqiane35fdd92008-12-10 05:08:54 +00001072// Sample functions and functors for testing Invoke() and etc.
1073int Nullary() { return 1; }
1074
1075class NullaryFunctor {
1076 public:
1077 int operator()() { return 2; }
1078};
1079
1080bool g_done = false;
1081void VoidNullary() { g_done = true; }
1082
1083class VoidNullaryFunctor {
1084 public:
1085 void operator()() { g_done = true; }
1086};
1087
shiqiane35fdd92008-12-10 05:08:54 +00001088class Foo {
1089 public:
1090 Foo() : value_(123) {}
1091
1092 int Nullary() const { return value_; }
zhanyong.wan29be9232013-03-01 06:53:35 +00001093
shiqiane35fdd92008-12-10 05:08:54 +00001094 private:
1095 int value_;
1096};
1097
1098// Tests InvokeWithoutArgs(function).
1099TEST(InvokeWithoutArgsTest, Function) {
1100 // As an action that takes one argument.
1101 Action<int(int)> a = InvokeWithoutArgs(Nullary); // NOLINT
1102 EXPECT_EQ(1, a.Perform(make_tuple(2)));
1103
1104 // As an action that takes two arguments.
zhanyong.wan32de5f52009-12-23 00:13:23 +00001105 Action<int(int, double)> a2 = InvokeWithoutArgs(Nullary); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00001106 EXPECT_EQ(1, a2.Perform(make_tuple(2, 3.5)));
1107
1108 // As an action that returns void.
1109 Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary); // NOLINT
1110 g_done = false;
1111 a3.Perform(make_tuple(1));
1112 EXPECT_TRUE(g_done);
1113}
1114
1115// Tests InvokeWithoutArgs(functor).
1116TEST(InvokeWithoutArgsTest, Functor) {
1117 // As an action that takes no argument.
1118 Action<int()> a = InvokeWithoutArgs(NullaryFunctor()); // NOLINT
1119 EXPECT_EQ(2, a.Perform(make_tuple()));
1120
1121 // As an action that takes three arguments.
zhanyong.wan32de5f52009-12-23 00:13:23 +00001122 Action<int(int, double, char)> a2 = // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00001123 InvokeWithoutArgs(NullaryFunctor());
1124 EXPECT_EQ(2, a2.Perform(make_tuple(3, 3.5, 'a')));
1125
1126 // As an action that returns void.
1127 Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor());
1128 g_done = false;
1129 a3.Perform(make_tuple());
1130 EXPECT_TRUE(g_done);
1131}
1132
1133// Tests InvokeWithoutArgs(obj_ptr, method).
1134TEST(InvokeWithoutArgsTest, Method) {
1135 Foo foo;
1136 Action<int(bool, char)> a = // NOLINT
1137 InvokeWithoutArgs(&foo, &Foo::Nullary);
1138 EXPECT_EQ(123, a.Perform(make_tuple(true, 'a')));
1139}
1140
1141// Tests using IgnoreResult() on a polymorphic action.
1142TEST(IgnoreResultTest, PolymorphicAction) {
1143 Action<void(int)> a = IgnoreResult(Return(5)); // NOLINT
1144 a.Perform(make_tuple(1));
1145}
1146
1147// Tests using IgnoreResult() on a monomorphic action.
1148
1149int ReturnOne() {
1150 g_done = true;
1151 return 1;
1152}
1153
1154TEST(IgnoreResultTest, MonomorphicAction) {
1155 g_done = false;
1156 Action<void()> a = IgnoreResult(Invoke(ReturnOne));
1157 a.Perform(make_tuple());
1158 EXPECT_TRUE(g_done);
1159}
1160
1161// Tests using IgnoreResult() on an action that returns a class type.
1162
zhanyong.wan32de5f52009-12-23 00:13:23 +00001163MyClass ReturnMyClass(double /* x */) {
shiqiane35fdd92008-12-10 05:08:54 +00001164 g_done = true;
1165 return MyClass();
1166}
1167
1168TEST(IgnoreResultTest, ActionReturningClass) {
1169 g_done = false;
1170 Action<void(int)> a = IgnoreResult(Invoke(ReturnMyClass)); // NOLINT
1171 a.Perform(make_tuple(2));
1172 EXPECT_TRUE(g_done);
1173}
1174
1175TEST(AssignTest, Int) {
1176 int x = 0;
1177 Action<void(int)> a = Assign(&x, 5);
1178 a.Perform(make_tuple(0));
1179 EXPECT_EQ(5, x);
1180}
1181
1182TEST(AssignTest, String) {
1183 ::std::string x;
1184 Action<void(void)> a = Assign(&x, "Hello, world");
1185 a.Perform(make_tuple());
1186 EXPECT_EQ("Hello, world", x);
1187}
1188
1189TEST(AssignTest, CompatibleTypes) {
1190 double x = 0;
1191 Action<void(int)> a = Assign(&x, 5);
1192 a.Perform(make_tuple(0));
1193 EXPECT_DOUBLE_EQ(5, x);
1194}
1195
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001196#if !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001197
shiqiane35fdd92008-12-10 05:08:54 +00001198class SetErrnoAndReturnTest : public testing::Test {
1199 protected:
1200 virtual void SetUp() { errno = 0; }
1201 virtual void TearDown() { errno = 0; }
1202};
1203
1204TEST_F(SetErrnoAndReturnTest, Int) {
1205 Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5);
1206 EXPECT_EQ(-5, a.Perform(make_tuple()));
1207 EXPECT_EQ(ENOTTY, errno);
1208}
1209
1210TEST_F(SetErrnoAndReturnTest, Ptr) {
1211 int x;
1212 Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x);
1213 EXPECT_EQ(&x, a.Perform(make_tuple()));
1214 EXPECT_EQ(ENOTTY, errno);
1215}
1216
1217TEST_F(SetErrnoAndReturnTest, CompatibleTypes) {
1218 Action<double()> a = SetErrnoAndReturn(EINVAL, 5);
1219 EXPECT_DOUBLE_EQ(5.0, a.Perform(make_tuple()));
1220 EXPECT_EQ(EINVAL, errno);
1221}
1222
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001223#endif // !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001224
zhanyong.wana18423e2009-07-22 23:58:19 +00001225// Tests ByRef().
1226
1227// Tests that ReferenceWrapper<T> is copyable.
1228TEST(ByRefTest, IsCopyable) {
1229 const std::string s1 = "Hi";
1230 const std::string s2 = "Hello";
1231
jgm79a367e2012-04-10 16:02:11 +00001232 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper =
1233 ByRef(s1);
zhanyong.wana18423e2009-07-22 23:58:19 +00001234 const std::string& r1 = ref_wrapper;
1235 EXPECT_EQ(&s1, &r1);
1236
1237 // Assigns a new value to ref_wrapper.
1238 ref_wrapper = ByRef(s2);
1239 const std::string& r2 = ref_wrapper;
1240 EXPECT_EQ(&s2, &r2);
1241
jgm79a367e2012-04-10 16:02:11 +00001242 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper1 =
1243 ByRef(s1);
zhanyong.wana18423e2009-07-22 23:58:19 +00001244 // Copies ref_wrapper1 to ref_wrapper.
1245 ref_wrapper = ref_wrapper1;
1246 const std::string& r3 = ref_wrapper;
1247 EXPECT_EQ(&s1, &r3);
1248}
1249
1250// Tests using ByRef() on a const value.
1251TEST(ByRefTest, ConstValue) {
1252 const int n = 0;
1253 // int& ref = ByRef(n); // This shouldn't compile - we have a
1254 // negative compilation test to catch it.
1255 const int& const_ref = ByRef(n);
1256 EXPECT_EQ(&n, &const_ref);
1257}
1258
1259// Tests using ByRef() on a non-const value.
1260TEST(ByRefTest, NonConstValue) {
1261 int n = 0;
1262
1263 // ByRef(n) can be used as either an int&,
1264 int& ref = ByRef(n);
1265 EXPECT_EQ(&n, &ref);
1266
1267 // or a const int&.
1268 const int& const_ref = ByRef(n);
1269 EXPECT_EQ(&n, &const_ref);
1270}
1271
1272// Tests explicitly specifying the type when using ByRef().
1273TEST(ByRefTest, ExplicitType) {
1274 int n = 0;
1275 const int& r1 = ByRef<const int>(n);
1276 EXPECT_EQ(&n, &r1);
1277
1278 // ByRef<char>(n); // This shouldn't compile - we have a negative
1279 // compilation test to catch it.
1280
1281 Derived d;
1282 Derived& r2 = ByRef<Derived>(d);
1283 EXPECT_EQ(&d, &r2);
1284
1285 const Derived& r3 = ByRef<const Derived>(d);
1286 EXPECT_EQ(&d, &r3);
1287
1288 Base& r4 = ByRef<Base>(d);
1289 EXPECT_EQ(&d, &r4);
1290
1291 const Base& r5 = ByRef<const Base>(d);
1292 EXPECT_EQ(&d, &r5);
1293
1294 // The following shouldn't compile - we have a negative compilation
1295 // test for it.
1296 //
1297 // Base b;
1298 // ByRef<Derived>(b);
1299}
1300
1301// Tests that Google Mock prints expression ByRef(x) as a reference to x.
1302TEST(ByRefTest, PrintsCorrectly) {
1303 int n = 42;
1304 ::std::stringstream expected, actual;
1305 testing::internal::UniversalPrinter<const int&>::Print(n, &expected);
1306 testing::internal::UniversalPrint(ByRef(n), &actual);
1307 EXPECT_EQ(expected.str(), actual.str());
1308}
1309
kosak5b9cbbb2014-11-17 00:28:55 +00001310#if GTEST_HAS_STD_UNIQUE_PTR_
kosakb5c81092014-01-29 06:41:44 +00001311
1312std::unique_ptr<int> UniquePtrSource() {
1313 return std::unique_ptr<int>(new int(19));
1314}
1315
1316std::vector<std::unique_ptr<int>> VectorUniquePtrSource() {
1317 std::vector<std::unique_ptr<int>> out;
1318 out.emplace_back(new int(7));
1319 return out;
1320}
1321
kosak3d1c78b2014-11-17 00:56:52 +00001322TEST(MockMethodTest, CanReturnMoveOnlyValue_Return) {
1323 MockClass mock;
1324 std::unique_ptr<int> i(new int(19));
1325 EXPECT_CALL(mock, MakeUnique()).WillOnce(Return(ByMove(std::move(i))));
1326 EXPECT_CALL(mock, MakeVectorUnique())
1327 .WillOnce(Return(ByMove(VectorUniquePtrSource())));
1328 Derived* d = new Derived;
1329 EXPECT_CALL(mock, MakeUniqueBase())
1330 .WillOnce(Return(ByMove(std::unique_ptr<Derived>(d))));
1331
1332 std::unique_ptr<int> result1 = mock.MakeUnique();
1333 EXPECT_EQ(19, *result1);
1334
1335 std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
kosak389bad62014-11-17 01:08:51 +00001336 EXPECT_EQ(1u, vresult.size());
kosak3d1c78b2014-11-17 00:56:52 +00001337 EXPECT_NE(nullptr, vresult[0]);
1338 EXPECT_EQ(7, *vresult[0]);
1339
1340 std::unique_ptr<Base> result2 = mock.MakeUniqueBase();
1341 EXPECT_EQ(d, result2.get());
1342}
1343
1344TEST(MockMethodTest, CanReturnMoveOnlyValue_DoAllReturn) {
1345 testing::MockFunction<void()> mock_function;
1346 MockClass mock;
1347 std::unique_ptr<int> i(new int(19));
1348 EXPECT_CALL(mock_function, Call());
1349 EXPECT_CALL(mock, MakeUnique()).WillOnce(DoAll(
1350 InvokeWithoutArgs(&mock_function, &testing::MockFunction<void()>::Call),
1351 Return(ByMove(std::move(i)))));
1352
1353 std::unique_ptr<int> result1 = mock.MakeUnique();
1354 EXPECT_EQ(19, *result1);
1355}
1356
1357TEST(MockMethodTest, CanReturnMoveOnlyValue_Invoke) {
kosakb5c81092014-01-29 06:41:44 +00001358 MockClass mock;
1359
1360 // Check default value
1361 DefaultValue<std::unique_ptr<int>>::SetFactory([] {
1362 return std::unique_ptr<int>(new int(42));
1363 });
1364 EXPECT_EQ(42, *mock.MakeUnique());
1365
kosak3d1c78b2014-11-17 00:56:52 +00001366 EXPECT_CALL(mock, MakeUnique()).WillRepeatedly(Invoke(UniquePtrSource));
kosakb5c81092014-01-29 06:41:44 +00001367 EXPECT_CALL(mock, MakeVectorUnique())
1368 .WillRepeatedly(Invoke(VectorUniquePtrSource));
1369 std::unique_ptr<int> result1 = mock.MakeUnique();
1370 EXPECT_EQ(19, *result1);
1371 std::unique_ptr<int> result2 = mock.MakeUnique();
1372 EXPECT_EQ(19, *result2);
1373 EXPECT_NE(result1, result2);
1374
1375 std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
kosak389bad62014-11-17 01:08:51 +00001376 EXPECT_EQ(1u, vresult.size());
kosakb5c81092014-01-29 06:41:44 +00001377 EXPECT_NE(nullptr, vresult[0]);
1378 EXPECT_EQ(7, *vresult[0]);
1379}
1380
kosak5b9cbbb2014-11-17 00:28:55 +00001381#endif // GTEST_HAS_STD_UNIQUE_PTR_
kosakb5c81092014-01-29 06:41:44 +00001382
shiqiane35fdd92008-12-10 05:08:54 +00001383} // Unnamed namespace