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

Mock method with multiple arguments returning std::unique_ptr fails on MSVC14 #799

Closed
torbjoernk opened this issue Jun 15, 2016 · 4 comments

Comments

@torbjoernk
Copy link

Mocking a method returning a std::unique_ptr does not compile with MSVC14 (i.e. Visual Studio 2015, v140). Works well with GCC 5.3.

The problem is with the mocked method taking three arguments (MOCK_METHOD3).

I'm not sure, whether this is me being dumb using gmock or whether it is a compiler bug. In case it is the latter, is it in the scope of GTest/GMock to work around this?

#include "gtest/gtest.h"
#include "gmock/gmock.h"

#include <memory>
#include <string>


namespace movelib {
  struct Data {
      int a{42};
  };

  class Mover {
    public:
      virtual std::unique_ptr<Data> takeIt() {
        return std::make_unique<Data>();
      }
      virtual std::unique_ptr<Data> takeIt(std::string const& a) {
        return std::make_unique<Data>();
      }
      virtual std::unique_ptr<Data> takeIt(std::string const& a, std::string const& b, std::string const& c) {
        return std::make_unique<Data>();
      }
  };
} // namespace movelib


class MoverMock : public movelib::Mover {
  public:
    MOCK_METHOD0(takeIt, std::unique_ptr<movelib::Data>());
    MOCK_METHOD1(takeIt, std::unique_ptr<movelib::Data>(std::string const&));
    MOCK_METHOD3(takeIt, std::unique_ptr<movelib::Data>(std::string const&, std::string const&, std::string const&));
};
1>C:\Users\Tobbe\Source\Repos\gmock_move_types\googletest\googlemock\include\gmock/gmock-spec-builders.h(1312): error C2280: 'std::unique_ptr<movelib::Data,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function
1>          with
1>          [
1>              _Ty=movelib::Data
1>          ]
1>  H:\Programs\VisualStudio 2015\VC\include\memory(1435): note: see declaration of 'std::unique_ptr<movelib::Data,std::default_delete<_Ty>>::unique_ptr'
1>          with
1>          [
1>              _Ty=movelib::Data
1>          ]
1>  C:\Users\Tobbe\Source\Repos\gmock_move_types\googletest\googlemock\include\gmock/gmock-spec-builders.h(1312): note: while compiling class template member function 'std::unique_ptr<movelib::Data,std::default_delete<_Ty>> testing::internal::ReferenceOrValueWrapper<T>::Unwrap(void)'
1>          with
1>          [
1>              _Ty=movelib::Data,
1>              T=std::unique_ptr<movelib::Data,std::default_delete<movelib::Data>>
1>          ]
1>  C:\Users\Tobbe\Source\Repos\gmock_move_types\googletest\googlemock\include\gmock/gmock-spec-builders.h(1376): note: see reference to function template instantiation 'std::unique_ptr<movelib::Data,std::default_delete<_Ty>> testing::internal::ReferenceOrValueWrapper<T>::Unwrap(void)' being compiled
1>          with
1>          [
1>              _Ty=movelib::Data,
1>              T=std::unique_ptr<movelib::Data,std::default_delete<movelib::Data>>
1>          ]
1>  C:\Users\Tobbe\Source\Repos\gmock_move_types\googletest\googlemock\include\gmock/gmock-spec-builders.h(1413): note: see reference to class template instantiation 'testing::internal::ReferenceOrValueWrapper<T>' being compiled
1>          with
1>          [
1>              T=std::unique_ptr<movelib::Data,std::default_delete<movelib::Data>>
1>          ]
1>  C:\Users\Tobbe\Source\Repos\gmock_move_types\googletest\googlemock\include\gmock/gmock-spec-builders.h(1542): note: see reference to class template instantiation 'testing::internal::ActionResultHolder<std::unique_ptr<movelib::Data,std::default_delete<_Ty>>>' being compiled
1>          with
1>          [
1>              _Ty=movelib::Data
1>          ]
1>  C:\Users\Tobbe\Source\Repos\gmock_move_types\googletest\googlemock\include\gmock/gmock-spec-builders.h(1536): note: while compiling class template member function 'testing::internal::UntypedActionResultHolderBase *testing::internal::FunctionMockerBase<R (A1,A2,A3)>::UntypedPerformAction(const void *,const void *) const'
1>          with
1>          [
1>              R=std::unique_ptr<movelib::Data,std::default_delete<movelib::Data>>,
1>              A1=const std::string &,
1>              A2=const std::string &,
1>              A3=const std::string &
1>          ]
1>  C:\Users\Tobbe\Source\Repos\gmock_move_types\googletest\googlemock\include\gmock/gmock-generated-function-mockers.h(128): note: see reference to class template instantiation 'testing::internal::FunctionMockerBase<R (A1,A2,A3)>' being compiled
1>          with
1>          [
1>              R=std::unique_ptr<movelib::Data,std::default_delete<movelib::Data>>,
1>              A1=const std::string &,
1>              A2=const std::string &,
1>              A3=const std::string &
1>          ]
1>  C:\Users\Tobbe\Source\Repos\gmock_move_types\main.cpp(39): note: see reference to class template instantiation 'testing::internal::FunctionMocker<std::unique_ptr<movelib::Data,std::default_delete<_Ty>> (const std::string &,const std::string &,const std::string &)>' being compiled
1>          with
1>          [
1>              _Ty=movelib::Data
1>          ]
@sbenzaquen
Copy link
Collaborator

