Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:19:54

0001 // Copyright 2007, Google Inc.
0002 // All rights reserved.
0003 //
0004 // Redistribution and use in source and binary forms, with or without
0005 // modification, are permitted provided that the following conditions are
0006 // met:
0007 //
0008 //     * Redistributions of source code must retain the above copyright
0009 // notice, this list of conditions and the following disclaimer.
0010 //     * Redistributions in binary form must reproduce the above
0011 // copyright notice, this list of conditions and the following disclaimer
0012 // in the documentation and/or other materials provided with the
0013 // distribution.
0014 //     * Neither the name of Google Inc. nor the names of its
0015 // contributors may be used to endorse or promote products derived from
0016 // this software without specific prior written permission.
0017 //
0018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0029 //
0030 // Author: wan@google.com (Zhanyong Wan)
0031 
0032 // Google Mock - a framework for writing C++ mock classes.
0033 //
0034 // This file tests the built-in actions.
0035 
0036 #include "gmock/gmock-actions.h"
0037 #include <algorithm>
0038 #include <iterator>
0039 #include <memory>
0040 #include <string>
0041 #include "gmock/gmock.h"
0042 #include "gmock/internal/gmock-port.h"
0043 #include "gtest/gtest.h"
0044 #include "gtest/gtest-spi.h"
0045 
0046 namespace {
0047 
0048 // This list should be kept sorted.
0049 using testing::Action;
0050 using testing::ActionInterface;
0051 using testing::Assign;
0052 using testing::ByMove;
0053 using testing::ByRef;
0054 using testing::DefaultValue;
0055 using testing::DoDefault;
0056 using testing::IgnoreResult;
0057 using testing::Invoke;
0058 using testing::InvokeWithoutArgs;
0059 using testing::MakePolymorphicAction;
0060 using testing::Ne;
0061 using testing::PolymorphicAction;
0062 using testing::Return;
0063 using testing::ReturnNull;
0064 using testing::ReturnRef;
0065 using testing::ReturnRefOfCopy;
0066 using testing::SetArgPointee;
0067 using testing::SetArgumentPointee;
0068 using testing::_;
0069 using testing::get;
0070 using testing::internal::BuiltInDefaultValue;
0071 using testing::internal::Int64;
0072 using testing::internal::UInt64;
0073 using testing::make_tuple;
0074 using testing::tuple;
0075 using testing::tuple_element;
0076 
0077 #if !GTEST_OS_WINDOWS_MOBILE
0078 using testing::SetErrnoAndReturn;
0079 #endif
0080 
0081 #if GTEST_HAS_PROTOBUF_
0082 using testing::internal::TestMessage;
0083 #endif  // GTEST_HAS_PROTOBUF_
0084 
0085 // Tests that BuiltInDefaultValue<T*>::Get() returns NULL.
0086 TEST(BuiltInDefaultValueTest, IsNullForPointerTypes) {
0087   EXPECT_TRUE(BuiltInDefaultValue<int*>::Get() == NULL);
0088   EXPECT_TRUE(BuiltInDefaultValue<const char*>::Get() == NULL);
0089   EXPECT_TRUE(BuiltInDefaultValue<void*>::Get() == NULL);
0090 }
0091 
0092 // Tests that BuiltInDefaultValue<T*>::Exists() return true.
0093 TEST(BuiltInDefaultValueTest, ExistsForPointerTypes) {
0094   EXPECT_TRUE(BuiltInDefaultValue<int*>::Exists());
0095   EXPECT_TRUE(BuiltInDefaultValue<const char*>::Exists());
0096   EXPECT_TRUE(BuiltInDefaultValue<void*>::Exists());
0097 }
0098 
0099 // Tests that BuiltInDefaultValue<T>::Get() returns 0 when T is a
0100 // built-in numeric type.
0101 TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) {
0102   EXPECT_EQ(0U, BuiltInDefaultValue<unsigned char>::Get());
0103   EXPECT_EQ(0, BuiltInDefaultValue<signed char>::Get());
0104   EXPECT_EQ(0, BuiltInDefaultValue<char>::Get());
0105 #if GMOCK_HAS_SIGNED_WCHAR_T_
0106   EXPECT_EQ(0U, BuiltInDefaultValue<unsigned wchar_t>::Get());
0107   EXPECT_EQ(0, BuiltInDefaultValue<signed wchar_t>::Get());
0108 #endif
0109 #if GMOCK_WCHAR_T_IS_NATIVE_
0110   EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get());
0111 #endif
0112   EXPECT_EQ(0U, BuiltInDefaultValue<unsigned short>::Get());  // NOLINT
0113   EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get());  // NOLINT
0114   EXPECT_EQ(0, BuiltInDefaultValue<short>::Get());  // NOLINT
0115   EXPECT_EQ(0U, BuiltInDefaultValue<unsigned int>::Get());
0116   EXPECT_EQ(0, BuiltInDefaultValue<signed int>::Get());
0117   EXPECT_EQ(0, BuiltInDefaultValue<int>::Get());
0118   EXPECT_EQ(0U, BuiltInDefaultValue<unsigned long>::Get());  // NOLINT
0119   EXPECT_EQ(0, BuiltInDefaultValue<signed long>::Get());  // NOLINT
0120   EXPECT_EQ(0, BuiltInDefaultValue<long>::Get());  // NOLINT
0121   EXPECT_EQ(0U, BuiltInDefaultValue<UInt64>::Get());
0122   EXPECT_EQ(0, BuiltInDefaultValue<Int64>::Get());
0123   EXPECT_EQ(0, BuiltInDefaultValue<float>::Get());
0124   EXPECT_EQ(0, BuiltInDefaultValue<double>::Get());
0125 }
0126 
0127 // Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
0128 // built-in numeric type.
0129 TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) {
0130   EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists());
0131   EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists());
0132   EXPECT_TRUE(BuiltInDefaultValue<char>::Exists());
0133 #if GMOCK_HAS_SIGNED_WCHAR_T_
0134   EXPECT_TRUE(BuiltInDefaultValue<unsigned wchar_t>::Exists());
0135   EXPECT_TRUE(BuiltInDefaultValue<signed wchar_t>::Exists());
0136 #endif
0137 #if GMOCK_WCHAR_T_IS_NATIVE_
0138   EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists());
0139 #endif
0140   EXPECT_TRUE(BuiltInDefaultValue<unsigned short>::Exists());  // NOLINT
0141   EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists());  // NOLINT
0142   EXPECT_TRUE(BuiltInDefaultValue<short>::Exists());  // NOLINT
0143   EXPECT_TRUE(BuiltInDefaultValue<unsigned int>::Exists());
0144   EXPECT_TRUE(BuiltInDefaultValue<signed int>::Exists());
0145   EXPECT_TRUE(BuiltInDefaultValue<int>::Exists());
0146   EXPECT_TRUE(BuiltInDefaultValue<unsigned long>::Exists());  // NOLINT
0147   EXPECT_TRUE(BuiltInDefaultValue<signed long>::Exists());  // NOLINT
0148   EXPECT_TRUE(BuiltInDefaultValue<long>::Exists());  // NOLINT
0149   EXPECT_TRUE(BuiltInDefaultValue<UInt64>::Exists());
0150   EXPECT_TRUE(BuiltInDefaultValue<Int64>::Exists());
0151   EXPECT_TRUE(BuiltInDefaultValue<float>::Exists());
0152   EXPECT_TRUE(BuiltInDefaultValue<double>::Exists());
0153 }
0154 
0155 // Tests that BuiltInDefaultValue<bool>::Get() returns false.
0156 TEST(BuiltInDefaultValueTest, IsFalseForBool) {
0157   EXPECT_FALSE(BuiltInDefaultValue<bool>::Get());
0158 }
0159 
0160 // Tests that BuiltInDefaultValue<bool>::Exists() returns true.
0161 TEST(BuiltInDefaultValueTest, BoolExists) {
0162   EXPECT_TRUE(BuiltInDefaultValue<bool>::Exists());
0163 }
0164 
0165 // Tests that BuiltInDefaultValue<T>::Get() returns "" when T is a
0166 // string type.
0167 TEST(BuiltInDefaultValueTest, IsEmptyStringForString) {
0168 #if GTEST_HAS_GLOBAL_STRING
0169   EXPECT_EQ("", BuiltInDefaultValue< ::string>::Get());
0170 #endif  // GTEST_HAS_GLOBAL_STRING
0171 
0172   EXPECT_EQ("", BuiltInDefaultValue< ::std::string>::Get());
0173 }
0174 
0175 // Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
0176 // string type.
0177 TEST(BuiltInDefaultValueTest, ExistsForString) {
0178 #if GTEST_HAS_GLOBAL_STRING
0179   EXPECT_TRUE(BuiltInDefaultValue< ::string>::Exists());
0180 #endif  // GTEST_HAS_GLOBAL_STRING
0181 
0182   EXPECT_TRUE(BuiltInDefaultValue< ::std::string>::Exists());
0183 }
0184 
0185 // Tests that BuiltInDefaultValue<const T>::Get() returns the same
0186 // value as BuiltInDefaultValue<T>::Get() does.
0187 TEST(BuiltInDefaultValueTest, WorksForConstTypes) {
0188   EXPECT_EQ("", BuiltInDefaultValue<const std::string>::Get());
0189   EXPECT_EQ(0, BuiltInDefaultValue<const int>::Get());
0190   EXPECT_TRUE(BuiltInDefaultValue<char* const>::Get() == NULL);
0191   EXPECT_FALSE(BuiltInDefaultValue<const bool>::Get());
0192 }
0193 
0194 // A type that's default constructible.
0195 class MyDefaultConstructible {
0196  public:
0197   MyDefaultConstructible() : value_(42) {}
0198 
0199   int value() const { return value_; }
0200 
0201  private:
0202   int value_;
0203 };
0204 
0205 // A type that's not default constructible.
0206 class MyNonDefaultConstructible {
0207  public:
0208   // Does not have a default ctor.
0209   explicit MyNonDefaultConstructible(int a_value) : value_(a_value) {}
0210 
0211   int value() const { return value_; }
0212 
0213  private:
0214   int value_;
0215 };
0216 
0217 #if GTEST_HAS_STD_TYPE_TRAITS_
0218 
0219 TEST(BuiltInDefaultValueTest, ExistsForDefaultConstructibleType) {
0220   EXPECT_TRUE(BuiltInDefaultValue<MyDefaultConstructible>::Exists());
0221 }
0222 
0223 TEST(BuiltInDefaultValueTest, IsDefaultConstructedForDefaultConstructibleType) {
0224   EXPECT_EQ(42, BuiltInDefaultValue<MyDefaultConstructible>::Get().value());
0225 }
0226 
0227 #endif  // GTEST_HAS_STD_TYPE_TRAITS_
0228 
0229 TEST(BuiltInDefaultValueTest, DoesNotExistForNonDefaultConstructibleType) {
0230   EXPECT_FALSE(BuiltInDefaultValue<MyNonDefaultConstructible>::Exists());
0231 }
0232 
0233 // Tests that BuiltInDefaultValue<T&>::Get() aborts the program.
0234 TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) {
0235   EXPECT_DEATH_IF_SUPPORTED({
0236     BuiltInDefaultValue<int&>::Get();
0237   }, "");
0238   EXPECT_DEATH_IF_SUPPORTED({
0239     BuiltInDefaultValue<const char&>::Get();
0240   }, "");
0241 }
0242 
0243 TEST(BuiltInDefaultValueDeathTest, IsUndefinedForNonDefaultConstructibleType) {
0244   EXPECT_DEATH_IF_SUPPORTED({
0245     BuiltInDefaultValue<MyNonDefaultConstructible>::Get();
0246   }, "");
0247 }
0248 
0249 // Tests that DefaultValue<T>::IsSet() is false initially.
0250 TEST(DefaultValueTest, IsInitiallyUnset) {
0251   EXPECT_FALSE(DefaultValue<int>::IsSet());
0252   EXPECT_FALSE(DefaultValue<MyDefaultConstructible>::IsSet());
0253   EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::IsSet());
0254 }
0255 
0256 // Tests that DefaultValue<T> can be set and then unset.
0257 TEST(DefaultValueTest, CanBeSetAndUnset) {
0258   EXPECT_TRUE(DefaultValue<int>::Exists());
0259   EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::Exists());
0260 
0261   DefaultValue<int>::Set(1);
0262   DefaultValue<const MyNonDefaultConstructible>::Set(
0263       MyNonDefaultConstructible(42));
0264 
0265   EXPECT_EQ(1, DefaultValue<int>::Get());
0266   EXPECT_EQ(42, DefaultValue<const MyNonDefaultConstructible>::Get().value());
0267 
0268   EXPECT_TRUE(DefaultValue<int>::Exists());
0269   EXPECT_TRUE(DefaultValue<const MyNonDefaultConstructible>::Exists());
0270 
0271   DefaultValue<int>::Clear();
0272   DefaultValue<const MyNonDefaultConstructible>::Clear();
0273 
0274   EXPECT_FALSE(DefaultValue<int>::IsSet());
0275   EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::IsSet());
0276 
0277   EXPECT_TRUE(DefaultValue<int>::Exists());
0278   EXPECT_FALSE(DefaultValue<const MyNonDefaultConstructible>::Exists());
0279 }
0280 
0281 // Tests that DefaultValue<T>::Get() returns the
0282 // BuiltInDefaultValue<T>::Get() when DefaultValue<T>::IsSet() is
0283 // false.
0284 TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
0285   EXPECT_FALSE(DefaultValue<int>::IsSet());
0286   EXPECT_TRUE(DefaultValue<int>::Exists());
0287   EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible>::IsSet());
0288   EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible>::Exists());
0289 
0290   EXPECT_EQ(0, DefaultValue<int>::Get());
0291 
0292   EXPECT_DEATH_IF_SUPPORTED({
0293     DefaultValue<MyNonDefaultConstructible>::Get();
0294   }, "");
0295 }
0296 
0297 #if GTEST_HAS_STD_UNIQUE_PTR_
0298 TEST(DefaultValueTest, GetWorksForMoveOnlyIfSet) {
0299   EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Exists());
0300   EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Get() == NULL);
0301   DefaultValue<std::unique_ptr<int>>::SetFactory([] {
0302     return std::unique_ptr<int>(new int(42));
0303   });
0304   EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Exists());
0305   std::unique_ptr<int> i = DefaultValue<std::unique_ptr<int>>::Get();
0306   EXPECT_EQ(42, *i);
0307 }
0308 #endif  // GTEST_HAS_STD_UNIQUE_PTR_
0309 
0310 // Tests that DefaultValue<void>::Get() returns void.
0311 TEST(DefaultValueTest, GetWorksForVoid) {
0312   return DefaultValue<void>::Get();
0313 }
0314 
0315 // Tests using DefaultValue with a reference type.
0316 
0317 // Tests that DefaultValue<T&>::IsSet() is false initially.
0318 TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) {
0319   EXPECT_FALSE(DefaultValue<int&>::IsSet());
0320   EXPECT_FALSE(DefaultValue<MyDefaultConstructible&>::IsSet());
0321   EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
0322 }
0323 
0324 // Tests that DefaultValue<T&>::Exists is false initiallly.
0325 TEST(DefaultValueOfReferenceTest, IsInitiallyNotExisting) {
0326   EXPECT_FALSE(DefaultValue<int&>::Exists());
0327   EXPECT_FALSE(DefaultValue<MyDefaultConstructible&>::Exists());
0328   EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::Exists());
0329 }
0330 
0331 // Tests that DefaultValue<T&> can be set and then unset.
0332 TEST(DefaultValueOfReferenceTest, CanBeSetAndUnset) {
0333   int n = 1;
0334   DefaultValue<const int&>::Set(n);
0335   MyNonDefaultConstructible x(42);
0336   DefaultValue<MyNonDefaultConstructible&>::Set(x);
0337 
0338   EXPECT_TRUE(DefaultValue<const int&>::Exists());
0339   EXPECT_TRUE(DefaultValue<MyNonDefaultConstructible&>::Exists());
0340 
0341   EXPECT_EQ(&n, &(DefaultValue<const int&>::Get()));
0342   EXPECT_EQ(&x, &(DefaultValue<MyNonDefaultConstructible&>::Get()));
0343 
0344   DefaultValue<const int&>::Clear();
0345   DefaultValue<MyNonDefaultConstructible&>::Clear();
0346 
0347   EXPECT_FALSE(DefaultValue<const int&>::Exists());
0348   EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::Exists());
0349 
0350   EXPECT_FALSE(DefaultValue<const int&>::IsSet());
0351   EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
0352 }
0353 
0354 // Tests that DefaultValue<T&>::Get() returns the
0355 // BuiltInDefaultValue<T&>::Get() when DefaultValue<T&>::IsSet() is
0356 // false.
0357 TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
0358   EXPECT_FALSE(DefaultValue<int&>::IsSet());
0359   EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
0360 
0361   EXPECT_DEATH_IF_SUPPORTED({
0362     DefaultValue<int&>::Get();
0363   }, "");
0364   EXPECT_DEATH_IF_SUPPORTED({
0365     DefaultValue<MyNonDefaultConstructible>::Get();
0366   }, "");
0367 }
0368 
0369 // Tests that ActionInterface can be implemented by defining the
0370 // Perform method.
0371 
0372 typedef int MyGlobalFunction(bool, int);
0373 
0374 class MyActionImpl : public ActionInterface<MyGlobalFunction> {
0375  public:
0376   virtual int Perform(const tuple<bool, int>& args) {
0377     return get<0>(args) ? get<1>(args) : 0;
0378   }
0379 };
0380 
0381 TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) {
0382   MyActionImpl my_action_impl;
0383   (void)my_action_impl;
0384 }
0385 
0386 TEST(ActionInterfaceTest, MakeAction) {
0387   Action<MyGlobalFunction> action = MakeAction(new MyActionImpl);
0388 
0389   // When exercising the Perform() method of Action<F>, we must pass
0390   // it a tuple whose size and type are compatible with F's argument
0391   // types.  For example, if F is int(), then Perform() takes a
0392   // 0-tuple; if F is void(bool, int), then Perform() takes a
0393   // tuple<bool, int>, and so on.
0394   EXPECT_EQ(5, action.Perform(make_tuple(true, 5)));
0395 }
0396 
0397 // Tests that Action<F> can be contructed from a pointer to
0398 // ActionInterface<F>.
0399 TEST(ActionTest, CanBeConstructedFromActionInterface) {
0400   Action<MyGlobalFunction> action(new MyActionImpl);
0401 }
0402 
0403 // Tests that Action<F> delegates actual work to ActionInterface<F>.
0404 TEST(ActionTest, DelegatesWorkToActionInterface) {
0405   const Action<MyGlobalFunction> action(new MyActionImpl);
0406 
0407   EXPECT_EQ(5, action.Perform(make_tuple(true, 5)));
0408   EXPECT_EQ(0, action.Perform(make_tuple(false, 1)));
0409 }
0410 
0411 // Tests that Action<F> can be copied.
0412 TEST(ActionTest, IsCopyable) {
0413   Action<MyGlobalFunction> a1(new MyActionImpl);
0414   Action<MyGlobalFunction> a2(a1);  // Tests the copy constructor.
0415 
0416   // a1 should continue to work after being copied from.
0417   EXPECT_EQ(5, a1.Perform(make_tuple(true, 5)));
0418   EXPECT_EQ(0, a1.Perform(make_tuple(false, 1)));
0419 
0420   // a2 should work like the action it was copied from.
0421   EXPECT_EQ(5, a2.Perform(make_tuple(true, 5)));
0422   EXPECT_EQ(0, a2.Perform(make_tuple(false, 1)));
0423 
0424   a2 = a1;  // Tests the assignment operator.
0425 
0426   // a1 should continue to work after being copied from.
0427   EXPECT_EQ(5, a1.Perform(make_tuple(true, 5)));
0428   EXPECT_EQ(0, a1.Perform(make_tuple(false, 1)));
0429 
0430   // a2 should work like the action it was copied from.
0431   EXPECT_EQ(5, a2.Perform(make_tuple(true, 5)));
0432   EXPECT_EQ(0, a2.Perform(make_tuple(false, 1)));
0433 }
0434 
0435 // Tests that an Action<From> object can be converted to a
0436 // compatible Action<To> object.
0437 
0438 class IsNotZero : public ActionInterface<bool(int)> {  // NOLINT
0439  public:
0440   virtual bool Perform(const tuple<int>& arg) {
0441     return get<0>(arg) != 0;
0442   }
0443 };
0444 
0445 #if !GTEST_OS_SYMBIAN
0446 // Compiling this test on Nokia's Symbian compiler fails with:
0447 //  'Result' is not a member of class 'testing::internal::Function<int>'
0448 //  (point of instantiation: '@unnamed@gmock_actions_test_cc@::
0449 //      ActionTest_CanBeConvertedToOtherActionType_Test::TestBody()')
0450 // with no obvious fix.
0451 TEST(ActionTest, CanBeConvertedToOtherActionType) {
0452   const Action<bool(int)> a1(new IsNotZero);  // NOLINT
0453   const Action<int(char)> a2 = Action<int(char)>(a1);  // NOLINT
0454   EXPECT_EQ(1, a2.Perform(make_tuple('a')));
0455   EXPECT_EQ(0, a2.Perform(make_tuple('\0')));
0456 }
0457 #endif  // !GTEST_OS_SYMBIAN
0458 
0459 // The following two classes are for testing MakePolymorphicAction().
0460 
0461 // Implements a polymorphic action that returns the second of the
0462 // arguments it receives.
0463 class ReturnSecondArgumentAction {
0464  public:
0465   // We want to verify that MakePolymorphicAction() can work with a
0466   // polymorphic action whose Perform() method template is either
0467   // const or not.  This lets us verify the non-const case.
0468   template <typename Result, typename ArgumentTuple>
0469   Result Perform(const ArgumentTuple& args) { return get<1>(args); }
0470 };
0471 
0472 // Implements a polymorphic action that can be used in a nullary
0473 // function to return 0.
0474 class ReturnZeroFromNullaryFunctionAction {
0475  public:
0476   // For testing that MakePolymorphicAction() works when the
0477   // implementation class' Perform() method template takes only one
0478   // template parameter.
0479   //
0480   // We want to verify that MakePolymorphicAction() can work with a
0481   // polymorphic action whose Perform() method template is either
0482   // const or not.  This lets us verify the const case.
0483   template <typename Result>
0484   Result Perform(const tuple<>&) const { return 0; }
0485 };
0486 
0487 // These functions verify that MakePolymorphicAction() returns a
0488 // PolymorphicAction<T> where T is the argument's type.
0489 
0490 PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() {
0491   return MakePolymorphicAction(ReturnSecondArgumentAction());
0492 }
0493 
0494 PolymorphicAction<ReturnZeroFromNullaryFunctionAction>
0495 ReturnZeroFromNullaryFunction() {
0496   return MakePolymorphicAction(ReturnZeroFromNullaryFunctionAction());
0497 }
0498 
0499 // Tests that MakePolymorphicAction() turns a polymorphic action
0500 // implementation class into a polymorphic action.
0501 TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) {
0502   Action<int(bool, int, double)> a1 = ReturnSecondArgument();  // NOLINT
0503   EXPECT_EQ(5, a1.Perform(make_tuple(false, 5, 2.0)));
0504 }
0505 
0506 // Tests that MakePolymorphicAction() works when the implementation
0507 // class' Perform() method template has only one template parameter.
0508 TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) {
0509   Action<int()> a1 = ReturnZeroFromNullaryFunction();
0510   EXPECT_EQ(0, a1.Perform(make_tuple()));
0511 
0512   Action<void*()> a2 = ReturnZeroFromNullaryFunction();
0513   EXPECT_TRUE(a2.Perform(make_tuple()) == NULL);
0514 }
0515 
0516 // Tests that Return() works as an action for void-returning
0517 // functions.
0518 TEST(ReturnTest, WorksForVoid) {
0519   const Action<void(int)> ret = Return();  // NOLINT
0520   return ret.Perform(make_tuple(1));
0521 }
0522 
0523 // Tests that Return(v) returns v.
0524 TEST(ReturnTest, ReturnsGivenValue) {
0525   Action<int()> ret = Return(1);  // NOLINT
0526   EXPECT_EQ(1, ret.Perform(make_tuple()));
0527 
0528   ret = Return(-5);
0529   EXPECT_EQ(-5, ret.Perform(make_tuple()));
0530 }
0531 
0532 // Tests that Return("string literal") works.
0533 TEST(ReturnTest, AcceptsStringLiteral) {
0534   Action<const char*()> a1 = Return("Hello");
0535   EXPECT_STREQ("Hello", a1.Perform(make_tuple()));
0536 
0537   Action<std::string()> a2 = Return("world");
0538   EXPECT_EQ("world", a2.Perform(make_tuple()));
0539 }
0540 
0541 // Test struct which wraps a vector of integers. Used in
0542 // 'SupportsWrapperReturnType' test.
0543 struct IntegerVectorWrapper {
0544   std::vector<int> * v;
0545   IntegerVectorWrapper(std::vector<int>& _v) : v(&_v) {}  // NOLINT
0546 };
0547 
0548 // Tests that Return() works when return type is a wrapper type.
0549 TEST(ReturnTest, SupportsWrapperReturnType) {
0550   // Initialize vector of integers.
0551   std::vector<int> v;
0552   for (int i = 0; i < 5; ++i) v.push_back(i);
0553 
0554   // Return() called with 'v' as argument. The Action will return the same data
0555   // as 'v' (copy) but it will be wrapped in an IntegerVectorWrapper.
0556   Action<IntegerVectorWrapper()> a = Return(v);
0557   const std::vector<int>& result = *(a.Perform(make_tuple()).v);
0558   EXPECT_THAT(result, ::testing::ElementsAre(0, 1, 2, 3, 4));
0559 }
0560 
0561 // Tests that Return(v) is covaraint.
0562 
0563 struct Base {
0564   bool operator==(const Base&) { return true; }
0565 };
0566 
0567 struct Derived : public Base {
0568   bool operator==(const Derived&) { return true; }
0569 };
0570 
0571 TEST(ReturnTest, IsCovariant) {
0572   Base base;
0573   Derived derived;
0574   Action<Base*()> ret = Return(&base);
0575   EXPECT_EQ(&base, ret.Perform(make_tuple()));
0576 
0577   ret = Return(&derived);
0578   EXPECT_EQ(&derived, ret.Perform(make_tuple()));
0579 }
0580 
0581 // Tests that the type of the value passed into Return is converted into T
0582 // when the action is cast to Action<T(...)> rather than when the action is
0583 // performed. See comments on testing::internal::ReturnAction in
0584 // gmock-actions.h for more information.
0585 class FromType {
0586  public:
0587   explicit FromType(bool* is_converted) : converted_(is_converted) {}
0588   bool* converted() const { return converted_; }
0589 
0590  private:
0591   bool* const converted_;
0592 
0593   GTEST_DISALLOW_ASSIGN_(FromType);
0594 };
0595 
0596 class ToType {
0597  public:
0598   // Must allow implicit conversion due to use in ImplicitCast_<T>.
0599   ToType(const FromType& x) { *x.converted() = true; }  // NOLINT
0600 };
0601 
0602 TEST(ReturnTest, ConvertsArgumentWhenConverted) {
0603   bool converted = false;
0604   FromType x(&converted);
0605   Action<ToType()> action(Return(x));
0606   EXPECT_TRUE(converted) << "Return must convert its argument in its own "
0607                          << "conversion operator.";
0608   converted = false;
0609   action.Perform(tuple<>());
0610   EXPECT_FALSE(converted) << "Action must NOT convert its argument "
0611                           << "when performed.";
0612 }
0613 
0614 class DestinationType {};
0615 
0616 class SourceType {
0617  public:
0618   // Note: a non-const typecast operator.
0619   operator DestinationType() { return DestinationType(); }
0620 };
0621 
0622 TEST(ReturnTest, CanConvertArgumentUsingNonConstTypeCastOperator) {
0623   SourceType s;
0624   Action<DestinationType()> action(Return(s));
0625 }
0626 
0627 // Tests that ReturnNull() returns NULL in a pointer-returning function.
0628 TEST(ReturnNullTest, WorksInPointerReturningFunction) {
0629   const Action<int*()> a1 = ReturnNull();
0630   EXPECT_TRUE(a1.Perform(make_tuple()) == NULL);
0631 
0632   const Action<const char*(bool)> a2 = ReturnNull();  // NOLINT
0633   EXPECT_TRUE(a2.Perform(make_tuple(true)) == NULL);
0634 }
0635 
0636 #if GTEST_HAS_STD_UNIQUE_PTR_
0637 // Tests that ReturnNull() returns NULL for shared_ptr and unique_ptr returning
0638 // functions.
0639 TEST(ReturnNullTest, WorksInSmartPointerReturningFunction) {
0640   const Action<std::unique_ptr<const int>()> a1 = ReturnNull();
0641   EXPECT_TRUE(a1.Perform(make_tuple()) == nullptr);
0642 
0643   const Action<std::shared_ptr<int>(std::string)> a2 = ReturnNull();
0644   EXPECT_TRUE(a2.Perform(make_tuple("foo")) == nullptr);
0645 }
0646 #endif  // GTEST_HAS_STD_UNIQUE_PTR_
0647 
0648 // Tests that ReturnRef(v) works for reference types.
0649 TEST(ReturnRefTest, WorksForReference) {
0650   const int n = 0;
0651   const Action<const int&(bool)> ret = ReturnRef(n);  // NOLINT
0652 
0653   EXPECT_EQ(&n, &ret.Perform(make_tuple(true)));
0654 }
0655 
0656 // Tests that ReturnRef(v) is covariant.
0657 TEST(ReturnRefTest, IsCovariant) {
0658   Base base;
0659   Derived derived;
0660   Action<Base&()> a = ReturnRef(base);
0661   EXPECT_EQ(&base, &a.Perform(make_tuple()));
0662 
0663   a = ReturnRef(derived);
0664   EXPECT_EQ(&derived, &a.Perform(make_tuple()));
0665 }
0666 
0667 // Tests that ReturnRefOfCopy(v) works for reference types.
0668 TEST(ReturnRefOfCopyTest, WorksForReference) {
0669   int n = 42;
0670   const Action<const int&()> ret = ReturnRefOfCopy(n);
0671 
0672   EXPECT_NE(&n, &ret.Perform(make_tuple()));
0673   EXPECT_EQ(42, ret.Perform(make_tuple()));
0674 
0675   n = 43;
0676   EXPECT_NE(&n, &ret.Perform(make_tuple()));
0677   EXPECT_EQ(42, ret.Perform(make_tuple()));
0678 }
0679 
0680 // Tests that ReturnRefOfCopy(v) is covariant.
0681 TEST(ReturnRefOfCopyTest, IsCovariant) {
0682   Base base;
0683   Derived derived;
0684   Action<Base&()> a = ReturnRefOfCopy(base);
0685   EXPECT_NE(&base, &a.Perform(make_tuple()));
0686 
0687   a = ReturnRefOfCopy(derived);
0688   EXPECT_NE(&derived, &a.Perform(make_tuple()));
0689 }
0690 
0691 // Tests that DoDefault() does the default action for the mock method.
0692 
0693 class MockClass {
0694  public:
0695   MockClass() {}
0696 
0697   MOCK_METHOD1(IntFunc, int(bool flag));  // NOLINT
0698   MOCK_METHOD0(Foo, MyNonDefaultConstructible());
0699 #if GTEST_HAS_STD_UNIQUE_PTR_
0700   MOCK_METHOD0(MakeUnique, std::unique_ptr<int>());
0701   MOCK_METHOD0(MakeUniqueBase, std::unique_ptr<Base>());
0702   MOCK_METHOD0(MakeVectorUnique, std::vector<std::unique_ptr<int>>());
0703 #endif
0704 
0705  private:
0706   GTEST_DISALLOW_COPY_AND_ASSIGN_(MockClass);
0707 };
0708 
0709 // Tests that DoDefault() returns the built-in default value for the
0710 // return type by default.
0711 TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) {
0712   MockClass mock;
0713   EXPECT_CALL(mock, IntFunc(_))
0714       .WillOnce(DoDefault());
0715   EXPECT_EQ(0, mock.IntFunc(true));
0716 }
0717 
0718 // Tests that DoDefault() throws (when exceptions are enabled) or aborts
0719 // the process when there is no built-in default value for the return type.
0720 TEST(DoDefaultDeathTest, DiesForUnknowType) {
0721   MockClass mock;
0722   EXPECT_CALL(mock, Foo())
0723       .WillRepeatedly(DoDefault());
0724 #if GTEST_HAS_EXCEPTIONS
0725   EXPECT_ANY_THROW(mock.Foo());
0726 #else
0727   EXPECT_DEATH_IF_SUPPORTED({
0728     mock.Foo();
0729   }, "");
0730 #endif
0731 }
0732 
0733 // Tests that using DoDefault() inside a composite action leads to a
0734 // run-time error.
0735 
0736 void VoidFunc(bool /* flag */) {}
0737 
0738 TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
0739   MockClass mock;
0740   EXPECT_CALL(mock, IntFunc(_))
0741       .WillRepeatedly(DoAll(Invoke(VoidFunc),
0742                             DoDefault()));
0743 
0744   // Ideally we should verify the error message as well.  Sadly,
0745   // EXPECT_DEATH() can only capture stderr, while Google Mock's
0746   // errors are printed on stdout.  Therefore we have to settle for
0747   // not verifying the message.
0748   EXPECT_DEATH_IF_SUPPORTED({
0749     mock.IntFunc(true);
0750   }, "");
0751 }
0752 
0753 // Tests that DoDefault() returns the default value set by
0754 // DefaultValue<T>::Set() when it's not overriden by an ON_CALL().
0755 TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) {
0756   DefaultValue<int>::Set(1);
0757   MockClass mock;
0758   EXPECT_CALL(mock, IntFunc(_))
0759       .WillOnce(DoDefault());
0760   EXPECT_EQ(1, mock.IntFunc(false));
0761   DefaultValue<int>::Clear();
0762 }
0763 
0764 // Tests that DoDefault() does the action specified by ON_CALL().
0765 TEST(DoDefaultTest, DoesWhatOnCallSpecifies) {
0766   MockClass mock;
0767   ON_CALL(mock, IntFunc(_))
0768       .WillByDefault(Return(2));
0769   EXPECT_CALL(mock, IntFunc(_))
0770       .WillOnce(DoDefault());
0771   EXPECT_EQ(2, mock.IntFunc(false));
0772 }
0773 
0774 // Tests that using DoDefault() in ON_CALL() leads to a run-time failure.
0775 TEST(DoDefaultTest, CannotBeUsedInOnCall) {
0776   MockClass mock;
0777   EXPECT_NONFATAL_FAILURE({  // NOLINT
0778     ON_CALL(mock, IntFunc(_))
0779       .WillByDefault(DoDefault());
0780   }, "DoDefault() cannot be used in ON_CALL()");
0781 }
0782 
0783 // Tests that SetArgPointee<N>(v) sets the variable pointed to by
0784 // the N-th (0-based) argument to v.
0785 TEST(SetArgPointeeTest, SetsTheNthPointee) {
0786   typedef void MyFunction(bool, int*, char*);
0787   Action<MyFunction> a = SetArgPointee<1>(2);
0788 
0789   int n = 0;
0790   char ch = '\0';
0791   a.Perform(make_tuple(true, &n, &ch));
0792   EXPECT_EQ(2, n);
0793   EXPECT_EQ('\0', ch);
0794 
0795   a = SetArgPointee<2>('a');
0796   n = 0;
0797   ch = '\0';
0798   a.Perform(make_tuple(true, &n, &ch));
0799   EXPECT_EQ(0, n);
0800   EXPECT_EQ('a', ch);
0801 }
0802 
0803 #if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN)
0804 // Tests that SetArgPointee<N>() accepts a string literal.
0805 // GCC prior to v4.0 and the Symbian compiler do not support this.
0806 TEST(SetArgPointeeTest, AcceptsStringLiteral) {
0807   typedef void MyFunction(std::string*, const char**);
0808   Action<MyFunction> a = SetArgPointee<0>("hi");
0809   std::string str;
0810   const char* ptr = NULL;
0811   a.Perform(make_tuple(&str, &ptr));
0812   EXPECT_EQ("hi", str);
0813   EXPECT_TRUE(ptr == NULL);
0814 
0815   a = SetArgPointee<1>("world");
0816   str = "";
0817   a.Perform(make_tuple(&str, &ptr));
0818   EXPECT_EQ("", str);
0819   EXPECT_STREQ("world", ptr);
0820 }
0821 
0822 TEST(SetArgPointeeTest, AcceptsWideStringLiteral) {
0823   typedef void MyFunction(const wchar_t**);
0824   Action<MyFunction> a = SetArgPointee<0>(L"world");
0825   const wchar_t* ptr = NULL;
0826   a.Perform(make_tuple(&ptr));
0827   EXPECT_STREQ(L"world", ptr);
0828 
0829 # if GTEST_HAS_STD_WSTRING
0830 
0831   typedef void MyStringFunction(std::wstring*);
0832   Action<MyStringFunction> a2 = SetArgPointee<0>(L"world");
0833   std::wstring str = L"";
0834   a2.Perform(make_tuple(&str));
0835   EXPECT_EQ(L"world", str);
0836 
0837 # endif
0838 }
0839 #endif
0840 
0841 // Tests that SetArgPointee<N>() accepts a char pointer.
0842 TEST(SetArgPointeeTest, AcceptsCharPointer) {
0843   typedef void MyFunction(bool, std::string*, const char**);
0844   const char* const hi = "hi";
0845   Action<MyFunction> a = SetArgPointee<1>(hi);
0846   std::string str;
0847   const char* ptr = NULL;
0848   a.Perform(make_tuple(true, &str, &ptr));
0849   EXPECT_EQ("hi", str);
0850   EXPECT_TRUE(ptr == NULL);
0851 
0852   char world_array[] = "world";
0853   char* const world = world_array;
0854   a = SetArgPointee<2>(world);
0855   str = "";
0856   a.Perform(make_tuple(true, &str, &ptr));
0857   EXPECT_EQ("", str);
0858   EXPECT_EQ(world, ptr);
0859 }
0860 
0861 TEST(SetArgPointeeTest, AcceptsWideCharPointer) {
0862   typedef void MyFunction(bool, const wchar_t**);
0863   const wchar_t* const hi = L"hi";
0864   Action<MyFunction> a = SetArgPointee<1>(hi);
0865   const wchar_t* ptr = NULL;
0866   a.Perform(make_tuple(true, &ptr));
0867   EXPECT_EQ(hi, ptr);
0868 
0869 # if GTEST_HAS_STD_WSTRING
0870 
0871   typedef void MyStringFunction(bool, std::wstring*);
0872   wchar_t world_array[] = L"world";
0873   wchar_t* const world = world_array;
0874   Action<MyStringFunction> a2 = SetArgPointee<1>(world);
0875   std::wstring str;
0876   a2.Perform(make_tuple(true, &str));
0877   EXPECT_EQ(world_array, str);
0878 # endif
0879 }
0880 
0881 #if GTEST_HAS_PROTOBUF_
0882 
0883 // Tests that SetArgPointee<N>(proto_buffer) sets the v1 protobuf
0884 // variable pointed to by the N-th (0-based) argument to proto_buffer.
0885 TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
0886   TestMessage* const msg = new TestMessage;
0887   msg->set_member("yes");
0888   TestMessage orig_msg;
0889   orig_msg.CopyFrom(*msg);
0890 
0891   Action<void(bool, TestMessage*)> a = SetArgPointee<1>(*msg);
0892   // SetArgPointee<N>(proto_buffer) makes a copy of proto_buffer
0893   // s.t. the action works even when the original proto_buffer has
0894   // died.  We ensure this behavior by deleting msg before using the
0895   // action.
0896   delete msg;
0897 
0898   TestMessage dest;
0899   EXPECT_FALSE(orig_msg.Equals(dest));
0900   a.Perform(make_tuple(true, &dest));
0901   EXPECT_TRUE(orig_msg.Equals(dest));
0902 }
0903 
0904 // Tests that SetArgPointee<N>(proto_buffer) sets the
0905 // ::ProtocolMessage variable pointed to by the N-th (0-based)
0906 // argument to proto_buffer.
0907 TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
0908   TestMessage* const msg = new TestMessage;
0909   msg->set_member("yes");
0910   TestMessage orig_msg;
0911   orig_msg.CopyFrom(*msg);
0912 
0913   Action<void(bool, ::ProtocolMessage*)> a = SetArgPointee<1>(*msg);
0914   // SetArgPointee<N>(proto_buffer) makes a copy of proto_buffer
0915   // s.t. the action works even when the original proto_buffer has
0916   // died.  We ensure this behavior by deleting msg before using the
0917   // action.
0918   delete msg;
0919 
0920   TestMessage dest;
0921   ::ProtocolMessage* const dest_base = &dest;
0922   EXPECT_FALSE(orig_msg.Equals(dest));
0923   a.Perform(make_tuple(true, dest_base));
0924   EXPECT_TRUE(orig_msg.Equals(dest));
0925 }
0926 
0927 // Tests that SetArgPointee<N>(proto2_buffer) sets the v2
0928 // protobuf variable pointed to by the N-th (0-based) argument to
0929 // proto2_buffer.
0930 TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
0931   using testing::internal::FooMessage;
0932   FooMessage* const msg = new FooMessage;
0933   msg->set_int_field(2);
0934   msg->set_string_field("hi");
0935   FooMessage orig_msg;
0936   orig_msg.CopyFrom(*msg);
0937 
0938   Action<void(bool, FooMessage*)> a = SetArgPointee<1>(*msg);
0939   // SetArgPointee<N>(proto2_buffer) makes a copy of
0940   // proto2_buffer s.t. the action works even when the original
0941   // proto2_buffer has died.  We ensure this behavior by deleting msg
0942   // before using the action.
0943   delete msg;
0944 
0945   FooMessage dest;
0946   dest.set_int_field(0);
0947   a.Perform(make_tuple(true, &dest));
0948   EXPECT_EQ(2, dest.int_field());
0949   EXPECT_EQ("hi", dest.string_field());
0950 }
0951 
0952 // Tests that SetArgPointee<N>(proto2_buffer) sets the
0953 // proto2::Message variable pointed to by the N-th (0-based) argument
0954 // to proto2_buffer.
0955 TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
0956   using testing::internal::FooMessage;
0957   FooMessage* const msg = new FooMessage;
0958   msg->set_int_field(2);
0959   msg->set_string_field("hi");
0960   FooMessage orig_msg;
0961   orig_msg.CopyFrom(*msg);
0962 
0963   Action<void(bool, ::proto2::Message*)> a = SetArgPointee<1>(*msg);
0964   // SetArgPointee<N>(proto2_buffer) makes a copy of
0965   // proto2_buffer s.t. the action works even when the original
0966   // proto2_buffer has died.  We ensure this behavior by deleting msg
0967   // before using the action.
0968   delete msg;
0969 
0970   FooMessage dest;
0971   dest.set_int_field(0);
0972   ::proto2::Message* const dest_base = &dest;
0973   a.Perform(make_tuple(true, dest_base));
0974   EXPECT_EQ(2, dest.int_field());
0975   EXPECT_EQ("hi", dest.string_field());
0976 }
0977 
0978 #endif  // GTEST_HAS_PROTOBUF_
0979 
0980 // Tests that SetArgumentPointee<N>(v) sets the variable pointed to by
0981 // the N-th (0-based) argument to v.
0982 TEST(SetArgumentPointeeTest, SetsTheNthPointee) {
0983   typedef void MyFunction(bool, int*, char*);
0984   Action<MyFunction> a = SetArgumentPointee<1>(2);
0985 
0986   int n = 0;
0987   char ch = '\0';
0988   a.Perform(make_tuple(true, &n, &ch));
0989   EXPECT_EQ(2, n);
0990   EXPECT_EQ('\0', ch);
0991 
0992   a = SetArgumentPointee<2>('a');
0993   n = 0;
0994   ch = '\0';
0995   a.Perform(make_tuple(true, &n, &ch));
0996   EXPECT_EQ(0, n);
0997   EXPECT_EQ('a', ch);
0998 }
0999 
1000 #if GTEST_HAS_PROTOBUF_
1001 
1002 // Tests that SetArgumentPointee<N>(proto_buffer) sets the v1 protobuf
1003 // variable pointed to by the N-th (0-based) argument to proto_buffer.
1004 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
1005   TestMessage* const msg = new TestMessage;
1006   msg->set_member("yes");
1007   TestMessage orig_msg;
1008   orig_msg.CopyFrom(*msg);
1009 
1010   Action<void(bool, TestMessage*)> a = SetArgumentPointee<1>(*msg);
1011   // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
1012   // s.t. the action works even when the original proto_buffer has
1013   // died.  We ensure this behavior by deleting msg before using the
1014   // action.
1015   delete msg;
1016 
1017   TestMessage dest;
1018   EXPECT_FALSE(orig_msg.Equals(dest));
1019   a.Perform(make_tuple(true, &dest));
1020   EXPECT_TRUE(orig_msg.Equals(dest));
1021 }
1022 
1023 // Tests that SetArgumentPointee<N>(proto_buffer) sets the
1024 // ::ProtocolMessage variable pointed to by the N-th (0-based)
1025 // argument to proto_buffer.
1026 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
1027   TestMessage* const msg = new TestMessage;
1028   msg->set_member("yes");
1029   TestMessage orig_msg;
1030   orig_msg.CopyFrom(*msg);
1031 
1032   Action<void(bool, ::ProtocolMessage*)> a = SetArgumentPointee<1>(*msg);
1033   // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
1034   // s.t. the action works even when the original proto_buffer has
1035   // died.  We ensure this behavior by deleting msg before using the
1036   // action.
1037   delete msg;
1038 
1039   TestMessage dest;
1040   ::ProtocolMessage* const dest_base = &dest;
1041   EXPECT_FALSE(orig_msg.Equals(dest));
1042   a.Perform(make_tuple(true, dest_base));
1043   EXPECT_TRUE(orig_msg.Equals(dest));
1044 }
1045 
1046 // Tests that SetArgumentPointee<N>(proto2_buffer) sets the v2
1047 // protobuf variable pointed to by the N-th (0-based) argument to
1048 // proto2_buffer.
1049 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
1050   using testing::internal::FooMessage;
1051   FooMessage* const msg = new FooMessage;
1052   msg->set_int_field(2);
1053   msg->set_string_field("hi");
1054   FooMessage orig_msg;
1055   orig_msg.CopyFrom(*msg);
1056 
1057   Action<void(bool, FooMessage*)> a = SetArgumentPointee<1>(*msg);
1058   // SetArgumentPointee<N>(proto2_buffer) makes a copy of
1059   // proto2_buffer s.t. the action works even when the original
1060   // proto2_buffer has died.  We ensure this behavior by deleting msg
1061   // before using the action.
1062   delete msg;
1063 
1064   FooMessage dest;
1065   dest.set_int_field(0);
1066   a.Perform(make_tuple(true, &dest));
1067   EXPECT_EQ(2, dest.int_field());
1068   EXPECT_EQ("hi", dest.string_field());
1069 }
1070 
1071 // Tests that SetArgumentPointee<N>(proto2_buffer) sets the
1072 // proto2::Message variable pointed to by the N-th (0-based) argument
1073 // to proto2_buffer.
1074 TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
1075   using testing::internal::FooMessage;
1076   FooMessage* const msg = new FooMessage;
1077   msg->set_int_field(2);
1078   msg->set_string_field("hi");
1079   FooMessage orig_msg;
1080   orig_msg.CopyFrom(*msg);
1081 
1082   Action<void(bool, ::proto2::Message*)> a = SetArgumentPointee<1>(*msg);
1083   // SetArgumentPointee<N>(proto2_buffer) makes a copy of
1084   // proto2_buffer s.t. the action works even when the original
1085   // proto2_buffer has died.  We ensure this behavior by deleting msg
1086   // before using the action.
1087   delete msg;
1088 
1089   FooMessage dest;
1090   dest.set_int_field(0);
1091   ::proto2::Message* const dest_base = &dest;
1092   a.Perform(make_tuple(true, dest_base));
1093   EXPECT_EQ(2, dest.int_field());
1094   EXPECT_EQ("hi", dest.string_field());
1095 }
1096 
1097 #endif  // GTEST_HAS_PROTOBUF_
1098 
1099 // Sample functions and functors for testing Invoke() and etc.
1100 int Nullary() { return 1; }
1101 
1102 class NullaryFunctor {
1103  public:
1104   int operator()() { return 2; }
1105 };
1106 
1107 bool g_done = false;
1108 void VoidNullary() { g_done = true; }
1109 
1110 class VoidNullaryFunctor {
1111  public:
1112   void operator()() { g_done = true; }
1113 };
1114 
1115 class Foo {
1116  public:
1117   Foo() : value_(123) {}
1118 
1119   int Nullary() const { return value_; }
1120 
1121  private:
1122   int value_;
1123 };
1124 
1125 // Tests InvokeWithoutArgs(function).
1126 TEST(InvokeWithoutArgsTest, Function) {
1127   // As an action that takes one argument.
1128   Action<int(int)> a = InvokeWithoutArgs(Nullary);  // NOLINT
1129   EXPECT_EQ(1, a.Perform(make_tuple(2)));
1130 
1131   // As an action that takes two arguments.
1132   Action<int(int, double)> a2 = InvokeWithoutArgs(Nullary);  // NOLINT
1133   EXPECT_EQ(1, a2.Perform(make_tuple(2, 3.5)));
1134 
1135   // As an action that returns void.
1136   Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary);  // NOLINT
1137   g_done = false;
1138   a3.Perform(make_tuple(1));
1139   EXPECT_TRUE(g_done);
1140 }
1141 
1142 // Tests InvokeWithoutArgs(functor).
1143 TEST(InvokeWithoutArgsTest, Functor) {
1144   // As an action that takes no argument.
1145   Action<int()> a = InvokeWithoutArgs(NullaryFunctor());  // NOLINT
1146   EXPECT_EQ(2, a.Perform(make_tuple()));
1147 
1148   // As an action that takes three arguments.
1149   Action<int(int, double, char)> a2 =  // NOLINT
1150       InvokeWithoutArgs(NullaryFunctor());
1151   EXPECT_EQ(2, a2.Perform(make_tuple(3, 3.5, 'a')));
1152 
1153   // As an action that returns void.
1154   Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor());
1155   g_done = false;
1156   a3.Perform(make_tuple());
1157   EXPECT_TRUE(g_done);
1158 }
1159 
1160 // Tests InvokeWithoutArgs(obj_ptr, method).
1161 TEST(InvokeWithoutArgsTest, Method) {
1162   Foo foo;
1163   Action<int(bool, char)> a =  // NOLINT
1164       InvokeWithoutArgs(&foo, &Foo::Nullary);
1165   EXPECT_EQ(123, a.Perform(make_tuple(true, 'a')));
1166 }
1167 
1168 // Tests using IgnoreResult() on a polymorphic action.
1169 TEST(IgnoreResultTest, PolymorphicAction) {
1170   Action<void(int)> a = IgnoreResult(Return(5));  // NOLINT
1171   a.Perform(make_tuple(1));
1172 }
1173 
1174 // Tests using IgnoreResult() on a monomorphic action.
1175 
1176 int ReturnOne() {
1177   g_done = true;
1178   return 1;
1179 }
1180 
1181 TEST(IgnoreResultTest, MonomorphicAction) {
1182   g_done = false;
1183   Action<void()> a = IgnoreResult(Invoke(ReturnOne));
1184   a.Perform(make_tuple());
1185   EXPECT_TRUE(g_done);
1186 }
1187 
1188 // Tests using IgnoreResult() on an action that returns a class type.
1189 
1190 MyNonDefaultConstructible ReturnMyNonDefaultConstructible(double /* x */) {
1191   g_done = true;
1192   return MyNonDefaultConstructible(42);
1193 }
1194 
1195 TEST(IgnoreResultTest, ActionReturningClass) {
1196   g_done = false;
1197   Action<void(int)> a =
1198       IgnoreResult(Invoke(ReturnMyNonDefaultConstructible));  // NOLINT
1199   a.Perform(make_tuple(2));
1200   EXPECT_TRUE(g_done);
1201 }
1202 
1203 TEST(AssignTest, Int) {
1204   int x = 0;
1205   Action<void(int)> a = Assign(&x, 5);
1206   a.Perform(make_tuple(0));
1207   EXPECT_EQ(5, x);
1208 }
1209 
1210 TEST(AssignTest, String) {
1211   ::std::string x;
1212   Action<void(void)> a = Assign(&x, "Hello, world");
1213   a.Perform(make_tuple());
1214   EXPECT_EQ("Hello, world", x);
1215 }
1216 
1217 TEST(AssignTest, CompatibleTypes) {
1218   double x = 0;
1219   Action<void(int)> a = Assign(&x, 5);
1220   a.Perform(make_tuple(0));
1221   EXPECT_DOUBLE_EQ(5, x);
1222 }
1223 
1224 #if !GTEST_OS_WINDOWS_MOBILE
1225 
1226 class SetErrnoAndReturnTest : public testing::Test {
1227  protected:
1228   virtual void SetUp() { errno = 0; }
1229   virtual void TearDown() { errno = 0; }
1230 };
1231 
1232 TEST_F(SetErrnoAndReturnTest, Int) {
1233   Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5);
1234   EXPECT_EQ(-5, a.Perform(make_tuple()));
1235   EXPECT_EQ(ENOTTY, errno);
1236 }
1237 
1238 TEST_F(SetErrnoAndReturnTest, Ptr) {
1239   int x;
1240   Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x);
1241   EXPECT_EQ(&x, a.Perform(make_tuple()));
1242   EXPECT_EQ(ENOTTY, errno);
1243 }
1244 
1245 TEST_F(SetErrnoAndReturnTest, CompatibleTypes) {
1246   Action<double()> a = SetErrnoAndReturn(EINVAL, 5);
1247   EXPECT_DOUBLE_EQ(5.0, a.Perform(make_tuple()));
1248   EXPECT_EQ(EINVAL, errno);
1249 }
1250 
1251 #endif  // !GTEST_OS_WINDOWS_MOBILE
1252 
1253 // Tests ByRef().
1254 
1255 // Tests that ReferenceWrapper<T> is copyable.
1256 TEST(ByRefTest, IsCopyable) {
1257   const std::string s1 = "Hi";
1258   const std::string s2 = "Hello";
1259 
1260   ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper =
1261       ByRef(s1);
1262   const std::string& r1 = ref_wrapper;
1263   EXPECT_EQ(&s1, &r1);
1264 
1265   // Assigns a new value to ref_wrapper.
1266   ref_wrapper = ByRef(s2);
1267   const std::string& r2 = ref_wrapper;
1268   EXPECT_EQ(&s2, &r2);
1269 
1270   ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper1 =
1271       ByRef(s1);
1272   // Copies ref_wrapper1 to ref_wrapper.
1273   ref_wrapper = ref_wrapper1;
1274   const std::string& r3 = ref_wrapper;
1275   EXPECT_EQ(&s1, &r3);
1276 }
1277 
1278 // Tests using ByRef() on a const value.
1279 TEST(ByRefTest, ConstValue) {
1280   const int n = 0;
1281   // int& ref = ByRef(n);  // This shouldn't compile - we have a
1282                            // negative compilation test to catch it.
1283   const int& const_ref = ByRef(n);
1284   EXPECT_EQ(&n, &const_ref);
1285 }
1286 
1287 // Tests using ByRef() on a non-const value.
1288 TEST(ByRefTest, NonConstValue) {
1289   int n = 0;
1290 
1291   // ByRef(n) can be used as either an int&,
1292   int& ref = ByRef(n);
1293   EXPECT_EQ(&n, &ref);
1294 
1295   // or a const int&.
1296   const int& const_ref = ByRef(n);
1297   EXPECT_EQ(&n, &const_ref);
1298 }
1299 
1300 // Tests explicitly specifying the type when using ByRef().
1301 TEST(ByRefTest, ExplicitType) {
1302   int n = 0;
1303   const int& r1 = ByRef<const int>(n);
1304   EXPECT_EQ(&n, &r1);
1305 
1306   // ByRef<char>(n);  // This shouldn't compile - we have a negative
1307                       // compilation test to catch it.
1308 
1309   Derived d;
1310   Derived& r2 = ByRef<Derived>(d);
1311   EXPECT_EQ(&d, &r2);
1312 
1313   const Derived& r3 = ByRef<const Derived>(d);
1314   EXPECT_EQ(&d, &r3);
1315 
1316   Base& r4 = ByRef<Base>(d);
1317   EXPECT_EQ(&d, &r4);
1318 
1319   const Base& r5 = ByRef<const Base>(d);
1320   EXPECT_EQ(&d, &r5);
1321 
1322   // The following shouldn't compile - we have a negative compilation
1323   // test for it.
1324   //
1325   // Base b;
1326   // ByRef<Derived>(b);
1327 }
1328 
1329 // Tests that Google Mock prints expression ByRef(x) as a reference to x.
1330 TEST(ByRefTest, PrintsCorrectly) {
1331   int n = 42;
1332   ::std::stringstream expected, actual;
1333   testing::internal::UniversalPrinter<const int&>::Print(n, &expected);
1334   testing::internal::UniversalPrint(ByRef(n), &actual);
1335   EXPECT_EQ(expected.str(), actual.str());
1336 }
1337 
1338 #if GTEST_HAS_STD_UNIQUE_PTR_
1339 
1340 std::unique_ptr<int> UniquePtrSource() {
1341   return std::unique_ptr<int>(new int(19));
1342 }
1343 
1344 std::vector<std::unique_ptr<int>> VectorUniquePtrSource() {
1345   std::vector<std::unique_ptr<int>> out;
1346   out.emplace_back(new int(7));
1347   return out;
1348 }
1349 
1350 TEST(MockMethodTest, CanReturnMoveOnlyValue_Return) {
1351   MockClass mock;
1352   std::unique_ptr<int> i(new int(19));
1353   EXPECT_CALL(mock, MakeUnique()).WillOnce(Return(ByMove(std::move(i))));
1354   EXPECT_CALL(mock, MakeVectorUnique())
1355       .WillOnce(Return(ByMove(VectorUniquePtrSource())));
1356   Derived* d = new Derived;
1357   EXPECT_CALL(mock, MakeUniqueBase())
1358       .WillOnce(Return(ByMove(std::unique_ptr<Derived>(d))));
1359 
1360   std::unique_ptr<int> result1 = mock.MakeUnique();
1361   EXPECT_EQ(19, *result1);
1362 
1363   std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
1364   EXPECT_EQ(1u, vresult.size());
1365   EXPECT_NE(nullptr, vresult[0]);
1366   EXPECT_EQ(7, *vresult[0]);
1367 
1368   std::unique_ptr<Base> result2 = mock.MakeUniqueBase();
1369   EXPECT_EQ(d, result2.get());
1370 }
1371 
1372 TEST(MockMethodTest, CanReturnMoveOnlyValue_DoAllReturn) {
1373   testing::MockFunction<void()> mock_function;
1374   MockClass mock;
1375   std::unique_ptr<int> i(new int(19));
1376   EXPECT_CALL(mock_function, Call());
1377   EXPECT_CALL(mock, MakeUnique()).WillOnce(DoAll(
1378       InvokeWithoutArgs(&mock_function, &testing::MockFunction<void()>::Call),
1379       Return(ByMove(std::move(i)))));
1380 
1381   std::unique_ptr<int> result1 = mock.MakeUnique();
1382   EXPECT_EQ(19, *result1);
1383 }
1384 
1385 TEST(MockMethodTest, CanReturnMoveOnlyValue_Invoke) {
1386   MockClass mock;
1387 
1388   // Check default value
1389   DefaultValue<std::unique_ptr<int>>::SetFactory([] {
1390     return std::unique_ptr<int>(new int(42));
1391   });
1392   EXPECT_EQ(42, *mock.MakeUnique());
1393 
1394   EXPECT_CALL(mock, MakeUnique()).WillRepeatedly(Invoke(UniquePtrSource));
1395   EXPECT_CALL(mock, MakeVectorUnique())
1396       .WillRepeatedly(Invoke(VectorUniquePtrSource));
1397   std::unique_ptr<int> result1 = mock.MakeUnique();
1398   EXPECT_EQ(19, *result1);
1399   std::unique_ptr<int> result2 = mock.MakeUnique();
1400   EXPECT_EQ(19, *result2);
1401   EXPECT_NE(result1, result2);
1402 
1403   std::vector<std::unique_ptr<int>> vresult = mock.MakeVectorUnique();
1404   EXPECT_EQ(1u, vresult.size());
1405   EXPECT_NE(nullptr, vresult[0]);
1406   EXPECT_EQ(7, *vresult[0]);
1407 }
1408 
1409 #endif  // GTEST_HAS_STD_UNIQUE_PTR_
1410 
1411 }  // Unnamed namespace