blob: 8089a81d45a84374258b0983ec419b833b7b8401 [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
607// Tests that ReturnRef(v) works for reference types.
608TEST(ReturnRefTest, WorksForReference) {
609 const int n = 0;
610 const Action<const int&(bool)> ret = ReturnRef(n); // NOLINT
611
612 EXPECT_EQ(&n, &ret.Perform(make_tuple(true)));
613}
614
615// Tests that ReturnRef(v) is covariant.
616TEST(ReturnRefTest, IsCovariant) {
617 Base base;
618 Derived derived;
619 Action<Base&()> a = ReturnRef(base);
620 EXPECT_EQ(&base, &a.Perform(make_tuple()));
621
622 a = ReturnRef(derived);
623 EXPECT_EQ(&derived, &a.Perform(make_tuple()));
624}
625
zhanyong.wane3bd0982010-07-03 00:16:42 +0000626// Tests that ReturnRefOfCopy(v) works for reference types.
627TEST(ReturnRefOfCopyTest, WorksForReference) {
628 int n = 42;
629 const Action<const int&()> ret = ReturnRefOfCopy(n);
630
631 EXPECT_NE(&n, &ret.Perform(make_tuple()));
632 EXPECT_EQ(42, ret.Perform(make_tuple()));
633
634 n = 43;
635 EXPECT_NE(&n, &ret.Perform(make_tuple()));
636 EXPECT_EQ(42, ret.Perform(make_tuple()));
637}
638
639// Tests that ReturnRefOfCopy(v) is covariant.
640TEST(ReturnRefOfCopyTest, IsCovariant) {
641 Base base;
642 Derived derived;
643 Action<Base&()> a = ReturnRefOfCopy(base);
644 EXPECT_NE(&base, &a.Perform(make_tuple()));
645
646 a = ReturnRefOfCopy(derived);
647 EXPECT_NE(&derived, &a.Perform(make_tuple()));
648}
649
shiqiane35fdd92008-12-10 05:08:54 +0000650// Tests that DoDefault() does the default action for the mock method.
651
652class MyClass {};
653
654class MockClass {
655 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000656 MockClass() {}
657
shiqiane35fdd92008-12-10 05:08:54 +0000658 MOCK_METHOD1(IntFunc, int(bool flag)); // NOLINT
659 MOCK_METHOD0(Foo, MyClass());
kosak5b9cbbb2014-11-17 00:28:55 +0000660#if GTEST_HAS_STD_UNIQUE_PTR_
kosakb5c81092014-01-29 06:41:44 +0000661 MOCK_METHOD0(MakeUnique, std::unique_ptr<int>());
kosak3d1c78b2014-11-17 00:56:52 +0000662 MOCK_METHOD0(MakeUniqueBase, std::unique_ptr<Base>());
kosakb5c81092014-01-29 06:41:44 +0000663 MOCK_METHOD0(MakeVectorUnique, std::vector<std::unique_ptr<int>>());
664#endif
zhanyong.wan32de5f52009-12-23 00:13:23 +0000665
666 private:
667 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockClass);
shiqiane35fdd92008-12-10 05:08:54 +0000668};
669
670// Tests that DoDefault() returns the built-in default value for the
671// return type by default.
672TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) {
673 MockClass mock;
674 EXPECT_CALL(mock, IntFunc(_))
675 .WillOnce(DoDefault());
676 EXPECT_EQ(0, mock.IntFunc(true));
677}
678
zhanyong.wanedd4ab42013-02-28 22:58:51 +0000679// Tests that DoDefault() throws (when exceptions are enabled) or aborts
680// the process when there is no built-in default value for the return type.
shiqiane35fdd92008-12-10 05:08:54 +0000681TEST(DoDefaultDeathTest, DiesForUnknowType) {
682 MockClass mock;
683 EXPECT_CALL(mock, Foo())
684 .WillRepeatedly(DoDefault());
zhanyong.wanedd4ab42013-02-28 22:58:51 +0000685#if GTEST_HAS_EXCEPTIONS
686 EXPECT_ANY_THROW(mock.Foo());
687#else
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000688 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000689 mock.Foo();
690 }, "");
zhanyong.wanedd4ab42013-02-28 22:58:51 +0000691#endif
shiqiane35fdd92008-12-10 05:08:54 +0000692}
693
694// Tests that using DoDefault() inside a composite action leads to a
695// run-time error.
696
zhanyong.wan32de5f52009-12-23 00:13:23 +0000697void VoidFunc(bool /* flag */) {}
shiqiane35fdd92008-12-10 05:08:54 +0000698
699TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
700 MockClass mock;
701 EXPECT_CALL(mock, IntFunc(_))
702 .WillRepeatedly(DoAll(Invoke(VoidFunc),
703 DoDefault()));
704
705 // Ideally we should verify the error message as well. Sadly,
706 // EXPECT_DEATH() can only capture stderr, while Google Mock's
707 // errors are printed on stdout. Therefore we have to settle for
708 // not verifying the message.
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000709 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000710 mock.IntFunc(true);
711 }, "");
712}
713
shiqiane35fdd92008-12-10 05:08:54 +0000714// Tests that DoDefault() returns the default value set by
715// DefaultValue<T>::Set() when it's not overriden by an ON_CALL().
716TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) {
717 DefaultValue<int>::Set(1);
718 MockClass mock;
719 EXPECT_CALL(mock, IntFunc(_))
720 .WillOnce(DoDefault());
721 EXPECT_EQ(1, mock.IntFunc(false));
722 DefaultValue<int>::Clear();
723}
724
725// Tests that DoDefault() does the action specified by ON_CALL().
726TEST(DoDefaultTest, DoesWhatOnCallSpecifies) {
727 MockClass mock;
728 ON_CALL(mock, IntFunc(_))
729 .WillByDefault(Return(2));
730 EXPECT_CALL(mock, IntFunc(_))
731 .WillOnce(DoDefault());
732 EXPECT_EQ(2, mock.IntFunc(false));
733}
734
735// Tests that using DoDefault() in ON_CALL() leads to a run-time failure.
736TEST(DoDefaultTest, CannotBeUsedInOnCall) {
737 MockClass mock;
738 EXPECT_NONFATAL_FAILURE({ // NOLINT
739 ON_CALL(mock, IntFunc(_))
740 .WillByDefault(DoDefault());
741 }, "DoDefault() cannot be used in ON_CALL()");
742}
743
zhanyong.wan59214832010-10-05 05:58:51 +0000744// Tests that SetArgPointee<N>(v) sets the variable pointed to by
745// the N-th (0-based) argument to v.
746TEST(SetArgPointeeTest, SetsTheNthPointee) {
747 typedef void MyFunction(bool, int*, char*);
748 Action<MyFunction> a = SetArgPointee<1>(2);
749
750 int n = 0;
751 char ch = '\0';
752 a.Perform(make_tuple(true, &n, &ch));
753 EXPECT_EQ(2, n);
754 EXPECT_EQ('\0', ch);
755
756 a = SetArgPointee<2>('a');
757 n = 0;
758 ch = '\0';
759 a.Perform(make_tuple(true, &n, &ch));
760 EXPECT_EQ(0, n);
761 EXPECT_EQ('a', ch);
762}
763
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000764#if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN)
zhanyong.wana684b5a2010-12-02 23:30:50 +0000765// Tests that SetArgPointee<N>() accepts a string literal.
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000766// GCC prior to v4.0 and the Symbian compiler do not support this.
zhanyong.wana684b5a2010-12-02 23:30:50 +0000767TEST(SetArgPointeeTest, AcceptsStringLiteral) {
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000768 typedef void MyFunction(std::string*, const char**);
769 Action<MyFunction> a = SetArgPointee<0>("hi");
zhanyong.wana684b5a2010-12-02 23:30:50 +0000770 std::string str;
771 const char* ptr = NULL;
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000772 a.Perform(make_tuple(&str, &ptr));
zhanyong.wana684b5a2010-12-02 23:30:50 +0000773 EXPECT_EQ("hi", str);
774 EXPECT_TRUE(ptr == NULL);
775
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000776 a = SetArgPointee<1>("world");
zhanyong.wana684b5a2010-12-02 23:30:50 +0000777 str = "";
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000778 a.Perform(make_tuple(&str, &ptr));
zhanyong.wana684b5a2010-12-02 23:30:50 +0000779 EXPECT_EQ("", str);
780 EXPECT_STREQ("world", ptr);
781}
782
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000783TEST(SetArgPointeeTest, AcceptsWideStringLiteral) {
784 typedef void MyFunction(const wchar_t**);
785 Action<MyFunction> a = SetArgPointee<0>(L"world");
786 const wchar_t* ptr = NULL;
787 a.Perform(make_tuple(&ptr));
788 EXPECT_STREQ(L"world", ptr);
789
790# if GTEST_HAS_STD_WSTRING
791
792 typedef void MyStringFunction(std::wstring*);
793 Action<MyStringFunction> a2 = SetArgPointee<0>(L"world");
794 std::wstring str = L"";
795 a2.Perform(make_tuple(&str));
796 EXPECT_EQ(L"world", str);
797
798# endif
799}
800#endif
801
zhanyong.wana684b5a2010-12-02 23:30:50 +0000802// Tests that SetArgPointee<N>() accepts a char pointer.
803TEST(SetArgPointeeTest, AcceptsCharPointer) {
804 typedef void MyFunction(bool, std::string*, const char**);
805 const char* const hi = "hi";
806 Action<MyFunction> a = SetArgPointee<1>(hi);
807 std::string str;
808 const char* ptr = NULL;
809 a.Perform(make_tuple(true, &str, &ptr));
810 EXPECT_EQ("hi", str);
811 EXPECT_TRUE(ptr == NULL);
812
813 char world_array[] = "world";
814 char* const world = world_array;
815 a = SetArgPointee<2>(world);
816 str = "";
817 a.Perform(make_tuple(true, &str, &ptr));
818 EXPECT_EQ("", str);
819 EXPECT_EQ(world, ptr);
820}
821
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000822TEST(SetArgPointeeTest, AcceptsWideCharPointer) {
823 typedef void MyFunction(bool, const wchar_t**);
824 const wchar_t* const hi = L"hi";
825 Action<MyFunction> a = SetArgPointee<1>(hi);
826 const wchar_t* ptr = NULL;
827 a.Perform(make_tuple(true, &ptr));
828 EXPECT_EQ(hi, ptr);
829
830# if GTEST_HAS_STD_WSTRING
831
832 typedef void MyStringFunction(bool, std::wstring*);
833 wchar_t world_array[] = L"world";
834 wchar_t* const world = world_array;
835 Action<MyStringFunction> a2 = SetArgPointee<1>(world);
836 std::wstring str;
837 a2.Perform(make_tuple(true, &str));
838 EXPECT_EQ(world_array, str);
839# endif
840}
841
zhanyong.wan59214832010-10-05 05:58:51 +0000842#if GTEST_HAS_PROTOBUF_
843
844// Tests that SetArgPointee<N>(proto_buffer) sets the v1 protobuf
845// variable pointed to by the N-th (0-based) argument to proto_buffer.
846TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
847 TestMessage* const msg = new TestMessage;
848 msg->set_member("yes");
849 TestMessage orig_msg;
850 orig_msg.CopyFrom(*msg);
851
852 Action<void(bool, TestMessage*)> a = SetArgPointee<1>(*msg);
853 // SetArgPointee<N>(proto_buffer) makes a copy of proto_buffer
854 // s.t. the action works even when the original proto_buffer has
855 // died. We ensure this behavior by deleting msg before using the
856 // action.
857 delete msg;
858
859 TestMessage dest;
860 EXPECT_FALSE(orig_msg.Equals(dest));
861 a.Perform(make_tuple(true, &dest));
862 EXPECT_TRUE(orig_msg.Equals(dest));
863}
864
865// Tests that SetArgPointee<N>(proto_buffer) sets the
866// ::ProtocolMessage variable pointed to by the N-th (0-based)
867// argument to proto_buffer.
868TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
869 TestMessage* const msg = new TestMessage;
870 msg->set_member("yes");
871 TestMessage orig_msg;
872 orig_msg.CopyFrom(*msg);
873
874 Action<void(bool, ::ProtocolMessage*)> a = SetArgPointee<1>(*msg);
875 // SetArgPointee<N>(proto_buffer) makes a copy of proto_buffer
876 // s.t. the action works even when the original proto_buffer has
877 // died. We ensure this behavior by deleting msg before using the
878 // action.
879 delete msg;
880
881 TestMessage dest;
882 ::ProtocolMessage* const dest_base = &dest;
883 EXPECT_FALSE(orig_msg.Equals(dest));
884 a.Perform(make_tuple(true, dest_base));
885 EXPECT_TRUE(orig_msg.Equals(dest));
886}
887
888// Tests that SetArgPointee<N>(proto2_buffer) sets the v2
889// protobuf variable pointed to by the N-th (0-based) argument to
890// proto2_buffer.
891TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
892 using testing::internal::FooMessage;
893 FooMessage* const msg = new FooMessage;
894 msg->set_int_field(2);
895 msg->set_string_field("hi");
896 FooMessage orig_msg;
897 orig_msg.CopyFrom(*msg);
898
899 Action<void(bool, FooMessage*)> a = SetArgPointee<1>(*msg);
900 // SetArgPointee<N>(proto2_buffer) makes a copy of
901 // proto2_buffer s.t. the action works even when the original
902 // proto2_buffer has died. We ensure this behavior by deleting msg
903 // before using the action.
904 delete msg;
905
906 FooMessage dest;
907 dest.set_int_field(0);
908 a.Perform(make_tuple(true, &dest));
909 EXPECT_EQ(2, dest.int_field());
910 EXPECT_EQ("hi", dest.string_field());
911}
912
913// Tests that SetArgPointee<N>(proto2_buffer) sets the
914// proto2::Message variable pointed to by the N-th (0-based) argument
915// to proto2_buffer.
916TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
917 using testing::internal::FooMessage;
918 FooMessage* const msg = new FooMessage;
919 msg->set_int_field(2);
920 msg->set_string_field("hi");
921 FooMessage orig_msg;
922 orig_msg.CopyFrom(*msg);
923
924 Action<void(bool, ::proto2::Message*)> a = SetArgPointee<1>(*msg);
925 // SetArgPointee<N>(proto2_buffer) makes a copy of
926 // proto2_buffer s.t. the action works even when the original
927 // proto2_buffer has died. We ensure this behavior by deleting msg
928 // before using the action.
929 delete msg;
930
931 FooMessage dest;
932 dest.set_int_field(0);
933 ::proto2::Message* const dest_base = &dest;
934 a.Perform(make_tuple(true, dest_base));
935 EXPECT_EQ(2, dest.int_field());
936 EXPECT_EQ("hi", dest.string_field());
937}
938
939#endif // GTEST_HAS_PROTOBUF_
940
shiqiane35fdd92008-12-10 05:08:54 +0000941// Tests that SetArgumentPointee<N>(v) sets the variable pointed to by
942// the N-th (0-based) argument to v.
943TEST(SetArgumentPointeeTest, SetsTheNthPointee) {
944 typedef void MyFunction(bool, int*, char*);
945 Action<MyFunction> a = SetArgumentPointee<1>(2);
946
947 int n = 0;
948 char ch = '\0';
949 a.Perform(make_tuple(true, &n, &ch));
950 EXPECT_EQ(2, n);
951 EXPECT_EQ('\0', ch);
952
953 a = SetArgumentPointee<2>('a');
954 n = 0;
955 ch = '\0';
956 a.Perform(make_tuple(true, &n, &ch));
957 EXPECT_EQ(0, n);
958 EXPECT_EQ('a', ch);
959}
960
zhanyong.wan02f71062010-05-10 17:14:29 +0000961#if GTEST_HAS_PROTOBUF_
shiqiane35fdd92008-12-10 05:08:54 +0000962
zhanyong.wanc6a41232009-05-13 23:38:40 +0000963// Tests that SetArgumentPointee<N>(proto_buffer) sets the v1 protobuf
964// variable pointed to by the N-th (0-based) argument to proto_buffer.
shiqiane35fdd92008-12-10 05:08:54 +0000965TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
shiqiane35fdd92008-12-10 05:08:54 +0000966 TestMessage* const msg = new TestMessage;
967 msg->set_member("yes");
968 TestMessage orig_msg;
969 orig_msg.CopyFrom(*msg);
970
zhanyong.wanc6a41232009-05-13 23:38:40 +0000971 Action<void(bool, TestMessage*)> a = SetArgumentPointee<1>(*msg);
shiqiane35fdd92008-12-10 05:08:54 +0000972 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
973 // s.t. the action works even when the original proto_buffer has
974 // died. We ensure this behavior by deleting msg before using the
975 // action.
976 delete msg;
977
978 TestMessage dest;
979 EXPECT_FALSE(orig_msg.Equals(dest));
980 a.Perform(make_tuple(true, &dest));
981 EXPECT_TRUE(orig_msg.Equals(dest));
982}
983
zhanyong.wanc6a41232009-05-13 23:38:40 +0000984// Tests that SetArgumentPointee<N>(proto_buffer) sets the
985// ::ProtocolMessage variable pointed to by the N-th (0-based)
986// argument to proto_buffer.
987TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
988 TestMessage* const msg = new TestMessage;
989 msg->set_member("yes");
990 TestMessage orig_msg;
991 orig_msg.CopyFrom(*msg);
992
993 Action<void(bool, ::ProtocolMessage*)> a = SetArgumentPointee<1>(*msg);
994 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
995 // s.t. the action works even when the original proto_buffer has
996 // died. We ensure this behavior by deleting msg before using the
997 // action.
998 delete msg;
999
1000 TestMessage dest;
1001 ::ProtocolMessage* const dest_base = &dest;
1002 EXPECT_FALSE(orig_msg.Equals(dest));
1003 a.Perform(make_tuple(true, dest_base));
1004 EXPECT_TRUE(orig_msg.Equals(dest));
1005}
1006
1007// Tests that SetArgumentPointee<N>(proto2_buffer) sets the v2
1008// protobuf variable pointed to by the N-th (0-based) argument to
1009// proto2_buffer.
shiqiane35fdd92008-12-10 05:08:54 +00001010TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
1011 using testing::internal::FooMessage;
shiqiane35fdd92008-12-10 05:08:54 +00001012 FooMessage* const msg = new FooMessage;
1013 msg->set_int_field(2);
1014 msg->set_string_field("hi");
1015 FooMessage orig_msg;
1016 orig_msg.CopyFrom(*msg);
1017
zhanyong.wanc6a41232009-05-13 23:38:40 +00001018 Action<void(bool, FooMessage*)> a = SetArgumentPointee<1>(*msg);
shiqiane35fdd92008-12-10 05:08:54 +00001019 // SetArgumentPointee<N>(proto2_buffer) makes a copy of
1020 // proto2_buffer s.t. the action works even when the original
1021 // proto2_buffer has died. We ensure this behavior by deleting msg
1022 // before using the action.
1023 delete msg;
1024
1025 FooMessage dest;
1026 dest.set_int_field(0);
1027 a.Perform(make_tuple(true, &dest));
1028 EXPECT_EQ(2, dest.int_field());
1029 EXPECT_EQ("hi", dest.string_field());
1030}
1031
zhanyong.wanc6a41232009-05-13 23:38:40 +00001032// Tests that SetArgumentPointee<N>(proto2_buffer) sets the
1033// proto2::Message variable pointed to by the N-th (0-based) argument
1034// to proto2_buffer.
1035TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
1036 using testing::internal::FooMessage;
1037 FooMessage* const msg = new FooMessage;
1038 msg->set_int_field(2);
1039 msg->set_string_field("hi");
1040 FooMessage orig_msg;
1041 orig_msg.CopyFrom(*msg);
1042
1043 Action<void(bool, ::proto2::Message*)> a = SetArgumentPointee<1>(*msg);
1044 // SetArgumentPointee<N>(proto2_buffer) makes a copy of
1045 // proto2_buffer s.t. the action works even when the original
1046 // proto2_buffer has died. We ensure this behavior by deleting msg
1047 // before using the action.
1048 delete msg;
1049
1050 FooMessage dest;
1051 dest.set_int_field(0);
1052 ::proto2::Message* const dest_base = &dest;
1053 a.Perform(make_tuple(true, dest_base));
1054 EXPECT_EQ(2, dest.int_field());
1055 EXPECT_EQ("hi", dest.string_field());
1056}
1057
zhanyong.wan02f71062010-05-10 17:14:29 +00001058#endif // GTEST_HAS_PROTOBUF_
shiqiane35fdd92008-12-10 05:08:54 +00001059
shiqiane35fdd92008-12-10 05:08:54 +00001060// Sample functions and functors for testing Invoke() and etc.
1061int Nullary() { return 1; }
1062
1063class NullaryFunctor {
1064 public:
1065 int operator()() { return 2; }
1066};
1067
1068bool g_done = false;
1069void VoidNullary() { g_done = true; }
1070
1071class VoidNullaryFunctor {
1072 public:
1073 void operator()() { g_done = true; }
1074};
1075
shiqiane35fdd92008-12-10 05:08:54 +00001076class Foo {
1077 public:
1078 Foo() : value_(123) {}
1079
1080 int Nullary() const { return value_; }
zhanyong.wan29be9232013-03-01 06:53:35 +00001081
shiqiane35fdd92008-12-10 05:08:54 +00001082 private:
1083 int value_;
1084};
1085
1086// Tests InvokeWithoutArgs(function).
1087TEST(InvokeWithoutArgsTest, Function) {
1088 // As an action that takes one argument.
1089 Action<int(int)> a = InvokeWithoutArgs(Nullary); // NOLINT
1090 EXPECT_EQ(1, a.Perform(make_tuple(2)));
1091
1092 // As an action that takes two arguments.
zhanyong.wan32de5f52009-12-23 00:13:23 +00001093 Action<int(int, double)> a2 = InvokeWithoutArgs(Nullary); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00001094 EXPECT_EQ(1, a2.Perform(make_tuple(2, 3.5)));
1095
1096 // As an action that returns void.
1097 Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary); // NOLINT
1098 g_done = false;
1099 a3.Perform(make_tuple(1));
1100 EXPECT_TRUE(g_done);
1101}
1102
1103// Tests InvokeWithoutArgs(functor).
1104TEST(InvokeWithoutArgsTest, Functor) {
1105 // As an action that takes no argument.
1106 Action<int()> a = InvokeWithoutArgs(NullaryFunctor()); // NOLINT
1107 EXPECT_EQ(2, a.Perform(make_tuple()));
1108
1109 // As an action that takes three arguments.
zhanyong.wan32de5f52009-12-23 00:13:23 +00001110 Action<int(int, double, char)> a2 = // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00001111 InvokeWithoutArgs(NullaryFunctor());
1112 EXPECT_EQ(2, a2.Perform(make_tuple(3, 3.5, 'a')));
1113
1114 // As an action that returns void.
1115 Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor());
1116 g_done = false;
1117 a3.Perform(make_tuple());
1118 EXPECT_TRUE(g_done);
1119}
1120
1121// Tests InvokeWithoutArgs(obj_ptr, method).
1122TEST(InvokeWithoutArgsTest, Method) {
1123 Foo foo;
1124 Action<int(bool, char)> a = // NOLINT
1125 InvokeWithoutArgs(&foo, &Foo::Nullary);
1126 EXPECT_EQ(123, a.Perform(make_tuple(true, 'a')));
1127}
1128
1129// Tests using IgnoreResult() on a polymorphic action.
1130TEST(IgnoreResultTest, PolymorphicAction) {
1131 Action<void(int)> a = IgnoreResult(Return(5)); // NOLINT
1132 a.Perform(make_tuple(1));
1133}
1134
1135// Tests using IgnoreResult() on a monomorphic action.
1136
1137int ReturnOne() {
1138 g_done = true;
1139 return 1;
1140}
1141
1142TEST(IgnoreResultTest, MonomorphicAction) {
1143 g_done = false;
1144 Action<void()> a = IgnoreResult(Invoke(ReturnOne));
1145 a.Perform(make_tuple());
1146 EXPECT_TRUE(g_done);
1147}
1148
1149// Tests using IgnoreResult() on an action that returns a class type.
1150
zhanyong.wan32de5f52009-12-23 00:13:23 +00001151MyClass ReturnMyClass(double /* x */) {
shiqiane35fdd92008-12-10 05:08:54 +00001152 g_done = true;
1153 return MyClass();
1154}
1155
1156TEST(IgnoreResultTest, ActionReturningClass) {
1157 g_done = false;
1158 Action<void(int)> a = IgnoreResult(Invoke(ReturnMyClass)); // NOLINT
1159 a.Perform(make_tuple(2));
1160 EXPECT_TRUE(g_done);
1161}
1162
1163TEST(AssignTest, Int) {
1164 int x = 0;
1165 Action<void(int)> a = Assign(&x, 5);
1166 a.Perform(make_tuple(0));
1167 EXPECT_EQ(5, x);
1168}
1169
1170TEST(AssignTest, String) {
1171 ::std::string x;
1172 Action<void(void)> a = Assign(&x, "Hello, world");
1173 a.Perform(make_tuple());
1174 EXPECT_EQ("Hello, world", x);
1175}
1176
1177TEST(AssignTest, CompatibleTypes) {
1178 double x = 0;
1179 Action<void(int)> a = Assign(&x, 5);
1180 a.Perform(make_tuple(0));
1181 EXPECT_DOUBLE_EQ(5, x);
1182}
1183
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001184#if !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001185
shiqiane35fdd92008-12-10 05:08:54 +00001186class SetErrnoAndReturnTest : public testing::Test {
1187 protected:
1188 virtual void SetUp() { errno = 0; }
1189 virtual void TearDown() { errno = 0; }
1190};
1191
1192TEST_F(SetErrnoAndReturnTest, Int) {
1193 Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5);
1194 EXPECT_EQ(-5, a.Perform(make_tuple()));
1195 EXPECT_EQ(ENOTTY, errno);
1196}
1197
1198TEST_F(SetErrnoAndReturnTest, Ptr) {
1199 int x;
1200 Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x);
1201 EXPECT_EQ(&x, a.Perform(make_tuple()));
1202 EXPECT_EQ(ENOTTY, errno);
1203}
1204
1205TEST_F(SetErrnoAndReturnTest, CompatibleTypes) {
1206 Action<double()> a = SetErrnoAndReturn(EINVAL, 5);
1207 EXPECT_DOUBLE_EQ(5.0, a.Perform(make_tuple()));
1208 EXPECT_EQ(EINVAL, errno);
1209}
1210
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001211#endif // !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001212
zhanyong.wana18423e2009-07-22 23:58:19 +00001213// Tests ByRef().
1214
1215// Tests that ReferenceWrapper<T> is copyable.
1216TEST(ByRefTest, IsCopyable) {
1217 const std::string s1 = "Hi";
1218 const std::string s2 = "Hello";
1219
jgm79a367e2012-04-10 16:02:11 +00001220 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper =
1221 ByRef(s1);
zhanyong.wana18423e2009-07-22 23:58:19 +00001222 const std::string& r1 = ref_wrapper;
1223 EXPECT_EQ(&s1, &r1);
1224
1225 // Assigns a new value to ref_wrapper.
1226 ref_wrapper = ByRef(s2);
1227 const std::string& r2 = ref_wrapper;
1228 EXPECT_EQ(&s2, &r2);
1229
jgm79a367e2012-04-10 16:02:11 +00001230 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper1 =
1231 ByRef(s1);
zhanyong.wana18423e2009-07-22 23:58:19 +00001232 // Copies ref_wrapper1 to ref_wrapper.
1233 ref_wrapper = ref_wrapper1;
1234 const std::string& r3 = ref_wrapper;
1235 EXPECT_EQ(&s1, &r3);
1236}
1237
1238// Tests using ByRef() on a const value.
1239TEST(ByRefTest, ConstValue) {
1240 const int n = 0;
1241 // int& ref = ByRef(n); // This shouldn't compile - we have a
1242 // negative compilation test to catch it.
1243 const int& const_ref = ByRef(n);
1244 EXPECT_EQ(&n, &const_ref);
1245}
1246
1247// Tests using ByRef() on a non-const value.
1248TEST(ByRefTest, NonConstValue) {
1249 int n = 0;
1250
1251 // ByRef(n) can be used as either an int&,
1252 int& ref = ByRef(n);
1253 EXPECT_EQ(&n, &ref);
1254
1255 // or a const int&.
1256 const int& const_ref = ByRef(n);
1257 EXPECT_EQ(&n, &const_ref);
1258}
1259
1260// Tests explicitly specifying the type when using ByRef().
1261TEST(ByRefTest, ExplicitType) {
1262 int n = 0;
1263 const int& r1 = ByRef<const int>(n);
1264 EXPECT_EQ(&n, &r1);
1265
1266 // ByRef<char>(n); // This shouldn't compile - we have a negative
1267 // compilation test to catch it.
1268
1269 Derived d;
1270 Derived& r2 = ByRef<Derived>(d);
1271 EXPECT_EQ(&d, &r2);
1272
1273 const Derived& r3 = ByRef<const Derived>(d);
1274 EXPECT_EQ(&d, &r3);
1275
1276 Base& r4 = ByRef<Base>(d);
1277 EXPECT_EQ(&d, &r4);
1278
1279 const Base& r5 = ByRef<const Base>(d);
1280 EXPECT_EQ(&d, &r5);
1281
1282 // The following shouldn't compile - we have a negative compilation
1283 // test for it.
1284 //
1285 // Base b;
1286 // ByRef<Derived>(b);
1287}
1288
1289// Tests that Google Mock prints expression ByRef(x) as a reference to x.
1290TEST(ByRefTest, PrintsCorrectly) {
1291 int n = 42;
1292 ::std::stringstream expected, actual;
1293 testing::internal::UniversalPrinter<const int&>::Print(n, &expected);
1294 testing::internal::UniversalPrint(ByRef(n), &actual);
1295 EXPECT_EQ(expected.str(), actual.str());
1296}
1297
kosak5b9cbbb2014-11-17 00:28:55 +00001298#if GTEST_HAS_STD_UNIQUE_PTR_
kosakb5c81092014-01-29 06:41:44 +00001299
1300std::unique_ptr<int> UniquePtrSource() {
1301 return std::unique_ptr<int>(new int(19));
1302}
1303
1304std::vector<std::unique_ptr<int>> VectorUniquePtrSource() {
1305 std::vector<std::unique_ptr<int>> out;
1306 out.emplace_back(new int(7));
1307 return out;
1308}
1309
kosak3d1c78b2014-11-17 00:56:52 +00001310TEST(MockMethodTest, CanReturnMoveOnlyValue_Return) {
1311 MockClass mock;
1312 std::unique_ptr<int> i(new int(19));
1313 EXPECT_CALL(mock, MakeUnique()).WillOnce(Return(ByMove(std::move(i))));
1314 EXPECT_CALL(mock, MakeVectorUnique())
1315 .WillOnce(Return(ByMove(VectorUniquePtrSource())));
1316 Derived* d = new Derived;
1317 EXPECT_CALL(mock, MakeUniqueBase())
1318 .WillOnce(Return(ByMove(std::unique_ptr<Derived>(d))));
1319
1320 std::unique_ptr<int> result1 = mock.MakeUnique();
1321 EXPECT_EQ(19, *result1);
1322
1323 std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
kosak389bad62014-11-17 01:08:51 +00001324 EXPECT_EQ(1u, vresult.size());
kosak3d1c78b2014-11-17 00:56:52 +00001325 EXPECT_NE(nullptr, vresult[0]);
1326 EXPECT_EQ(7, *vresult[0]);
1327
1328 std::unique_ptr<Base> result2 = mock.MakeUniqueBase();
1329 EXPECT_EQ(d, result2.get());
1330}
1331
1332TEST(MockMethodTest, CanReturnMoveOnlyValue_DoAllReturn) {
1333 testing::MockFunction<void()> mock_function;
1334 MockClass mock;
1335 std::unique_ptr<int> i(new int(19));
1336 EXPECT_CALL(mock_function, Call());
1337 EXPECT_CALL(mock, MakeUnique()).WillOnce(DoAll(
1338 InvokeWithoutArgs(&mock_function, &testing::MockFunction<void()>::Call),
1339 Return(ByMove(std::move(i)))));
1340
1341 std::unique_ptr<int> result1 = mock.MakeUnique();
1342 EXPECT_EQ(19, *result1);
1343}
1344
1345TEST(MockMethodTest, CanReturnMoveOnlyValue_Invoke) {
kosakb5c81092014-01-29 06:41:44 +00001346 MockClass mock;
1347
1348 // Check default value
1349 DefaultValue<std::unique_ptr<int>>::SetFactory([] {
1350 return std::unique_ptr<int>(new int(42));
1351 });
1352 EXPECT_EQ(42, *mock.MakeUnique());
1353
kosak3d1c78b2014-11-17 00:56:52 +00001354 EXPECT_CALL(mock, MakeUnique()).WillRepeatedly(Invoke(UniquePtrSource));
kosakb5c81092014-01-29 06:41:44 +00001355 EXPECT_CALL(mock, MakeVectorUnique())
1356 .WillRepeatedly(Invoke(VectorUniquePtrSource));
1357 std::unique_ptr<int> result1 = mock.MakeUnique();
1358 EXPECT_EQ(19, *result1);
1359 std::unique_ptr<int> result2 = mock.MakeUnique();
1360 EXPECT_EQ(19, *result2);
1361 EXPECT_NE(result1, result2);
1362
1363 std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
kosak389bad62014-11-17 01:08:51 +00001364 EXPECT_EQ(1u, vresult.size());
kosakb5c81092014-01-29 06:41:44 +00001365 EXPECT_NE(nullptr, vresult[0]);
1366 EXPECT_EQ(7, *vresult[0]);
1367}
1368
kosak5b9cbbb2014-11-17 00:28:55 +00001369#endif // GTEST_HAS_STD_UNIQUE_PTR_
kosakb5c81092014-01-29 06:41:44 +00001370
shiqiane35fdd92008-12-10 05:08:54 +00001371} // Unnamed namespace