christophgysin / googlemock

Automatically exported from code.google.com/p/googlemock
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

SetArgumentReferee #17

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Possible Enhancement:

template <size_t N, typename A, bool kIsProto>
class SetArgumentRefereeAction {
 public:
  // Constructs an action that sets the variable pointed to by the
  // N-th function argument to 'value'.
  explicit SetArgumentRefereeAction(const A& value) : value_(value) {}

  template <typename Result, typename ArgumentTuple>
      void Perform(const ArgumentTuple& args) const {
           CompileAssertTypesEqual<void, Result>();
       ::std::tr1::get<N>(args) = value_;
  }

 private:
  const A value_;
};

template <size_t N, typename T>
PolymorphicAction<
  internal::SetArgumentRefereeAction<
    N, T, internal::IsAProtocolMessage<T>::value> >
SetArgumentReferee(const T& x) {
  return MakePolymorphicAction(internal::SetArgumentRefereeAction<
      N, T, internal::IsAProtocolMessage<T>::value>(x));
}

with following test case:
#include <gmock/gmock.h>
#include <memory>

using testing::_;
using testing::Return;
using testing::Action;
using testing::ActionInterface;
using testing::MakeAction;
using testing::DoAll;
using testing::SetArgumentPointee;
using testing::SetArgumentReferee;
using testing::internal::Function;

class Foo
{
public:
  virtual bool func(int*) = 0;
  virtual bool funcRef(int&) = 0;
};

class MockFoo : public Foo
{
public:
  MOCK_METHOD1(func, bool(int*));
  MOCK_METHOD1(funcRef, bool(int&));
};

class Bar
{
public:
  explicit Bar(Foo* pFoo) : m_pFoo(pFoo)
  {
  }

  int CallIt()
  {
    int i = 0;
    m_pFoo->func(&i);
    return i;
  }

  int CallItRef()
  {
    int i = 0;
    m_pFoo->funcRef(i);
    return i;
  }

  Foo* m_pFoo;
};

TEST(Testitout, argpointee)
{
  MockFoo foo;
  EXPECT_CALL(foo, func(_))
    .WillOnce(DoAll(SetArgumentPointee<0>(5),Return(true)));

  EXPECT_CALL(foo, funcRef(_))
    .WillOnce(DoAll(SetArgumentReferee<0>(5),Return(true)));

  Bar bar(&foo);

  EXPECT_EQ(5, bar.CallIt());
  EXPECT_EQ(5, bar.CallItRef());
}

int main(int argc, char **argv) {

  std::cout << "Running main() from gtest_main.cc\n";
  testing::InitGoogleTest(&argc, argv);

  return RUN_ALL_TESTS();
}

Original issue reported on code.google.com by ecurbks...@gmail.com on 25 Dec 2008 at 6:02

GoogleCodeExporter commented 9 years ago

Original comment by shiq...@gmail.com on 26 Dec 2008 at 6:58

GoogleCodeExporter commented 9 years ago

Original comment by shiq...@gmail.com on 26 Dec 2008 at 6:59

GoogleCodeExporter commented 9 years ago
I've implemented SetArgReferee<k>(value) in the trunk, using the ACTION_P macro.

Original comment by zhanyong...@gmail.com on 19 Feb 2009 at 10:38