Index: include/llvm/Testing/Support/Error.h =================================================================== --- include/llvm/Testing/Support/Error.h +++ include/llvm/Testing/Support/Error.h @@ -28,6 +28,60 @@ template ExpectedHolder TakeExpected(Expected &&Exp) { return TakeExpected(Exp); } + +template +class ValueMatchesMono + : public testing::MatcherInterface &> { +public: + explicit ValueMatchesMono(const testing::Matcher &Matcher) + : Matcher(Matcher) {} + + bool MatchAndExplain(const ExpectedHolder &Holder, + testing::MatchResultListener *listener) const override { + if (!Holder.Success) + return false; + + bool result = Matcher.MatchAndExplain(*Holder.Exp, listener); + + if (result) + return result; + *listener << "("; + Matcher.DescribeNegationTo(listener->stream()); + *listener << ")"; + return result; + } + + void DescribeTo(std::ostream *OS) const override { + *OS << "succeeded with value ("; + Matcher.DescribeTo(OS); + *OS << ")"; + } + + void DescribeNegationTo(std::ostream *OS) const override { + *OS << "did not succeed or value ("; + Matcher.DescribeNegationTo(OS); + *OS << ")"; + } + +private: + testing::Matcher Matcher; +}; + +template +class ValueMatchesPoly { +public: + explicit ValueMatchesPoly(const M &Matcher) : Matcher(Matcher) {} + + template + operator testing::Matcher &>() const { + return MakeMatcher( + new ValueMatchesMono(testing::SafeMatcherCast(Matcher))); + } + +private: + M Matcher; +}; + } // namespace detail #define EXPECT_THAT_ERROR(Err, Matcher) \ @@ -43,21 +97,11 @@ MATCHER(Succeeded, "") { return arg.Success; } MATCHER(Failed, "") { return !arg.Success; } -MATCHER_P(HasValue, value, - "succeeded with value \"" + testing::PrintToString(value) + '"') { - if (!arg.Success) { - *result_listener << "operation failed"; - return false; - } - - if (*arg.Exp != value) { - *result_listener << "but \"" + testing::PrintToString(*arg.Exp) + - "\" != \"" + testing::PrintToString(value) + '"'; - return false; - } - - return true; +template +detail::ValueMatchesPoly HasValue(M Matcher) { + return detail::ValueMatchesPoly(Matcher); } + } // namespace llvm #endif Index: include/llvm/Testing/Support/SupportHelpers.h =================================================================== --- include/llvm/Testing/Support/SupportHelpers.h +++ include/llvm/Testing/Support/SupportHelpers.h @@ -38,8 +38,7 @@ template void PrintTo(const ExpectedHolder &Item, std::ostream *Out) { if (Item.Success) { - *Out << "succeeded with value \"" << ::testing::PrintToString(*Item.Exp) - << "\""; + *Out << "succeeded with value " << ::testing::PrintToString(*Item.Exp); } else { PrintTo(static_cast(Item), Out); } Index: unittests/Support/ErrorTest.cpp =================================================================== --- unittests/Support/ErrorTest.cpp +++ unittests/Support/ErrorTest.cpp @@ -735,23 +735,34 @@ EXPECT_THAT_EXPECTED(Expected(make_error(0)), Failed()); EXPECT_NONFATAL_FAILURE( EXPECT_THAT_EXPECTED(Expected(0), Failed()), - "Expected: failed\n Actual: succeeded with value \"0\""); + "Expected: failed\n Actual: succeeded with value 0"); EXPECT_THAT_EXPECTED(Expected(0), HasValue(0)); EXPECT_NONFATAL_FAILURE( EXPECT_THAT_EXPECTED(Expected(make_error(0)), HasValue(0)), - "Expected: succeeded with value \"0\"\n" + "Expected: succeeded with value (is equal to 0)\n" " Actual: failed (CustomError { 0})"); EXPECT_NONFATAL_FAILURE( EXPECT_THAT_EXPECTED(Expected(1), HasValue(0)), - "Expected: succeeded with value \"0\"\n" - " Actual: succeeded with value \"1\", but \"1\" != \"0\""); + "Expected: succeeded with value (is equal to 0)\n" + " Actual: succeeded with value 1, (isn't equal to 0)"); EXPECT_THAT_EXPECTED(Expected(make_error(0)), Failed()); int a = 1; EXPECT_THAT_EXPECTED(Expected(a), Succeeded()); - EXPECT_THAT_EXPECTED(Expected(a), HasValue(1)); + EXPECT_THAT_EXPECTED(Expected(a), HasValue(testing::Eq(1))); + + EXPECT_THAT_EXPECTED(Expected(1), HasValue(testing::Gt(0))); + EXPECT_NONFATAL_FAILURE( + EXPECT_THAT_EXPECTED(Expected(0), HasValue(testing::Gt(1))), + "Expected: succeeded with value (is > 1)\n" + " Actual: succeeded with value 0, (isn't > 1)"); + EXPECT_NONFATAL_FAILURE( + EXPECT_THAT_EXPECTED(Expected(make_error(0)), + HasValue(testing::Gt(1))), + "Expected: succeeded with value (is > 1)\n" + " Actual: failed (CustomError { 0})"); } } // end anon namespace