blob: 28b48f16354d393944aab88173068560d701d00e [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
512// Tests that Return(v) is covaraint.
513
514struct Base {
515 bool operator==(const Base&) { return true; }
516};
517
518struct Derived : public Base {
519 bool operator==(const Derived&) { return true; }
520};
521
522TEST(ReturnTest, IsCovariant) {
523 Base base;
524 Derived derived;
525 Action<Base*()> ret = Return(&base);
526 EXPECT_EQ(&base, ret.Perform(make_tuple()));
527
528 ret = Return(&derived);
529 EXPECT_EQ(&derived, ret.Perform(make_tuple()));
530}
531
vladloseva070cbd2009-11-18 00:09:28 +0000532// Tests that the type of the value passed into Return is converted into T
533// when the action is cast to Action<T(...)> rather than when the action is
534// performed. See comments on testing::internal::ReturnAction in
535// gmock-actions.h for more information.
536class FromType {
537 public:
jgm79a367e2012-04-10 16:02:11 +0000538 explicit FromType(bool* is_converted) : converted_(is_converted) {}
vladloseva070cbd2009-11-18 00:09:28 +0000539 bool* converted() const { return converted_; }
540
541 private:
542 bool* const converted_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000543
544 GTEST_DISALLOW_ASSIGN_(FromType);
vladloseva070cbd2009-11-18 00:09:28 +0000545};
546
547class ToType {
548 public:
jgm79a367e2012-04-10 16:02:11 +0000549 // Must allow implicit conversion due to use in ImplicitCast_<T>.
550 ToType(const FromType& x) { *x.converted() = true; } // NOLINT
vladloseva070cbd2009-11-18 00:09:28 +0000551};
552
553TEST(ReturnTest, ConvertsArgumentWhenConverted) {
554 bool converted = false;
555 FromType x(&converted);
556 Action<ToType()> action(Return(x));
557 EXPECT_TRUE(converted) << "Return must convert its argument in its own "
558 << "conversion operator.";
559 converted = false;
560 action.Perform(tuple<>());
561 EXPECT_FALSE(converted) << "Action must NOT convert its argument "
jgm79a367e2012-04-10 16:02:11 +0000562 << "when performed.";
vladloseva070cbd2009-11-18 00:09:28 +0000563}
564
vladloseva070cbd2009-11-18 00:09:28 +0000565class DestinationType {};
566
567class SourceType {
568 public:
569 // Note: a non-const typecast operator.
570 operator DestinationType() { return DestinationType(); }
571};
572
573TEST(ReturnTest, CanConvertArgumentUsingNonConstTypeCastOperator) {
574 SourceType s;
575 Action<DestinationType()> action(Return(s));
576}
vladloseva070cbd2009-11-18 00:09:28 +0000577
shiqiane35fdd92008-12-10 05:08:54 +0000578// Tests that ReturnNull() returns NULL in a pointer-returning function.
579TEST(ReturnNullTest, WorksInPointerReturningFunction) {
580 const Action<int*()> a1 = ReturnNull();
581 EXPECT_TRUE(a1.Perform(make_tuple()) == NULL);
582
583 const Action<const char*(bool)> a2 = ReturnNull(); // NOLINT
584 EXPECT_TRUE(a2.Perform(make_tuple(true)) == NULL);
585}
586
587// Tests that ReturnRef(v) works for reference types.
588TEST(ReturnRefTest, WorksForReference) {
589 const int n = 0;
590 const Action<const int&(bool)> ret = ReturnRef(n); // NOLINT
591
592 EXPECT_EQ(&n, &ret.Perform(make_tuple(true)));
593}
594
595// Tests that ReturnRef(v) is covariant.
596TEST(ReturnRefTest, IsCovariant) {
597 Base base;
598 Derived derived;
599 Action<Base&()> a = ReturnRef(base);
600 EXPECT_EQ(&base, &a.Perform(make_tuple()));
601
602 a = ReturnRef(derived);
603 EXPECT_EQ(&derived, &a.Perform(make_tuple()));
604}
605
zhanyong.wane3bd0982010-07-03 00:16:42 +0000606// Tests that ReturnRefOfCopy(v) works for reference types.
607TEST(ReturnRefOfCopyTest, WorksForReference) {
608 int n = 42;
609 const Action<const int&()> ret = ReturnRefOfCopy(n);
610
611 EXPECT_NE(&n, &ret.Perform(make_tuple()));
612 EXPECT_EQ(42, ret.Perform(make_tuple()));
613
614 n = 43;
615 EXPECT_NE(&n, &ret.Perform(make_tuple()));
616 EXPECT_EQ(42, ret.Perform(make_tuple()));
617}
618
619// Tests that ReturnRefOfCopy(v) is covariant.
620TEST(ReturnRefOfCopyTest, IsCovariant) {
621 Base base;
622 Derived derived;
623 Action<Base&()> a = ReturnRefOfCopy(base);
624 EXPECT_NE(&base, &a.Perform(make_tuple()));
625
626 a = ReturnRefOfCopy(derived);
627 EXPECT_NE(&derived, &a.Perform(make_tuple()));
628}
629
shiqiane35fdd92008-12-10 05:08:54 +0000630// Tests that DoDefault() does the default action for the mock method.
631
632class MyClass {};
633
634class MockClass {
635 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000636 MockClass() {}
637
shiqiane35fdd92008-12-10 05:08:54 +0000638 MOCK_METHOD1(IntFunc, int(bool flag)); // NOLINT
639 MOCK_METHOD0(Foo, MyClass());
kosak5b9cbbb2014-11-17 00:28:55 +0000640#if GTEST_HAS_STD_UNIQUE_PTR_
kosakb5c81092014-01-29 06:41:44 +0000641 MOCK_METHOD0(MakeUnique, std::unique_ptr<int>());
kosak3d1c78b2014-11-17 00:56:52 +0000642 MOCK_METHOD0(MakeUniqueBase, std::unique_ptr<Base>());
kosakb5c81092014-01-29 06:41:44 +0000643 MOCK_METHOD0(MakeVectorUnique, std::vector<std::unique_ptr<int>>());
644#endif
zhanyong.wan32de5f52009-12-23 00:13:23 +0000645
646 private:
647 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockClass);
shiqiane35fdd92008-12-10 05:08:54 +0000648};
649
650// Tests that DoDefault() returns the built-in default value for the
651// return type by default.
652TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) {
653 MockClass mock;
654 EXPECT_CALL(mock, IntFunc(_))
655 .WillOnce(DoDefault());
656 EXPECT_EQ(0, mock.IntFunc(true));
657}
658
zhanyong.wanedd4ab42013-02-28 22:58:51 +0000659// Tests that DoDefault() throws (when exceptions are enabled) or aborts
660// the process when there is no built-in default value for the return type.
shiqiane35fdd92008-12-10 05:08:54 +0000661TEST(DoDefaultDeathTest, DiesForUnknowType) {
662 MockClass mock;
663 EXPECT_CALL(mock, Foo())
664 .WillRepeatedly(DoDefault());
zhanyong.wanedd4ab42013-02-28 22:58:51 +0000665#if GTEST_HAS_EXCEPTIONS
666 EXPECT_ANY_THROW(mock.Foo());
667#else
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000668 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000669 mock.Foo();
670 }, "");
zhanyong.wanedd4ab42013-02-28 22:58:51 +0000671#endif
shiqiane35fdd92008-12-10 05:08:54 +0000672}
673
674// Tests that using DoDefault() inside a composite action leads to a
675// run-time error.
676
zhanyong.wan32de5f52009-12-23 00:13:23 +0000677void VoidFunc(bool /* flag */) {}
shiqiane35fdd92008-12-10 05:08:54 +0000678
679TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
680 MockClass mock;
681 EXPECT_CALL(mock, IntFunc(_))
682 .WillRepeatedly(DoAll(Invoke(VoidFunc),
683 DoDefault()));
684
685 // Ideally we should verify the error message as well. Sadly,
686 // EXPECT_DEATH() can only capture stderr, while Google Mock's
687 // errors are printed on stdout. Therefore we have to settle for
688 // not verifying the message.
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000689 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000690 mock.IntFunc(true);
691 }, "");
692}
693
shiqiane35fdd92008-12-10 05:08:54 +0000694// Tests that DoDefault() returns the default value set by
695// DefaultValue<T>::Set() when it's not overriden by an ON_CALL().
696TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) {
697 DefaultValue<int>::Set(1);
698 MockClass mock;
699 EXPECT_CALL(mock, IntFunc(_))
700 .WillOnce(DoDefault());
701 EXPECT_EQ(1, mock.IntFunc(false));
702 DefaultValue<int>::Clear();
703}
704
705// Tests that DoDefault() does the action specified by ON_CALL().
706TEST(DoDefaultTest, DoesWhatOnCallSpecifies) {
707 MockClass mock;
708 ON_CALL(mock, IntFunc(_))
709 .WillByDefault(Return(2));
710 EXPECT_CALL(mock, IntFunc(_))
711 .WillOnce(DoDefault());
712 EXPECT_EQ(2, mock.IntFunc(false));
713}
714
715// Tests that using DoDefault() in ON_CALL() leads to a run-time failure.
716TEST(DoDefaultTest, CannotBeUsedInOnCall) {
717 MockClass mock;
718 EXPECT_NONFATAL_FAILURE({ // NOLINT
719 ON_CALL(mock, IntFunc(_))
720 .WillByDefault(DoDefault());
721 }, "DoDefault() cannot be used in ON_CALL()");
722}
723
zhanyong.wan59214832010-10-05 05:58:51 +0000724// Tests that SetArgPointee<N>(v) sets the variable pointed to by
725// the N-th (0-based) argument to v.
726TEST(SetArgPointeeTest, SetsTheNthPointee) {
727 typedef void MyFunction(bool, int*, char*);
728 Action<MyFunction> a = SetArgPointee<1>(2);
729
730 int n = 0;
731 char ch = '\0';
732 a.Perform(make_tuple(true, &n, &ch));
733 EXPECT_EQ(2, n);
734 EXPECT_EQ('\0', ch);
735
736 a = SetArgPointee<2>('a');
737 n = 0;
738 ch = '\0';
739 a.Perform(make_tuple(true, &n, &ch));
740 EXPECT_EQ(0, n);
741 EXPECT_EQ('a', ch);
742}
743
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000744#if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN)
zhanyong.wana684b5a2010-12-02 23:30:50 +0000745// Tests that SetArgPointee<N>() accepts a string literal.
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000746// GCC prior to v4.0 and the Symbian compiler do not support this.
zhanyong.wana684b5a2010-12-02 23:30:50 +0000747TEST(SetArgPointeeTest, AcceptsStringLiteral) {
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000748 typedef void MyFunction(std::string*, const char**);
749 Action<MyFunction> a = SetArgPointee<0>("hi");
zhanyong.wana684b5a2010-12-02 23:30:50 +0000750 std::string str;
751 const char* ptr = NULL;
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000752 a.Perform(make_tuple(&str, &ptr));
zhanyong.wana684b5a2010-12-02 23:30:50 +0000753 EXPECT_EQ("hi", str);
754 EXPECT_TRUE(ptr == NULL);
755
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000756 a = SetArgPointee<1>("world");
zhanyong.wana684b5a2010-12-02 23:30:50 +0000757 str = "";
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000758 a.Perform(make_tuple(&str, &ptr));
zhanyong.wana684b5a2010-12-02 23:30:50 +0000759 EXPECT_EQ("", str);
760 EXPECT_STREQ("world", ptr);
761}
762
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000763TEST(SetArgPointeeTest, AcceptsWideStringLiteral) {
764 typedef void MyFunction(const wchar_t**);
765 Action<MyFunction> a = SetArgPointee<0>(L"world");
766 const wchar_t* ptr = NULL;
767 a.Perform(make_tuple(&ptr));
768 EXPECT_STREQ(L"world", ptr);
769
770# if GTEST_HAS_STD_WSTRING
771
772 typedef void MyStringFunction(std::wstring*);
773 Action<MyStringFunction> a2 = SetArgPointee<0>(L"world");
774 std::wstring str = L"";
775 a2.Perform(make_tuple(&str));
776 EXPECT_EQ(L"world", str);
777
778# endif
779}
780#endif
781
zhanyong.wana684b5a2010-12-02 23:30:50 +0000782// Tests that SetArgPointee<N>() accepts a char pointer.
783TEST(SetArgPointeeTest, AcceptsCharPointer) {
784 typedef void MyFunction(bool, std::string*, const char**);
785 const char* const hi = "hi";
786 Action<MyFunction> a = SetArgPointee<1>(hi);
787 std::string str;
788 const char* ptr = NULL;
789 a.Perform(make_tuple(true, &str, &ptr));
790 EXPECT_EQ("hi", str);
791 EXPECT_TRUE(ptr == NULL);
792
793 char world_array[] = "world";
794 char* const world = world_array;
795 a = SetArgPointee<2>(world);
796 str = "";
797 a.Perform(make_tuple(true, &str, &ptr));
798 EXPECT_EQ("", str);
799 EXPECT_EQ(world, ptr);
800}
801
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000802TEST(SetArgPointeeTest, AcceptsWideCharPointer) {
803 typedef void MyFunction(bool, const wchar_t**);
804 const wchar_t* const hi = L"hi";
805 Action<MyFunction> a = SetArgPointee<1>(hi);
806 const wchar_t* ptr = NULL;
807 a.Perform(make_tuple(true, &ptr));
808 EXPECT_EQ(hi, ptr);
809
810# if GTEST_HAS_STD_WSTRING
811
812 typedef void MyStringFunction(bool, std::wstring*);
813 wchar_t world_array[] = L"world";
814 wchar_t* const world = world_array;
815 Action<MyStringFunction> a2 = SetArgPointee<1>(world);
816 std::wstring str;
817 a2.Perform(make_tuple(true, &str));
818 EXPECT_EQ(world_array, str);
819# endif
820}
821
zhanyong.wan59214832010-10-05 05:58:51 +0000822#if GTEST_HAS_PROTOBUF_
823
824// Tests that SetArgPointee<N>(proto_buffer) sets the v1 protobuf
825// variable pointed to by the N-th (0-based) argument to proto_buffer.
826TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
827 TestMessage* const msg = new TestMessage;
828 msg->set_member("yes");
829 TestMessage orig_msg;
830 orig_msg.CopyFrom(*msg);
831
832 Action<void(bool, TestMessage*)> a = SetArgPointee<1>(*msg);
833 // SetArgPointee<N>(proto_buffer) makes a copy of proto_buffer
834 // s.t. the action works even when the original proto_buffer has
835 // died. We ensure this behavior by deleting msg before using the
836 // action.
837 delete msg;
838
839 TestMessage dest;
840 EXPECT_FALSE(orig_msg.Equals(dest));
841 a.Perform(make_tuple(true, &dest));
842 EXPECT_TRUE(orig_msg.Equals(dest));
843}
844
845// Tests that SetArgPointee<N>(proto_buffer) sets the
846// ::ProtocolMessage variable pointed to by the N-th (0-based)
847// argument to proto_buffer.
848TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
849 TestMessage* const msg = new TestMessage;
850 msg->set_member("yes");
851 TestMessage orig_msg;
852 orig_msg.CopyFrom(*msg);
853
854 Action<void(bool, ::ProtocolMessage*)> a = SetArgPointee<1>(*msg);
855 // SetArgPointee<N>(proto_buffer) makes a copy of proto_buffer
856 // s.t. the action works even when the original proto_buffer has
857 // died. We ensure this behavior by deleting msg before using the
858 // action.
859 delete msg;
860
861 TestMessage dest;
862 ::ProtocolMessage* const dest_base = &dest;
863 EXPECT_FALSE(orig_msg.Equals(dest));
864 a.Perform(make_tuple(true, dest_base));
865 EXPECT_TRUE(orig_msg.Equals(dest));
866}
867
868// Tests that SetArgPointee<N>(proto2_buffer) sets the v2
869// protobuf variable pointed to by the N-th (0-based) argument to
870// proto2_buffer.
871TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
872 using testing::internal::FooMessage;
873 FooMessage* const msg = new FooMessage;
874 msg->set_int_field(2);
875 msg->set_string_field("hi");
876 FooMessage orig_msg;
877 orig_msg.CopyFrom(*msg);
878
879 Action<void(bool, FooMessage*)> a = SetArgPointee<1>(*msg);
880 // SetArgPointee<N>(proto2_buffer) makes a copy of
881 // proto2_buffer s.t. the action works even when the original
882 // proto2_buffer has died. We ensure this behavior by deleting msg
883 // before using the action.
884 delete msg;
885
886 FooMessage dest;
887 dest.set_int_field(0);
888 a.Perform(make_tuple(true, &dest));
889 EXPECT_EQ(2, dest.int_field());
890 EXPECT_EQ("hi", dest.string_field());
891}
892
893// Tests that SetArgPointee<N>(proto2_buffer) sets the
894// proto2::Message variable pointed to by the N-th (0-based) argument
895// to proto2_buffer.
896TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
897 using testing::internal::FooMessage;
898 FooMessage* const msg = new FooMessage;
899 msg->set_int_field(2);
900 msg->set_string_field("hi");
901 FooMessage orig_msg;
902 orig_msg.CopyFrom(*msg);
903
904 Action<void(bool, ::proto2::Message*)> a = SetArgPointee<1>(*msg);
905 // SetArgPointee<N>(proto2_buffer) makes a copy of
906 // proto2_buffer s.t. the action works even when the original
907 // proto2_buffer has died. We ensure this behavior by deleting msg
908 // before using the action.
909 delete msg;
910
911 FooMessage dest;
912 dest.set_int_field(0);
913 ::proto2::Message* const dest_base = &dest;
914 a.Perform(make_tuple(true, dest_base));
915 EXPECT_EQ(2, dest.int_field());
916 EXPECT_EQ("hi", dest.string_field());
917}
918
919#endif // GTEST_HAS_PROTOBUF_
920
shiqiane35fdd92008-12-10 05:08:54 +0000921// Tests that SetArgumentPointee<N>(v) sets the variable pointed to by
922// the N-th (0-based) argument to v.
923TEST(SetArgumentPointeeTest, SetsTheNthPointee) {
924 typedef void MyFunction(bool, int*, char*);
925 Action<MyFunction> a = SetArgumentPointee<1>(2);
926
927 int n = 0;
928 char ch = '\0';
929 a.Perform(make_tuple(true, &n, &ch));
930 EXPECT_EQ(2, n);
931 EXPECT_EQ('\0', ch);
932
933 a = SetArgumentPointee<2>('a');
934 n = 0;
935 ch = '\0';
936 a.Perform(make_tuple(true, &n, &ch));
937 EXPECT_EQ(0, n);
938 EXPECT_EQ('a', ch);
939}
940
zhanyong.wan02f71062010-05-10 17:14:29 +0000941#if GTEST_HAS_PROTOBUF_
shiqiane35fdd92008-12-10 05:08:54 +0000942
zhanyong.wanc6a41232009-05-13 23:38:40 +0000943// Tests that SetArgumentPointee<N>(proto_buffer) sets the v1 protobuf
944// variable pointed to by the N-th (0-based) argument to proto_buffer.
shiqiane35fdd92008-12-10 05:08:54 +0000945TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
shiqiane35fdd92008-12-10 05:08:54 +0000946 TestMessage* const msg = new TestMessage;
947 msg->set_member("yes");
948 TestMessage orig_msg;
949 orig_msg.CopyFrom(*msg);
950
zhanyong.wanc6a41232009-05-13 23:38:40 +0000951 Action<void(bool, TestMessage*)> a = SetArgumentPointee<1>(*msg);
shiqiane35fdd92008-12-10 05:08:54 +0000952 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
953 // s.t. the action works even when the original proto_buffer has
954 // died. We ensure this behavior by deleting msg before using the
955 // action.
956 delete msg;
957
958 TestMessage dest;
959 EXPECT_FALSE(orig_msg.Equals(dest));
960 a.Perform(make_tuple(true, &dest));
961 EXPECT_TRUE(orig_msg.Equals(dest));
962}
963
zhanyong.wanc6a41232009-05-13 23:38:40 +0000964// Tests that SetArgumentPointee<N>(proto_buffer) sets the
965// ::ProtocolMessage variable pointed to by the N-th (0-based)
966// argument to proto_buffer.
967TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
968 TestMessage* const msg = new TestMessage;
969 msg->set_member("yes");
970 TestMessage orig_msg;
971 orig_msg.CopyFrom(*msg);
972
973 Action<void(bool, ::ProtocolMessage*)> a = SetArgumentPointee<1>(*msg);
974 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
975 // s.t. the action works even when the original proto_buffer has
976 // died. We ensure this behavior by deleting msg before using the
977 // action.
978 delete msg;
979
980 TestMessage dest;
981 ::ProtocolMessage* const dest_base = &dest;
982 EXPECT_FALSE(orig_msg.Equals(dest));
983 a.Perform(make_tuple(true, dest_base));
984 EXPECT_TRUE(orig_msg.Equals(dest));
985}
986
987// Tests that SetArgumentPointee<N>(proto2_buffer) sets the v2
988// protobuf variable pointed to by the N-th (0-based) argument to
989// proto2_buffer.
shiqiane35fdd92008-12-10 05:08:54 +0000990TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
991 using testing::internal::FooMessage;
shiqiane35fdd92008-12-10 05:08:54 +0000992 FooMessage* const msg = new FooMessage;
993 msg->set_int_field(2);
994 msg->set_string_field("hi");
995 FooMessage orig_msg;
996 orig_msg.CopyFrom(*msg);
997
zhanyong.wanc6a41232009-05-13 23:38:40 +0000998 Action<void(bool, FooMessage*)> a = SetArgumentPointee<1>(*msg);
shiqiane35fdd92008-12-10 05:08:54 +0000999 // SetArgumentPointee<N>(proto2_buffer) makes a copy of
1000 // proto2_buffer s.t. the action works even when the original
1001 // proto2_buffer has died. We ensure this behavior by deleting msg
1002 // before using the action.
1003 delete msg;
1004
1005 FooMessage dest;
1006 dest.set_int_field(0);
1007 a.Perform(make_tuple(true, &dest));
1008 EXPECT_EQ(2, dest.int_field());
1009 EXPECT_EQ("hi", dest.string_field());
1010}
1011
zhanyong.wanc6a41232009-05-13 23:38:40 +00001012// Tests that SetArgumentPointee<N>(proto2_buffer) sets the
1013// proto2::Message variable pointed to by the N-th (0-based) argument
1014// to proto2_buffer.
1015TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
1016 using testing::internal::FooMessage;
1017 FooMessage* const msg = new FooMessage;
1018 msg->set_int_field(2);
1019 msg->set_string_field("hi");
1020 FooMessage orig_msg;
1021 orig_msg.CopyFrom(*msg);
1022
1023 Action<void(bool, ::proto2::Message*)> a = SetArgumentPointee<1>(*msg);
1024 // SetArgumentPointee<N>(proto2_buffer) makes a copy of
1025 // proto2_buffer s.t. the action works even when the original
1026 // proto2_buffer has died. We ensure this behavior by deleting msg
1027 // before using the action.
1028 delete msg;
1029
1030 FooMessage dest;
1031 dest.set_int_field(0);
1032 ::proto2::Message* const dest_base = &dest;
1033 a.Perform(make_tuple(true, dest_base));
1034 EXPECT_EQ(2, dest.int_field());
1035 EXPECT_EQ("hi", dest.string_field());
1036}
1037
zhanyong.wan02f71062010-05-10 17:14:29 +00001038#endif // GTEST_HAS_PROTOBUF_
shiqiane35fdd92008-12-10 05:08:54 +00001039
shiqiane35fdd92008-12-10 05:08:54 +00001040// Sample functions and functors for testing Invoke() and etc.
1041int Nullary() { return 1; }
1042
1043class NullaryFunctor {
1044 public:
1045 int operator()() { return 2; }
1046};
1047
1048bool g_done = false;
1049void VoidNullary() { g_done = true; }
1050
1051class VoidNullaryFunctor {
1052 public:
1053 void operator()() { g_done = true; }
1054};
1055
shiqiane35fdd92008-12-10 05:08:54 +00001056class Foo {
1057 public:
1058 Foo() : value_(123) {}
1059
1060 int Nullary() const { return value_; }
zhanyong.wan29be9232013-03-01 06:53:35 +00001061
shiqiane35fdd92008-12-10 05:08:54 +00001062 private:
1063 int value_;
1064};
1065
1066// Tests InvokeWithoutArgs(function).
1067TEST(InvokeWithoutArgsTest, Function) {
1068 // As an action that takes one argument.
1069 Action<int(int)> a = InvokeWithoutArgs(Nullary); // NOLINT
1070 EXPECT_EQ(1, a.Perform(make_tuple(2)));
1071
1072 // As an action that takes two arguments.
zhanyong.wan32de5f52009-12-23 00:13:23 +00001073 Action<int(int, double)> a2 = InvokeWithoutArgs(Nullary); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00001074 EXPECT_EQ(1, a2.Perform(make_tuple(2, 3.5)));
1075
1076 // As an action that returns void.
1077 Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary); // NOLINT
1078 g_done = false;
1079 a3.Perform(make_tuple(1));
1080 EXPECT_TRUE(g_done);
1081}
1082
1083// Tests InvokeWithoutArgs(functor).
1084TEST(InvokeWithoutArgsTest, Functor) {
1085 // As an action that takes no argument.
1086 Action<int()> a = InvokeWithoutArgs(NullaryFunctor()); // NOLINT
1087 EXPECT_EQ(2, a.Perform(make_tuple()));
1088
1089 // As an action that takes three arguments.
zhanyong.wan32de5f52009-12-23 00:13:23 +00001090 Action<int(int, double, char)> a2 = // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00001091 InvokeWithoutArgs(NullaryFunctor());
1092 EXPECT_EQ(2, a2.Perform(make_tuple(3, 3.5, 'a')));
1093
1094 // As an action that returns void.
1095 Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor());
1096 g_done = false;
1097 a3.Perform(make_tuple());
1098 EXPECT_TRUE(g_done);
1099}
1100
1101// Tests InvokeWithoutArgs(obj_ptr, method).
1102TEST(InvokeWithoutArgsTest, Method) {
1103 Foo foo;
1104 Action<int(bool, char)> a = // NOLINT
1105 InvokeWithoutArgs(&foo, &Foo::Nullary);
1106 EXPECT_EQ(123, a.Perform(make_tuple(true, 'a')));
1107}
1108
1109// Tests using IgnoreResult() on a polymorphic action.
1110TEST(IgnoreResultTest, PolymorphicAction) {
1111 Action<void(int)> a = IgnoreResult(Return(5)); // NOLINT
1112 a.Perform(make_tuple(1));
1113}
1114
1115// Tests using IgnoreResult() on a monomorphic action.
1116
1117int ReturnOne() {
1118 g_done = true;
1119 return 1;
1120}
1121
1122TEST(IgnoreResultTest, MonomorphicAction) {
1123 g_done = false;
1124 Action<void()> a = IgnoreResult(Invoke(ReturnOne));
1125 a.Perform(make_tuple());
1126 EXPECT_TRUE(g_done);
1127}
1128
1129// Tests using IgnoreResult() on an action that returns a class type.
1130
zhanyong.wan32de5f52009-12-23 00:13:23 +00001131MyClass ReturnMyClass(double /* x */) {
shiqiane35fdd92008-12-10 05:08:54 +00001132 g_done = true;
1133 return MyClass();
1134}
1135
1136TEST(IgnoreResultTest, ActionReturningClass) {
1137 g_done = false;
1138 Action<void(int)> a = IgnoreResult(Invoke(ReturnMyClass)); // NOLINT
1139 a.Perform(make_tuple(2));
1140 EXPECT_TRUE(g_done);
1141}
1142
1143TEST(AssignTest, Int) {
1144 int x = 0;
1145 Action<void(int)> a = Assign(&x, 5);
1146 a.Perform(make_tuple(0));
1147 EXPECT_EQ(5, x);
1148}
1149
1150TEST(AssignTest, String) {
1151 ::std::string x;
1152 Action<void(void)> a = Assign(&x, "Hello, world");
1153 a.Perform(make_tuple());
1154 EXPECT_EQ("Hello, world", x);
1155}
1156
1157TEST(AssignTest, CompatibleTypes) {
1158 double x = 0;
1159 Action<void(int)> a = Assign(&x, 5);
1160 a.Perform(make_tuple(0));
1161 EXPECT_DOUBLE_EQ(5, x);
1162}
1163
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001164#if !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001165
shiqiane35fdd92008-12-10 05:08:54 +00001166class SetErrnoAndReturnTest : public testing::Test {
1167 protected:
1168 virtual void SetUp() { errno = 0; }
1169 virtual void TearDown() { errno = 0; }
1170};
1171
1172TEST_F(SetErrnoAndReturnTest, Int) {
1173 Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5);
1174 EXPECT_EQ(-5, a.Perform(make_tuple()));
1175 EXPECT_EQ(ENOTTY, errno);
1176}
1177
1178TEST_F(SetErrnoAndReturnTest, Ptr) {
1179 int x;
1180 Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x);
1181 EXPECT_EQ(&x, a.Perform(make_tuple()));
1182 EXPECT_EQ(ENOTTY, errno);
1183}
1184
1185TEST_F(SetErrnoAndReturnTest, CompatibleTypes) {
1186 Action<double()> a = SetErrnoAndReturn(EINVAL, 5);
1187 EXPECT_DOUBLE_EQ(5.0, a.Perform(make_tuple()));
1188 EXPECT_EQ(EINVAL, errno);
1189}
1190
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001191#endif // !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001192
zhanyong.wana18423e2009-07-22 23:58:19 +00001193// Tests ByRef().
1194
1195// Tests that ReferenceWrapper<T> is copyable.
1196TEST(ByRefTest, IsCopyable) {
1197 const std::string s1 = "Hi";
1198 const std::string s2 = "Hello";
1199
jgm79a367e2012-04-10 16:02:11 +00001200 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper =
1201 ByRef(s1);
zhanyong.wana18423e2009-07-22 23:58:19 +00001202 const std::string& r1 = ref_wrapper;
1203 EXPECT_EQ(&s1, &r1);
1204
1205 // Assigns a new value to ref_wrapper.
1206 ref_wrapper = ByRef(s2);
1207 const std::string& r2 = ref_wrapper;
1208 EXPECT_EQ(&s2, &r2);
1209
jgm79a367e2012-04-10 16:02:11 +00001210 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper1 =
1211 ByRef(s1);
zhanyong.wana18423e2009-07-22 23:58:19 +00001212 // Copies ref_wrapper1 to ref_wrapper.
1213 ref_wrapper = ref_wrapper1;
1214 const std::string& r3 = ref_wrapper;
1215 EXPECT_EQ(&s1, &r3);
1216}
1217
1218// Tests using ByRef() on a const value.
1219TEST(ByRefTest, ConstValue) {
1220 const int n = 0;
1221 // int& ref = ByRef(n); // This shouldn't compile - we have a
1222 // negative compilation test to catch it.
1223 const int& const_ref = ByRef(n);
1224 EXPECT_EQ(&n, &const_ref);
1225}
1226
1227// Tests using ByRef() on a non-const value.
1228TEST(ByRefTest, NonConstValue) {
1229 int n = 0;
1230
1231 // ByRef(n) can be used as either an int&,
1232 int& ref = ByRef(n);
1233 EXPECT_EQ(&n, &ref);
1234
1235 // or a const int&.
1236 const int& const_ref = ByRef(n);
1237 EXPECT_EQ(&n, &const_ref);
1238}
1239
1240// Tests explicitly specifying the type when using ByRef().
1241TEST(ByRefTest, ExplicitType) {
1242 int n = 0;
1243 const int& r1 = ByRef<const int>(n);
1244 EXPECT_EQ(&n, &r1);
1245
1246 // ByRef<char>(n); // This shouldn't compile - we have a negative
1247 // compilation test to catch it.
1248
1249 Derived d;
1250 Derived& r2 = ByRef<Derived>(d);
1251 EXPECT_EQ(&d, &r2);
1252
1253 const Derived& r3 = ByRef<const Derived>(d);
1254 EXPECT_EQ(&d, &r3);
1255
1256 Base& r4 = ByRef<Base>(d);
1257 EXPECT_EQ(&d, &r4);
1258
1259 const Base& r5 = ByRef<const Base>(d);
1260 EXPECT_EQ(&d, &r5);
1261
1262 // The following shouldn't compile - we have a negative compilation
1263 // test for it.
1264 //
1265 // Base b;
1266 // ByRef<Derived>(b);
1267}
1268
1269// Tests that Google Mock prints expression ByRef(x) as a reference to x.
1270TEST(ByRefTest, PrintsCorrectly) {
1271 int n = 42;
1272 ::std::stringstream expected, actual;
1273 testing::internal::UniversalPrinter<const int&>::Print(n, &expected);
1274 testing::internal::UniversalPrint(ByRef(n), &actual);
1275 EXPECT_EQ(expected.str(), actual.str());
1276}
1277
kosak5b9cbbb2014-11-17 00:28:55 +00001278#if GTEST_HAS_STD_UNIQUE_PTR_
kosakb5c81092014-01-29 06:41:44 +00001279
1280std::unique_ptr<int> UniquePtrSource() {
1281 return std::unique_ptr<int>(new int(19));
1282}
1283
1284std::vector<std::unique_ptr<int>> VectorUniquePtrSource() {
1285 std::vector<std::unique_ptr<int>> out;
1286 out.emplace_back(new int(7));
1287 return out;
1288}
1289
kosak3d1c78b2014-11-17 00:56:52 +00001290TEST(MockMethodTest, CanReturnMoveOnlyValue_Return) {
1291 MockClass mock;
1292 std::unique_ptr<int> i(new int(19));
1293 EXPECT_CALL(mock, MakeUnique()).WillOnce(Return(ByMove(std::move(i))));
1294 EXPECT_CALL(mock, MakeVectorUnique())
1295 .WillOnce(Return(ByMove(VectorUniquePtrSource())));
1296 Derived* d = new Derived;
1297 EXPECT_CALL(mock, MakeUniqueBase())
1298 .WillOnce(Return(ByMove(std::unique_ptr<Derived>(d))));
1299
1300 std::unique_ptr<int> result1 = mock.MakeUnique();
1301 EXPECT_EQ(19, *result1);
1302
1303 std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
kosak389bad62014-11-17 01:08:51 +00001304 EXPECT_EQ(1u, vresult.size());
kosak3d1c78b2014-11-17 00:56:52 +00001305 EXPECT_NE(nullptr, vresult[0]);
1306 EXPECT_EQ(7, *vresult[0]);
1307
1308 std::unique_ptr<Base> result2 = mock.MakeUniqueBase();
1309 EXPECT_EQ(d, result2.get());
1310}
1311
1312TEST(MockMethodTest, CanReturnMoveOnlyValue_DoAllReturn) {
1313 testing::MockFunction<void()> mock_function;
1314 MockClass mock;
1315 std::unique_ptr<int> i(new int(19));
1316 EXPECT_CALL(mock_function, Call());
1317 EXPECT_CALL(mock, MakeUnique()).WillOnce(DoAll(
1318 InvokeWithoutArgs(&mock_function, &testing::MockFunction<void()>::Call),
1319 Return(ByMove(std::move(i)))));
1320
1321 std::unique_ptr<int> result1 = mock.MakeUnique();
1322 EXPECT_EQ(19, *result1);
1323}
1324
1325TEST(MockMethodTest, CanReturnMoveOnlyValue_Invoke) {
kosakb5c81092014-01-29 06:41:44 +00001326 MockClass mock;
1327
1328 // Check default value
1329 DefaultValue<std::unique_ptr<int>>::SetFactory([] {
1330 return std::unique_ptr<int>(new int(42));
1331 });
1332 EXPECT_EQ(42, *mock.MakeUnique());
1333
kosak3d1c78b2014-11-17 00:56:52 +00001334 EXPECT_CALL(mock, MakeUnique()).WillRepeatedly(Invoke(UniquePtrSource));
kosakb5c81092014-01-29 06:41:44 +00001335 EXPECT_CALL(mock, MakeVectorUnique())
1336 .WillRepeatedly(Invoke(VectorUniquePtrSource));
1337 std::unique_ptr<int> result1 = mock.MakeUnique();
1338 EXPECT_EQ(19, *result1);
1339 std::unique_ptr<int> result2 = mock.MakeUnique();
1340 EXPECT_EQ(19, *result2);
1341 EXPECT_NE(result1, result2);
1342
1343 std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
kosak389bad62014-11-17 01:08:51 +00001344 EXPECT_EQ(1u, vresult.size());
kosakb5c81092014-01-29 06:41:44 +00001345 EXPECT_NE(nullptr, vresult[0]);
1346 EXPECT_EQ(7, *vresult[0]);
1347}
1348
kosak5b9cbbb2014-11-17 00:28:55 +00001349#endif // GTEST_HAS_STD_UNIQUE_PTR_
kosakb5c81092014-01-29 06:41:44 +00001350
shiqiane35fdd92008-12-10 05:08:54 +00001351} // Unnamed namespace