C++11 features are only enabled if the compiler provides the right
__cplusplus value.
See the definition of GTEST_LANG_CXX11 in gtest-port.h
Verify that you are compiling with C++11 support and that the compiler is
setting the macro correctly.

Otherwise, you could just force it by setting GTEST_LANG_CXX11 to 1 in your
compilation flags.

_Sam

On Wed, Jun 15, 2016 at 1:34 PM, Torbjörn Klatt notifications@github.com
wrote:

Mocking a method returning a std::unique_ptr does not compile with MSVC14
(i.e. Visual Studio 2015, v140). Works well with GCC 5.3.

The problem is with the mocked method taking three arguments (MOCK_METHOD3
).

I'm not sure, whether this is me being dumb using gmock or whether it is a
compiler bug. In case it is the latter, is it in the scope of GTest/GMock
to work around this?

#include "gtest/gtest.h"
#include "gmock/gmock.h"

#include
#include

namespace movelib {
struct Data {
int a{42};
};

class Mover {
public:
virtual std::unique_ptr takeIt() {
return std::make_unique();
}
virtual std::unique_ptr takeIt(std::string const& a) {
return std::make_unique();
}
virtual std::unique_ptr takeIt(std::string const& a, std::string const& b, std::string const& c) {
return std::make_unique();
}
};
} // namespace movelib

class MoverMock : public movelib::Mover {
public:
MOCK_METHOD0(takeIt, std::unique_ptrmovelib::Data());
MOCK_METHOD1(takeIt, std::unique_ptrmovelib::Data(std::string const&));
MOCK_METHOD3(takeIt, std::unique_ptrmovelib::Data(std::string const&, std::string const&, std::string const&));
};

