-
Notifications
You must be signed in to change notification settings - Fork 86
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
Composed matchers #340
Comments
This is an interesting idea. I have very deliberately refrained from adding such things, because I think GMock suffers from having a too large API that no one can remember, but your example is undoubtedly very sweet. Thank you. Do you have a wish-list for matchers? |
Have a look at branch range_matchers and let me know what you think. |
Hey!
Basically, all of GMock matchers are quite universal and useful. These matchers make mock-testing significantly easier. This is the only reason I sometimes prefer GMock. |
Thank you! Just checked the branch:
But I was originally referring to composed matchers, not only range matchers. Making complex matchers from the basic and simple ones would be a great feature. Thus, matchers like In my initial example we can do such things (and I do in my code): struct S { int x; int y; };
ElementsAre(
AllOf(Field(&S::x, 10), Field(&S::y, 20)),
AllOf(Field(&S::x, 100))
) |
Here is my template<typename T, typename C, typename M>
auto Field(
T C::*member,
M const& matcher,
std::string const& member_name)
{
return trompeloeil::make_matcher<trompeloeil::wildcard>(
[](auto const& obj, auto const& /*name*/, auto const& member, M const& matcher) -> bool
{
if constexpr (trompeloeil::is_matcher<M>::value)
return matcher.matches(std::invoke(member, obj));
else
return matcher == std::invoke(member, obj);
},
[](std::ostream& os, auto const& member_name, auto const& member, M const& matcher)
{
os << " matching " << member_name << " with ";
trompeloeil::print(os, matcher);
},
member_name,
member,
matcher
);
} Probably, the name |
Thank you. I need to think a bit about the latter. I like the idea. |
Added these all. I'm using |
Thank you! Yeah, description of composed matchers might be quite long, but still informative. And few points about range matchers. Do they respect mutable range views like And, I suppose, along with a heterogeneous matcher (which
For homogeneous matching of ranges we can add extra overloads for iterator-based construction, e.g.: How's that? What do you say? |
The current implementation does not work with generators, but most can be made to work. I think
I'll definitely have a stab at making the matchers work with generators, and I'll take your name suggestions into account since I'm not very happy with the names I've used. I do fear a very unfortunate growth in the API. One of the reasons I originally made Trompeloeil, is that I strongly dislike the very bloated API of gmock (which has become even more bloated in the 10 years since then). Nothing in this branch adds any new functionality. Everything can already be expressed using |
In the example, you equal-compare two
Unfortunately, |
They should work with input ranges now, except for "ends_with". I'll try to work on the names tomorrow. |
Phiew, that took a lot of time. The range-matchers are now overloaded to either take matchers as direct arguments, or a range of elements to compare with. Also improved the names a bit (IMO). Curiously, it works fine to pass, e.g. a std::vector of matchers instead of raw values for equality comparison, provided that all matchers are of the same type. I don't really see a use for it, but I don't see a reason to add code to explicitly ban it either. It does look pretty neat. Thanks for all the help. |
Thank you! |
Merged to main. I will leave the issue open until a release has been tagged. |
Included in release v49 |
There are no useful composed matchers like the ones in GMock:
ElementsAre
,AllOf
,Field
etc. This might be easily implemented using C++11/14. Here is C++20 implementation which I use in my code (C++14 requires a little bit more code for that):The text was updated successfully, but these errors were encountered: