From 419752dccc8d4fb2af6356646e03b64f5a5f82da Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Fri, 23 Jun 2023 10:45:19 +0700 Subject: [PATCH 1/2] feat(error): add `Error::match(std::string)` method --- error/include/error/error.hpp | 7 +++++++ error/src/error.cpp | 4 ++++ error/test/error_test.cpp | 15 ++++++++++----- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/error/include/error/error.hpp b/error/include/error/error.hpp index efd0f5b..a480da7 100644 --- a/error/include/error/error.hpp +++ b/error/include/error/error.hpp @@ -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 The string to be matched. + * @return True if it matches, false otherwise. + */ + bool match(const std::string& str) const noexcept; }; /** diff --git a/error/src/error.cpp b/error/src/error.cpp index 6d6310e..dc7c0e3 100644 --- a/error/src/error.cpp +++ b/error/src/error.cpp @@ -4,4 +4,8 @@ namespace error { const char* Error::what() const noexcept { return message.c_str(); } +bool Error::match(const std::string& str) const noexcept { + return message == str; +} + } // namespace error diff --git a/error/test/error_test.cpp b/error/test/error_test.cpp index 56943d0..8204348 100644 --- a/error/test/error_test.cpp +++ b/error/test/error_test.cpp @@ -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.match("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.match("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->match("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->match("HTTP error 404")); } } @@ -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.match("unknown error")); } catch (...) { FAIL("Expected to be caught as error::Error"); } @@ -47,3 +47,8 @@ TEST_CASE("Error Throwing and Catching") { } } } + +TEST_CASE("Error Message Matching") { + const error::Error err("unknown error"); + REQUIRE(err.match("unknown error")); +} From 27126756003db55dde8908b3e5c08bfb8df0cc2e Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Fri, 23 Jun 2023 11:10:21 +0700 Subject: [PATCH 2/2] feat(error): rename `Error::match` to `Error::matches` --- error/include/error/error.hpp | 4 ++-- error/src/error.cpp | 2 +- error/test/error_test.cpp | 12 ++++++------ 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/error/include/error/error.hpp b/error/include/error/error.hpp index a480da7..c5b97b0 100644 --- a/error/include/error/error.hpp +++ b/error/include/error/error.hpp @@ -35,10 +35,10 @@ class Error : public std::exception { /** * @brief Checks if the error message matches the given string. - * @param str The string to be matched. + * @param str A string to be matched. * @return True if it matches, false otherwise. */ - bool match(const std::string& str) const noexcept; + bool matches(const std::string& str) const noexcept; }; /** diff --git a/error/src/error.cpp b/error/src/error.cpp index dc7c0e3..310a91a 100644 --- a/error/src/error.cpp +++ b/error/src/error.cpp @@ -4,7 +4,7 @@ namespace error { const char* Error::what() const noexcept { return message.c_str(); } -bool Error::match(const std::string& str) const noexcept { +bool Error::matches(const std::string& str) const noexcept { return message == str; } diff --git a/error/test/error_test.cpp b/error/test/error_test.cpp index 8204348..e0067df 100644 --- a/error/test/error_test.cpp +++ b/error/test/error_test.cpp @@ -5,24 +5,24 @@ TEST_CASE("Error Construction") { SECTION("With one argument") { const error::Error err("unknown error"); - REQUIRE(err.match("unknown error")); + REQUIRE(err.matches("unknown error")); } SECTION("With one or more arguments") { const error::Error err("HTTP error {}", 404); - REQUIRE(err.match("HTTP error 404")); + REQUIRE(err.matches("HTTP error 404")); } } TEST_CASE("Error Pointer Construction") { SECTION("With one argument") { const error::ErrorPtr err = error::make("unknown error"); - REQUIRE(err->match("unknown error")); + REQUIRE(err->matches("unknown error")); } SECTION("With one or more arguments") { const error::ErrorPtr err = error::make("HTTP error {}", 404); - REQUIRE(err->match("HTTP error 404")); + REQUIRE(err->matches("HTTP error 404")); } } @@ -31,7 +31,7 @@ TEST_CASE("Error Throwing and Catching") { try { throw error::Error("unknown error"); } catch (const error::Error& err) { - REQUIRE(err.match("unknown error")); + REQUIRE(err.matches("unknown error")); } catch (...) { FAIL("Expected to be caught as error::Error"); } @@ -50,5 +50,5 @@ TEST_CASE("Error Throwing and Catching") { TEST_CASE("Error Message Matching") { const error::Error err("unknown error"); - REQUIRE(err.match("unknown error")); + REQUIRE(err.matches("unknown error")); }