diff --git a/include/rfl/enums.hpp b/include/rfl/enums.hpp index f2cab910..1829a75c 100644 --- a/include/rfl/enums.hpp +++ b/include/rfl/enums.hpp @@ -2,6 +2,7 @@ #define RFL_ENUMS_HPP_ #include +#include #include "Result.hpp" #include "internal/enums/get_enum_names.hpp" @@ -11,6 +12,46 @@ namespace rfl { +// Returns a named tuple mapping names of enumerators of the given enum type to +// their values. +template +auto get_enumerators() { + return internal::enums::names_to_enumerator_named_tuple( + internal::enums::get_enum_names()); +} + +// Returns a named tuple mapping names of enumerators of the given enum type to +// their underlying values. +template +auto get_underlying_enumerators() { + return internal::enums::names_to_underlying_enumerator_named_tuple( + internal::enums::get_enum_names()); +} + +// Returns an std::array containing pairs of enumerator names (as +// std::string_view) and values. +template +constexpr auto get_enumerator_array() { + return internal::enums::names_to_enumerator_array( + internal::enums::get_enum_names()); +} + +// Returns an std::array containing pairs of enumerator names (as +// std::string_view) and underlying values. +template +constexpr auto get_underlying_enumerator_array() { + return internal::enums::names_to_underlying_enumerator_array( + internal::enums::get_enum_names()); +} + +// Returns the range of the given enum type as a pair of the minimum and maximum +template +constexpr auto get_enum_range() { + return std::make_pair(enchantum::enum_traits::min, + enchantum::enum_traits::max); +} + +// Converts an enum value to tis string representation. template std::string enum_to_string(const EnumType _enum) { const auto to_string_or_number = [](const EnumType e) { @@ -52,7 +93,17 @@ Result string_to_enum(const std::string& _str) { try { return static_cast(std::stoi(name)); } catch (std::exception& exp) { - return error(exp.what()); + std::string msg = "Invalid enum value: '"; + msg += name; + msg += "'. Must be one of ["; + const char* sep = ""; + for (const auto& p : get_enumerator_array()) { + msg += sep; + msg += p.first; + sep = ", "; + } + msg += "]."; + return error(msg); } }; @@ -74,45 +125,6 @@ Result string_to_enum(const std::string& _str) { } } -// Returns a named tuple mapping names of enumerators of the given enum type to -// their values. -template -auto get_enumerators() { - return internal::enums::names_to_enumerator_named_tuple( - internal::enums::get_enum_names()); -} - -// Returns a named tuple mapping names of enumerators of the given enum type to -// their underlying values. -template -auto get_underlying_enumerators() { - return internal::enums::names_to_underlying_enumerator_named_tuple( - internal::enums::get_enum_names()); -} - -// Returns an std::array containing pairs of enumerator names (as -// std::string_view) and values. -template -constexpr auto get_enumerator_array() { - return internal::enums::names_to_enumerator_array( - internal::enums::get_enum_names()); -} - -// Returns an std::array containing pairs of enumerator names (as -// std::string_view) and underlying values. -template -constexpr auto get_underlying_enumerator_array() { - return internal::enums::names_to_underlying_enumerator_array( - internal::enums::get_enum_names()); -} - -// Returns the range of the given enum type as a pair of the minimum and maximum -template -constexpr auto get_enum_range() { - return std::make_pair(enchantum::enum_traits::min, - enchantum::enum_traits::max); -} - } // namespace rfl #endif // RFL_ENUMS_HPP_ diff --git a/tests/json/test_enum_error_messages.cpp b/tests/json/test_enum_error_messages.cpp new file mode 100644 index 00000000..7514bbe3 --- /dev/null +++ b/tests/json/test_enum_error_messages.cpp @@ -0,0 +1,45 @@ +#include +#include +#include + +#include + +namespace test_enum_error_messages { + +enum class Color { red, green, blue, yellow }; + +TEST(json, test_enum_error_message_invalid_enum) { + + const auto result = rfl::string_to_enum("bart"); + + const std::string expected = R"(Invalid enum value: 'bart'. Must be one of [red, green, blue, yellow].)"; + + EXPECT_TRUE(!result.has_value()); + + EXPECT_EQ(result.error().what(), expected); +} + +TEST(json, test_enum_error_message_empty_enum) { + + const auto result = rfl::string_to_enum(""); + + const std::string expected = R"(Invalid enum value: ''. Must be one of [red, green, blue, yellow].)"; + + EXPECT_TRUE(!result.has_value()); + + EXPECT_EQ(result.error().what(), expected); +} + +TEST(json, test_enum_error_message_case_sensitive) { + + const auto result = rfl::string_to_enum("RED"); + + const std::string expected = R"(Invalid enum value: 'RED'. Must be one of [red, green, blue, yellow].)"; + + EXPECT_TRUE(!result.has_value()); + + EXPECT_EQ(result.error().what(), expected); +} + + +} // namespace test_enum_error_messages