hippomocks icon indicating copy to clipboard operation
hippomocks copied to clipboard

cpp11 branch - support for move only types

Open tnovotny opened this issue 8 years ago • 2 comments

It would be very useful if functions with move only types such as uinique_ptr could be mocked.

class IFoo {
  virtual void bar( std::unique_ptr<IBar>) = 0:
}

I've tried to see where the problems are. Part of the problem is missing forwarding or moving

  template <int X, typename... Args>
  void expectation(Args... args)
  {
    std::tuple<Args...> argT(std::forward<Args>(args)...); /// <-- added forward here
    mock<Z> *realMock = mock<Z>::getRealThis();
    if (realMock->isZombie)
      RAISEEXCEPTION(ZombieMockException(realMock->repo));
    MockRepository *repo = realMock->repo;
    repo->DoVoidExpectation(realMock, realMock->translateX(X), std::move(argT)); /// <-- added move here
  }

Those can easily be changed. The more problematic issue is with constness.

template <typename Y, typename... Args, int... Nums>
Y _invoke(const std::function<Y(Args...)> &func, seq<Nums...>, const std::tuple<Args...>& args) {
   return func( std::forward<Args>(std::get<Nums>( *const_cast<std::tuple<Args...>*>(&args))...)...); /// <- removed const & forward here
}

With that "hack" the following code compiled.

MockRepository mocks;
auto foo = mocks.Mock<IFoo>();
mocks.ExpectCall(foo, IFoo::bar);

tnovotny avatar Dec 10 '16 20:12 tnovotny

I've looked into making the auto_ptr case compile for unique_ptr and I got mostly stuck with OnCall with returns. You can only return it once - because then it's gone. Not sure if I committed that change though; I think I haven't.

The tuple const-ness is an issue that needs fixing. I think it shouldn't be const in most cases actually. You'll run into issues with in/out parameters though.

dascandy avatar Dec 10 '16 21:12 dascandy

For returning move only types, either once is OK or a generator must be used. Do(...) can be used for that, so that seems reasonable. Out parameters makes no sense for a move only types, so that that doesn't compile also seems reasonable.

tnovotny avatar Dec 10 '16 22:12 tnovotny