Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cpp11 branch - support for move only types #70

Open
tnovotny opened this issue Dec 10, 2016 · 2 comments
Open

cpp11 branch - support for move only types #70

tnovotny opened this issue Dec 10, 2016 · 2 comments

Comments

@tnovotny
Copy link
Collaborator

tnovotny commented Dec 10, 2016

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);
@dascandy
Copy link
Owner

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.

@tnovotny
Copy link
Collaborator Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants