Skip to content

Error: Add Error::matches Method #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions error/include/error/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ class Error : public std::exception {
* @return Pointer to a null-terminated string with explanatory information.
*/
const char* what() const noexcept override;

/**
* @brief Checks if the error message matches the given string.
* @param str A string to be matched.
* @return True if it matches, false otherwise.
*/
bool matches(const std::string& str) const noexcept;
};

/**
Expand Down
4 changes: 4 additions & 0 deletions error/src/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ namespace error {

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

bool Error::matches(const std::string& str) const noexcept {
return message == str;
}

} // namespace error
15 changes: 10 additions & 5 deletions error/test/error_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
TEST_CASE("Error Construction") {
SECTION("With one argument") {
const error::Error err("unknown error");
REQUIRE(std::string("unknown error") == err.what());
REQUIRE(err.matches("unknown error"));
}

SECTION("With one or more arguments") {
const error::Error err("HTTP error {}", 404);
REQUIRE(std::string("HTTP error 404") == err.what());
REQUIRE(err.matches("HTTP error 404"));
}
}

TEST_CASE("Error Pointer Construction") {
SECTION("With one argument") {
const error::ErrorPtr err = error::make("unknown error");
REQUIRE(std::string("unknown error") == err->what());
REQUIRE(err->matches("unknown error"));
}

SECTION("With one or more arguments") {
const error::ErrorPtr err = error::make("HTTP error {}", 404);
REQUIRE(std::string("HTTP error 404") == err->what());
REQUIRE(err->matches("HTTP error 404"));
}
}

Expand All @@ -31,7 +31,7 @@ TEST_CASE("Error Throwing and Catching") {
try {
throw error::Error("unknown error");
} catch (const error::Error& err) {
REQUIRE(std::string("unknown error") == err.what());
REQUIRE(err.matches("unknown error"));
} catch (...) {
FAIL("Expected to be caught as error::Error");
}
Expand All @@ -47,3 +47,8 @@ TEST_CASE("Error Throwing and Catching") {
}
}
}

TEST_CASE("Error Message Matching") {
const error::Error err("unknown error");
REQUIRE(err.matches("unknown error"));
}