blob: 115a90208d41fa8ceba31eaffcb5f383b2a9ae39 [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
48using ::std::tr1::get;
49using ::std::tr1::make_tuple;
50using ::std::tr1::tuple;
51using ::std::tr1::tuple_element;
52using 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;
zhanyong.wana18423e2009-07-22 23:58:19 +000060using testing::ByRef;
shiqiane35fdd92008-12-10 05:08:54 +000061using testing::DefaultValue;
62using testing::DoDefault;
63using testing::IgnoreResult;
64using testing::Invoke;
65using testing::InvokeWithoutArgs;
66using testing::MakePolymorphicAction;
67using testing::Ne;
68using testing::PolymorphicAction;
69using testing::Return;
70using testing::ReturnNull;
71using testing::ReturnRef;
zhanyong.wane3bd0982010-07-03 00:16:42 +000072using testing::ReturnRefOfCopy;
zhanyong.wan59214832010-10-05 05:58:51 +000073using testing::SetArgPointee;
shiqiane35fdd92008-12-10 05:08:54 +000074using testing::SetArgumentPointee;
zhanyong.wan5b5d62f2009-03-11 23:37:56 +000075
zhanyong.wanf7af24c2009-09-24 21:17:24 +000076#if !GTEST_OS_WINDOWS_MOBILE
shiqiane35fdd92008-12-10 05:08:54 +000077using testing::SetErrnoAndReturn;
zhanyong.wanf7af24c2009-09-24 21:17:24 +000078#endif
shiqiane35fdd92008-12-10 05:08:54 +000079
zhanyong.wan02f71062010-05-10 17:14:29 +000080#if GTEST_HAS_PROTOBUF_
shiqiane35fdd92008-12-10 05:08:54 +000081using testing::internal::TestMessage;
zhanyong.wan02f71062010-05-10 17:14:29 +000082#endif // GTEST_HAS_PROTOBUF_
shiqiane35fdd92008-12-10 05:08:54 +000083
84// Tests that BuiltInDefaultValue<T*>::Get() returns NULL.
85TEST(BuiltInDefaultValueTest, IsNullForPointerTypes) {
86 EXPECT_TRUE(BuiltInDefaultValue<int*>::Get() == NULL);
87 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Get() == NULL);
88 EXPECT_TRUE(BuiltInDefaultValue<void*>::Get() == NULL);
89}
90
zhanyong.wan5b95fa72009-01-27 22:28:45 +000091// Tests that BuiltInDefaultValue<T*>::Exists() return true.
92TEST(BuiltInDefaultValueTest, ExistsForPointerTypes) {
93 EXPECT_TRUE(BuiltInDefaultValue<int*>::Exists());
94 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Exists());
95 EXPECT_TRUE(BuiltInDefaultValue<void*>::Exists());
96}
97
shiqiane35fdd92008-12-10 05:08:54 +000098// Tests that BuiltInDefaultValue<T>::Get() returns 0 when T is a
99// built-in numeric type.
100TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) {
zhanyong.wan32de5f52009-12-23 00:13:23 +0000101 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned char>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000102 EXPECT_EQ(0, BuiltInDefaultValue<signed char>::Get());
103 EXPECT_EQ(0, BuiltInDefaultValue<char>::Get());
zhanyong.wan95b12332009-09-25 18:55:50 +0000104#if GMOCK_HAS_SIGNED_WCHAR_T_
zhanyong.wan32de5f52009-12-23 00:13:23 +0000105 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned wchar_t>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000106 EXPECT_EQ(0, BuiltInDefaultValue<signed wchar_t>::Get());
zhanyong.wan95b12332009-09-25 18:55:50 +0000107#endif
108#if GMOCK_WCHAR_T_IS_NATIVE_
shiqiane35fdd92008-12-10 05:08:54 +0000109 EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get());
zhanyong.wan95b12332009-09-25 18:55:50 +0000110#endif
zhanyong.wan32de5f52009-12-23 00:13:23 +0000111 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned short>::Get()); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +0000112 EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get()); // NOLINT
113 EXPECT_EQ(0, BuiltInDefaultValue<short>::Get()); // NOLINT
zhanyong.wan32de5f52009-12-23 00:13:23 +0000114 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned int>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000115 EXPECT_EQ(0, BuiltInDefaultValue<signed int>::Get());
116 EXPECT_EQ(0, BuiltInDefaultValue<int>::Get());
zhanyong.wan32de5f52009-12-23 00:13:23 +0000117 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned long>::Get()); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +0000118 EXPECT_EQ(0, BuiltInDefaultValue<signed long>::Get()); // NOLINT
119 EXPECT_EQ(0, BuiltInDefaultValue<long>::Get()); // NOLINT
zhanyong.wan32de5f52009-12-23 00:13:23 +0000120 EXPECT_EQ(0U, BuiltInDefaultValue<UInt64>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000121 EXPECT_EQ(0, BuiltInDefaultValue<Int64>::Get());
122 EXPECT_EQ(0, BuiltInDefaultValue<float>::Get());
123 EXPECT_EQ(0, BuiltInDefaultValue<double>::Get());
124}
125
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000126// Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
127// built-in numeric type.
128TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) {
129 EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists());
130 EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists());
131 EXPECT_TRUE(BuiltInDefaultValue<char>::Exists());
zhanyong.wan95b12332009-09-25 18:55:50 +0000132#if GMOCK_HAS_SIGNED_WCHAR_T_
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000133 EXPECT_TRUE(BuiltInDefaultValue<unsigned wchar_t>::Exists());
134 EXPECT_TRUE(BuiltInDefaultValue<signed wchar_t>::Exists());
zhanyong.wan95b12332009-09-25 18:55:50 +0000135#endif
136#if GMOCK_WCHAR_T_IS_NATIVE_
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000137 EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists());
zhanyong.wan95b12332009-09-25 18:55:50 +0000138#endif
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000139 EXPECT_TRUE(BuiltInDefaultValue<unsigned short>::Exists()); // NOLINT
140 EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists()); // NOLINT
141 EXPECT_TRUE(BuiltInDefaultValue<short>::Exists()); // NOLINT
142 EXPECT_TRUE(BuiltInDefaultValue<unsigned int>::Exists());
143 EXPECT_TRUE(BuiltInDefaultValue<signed int>::Exists());
144 EXPECT_TRUE(BuiltInDefaultValue<int>::Exists());
145 EXPECT_TRUE(BuiltInDefaultValue<unsigned long>::Exists()); // NOLINT
146 EXPECT_TRUE(BuiltInDefaultValue<signed long>::Exists()); // NOLINT
147 EXPECT_TRUE(BuiltInDefaultValue<long>::Exists()); // NOLINT
148 EXPECT_TRUE(BuiltInDefaultValue<UInt64>::Exists());
149 EXPECT_TRUE(BuiltInDefaultValue<Int64>::Exists());
150 EXPECT_TRUE(BuiltInDefaultValue<float>::Exists());
151 EXPECT_TRUE(BuiltInDefaultValue<double>::Exists());
152}
153
shiqiane35fdd92008-12-10 05:08:54 +0000154// Tests that BuiltInDefaultValue<bool>::Get() returns false.
155TEST(BuiltInDefaultValueTest, IsFalseForBool) {
156 EXPECT_FALSE(BuiltInDefaultValue<bool>::Get());
157}
158
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000159// Tests that BuiltInDefaultValue<bool>::Exists() returns true.
160TEST(BuiltInDefaultValueTest, BoolExists) {
161 EXPECT_TRUE(BuiltInDefaultValue<bool>::Exists());
162}
163
shiqiane35fdd92008-12-10 05:08:54 +0000164// Tests that BuiltInDefaultValue<T>::Get() returns "" when T is a
165// string type.
166TEST(BuiltInDefaultValueTest, IsEmptyStringForString) {
167#if GTEST_HAS_GLOBAL_STRING
168 EXPECT_EQ("", BuiltInDefaultValue< ::string>::Get());
169#endif // GTEST_HAS_GLOBAL_STRING
170
shiqiane35fdd92008-12-10 05:08:54 +0000171 EXPECT_EQ("", BuiltInDefaultValue< ::std::string>::Get());
shiqiane35fdd92008-12-10 05:08:54 +0000172}
173
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000174// Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
175// string type.
176TEST(BuiltInDefaultValueTest, ExistsForString) {
177#if GTEST_HAS_GLOBAL_STRING
178 EXPECT_TRUE(BuiltInDefaultValue< ::string>::Exists());
179#endif // GTEST_HAS_GLOBAL_STRING
180
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000181 EXPECT_TRUE(BuiltInDefaultValue< ::std::string>::Exists());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000182}
183
shiqiane35fdd92008-12-10 05:08:54 +0000184// Tests that BuiltInDefaultValue<const T>::Get() returns the same
185// value as BuiltInDefaultValue<T>::Get() does.
186TEST(BuiltInDefaultValueTest, WorksForConstTypes) {
187 EXPECT_EQ("", BuiltInDefaultValue<const std::string>::Get());
188 EXPECT_EQ(0, BuiltInDefaultValue<const int>::Get());
189 EXPECT_TRUE(BuiltInDefaultValue<char* const>::Get() == NULL);
190 EXPECT_FALSE(BuiltInDefaultValue<const bool>::Get());
191}
192
193// Tests that BuiltInDefaultValue<T>::Get() aborts the program with
194// the correct error message when T is a user-defined type.
195struct UserType {
196 UserType() : value(0) {}
197
198 int value;
199};
200
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000201TEST(BuiltInDefaultValueTest, UserTypeHasNoDefault) {
202 EXPECT_FALSE(BuiltInDefaultValue<UserType>::Exists());
203}
204
shiqiane35fdd92008-12-10 05:08:54 +0000205// Tests that BuiltInDefaultValue<T&>::Get() aborts the program.
206TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000207 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000208 BuiltInDefaultValue<int&>::Get();
209 }, "");
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000210 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000211 BuiltInDefaultValue<const char&>::Get();
212 }, "");
213}
214
215TEST(BuiltInDefaultValueDeathTest, IsUndefinedForUserTypes) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000216 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000217 BuiltInDefaultValue<UserType>::Get();
218 }, "");
219}
220
shiqiane35fdd92008-12-10 05:08:54 +0000221// Tests that DefaultValue<T>::IsSet() is false initially.
222TEST(DefaultValueTest, IsInitiallyUnset) {
223 EXPECT_FALSE(DefaultValue<int>::IsSet());
224 EXPECT_FALSE(DefaultValue<const UserType>::IsSet());
225}
226
227// Tests that DefaultValue<T> can be set and then unset.
228TEST(DefaultValueTest, CanBeSetAndUnset) {
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000229 EXPECT_TRUE(DefaultValue<int>::Exists());
230 EXPECT_FALSE(DefaultValue<const UserType>::Exists());
231
shiqiane35fdd92008-12-10 05:08:54 +0000232 DefaultValue<int>::Set(1);
233 DefaultValue<const UserType>::Set(UserType());
234
235 EXPECT_EQ(1, DefaultValue<int>::Get());
236 EXPECT_EQ(0, DefaultValue<const UserType>::Get().value);
237
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000238 EXPECT_TRUE(DefaultValue<int>::Exists());
239 EXPECT_TRUE(DefaultValue<const UserType>::Exists());
240
shiqiane35fdd92008-12-10 05:08:54 +0000241 DefaultValue<int>::Clear();
242 DefaultValue<const UserType>::Clear();
243
244 EXPECT_FALSE(DefaultValue<int>::IsSet());
245 EXPECT_FALSE(DefaultValue<const UserType>::IsSet());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000246
247 EXPECT_TRUE(DefaultValue<int>::Exists());
248 EXPECT_FALSE(DefaultValue<const UserType>::Exists());
shiqiane35fdd92008-12-10 05:08:54 +0000249}
250
251// Tests that DefaultValue<T>::Get() returns the
252// BuiltInDefaultValue<T>::Get() when DefaultValue<T>::IsSet() is
253// false.
254TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
255 EXPECT_FALSE(DefaultValue<int>::IsSet());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000256 EXPECT_TRUE(DefaultValue<int>::Exists());
shiqiane35fdd92008-12-10 05:08:54 +0000257 EXPECT_FALSE(DefaultValue<UserType>::IsSet());
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000258 EXPECT_FALSE(DefaultValue<UserType>::Exists());
shiqiane35fdd92008-12-10 05:08:54 +0000259
260 EXPECT_EQ(0, DefaultValue<int>::Get());
261
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000262 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000263 DefaultValue<UserType>::Get();
264 }, "");
shiqiane35fdd92008-12-10 05:08:54 +0000265}
266
kosakb5c81092014-01-29 06:41:44 +0000267#if GTEST_LANG_CXX11
268TEST(DefaultValueDeathTest, GetWorksForMoveOnlyIfSet) {
269 EXPECT_FALSE(DefaultValue<std::unique_ptr<int>>::Exists());
270 EXPECT_DEATH_IF_SUPPORTED({
271 DefaultValue<std::unique_ptr<int>>::Get();
272 }, "");
273 DefaultValue<std::unique_ptr<int>>::SetFactory([] {
274 return std::unique_ptr<int>(new int(42));
275 });
276 EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Exists());
277 std::unique_ptr<int> i = DefaultValue<std::unique_ptr<int>>::Get();
278 EXPECT_EQ(42, *i);
279}
280#endif // GTEST_LANG_CXX11
281
shiqiane35fdd92008-12-10 05:08:54 +0000282// Tests that DefaultValue<void>::Get() returns void.
283TEST(DefaultValueTest, GetWorksForVoid) {
284 return DefaultValue<void>::Get();
285}
286
287// Tests using DefaultValue with a reference type.
288
289// Tests that DefaultValue<T&>::IsSet() is false initially.
290TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) {
291 EXPECT_FALSE(DefaultValue<int&>::IsSet());
292 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
293}
294
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000295// Tests that DefaultValue<T&>::Exists is false initiallly.
296TEST(DefaultValueOfReferenceTest, IsInitiallyNotExisting) {
297 EXPECT_FALSE(DefaultValue<int&>::Exists());
298 EXPECT_FALSE(DefaultValue<UserType&>::Exists());
299}
300
shiqiane35fdd92008-12-10 05:08:54 +0000301// Tests that DefaultValue<T&> can be set and then unset.
302TEST(DefaultValueOfReferenceTest, CanBeSetAndUnset) {
303 int n = 1;
304 DefaultValue<const int&>::Set(n);
305 UserType u;
306 DefaultValue<UserType&>::Set(u);
307
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000308 EXPECT_TRUE(DefaultValue<const int&>::Exists());
309 EXPECT_TRUE(DefaultValue<UserType&>::Exists());
310
shiqiane35fdd92008-12-10 05:08:54 +0000311 EXPECT_EQ(&n, &(DefaultValue<const int&>::Get()));
312 EXPECT_EQ(&u, &(DefaultValue<UserType&>::Get()));
313
314 DefaultValue<const int&>::Clear();
315 DefaultValue<UserType&>::Clear();
316
zhanyong.wan5b95fa72009-01-27 22:28:45 +0000317 EXPECT_FALSE(DefaultValue<const int&>::Exists());
318 EXPECT_FALSE(DefaultValue<UserType&>::Exists());
319
shiqiane35fdd92008-12-10 05:08:54 +0000320 EXPECT_FALSE(DefaultValue<const int&>::IsSet());
321 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
322}
323
324// Tests that DefaultValue<T&>::Get() returns the
325// BuiltInDefaultValue<T&>::Get() when DefaultValue<T&>::IsSet() is
326// false.
327TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
328 EXPECT_FALSE(DefaultValue<int&>::IsSet());
329 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
330
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000331 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000332 DefaultValue<int&>::Get();
333 }, "");
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000334 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000335 DefaultValue<UserType>::Get();
336 }, "");
shiqiane35fdd92008-12-10 05:08:54 +0000337}
338
339// Tests that ActionInterface can be implemented by defining the
340// Perform method.
341
zhanyong.wana1a98f82013-03-01 21:28:40 +0000342typedef int MyGlobalFunction(bool, int);
shiqiane35fdd92008-12-10 05:08:54 +0000343
zhanyong.wana1a98f82013-03-01 21:28:40 +0000344class MyActionImpl : public ActionInterface<MyGlobalFunction> {
shiqiane35fdd92008-12-10 05:08:54 +0000345 public:
346 virtual int Perform(const tuple<bool, int>& args) {
347 return get<0>(args) ? get<1>(args) : 0;
348 }
349};
350
351TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) {
352 MyActionImpl my_action_impl;
zhanyong.waned6c9272011-02-23 19:39:27 +0000353 (void)my_action_impl;
shiqiane35fdd92008-12-10 05:08:54 +0000354}
355
356TEST(ActionInterfaceTest, MakeAction) {
zhanyong.wana1a98f82013-03-01 21:28:40 +0000357 Action<MyGlobalFunction> action = MakeAction(new MyActionImpl);
shiqiane35fdd92008-12-10 05:08:54 +0000358
359 // When exercising the Perform() method of Action<F>, we must pass
360 // it a tuple whose size and type are compatible with F's argument
361 // types. For example, if F is int(), then Perform() takes a
362 // 0-tuple; if F is void(bool, int), then Perform() takes a
363 // tuple<bool, int>, and so on.
364 EXPECT_EQ(5, action.Perform(make_tuple(true, 5)));
365}
366
367// Tests that Action<F> can be contructed from a pointer to
368// ActionInterface<F>.
369TEST(ActionTest, CanBeConstructedFromActionInterface) {
zhanyong.wana1a98f82013-03-01 21:28:40 +0000370 Action<MyGlobalFunction> action(new MyActionImpl);
shiqiane35fdd92008-12-10 05:08:54 +0000371}
372
373// Tests that Action<F> delegates actual work to ActionInterface<F>.
374TEST(ActionTest, DelegatesWorkToActionInterface) {
zhanyong.wana1a98f82013-03-01 21:28:40 +0000375 const Action<MyGlobalFunction> action(new MyActionImpl);
shiqiane35fdd92008-12-10 05:08:54 +0000376
377 EXPECT_EQ(5, action.Perform(make_tuple(true, 5)));
378 EXPECT_EQ(0, action.Perform(make_tuple(false, 1)));
379}
380
381// Tests that Action<F> can be copied.
382TEST(ActionTest, IsCopyable) {
zhanyong.wana1a98f82013-03-01 21:28:40 +0000383 Action<MyGlobalFunction> a1(new MyActionImpl);
384 Action<MyGlobalFunction> a2(a1); // Tests the copy constructor.
shiqiane35fdd92008-12-10 05:08:54 +0000385
386 // a1 should continue to work after being copied from.
387 EXPECT_EQ(5, a1.Perform(make_tuple(true, 5)));
388 EXPECT_EQ(0, a1.Perform(make_tuple(false, 1)));
389
390 // a2 should work like the action it was copied from.
391 EXPECT_EQ(5, a2.Perform(make_tuple(true, 5)));
392 EXPECT_EQ(0, a2.Perform(make_tuple(false, 1)));
393
394 a2 = a1; // Tests the assignment operator.
395
396 // a1 should continue to work after being copied from.
397 EXPECT_EQ(5, a1.Perform(make_tuple(true, 5)));
398 EXPECT_EQ(0, a1.Perform(make_tuple(false, 1)));
399
400 // a2 should work like the action it was copied from.
401 EXPECT_EQ(5, a2.Perform(make_tuple(true, 5)));
402 EXPECT_EQ(0, a2.Perform(make_tuple(false, 1)));
403}
404
405// Tests that an Action<From> object can be converted to a
406// compatible Action<To> object.
407
408class IsNotZero : public ActionInterface<bool(int)> { // NOLINT
409 public:
410 virtual bool Perform(const tuple<int>& arg) {
411 return get<0>(arg) != 0;
412 }
413};
414
zhanyong.wan95b12332009-09-25 18:55:50 +0000415#if !GTEST_OS_SYMBIAN
416// Compiling this test on Nokia's Symbian compiler fails with:
417// 'Result' is not a member of class 'testing::internal::Function<int>'
418// (point of instantiation: '@unnamed@gmock_actions_test_cc@::
419// ActionTest_CanBeConvertedToOtherActionType_Test::TestBody()')
420// with no obvious fix.
shiqiane35fdd92008-12-10 05:08:54 +0000421TEST(ActionTest, CanBeConvertedToOtherActionType) {
422 const Action<bool(int)> a1(new IsNotZero); // NOLINT
423 const Action<int(char)> a2 = Action<int(char)>(a1); // NOLINT
424 EXPECT_EQ(1, a2.Perform(make_tuple('a')));
425 EXPECT_EQ(0, a2.Perform(make_tuple('\0')));
426}
zhanyong.wan95b12332009-09-25 18:55:50 +0000427#endif // !GTEST_OS_SYMBIAN
shiqiane35fdd92008-12-10 05:08:54 +0000428
429// The following two classes are for testing MakePolymorphicAction().
430
431// Implements a polymorphic action that returns the second of the
432// arguments it receives.
433class ReturnSecondArgumentAction {
434 public:
435 // We want to verify that MakePolymorphicAction() can work with a
436 // polymorphic action whose Perform() method template is either
437 // const or not. This lets us verify the non-const case.
438 template <typename Result, typename ArgumentTuple>
439 Result Perform(const ArgumentTuple& args) { return get<1>(args); }
440};
441
442// Implements a polymorphic action that can be used in a nullary
443// function to return 0.
444class ReturnZeroFromNullaryFunctionAction {
445 public:
446 // For testing that MakePolymorphicAction() works when the
447 // implementation class' Perform() method template takes only one
448 // template parameter.
449 //
450 // We want to verify that MakePolymorphicAction() can work with a
451 // polymorphic action whose Perform() method template is either
452 // const or not. This lets us verify the const case.
453 template <typename Result>
454 Result Perform(const tuple<>&) const { return 0; }
455};
456
457// These functions verify that MakePolymorphicAction() returns a
458// PolymorphicAction<T> where T is the argument's type.
459
460PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() {
461 return MakePolymorphicAction(ReturnSecondArgumentAction());
462}
463
464PolymorphicAction<ReturnZeroFromNullaryFunctionAction>
465ReturnZeroFromNullaryFunction() {
466 return MakePolymorphicAction(ReturnZeroFromNullaryFunctionAction());
467}
468
469// Tests that MakePolymorphicAction() turns a polymorphic action
470// implementation class into a polymorphic action.
471TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) {
472 Action<int(bool, int, double)> a1 = ReturnSecondArgument(); // NOLINT
473 EXPECT_EQ(5, a1.Perform(make_tuple(false, 5, 2.0)));
474}
475
476// Tests that MakePolymorphicAction() works when the implementation
477// class' Perform() method template has only one template parameter.
478TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) {
479 Action<int()> a1 = ReturnZeroFromNullaryFunction();
480 EXPECT_EQ(0, a1.Perform(make_tuple()));
481
482 Action<void*()> a2 = ReturnZeroFromNullaryFunction();
483 EXPECT_TRUE(a2.Perform(make_tuple()) == NULL);
484}
485
486// Tests that Return() works as an action for void-returning
487// functions.
488TEST(ReturnTest, WorksForVoid) {
489 const Action<void(int)> ret = Return(); // NOLINT
490 return ret.Perform(make_tuple(1));
491}
492
493// Tests that Return(v) returns v.
494TEST(ReturnTest, ReturnsGivenValue) {
495 Action<int()> ret = Return(1); // NOLINT
496 EXPECT_EQ(1, ret.Perform(make_tuple()));
497
498 ret = Return(-5);
499 EXPECT_EQ(-5, ret.Perform(make_tuple()));
500}
501
502// Tests that Return("string literal") works.
503TEST(ReturnTest, AcceptsStringLiteral) {
504 Action<const char*()> a1 = Return("Hello");
505 EXPECT_STREQ("Hello", a1.Perform(make_tuple()));
506
507 Action<std::string()> a2 = Return("world");
508 EXPECT_EQ("world", a2.Perform(make_tuple()));
509}
510
511// Tests that Return(v) is covaraint.
512
513struct Base {
514 bool operator==(const Base&) { return true; }
515};
516
517struct Derived : public Base {
518 bool operator==(const Derived&) { return true; }
519};
520
521TEST(ReturnTest, IsCovariant) {
522 Base base;
523 Derived derived;
524 Action<Base*()> ret = Return(&base);
525 EXPECT_EQ(&base, ret.Perform(make_tuple()));
526
527 ret = Return(&derived);
528 EXPECT_EQ(&derived, ret.Perform(make_tuple()));
529}
530
vladloseva070cbd2009-11-18 00:09:28 +0000531// Tests that the type of the value passed into Return is converted into T
532// when the action is cast to Action<T(...)> rather than when the action is
533// performed. See comments on testing::internal::ReturnAction in
534// gmock-actions.h for more information.
535class FromType {
536 public:
jgm79a367e2012-04-10 16:02:11 +0000537 explicit FromType(bool* is_converted) : converted_(is_converted) {}
vladloseva070cbd2009-11-18 00:09:28 +0000538 bool* converted() const { return converted_; }
539
540 private:
541 bool* const converted_;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000542
543 GTEST_DISALLOW_ASSIGN_(FromType);
vladloseva070cbd2009-11-18 00:09:28 +0000544};
545
546class ToType {
547 public:
jgm79a367e2012-04-10 16:02:11 +0000548 // Must allow implicit conversion due to use in ImplicitCast_<T>.
549 ToType(const FromType& x) { *x.converted() = true; } // NOLINT
vladloseva070cbd2009-11-18 00:09:28 +0000550};
551
552TEST(ReturnTest, ConvertsArgumentWhenConverted) {
553 bool converted = false;
554 FromType x(&converted);
555 Action<ToType()> action(Return(x));
556 EXPECT_TRUE(converted) << "Return must convert its argument in its own "
557 << "conversion operator.";
558 converted = false;
559 action.Perform(tuple<>());
560 EXPECT_FALSE(converted) << "Action must NOT convert its argument "
jgm79a367e2012-04-10 16:02:11 +0000561 << "when performed.";
vladloseva070cbd2009-11-18 00:09:28 +0000562}
563
vladloseva070cbd2009-11-18 00:09:28 +0000564class DestinationType {};
565
566class SourceType {
567 public:
568 // Note: a non-const typecast operator.
569 operator DestinationType() { return DestinationType(); }
570};
571
572TEST(ReturnTest, CanConvertArgumentUsingNonConstTypeCastOperator) {
573 SourceType s;
574 Action<DestinationType()> action(Return(s));
575}
vladloseva070cbd2009-11-18 00:09:28 +0000576
shiqiane35fdd92008-12-10 05:08:54 +0000577// Tests that ReturnNull() returns NULL in a pointer-returning function.
578TEST(ReturnNullTest, WorksInPointerReturningFunction) {
579 const Action<int*()> a1 = ReturnNull();
580 EXPECT_TRUE(a1.Perform(make_tuple()) == NULL);
581
582 const Action<const char*(bool)> a2 = ReturnNull(); // NOLINT
583 EXPECT_TRUE(a2.Perform(make_tuple(true)) == NULL);
584}
585
586// Tests that ReturnRef(v) works for reference types.
587TEST(ReturnRefTest, WorksForReference) {
588 const int n = 0;
589 const Action<const int&(bool)> ret = ReturnRef(n); // NOLINT
590
591 EXPECT_EQ(&n, &ret.Perform(make_tuple(true)));
592}
593
594// Tests that ReturnRef(v) is covariant.
595TEST(ReturnRefTest, IsCovariant) {
596 Base base;
597 Derived derived;
598 Action<Base&()> a = ReturnRef(base);
599 EXPECT_EQ(&base, &a.Perform(make_tuple()));
600
601 a = ReturnRef(derived);
602 EXPECT_EQ(&derived, &a.Perform(make_tuple()));
603}
604
zhanyong.wane3bd0982010-07-03 00:16:42 +0000605// Tests that ReturnRefOfCopy(v) works for reference types.
606TEST(ReturnRefOfCopyTest, WorksForReference) {
607 int n = 42;
608 const Action<const int&()> ret = ReturnRefOfCopy(n);
609
610 EXPECT_NE(&n, &ret.Perform(make_tuple()));
611 EXPECT_EQ(42, ret.Perform(make_tuple()));
612
613 n = 43;
614 EXPECT_NE(&n, &ret.Perform(make_tuple()));
615 EXPECT_EQ(42, ret.Perform(make_tuple()));
616}
617
618// Tests that ReturnRefOfCopy(v) is covariant.
619TEST(ReturnRefOfCopyTest, IsCovariant) {
620 Base base;
621 Derived derived;
622 Action<Base&()> a = ReturnRefOfCopy(base);
623 EXPECT_NE(&base, &a.Perform(make_tuple()));
624
625 a = ReturnRefOfCopy(derived);
626 EXPECT_NE(&derived, &a.Perform(make_tuple()));
627}
628
shiqiane35fdd92008-12-10 05:08:54 +0000629// Tests that DoDefault() does the default action for the mock method.
630
631class MyClass {};
632
633class MockClass {
634 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000635 MockClass() {}
636
shiqiane35fdd92008-12-10 05:08:54 +0000637 MOCK_METHOD1(IntFunc, int(bool flag)); // NOLINT
638 MOCK_METHOD0(Foo, MyClass());
kosakb5c81092014-01-29 06:41:44 +0000639#if GTEST_LANG_CXX11
640 MOCK_METHOD0(MakeUnique, std::unique_ptr<int>());
641 MOCK_METHOD0(MakeVectorUnique, std::vector<std::unique_ptr<int>>());
642#endif
zhanyong.wan32de5f52009-12-23 00:13:23 +0000643
644 private:
645 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockClass);
shiqiane35fdd92008-12-10 05:08:54 +0000646};
647
648// Tests that DoDefault() returns the built-in default value for the
649// return type by default.
650TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) {
651 MockClass mock;
652 EXPECT_CALL(mock, IntFunc(_))
653 .WillOnce(DoDefault());
654 EXPECT_EQ(0, mock.IntFunc(true));
655}
656
zhanyong.wanedd4ab42013-02-28 22:58:51 +0000657// Tests that DoDefault() throws (when exceptions are enabled) or aborts
658// the process when there is no built-in default value for the return type.
shiqiane35fdd92008-12-10 05:08:54 +0000659TEST(DoDefaultDeathTest, DiesForUnknowType) {
660 MockClass mock;
661 EXPECT_CALL(mock, Foo())
662 .WillRepeatedly(DoDefault());
zhanyong.wanedd4ab42013-02-28 22:58:51 +0000663#if GTEST_HAS_EXCEPTIONS
664 EXPECT_ANY_THROW(mock.Foo());
665#else
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000666 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000667 mock.Foo();
668 }, "");
zhanyong.wanedd4ab42013-02-28 22:58:51 +0000669#endif
shiqiane35fdd92008-12-10 05:08:54 +0000670}
671
672// Tests that using DoDefault() inside a composite action leads to a
673// run-time error.
674
zhanyong.wan32de5f52009-12-23 00:13:23 +0000675void VoidFunc(bool /* flag */) {}
shiqiane35fdd92008-12-10 05:08:54 +0000676
677TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
678 MockClass mock;
679 EXPECT_CALL(mock, IntFunc(_))
680 .WillRepeatedly(DoAll(Invoke(VoidFunc),
681 DoDefault()));
682
683 // Ideally we should verify the error message as well. Sadly,
684 // EXPECT_DEATH() can only capture stderr, while Google Mock's
685 // errors are printed on stdout. Therefore we have to settle for
686 // not verifying the message.
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000687 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000688 mock.IntFunc(true);
689 }, "");
690}
691
shiqiane35fdd92008-12-10 05:08:54 +0000692// Tests that DoDefault() returns the default value set by
693// DefaultValue<T>::Set() when it's not overriden by an ON_CALL().
694TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) {
695 DefaultValue<int>::Set(1);
696 MockClass mock;
697 EXPECT_CALL(mock, IntFunc(_))
698 .WillOnce(DoDefault());
699 EXPECT_EQ(1, mock.IntFunc(false));
700 DefaultValue<int>::Clear();
701}
702
703// Tests that DoDefault() does the action specified by ON_CALL().
704TEST(DoDefaultTest, DoesWhatOnCallSpecifies) {
705 MockClass mock;
706 ON_CALL(mock, IntFunc(_))
707 .WillByDefault(Return(2));
708 EXPECT_CALL(mock, IntFunc(_))
709 .WillOnce(DoDefault());
710 EXPECT_EQ(2, mock.IntFunc(false));
711}
712
713// Tests that using DoDefault() in ON_CALL() leads to a run-time failure.
714TEST(DoDefaultTest, CannotBeUsedInOnCall) {
715 MockClass mock;
716 EXPECT_NONFATAL_FAILURE({ // NOLINT
717 ON_CALL(mock, IntFunc(_))
718 .WillByDefault(DoDefault());
719 }, "DoDefault() cannot be used in ON_CALL()");
720}
721
zhanyong.wan59214832010-10-05 05:58:51 +0000722// Tests that SetArgPointee<N>(v) sets the variable pointed to by
723// the N-th (0-based) argument to v.
724TEST(SetArgPointeeTest, SetsTheNthPointee) {
725 typedef void MyFunction(bool, int*, char*);
726 Action<MyFunction> a = SetArgPointee<1>(2);
727
728 int n = 0;
729 char ch = '\0';
730 a.Perform(make_tuple(true, &n, &ch));
731 EXPECT_EQ(2, n);
732 EXPECT_EQ('\0', ch);
733
734 a = SetArgPointee<2>('a');
735 n = 0;
736 ch = '\0';
737 a.Perform(make_tuple(true, &n, &ch));
738 EXPECT_EQ(0, n);
739 EXPECT_EQ('a', ch);
740}
741
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000742#if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN)
zhanyong.wana684b5a2010-12-02 23:30:50 +0000743// Tests that SetArgPointee<N>() accepts a string literal.
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000744// GCC prior to v4.0 and the Symbian compiler do not support this.
zhanyong.wana684b5a2010-12-02 23:30:50 +0000745TEST(SetArgPointeeTest, AcceptsStringLiteral) {
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000746 typedef void MyFunction(std::string*, const char**);
747 Action<MyFunction> a = SetArgPointee<0>("hi");
zhanyong.wana684b5a2010-12-02 23:30:50 +0000748 std::string str;
749 const char* ptr = NULL;
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000750 a.Perform(make_tuple(&str, &ptr));
zhanyong.wana684b5a2010-12-02 23:30:50 +0000751 EXPECT_EQ("hi", str);
752 EXPECT_TRUE(ptr == NULL);
753
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000754 a = SetArgPointee<1>("world");
zhanyong.wana684b5a2010-12-02 23:30:50 +0000755 str = "";
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000756 a.Perform(make_tuple(&str, &ptr));
zhanyong.wana684b5a2010-12-02 23:30:50 +0000757 EXPECT_EQ("", str);
758 EXPECT_STREQ("world", ptr);
759}
760
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000761TEST(SetArgPointeeTest, AcceptsWideStringLiteral) {
762 typedef void MyFunction(const wchar_t**);
763 Action<MyFunction> a = SetArgPointee<0>(L"world");
764 const wchar_t* ptr = NULL;
765 a.Perform(make_tuple(&ptr));
766 EXPECT_STREQ(L"world", ptr);
767
768# if GTEST_HAS_STD_WSTRING
769
770 typedef void MyStringFunction(std::wstring*);
771 Action<MyStringFunction> a2 = SetArgPointee<0>(L"world");
772 std::wstring str = L"";
773 a2.Perform(make_tuple(&str));
774 EXPECT_EQ(L"world", str);
775
776# endif
777}
778#endif
779
zhanyong.wana684b5a2010-12-02 23:30:50 +0000780// Tests that SetArgPointee<N>() accepts a char pointer.
781TEST(SetArgPointeeTest, AcceptsCharPointer) {
782 typedef void MyFunction(bool, std::string*, const char**);
783 const char* const hi = "hi";
784 Action<MyFunction> a = SetArgPointee<1>(hi);
785 std::string str;
786 const char* ptr = NULL;
787 a.Perform(make_tuple(true, &str, &ptr));
788 EXPECT_EQ("hi", str);
789 EXPECT_TRUE(ptr == NULL);
790
791 char world_array[] = "world";
792 char* const world = world_array;
793 a = SetArgPointee<2>(world);
794 str = "";
795 a.Perform(make_tuple(true, &str, &ptr));
796 EXPECT_EQ("", str);
797 EXPECT_EQ(world, ptr);
798}
799
zhanyong.wanfc8c6c42011-03-09 01:18:08 +0000800TEST(SetArgPointeeTest, AcceptsWideCharPointer) {
801 typedef void MyFunction(bool, const wchar_t**);
802 const wchar_t* const hi = L"hi";
803 Action<MyFunction> a = SetArgPointee<1>(hi);
804 const wchar_t* ptr = NULL;
805 a.Perform(make_tuple(true, &ptr));
806 EXPECT_EQ(hi, ptr);
807
808# if GTEST_HAS_STD_WSTRING
809
810 typedef void MyStringFunction(bool, std::wstring*);
811 wchar_t world_array[] = L"world";
812 wchar_t* const world = world_array;
813 Action<MyStringFunction> a2 = SetArgPointee<1>(world);
814 std::wstring str;
815 a2.Perform(make_tuple(true, &str));
816 EXPECT_EQ(world_array, str);
817# endif
818}
819
zhanyong.wan59214832010-10-05 05:58:51 +0000820#if GTEST_HAS_PROTOBUF_
821
822// Tests that SetArgPointee<N>(proto_buffer) sets the v1 protobuf
823// variable pointed to by the N-th (0-based) argument to proto_buffer.
824TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
825 TestMessage* const msg = new TestMessage;
826 msg->set_member("yes");
827 TestMessage orig_msg;
828 orig_msg.CopyFrom(*msg);
829
830 Action<void(bool, TestMessage*)> a = SetArgPointee<1>(*msg);
831 // SetArgPointee<N>(proto_buffer) makes a copy of proto_buffer
832 // s.t. the action works even when the original proto_buffer has
833 // died. We ensure this behavior by deleting msg before using the
834 // action.
835 delete msg;
836
837 TestMessage dest;
838 EXPECT_FALSE(orig_msg.Equals(dest));
839 a.Perform(make_tuple(true, &dest));
840 EXPECT_TRUE(orig_msg.Equals(dest));
841}
842
843// Tests that SetArgPointee<N>(proto_buffer) sets the
844// ::ProtocolMessage variable pointed to by the N-th (0-based)
845// argument to proto_buffer.
846TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
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, ::ProtocolMessage*)> 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 ::ProtocolMessage* const dest_base = &dest;
861 EXPECT_FALSE(orig_msg.Equals(dest));
862 a.Perform(make_tuple(true, dest_base));
863 EXPECT_TRUE(orig_msg.Equals(dest));
864}
865
866// Tests that SetArgPointee<N>(proto2_buffer) sets the v2
867// protobuf variable pointed to by the N-th (0-based) argument to
868// proto2_buffer.
869TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
870 using testing::internal::FooMessage;
871 FooMessage* const msg = new FooMessage;
872 msg->set_int_field(2);
873 msg->set_string_field("hi");
874 FooMessage orig_msg;
875 orig_msg.CopyFrom(*msg);
876
877 Action<void(bool, FooMessage*)> a = SetArgPointee<1>(*msg);
878 // SetArgPointee<N>(proto2_buffer) makes a copy of
879 // proto2_buffer s.t. the action works even when the original
880 // proto2_buffer has died. We ensure this behavior by deleting msg
881 // before using the action.
882 delete msg;
883
884 FooMessage dest;
885 dest.set_int_field(0);
886 a.Perform(make_tuple(true, &dest));
887 EXPECT_EQ(2, dest.int_field());
888 EXPECT_EQ("hi", dest.string_field());
889}
890
891// Tests that SetArgPointee<N>(proto2_buffer) sets the
892// proto2::Message variable pointed to by the N-th (0-based) argument
893// to proto2_buffer.
894TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
895 using testing::internal::FooMessage;
896 FooMessage* const msg = new FooMessage;
897 msg->set_int_field(2);
898 msg->set_string_field("hi");
899 FooMessage orig_msg;
900 orig_msg.CopyFrom(*msg);
901
902 Action<void(bool, ::proto2::Message*)> a = SetArgPointee<1>(*msg);
903 // SetArgPointee<N>(proto2_buffer) makes a copy of
904 // proto2_buffer s.t. the action works even when the original
905 // proto2_buffer has died. We ensure this behavior by deleting msg
906 // before using the action.
907 delete msg;
908
909 FooMessage dest;
910 dest.set_int_field(0);
911 ::proto2::Message* const dest_base = &dest;
912 a.Perform(make_tuple(true, dest_base));
913 EXPECT_EQ(2, dest.int_field());
914 EXPECT_EQ("hi", dest.string_field());
915}
916
917#endif // GTEST_HAS_PROTOBUF_
918
shiqiane35fdd92008-12-10 05:08:54 +0000919// Tests that SetArgumentPointee<N>(v) sets the variable pointed to by
920// the N-th (0-based) argument to v.
921TEST(SetArgumentPointeeTest, SetsTheNthPointee) {
922 typedef void MyFunction(bool, int*, char*);
923 Action<MyFunction> a = SetArgumentPointee<1>(2);
924
925 int n = 0;
926 char ch = '\0';
927 a.Perform(make_tuple(true, &n, &ch));
928 EXPECT_EQ(2, n);
929 EXPECT_EQ('\0', ch);
930
931 a = SetArgumentPointee<2>('a');
932 n = 0;
933 ch = '\0';
934 a.Perform(make_tuple(true, &n, &ch));
935 EXPECT_EQ(0, n);
936 EXPECT_EQ('a', ch);
937}
938
zhanyong.wan02f71062010-05-10 17:14:29 +0000939#if GTEST_HAS_PROTOBUF_
shiqiane35fdd92008-12-10 05:08:54 +0000940
zhanyong.wanc6a41232009-05-13 23:38:40 +0000941// Tests that SetArgumentPointee<N>(proto_buffer) sets the v1 protobuf
942// variable pointed to by the N-th (0-based) argument to proto_buffer.
shiqiane35fdd92008-12-10 05:08:54 +0000943TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
shiqiane35fdd92008-12-10 05:08:54 +0000944 TestMessage* const msg = new TestMessage;
945 msg->set_member("yes");
946 TestMessage orig_msg;
947 orig_msg.CopyFrom(*msg);
948
zhanyong.wanc6a41232009-05-13 23:38:40 +0000949 Action<void(bool, TestMessage*)> a = SetArgumentPointee<1>(*msg);
shiqiane35fdd92008-12-10 05:08:54 +0000950 // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
951 // s.t. the action works even when the original proto_buffer has
952 // died. We ensure this behavior by deleting msg before using the
953 // action.
954 delete msg;
955
956 TestMessage dest;
957 EXPECT_FALSE(orig_msg.Equals(dest));
958 a.Perform(make_tuple(true, &dest));
959 EXPECT_TRUE(orig_msg.Equals(dest));
960}
961
zhanyong.wanc6a41232009-05-13 23:38:40 +0000962// Tests that SetArgumentPointee<N>(proto_buffer) sets the
963// ::ProtocolMessage variable pointed to by the N-th (0-based)
964// argument to proto_buffer.
965TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
966 TestMessage* const msg = new TestMessage;
967 msg->set_member("yes");
968 TestMessage orig_msg;
969 orig_msg.CopyFrom(*msg);
970
971 Action<void(bool, ::ProtocolMessage*)> a = SetArgumentPointee<1>(*msg);
972 // 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 ::ProtocolMessage* const dest_base = &dest;
980 EXPECT_FALSE(orig_msg.Equals(dest));
981 a.Perform(make_tuple(true, dest_base));
982 EXPECT_TRUE(orig_msg.Equals(dest));
983}
984
985// Tests that SetArgumentPointee<N>(proto2_buffer) sets the v2
986// protobuf variable pointed to by the N-th (0-based) argument to
987// proto2_buffer.
shiqiane35fdd92008-12-10 05:08:54 +0000988TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
989 using testing::internal::FooMessage;
shiqiane35fdd92008-12-10 05:08:54 +0000990 FooMessage* const msg = new FooMessage;
991 msg->set_int_field(2);
992 msg->set_string_field("hi");
993 FooMessage orig_msg;
994 orig_msg.CopyFrom(*msg);
995
zhanyong.wanc6a41232009-05-13 23:38:40 +0000996 Action<void(bool, FooMessage*)> a = SetArgumentPointee<1>(*msg);
shiqiane35fdd92008-12-10 05:08:54 +0000997 // SetArgumentPointee<N>(proto2_buffer) makes a copy of
998 // proto2_buffer s.t. the action works even when the original
999 // proto2_buffer has died. We ensure this behavior by deleting msg
1000 // before using the action.
1001 delete msg;
1002
1003 FooMessage dest;
1004 dest.set_int_field(0);
1005 a.Perform(make_tuple(true, &dest));
1006 EXPECT_EQ(2, dest.int_field());
1007 EXPECT_EQ("hi", dest.string_field());
1008}
1009
zhanyong.wanc6a41232009-05-13 23:38:40 +00001010// Tests that SetArgumentPointee<N>(proto2_buffer) sets the
1011// proto2::Message variable pointed to by the N-th (0-based) argument
1012// to proto2_buffer.
1013TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
1014 using testing::internal::FooMessage;
1015 FooMessage* const msg = new FooMessage;
1016 msg->set_int_field(2);
1017 msg->set_string_field("hi");
1018 FooMessage orig_msg;
1019 orig_msg.CopyFrom(*msg);
1020
1021 Action<void(bool, ::proto2::Message*)> a = SetArgumentPointee<1>(*msg);
1022 // SetArgumentPointee<N>(proto2_buffer) makes a copy of
1023 // proto2_buffer s.t. the action works even when the original
1024 // proto2_buffer has died. We ensure this behavior by deleting msg
1025 // before using the action.
1026 delete msg;
1027
1028 FooMessage dest;
1029 dest.set_int_field(0);
1030 ::proto2::Message* const dest_base = &dest;
1031 a.Perform(make_tuple(true, dest_base));
1032 EXPECT_EQ(2, dest.int_field());
1033 EXPECT_EQ("hi", dest.string_field());
1034}
1035
zhanyong.wan02f71062010-05-10 17:14:29 +00001036#endif // GTEST_HAS_PROTOBUF_
shiqiane35fdd92008-12-10 05:08:54 +00001037
shiqiane35fdd92008-12-10 05:08:54 +00001038// Sample functions and functors for testing Invoke() and etc.
1039int Nullary() { return 1; }
1040
1041class NullaryFunctor {
1042 public:
1043 int operator()() { return 2; }
1044};
1045
1046bool g_done = false;
1047void VoidNullary() { g_done = true; }
1048
1049class VoidNullaryFunctor {
1050 public:
1051 void operator()() { g_done = true; }
1052};
1053
shiqiane35fdd92008-12-10 05:08:54 +00001054class Foo {
1055 public:
1056 Foo() : value_(123) {}
1057
1058 int Nullary() const { return value_; }
zhanyong.wan29be9232013-03-01 06:53:35 +00001059
shiqiane35fdd92008-12-10 05:08:54 +00001060 private:
1061 int value_;
1062};
1063
1064// Tests InvokeWithoutArgs(function).
1065TEST(InvokeWithoutArgsTest, Function) {
1066 // As an action that takes one argument.
1067 Action<int(int)> a = InvokeWithoutArgs(Nullary); // NOLINT
1068 EXPECT_EQ(1, a.Perform(make_tuple(2)));
1069
1070 // As an action that takes two arguments.
zhanyong.wan32de5f52009-12-23 00:13:23 +00001071 Action<int(int, double)> a2 = InvokeWithoutArgs(Nullary); // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00001072 EXPECT_EQ(1, a2.Perform(make_tuple(2, 3.5)));
1073
1074 // As an action that returns void.
1075 Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary); // NOLINT
1076 g_done = false;
1077 a3.Perform(make_tuple(1));
1078 EXPECT_TRUE(g_done);
1079}
1080
1081// Tests InvokeWithoutArgs(functor).
1082TEST(InvokeWithoutArgsTest, Functor) {
1083 // As an action that takes no argument.
1084 Action<int()> a = InvokeWithoutArgs(NullaryFunctor()); // NOLINT
1085 EXPECT_EQ(2, a.Perform(make_tuple()));
1086
1087 // As an action that takes three arguments.
zhanyong.wan32de5f52009-12-23 00:13:23 +00001088 Action<int(int, double, char)> a2 = // NOLINT
shiqiane35fdd92008-12-10 05:08:54 +00001089 InvokeWithoutArgs(NullaryFunctor());
1090 EXPECT_EQ(2, a2.Perform(make_tuple(3, 3.5, 'a')));
1091
1092 // As an action that returns void.
1093 Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor());
1094 g_done = false;
1095 a3.Perform(make_tuple());
1096 EXPECT_TRUE(g_done);
1097}
1098
1099// Tests InvokeWithoutArgs(obj_ptr, method).
1100TEST(InvokeWithoutArgsTest, Method) {
1101 Foo foo;
1102 Action<int(bool, char)> a = // NOLINT
1103 InvokeWithoutArgs(&foo, &Foo::Nullary);
1104 EXPECT_EQ(123, a.Perform(make_tuple(true, 'a')));
1105}
1106
1107// Tests using IgnoreResult() on a polymorphic action.
1108TEST(IgnoreResultTest, PolymorphicAction) {
1109 Action<void(int)> a = IgnoreResult(Return(5)); // NOLINT
1110 a.Perform(make_tuple(1));
1111}
1112
1113// Tests using IgnoreResult() on a monomorphic action.
1114
1115int ReturnOne() {
1116 g_done = true;
1117 return 1;
1118}
1119
1120TEST(IgnoreResultTest, MonomorphicAction) {
1121 g_done = false;
1122 Action<void()> a = IgnoreResult(Invoke(ReturnOne));
1123 a.Perform(make_tuple());
1124 EXPECT_TRUE(g_done);
1125}
1126
1127// Tests using IgnoreResult() on an action that returns a class type.
1128
zhanyong.wan32de5f52009-12-23 00:13:23 +00001129MyClass ReturnMyClass(double /* x */) {
shiqiane35fdd92008-12-10 05:08:54 +00001130 g_done = true;
1131 return MyClass();
1132}
1133
1134TEST(IgnoreResultTest, ActionReturningClass) {
1135 g_done = false;
1136 Action<void(int)> a = IgnoreResult(Invoke(ReturnMyClass)); // NOLINT
1137 a.Perform(make_tuple(2));
1138 EXPECT_TRUE(g_done);
1139}
1140
1141TEST(AssignTest, Int) {
1142 int x = 0;
1143 Action<void(int)> a = Assign(&x, 5);
1144 a.Perform(make_tuple(0));
1145 EXPECT_EQ(5, x);
1146}
1147
1148TEST(AssignTest, String) {
1149 ::std::string x;
1150 Action<void(void)> a = Assign(&x, "Hello, world");
1151 a.Perform(make_tuple());
1152 EXPECT_EQ("Hello, world", x);
1153}
1154
1155TEST(AssignTest, CompatibleTypes) {
1156 double x = 0;
1157 Action<void(int)> a = Assign(&x, 5);
1158 a.Perform(make_tuple(0));
1159 EXPECT_DOUBLE_EQ(5, x);
1160}
1161
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001162#if !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001163
shiqiane35fdd92008-12-10 05:08:54 +00001164class SetErrnoAndReturnTest : public testing::Test {
1165 protected:
1166 virtual void SetUp() { errno = 0; }
1167 virtual void TearDown() { errno = 0; }
1168};
1169
1170TEST_F(SetErrnoAndReturnTest, Int) {
1171 Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5);
1172 EXPECT_EQ(-5, a.Perform(make_tuple()));
1173 EXPECT_EQ(ENOTTY, errno);
1174}
1175
1176TEST_F(SetErrnoAndReturnTest, Ptr) {
1177 int x;
1178 Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x);
1179 EXPECT_EQ(&x, a.Perform(make_tuple()));
1180 EXPECT_EQ(ENOTTY, errno);
1181}
1182
1183TEST_F(SetErrnoAndReturnTest, CompatibleTypes) {
1184 Action<double()> a = SetErrnoAndReturn(EINVAL, 5);
1185 EXPECT_DOUBLE_EQ(5.0, a.Perform(make_tuple()));
1186 EXPECT_EQ(EINVAL, errno);
1187}
1188
zhanyong.wanf7af24c2009-09-24 21:17:24 +00001189#endif // !GTEST_OS_WINDOWS_MOBILE
zhanyong.wan5b5d62f2009-03-11 23:37:56 +00001190
zhanyong.wana18423e2009-07-22 23:58:19 +00001191// Tests ByRef().
1192
1193// Tests that ReferenceWrapper<T> is copyable.
1194TEST(ByRefTest, IsCopyable) {
1195 const std::string s1 = "Hi";
1196 const std::string s2 = "Hello";
1197
jgm79a367e2012-04-10 16:02:11 +00001198 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper =
1199 ByRef(s1);
zhanyong.wana18423e2009-07-22 23:58:19 +00001200 const std::string& r1 = ref_wrapper;
1201 EXPECT_EQ(&s1, &r1);
1202
1203 // Assigns a new value to ref_wrapper.
1204 ref_wrapper = ByRef(s2);
1205 const std::string& r2 = ref_wrapper;
1206 EXPECT_EQ(&s2, &r2);
1207
jgm79a367e2012-04-10 16:02:11 +00001208 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper1 =
1209 ByRef(s1);
zhanyong.wana18423e2009-07-22 23:58:19 +00001210 // Copies ref_wrapper1 to ref_wrapper.
1211 ref_wrapper = ref_wrapper1;
1212 const std::string& r3 = ref_wrapper;
1213 EXPECT_EQ(&s1, &r3);
1214}
1215
1216// Tests using ByRef() on a const value.
1217TEST(ByRefTest, ConstValue) {
1218 const int n = 0;
1219 // int& ref = ByRef(n); // This shouldn't compile - we have a
1220 // negative compilation test to catch it.
1221 const int& const_ref = ByRef(n);
1222 EXPECT_EQ(&n, &const_ref);
1223}
1224
1225// Tests using ByRef() on a non-const value.
1226TEST(ByRefTest, NonConstValue) {
1227 int n = 0;
1228
1229 // ByRef(n) can be used as either an int&,
1230 int& ref = ByRef(n);
1231 EXPECT_EQ(&n, &ref);
1232
1233 // or a const int&.
1234 const int& const_ref = ByRef(n);
1235 EXPECT_EQ(&n, &const_ref);
1236}
1237
1238// Tests explicitly specifying the type when using ByRef().
1239TEST(ByRefTest, ExplicitType) {
1240 int n = 0;
1241 const int& r1 = ByRef<const int>(n);
1242 EXPECT_EQ(&n, &r1);
1243
1244 // ByRef<char>(n); // This shouldn't compile - we have a negative
1245 // compilation test to catch it.
1246
1247 Derived d;
1248 Derived& r2 = ByRef<Derived>(d);
1249 EXPECT_EQ(&d, &r2);
1250
1251 const Derived& r3 = ByRef<const Derived>(d);
1252 EXPECT_EQ(&d, &r3);
1253
1254 Base& r4 = ByRef<Base>(d);
1255 EXPECT_EQ(&d, &r4);
1256
1257 const Base& r5 = ByRef<const Base>(d);
1258 EXPECT_EQ(&d, &r5);
1259
1260 // The following shouldn't compile - we have a negative compilation
1261 // test for it.
1262 //
1263 // Base b;
1264 // ByRef<Derived>(b);
1265}
1266
1267// Tests that Google Mock prints expression ByRef(x) as a reference to x.
1268TEST(ByRefTest, PrintsCorrectly) {
1269 int n = 42;
1270 ::std::stringstream expected, actual;
1271 testing::internal::UniversalPrinter<const int&>::Print(n, &expected);
1272 testing::internal::UniversalPrint(ByRef(n), &actual);
1273 EXPECT_EQ(expected.str(), actual.str());
1274}
1275
kosakb5c81092014-01-29 06:41:44 +00001276#if GTEST_LANG_CXX11
1277
1278std::unique_ptr<int> UniquePtrSource() {
1279 return std::unique_ptr<int>(new int(19));
1280}
1281
1282std::vector<std::unique_ptr<int>> VectorUniquePtrSource() {
1283 std::vector<std::unique_ptr<int>> out;
1284 out.emplace_back(new int(7));
1285 return out;
1286}
1287
1288TEST(MockMethodTest, CanReturnMoveOnlyValue) {
1289 MockClass mock;
1290
1291 // Check default value
1292 DefaultValue<std::unique_ptr<int>>::SetFactory([] {
1293 return std::unique_ptr<int>(new int(42));
1294 });
1295 EXPECT_EQ(42, *mock.MakeUnique());
1296
1297 EXPECT_CALL(mock, MakeUnique())
1298 .WillRepeatedly(Invoke(UniquePtrSource));
1299 EXPECT_CALL(mock, MakeVectorUnique())
1300 .WillRepeatedly(Invoke(VectorUniquePtrSource));
1301 std::unique_ptr<int> result1 = mock.MakeUnique();
1302 EXPECT_EQ(19, *result1);
1303 std::unique_ptr<int> result2 = mock.MakeUnique();
1304 EXPECT_EQ(19, *result2);
1305 EXPECT_NE(result1, result2);
1306
1307 std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
1308 EXPECT_EQ(1, vresult.size());
1309 EXPECT_NE(nullptr, vresult[0]);
1310 EXPECT_EQ(7, *vresult[0]);
1311}
1312
1313#endif // GTEST_LANG_CXX11
1314
shiqiane35fdd92008-12-10 05:08:54 +00001315} // Unnamed namespace