Skip to content

Commit ae1cc39

Browse files
authored
chore: merge pull request #18 from threeal/add-error-comparison
Error: Add `Error` Comparison Operators
2 parents fd02f4c + 9c82ce8 commit ae1cc39

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

error/include/error/error.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,20 @@ Error format(fmt::format_string<T...> fmt, T&&... args) {
3939
return Error(fmt::format(fmt, std::forward<T>(args)...));
4040
}
4141

42+
/**
43+
* @brief Checks if two error objects are equal.
44+
* @param lhs The left-hand side error object.
45+
* @param rhs The right-hand side error object.
46+
* @return True if equal, false otherwise.
47+
*/
48+
bool operator==(const Error& lhs, const Error& rhs);
49+
50+
/**
51+
* @brief Checks if two error objects are not equal.
52+
* @param lhs The left-hand side error object.
53+
* @param rhs The right-hand side error object.
54+
* @return True if not equal, false otherwise.
55+
*/
56+
bool operator!=(const Error& lhs, const Error& rhs);
57+
4258
} // namespace error

error/src/error.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,12 @@ Error::Error(const std::string& msg) : message(msg) {}
66

77
const char* Error::what() const noexcept { return message.c_str(); }
88

9+
bool operator==(const Error& lhs, const Error& rhs) {
10+
return lhs.message == rhs.message;
11+
}
12+
13+
bool operator!=(const Error& lhs, const Error& rhs) {
14+
return lhs.message != rhs.message;
15+
}
16+
917
} // namespace error

error/test/error_test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,13 @@ TEST_CASE("Error Throwing and Catching") {
3333
}
3434
}
3535
}
36+
37+
TEST_CASE("Error Comparison") {
38+
const auto err = error::Error("unknown error");
39+
const auto err_copy = err;
40+
CHECK(err == err_copy);
41+
CHECK_FALSE(err != err_copy);
42+
const auto other_err = error::Error("other error");
43+
CHECK_FALSE(err == other_err);
44+
CHECK(err != other_err);
45+
}

0 commit comments

Comments
 (0)