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

Usage with gtest/gmock not working as expected #1044

Closed
deblauwetom opened this issue Apr 8, 2018 · 3 comments
Closed

Usage with gtest/gmock not working as expected #1044

deblauwetom opened this issue Apr 8, 2018 · 3 comments
Labels
state: help needed the issue needs help to proceed

Comments

@deblauwetom
Copy link

deblauwetom commented Apr 8, 2018

I am trying to use the library with google test/google mock. It seems gtest is not able to print the json value out with its universal printer.

Example that fails:

#include <gtest/gtest.h>
#include <nlohmann/json.hpp>

using json = nlohmann::json;
using namespace testing;

TEST(EnsureJsonComparesCorrectly)
{
   auto test1 = R"BLA(
{
    "event" : "test",
    "description" : "Test description"
}
)BLA"_json;

   auto test2 = R"BLA(
{
    "description" : "Test description",
    "event" : "test"
}
)BLA"_json;
    EXPECT_THAT(test1, Eq(test2));
}

I am using 3.1.2 of nlohmann and 1.8.0 of google test/mock. I am compiling and running on macOS.

I also tried to use it with gmock, but it also fails. The myCall function is defined as:

void myCall(std::string url, nlohmann::json message);

So this is in gmock:

MOCK_METHOD2(myCall, void(std::string url, nlohmann::json message));

And then in the test I set an expectation, like this:

 EXPECT_CALL(*myMock, myCall(_, test1));

but every time gtest tries to print, in this case the "test1" object, then it crashes.

So what could causing this? Gtest is normally able to use a custom PrintTo function but I could not get that to work. Any pointers on how to solve it?

I have a "workaround" but it goes against the "json as first-class citizen" philosophy :)
Workaround for EXPECT_THAT:

EXPECT_THAT(test1.dump(), Eq(test2.dump()));

That is more or less acceptable, but not really.
And then for gmock it is really too much :) see:

json tmp;
json test;
EXPECT_CALL(*myMock, myCall(_, _)).WillOnce(SaveArg<1>(&tmp));
EXPECT_THAT(tmp.dump(), Eq(test.dump()));

But of course it should be as easy as:

EXPECT_CALL(*myMock, myCall(_, test));
@deblauwetom deblauwetom changed the title Usage with gtest not working as expected Usage with gtest/gmock not working as expected Apr 8, 2018
@nlohmann
Copy link
Owner

nlohmann commented Apr 9, 2018

I am not using Google Mock myself, so I have no idea what to do here.

@nlohmann nlohmann added the state: help needed the issue needs help to proceed label Apr 9, 2018
@deblauwetom
Copy link
Author

It seems I am hitting this situation: #709
And the solution is in googletest itself, however they still need to do a release of their current master as 1.8.1 which I hope is very soon.
The workaround suggested in #709 is not working for me unfortunately.

@feedhandler
Copy link

feedhandler commented Jul 27, 2018

Initially the workaround in #709 worked for me, but some time later it seemed to stop working. I wasn't able to find out why but I suspect it was related to the problem discussed in google/googletest#1149
Eventually I fixed it by adding overloads for my other types that are based off nlohmann::basic_json<>, e.g.

namespace nlohmann
{
  void PrintTo(const json& j, std::ostream* os)
  {
    *os << j.dump();
  }
  
  void PrintTo(const ordered_json& j, std::ostream* os)
  {
    *os << j.dump();
  }
}

where ordered_json is

using ordered_json = nlohmann::basic_json<ifo_map>;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: help needed the issue needs help to proceed
Projects
None yet
Development

No branches or pull requests

3 participants