From 1cd582460850f0b96e06fc6b0d60d3163a3dc257 Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Tue, 8 Mar 2022 17:38:02 +0000 Subject: [PATCH] Add a test case for errors::getMessage --- libserene/include/serene/errors.h | 3 ++ libserene/include/serene/errors/base.h | 4 +-- libserene/lib/errors.cpp | 10 +++++++ libserene/tests/errors/error_tests.cpp.inc | 32 +++++++++++++--------- 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/libserene/include/serene/errors.h b/libserene/include/serene/errors.h index 3d5dc81..74cff16 100644 --- a/libserene/include/serene/errors.h +++ b/libserene/include/serene/errors.h @@ -23,6 +23,8 @@ #include "serene/errors/errc.h" #include "serene/export.h" +#include + #define GET_CLASS_DEFS #include "serene/errors/errs.h.inc" @@ -39,6 +41,7 @@ SERENE_EXPORT llvm::Error makeError(Args &&...args) { return llvm::make_error(std::forward(args)...); }; +SERENE_EXPORT std::string getMessage(const llvm::Error &e); } // namespace serene::errors #endif diff --git a/libserene/include/serene/errors/base.h b/libserene/include/serene/errors/base.h index 2358152..75151f5 100644 --- a/libserene/include/serene/errors/base.h +++ b/libserene/include/serene/errors/base.h @@ -52,8 +52,8 @@ class SereneError : public llvm::ErrorInfoBase { public: constexpr static const int ID = -1; - void log(llvm::raw_ostream &os) const override { os << msg; }; - std::string message() const override { return msg; }; + virtual void log(llvm::raw_ostream &os) const override { os << msg; }; + virtual std::string message() const override { return msg; }; std::error_code convertToErrorCode() const override { return std::error_code(); diff --git a/libserene/lib/errors.cpp b/libserene/lib/errors.cpp index 70f0c93..0237c6b 100644 --- a/libserene/lib/errors.cpp +++ b/libserene/lib/errors.cpp @@ -17,3 +17,13 @@ */ #include "serene/errors.h" + +namespace serene::errors { + +std::string getMessage(const llvm::Error &e) { + std::string msg; + llvm::raw_string_ostream os(msg); + os << e; + return os.str(); +}; +} // namespace serene::errors diff --git a/libserene/tests/errors/error_tests.cpp.inc b/libserene/tests/errors/error_tests.cpp.inc index 9015893..7f1c07c 100644 --- a/libserene/tests/errors/error_tests.cpp.inc +++ b/libserene/tests/errors/error_tests.cpp.inc @@ -31,21 +31,27 @@ namespace serene { namespace errors { TEST_CASE("Serene Error construction", "[errors]") { - { - std::unique_ptr range(dummyLocation()); - llvm::Error err = llvm::make_error(*range, "test error"); - auto unhandled = - llvm::handleErrors(std::move(err), [&](const PassFailureError &e) { - REQUIRE(e.message() == "test error"); - CHECK(errorVariants[e.ID].title == "PassFailureError"); - CHECK(errorVariants[e.ID].desc == "Pass Failure."); - CHECK(errorVariants[e.ID].help.empty()); - }); + std::unique_ptr range(dummyLocation()); + llvm::Error err = llvm::make_error(*range, "test error"); - CHECK(!unhandled); - }; -} // namespace errors + auto unhandled = + llvm::handleErrors(std::move(err), [&](const PassFailureError &e) { + REQUIRE(e.message() == "test error"); + CHECK(errorVariants[e.ID].title == "PassFailureError"); + CHECK(errorVariants[e.ID].desc == "Pass Failure."); + CHECK(errorVariants[e.ID].help.empty()); + }); + + CHECK(!unhandled); +} + +TEST_CASE("getMessage function", "[errors]") { + std::unique_ptr range(dummyLocation()); + if (auto err = llvm::make_error(*range, "test error")) { + CHECK(getMessage(err) == "test error"); + } +} }; // namespace errors } // namespace serene