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

Poor compilation error messages with mock mismatch in expectation #3

Open
rollbear opened this issue Nov 22, 2015 · 1 comment
Open
Labels

Comments

@rollbear
Copy link
Owner

Some examples:

class C
{
  MAKE_MOCK1(foo, void(int));
  void bar(int);
};

TEST(a_test)
{
  C obj;
  REQUIRE_CALL(obj, foo(3)); // OK
  REQUIRE_CALL(obj, foo("")); // type mismatch
  REQUIRE_CALL(obj, bar(3)); // not a mock
  REQUIRE_CALL(obj, baz(3)); // no such function
}

All the above gives hideous compilation errors that should be possible to make more succinct.

A promising technique is:

template <typename C, typename F>
auto can_call(C* c, F&& f) -> decltype(f(c), std::true_type{}) { return {}; }
template <typename F>
auto can_call(const void*, F&&) -> std::false_type{} { return {}; }

#define CAN_CALL(obj, func) can_call(&(obj), [](auto p) -> decltype(p->func) {});

It could be used to test:

CAN_CALL(obj, foo("")) // -> std::false_type
CAN_CALL(obj, foo(3)) // -> std::true_type

However, all my attempts to use this when creating the expectation object have so far failed to produce any better/shorter compilation errors. It's a bit tricky... assistance highly desired.

@rollbear
Copy link
Owner Author

rollbear commented Jan 2, 2016

This is largely addressed by commit 76499fd

It gives a good message for what's wrong and where the error is, when the expectation does not match any signature, or is ambiguous, for the mock object. The message is a bit too verbose with many secondary errors following. It also fails to provide good information if the signature does match, but is not a mock.

Perhaps this is good enough?

I have written a better solution, based on the idea outlined initially, but unfortunately clang++ is the only compiler I've tried that it works for. Both g++ and VisualStudio 2015 fails miserably, and in completely different ways.

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

No branches or pull requests

1 participant