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

Compilation error when using MOCK_METHOD1 from GMock and nlohmann::json #1212

Closed
urbanekpj opened this issue Aug 24, 2018 · 6 comments
Closed
Assignees
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Milestone

Comments

@urbanekpj
Copy link

  • What is the issue you have?
    Compilation error
  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?
    code :
#include "stdafx.h"
#include <gmock/gmock.h>
#include <nlohmann/json.hpp>

using Json = nlohmann::json;
//using Json = std::wstring;
namespace tests {
/**/
class GMockTest {
public:
    GMockTest() = default;
    virtual ~GMockTest() = default;
    virtual void read(const Json& data){

    };
};

class GMockTestMock final : public GMockTest {
public:
    ~GMockTestMock() override{};

    MOCK_METHOD1(read, void(const Json&));
};

using ::testing::_;
using ::testing::AnyNumber;
using ::testing::Invoke;
TEST(RequestClient, read_test)
{
    auto io = std::make_shared<GMockTestMock>();
    EXPECT_CALL(*io, read(_)).WillRepeatedly(Invoke([&](const Json& data) {
    }));
}
}
  • What is the expected behavior?
    code compiled
  • And what is the actual behavior instead?
    compilation errors:

1> \x86-windows-static\include\gmock\gmock-actions.h(388): error C2666: 'testing::internal::linked_ptr<testing::ActionInterface>::operator ==': 3 overloads have similar conversions
1> with
1> [
1> F=void (const nlohmann::basic_jsonstd::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer &)
1> ]
1> \x86-windows-static\include\gtest\internal\gtest-linked_ptr.h(186): note: could be 'bool testing::internal::linked_ptr<testing::ActionInterface>::operator ==(T *) const'
1> with
1> [
1> F=void (const nlohmann::basic_jsonstd::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer &),
1> T=testing::ActionInterface<void (const nlohmann::basic_jsonstd::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer &)>
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.15.26726\include\exception(321): note: or 'bool std::operator ==(const std::exception_ptr &,const std::exception_ptr &) noexcept' [found using argument-dependent lookup]
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.15.26726\include\exception(326): note: or 'bool std::operator ==(std::nullptr_t,const std::exception_ptr &) noexcept' [found using argument-dependent lookup]
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.15.26726\include\exception(331): note: or 'bool std::operator ==(const std::exception_ptr &,std::nullptr_t) noexcept' [found using argument-dependent lookup]
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.15.26726\include\system_error(266): note: or 'bool std::operator ==(const std::error_code &,const std::error_code &) noexcept' [found using argument-dependent lookup]
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.15.26726\include\system_error(272): note: or 'bool std::operator ==(const std::error_code &,const std::error_condition &) noexcept' [found using argument-dependent lookup]
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.15.26726\include\system_error(278): note: or 'bool std::operator ==(const std::error_condition &,const std::error_code &) noexcept' [found using argument-dependent lookup]
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.15.26726\include\system_error(284): note: or 'bool std::operator ==(const std::error_condition &,const std::error_condition &) noexcept' [found using argument-dependent lookup]
1> \x86-windows-static\include\nlohmann\json.hpp(16582): note: or 'bool nlohmann::basic_jsonstd::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer::operator ==(const nlohmann::basic_jsonstd::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer &,const nlohmann::basic_jsonstd::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer &) noexcept' [found using argument-dependent lookup]
1> \x86-windows-static\include\nlohmann\json.hpp(11112): note: or 'bool nlohmann::json_pointer<nlohmann::basic_jsonstd::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>::operator ==(const nlohmann::json_pointer<nlohmann::basic_jsonstd::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer> &,const nlohmann::json_pointer<nlohmann::basic_jsonstd::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer> &) noexcept' [found using argument-dependent lookup]
1> \x86-windows-static\include\nlohmann\json.hpp(16653): note: or 'bool nlohmann::operator ==<nullptr,0>(const nlohmann::basic_jsonstd::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer &,const ScalarType) noexcept'
1> with
1> [
1> ScalarType=nullptr
1> ]
1> \x86-windows-static\include\gmock\gmock-actions.h(388): note: while trying to match the argument list '(const testing::internal::linked_ptr<testing::ActionInterface>, nullptr)'
1> with
1> [
1> F=void (const nlohmann::basic_jsonstd::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer &)
1> ]
1> \x86-windows-static\include\gmock\gmock-actions.h(386): note: while compiling class template member function 'bool testing::Action::IsDoDefault(void) const'
1> with
1> [
1> F=void (const nlohmann::basic_jsonstd::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer &)
1> ]
1> \x86-windows-static\include\gmock\gmock-spec-builders.h(1712): note: see reference to function template instantiation 'bool testing::Action::IsDoDefault(void) const' being compiled
1> with
1> [
1> F=void (const nlohmann::basic_jsonstd::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer &)
1> ]
1> \x86-windows-static\include\gmock\gmock-spec-builders.h(1232): note: see reference to class template instantiation 'testing::Action' being compiled
1> with
1> [
1> F=void (const nlohmann::basic_jsonstd::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer &)
1> ]
1> \tests\test_jsonrpczmq.cpp(31): note: see reference to class template instantiation 'testing::internal::TypedExpectation' being compiled
1> with
1> [
1> F=void (const nlohmann::basic_jsonstd::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer &)
1> ]

@theodelrieu
Copy link
Contributor

This looks related to #960

@nlohmann
Copy link
Owner

It reminds me of #1067. @urbanekpj Could you check if the change from PR #1067 fixes your issue?

@urbanekpj
Copy link
Author

when i took it directly it didn't fix my issue .
but

    /*!
    @brief comparison: equal
    @copydoc operator==(const_reference, const_reference)
    */
    template < typename ScalarType, typename std::enable_if <
                   std::is_scalar<ScalarType>::value, int >::type = 0,
               typename std::enable_if <
                   !std::is_same<std::nullptr_t, std::remove_cv<ScalarType>>::value, int >::value >
    friend bool operator==(const_reference lhs, const ScalarType rhs) noexcept
    {
        return (lhs == basic_json(rhs));
    }

    /*!
    @brief comparison: equal
    @copydoc operator==(const_reference, const_reference)
    */
    template < typename ScalarType, typename std::enable_if <
                   std::is_scalar<ScalarType>::value, int >::type = 0,
               typename std::enable_if <
                   !std::is_same<std::nullptr_t, std::remove_cv<ScalarType>>::value, int >::value>
    friend bool operator==(const ScalarType lhs, const_reference rhs) noexcept
    {
        return (basic_json(lhs) == rhs);
    }

helped.

!std::is_same<std::nullptr_t, std::remove_cv<ScalarType>>::value, int >::value>

vs

!std::is_same<std::nullptr_t, std::remove_cv<ScalarType>>::value, int >::type = 0 >

but still it magic for me :)

@theodelrieu theodelrieu mentioned this issue Sep 6, 2018
4 tasks
@theodelrieu
Copy link
Contributor

Hello, could you try the problematic code with the changes made in #1228? I had a similar issue with a different mocking library and it fixed it, but I'd like to know that it fixes yours as well :)

@urbanekpj
Copy link
Author

@theodelrieu after quick test: it works for me also.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Sep 8, 2018
@nlohmann
Copy link
Owner

nlohmann commented Sep 9, 2018

Fixed after merging #1228.

@nlohmann nlohmann closed this as completed Sep 9, 2018
@nlohmann nlohmann self-assigned this Sep 9, 2018
@nlohmann nlohmann added this to the Release 3.2.1 milestone Sep 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

3 participants