Add a test case for errors::getMessage

This commit is contained in:
Sameer Rahmani 2022-03-08 17:38:02 +00:00
parent 1e8d23b8ef
commit 1cd5824608
4 changed files with 34 additions and 15 deletions

View File

@ -23,6 +23,8 @@
#include "serene/errors/errc.h"
#include "serene/export.h"
#include <llvm/Support/Casting.h>
#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<E>(std::forward<Args>(args)...);
};
SERENE_EXPORT std::string getMessage(const llvm::Error &e);
} // namespace serene::errors
#endif

View File

@ -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();

View File

@ -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

View File

@ -31,21 +31,27 @@ namespace serene {
namespace errors {
TEST_CASE("Serene Error construction", "[errors]") {
{
std::unique_ptr<reader::LocationRange> range(dummyLocation());
llvm::Error err = llvm::make_error<PassFailureError>(*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<reader::LocationRange> range(dummyLocation());
llvm::Error err = llvm::make_error<PassFailureError>(*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<reader::LocationRange> range(dummyLocation());
if (auto err = llvm::make_error<PassFailureError>(*range, "test error")) {
CHECK(getMessage(err) == "test error");
}
}
}; // namespace errors
} // namespace serene