1
0
Fork 0
mirror of https://github.com/Neargye/magic_enum.git synced 2026-01-10 23:44:29 +00:00
This commit is contained in:
terik23 2019-04-06 18:35:09 +05:00
parent 122aa8028d
commit 5a3d4b53e6
5 changed files with 105 additions and 70 deletions

View file

@ -34,7 +34,7 @@ enum class Numbers : char { one = 10, two = 20, three = 30 };
enum Directions { Up = 85, Down = -42, Right = 119, Left = -119 };
enum number : long { one = 10, two = 20, three = 30 };
enum number : unsigned long { one = 10, two = 20, three = 30 };
TEST_CASE("magic_enum::enum_to_string(enum)") {
Color cr = Color::RED;
@ -42,26 +42,30 @@ TEST_CASE("magic_enum::enum_to_string(enum)") {
REQUIRE(magic_enum::enum_to_string(cr).value() == "RED");
REQUIRE(magic_enum::enum_to_string(Color::BLUE).value() == "BLUE");
REQUIRE(magic_enum::enum_to_string(cm[1]).value() == "GREEN");
REQUIRE(!magic_enum::enum_to_string(static_cast<Color>(MAGIC_ENUM_RANGE)).has_value());
REQUIRE_FALSE(magic_enum::enum_to_string(static_cast<Color>(MAGIC_ENUM_RANGE)).has_value());
REQUIRE_FALSE(magic_enum::enum_to_string(static_cast<Color>(-MAGIC_ENUM_RANGE)).has_value());
Numbers no = Numbers::one;
REQUIRE(magic_enum::enum_to_string(no).value() == "one");
REQUIRE(magic_enum::enum_to_string(Numbers::two).value() == "two");
REQUIRE(magic_enum::enum_to_string(Numbers::three).value() == "three");
REQUIRE(!magic_enum::enum_to_string(static_cast<Numbers>(MAGIC_ENUM_RANGE)).has_value());
REQUIRE_FALSE(magic_enum::enum_to_string(static_cast<Numbers>(MAGIC_ENUM_RANGE)).has_value());
REQUIRE_FALSE(magic_enum::enum_to_string(static_cast<Numbers>(-MAGIC_ENUM_RANGE)).has_value());
Directions dr = Directions::Right;
REQUIRE(magic_enum::enum_to_string(Directions::Up).value() == "Up");
REQUIRE(magic_enum::enum_to_string(Directions::Down).value() == "Down");
REQUIRE(magic_enum::enum_to_string(dr).value() == "Right");
REQUIRE(magic_enum::enum_to_string(Directions::Left).value() == "Left");
REQUIRE(!magic_enum::enum_to_string(static_cast<Directions>(MAGIC_ENUM_RANGE)).has_value());
REQUIRE_FALSE(magic_enum::enum_to_string(static_cast<Directions>(MAGIC_ENUM_RANGE)).has_value());
REQUIRE_FALSE(magic_enum::enum_to_string(static_cast<Directions>(-MAGIC_ENUM_RANGE)).has_value());
number nt = number::three;
REQUIRE(magic_enum::enum_to_string(number::one).value() == "one");
REQUIRE(magic_enum::enum_to_string(number::two).value() == "two");
REQUIRE(magic_enum::enum_to_string(nt).value() == "three");
REQUIRE(!magic_enum::enum_to_string(static_cast<number>(MAGIC_ENUM_RANGE)).has_value());
REQUIRE_FALSE(magic_enum::enum_to_string(static_cast<number>(MAGIC_ENUM_RANGE)).has_value());
REQUIRE_FALSE(magic_enum::enum_to_string(static_cast<number>(-MAGIC_ENUM_RANGE)).has_value());
}
TEST_CASE("magic_enum::enum_to_string<enum>()") {
@ -70,62 +74,66 @@ TEST_CASE("magic_enum::enum_to_string<enum>()") {
REQUIRE(magic_enum::enum_to_string<cr>().value() == "RED");
REQUIRE(magic_enum::enum_to_string<Color::BLUE>().value() == "BLUE");
REQUIRE(magic_enum::enum_to_string<cm[1]>().value() == "GREEN");
REQUIRE(!magic_enum::enum_to_string<static_cast<Color>(MAGIC_ENUM_RANGE)>().has_value());
REQUIRE_FALSE(magic_enum::enum_to_string<static_cast<Color>(MAGIC_ENUM_RANGE)>().has_value());
REQUIRE_FALSE(magic_enum::enum_to_string<static_cast<Color>(-MAGIC_ENUM_RANGE)>().has_value());
constexpr Numbers no = Numbers::one;
REQUIRE(magic_enum::enum_to_string<no>().value() == "one");
REQUIRE(magic_enum::enum_to_string<Numbers::two>().value() == "two");
REQUIRE(magic_enum::enum_to_string<Numbers::three>().value() == "three");
REQUIRE(!magic_enum::enum_to_string<static_cast<Numbers>(MAGIC_ENUM_RANGE)>().has_value());
REQUIRE_FALSE(magic_enum::enum_to_string<static_cast<Numbers>(MAGIC_ENUM_RANGE)>().has_value());
REQUIRE_FALSE(magic_enum::enum_to_string<static_cast<Numbers>(-MAGIC_ENUM_RANGE)>().has_value());
constexpr Directions dr = Directions::Right;
REQUIRE(magic_enum::enum_to_string<Directions::Up>().value() == "Up");
REQUIRE(magic_enum::enum_to_string<Directions::Down>().value() == "Down");
REQUIRE(magic_enum::enum_to_string<dr>().value() == "Right");
REQUIRE(magic_enum::enum_to_string<Directions::Left>().value() == "Left");
REQUIRE(!magic_enum::enum_to_string<static_cast<Directions>(MAGIC_ENUM_RANGE)>().has_value());
REQUIRE_FALSE(magic_enum::enum_to_string<static_cast<Directions>(MAGIC_ENUM_RANGE)>().has_value());
REQUIRE_FALSE(magic_enum::enum_to_string<static_cast<Directions>(-MAGIC_ENUM_RANGE)>().has_value());
constexpr number nt = number::three;
REQUIRE(magic_enum::enum_to_string<number::one>().value() == "one");
REQUIRE(magic_enum::enum_to_string<number::two>().value() == "two");
REQUIRE(magic_enum::enum_to_string<nt>().value() == "three");
REQUIRE(!magic_enum::enum_to_string<static_cast<number>(MAGIC_ENUM_RANGE)>().has_value());
REQUIRE_FALSE(magic_enum::enum_to_string<static_cast<number>(MAGIC_ENUM_RANGE)>().has_value());
REQUIRE_FALSE(magic_enum::enum_to_string<static_cast<number>(-MAGIC_ENUM_RANGE)>().has_value());
}
TEST_CASE("magic_enum::enum_from_string(name)") {
REQUIRE(magic_enum::enum_from_string<Color>("RED").value() == Color::RED);
REQUIRE(magic_enum::enum_from_string<Color>("GREEN").value() == Color::GREEN);
REQUIRE(magic_enum::enum_from_string<Color>("BLUE").value() == Color::BLUE);
REQUIRE(!magic_enum::enum_from_string<Color>("None").has_value());
REQUIRE_FALSE(magic_enum::enum_from_string<Color>("None").has_value());
REQUIRE(magic_enum::enum_from_string<Numbers>("one").value() == Numbers::one);
REQUIRE(magic_enum::enum_from_string<Numbers>("two").value() == Numbers::two);
REQUIRE(magic_enum::enum_from_string<Numbers>("three").value() == Numbers::three);
REQUIRE(!magic_enum::enum_from_string<Numbers>("None").has_value());
REQUIRE_FALSE(magic_enum::enum_from_string<Numbers>("None").has_value());
REQUIRE(magic_enum::enum_from_string<Directions>("Up").value() == Directions::Up);
REQUIRE(magic_enum::enum_from_string<Directions>("Down").value() == Directions::Down);
REQUIRE(magic_enum::enum_from_string<Directions>("Right").value() == Directions::Right);
REQUIRE(magic_enum::enum_from_string<Directions>("Left").value() == Directions::Left);
REQUIRE(!magic_enum::enum_from_string<Directions>("None").has_value());
REQUIRE_FALSE(magic_enum::enum_from_string<Directions>("None").has_value());
REQUIRE(magic_enum::enum_from_string<number>("one").value() == number::one);
REQUIRE(magic_enum::enum_from_string<number>("two").value() == number::two);
REQUIRE(magic_enum::enum_from_string<number>("three").value() == number::three);
REQUIRE(!magic_enum::enum_from_string<number>("None").has_value());
REQUIRE_FALSE(magic_enum::enum_from_string<number>("None").has_value());
}
TEST_CASE("magic_enum::enum_sequence<enum>()") {
constexpr auto mge_s1 = magic_enum::enum_sequence<Color>();
constexpr auto mge_s1 = magic_enum::enum_to_sequence<Color>();
REQUIRE(mge_s1 == std::array<Color, 3>{Color::RED, Color::GREEN, Color::BLUE});
constexpr auto mge_s2 = magic_enum::enum_sequence<Numbers>();
constexpr auto mge_s2 = magic_enum::enum_to_sequence<Numbers>();
REQUIRE(mge_s2 == std::array<Numbers, 3>{Numbers::one, Numbers::two, Numbers::three});
constexpr auto mge_s3 = magic_enum::enum_sequence<Directions>();
constexpr auto mge_s3 = magic_enum::enum_to_sequence<Directions>();
REQUIRE(mge_s3 == std::array<Directions, 4>{Directions::Left, Directions::Down, Directions::Up, Directions::Right});
constexpr auto mge_s4 = magic_enum::enum_sequence<number>();
constexpr auto mge_s4 = magic_enum::enum_to_sequence<number>();
REQUIRE(mge_s4 == std::array<number, 3>{number::one, number::two, number::three});
}
@ -134,7 +142,7 @@ TEST_CASE("magic_enum::enum_to_string_sequence<enum>()") {
REQUIRE(mge_s1 == std::array<std::string_view, 3>{"RED", "GREEN", "BLUE"});
constexpr auto mge_s2 = magic_enum::enum_to_string_sequence<Numbers>();
REQUIRE(mge_s2 == std::array<std::string_view, 3>{"one", "two","three"});
REQUIRE(mge_s2 == std::array<std::string_view, 3>{"one", "two", "three"});
constexpr auto mge_s3 = magic_enum::enum_to_string_sequence<Directions>();
REQUIRE(mge_s3 == std::array<std::string_view, 4>{"Left", "Down", "Up", "Right"});