Index: include/llvm/Testing/Support/Error.h =================================================================== --- include/llvm/Testing/Support/Error.h +++ include/llvm/Testing/Support/Error.h @@ -28,6 +28,40 @@ return TakeExpected(Exp); } +class SucceededMatcher { +public: + bool MatchAndExplain(const ErrorHolder &Holder, + testing::MatchResultListener *Listener) const { + return Holder.Success(); + } + + bool MatchAndExplain(std::error_code EC, + testing::MatchResultListener *Listener) const { + *Listener << EC.message(); + return !EC; + } + + void DescribeTo(std::ostream *OS) const { *OS << "succeeded"; } + void DescribeNegationTo(std::ostream *OS) const { *OS << "failed"; } +}; + +class FailedMatcher { +public: + bool MatchAndExplain(const ErrorHolder &Holder, + testing::MatchResultListener *Listener) const { + return !Holder.Success(); + } + + bool MatchAndExplain(std::error_code EC, + testing::MatchResultListener *Listener) const { + *Listener << EC.message(); + return !!EC; + } + + void DescribeTo(std::ostream *OS) const { *OS << "failed"; } + void DescribeNegationTo(std::ostream *OS) const { *OS << "succeeded"; } +}; + template class ValueMatchesMono : public testing::MatcherInterface &> { @@ -140,8 +174,13 @@ #define ASSERT_THAT_EXPECTED(Err, Matcher) \ ASSERT_THAT(llvm::detail::TakeExpected(Err), Matcher) -MATCHER(Succeeded, "") { return arg.Success(); } -MATCHER(Failed, "") { return !arg.Success(); } +inline testing::PolymorphicMatcher Succeeded() { + return testing::MakePolymorphicMatcher(detail::SucceededMatcher()); +} + +inline testing::PolymorphicMatcher Failed() { + return testing::MakePolymorphicMatcher(detail::FailedMatcher()); +} template testing::Matcher Failed() { Index: unittests/BinaryFormat/TestFileMagic.cpp =================================================================== --- unittests/BinaryFormat/TestFileMagic.cpp +++ unittests/BinaryFormat/TestFileMagic.cpp @@ -11,22 +11,13 @@ #include "llvm/BinaryFormat/Magic.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Path.h" - +#include "llvm/Testing/Support/Error.h" #include "gtest/gtest.h" using namespace llvm; namespace fs = llvm::sys::fs; -#define ASSERT_NO_ERROR(x) \ - if (std::error_code ASSERT_NO_ERROR_ec = x) { \ - SmallString<128> MessageStorage; \ - raw_svector_ostream Message(MessageStorage); \ - Message << #x ": did not return errc::success.\n" \ - << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ - << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ - GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ - } else { \ - } +#define ASSERT_NO_ERROR(x) ASSERT_THAT(x, Succeeded()) class MagicTest : public testing::Test { protected: Index: unittests/Support/ErrorTest.cpp =================================================================== --- unittests/Support/ErrorTest.cpp +++ unittests/Support/ErrorTest.cpp @@ -758,12 +758,63 @@ } } +enum class test_error_code { + success = 0, + unspecified, + error_1, + error_2, +}; + +} // namespace + +namespace std { +template <> struct is_error_code_enum : std::true_type {}; +} // namespace std + +namespace { + +class TestErrorCategory : public std::error_category { +public: + const char *name() const noexcept override { return "TestErrorCategory"; } + std::string message(int Condition) const override { + switch (static_cast(Condition)) { + case test_error_code::success: + return "Success."; + case test_error_code::unspecified: + return "An unknown error has occurred."; + case test_error_code::error_1: + return "Error 1."; + case test_error_code::error_2: + return "Error 2."; + } + llvm_unreachable("Unrecognized test_error_code"); + } +}; + +static llvm::ManagedStatic TestErrCategory; +const std::error_category &TErrorCategory() { return *TestErrCategory; } + +static std::error_code make_error_code(test_error_code E) { + return std::error_code(static_cast(E), TErrorCategory()); +} + TEST(Error, ErrorMatchers) { + EXPECT_THAT(std::error_code(), Succeeded()); + EXPECT_THAT(make_error_code(test_error_code::success), Succeeded()); + EXPECT_NONFATAL_FAILURE( + EXPECT_THAT(make_error_code(test_error_code::error_1), Succeeded()), + "Expected: succeeded\n Actual: TestErrorCategory:2, Error 1."); + EXPECT_THAT_ERROR(Error::success(), Succeeded()); EXPECT_NONFATAL_FAILURE( EXPECT_THAT_ERROR(make_error(0), Succeeded()), "Expected: succeeded\n Actual: failed (CustomError {0})"); + EXPECT_THAT(make_error_code(test_error_code::error_1), Failed()); + EXPECT_NONFATAL_FAILURE( + EXPECT_THAT(make_error_code(test_error_code::success), Failed()), + "Expected: failed\n Actual: TestErrorCategory:0, Success."); + EXPECT_THAT_ERROR(make_error(0), Failed()); EXPECT_NONFATAL_FAILURE(EXPECT_THAT_ERROR(Error::success(), Failed()), "Expected: failed\n Actual: succeeded"); @@ -903,27 +954,6 @@ 0); } -enum class test_error_code { - unspecified = 1, - error_1, - error_2, -}; - -} // end anon namespace - -namespace std { - template <> - struct is_error_code_enum : std::true_type {}; -} // namespace std - -namespace { - -const std::error_category &TErrorCategory(); - -inline std::error_code make_error_code(test_error_code E) { - return std::error_code(static_cast(E), TErrorCategory()); -} - class TestDebugError : public ErrorInfo { public: using ErrorInfo::ErrorInfo; // inherit constructors @@ -931,25 +961,6 @@ static char ID; }; -class TestErrorCategory : public std::error_category { -public: - const char *name() const noexcept override { return "error"; } - std::string message(int Condition) const override { - switch (static_cast(Condition)) { - case test_error_code::unspecified: - return "An unknown error has occurred."; - case test_error_code::error_1: - return "Error 1."; - case test_error_code::error_2: - return "Error 2."; - } - llvm_unreachable("Unrecognized test_error_code"); - } -}; - -static llvm::ManagedStatic TestErrCategory; -const std::error_category &TErrorCategory() { return *TestErrCategory; } - char TestDebugError::ID; TEST(Error, SubtypeStringErrorTest) { Index: unittests/Support/FileOutputBufferTest.cpp =================================================================== --- unittests/Support/FileOutputBufferTest.cpp +++ unittests/Support/FileOutputBufferTest.cpp @@ -13,21 +13,13 @@ #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Testing/Support/Error.h" #include "gtest/gtest.h" using namespace llvm; using namespace llvm::sys; -#define ASSERT_NO_ERROR(x) \ - if (std::error_code ASSERT_NO_ERROR_ec = x) { \ - SmallString<128> MessageStorage; \ - raw_svector_ostream Message(MessageStorage); \ - Message << #x ": did not return errc::success.\n" \ - << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ - << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ - GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ - } else { \ - } +#define ASSERT_NO_ERROR(x) ASSERT_THAT(x, Succeeded()) namespace { TEST(FileOutputBuffer, Test) { Index: unittests/Support/Host.cpp =================================================================== --- unittests/Support/Host.cpp +++ unittests/Support/Host.cpp @@ -7,25 +7,16 @@ //===----------------------------------------------------------------------===// #include "llvm/Support/Host.h" -#include "llvm/Config/llvm-config.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/Triple.h" +#include "llvm/Config/llvm-config.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Path.h" #include "llvm/Support/Program.h" - +#include "llvm/Testing/Support/Error.h" #include "gtest/gtest.h" -#define ASSERT_NO_ERROR(x) \ - if (std::error_code ASSERT_NO_ERROR_ec = x) { \ - SmallString<128> MessageStorage; \ - raw_svector_ostream Message(MessageStorage); \ - Message << #x ": did not return errc::success.\n" \ - << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ - << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ - GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ - } else { \ - } +#define ASSERT_NO_ERROR(x) ASSERT_THAT(x, Succeeded()) using namespace llvm; Index: unittests/Support/Path.cpp =================================================================== --- unittests/Support/Path.cpp +++ unittests/Support/Path.cpp @@ -20,8 +20,9 @@ #include "llvm/Support/Host.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/raw_ostream.h" -#include "gtest/gtest.h" +#include "llvm/Testing/Support/Error.h" #include "gmock/gmock.h" +#include "gtest/gtest.h" #ifdef _WIN32 #include "llvm/ADT/ArrayRef.h" @@ -38,24 +39,9 @@ using namespace llvm; using namespace llvm::sys; -#define ASSERT_NO_ERROR(x) \ - if (std::error_code ASSERT_NO_ERROR_ec = x) { \ - SmallString<128> MessageStorage; \ - raw_svector_ostream Message(MessageStorage); \ - Message << #x ": did not return errc::success.\n" \ - << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ - << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ - GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ - } else { \ - } +#define ASSERT_NO_ERROR(x) ASSERT_THAT(x, Succeeded()); -#define ASSERT_ERROR(x) \ - if (!x) { \ - SmallString<128> MessageStorage; \ - raw_svector_ostream Message(MessageStorage); \ - Message << #x ": did not return a failure error code.\n"; \ - GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ - } +#define ASSERT_ERROR(x) ASSERT_THAT(x, Failed()); namespace { Index: unittests/Support/ProgramTest.cpp =================================================================== --- unittests/Support/ProgramTest.cpp +++ unittests/Support/ProgramTest.cpp @@ -12,6 +12,7 @@ #include "llvm/Support/ConvertUTF.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Path.h" +#include "llvm/Testing/Support/Error.h" #include "gtest/gtest.h" #include #if defined(__APPLE__) @@ -35,16 +36,8 @@ #error sleep_for is not implemented on your platform. #endif -#define ASSERT_NO_ERROR(x) \ - if (std::error_code ASSERT_NO_ERROR_ec = x) { \ - SmallString<128> MessageStorage; \ - raw_svector_ostream Message(MessageStorage); \ - Message << #x ": did not return errc::success.\n" \ - << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ - << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ - GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ - } else { \ - } +#define ASSERT_NO_ERROR(x) ASSERT_THAT(x, Succeeded()) + // From TestMain.cpp. extern const char *TestMainArgv0; Index: unittests/Support/ReplaceFileTest.cpp =================================================================== --- unittests/Support/ReplaceFileTest.cpp +++ unittests/Support/ReplaceFileTest.cpp @@ -12,19 +12,13 @@ #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" #include "llvm/Support/Process.h" +#include "llvm/Testing/Support/Error.h" #include "gtest/gtest.h" using namespace llvm; using namespace llvm::sys; -#define ASSERT_NO_ERROR(x) \ - do { \ - if (std::error_code ASSERT_NO_ERROR_ec = x) { \ - errs() << #x ": did not return errc::success.\n" \ - << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ - << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ - } \ - } while (false) +#define ASSERT_NO_ERROR(x) ASSERT_THAT(x, Succeeded()) namespace { std::error_code CreateFileWithContent(const SmallString<128> &FilePath, Index: unittests/Support/raw_pwrite_stream_test.cpp =================================================================== --- unittests/Support/raw_pwrite_stream_test.cpp +++ unittests/Support/raw_pwrite_stream_test.cpp @@ -11,20 +11,12 @@ #include "llvm/Support/FileSystem.h" #include "llvm/Support/FileUtilities.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Testing/Support/Error.h" #include "gtest/gtest.h" using namespace llvm; -#define ASSERT_NO_ERROR(x) \ - if (std::error_code ASSERT_NO_ERROR_ec = x) { \ - SmallString<128> MessageStorage; \ - raw_svector_ostream Message(MessageStorage); \ - Message << #x ": did not return errc::success.\n" \ - << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ - << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ - GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ - } else { \ - } +#define ASSERT_NO_ERROR(x) ASSERT_THAT(x, Succeeded()) namespace {