github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/src/third_party/googlemock/test/gmock-actions_test.cc (about)

     1  // Copyright 2007, Google Inc.
     2  // All rights reserved.
     3  //
     4  // Redistribution and use in source and binary forms, with or without
     5  // modification, are permitted provided that the following conditions are
     6  // met:
     7  //
     8  //     * Redistributions of source code must retain the above copyright
     9  // notice, this list of conditions and the following disclaimer.
    10  //     * Redistributions in binary form must reproduce the above
    11  // copyright notice, this list of conditions and the following disclaimer
    12  // in the documentation and/or other materials provided with the
    13  // distribution.
    14  //     * Neither the name of Google Inc. nor the names of its
    15  // contributors may be used to endorse or promote products derived from
    16  // this software without specific prior written permission.
    17  //
    18  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    19  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    20  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    21  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    22  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    23  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    24  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    25  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    26  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    27  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    28  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    29  //
    30  // Author: wan@google.com (Zhanyong Wan)
    31  
    32  // Google Mock - a framework for writing C++ mock classes.
    33  //
    34  // This file tests the built-in actions.
    35  
    36  #include "gmock/gmock-actions.h"
    37  #include <algorithm>
    38  #include <iterator>
    39  #include <string>
    40  #include "gmock/gmock.h"
    41  #include "gmock/internal/gmock-port.h"
    42  #include "gtest/gtest.h"
    43  #include "gtest/gtest-spi.h"
    44  
    45  namespace {
    46  
    47  using ::std::tr1::get;
    48  using ::std::tr1::make_tuple;
    49  using ::std::tr1::tuple;
    50  using ::std::tr1::tuple_element;
    51  using testing::internal::BuiltInDefaultValue;
    52  using testing::internal::Int64;
    53  using testing::internal::UInt64;
    54  // This list should be kept sorted.
    55  using testing::_;
    56  using testing::Action;
    57  using testing::ActionInterface;
    58  using testing::Assign;
    59  using testing::ByRef;
    60  using testing::DefaultValue;
    61  using testing::DoDefault;
    62  using testing::IgnoreResult;
    63  using testing::Invoke;
    64  using testing::InvokeWithoutArgs;
    65  using testing::MakePolymorphicAction;
    66  using testing::Ne;
    67  using testing::PolymorphicAction;
    68  using testing::Return;
    69  using testing::ReturnNull;
    70  using testing::ReturnRef;
    71  using testing::ReturnRefOfCopy;
    72  using testing::SetArgPointee;
    73  using testing::SetArgumentPointee;
    74  
    75  #if !GTEST_OS_WINDOWS_MOBILE
    76  using testing::SetErrnoAndReturn;
    77  #endif
    78  
    79  #if GTEST_HAS_PROTOBUF_
    80  using testing::internal::TestMessage;
    81  #endif  // GTEST_HAS_PROTOBUF_
    82  
    83  // Tests that BuiltInDefaultValue<T*>::Get() returns NULL.
    84  TEST(BuiltInDefaultValueTest, IsNullForPointerTypes) {
    85    EXPECT_TRUE(BuiltInDefaultValue<int*>::Get() == NULL);
    86    EXPECT_TRUE(BuiltInDefaultValue<const char*>::Get() == NULL);
    87    EXPECT_TRUE(BuiltInDefaultValue<void*>::Get() == NULL);
    88  }
    89  
    90  // Tests that BuiltInDefaultValue<T*>::Exists() return true.
    91  TEST(BuiltInDefaultValueTest, ExistsForPointerTypes) {
    92    EXPECT_TRUE(BuiltInDefaultValue<int*>::Exists());
    93    EXPECT_TRUE(BuiltInDefaultValue<const char*>::Exists());
    94    EXPECT_TRUE(BuiltInDefaultValue<void*>::Exists());
    95  }
    96  
    97  // Tests that BuiltInDefaultValue<T>::Get() returns 0 when T is a
    98  // built-in numeric type.
    99  TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) {
   100    EXPECT_EQ(0U, BuiltInDefaultValue<unsigned char>::Get());
   101    EXPECT_EQ(0, BuiltInDefaultValue<signed char>::Get());
   102    EXPECT_EQ(0, BuiltInDefaultValue<char>::Get());
   103  #if GMOCK_HAS_SIGNED_WCHAR_T_
   104    EXPECT_EQ(0U, BuiltInDefaultValue<unsigned wchar_t>::Get());
   105    EXPECT_EQ(0, BuiltInDefaultValue<signed wchar_t>::Get());
   106  #endif
   107  #if GMOCK_WCHAR_T_IS_NATIVE_
   108    EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get());
   109  #endif
   110    EXPECT_EQ(0U, BuiltInDefaultValue<unsigned short>::Get());  // NOLINT
   111    EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get());  // NOLINT
   112    EXPECT_EQ(0, BuiltInDefaultValue<short>::Get());  // NOLINT
   113    EXPECT_EQ(0U, BuiltInDefaultValue<unsigned int>::Get());
   114    EXPECT_EQ(0, BuiltInDefaultValue<signed int>::Get());
   115    EXPECT_EQ(0, BuiltInDefaultValue<int>::Get());
   116    EXPECT_EQ(0U, BuiltInDefaultValue<unsigned long>::Get());  // NOLINT
   117    EXPECT_EQ(0, BuiltInDefaultValue<signed long>::Get());  // NOLINT
   118    EXPECT_EQ(0, BuiltInDefaultValue<long>::Get());  // NOLINT
   119    EXPECT_EQ(0U, BuiltInDefaultValue<UInt64>::Get());
   120    EXPECT_EQ(0, BuiltInDefaultValue<Int64>::Get());
   121    EXPECT_EQ(0, BuiltInDefaultValue<float>::Get());
   122    EXPECT_EQ(0, BuiltInDefaultValue<double>::Get());
   123  }
   124  
   125  // Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
   126  // built-in numeric type.
   127  TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) {
   128    EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists());
   129    EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists());
   130    EXPECT_TRUE(BuiltInDefaultValue<char>::Exists());
   131  #if GMOCK_HAS_SIGNED_WCHAR_T_
   132    EXPECT_TRUE(BuiltInDefaultValue<unsigned wchar_t>::Exists());
   133    EXPECT_TRUE(BuiltInDefaultValue<signed wchar_t>::Exists());
   134  #endif
   135  #if GMOCK_WCHAR_T_IS_NATIVE_
   136    EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists());
   137  #endif
   138    EXPECT_TRUE(BuiltInDefaultValue<unsigned short>::Exists());  // NOLINT
   139    EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists());  // NOLINT
   140    EXPECT_TRUE(BuiltInDefaultValue<short>::Exists());  // NOLINT
   141    EXPECT_TRUE(BuiltInDefaultValue<unsigned int>::Exists());
   142    EXPECT_TRUE(BuiltInDefaultValue<signed int>::Exists());
   143    EXPECT_TRUE(BuiltInDefaultValue<int>::Exists());
   144    EXPECT_TRUE(BuiltInDefaultValue<unsigned long>::Exists());  // NOLINT
   145    EXPECT_TRUE(BuiltInDefaultValue<signed long>::Exists());  // NOLINT
   146    EXPECT_TRUE(BuiltInDefaultValue<long>::Exists());  // NOLINT
   147    EXPECT_TRUE(BuiltInDefaultValue<UInt64>::Exists());
   148    EXPECT_TRUE(BuiltInDefaultValue<Int64>::Exists());
   149    EXPECT_TRUE(BuiltInDefaultValue<float>::Exists());
   150    EXPECT_TRUE(BuiltInDefaultValue<double>::Exists());
   151  }
   152  
   153  // Tests that BuiltInDefaultValue<bool>::Get() returns false.
   154  TEST(BuiltInDefaultValueTest, IsFalseForBool) {
   155    EXPECT_FALSE(BuiltInDefaultValue<bool>::Get());
   156  }
   157  
   158  // Tests that BuiltInDefaultValue<bool>::Exists() returns true.
   159  TEST(BuiltInDefaultValueTest, BoolExists) {
   160    EXPECT_TRUE(BuiltInDefaultValue<bool>::Exists());
   161  }
   162  
   163  // Tests that BuiltInDefaultValue<T>::Get() returns "" when T is a
   164  // string type.
   165  TEST(BuiltInDefaultValueTest, IsEmptyStringForString) {
   166  #if GTEST_HAS_GLOBAL_STRING
   167    EXPECT_EQ("", BuiltInDefaultValue< ::string>::Get());
   168  #endif  // GTEST_HAS_GLOBAL_STRING
   169  
   170    EXPECT_EQ("", BuiltInDefaultValue< ::std::string>::Get());
   171  }
   172  
   173  // Tests that BuiltInDefaultValue<T>::Exists() returns true when T is a
   174  // string type.
   175  TEST(BuiltInDefaultValueTest, ExistsForString) {
   176  #if GTEST_HAS_GLOBAL_STRING
   177    EXPECT_TRUE(BuiltInDefaultValue< ::string>::Exists());
   178  #endif  // GTEST_HAS_GLOBAL_STRING
   179  
   180    EXPECT_TRUE(BuiltInDefaultValue< ::std::string>::Exists());
   181  }
   182  
   183  // Tests that BuiltInDefaultValue<const T>::Get() returns the same
   184  // value as BuiltInDefaultValue<T>::Get() does.
   185  TEST(BuiltInDefaultValueTest, WorksForConstTypes) {
   186    EXPECT_EQ("", BuiltInDefaultValue<const std::string>::Get());
   187    EXPECT_EQ(0, BuiltInDefaultValue<const int>::Get());
   188    EXPECT_TRUE(BuiltInDefaultValue<char* const>::Get() == NULL);
   189    EXPECT_FALSE(BuiltInDefaultValue<const bool>::Get());
   190  }
   191  
   192  // Tests that BuiltInDefaultValue<T>::Get() aborts the program with
   193  // the correct error message when T is a user-defined type.
   194  struct UserType {
   195    UserType() : value(0) {}
   196  
   197    int value;
   198  };
   199  
   200  TEST(BuiltInDefaultValueTest, UserTypeHasNoDefault) {
   201    EXPECT_FALSE(BuiltInDefaultValue<UserType>::Exists());
   202  }
   203  
   204  // Tests that BuiltInDefaultValue<T&>::Get() aborts the program.
   205  TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) {
   206    EXPECT_DEATH_IF_SUPPORTED({
   207      BuiltInDefaultValue<int&>::Get();
   208    }, "");
   209    EXPECT_DEATH_IF_SUPPORTED({
   210      BuiltInDefaultValue<const char&>::Get();
   211    }, "");
   212  }
   213  
   214  TEST(BuiltInDefaultValueDeathTest, IsUndefinedForUserTypes) {
   215    EXPECT_DEATH_IF_SUPPORTED({
   216      BuiltInDefaultValue<UserType>::Get();
   217    }, "");
   218  }
   219  
   220  // Tests that DefaultValue<T>::IsSet() is false initially.
   221  TEST(DefaultValueTest, IsInitiallyUnset) {
   222    EXPECT_FALSE(DefaultValue<int>::IsSet());
   223    EXPECT_FALSE(DefaultValue<const UserType>::IsSet());
   224  }
   225  
   226  // Tests that DefaultValue<T> can be set and then unset.
   227  TEST(DefaultValueTest, CanBeSetAndUnset) {
   228    EXPECT_TRUE(DefaultValue<int>::Exists());
   229    EXPECT_FALSE(DefaultValue<const UserType>::Exists());
   230  
   231    DefaultValue<int>::Set(1);
   232    DefaultValue<const UserType>::Set(UserType());
   233  
   234    EXPECT_EQ(1, DefaultValue<int>::Get());
   235    EXPECT_EQ(0, DefaultValue<const UserType>::Get().value);
   236  
   237    EXPECT_TRUE(DefaultValue<int>::Exists());
   238    EXPECT_TRUE(DefaultValue<const UserType>::Exists());
   239  
   240    DefaultValue<int>::Clear();
   241    DefaultValue<const UserType>::Clear();
   242  
   243    EXPECT_FALSE(DefaultValue<int>::IsSet());
   244    EXPECT_FALSE(DefaultValue<const UserType>::IsSet());
   245  
   246    EXPECT_TRUE(DefaultValue<int>::Exists());
   247    EXPECT_FALSE(DefaultValue<const UserType>::Exists());
   248  }
   249  
   250  // Tests that DefaultValue<T>::Get() returns the
   251  // BuiltInDefaultValue<T>::Get() when DefaultValue<T>::IsSet() is
   252  // false.
   253  TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
   254    EXPECT_FALSE(DefaultValue<int>::IsSet());
   255    EXPECT_TRUE(DefaultValue<int>::Exists());
   256    EXPECT_FALSE(DefaultValue<UserType>::IsSet());
   257    EXPECT_FALSE(DefaultValue<UserType>::Exists());
   258  
   259    EXPECT_EQ(0, DefaultValue<int>::Get());
   260  
   261    EXPECT_DEATH_IF_SUPPORTED({
   262      DefaultValue<UserType>::Get();
   263    }, "");
   264  }
   265  
   266  // Tests that DefaultValue<void>::Get() returns void.
   267  TEST(DefaultValueTest, GetWorksForVoid) {
   268    return DefaultValue<void>::Get();
   269  }
   270  
   271  // Tests using DefaultValue with a reference type.
   272  
   273  // Tests that DefaultValue<T&>::IsSet() is false initially.
   274  TEST(DefaultValueOfReferenceTest, IsInitiallyUnset) {
   275    EXPECT_FALSE(DefaultValue<int&>::IsSet());
   276    EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
   277  }
   278  
   279  // Tests that DefaultValue<T&>::Exists is false initiallly.
   280  TEST(DefaultValueOfReferenceTest, IsInitiallyNotExisting) {
   281    EXPECT_FALSE(DefaultValue<int&>::Exists());
   282    EXPECT_FALSE(DefaultValue<UserType&>::Exists());
   283  }
   284  
   285  // Tests that DefaultValue<T&> can be set and then unset.
   286  TEST(DefaultValueOfReferenceTest, CanBeSetAndUnset) {
   287    int n = 1;
   288    DefaultValue<const int&>::Set(n);
   289    UserType u;
   290    DefaultValue<UserType&>::Set(u);
   291  
   292    EXPECT_TRUE(DefaultValue<const int&>::Exists());
   293    EXPECT_TRUE(DefaultValue<UserType&>::Exists());
   294  
   295    EXPECT_EQ(&n, &(DefaultValue<const int&>::Get()));
   296    EXPECT_EQ(&u, &(DefaultValue<UserType&>::Get()));
   297  
   298    DefaultValue<const int&>::Clear();
   299    DefaultValue<UserType&>::Clear();
   300  
   301    EXPECT_FALSE(DefaultValue<const int&>::Exists());
   302    EXPECT_FALSE(DefaultValue<UserType&>::Exists());
   303  
   304    EXPECT_FALSE(DefaultValue<const int&>::IsSet());
   305    EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
   306  }
   307  
   308  // Tests that DefaultValue<T&>::Get() returns the
   309  // BuiltInDefaultValue<T&>::Get() when DefaultValue<T&>::IsSet() is
   310  // false.
   311  TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
   312    EXPECT_FALSE(DefaultValue<int&>::IsSet());
   313    EXPECT_FALSE(DefaultValue<UserType&>::IsSet());
   314  
   315    EXPECT_DEATH_IF_SUPPORTED({
   316      DefaultValue<int&>::Get();
   317    }, "");
   318    EXPECT_DEATH_IF_SUPPORTED({
   319      DefaultValue<UserType>::Get();
   320    }, "");
   321  }
   322  
   323  // Tests that ActionInterface can be implemented by defining the
   324  // Perform method.
   325  
   326  typedef int MyGlobalFunction(bool, int);
   327  
   328  class MyActionImpl : public ActionInterface<MyGlobalFunction> {
   329   public:
   330    virtual int Perform(const tuple<bool, int>& args) {
   331      return get<0>(args) ? get<1>(args) : 0;
   332    }
   333  };
   334  
   335  TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) {
   336    MyActionImpl my_action_impl;
   337    (void)my_action_impl;
   338  }
   339  
   340  TEST(ActionInterfaceTest, MakeAction) {
   341    Action<MyGlobalFunction> action = MakeAction(new MyActionImpl);
   342  
   343    // When exercising the Perform() method of Action<F>, we must pass
   344    // it a tuple whose size and type are compatible with F's argument
   345    // types.  For example, if F is int(), then Perform() takes a
   346    // 0-tuple; if F is void(bool, int), then Perform() takes a
   347    // tuple<bool, int>, and so on.
   348    EXPECT_EQ(5, action.Perform(make_tuple(true, 5)));
   349  }
   350  
   351  // Tests that Action<F> can be contructed from a pointer to
   352  // ActionInterface<F>.
   353  TEST(ActionTest, CanBeConstructedFromActionInterface) {
   354    Action<MyGlobalFunction> action(new MyActionImpl);
   355  }
   356  
   357  // Tests that Action<F> delegates actual work to ActionInterface<F>.
   358  TEST(ActionTest, DelegatesWorkToActionInterface) {
   359    const Action<MyGlobalFunction> action(new MyActionImpl);
   360  
   361    EXPECT_EQ(5, action.Perform(make_tuple(true, 5)));
   362    EXPECT_EQ(0, action.Perform(make_tuple(false, 1)));
   363  }
   364  
   365  // Tests that Action<F> can be copied.
   366  TEST(ActionTest, IsCopyable) {
   367    Action<MyGlobalFunction> a1(new MyActionImpl);
   368    Action<MyGlobalFunction> a2(a1);  // Tests the copy constructor.
   369  
   370    // a1 should continue to work after being copied from.
   371    EXPECT_EQ(5, a1.Perform(make_tuple(true, 5)));
   372    EXPECT_EQ(0, a1.Perform(make_tuple(false, 1)));
   373  
   374    // a2 should work like the action it was copied from.
   375    EXPECT_EQ(5, a2.Perform(make_tuple(true, 5)));
   376    EXPECT_EQ(0, a2.Perform(make_tuple(false, 1)));
   377  
   378    a2 = a1;  // Tests the assignment operator.
   379  
   380    // a1 should continue to work after being copied from.
   381    EXPECT_EQ(5, a1.Perform(make_tuple(true, 5)));
   382    EXPECT_EQ(0, a1.Perform(make_tuple(false, 1)));
   383  
   384    // a2 should work like the action it was copied from.
   385    EXPECT_EQ(5, a2.Perform(make_tuple(true, 5)));
   386    EXPECT_EQ(0, a2.Perform(make_tuple(false, 1)));
   387  }
   388  
   389  // Tests that an Action<From> object can be converted to a
   390  // compatible Action<To> object.
   391  
   392  class IsNotZero : public ActionInterface<bool(int)> {  // NOLINT
   393   public:
   394    virtual bool Perform(const tuple<int>& arg) {
   395      return get<0>(arg) != 0;
   396    }
   397  };
   398  
   399  #if !GTEST_OS_SYMBIAN
   400  // Compiling this test on Nokia's Symbian compiler fails with:
   401  //  'Result' is not a member of class 'testing::internal::Function<int>'
   402  //  (point of instantiation: '@unnamed@gmock_actions_test_cc@::
   403  //      ActionTest_CanBeConvertedToOtherActionType_Test::TestBody()')
   404  // with no obvious fix.
   405  TEST(ActionTest, CanBeConvertedToOtherActionType) {
   406    const Action<bool(int)> a1(new IsNotZero);  // NOLINT
   407    const Action<int(char)> a2 = Action<int(char)>(a1);  // NOLINT
   408    EXPECT_EQ(1, a2.Perform(make_tuple('a')));
   409    EXPECT_EQ(0, a2.Perform(make_tuple('\0')));
   410  }
   411  #endif  // !GTEST_OS_SYMBIAN
   412  
   413  // The following two classes are for testing MakePolymorphicAction().
   414  
   415  // Implements a polymorphic action that returns the second of the
   416  // arguments it receives.
   417  class ReturnSecondArgumentAction {
   418   public:
   419    // We want to verify that MakePolymorphicAction() can work with a
   420    // polymorphic action whose Perform() method template is either
   421    // const or not.  This lets us verify the non-const case.
   422    template <typename Result, typename ArgumentTuple>
   423    Result Perform(const ArgumentTuple& args) { return get<1>(args); }
   424  };
   425  
   426  // Implements a polymorphic action that can be used in a nullary
   427  // function to return 0.
   428  class ReturnZeroFromNullaryFunctionAction {
   429   public:
   430    // For testing that MakePolymorphicAction() works when the
   431    // implementation class' Perform() method template takes only one
   432    // template parameter.
   433    //
   434    // We want to verify that MakePolymorphicAction() can work with a
   435    // polymorphic action whose Perform() method template is either
   436    // const or not.  This lets us verify the const case.
   437    template <typename Result>
   438    Result Perform(const tuple<>&) const { return 0; }
   439  };
   440  
   441  // These functions verify that MakePolymorphicAction() returns a
   442  // PolymorphicAction<T> where T is the argument's type.
   443  
   444  PolymorphicAction<ReturnSecondArgumentAction> ReturnSecondArgument() {
   445    return MakePolymorphicAction(ReturnSecondArgumentAction());
   446  }
   447  
   448  PolymorphicAction<ReturnZeroFromNullaryFunctionAction>
   449  ReturnZeroFromNullaryFunction() {
   450    return MakePolymorphicAction(ReturnZeroFromNullaryFunctionAction());
   451  }
   452  
   453  // Tests that MakePolymorphicAction() turns a polymorphic action
   454  // implementation class into a polymorphic action.
   455  TEST(MakePolymorphicActionTest, ConstructsActionFromImpl) {
   456    Action<int(bool, int, double)> a1 = ReturnSecondArgument();  // NOLINT
   457    EXPECT_EQ(5, a1.Perform(make_tuple(false, 5, 2.0)));
   458  }
   459  
   460  // Tests that MakePolymorphicAction() works when the implementation
   461  // class' Perform() method template has only one template parameter.
   462  TEST(MakePolymorphicActionTest, WorksWhenPerformHasOneTemplateParameter) {
   463    Action<int()> a1 = ReturnZeroFromNullaryFunction();
   464    EXPECT_EQ(0, a1.Perform(make_tuple()));
   465  
   466    Action<void*()> a2 = ReturnZeroFromNullaryFunction();
   467    EXPECT_TRUE(a2.Perform(make_tuple()) == NULL);
   468  }
   469  
   470  // Tests that Return() works as an action for void-returning
   471  // functions.
   472  TEST(ReturnTest, WorksForVoid) {
   473    const Action<void(int)> ret = Return();  // NOLINT
   474    return ret.Perform(make_tuple(1));
   475  }
   476  
   477  // Tests that Return(v) returns v.
   478  TEST(ReturnTest, ReturnsGivenValue) {
   479    Action<int()> ret = Return(1);  // NOLINT
   480    EXPECT_EQ(1, ret.Perform(make_tuple()));
   481  
   482    ret = Return(-5);
   483    EXPECT_EQ(-5, ret.Perform(make_tuple()));
   484  }
   485  
   486  // Tests that Return("string literal") works.
   487  TEST(ReturnTest, AcceptsStringLiteral) {
   488    Action<const char*()> a1 = Return("Hello");
   489    EXPECT_STREQ("Hello", a1.Perform(make_tuple()));
   490  
   491    Action<std::string()> a2 = Return("world");
   492    EXPECT_EQ("world", a2.Perform(make_tuple()));
   493  }
   494  
   495  // Tests that Return(v) is covaraint.
   496  
   497  struct Base {
   498    bool operator==(const Base&) { return true; }
   499  };
   500  
   501  struct Derived : public Base {
   502    bool operator==(const Derived&) { return true; }
   503  };
   504  
   505  TEST(ReturnTest, IsCovariant) {
   506    Base base;
   507    Derived derived;
   508    Action<Base*()> ret = Return(&base);
   509    EXPECT_EQ(&base, ret.Perform(make_tuple()));
   510  
   511    ret = Return(&derived);
   512    EXPECT_EQ(&derived, ret.Perform(make_tuple()));
   513  }
   514  
   515  // Tests that the type of the value passed into Return is converted into T
   516  // when the action is cast to Action<T(...)> rather than when the action is
   517  // performed. See comments on testing::internal::ReturnAction in
   518  // gmock-actions.h for more information.
   519  class FromType {
   520   public:
   521    explicit FromType(bool* is_converted) : converted_(is_converted) {}
   522    bool* converted() const { return converted_; }
   523  
   524   private:
   525    bool* const converted_;
   526  
   527    GTEST_DISALLOW_ASSIGN_(FromType);
   528  };
   529  
   530  class ToType {
   531   public:
   532    // Must allow implicit conversion due to use in ImplicitCast_<T>.
   533    ToType(const FromType& x) { *x.converted() = true; }  // NOLINT
   534  };
   535  
   536  TEST(ReturnTest, ConvertsArgumentWhenConverted) {
   537    bool converted = false;
   538    FromType x(&converted);
   539    Action<ToType()> action(Return(x));
   540    EXPECT_TRUE(converted) << "Return must convert its argument in its own "
   541                           << "conversion operator.";
   542    converted = false;
   543    action.Perform(tuple<>());
   544    EXPECT_FALSE(converted) << "Action must NOT convert its argument "
   545                            << "when performed.";
   546  }
   547  
   548  class DestinationType {};
   549  
   550  class SourceType {
   551   public:
   552    // Note: a non-const typecast operator.
   553    operator DestinationType() { return DestinationType(); }
   554  };
   555  
   556  TEST(ReturnTest, CanConvertArgumentUsingNonConstTypeCastOperator) {
   557    SourceType s;
   558    Action<DestinationType()> action(Return(s));
   559  }
   560  
   561  // Tests that ReturnNull() returns NULL in a pointer-returning function.
   562  TEST(ReturnNullTest, WorksInPointerReturningFunction) {
   563    const Action<int*()> a1 = ReturnNull();
   564    EXPECT_TRUE(a1.Perform(make_tuple()) == NULL);
   565  
   566    const Action<const char*(bool)> a2 = ReturnNull();  // NOLINT
   567    EXPECT_TRUE(a2.Perform(make_tuple(true)) == NULL);
   568  }
   569  
   570  // Tests that ReturnRef(v) works for reference types.
   571  TEST(ReturnRefTest, WorksForReference) {
   572    const int n = 0;
   573    const Action<const int&(bool)> ret = ReturnRef(n);  // NOLINT
   574  
   575    EXPECT_EQ(&n, &ret.Perform(make_tuple(true)));
   576  }
   577  
   578  // Tests that ReturnRef(v) is covariant.
   579  TEST(ReturnRefTest, IsCovariant) {
   580    Base base;
   581    Derived derived;
   582    Action<Base&()> a = ReturnRef(base);
   583    EXPECT_EQ(&base, &a.Perform(make_tuple()));
   584  
   585    a = ReturnRef(derived);
   586    EXPECT_EQ(&derived, &a.Perform(make_tuple()));
   587  }
   588  
   589  // Tests that ReturnRefOfCopy(v) works for reference types.
   590  TEST(ReturnRefOfCopyTest, WorksForReference) {
   591    int n = 42;
   592    const Action<const int&()> ret = ReturnRefOfCopy(n);
   593  
   594    EXPECT_NE(&n, &ret.Perform(make_tuple()));
   595    EXPECT_EQ(42, ret.Perform(make_tuple()));
   596  
   597    n = 43;
   598    EXPECT_NE(&n, &ret.Perform(make_tuple()));
   599    EXPECT_EQ(42, ret.Perform(make_tuple()));
   600  }
   601  
   602  // Tests that ReturnRefOfCopy(v) is covariant.
   603  TEST(ReturnRefOfCopyTest, IsCovariant) {
   604    Base base;
   605    Derived derived;
   606    Action<Base&()> a = ReturnRefOfCopy(base);
   607    EXPECT_NE(&base, &a.Perform(make_tuple()));
   608  
   609    a = ReturnRefOfCopy(derived);
   610    EXPECT_NE(&derived, &a.Perform(make_tuple()));
   611  }
   612  
   613  // Tests that DoDefault() does the default action for the mock method.
   614  
   615  class MyClass {};
   616  
   617  class MockClass {
   618   public:
   619    MockClass() {}
   620  
   621    MOCK_METHOD1(IntFunc, int(bool flag));  // NOLINT
   622    MOCK_METHOD0(Foo, MyClass());
   623  
   624   private:
   625    GTEST_DISALLOW_COPY_AND_ASSIGN_(MockClass);
   626  };
   627  
   628  // Tests that DoDefault() returns the built-in default value for the
   629  // return type by default.
   630  TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) {
   631    MockClass mock;
   632    EXPECT_CALL(mock, IntFunc(_))
   633        .WillOnce(DoDefault());
   634    EXPECT_EQ(0, mock.IntFunc(true));
   635  }
   636  
   637  // Tests that DoDefault() throws (when exceptions are enabled) or aborts
   638  // the process when there is no built-in default value for the return type.
   639  TEST(DoDefaultDeathTest, DiesForUnknowType) {
   640    MockClass mock;
   641    EXPECT_CALL(mock, Foo())
   642        .WillRepeatedly(DoDefault());
   643  #if GTEST_HAS_EXCEPTIONS
   644    EXPECT_ANY_THROW(mock.Foo());
   645  #else
   646    EXPECT_DEATH_IF_SUPPORTED({
   647      mock.Foo();
   648    }, "");
   649  #endif
   650  }
   651  
   652  // Tests that using DoDefault() inside a composite action leads to a
   653  // run-time error.
   654  
   655  void VoidFunc(bool /* flag */) {}
   656  
   657  TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
   658    MockClass mock;
   659    EXPECT_CALL(mock, IntFunc(_))
   660        .WillRepeatedly(DoAll(Invoke(VoidFunc),
   661                              DoDefault()));
   662  
   663    // Ideally we should verify the error message as well.  Sadly,
   664    // EXPECT_DEATH() can only capture stderr, while Google Mock's
   665    // errors are printed on stdout.  Therefore we have to settle for
   666    // not verifying the message.
   667    EXPECT_DEATH_IF_SUPPORTED({
   668      mock.IntFunc(true);
   669    }, "");
   670  }
   671  
   672  // Tests that DoDefault() returns the default value set by
   673  // DefaultValue<T>::Set() when it's not overriden by an ON_CALL().
   674  TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) {
   675    DefaultValue<int>::Set(1);
   676    MockClass mock;
   677    EXPECT_CALL(mock, IntFunc(_))
   678        .WillOnce(DoDefault());
   679    EXPECT_EQ(1, mock.IntFunc(false));
   680    DefaultValue<int>::Clear();
   681  }
   682  
   683  // Tests that DoDefault() does the action specified by ON_CALL().
   684  TEST(DoDefaultTest, DoesWhatOnCallSpecifies) {
   685    MockClass mock;
   686    ON_CALL(mock, IntFunc(_))
   687        .WillByDefault(Return(2));
   688    EXPECT_CALL(mock, IntFunc(_))
   689        .WillOnce(DoDefault());
   690    EXPECT_EQ(2, mock.IntFunc(false));
   691  }
   692  
   693  // Tests that using DoDefault() in ON_CALL() leads to a run-time failure.
   694  TEST(DoDefaultTest, CannotBeUsedInOnCall) {
   695    MockClass mock;
   696    EXPECT_NONFATAL_FAILURE({  // NOLINT
   697      ON_CALL(mock, IntFunc(_))
   698        .WillByDefault(DoDefault());
   699    }, "DoDefault() cannot be used in ON_CALL()");
   700  }
   701  
   702  // Tests that SetArgPointee<N>(v) sets the variable pointed to by
   703  // the N-th (0-based) argument to v.
   704  TEST(SetArgPointeeTest, SetsTheNthPointee) {
   705    typedef void MyFunction(bool, int*, char*);
   706    Action<MyFunction> a = SetArgPointee<1>(2);
   707  
   708    int n = 0;
   709    char ch = '\0';
   710    a.Perform(make_tuple(true, &n, &ch));
   711    EXPECT_EQ(2, n);
   712    EXPECT_EQ('\0', ch);
   713  
   714    a = SetArgPointee<2>('a');
   715    n = 0;
   716    ch = '\0';
   717    a.Perform(make_tuple(true, &n, &ch));
   718    EXPECT_EQ(0, n);
   719    EXPECT_EQ('a', ch);
   720  }
   721  
   722  #if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN)
   723  // Tests that SetArgPointee<N>() accepts a string literal.
   724  // GCC prior to v4.0 and the Symbian compiler do not support this.
   725  TEST(SetArgPointeeTest, AcceptsStringLiteral) {
   726    typedef void MyFunction(std::string*, const char**);
   727    Action<MyFunction> a = SetArgPointee<0>("hi");
   728    std::string str;
   729    const char* ptr = NULL;
   730    a.Perform(make_tuple(&str, &ptr));
   731    EXPECT_EQ("hi", str);
   732    EXPECT_TRUE(ptr == NULL);
   733  
   734    a = SetArgPointee<1>("world");
   735    str = "";
   736    a.Perform(make_tuple(&str, &ptr));
   737    EXPECT_EQ("", str);
   738    EXPECT_STREQ("world", ptr);
   739  }
   740  
   741  TEST(SetArgPointeeTest, AcceptsWideStringLiteral) {
   742    typedef void MyFunction(const wchar_t**);
   743    Action<MyFunction> a = SetArgPointee<0>(L"world");
   744    const wchar_t* ptr = NULL;
   745    a.Perform(make_tuple(&ptr));
   746    EXPECT_STREQ(L"world", ptr);
   747  
   748  # if GTEST_HAS_STD_WSTRING
   749  
   750    typedef void MyStringFunction(std::wstring*);
   751    Action<MyStringFunction> a2 = SetArgPointee<0>(L"world");
   752    std::wstring str = L"";
   753    a2.Perform(make_tuple(&str));
   754    EXPECT_EQ(L"world", str);
   755  
   756  # endif
   757  }
   758  #endif
   759  
   760  // Tests that SetArgPointee<N>() accepts a char pointer.
   761  TEST(SetArgPointeeTest, AcceptsCharPointer) {
   762    typedef void MyFunction(bool, std::string*, const char**);
   763    const char* const hi = "hi";
   764    Action<MyFunction> a = SetArgPointee<1>(hi);
   765    std::string str;
   766    const char* ptr = NULL;
   767    a.Perform(make_tuple(true, &str, &ptr));
   768    EXPECT_EQ("hi", str);
   769    EXPECT_TRUE(ptr == NULL);
   770  
   771    char world_array[] = "world";
   772    char* const world = world_array;
   773    a = SetArgPointee<2>(world);
   774    str = "";
   775    a.Perform(make_tuple(true, &str, &ptr));
   776    EXPECT_EQ("", str);
   777    EXPECT_EQ(world, ptr);
   778  }
   779  
   780  TEST(SetArgPointeeTest, AcceptsWideCharPointer) {
   781    typedef void MyFunction(bool, const wchar_t**);
   782    const wchar_t* const hi = L"hi";
   783    Action<MyFunction> a = SetArgPointee<1>(hi);
   784    const wchar_t* ptr = NULL;
   785    a.Perform(make_tuple(true, &ptr));
   786    EXPECT_EQ(hi, ptr);
   787  
   788  # if GTEST_HAS_STD_WSTRING
   789  
   790    typedef void MyStringFunction(bool, std::wstring*);
   791    wchar_t world_array[] = L"world";
   792    wchar_t* const world = world_array;
   793    Action<MyStringFunction> a2 = SetArgPointee<1>(world);
   794    std::wstring str;
   795    a2.Perform(make_tuple(true, &str));
   796    EXPECT_EQ(world_array, str);
   797  # endif
   798  }
   799  
   800  #if GTEST_HAS_PROTOBUF_
   801  
   802  // Tests that SetArgPointee<N>(proto_buffer) sets the v1 protobuf
   803  // variable pointed to by the N-th (0-based) argument to proto_buffer.
   804  TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
   805    TestMessage* const msg = new TestMessage;
   806    msg->set_member("yes");
   807    TestMessage orig_msg;
   808    orig_msg.CopyFrom(*msg);
   809  
   810    Action<void(bool, TestMessage*)> a = SetArgPointee<1>(*msg);
   811    // SetArgPointee<N>(proto_buffer) makes a copy of proto_buffer
   812    // s.t. the action works even when the original proto_buffer has
   813    // died.  We ensure this behavior by deleting msg before using the
   814    // action.
   815    delete msg;
   816  
   817    TestMessage dest;
   818    EXPECT_FALSE(orig_msg.Equals(dest));
   819    a.Perform(make_tuple(true, &dest));
   820    EXPECT_TRUE(orig_msg.Equals(dest));
   821  }
   822  
   823  // Tests that SetArgPointee<N>(proto_buffer) sets the
   824  // ::ProtocolMessage variable pointed to by the N-th (0-based)
   825  // argument to proto_buffer.
   826  TEST(SetArgPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
   827    TestMessage* const msg = new TestMessage;
   828    msg->set_member("yes");
   829    TestMessage orig_msg;
   830    orig_msg.CopyFrom(*msg);
   831  
   832    Action<void(bool, ::ProtocolMessage*)> a = SetArgPointee<1>(*msg);
   833    // SetArgPointee<N>(proto_buffer) makes a copy of proto_buffer
   834    // s.t. the action works even when the original proto_buffer has
   835    // died.  We ensure this behavior by deleting msg before using the
   836    // action.
   837    delete msg;
   838  
   839    TestMessage dest;
   840    ::ProtocolMessage* const dest_base = &dest;
   841    EXPECT_FALSE(orig_msg.Equals(dest));
   842    a.Perform(make_tuple(true, dest_base));
   843    EXPECT_TRUE(orig_msg.Equals(dest));
   844  }
   845  
   846  // Tests that SetArgPointee<N>(proto2_buffer) sets the v2
   847  // protobuf variable pointed to by the N-th (0-based) argument to
   848  // proto2_buffer.
   849  TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
   850    using testing::internal::FooMessage;
   851    FooMessage* const msg = new FooMessage;
   852    msg->set_int_field(2);
   853    msg->set_string_field("hi");
   854    FooMessage orig_msg;
   855    orig_msg.CopyFrom(*msg);
   856  
   857    Action<void(bool, FooMessage*)> a = SetArgPointee<1>(*msg);
   858    // SetArgPointee<N>(proto2_buffer) makes a copy of
   859    // proto2_buffer s.t. the action works even when the original
   860    // proto2_buffer has died.  We ensure this behavior by deleting msg
   861    // before using the action.
   862    delete msg;
   863  
   864    FooMessage dest;
   865    dest.set_int_field(0);
   866    a.Perform(make_tuple(true, &dest));
   867    EXPECT_EQ(2, dest.int_field());
   868    EXPECT_EQ("hi", dest.string_field());
   869  }
   870  
   871  // Tests that SetArgPointee<N>(proto2_buffer) sets the
   872  // proto2::Message variable pointed to by the N-th (0-based) argument
   873  // to proto2_buffer.
   874  TEST(SetArgPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
   875    using testing::internal::FooMessage;
   876    FooMessage* const msg = new FooMessage;
   877    msg->set_int_field(2);
   878    msg->set_string_field("hi");
   879    FooMessage orig_msg;
   880    orig_msg.CopyFrom(*msg);
   881  
   882    Action<void(bool, ::proto2::Message*)> a = SetArgPointee<1>(*msg);
   883    // SetArgPointee<N>(proto2_buffer) makes a copy of
   884    // proto2_buffer s.t. the action works even when the original
   885    // proto2_buffer has died.  We ensure this behavior by deleting msg
   886    // before using the action.
   887    delete msg;
   888  
   889    FooMessage dest;
   890    dest.set_int_field(0);
   891    ::proto2::Message* const dest_base = &dest;
   892    a.Perform(make_tuple(true, dest_base));
   893    EXPECT_EQ(2, dest.int_field());
   894    EXPECT_EQ("hi", dest.string_field());
   895  }
   896  
   897  #endif  // GTEST_HAS_PROTOBUF_
   898  
   899  // Tests that SetArgumentPointee<N>(v) sets the variable pointed to by
   900  // the N-th (0-based) argument to v.
   901  TEST(SetArgumentPointeeTest, SetsTheNthPointee) {
   902    typedef void MyFunction(bool, int*, char*);
   903    Action<MyFunction> a = SetArgumentPointee<1>(2);
   904  
   905    int n = 0;
   906    char ch = '\0';
   907    a.Perform(make_tuple(true, &n, &ch));
   908    EXPECT_EQ(2, n);
   909    EXPECT_EQ('\0', ch);
   910  
   911    a = SetArgumentPointee<2>('a');
   912    n = 0;
   913    ch = '\0';
   914    a.Perform(make_tuple(true, &n, &ch));
   915    EXPECT_EQ(0, n);
   916    EXPECT_EQ('a', ch);
   917  }
   918  
   919  #if GTEST_HAS_PROTOBUF_
   920  
   921  // Tests that SetArgumentPointee<N>(proto_buffer) sets the v1 protobuf
   922  // variable pointed to by the N-th (0-based) argument to proto_buffer.
   923  TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferType) {
   924    TestMessage* const msg = new TestMessage;
   925    msg->set_member("yes");
   926    TestMessage orig_msg;
   927    orig_msg.CopyFrom(*msg);
   928  
   929    Action<void(bool, TestMessage*)> a = SetArgumentPointee<1>(*msg);
   930    // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
   931    // s.t. the action works even when the original proto_buffer has
   932    // died.  We ensure this behavior by deleting msg before using the
   933    // action.
   934    delete msg;
   935  
   936    TestMessage dest;
   937    EXPECT_FALSE(orig_msg.Equals(dest));
   938    a.Perform(make_tuple(true, &dest));
   939    EXPECT_TRUE(orig_msg.Equals(dest));
   940  }
   941  
   942  // Tests that SetArgumentPointee<N>(proto_buffer) sets the
   943  // ::ProtocolMessage variable pointed to by the N-th (0-based)
   944  // argument to proto_buffer.
   945  TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProtoBufferBaseType) {
   946    TestMessage* const msg = new TestMessage;
   947    msg->set_member("yes");
   948    TestMessage orig_msg;
   949    orig_msg.CopyFrom(*msg);
   950  
   951    Action<void(bool, ::ProtocolMessage*)> a = SetArgumentPointee<1>(*msg);
   952    // SetArgumentPointee<N>(proto_buffer) makes a copy of proto_buffer
   953    // s.t. the action works even when the original proto_buffer has
   954    // died.  We ensure this behavior by deleting msg before using the
   955    // action.
   956    delete msg;
   957  
   958    TestMessage dest;
   959    ::ProtocolMessage* const dest_base = &dest;
   960    EXPECT_FALSE(orig_msg.Equals(dest));
   961    a.Perform(make_tuple(true, dest_base));
   962    EXPECT_TRUE(orig_msg.Equals(dest));
   963  }
   964  
   965  // Tests that SetArgumentPointee<N>(proto2_buffer) sets the v2
   966  // protobuf variable pointed to by the N-th (0-based) argument to
   967  // proto2_buffer.
   968  TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferType) {
   969    using testing::internal::FooMessage;
   970    FooMessage* const msg = new FooMessage;
   971    msg->set_int_field(2);
   972    msg->set_string_field("hi");
   973    FooMessage orig_msg;
   974    orig_msg.CopyFrom(*msg);
   975  
   976    Action<void(bool, FooMessage*)> a = SetArgumentPointee<1>(*msg);
   977    // SetArgumentPointee<N>(proto2_buffer) makes a copy of
   978    // proto2_buffer s.t. the action works even when the original
   979    // proto2_buffer has died.  We ensure this behavior by deleting msg
   980    // before using the action.
   981    delete msg;
   982  
   983    FooMessage dest;
   984    dest.set_int_field(0);
   985    a.Perform(make_tuple(true, &dest));
   986    EXPECT_EQ(2, dest.int_field());
   987    EXPECT_EQ("hi", dest.string_field());
   988  }
   989  
   990  // Tests that SetArgumentPointee<N>(proto2_buffer) sets the
   991  // proto2::Message variable pointed to by the N-th (0-based) argument
   992  // to proto2_buffer.
   993  TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) {
   994    using testing::internal::FooMessage;
   995    FooMessage* const msg = new FooMessage;
   996    msg->set_int_field(2);
   997    msg->set_string_field("hi");
   998    FooMessage orig_msg;
   999    orig_msg.CopyFrom(*msg);
  1000  
  1001    Action<void(bool, ::proto2::Message*)> a = SetArgumentPointee<1>(*msg);
  1002    // SetArgumentPointee<N>(proto2_buffer) makes a copy of
  1003    // proto2_buffer s.t. the action works even when the original
  1004    // proto2_buffer has died.  We ensure this behavior by deleting msg
  1005    // before using the action.
  1006    delete msg;
  1007  
  1008    FooMessage dest;
  1009    dest.set_int_field(0);
  1010    ::proto2::Message* const dest_base = &dest;
  1011    a.Perform(make_tuple(true, dest_base));
  1012    EXPECT_EQ(2, dest.int_field());
  1013    EXPECT_EQ("hi", dest.string_field());
  1014  }
  1015  
  1016  #endif  // GTEST_HAS_PROTOBUF_
  1017  
  1018  // Sample functions and functors for testing Invoke() and etc.
  1019  int Nullary() { return 1; }
  1020  
  1021  class NullaryFunctor {
  1022   public:
  1023    int operator()() { return 2; }
  1024  };
  1025  
  1026  bool g_done = false;
  1027  void VoidNullary() { g_done = true; }
  1028  
  1029  class VoidNullaryFunctor {
  1030   public:
  1031    void operator()() { g_done = true; }
  1032  };
  1033  
  1034  class Foo {
  1035   public:
  1036    Foo() : value_(123) {}
  1037  
  1038    int Nullary() const { return value_; }
  1039  
  1040   private:
  1041    int value_;
  1042  };
  1043  
  1044  // Tests InvokeWithoutArgs(function).
  1045  TEST(InvokeWithoutArgsTest, Function) {
  1046    // As an action that takes one argument.
  1047    Action<int(int)> a = InvokeWithoutArgs(Nullary);  // NOLINT
  1048    EXPECT_EQ(1, a.Perform(make_tuple(2)));
  1049  
  1050    // As an action that takes two arguments.
  1051    Action<int(int, double)> a2 = InvokeWithoutArgs(Nullary);  // NOLINT
  1052    EXPECT_EQ(1, a2.Perform(make_tuple(2, 3.5)));
  1053  
  1054    // As an action that returns void.
  1055    Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary);  // NOLINT
  1056    g_done = false;
  1057    a3.Perform(make_tuple(1));
  1058    EXPECT_TRUE(g_done);
  1059  }
  1060  
  1061  // Tests InvokeWithoutArgs(functor).
  1062  TEST(InvokeWithoutArgsTest, Functor) {
  1063    // As an action that takes no argument.
  1064    Action<int()> a = InvokeWithoutArgs(NullaryFunctor());  // NOLINT
  1065    EXPECT_EQ(2, a.Perform(make_tuple()));
  1066  
  1067    // As an action that takes three arguments.
  1068    Action<int(int, double, char)> a2 =  // NOLINT
  1069        InvokeWithoutArgs(NullaryFunctor());
  1070    EXPECT_EQ(2, a2.Perform(make_tuple(3, 3.5, 'a')));
  1071  
  1072    // As an action that returns void.
  1073    Action<void()> a3 = InvokeWithoutArgs(VoidNullaryFunctor());
  1074    g_done = false;
  1075    a3.Perform(make_tuple());
  1076    EXPECT_TRUE(g_done);
  1077  }
  1078  
  1079  // Tests InvokeWithoutArgs(obj_ptr, method).
  1080  TEST(InvokeWithoutArgsTest, Method) {
  1081    Foo foo;
  1082    Action<int(bool, char)> a =  // NOLINT
  1083        InvokeWithoutArgs(&foo, &Foo::Nullary);
  1084    EXPECT_EQ(123, a.Perform(make_tuple(true, 'a')));
  1085  }
  1086  
  1087  // Tests using IgnoreResult() on a polymorphic action.
  1088  TEST(IgnoreResultTest, PolymorphicAction) {
  1089    Action<void(int)> a = IgnoreResult(Return(5));  // NOLINT
  1090    a.Perform(make_tuple(1));
  1091  }
  1092  
  1093  // Tests using IgnoreResult() on a monomorphic action.
  1094  
  1095  int ReturnOne() {
  1096    g_done = true;
  1097    return 1;
  1098  }
  1099  
  1100  TEST(IgnoreResultTest, MonomorphicAction) {
  1101    g_done = false;
  1102    Action<void()> a = IgnoreResult(Invoke(ReturnOne));
  1103    a.Perform(make_tuple());
  1104    EXPECT_TRUE(g_done);
  1105  }
  1106  
  1107  // Tests using IgnoreResult() on an action that returns a class type.
  1108  
  1109  MyClass ReturnMyClass(double /* x */) {
  1110    g_done = true;
  1111    return MyClass();
  1112  }
  1113  
  1114  TEST(IgnoreResultTest, ActionReturningClass) {
  1115    g_done = false;
  1116    Action<void(int)> a = IgnoreResult(Invoke(ReturnMyClass));  // NOLINT
  1117    a.Perform(make_tuple(2));
  1118    EXPECT_TRUE(g_done);
  1119  }
  1120  
  1121  TEST(AssignTest, Int) {
  1122    int x = 0;
  1123    Action<void(int)> a = Assign(&x, 5);
  1124    a.Perform(make_tuple(0));
  1125    EXPECT_EQ(5, x);
  1126  }
  1127  
  1128  TEST(AssignTest, String) {
  1129    ::std::string x;
  1130    Action<void(void)> a = Assign(&x, "Hello, world");
  1131    a.Perform(make_tuple());
  1132    EXPECT_EQ("Hello, world", x);
  1133  }
  1134  
  1135  TEST(AssignTest, CompatibleTypes) {
  1136    double x = 0;
  1137    Action<void(int)> a = Assign(&x, 5);
  1138    a.Perform(make_tuple(0));
  1139    EXPECT_DOUBLE_EQ(5, x);
  1140  }
  1141  
  1142  #if !GTEST_OS_WINDOWS_MOBILE
  1143  
  1144  class SetErrnoAndReturnTest : public testing::Test {
  1145   protected:
  1146    virtual void SetUp() { errno = 0; }
  1147    virtual void TearDown() { errno = 0; }
  1148  };
  1149  
  1150  TEST_F(SetErrnoAndReturnTest, Int) {
  1151    Action<int(void)> a = SetErrnoAndReturn(ENOTTY, -5);
  1152    EXPECT_EQ(-5, a.Perform(make_tuple()));
  1153    EXPECT_EQ(ENOTTY, errno);
  1154  }
  1155  
  1156  TEST_F(SetErrnoAndReturnTest, Ptr) {
  1157    int x;
  1158    Action<int*(void)> a = SetErrnoAndReturn(ENOTTY, &x);
  1159    EXPECT_EQ(&x, a.Perform(make_tuple()));
  1160    EXPECT_EQ(ENOTTY, errno);
  1161  }
  1162  
  1163  TEST_F(SetErrnoAndReturnTest, CompatibleTypes) {
  1164    Action<double()> a = SetErrnoAndReturn(EINVAL, 5);
  1165    EXPECT_DOUBLE_EQ(5.0, a.Perform(make_tuple()));
  1166    EXPECT_EQ(EINVAL, errno);
  1167  }
  1168  
  1169  #endif  // !GTEST_OS_WINDOWS_MOBILE
  1170  
  1171  // Tests ByRef().
  1172  
  1173  // Tests that ReferenceWrapper<T> is copyable.
  1174  TEST(ByRefTest, IsCopyable) {
  1175    const std::string s1 = "Hi";
  1176    const std::string s2 = "Hello";
  1177  
  1178    ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper =
  1179        ByRef(s1);
  1180    const std::string& r1 = ref_wrapper;
  1181    EXPECT_EQ(&s1, &r1);
  1182  
  1183    // Assigns a new value to ref_wrapper.
  1184    ref_wrapper = ByRef(s2);
  1185    const std::string& r2 = ref_wrapper;
  1186    EXPECT_EQ(&s2, &r2);
  1187  
  1188    ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper1 =
  1189        ByRef(s1);
  1190    // Copies ref_wrapper1 to ref_wrapper.
  1191    ref_wrapper = ref_wrapper1;
  1192    const std::string& r3 = ref_wrapper;
  1193    EXPECT_EQ(&s1, &r3);
  1194  }
  1195  
  1196  // Tests using ByRef() on a const value.
  1197  TEST(ByRefTest, ConstValue) {
  1198    const int n = 0;
  1199    // int& ref = ByRef(n);  // This shouldn't compile - we have a
  1200                             // negative compilation test to catch it.
  1201    const int& const_ref = ByRef(n);
  1202    EXPECT_EQ(&n, &const_ref);
  1203  }
  1204  
  1205  // Tests using ByRef() on a non-const value.
  1206  TEST(ByRefTest, NonConstValue) {
  1207    int n = 0;
  1208  
  1209    // ByRef(n) can be used as either an int&,
  1210    int& ref = ByRef(n);
  1211    EXPECT_EQ(&n, &ref);
  1212  
  1213    // or a const int&.
  1214    const int& const_ref = ByRef(n);
  1215    EXPECT_EQ(&n, &const_ref);
  1216  }
  1217  
  1218  // Tests explicitly specifying the type when using ByRef().
  1219  TEST(ByRefTest, ExplicitType) {
  1220    int n = 0;
  1221    const int& r1 = ByRef<const int>(n);
  1222    EXPECT_EQ(&n, &r1);
  1223  
  1224    // ByRef<char>(n);  // This shouldn't compile - we have a negative
  1225                        // compilation test to catch it.
  1226  
  1227    Derived d;
  1228    Derived& r2 = ByRef<Derived>(d);
  1229    EXPECT_EQ(&d, &r2);
  1230  
  1231    const Derived& r3 = ByRef<const Derived>(d);
  1232    EXPECT_EQ(&d, &r3);
  1233  
  1234    Base& r4 = ByRef<Base>(d);
  1235    EXPECT_EQ(&d, &r4);
  1236  
  1237    const Base& r5 = ByRef<const Base>(d);
  1238    EXPECT_EQ(&d, &r5);
  1239  
  1240    // The following shouldn't compile - we have a negative compilation
  1241    // test for it.
  1242    //
  1243    // Base b;
  1244    // ByRef<Derived>(b);
  1245  }
  1246  
  1247  // Tests that Google Mock prints expression ByRef(x) as a reference to x.
  1248  TEST(ByRefTest, PrintsCorrectly) {
  1249    int n = 42;
  1250    ::std::stringstream expected, actual;
  1251    testing::internal::UniversalPrinter<const int&>::Print(n, &expected);
  1252    testing::internal::UniversalPrint(ByRef(n), &actual);
  1253    EXPECT_EQ(expected.str(), actual.str());
  1254  }
  1255  
  1256  }  // Unnamed namespace