This is an archive of the discontinued LLVM Phabricator instance.

[Error/unittests] Add a FailedWithMessage gtest matcher
ClosedPublic

Authored by labath on Feb 20 2020, 6:32 AM.

Details

Summary

We already have a "Failed" matcher, which can be used to check any
property of the Error object. However, most frequently one just wants to
check the error message, and while this is possible with the "Failed"
matcher, it is also very convoluted
(Failed<ErrorInfoBase>(testing::Property(&ErrorInfoBase::message, "the
message"))).

Now, one can just write: FailedWithMessage("the message"). I expect that
most of the usages will remain this simple, but the argument of the
matcher is not limited to simple strings -- the argument of the matcher
can be any other matcher, so one can write more complicated assertions
if needed (FailedWithMessage(ContainsRegex("foo|bar"))). If one wants to
match multiple error messages, he can pass multiple arguments to the
matcher.

If one wants to match the message list as a whole (perhaps to check the
message count), I've also included a FailedWithMessageArray matcher,
which takes a single matcher receiving a vector of error message
strings.

Diff Detail

Event Timeline

labath created this revision.Feb 20 2020, 6:32 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 20 2020, 6:32 AM

You can take a look at D74899 to see how this would look in action. I've converted one file with a very complex set of error assertions to ensure that this can handle a variety of use cases.

sammccall accepted this revision.Feb 20 2020, 7:10 AM
This revision is now accepted and ready to land.Feb 20 2020, 7:10 AM
This revision was automatically updated to reflect the committed changes.