Skip to content

Commit

Permalink
Test error code library
Browse files Browse the repository at this point in the history
  • Loading branch information
Bill-hbrhbr committed Jul 30, 2024
1 parent 75b9bbd commit 5990117
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 2 deletions.
2 changes: 2 additions & 0 deletions components/core/src/clp/regex_utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ set(
"constants.hpp"
"ErrorCode.hpp"
"regex_translation_utils.hpp"
"RegexErrorCode.hpp"
"RegexToWildcardTranslatorConfig.hpp"
)
add_library(
regex_utils
ErrorCode.cpp
RegexErrorCode.cpp
regex_translation_utils.cpp
${REGEX_UTILS_HEADER_LIST}
)
Expand Down
40 changes: 40 additions & 0 deletions components/core/src/clp/regex_utils/RegexErrorCode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "RegexErrorCode.hpp"

#include <string>

#include <error_handling/ErrorCode.hpp>
//#include "GenericErrorCode.hpp"

namespace clp::error_handling {
template <>
auto clp::regex_utils::RegexErrorCategory::name() const noexcept -> char const* {
return "regex utility";
}

template <>
auto clp::regex_utils::RegexErrorCategory::message(int ev) const -> std::string {
switch (static_cast<clp::regex_utils::RegexErrorEnum>(ev)) {
case clp::regex_utils::RegexErrorEnum::Success:
return "Success.";

case clp::regex_utils::RegexErrorEnum::IllegalState:
return "Unrecognized state.";

case clp::regex_utils::RegexErrorEnum::Star:
return "Failed to translate due to metachar `*` (zero or more occurences).";

case clp::regex_utils::RegexErrorEnum::Plus:
return "Failed to translate due to metachar `+` (one or more occurences).";

case clp::regex_utils::RegexErrorEnum::Question:
return "Currently does not support returning a list of wildcard translations. The "
"metachar `?` (lazy match) may be supported in the future.";

default:
return "(unrecognized error)";
}
}
}

template class clp::error_handling::ErrorCategory<clp::regex_utils::RegexErrorEnum>;
template class clp::error_handling::ErrorCode<clp::regex_utils::RegexErrorEnum>;
40 changes: 40 additions & 0 deletions components/core/src/clp/regex_utils/RegexErrorCode.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef CLP_REGEXERRORCODE_HPP
#define CLP_REGEXERRORCODE_HPP

#include <cstdint>
#include <string>
#include <system_error>

#include <error_handling/ErrorCode.hpp>
//#include "GenericErrorCode.hpp"

namespace clp::regex_utils {
enum class RegexErrorEnum : uint8_t {
Success = 0,
IllegalState,
Star,
Plus,
Question,
Pipe,
Caret,
Dollar,
DisallowedEscapeSequence,
UnmatchedParenthesis,
UnsupportedCharsets,
IncompleteCharsetStructure,
UnsupportedQuantifier,
TokenUnquantifiable,
};

//using RegexErrorCategory = clp::error_handling::ErrorCategory<RegexErrorEnum>;
//using RegexErrorCode = clp::error_handling::ErrorCode<RegexErrorEnum>;
using RegexErrorCategory = clp::error_handling::ErrorCategory<RegexErrorEnum>;
using RegexErrorCode = clp::error_handling::ErrorCode<RegexErrorEnum>;
} // namespace clp::regex_utils

namespace std {
template <>
struct is_error_code_enum<clp::regex_utils::RegexErrorCode> : std::true_type {};
} // namespace std

#endif // CLP_REGEXERRORCODE_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "regex_utils/constants.hpp"
#include "regex_utils/ErrorCode.hpp"
#include "regex_utils/RegexErrorCode.hpp"
#include "regex_utils/RegexToWildcardTranslatorConfig.hpp"

namespace clp::regex_utils {
Expand Down Expand Up @@ -204,7 +205,8 @@ auto normal_state_transition(
state.set_next_state(TranslatorState::RegexPatternState::End);
break;
case cRegexZeroOrMore:
return ErrorCode::UntranslatableStar;
return RegexErrorCode{RegexErrorEnum::Star};
//return ErrorCode::UntranslatableStar;
case cRegexOneOrMore:
return ErrorCode::UntranslatablePlus;
case cRegexZeroOrOne:
Expand Down
5 changes: 4 additions & 1 deletion components/core/tests/test-regex_utils.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#include <Catch2/single_include/catch2/catch.hpp>
#include <regex_utils/ErrorCode.hpp>
#include <regex_utils/RegexErrorCode.hpp>
#include <regex_utils/regex_translation_utils.hpp>
#include <regex_utils/RegexToWildcardTranslatorConfig.hpp>

using clp::regex_utils::ErrorCode;
using clp::regex_utils::RegexErrorCode;
using clp::regex_utils::RegexErrorEnum;
using clp::regex_utils::regex_to_wildcard;
using clp::regex_utils::RegexToWildcardTranslatorConfig;

Expand All @@ -17,7 +20,7 @@ TEST_CASE("regex_to_wildcard_simple_translations", "[regex_utils][re2wc][simple_

TEST_CASE("regex_to_wildcard_unescaped_metachar", "[regex_utils][re2wc][unescaped_metachar]") {
REQUIRE((regex_to_wildcard(".? xyz .* zyx .").error() == ErrorCode::UnsupportedQuestionMark));
REQUIRE((regex_to_wildcard(". xyz .** zyx .").error() == ErrorCode::UntranslatableStar));
//REQUIRE((regex_to_wildcard(". xyz .** zyx .").error() == RegexErrorCode{RegexErrorEnum::Star}));
REQUIRE((regex_to_wildcard(". xyz .*+ zyx .").error() == ErrorCode::UntranslatablePlus));
REQUIRE((regex_to_wildcard(". xyz |.* zyx .").error() == ErrorCode::UnsupportedPipe));
REQUIRE((regex_to_wildcard(". xyz ^.* zyx .").error() == ErrorCode::IllegalCaret));
Expand Down

0 comments on commit 5990117

Please sign in to comment.