1>C:\Users\Tobbe\Source\Repos\gmock_move_types\googletest\googlemock\include\gmock/gmock-spec-builders.h(1312): error C2280: 'std::unique_ptrmovelib::Data,std::default_delete<_Ty>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function
1> with
1> [
1> _Ty=movelib::Data
1> ]
1> H:\Programs\VisualStudio 2015\VC\include\memory(1435): note: see declaration of 'std::unique_ptrmovelib::Data,std::default_delete<_Ty>::unique_ptr'
1> with
1> [
1> _Ty=movelib::Data
1> ]
1> C:\Users\Tobbe\Source\Repos\gmock_move_types\googletest\googlemock\include\gmock/gmock-spec-builders.h(1312): note: while compiling class template member function 'std::unique_ptrmovelib::Data,std::default_delete<_Ty> testing::internal::ReferenceOrValueWrapper::Unwrap(void)'
1> with
1> [
1> _Ty=movelib::Data,
1> T=std::unique_ptrmovelib::Data,std::default_deletemovelib::Data
1> ]
1> C:\Users\Tobbe\Source\Repos\gmock_move_types\googletest\googlemock\include\gmock/gmock-spec-builders.h(1376): note: see reference to function template instantiation 'std::unique_ptrmovelib::Data,std::default_delete<_Ty> testing::internal::ReferenceOrValueWrapper::Unwrap(void)' being compiled
1> with
1> [
1> _Ty=movelib::Data,
1> T=std::unique_ptrmovelib::Data,std::default_deletemovelib::Data
1> ]
1> C:\Users\Tobbe\Source\Repos\gmock_move_types\googletest\googlemock\include\gmock/gmock-spec-builders.h(1413): note: see reference to class template instantiation 'testing::internal::ReferenceOrValueWrapper' being compiled
1> with
1> [
1> T=std::unique_ptrmovelib::Data,std::default_deletemovelib::Data
1> ]
1> C:\Users\Tobbe\Source\Repos\gmock_move_types\googletest\googlemock\include\gmock/gmock-spec-builders.h(1542): note: see reference to class template instantiation 'testing::internal::ActionResultHolderstd::unique_ptr<movelib::Data,std::default_delete<_Ty>>' being compiled
1> with
1> [
1> _Ty=movelib::Data
1> ]
1> C:\Users\Tobbe\Source\Repos\gmock_move_types\googletest\googlemock\include\gmock/gmock-spec-builders.h(1536): note: while compiling class template member function 'testing::internal::UntypedActionResultHolderBase *testing::internal::FunctionMockerBase<R (A1,A2,A3)>::UntypedPerformAction(const void *,const void *) const'
1> with
1> [
1> R=std::unique_ptrmovelib::Data,std::default_deletemovelib::Data,
1> A1=const std::string &,
1> A2=const std::string &,
1> A3=const std::string &
1> ]
1> C:\Users\Tobbe\Source\Repos\gmock_move_types\googletest\googlemock\include\gmock/gmock-generated-function-mockers.h(128): note: see reference to class template instantiation 'testing::internal::FunctionMockerBase<R (A1,A2,A3)>' being compiled
1> with
1> [
1> R=std::unique_ptrmovelib::Data,std::default_deletemovelib::Data,
1> A1=const std::string &,
1> A2=const std::string &,
1> A3=const std::string &
1> ]
1> C:\Users\Tobbe\Source\Repos\gmock_move_types\main.cpp(39): note: see reference to class template instantiation 'testing::internal::FunctionMockerstd::unique_ptr<movelib::Data,std::default_delete<_Ty> (const std::string &,const std::string &,const std::string &)>' being compiled
1> with
1> [
1> _Ty=movelib::Data
1> ]


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#799, or mute the thread
https://github.com/notifications/unsubscribe/ANcRPR8_i4o8uZn4aYOac_rxG0coxU90ks5qMDfBgaJpZM4I2m2M
.

@torbjoernk
Copy link
Author

That was a perfect hint! Thanks a lot! 👍

In deed, Visual Studio 2015 (i.e. v140 / MSVC14 / _MSC_VER == 1900) has __cplusplus defined as 199711L. m(

I'll try to prepare a PR. Either for the header file gtest-port.h or the docu. I'd prefer the former.

@KindDragon
Copy link
Contributor

KindDragon commented Jun 17, 2016

I think this should be fixed in PR #811

@KindDragon
Copy link
Contributor

Can be closed. PR #1218 fixed this

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

4 participants