1 // Copyright 2007, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 // Author: wan@google.com (Zhanyong Wan)
32 // Google Mock - a framework for writing C++ mock classes.
34 // This file tests the built-in actions.
36 #include "gmock/gmock-actions.h"
41 #include "gmock/gmock.h"
42 #include "gmock/internal/gmock-port.h"
43 #include "gtest/gtest.h"
44 #include "gtest/gtest-spi.h"
49 using testing::make_tuple;
51 using testing::tuple_element;
52 using testing::internal::BuiltInDefaultValue;
53 using testing::internal::Int64;
54 using testing::internal::UInt64;
55 // This list should be kept sorted.
57 using testing::Action;
58 using testing::ActionInterface;
59 using testing::Assign;
61 using testing::DefaultValue;
62 using testing::DoDefault;
63 using testing::IgnoreResult;
64 using testing::Invoke;
65 using testing::InvokeWithoutArgs;
66 using testing::MakePolymorphicAction;
68 using testing::PolymorphicAction;
69 using testing::Return;
70 using testing::ReturnNull;
71 using testing::ReturnRef;
72 using testing::ReturnRefOfCopy;
73 using testing::SetArgPointee;
74 using testing::SetArgumentPointee;
76 #if !GTEST_OS_WINDOWS_MOBILE
77 using testing::SetErrnoAndReturn;
80 #if GTEST_HAS_PROTOBUF_
81 using testing::internal::TestMessage;
82 #endif // GTEST_HAS_PROTOBUF_
84 // Tests that BuiltInDefaultValue<T*>::Get() returns NULL.
85 TEST(BuiltInDefaultValueTest, IsNullForPointerTypes) {
86 EXPECT_TRUE(BuiltInDefaultValue<int*>::Get() == NULL);
87 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Get() == NULL);
88 EXPECT_TRUE(BuiltInDefaultValue<void*>::Get() == NULL);
91 // Tests that BuiltInDefaultValue<T*>::Exists() return true.
92 TEST(BuiltInDefaultValueTest, ExistsForPointerTypes) {
93 EXPECT_TRUE(BuiltInDefaultValue<int*>::Exists());
94 EXPECT_TRUE(BuiltInDefaultValue<const char*>::Exists());
95 EXPECT_TRUE(BuiltInDefaultValue<void*>::Exists());
98 // Tests that BuiltInDefaultValue<T>::Get() returns 0 when T is a
99 // built-in numeric type.
100 TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) {
101 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned char>::Get());
102 EXPECT_EQ(0, BuiltInDefaultValue<signed char>::Get());
103 EXPECT_EQ(0, BuiltInDefaultValue<char>::Get());
104 #if GMOCK_HAS_SIGNED_WCHAR_T_
105 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned wchar_t>::Get());
106 EXPECT_EQ(0, BuiltInDefaultValue<signed wchar_t>::Get());
108 #if GMOCK_WCHAR_T_IS_NATIVE_
109 EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get());
111 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned short>::Get()); // NOLINT
112 EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get()); // NOLINT
113 EXPECT_EQ(0, BuiltInDefaultValue<short>::Get()); // NOLINT
114 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned int>::Get());
115 EXPECT_EQ(0, BuiltInDefaultValue<signed int>::Get());
116 EXPECT_EQ(0, BuiltInDefaultValue<int>::Get());
117 EXPECT_EQ(0U, BuiltInDefaultValue<unsigned long>::Get()); // NOLINT
118 EXPECT_EQ(0, BuiltInDefaultValue<signed long>::Get()); // NOLINT
119 EXPECT_EQ(0, BuiltInDefaultValue<long>::Get()); // NOLINT
120 EXPECT_EQ(0U, BuiltInDefaultValue<UInt64>::Get());
121 EXPECT_EQ(0, BuiltInDefaultValue<Int64>::Get());
122 EXPECT_EQ(0, BuiltInDefaultValue<float>::Get());
123 EXPECT_EQ(0, BuiltInDefaultValue<double>::Get());
126 // Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
127 // built-in numeric type.
128 TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) {
129 EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists());
130 EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists());
131 EXPECT_TRUE(BuiltInDefaultValue<char>::Exists());
132 #if GMOCK_HAS_SIGNED_WCHAR_T_
133 EXPECT_TRUE(BuiltInDefaultValue<unsigned wchar_t>::Exists());
134 EXPECT_TRUE(BuiltInDefaultValue<signed wchar_t>::Exists());
136 #if GMOCK_WCHAR_T_IS_NATIVE_
137 EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists());
139 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());
154 // Tests that BuiltInDefaultValue<bool>::Get() returns false.
155 TEST(BuiltInDefaultValueTest, IsFalseForBool) {
156 EXPECT_FALSE(BuiltInDefaultValue<bool>::Get());
159 // Tests that BuiltInDefaultValue<bool>::Exists() returns true.
160 TEST(BuiltInDefaultValueTest, BoolExists) {
161 EXPECT_TRUE(BuiltInDefaultValue<bool>::Exists());
164 // Tests that BuiltInDefaultValue<T>::Get() returns "" when T is a
166 TEST(BuiltInDefaultValueTest, IsEmptyStringForString) {
167 #if GTEST_HAS_GLOBAL_STRING
168 EXPECT_EQ("", BuiltInDefaultValue< ::string>::Get());
169 #endif // GTEST_HAS_GLOBAL_STRING
171 EXPECT_EQ("", BuiltInDefaultValue< ::std::string>::Get());
174 // Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
176 TEST(BuiltInDefaultValueTest, ExistsForString) {
177 #if GTEST_HAS_GLOBAL_STRING
178 EXPECT_TRUE(BuiltInDefaultValue< ::string>::Exists());
179 #endif // GTEST_HAS_GLOBAL_STRING
181 EXPECT_TRUE(BuiltInDefaultValue< ::std::string>::Exists());
184 // Tests that BuiltInDefaultValue<const T>::Get() returns the same
185 // value as BuiltInDefaultValue<T>::Get() does.
186 TEST(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());
193 // Tests that BuiltInDefaultValue<T>::Get() aborts the program with
194 // the correct error message when T is a user-defined type.
196 UserType() : value(0) {}
201 TEST(BuiltInDefaultValueTest, UserTypeHasNoDefault) {
202 EXPECT_FALSE(BuiltInDefaultValue<UserType>::Exists());
205 // Tests that BuiltInDefaultValue<T&>::Get() aborts the program.
206 TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) {
207 EXPECT_DEATH_IF_SUPPORTED({
208 BuiltInDefaultValue<int&>::Get();
210 EXPECT_DEATH_IF_SUPPORTED({
211 BuiltInDefaultValue<const char&>::Get();
215 TEST(BuiltInDefaultValueDeathTest, IsUndefinedForUserTypes) {
216 EXPECT_DEATH_IF_SUPPORTED({
217 BuiltInDefaultValue<UserType>::Get();
221 // Tests that DefaultValue<T>::IsSet() is false initially.
222 TEST(DefaultValueTest, IsInitiallyUnset) {
223 EXPECT_FALSE(DefaultValue<int>::IsSet());
224 EXPECT_FALSE(DefaultValue<const UserType>::IsSet());
227 // Tests that DefaultValue<T> can be set and then unset.
228 TEST(DefaultValueTest, CanBeSetAndUnset) {
229 EXPECT_TRUE(DefaultValue<int>::Exists());
230 EXPECT_FALSE(DefaultValue<const UserType>::Exists());
232 DefaultValue<int>::Set(1);
233 DefaultValue<const UserType>::Set(UserType());
235 EXPECT_EQ(1, DefaultValue<int>::Get());
236 EXPECT_EQ(0, DefaultValue<const UserType>::Get().value);
238 EXPECT_TRUE(DefaultValue<int>::Exists());
239 EXPECT_TRUE(DefaultValue<const UserType>::Exists());
241 DefaultValue<int>::Clear();
242 DefaultValue<const UserType>::Clear();
244 EXPECT_FALSE(DefaultValue<int>::IsSet());
245 EXPECT_FALSE(DefaultValue<const UserType>::IsSet());
247 EXPECT_TRUE(DefaultValue<int>::Exists());
248 EXPECT_FALSE(DefaultValue<const UserType>::Exists());
251 // Tests that DefaultValue<T>::Get() returns the
252 // BuiltInDefaultValue<T>::Get() when DefaultValue<T>::IsSet() is
254 TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
255 EXPECT_FALSE(DefaultValue<int>::IsSet());
256 EXPECT_TRUE(DefaultValue<int>::Exists());
257 EXPECT_FALSE(DefaultValue<UserType>::IsSet());
258 EXPECT_FALSE(DefaultValue<UserType>::Exists());
260 EXPECT_EQ(0, DefaultValue<int>::Get());
262 EXPECT_DEATH_IF_SUPPORTED({
263 DefaultValue<UserType>::Get();
268 TEST(DefaultValueDeathTest, GetWorksForMoveOnlyIfSet) {
269 EXPECT_FALSE(DefaultValue<std::unique_ptr<int>>::Exists());
270 EXPECT_DEATH_IF_SUPPORTED({
271 DefaultValue<std::unique_ptr<int>>::Get();
273 DefaultValue<std::unique_ptr<int>>::SetFactory([] {
274 return std::unique_ptr<int>(new int(42));
276 EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Exists());
277 std::unique_ptr<int> i = DefaultValue<std::unique_ptr<int>>::Get();
280 #endif // GTEST_LANG_CXX11
282 // Tests that DefaultValue<void>::Get() returns void.
283 TEST(DefaultValueTest, GetWorksForVoid) {
284 return DefaultValue<void>::Get();
287 // Tests using DefaultValue with a reference type.
289 // Tests that DefaultValue<T&>::IsSet() is false initially.
290 TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) {
291 EXPECT_FALSE(DefaultValue<int&>::IsSet());
292 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
295 // Tests that DefaultValue<T&>::Exists is false initiallly.
296 TEST(DefaultValueOfReferenceTest, IsInitiallyNotExisting) {
297 EXPECT_FALSE(DefaultValue<int&>::Exists());
298 EXPECT_FALSE(DefaultValue<UserType&>::Exists());
301 // Tests that DefaultValue<T&> can be set and then unset.
302 TEST(DefaultValueOfReferenceTest, CanBeSetAndUnset) {
304 DefaultValue<const int&>::Set(n);
306 DefaultValue<UserType&>::Set(u);
308 EXPECT_TRUE(DefaultValue<const int&>::Exists());
309 EXPECT_TRUE(DefaultValue<UserType&>::Exists());
311 EXPECT_EQ(&n, &(DefaultValue<const int&>::Get()));
312 EXPECT_EQ(&u, &(DefaultValue<UserType&>::Get()));
314 DefaultValue<const int&>::Clear();
315 DefaultValue<UserType&>::Clear();
317 EXPECT_FALSE(DefaultValue<const int&>::Exists());
318 EXPECT_FALSE(DefaultValue<UserType&>::Exists());
320 EXPECT_FALSE(DefaultValue<const int&>::IsSet());
321 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
324 // Tests that DefaultValue<T&>::Get() returns the
325 // BuiltInDefaultValue<T&>::Get() when DefaultValue<T&>::IsSet() is
327 TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
328 EXPECT_FALSE(DefaultValue<int&>::IsSet());
329 EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
331 EXPECT_DEATH_IF_SUPPORTED({
332 DefaultValue<int&>::Get();
334 EXPECT_DEATH_IF_SUPPORTED({
335 DefaultValue<UserType>::Get();
339 // Tests that ActionInterface can be implemented by defining the
342 typedef int MyGlobalFunction(bool, int);
344 class MyActionImpl : public ActionInterface<MyGlobalFunction> {
346 virtual int Perform(const tuple<bool, int>& args) {
347 return get<0>(args) ? get<1>(args) : 0;
351 TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) {
352 MyActionImpl my_action_impl;
353 (void)my_action_impl;
356 TEST(ActionInterfaceTest, MakeAction) {
357 Action<MyGlobalFunction> action = MakeAction(new MyActionImpl);
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)));
367 // Tests that Action<F> can be contructed from a pointer to
368 // ActionInterface<F>.
369 TEST(ActionTest, CanBeConstructedFromActionInterface) {
370 Action<MyGlobalFunction> action(new MyActionImpl);
373 // Tests that Action<F> delegates actual work to ActionInterface<F>.
374 TEST(ActionTest, DelegatesWorkToActionInterface) {
375 const Action<MyGlobalFunction> action(new MyActionImpl);
377 EXPECT_EQ(5, action.Perform(make_tuple(true, 5)));
378 EXPECT_EQ(0, action.Perform(make_tuple(false, 1)));
381 // Tests that Action<F> can be copied.
382 TEST(ActionTest, IsCopyable) {
383 Action<MyGlobalFunction> a1(new MyActionImpl);
384 Action<MyGlobalFunction> a2(a1); // Tests the copy constructor.
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)));
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)));
394 a2 = a1; // Tests the assignment operator.
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)));
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)));
405 // Tests that an Action<From> object can be converted to a
406 // compatible Action<To> object.
408 class IsNotZero : public ActionInterface<bool(int)> { // NOLINT
410 virtual bool Perform(const tuple<int>& arg) {
411 return get<0>(arg) != 0;
415 #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.
421 TEST(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')));
427 #endif // !GTEST_OS_SYMBIAN
429 // The following two classes are for testing MakePolymorphicAction().
431 // Implements a polymorphic action that returns the second of the
432 // arguments it receives.
433 class ReturnSecondArgumentAction {
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); }
442 // Implements a polymorphic action that can be used in a nullary
443 // function to return 0.
444 class ReturnZeroFromNullaryFunctionAction {
446 // For testing that MakePolymorphicAction() works when the
447 // implementation class' Perform() method template takes only one
448 // template parameter.
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; }
457 // These functions verify that MakePolymorphicAction() returns a
458 // PolymorphicAction<T> where T is the argument's type.
460 PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() {
461 return MakePolymorphicAction(ReturnSecondArgumentAction());
464 PolymorphicAction<ReturnZeroFromNullaryFunctionAction>
465 ReturnZeroFromNullaryFunction() {
466 return MakePolymorphicAction(ReturnZeroFromNullaryFunctionAction());
469 // Tests that MakePolymorphicAction() turns a polymorphic action
470 // implementation class into a polymorphic action.
471 TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) {
472 Action<int(bool, int, double)> a1 = ReturnSecondArgument(); // NOLINT
473 EXPECT_EQ(5, a1.Perform(make_tuple(false, 5, 2.0)));
476 // Tests that MakePolymorphicAction() works when the implementation
477 // class' Perform() method template has only one template parameter.
478 TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) {
479 Action<int()> a1 = ReturnZeroFromNullaryFunction();
480 EXPECT_EQ(0, a1.Perform(make_tuple()));
482 Action<void*()> a2 = ReturnZeroFromNullaryFunction();
483 EXPECT_TRUE(a2.Perform(make_tuple()) == NULL);
486 // Tests that Return() works as an action for void-returning
488 TEST(ReturnTest, WorksForVoid) {
489 const Action<void(int)> ret = Return(); // NOLINT
490 return ret.Perform(make_tuple(1));
493 // Tests that Return(v) returns v.
494 TEST(ReturnTest, ReturnsGivenValue) {
495 Action<int()> ret = Return(1); // NOLINT
496 EXPECT_EQ(1, ret.Perform(make_tuple()));
499 EXPECT_EQ(-5, ret.Perform(make_tuple()));
502 // Tests that Return("string literal") works.
503 TEST(ReturnTest, AcceptsStringLiteral) {
504 Action<const char*()> a1 = Return("Hello");
505 EXPECT_STREQ("Hello", a1.Perform(make_tuple()));
507 Action<std::string()> a2 = Return("world");
508 EXPECT_EQ("world", a2.Perform(make_tuple()));
511 // Tests that Return(v) is covaraint.
514 bool operator==(const Base&) { return true; }
517 struct Derived : public Base {
518 bool operator==(const Derived&) { return true; }
521 TEST(ReturnTest, IsCovariant) {
524 Action<Base*()> ret = Return(&base);
525 EXPECT_EQ(&base, ret.Perform(make_tuple()));
527 ret = Return(&derived);
528 EXPECT_EQ(&derived, ret.Perform(make_tuple()));
531 // 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.
537 explicit FromType(bool* is_converted) : converted_(is_converted) {}
538 bool* converted() const { return converted_; }
541 bool* const converted_;
543 GTEST_DISALLOW_ASSIGN_(FromType);
548 // Must allow implicit conversion due to use in ImplicitCast_<T>.
549 ToType(const FromType& x) { *x.converted() = true; } // NOLINT
552 TEST(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.";
559 action.Perform(tuple<>());
560 EXPECT_FALSE(converted) << "Action must NOT convert its argument "
561 << "when performed.";
564 class DestinationType {};
568 // Note: a non-const typecast operator.
569 operator DestinationType() { return DestinationType(); }
572 TEST(ReturnTest, CanConvertArgumentUsingNonConstTypeCastOperator) {
574 Action<DestinationType()> action(Return(s));
577 // Tests that ReturnNull() returns NULL in a pointer-returning function.
578 TEST(ReturnNullTest, WorksInPointerReturningFunction) {
579 const Action<int*()> a1 = ReturnNull();
580 EXPECT_TRUE(a1.Perform(make_tuple()) == NULL);
582 const Action<const char*(bool)> a2 = ReturnNull(); // NOLINT
583 EXPECT_TRUE(a2.Perform(make_tuple(true)) == NULL);
586 // Tests that ReturnRef(v) works for reference types.
587 TEST(ReturnRefTest, WorksForReference) {
589 const Action<const int&(bool)> ret = ReturnRef(n); // NOLINT
591 EXPECT_EQ(&n, &ret.Perform(make_tuple(true)));
594 // Tests that ReturnRef(v) is covariant.
595 TEST(ReturnRefTest, IsCovariant) {
598 Action<Base&()> a = ReturnRef(base);
599 EXPECT_EQ(&base, &a.Perform(make_tuple()));
601 a = ReturnRef(derived);
602 EXPECT_EQ(&derived, &a.Perform(make_tuple()));
605 // Tests that ReturnRefOfCopy(v) works for reference types.
606 TEST(ReturnRefOfCopyTest, WorksForReference) {
608 const Action<const int&()> ret = ReturnRefOfCopy(n);
610 EXPECT_NE(&n, &ret.Perform(make_tuple()));
611 EXPECT_EQ(42, ret.Perform(make_tuple()));
614 EXPECT_NE(&n, &ret.Perform(make_tuple()));
615 EXPECT_EQ(42, ret.Perform(make_tuple()));
618 // Tests that ReturnRefOfCopy(v) is covariant.
619 TEST(ReturnRefOfCopyTest, IsCovariant) {
622 Action<Base&()> a = ReturnRefOfCopy(base);
623 EXPECT_NE(&base, &a.Perform(make_tuple()));
625 a = ReturnRefOfCopy(derived);
626 EXPECT_NE(&derived, &a.Perform(make_tuple()));
629 // Tests that DoDefault() does the default action for the mock method.
637 MOCK_METHOD1(IntFunc, int(bool flag)); // NOLINT
638 MOCK_METHOD0(Foo, MyClass());
640 MOCK_METHOD0(MakeUnique, std::unique_ptr<int>());
641 MOCK_METHOD0(MakeVectorUnique, std::vector<std::unique_ptr<int>>());
645 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockClass);
648 // Tests that DoDefault() returns the built-in default value for the
649 // return type by default.
650 TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) {
652 EXPECT_CALL(mock, IntFunc(_))
653 .WillOnce(DoDefault());
654 EXPECT_EQ(0, mock.IntFunc(true));
657 // 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.
659 TEST(DoDefaultDeathTest, DiesForUnknowType) {
661 EXPECT_CALL(mock, Foo())
662 .WillRepeatedly(DoDefault());
663 #if GTEST_HAS_EXCEPTIONS
664 EXPECT_ANY_THROW(mock.Foo());
666 EXPECT_DEATH_IF_SUPPORTED({
672 // Tests that using DoDefault() inside a composite action leads to a
675 void VoidFunc(bool /* flag */) {}
677 TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
679 EXPECT_CALL(mock, IntFunc(_))
680 .WillRepeatedly(DoAll(Invoke(VoidFunc),
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.
687 EXPECT_DEATH_IF_SUPPORTED({
692 // Tests that DoDefault() returns the default value set by
693 // DefaultValue<T>::Set() when it's not overriden by an ON_CALL().
694 TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) {
695 DefaultValue<int>::Set(1);
697 EXPECT_CALL(mock, IntFunc(_))
698 .WillOnce(DoDefault());
699 EXPECT_EQ(1, mock.IntFunc(false));
700 DefaultValue<int>::Clear();
703 // Tests that DoDefault() does the action specified by ON_CALL().
704 TEST(DoDefaultTest, DoesWhatOnCallSpecifies) {
706 ON_CALL(mock, IntFunc(_))
707 .WillByDefault(Return(2));
708 EXPECT_CALL(mock, IntFunc(_))
709 .WillOnce(DoDefault());
710 EXPECT_EQ(2, mock.IntFunc(false));
713 // Tests that using DoDefault() in ON_CALL() leads to a run-time failure.
714 TEST(DoDefaultTest, CannotBeUsedInOnCall) {
716 EXPECT_NONFATAL_FAILURE({ // NOLINT
717 ON_CALL(mock, IntFunc(_))
718 .WillByDefault(DoDefault());
719 }, "DoDefault() cannot be used in ON_CALL()");
722 // Tests that SetArgPointee<N>(v) sets the variable pointed to by
723 // the N-th (0-based) argument to v.
724 TEST(SetArgPointeeTest, SetsTheNthPointee) {
725 typedef void MyFunction(bool, int*, char*);
726 Action<MyFunction> a = SetArgPointee<1>(2);
730 a.Perform(make_tuple(true, &n, &ch));
734 a = SetArgPointee<2>('a');
737 a.Perform(make_tuple(true, &n, &ch));
742 #if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN)
743 // Tests that SetArgPointee<N>() accepts a string literal.
744 // GCC prior to v4.0 and the Symbian compiler do not support this.
745 TEST(SetArgPointeeTest, AcceptsStringLiteral) {
746 typedef void MyFunction(std::string*, const char**);
747 Action<MyFunction> a = SetArgPointee<0>("hi");
749 const char* ptr = NULL;
750 a.Perform(make_tuple(&str, &ptr));
751 EXPECT_EQ("hi", str);
752 EXPECT_TRUE(ptr == NULL);
754 a = SetArgPointee<1>("world");
756 a.Perform(make_tuple(&str, &ptr));
758 EXPECT_STREQ("world", ptr);
761 TEST(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);
768 # if GTEST_HAS_STD_WSTRING
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);
780 // Tests that SetArgPointee<N>() accepts a char pointer.
781 TEST(SetArgPointeeTest, AcceptsCharPointer) {
782 typedef void MyFunction(bool, std::string*, const char**);
783 const char* const hi = "hi";
784 Action<MyFunction> a = SetArgPointee<1>(hi);
786 const char* ptr = NULL;
787 a.Perform(make_tuple(true, &str, &ptr));
788 EXPECT_EQ("hi", str);
789 EXPECT_TRUE(ptr == NULL);
791 char world_array[] = "world";
792 char* const world = world_array;
793 a = SetArgPointee<2>(world);
795 a.Perform(make_tuple(true, &str, &ptr));
797 EXPECT_EQ(world, ptr);
800 TEST(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));
808 # if GTEST_HAS_STD_WSTRING
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);
815 a2.Perform(make_tuple(true, &str));
816 EXPECT_EQ(world_array, str);
820 #if GTEST_HAS_PROTOBUF_
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.
824 TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
825 TestMessage* const msg = new TestMessage;
826 msg->set_member("yes");
827 TestMessage orig_msg;
828 orig_msg.CopyFrom(*msg);
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
838 EXPECT_FALSE(orig_msg.Equals(dest));
839 a.Perform(make_tuple(true, &dest));
840 EXPECT_TRUE(orig_msg.Equals(dest));
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.
846 TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
847 TestMessage* const msg = new TestMessage;
848 msg->set_member("yes");
849 TestMessage orig_msg;
850 orig_msg.CopyFrom(*msg);
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
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));
866 // Tests that SetArgPointee<N>(proto2_buffer) sets the v2
867 // protobuf variable pointed to by the N-th (0-based) argument to
869 TEST(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");
875 orig_msg.CopyFrom(*msg);
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.
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());
891 // Tests that SetArgPointee<N>(proto2_buffer) sets the
892 // proto2::Message variable pointed to by the N-th (0-based) argument
894 TEST(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");
900 orig_msg.CopyFrom(*msg);
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.
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());
917 #endif // GTEST_HAS_PROTOBUF_
919 // Tests that SetArgumentPointee<N>(v) sets the variable pointed to by
920 // the N-th (0-based) argument to v.
921 TEST(SetArgumentPointeeTest, SetsTheNthPointee) {
922 typedef void MyFunction(bool, int*, char*);
923 Action<MyFunction> a = SetArgumentPointee<1>(2);
927 a.Perform(make_tuple(true, &n, &ch));
931 a = SetArgumentPointee<2>('a');
934 a.Perform(make_tuple(true, &n, &ch));
939 #if GTEST_HAS_PROTOBUF_
941 // Tests that SetArgumentPointee<N>(proto_buffer) sets the v1 protobuf
942 // variable pointed to by the N-th (0-based) argument to proto_buffer.
943 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
944 TestMessage* const msg = new TestMessage;
945 msg->set_member("yes");
946 TestMessage orig_msg;
947 orig_msg.CopyFrom(*msg);
949 Action<void(bool, TestMessage*)> a = SetArgumentPointee<1>(*msg);
950 // 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
957 EXPECT_FALSE(orig_msg.Equals(dest));
958 a.Perform(make_tuple(true, &dest));
959 EXPECT_TRUE(orig_msg.Equals(dest));
962 // Tests that SetArgumentPointee<N>(proto_buffer) sets the
963 // ::ProtocolMessage variable pointed to by the N-th (0-based)
964 // argument to proto_buffer.
965 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
966 TestMessage* const msg = new TestMessage;
967 msg->set_member("yes");
968 TestMessage orig_msg;
969 orig_msg.CopyFrom(*msg);
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
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));
985 // Tests that SetArgumentPointee<N>(proto2_buffer) sets the v2
986 // protobuf variable pointed to by the N-th (0-based) argument to
988 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
989 using testing::internal::FooMessage;
990 FooMessage* const msg = new FooMessage;
991 msg->set_int_field(2);
992 msg->set_string_field("hi");
994 orig_msg.CopyFrom(*msg);
996 Action<void(bool, FooMessage*)> a = SetArgumentPointee<1>(*msg);
997 // 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.
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());
1010 // 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.
1013 TEST(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);
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.
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());
1036 #endif // GTEST_HAS_PROTOBUF_
1038 // Sample functions and functors for testing Invoke() and etc.
1039 int Nullary() { return 1; }
1041 class NullaryFunctor {
1043 int operator()() { return 2; }
1046 bool g_done = false;
1047 void VoidNullary() { g_done = true; }
1049 class VoidNullaryFunctor {
1051 void operator()() { g_done = true; }
1056 Foo() : value_(123) {}
1058 int Nullary() const { return value_; }
1064 // Tests InvokeWithoutArgs(function).
1065 TEST(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)));
1070 // As an action that takes two arguments.
1071 Action<int(int, double)> a2 = InvokeWithoutArgs(Nullary); // NOLINT
1072 EXPECT_EQ(1, a2.Perform(make_tuple(2, 3.5)));
1074 // As an action that returns void.
1075 Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary); // NOLINT
1077 a3.Perform(make_tuple(1));
1078 EXPECT_TRUE(g_done);
1081 // Tests InvokeWithoutArgs(functor).
1082 TEST(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()));
1087 // As an action that takes three arguments.
1088 Action<int(int, double, char)> a2 = // NOLINT
1089 InvokeWithoutArgs(NullaryFunctor());
1090 EXPECT_EQ(2, a2.Perform(make_tuple(3, 3.5, 'a')));
1092 // As an action that returns void.
1093 Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor());
1095 a3.Perform(make_tuple());
1096 EXPECT_TRUE(g_done);
1099 // Tests InvokeWithoutArgs(obj_ptr, method).
1100 TEST(InvokeWithoutArgsTest, Method) {
1102 Action<int(bool, char)> a = // NOLINT
1103 InvokeWithoutArgs(&foo, &Foo::Nullary);
1104 EXPECT_EQ(123, a.Perform(make_tuple(true, 'a')));
1107 // Tests using IgnoreResult() on a polymorphic action.
1108 TEST(IgnoreResultTest, PolymorphicAction) {
1109 Action<void(int)> a = IgnoreResult(Return(5)); // NOLINT
1110 a.Perform(make_tuple(1));
1113 // Tests using IgnoreResult() on a monomorphic action.
1120 TEST(IgnoreResultTest, MonomorphicAction) {
1122 Action<void()> a = IgnoreResult(Invoke(ReturnOne));
1123 a.Perform(make_tuple());
1124 EXPECT_TRUE(g_done);
1127 // Tests using IgnoreResult() on an action that returns a class type.
1129 MyClass ReturnMyClass(double /* x */) {
1134 TEST(IgnoreResultTest, ActionReturningClass) {
1136 Action<void(int)> a = IgnoreResult(Invoke(ReturnMyClass)); // NOLINT
1137 a.Perform(make_tuple(2));
1138 EXPECT_TRUE(g_done);
1141 TEST(AssignTest, Int) {
1143 Action<void(int)> a = Assign(&x, 5);
1144 a.Perform(make_tuple(0));
1148 TEST(AssignTest, String) {
1150 Action<void(void)> a = Assign(&x, "Hello, world");
1151 a.Perform(make_tuple());
1152 EXPECT_EQ("Hello, world", x);
1155 TEST(AssignTest, CompatibleTypes) {
1157 Action<void(int)> a = Assign(&x, 5);
1158 a.Perform(make_tuple(0));
1159 EXPECT_DOUBLE_EQ(5, x);
1162 #if !GTEST_OS_WINDOWS_MOBILE
1164 class SetErrnoAndReturnTest : public testing::Test {
1166 virtual void SetUp() { errno = 0; }
1167 virtual void TearDown() { errno = 0; }
1170 TEST_F(SetErrnoAndReturnTest, Int) {
1171 Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5);
1172 EXPECT_EQ(-5, a.Perform(make_tuple()));
1173 EXPECT_EQ(ENOTTY, errno);
1176 TEST_F(SetErrnoAndReturnTest, Ptr) {
1178 Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x);
1179 EXPECT_EQ(&x, a.Perform(make_tuple()));
1180 EXPECT_EQ(ENOTTY, errno);
1183 TEST_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);
1189 #endif // !GTEST_OS_WINDOWS_MOBILE
1193 // Tests that ReferenceWrapper<T> is copyable.
1194 TEST(ByRefTest, IsCopyable) {
1195 const std::string s1 = "Hi";
1196 const std::string s2 = "Hello";
1198 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper =
1200 const std::string& r1 = ref_wrapper;
1201 EXPECT_EQ(&s1, &r1);
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);
1208 ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper1 =
1210 // Copies ref_wrapper1 to ref_wrapper.
1211 ref_wrapper = ref_wrapper1;
1212 const std::string& r3 = ref_wrapper;
1213 EXPECT_EQ(&s1, &r3);
1216 // Tests using ByRef() on a const value.
1217 TEST(ByRefTest, ConstValue) {
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);
1225 // Tests using ByRef() on a non-const value.
1226 TEST(ByRefTest, NonConstValue) {
1229 // ByRef(n) can be used as either an int&,
1230 int& ref = ByRef(n);
1231 EXPECT_EQ(&n, &ref);
1234 const int& const_ref = ByRef(n);
1235 EXPECT_EQ(&n, &const_ref);
1238 // Tests explicitly specifying the type when using ByRef().
1239 TEST(ByRefTest, ExplicitType) {
1241 const int& r1 = ByRef<const int>(n);
1244 // ByRef<char>(n); // This shouldn't compile - we have a negative
1245 // compilation test to catch it.
1248 Derived& r2 = ByRef<Derived>(d);
1251 const Derived& r3 = ByRef<const Derived>(d);
1254 Base& r4 = ByRef<Base>(d);
1257 const Base& r5 = ByRef<const Base>(d);
1260 // The following shouldn't compile - we have a negative compilation
1264 // ByRef<Derived>(b);
1267 // Tests that Google Mock prints expression ByRef(x) as a reference to x.
1268 TEST(ByRefTest, PrintsCorrectly) {
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());
1276 #if GTEST_LANG_CXX11
1278 std::unique_ptr<int> UniquePtrSource() {
1279 return std::unique_ptr<int>(new int(19));
1282 std::vector<std::unique_ptr<int>> VectorUniquePtrSource() {
1283 std::vector<std::unique_ptr<int>> out;
1284 out.emplace_back(new int(7));
1288 TEST(MockMethodTest, CanReturnMoveOnlyValue) {
1291 // Check default value
1292 DefaultValue<std::unique_ptr<int>>::SetFactory([] {
1293 return std::unique_ptr<int>(new int(42));
1295 EXPECT_EQ(42, *mock.MakeUnique());
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);
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]);
1313 #endif // GTEST_LANG_CXX11
1315 } // Unnamed namespace