Custom Matchers

1,109 views
Skip to first unread message

Venu

unread,
Oct 25, 2010, 4:20:00 PM10/25/10
to Google C++ Mocking Framework

Hi,

is there a way to partially validate the matcher?

I have written a matcher with MATCHER_P(..)

I am validating a protocol buffer. the matcher i have written
validates the whole protocol buffer, but what if i want to validate
partially? like if i have another protocol buffer inside and i want to
validate the inner proto buff. i am thinking of writing an ACTION to
validate the inner message. but can i call a matcher from an
ACTION_P(..)?

any suggestions are appreciated...

--Venu

Vlad Losev

unread,
Oct 25, 2010, 8:12:26 PM10/25/10
to Venu, Google C++ Mocking Framework
Hi Venu,

What about defining something like

const ContainedProtoClass& SelectInterestingSubproto(const ContainerProtoClass& proto) {
  return proto.other_proto().interesting_subproto();
}

and then using ResultOf(&SelectInterestingSubproto, matcher_for_ContainedProtoClass)?

If the selection is only one level deep, you can use

Property(&ContainerProtoClass::interesting_subproto, matcher_for_ContainedProtoClass).

HTH,
Vlad

Venugopal Devarapalli

unread,
Oct 26, 2010, 9:51:09 AM10/26/10
to Vlad Losev, Google C++ Mocking Framework

Hi Vlad,

I have a situation like below:

EXPECT_CALL(mock_obj, func(validateProto(expectedProto))
.Times(1);

the proto that am validating is a hughe one, means it has lot of other protos inside.

Can i use an ACTION  to call another matcher from it?

or can i call a MATCHER from a method that was called by using "Invoke"?

--Venu

Vlad Losev

unread,
Oct 26, 2010, 1:55:35 PM10/26/10
to Google C++ Mocking Framework
On Tue, Oct 26, 2010 at 6:51 AM, Venugopal Devarapalli <dvg...@gmail.com> wrote:

Hi Vlad,

I have a situation like below:

EXPECT_CALL(mock_obj, func(validateProto(expectedProto))
.Times(1);

the proto that am validating is a hughe one, means it has lot of other protos inside.

Can i use an ACTION  to call another matcher from it?

or can i call a MATCHER from a method that was called by using "Invoke"?

No, on both counts. Simply put, a matcher is a specialized kind of predicate, and an action is like a function call. They are not designed to mix and match.

Now, ON_CALL is intended to let you imitate the behavior of other objects in the system, not to validate values. For that, you should use either advanced Google Test assertions (http://code.google.com/p/googletest/wiki/AdvancedGuide#Predicate_Assertions_for_Better_Error_Messages) or Google Mock's EXPECT_THAT (http://code.google.com/p/googlemock/wiki/CheatSheet#Matchers_as_Test_Assertions). If you intend to use EXPECT_THAT, see my previous post on how to select parts of a big object for using matchers on them.
Reply all
Reply to author
Forward
0 new messages