diff --git a/error/include/error/error.hpp b/error/include/error/error.hpp index e83a317..d06133c 100644 --- a/error/include/error/error.hpp +++ b/error/include/error/error.hpp @@ -39,4 +39,20 @@ Error format(fmt::format_string fmt, T&&... args) { return Error(fmt::format(fmt, std::forward(args)...)); } +/** + * @brief Checks if two error objects are equal. + * @param lhs The left-hand side error object. + * @param rhs The right-hand side error object. + * @return True if equal, false otherwise. + */ +bool operator==(const Error& lhs, const Error& rhs); + +/** + * @brief Checks if two error objects are not equal. + * @param lhs The left-hand side error object. + * @param rhs The right-hand side error object. + * @return True if not equal, false otherwise. + */ +bool operator!=(const Error& lhs, const Error& rhs); + } // namespace error diff --git a/error/src/error.cpp b/error/src/error.cpp index 4e3d0b0..114036c 100644 --- a/error/src/error.cpp +++ b/error/src/error.cpp @@ -6,4 +6,12 @@ Error::Error(const std::string& msg) : message(msg) {} const char* Error::what() const noexcept { return message.c_str(); } +bool operator==(const Error& lhs, const Error& rhs) { + return lhs.message == rhs.message; +} + +bool operator!=(const Error& lhs, const Error& rhs) { + return lhs.message != rhs.message; +} + } // namespace error diff --git a/error/test/error_test.cpp b/error/test/error_test.cpp index 2504d47..b9dd9db 100644 --- a/error/test/error_test.cpp +++ b/error/test/error_test.cpp @@ -33,3 +33,13 @@ TEST_CASE("Error Throwing and Catching") { } } } + +TEST_CASE("Error Comparison") { + const auto err = error::Error("unknown error"); + const auto err_copy = err; + CHECK(err == err_copy); + CHECK_FALSE(err != err_copy); + const auto other_err = error::Error("other error"); + CHECK_FALSE(err == other_err); + CHECK(err != other_err); +}