dascandy / hippomocks

GNU Lesser General Public License v2.1
196 stars 67 forks source link

[cpp11 branch] How to migrate old `ByRef` mocks to cpp11? #91

Closed torbjoernk closed 6 years ago

torbjoernk commented 6 years ago

I've got some old test code using mockRepo.ExpectCall(...).With(ByRef(var)) from the master branch's commit dca4725.

As we are switching our code to C++17 (with MSVC 19.14) we cannot stay on that due to #55.

How can we migrate the usages of ByRef to the new cpp11 branch as it seems to be missing.

dascandy commented 6 years ago

In the c++11 branch ByRef is replaced with std::ref.

tnovotny commented 6 years ago

When I try this with the SingleInclude/hippomocks.h on MSVC i get

hippomocks\singleinclude\hippomocks.h(2222): fatal error C1020: unexpected #79 
tnovotny commented 6 years ago

When I fix that and try it on the following code:

struct Bar {
  virtual ~Bar() = default;
  virtual void  bar(  ) = 0;
};

struct Foo{
  virtual ~Foo() = default;
  virtual void  bar( Bar const& ) = 0;
};

TEST(RefTest, std_ref) {
  MockRepository mocks;
  Bar* bar = mocks.Mock<Bar>();
  Foo* foo = mocks.Mock<Foo>();
  mocks.OnCall( foo, Foo::bar ).With( std::ref( *bar ) ).Do( []( Bar const& bar) {
    std::cout << std::addressof( bar ) << "\n";
} );

I get the following

test.cpp
1>c:\program files (x86)\microsoft visual studio\preview\enterprise\vc\tools\msvc\14.15.26608\include\tuple(833): error C2678: binary '==': no operator found which takes a left-hand operand of type 'const _Ty' (or there is no acceptable conversion)
1>        with
1>        [
1>            _Ty=std::reference_wrapper<Bar>
1>        ]
1>c:\program files (x86)\microsoft visual studio\preview\enterprise\vc\tools\msvc\14.15.26608\include\system_error(284): note: could be 'bool std::operator ==(const std::error_condition &,const std::error_condition &) noexcept'
1>c:\program files (x86)\microsoft visual studio\preview\enterprise\vc\tools\msvc\14.15.26608\include\system_error(278): note: or       'bool std::operator ==(const std::error_condition &,const std::error_code &) noexcept'
1>c:\program files (x86)\microsoft visual studio\preview\enterprise\vc\tools\msvc\14.15.26608\include\system_error(272): note: or       'bool std::operator ==(const std::error_code &,const std::error_condition &) noexcept'
1>c:\program files (x86)\microsoft visual studio\preview\enterprise\vc\tools\msvc\14.15.26608\include\system_error(266): note: or       'bool std::operator ==(const std::error_code &,const std::error_code &) noexcept'
1>c:\program files (x86)\microsoft visual studio\preview\enterprise\vc\tools\msvc\14.15.26608\include\exception(330): note: or       'bool std::operator ==(const std::exception_ptr &,std::nullptr_t) noexcept'
1>c:\program files (x86)\microsoft visual studio\preview\enterprise\vc\tools\msvc\14.15.26608\include\exception(325): note: or       'bool std::operator ==(std::nullptr_t,const std::exception_ptr &) noexcept'
1>c:\program files (x86)\microsoft visual studio\preview\enterprise\vc\tools\msvc\14.15.26608\include\exception(320): note: or       'bool std::operator ==(const std::exception_ptr &,const std::exception_ptr &) noexcept'
1>c:\program files (x86)\microsoft visual studio\preview\enterprise\vc\tools\msvc\14.15.26608\include\tuple(834): note: while trying to match the argument list '(const _Ty, const Bar)'
1>        with
1>        [
1>            _Ty=std::reference_wrapper<Bar>
1>        ]
1>c:\program files (x86)\microsoft visual studio\preview\enterprise\vc\tools\msvc\14.15.26608\include\tuple(876): note: see reference to function template instantiation 'bool std::tuple<std::reference_wrapper<Bar>>::_Equals<const Bar&>(const std::tuple<const Bar &> &) const' being compiled
1>c:\program files (x86)\microsoft visual studio\preview\enterprise\vc\tools\msvc\14.15.26608\include\tuple(876): note: see reference to function template instantiation 'bool std::tuple<std::reference_wrapper<Bar>>::_Equals<const Bar&>(const std::tuple<const Bar &> &) const' being compiled
1>c:\users\thomas\source\repos\hippomocks\singleinclude\hippomocks.h(1286): note: see reference to function template instantiation 'bool std::operator ==<std::reference_wrapper<Bar>,const Bar&>(const std::tuple<std::reference_wrapper<Bar>> &,const std::tuple<const Bar &> &)' being compiled
1>c:\users\thomas\source\repos\hippomocks\singleinclude\hippomocks.h(1284): note: while compiling class template member function 'bool HippoMocks::ComparableTuple<std::tuple<const Bar &>,std::reference_wrapper<Bar>>::equals(const std::tuple<const Bar &> &)'
1>c:\users\thomas\source\repos\hippomocks\singleinclude\hippomocks.h(1373): note: see reference to class template instantiation 'HippoMocks::ComparableTuple<std::tuple<const Bar &>,std::reference_wrapper<Bar>>' being compiled
1>c:\users\thomas\source\repos\reftest\reftest\test.cpp(31): note: see reference to function template instantiation 'HippoMocks::TCall<void,const Bar &> &HippoMocks::TCall<void,const Bar &>::With<std::reference_wrapper<Bar>>(std::reference_wrapper<Bar>)' being compiled
1>c:\users\thomas\source\repos\reftest\reftest\test.cpp(31): note: see reference to function template instantiation 'HippoMocks::TCall<void,const Bar &> &HippoMocks::TCall<void,const Bar &>::With<std::reference_wrapper<Bar>>(std::reference_wrapper<Bar>)' being compiled
1>Done building project "reftest.vcxproj" -- FAILED.

Any ideas what's going wrong?

tnovotny commented 6 years ago

Ok, I got it, to check the arg you need comparison, so if I add

bool operator==( Bar const& lhs, Bar const& rhs ) { return true; }

the code will compile and work as expected.