calvert1991/googlemock

need generalized ContainerEq() where the user can specify how to compare two elements

Closed this issue · 2 comments

ContainerEq() is handy when comparing two collections of values.
However, it is hard-coded to compare two values by equality, using
operator==.  Sometimes this is impossible (when the objects don't
support ==) or undesired (when we want to use a special matcher to
compare two vlaues as it can generate better failure messages).

Perhaps something like this:

struct EqProto {
 template <typename T, typename Proto>
 Matcher<T> MatchRhs(const Proto& rhs) { return EqualsProto(rhs); }
};

EXPECT_THAT(vec1, Pointwise(EqProto(), vec2));

struct LessThan {
 template <typename T, typename U>
 Matcher<T> MatchRhs(const U& rhs) { return Lt(rhs); }
};

EXPECT_THAT(list1, Pointwise(LessThan(), list2));

The user of Pointwise() will define and pass to it a functor that has
a method template MatchRhs() (a better name welcome), which is
poor-man's lambda.  We can pre-define some commonly used functors.

Original issue reported on code.google.com by w...@google.com on 23 Mar 2010 at 5:39

Another idea is to use matchers of pairs in Pointwise().  For example,
nullary Lt() matches a pair where the first field < the second field
(see http://go/matchers), so we can say:

 EXPECT_THAT(vec1, Pointwise(Lt(), vec2));

to verify that each element in vec1 < the corresponding element in
vec2.  This doesn't involve any lambda or callback.  It doesn't let
you use existing EqualsProto(pb) matchers, so we'll need to define
nullary matchers like AreEqualProtos() to verify that the value it's
matching is a pair of equal protobufs.  It's not a big deal if we can
easily define AreEqualsProtos() in terms of EqualsProto(pb).

Original comment by w...@google.com on 6 Apr 2010 at 12:43

Original comment by vladlosev on 20 May 2010 at 10:18

  • Changed state: Fixed