diff options
author | Pavel Labath <pavel@labath.sk> | 2020-02-20 15:06:01 +0100 |
---|---|---|
committer | Pavel Labath <pavel@labath.sk> | 2020-02-21 15:29:48 +0100 |
commit | b55c58a2d569f2d92333b05c1a7a00114d75e0a6 (patch) | |
tree | a241e4ff88fc7ff7ce105f5513de3584a141bc86 /llvm/unittests/Support/ErrorTest.cpp | |
parent | d33e96b68c6f6424dc81145b1301f4cd478e84a2 (diff) | |
download | llvm-b55c58a2d569f2d92333b05c1a7a00114d75e0a6.zip llvm-b55c58a2d569f2d92333b05c1a7a00114d75e0a6.tar.gz llvm-b55c58a2d569f2d92333b05c1a7a00114d75e0a6.tar.bz2 |
[Error/unittests] Add a FailedWithMessage gtest matcher
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.
Reviewers: sammccall, dblaikie, jhenderson
Subscribers: llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D74898
Diffstat (limited to 'llvm/unittests/Support/ErrorTest.cpp')
-rw-r--r-- | llvm/unittests/Support/ErrorTest.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/llvm/unittests/Support/ErrorTest.cpp b/llvm/unittests/Support/ErrorTest.cpp index 8b8a621..37289e1 100644 --- a/llvm/unittests/Support/ErrorTest.cpp +++ b/llvm/unittests/Support/ErrorTest.cpp @@ -840,6 +840,46 @@ TEST(Error, HasValueMatcher) { " Actual: failed (CustomError {0})"); } +TEST(Error, FailedWithMessageMatcher) { + EXPECT_THAT_EXPECTED(Expected<int>(make_error<CustomError>(0)), + FailedWithMessage("CustomError {0}")); + + EXPECT_NONFATAL_FAILURE( + EXPECT_THAT_EXPECTED(Expected<int>(make_error<CustomError>(1)), + FailedWithMessage("CustomError {0}")), + "Expected: failed with Error whose message has 1 element that is equal " + "to \"CustomError {0}\"\n" + " Actual: failed (CustomError {1})"); + + EXPECT_NONFATAL_FAILURE( + EXPECT_THAT_EXPECTED(Expected<int>(0), + FailedWithMessage("CustomError {0}")), + "Expected: failed with Error whose message has 1 element that is equal " + "to \"CustomError {0}\"\n" + " Actual: succeeded with value 0"); + + EXPECT_NONFATAL_FAILURE( + EXPECT_THAT_EXPECTED(Expected<int>(make_error<CustomError>(0)), + FailedWithMessage("CustomError {0}", "CustomError {0}")), + "Expected: failed with Error whose message has 2 elements where\n" + "element #0 is equal to \"CustomError {0}\",\n" + "element #1 is equal to \"CustomError {0}\"\n" + " Actual: failed (CustomError {0}), which has 1 element"); + + EXPECT_NONFATAL_FAILURE( + EXPECT_THAT_EXPECTED( + Expected<int>(joinErrors(make_error<CustomError>(0), + make_error<CustomError>(0))), + FailedWithMessage("CustomError {0}")), + "Expected: failed with Error whose message has 1 element that is equal " + "to \"CustomError {0}\"\n" + " Actual: failed (CustomError {0}; CustomError {0}), which has 2 elements"); + + EXPECT_THAT_ERROR( + joinErrors(make_error<CustomError>(0), make_error<CustomError>(0)), + FailedWithMessageArray(testing::SizeIs(2))); +} + TEST(Error, C_API) { EXPECT_THAT_ERROR(unwrap(wrap(Error::success())), Succeeded()) << "Failed to round-trip Error success value via C API"; |