1
0
Fork 0
mirror of https://github.com/Neargye/magic_enum.git synced 2026-01-10 23:44:29 +00:00

dev v0.3.0

This commit is contained in:
terik23 2019-04-07 04:02:53 +05:00
parent 5a3d4b53e6
commit 74eac12c11
5 changed files with 399 additions and 219 deletions

View file

@ -27,6 +27,8 @@
#include <magic_enum.hpp>
#include <array>
#include <string>
#include <sstream>
enum class Color { RED = -12, GREEN = 7, BLUE = 15 };
@ -36,117 +38,195 @@ enum Directions { Up = 85, Down = -42, Right = 119, Left = -119 };
enum number : unsigned long { one = 10, two = 20, three = 30 };
TEST_CASE("magic_enum::enum_to_string(enum)") {
Color cr = Color::RED;
Color cm[3] = {Color::RED, Color::GREEN, Color::BLUE};
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_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());
TEST_CASE("enum_cast") {
SECTION("string") {
#if defined(_MSC_VER) && _MSC_VER < 1920
# define constexpr // Visual Studio 2017 have bug with string_view constexpr compare.
#endif
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_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 auto cr = magic_enum::enum_cast<Color>("RED");
REQUIRE(cr.value() == Color::RED);
REQUIRE(magic_enum::enum_cast<Color>("GREEN").value() == Color::GREEN);
REQUIRE(magic_enum::enum_cast<Color>("BLUE").value() == Color::BLUE);
REQUIRE_FALSE(magic_enum::enum_cast<Color>("None").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_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 auto no = magic_enum::enum_cast<Numbers>("one");
REQUIRE(no.value() == Numbers::one);
REQUIRE(magic_enum::enum_cast<Numbers>("two").value() == Numbers::two);
REQUIRE(magic_enum::enum_cast<Numbers>("three").value() == Numbers::three);
REQUIRE_FALSE(magic_enum::enum_cast<Numbers>("None").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_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());
constexpr auto dr = magic_enum::enum_cast<Directions>("Right");
REQUIRE(magic_enum::enum_cast<Directions>("Up").value() == Directions::Up);
REQUIRE(magic_enum::enum_cast<Directions>("Down").value() == Directions::Down);
REQUIRE(dr.value() == Directions::Right);
REQUIRE(magic_enum::enum_cast<Directions>("Left").value() == Directions::Left);
REQUIRE_FALSE(magic_enum::enum_cast<Directions>("None").has_value());
constexpr auto nt = magic_enum::enum_cast<number>("three");
REQUIRE(magic_enum::enum_cast<number>("one").value() == number::one);
REQUIRE(magic_enum::enum_cast<number>("two").value() == number::two);
REQUIRE(nt.value() == number::three);
REQUIRE_FALSE(magic_enum::enum_cast<number>("None").has_value());
#undef constexpr
}
SECTION("string") {
constexpr auto cr = magic_enum::enum_cast<Color>(-12);
REQUIRE(cr.value() == Color::RED);
REQUIRE(magic_enum::enum_cast<Color>(7).value() == Color::GREEN);
REQUIRE(magic_enum::enum_cast<Color>(15).value() == Color::BLUE);
REQUIRE_FALSE(magic_enum::enum_cast<Color>(0).has_value());
constexpr auto no = magic_enum::enum_cast<Numbers>(10);
REQUIRE(no.value() == Numbers::one);
REQUIRE(magic_enum::enum_cast<Numbers>(20).value() == Numbers::two);
REQUIRE(magic_enum::enum_cast<Numbers>(30).value() == Numbers::three);
REQUIRE_FALSE(magic_enum::enum_cast<Numbers>(0).has_value());
constexpr auto dr = magic_enum::enum_cast<Directions>(119);
REQUIRE(magic_enum::enum_cast<Directions>(85).value() == Directions::Up);
REQUIRE(magic_enum::enum_cast<Directions>(-42).value() == Directions::Down);
REQUIRE(dr.value() == Directions::Right);
REQUIRE(magic_enum::enum_cast<Directions>(-119).value() == Directions::Left);
REQUIRE_FALSE(magic_enum::enum_cast<Directions>(0).has_value());
constexpr auto nt = magic_enum::enum_cast<number>(30);
REQUIRE(magic_enum::enum_cast<number>(10).value() == number::one);
REQUIRE(magic_enum::enum_cast<number>(20).value() == number::two);
REQUIRE(nt.value() == number::three);
REQUIRE_FALSE(magic_enum::enum_cast<number>(0).has_value());
}
}
TEST_CASE("magic_enum::enum_to_string<enum>()") {
constexpr Color cr = Color::RED;
constexpr Color cm[3] = {Color::RED, Color::GREEN, Color::BLUE};
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_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());
TEST_CASE("enum_value") {
constexpr auto cr = magic_enum::enum_value<Color>(0);
REQUIRE(cr == Color::RED);
REQUIRE(magic_enum::enum_value<Color>(1) == Color::GREEN);
REQUIRE(magic_enum::enum_value<Color>(2) == Color::BLUE);
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_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 auto no = magic_enum::enum_value<Numbers>(0);
REQUIRE(no == Numbers::one);
REQUIRE(magic_enum::enum_value<Numbers>(1) == Numbers::two);
REQUIRE(magic_enum::enum_value<Numbers>(2) == Numbers::three);
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_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 auto dr = magic_enum::enum_value<Directions>(3);
REQUIRE(magic_enum::enum_value<Directions>(0) == Directions::Left);
REQUIRE(magic_enum::enum_value<Directions>(1) == Directions::Down);
REQUIRE(magic_enum::enum_value<Directions>(2) == Directions::Up);
REQUIRE(dr == Directions::Right);
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_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());
constexpr auto nt = magic_enum::enum_value<number>(2);
REQUIRE(magic_enum::enum_value<number>(0) == number::one);
REQUIRE(magic_enum::enum_value<number>(1) == number::two);
REQUIRE(nt == number::three);
}
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_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_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_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_FALSE(magic_enum::enum_from_string<number>("None").has_value());
}
TEST_CASE("magic_enum::enum_sequence<enum>()") {
constexpr auto mge_s1 = magic_enum::enum_to_sequence<Color>();
TEST_CASE("enum_values") {
constexpr auto mge_s1 = magic_enum::enum_values<Color>();
REQUIRE(mge_s1 == std::array<Color, 3>{Color::RED, Color::GREEN, Color::BLUE});
constexpr auto mge_s2 = magic_enum::enum_to_sequence<Numbers>();
constexpr auto mge_s2 = magic_enum::enum_values<Numbers>();
REQUIRE(mge_s2 == std::array<Numbers, 3>{Numbers::one, Numbers::two, Numbers::three});
constexpr auto mge_s3 = magic_enum::enum_to_sequence<Directions>();
constexpr auto mge_s3 = magic_enum::enum_values<Directions>();
REQUIRE(mge_s3 == std::array<Directions, 4>{Directions::Left, Directions::Down, Directions::Up, Directions::Right});
constexpr auto mge_s4 = magic_enum::enum_to_sequence<number>();
constexpr auto mge_s4 = magic_enum::enum_values<number>();
REQUIRE(mge_s4 == std::array<number, 3>{number::one, number::two, number::three});
}
TEST_CASE("magic_enum::enum_to_string_sequence<enum>()") {
constexpr auto mge_s1 = magic_enum::enum_to_string_sequence<Color>();
TEST_CASE("enum_count") {
constexpr auto mge_s1 = magic_enum::enum_count<Color>();
REQUIRE(mge_s1 == 3);
constexpr auto mge_s2 = magic_enum::enum_count<Numbers>();
REQUIRE(mge_s2 == 3);
constexpr auto mge_s3 = magic_enum::enum_count<Directions>();
REQUIRE(mge_s3 == 4);
constexpr auto mge_s4 = magic_enum::enum_count<number>();
REQUIRE(mge_s4 == 3);
}
TEST_CASE("enum_name") {
constexpr Color cr = Color::RED;
constexpr auto cr_name = magic_enum::enum_name(cr);
Color cm[3] = {Color::RED, Color::GREEN, Color::BLUE};
REQUIRE(cr_name.value() == "RED");
REQUIRE(magic_enum::enum_name(Color::BLUE).value() == "BLUE");
REQUIRE(magic_enum::enum_name(cm[1]).value() == "GREEN");
REQUIRE_FALSE(magic_enum::enum_name(static_cast<Color>(MAGIC_ENUM_RANGE)).has_value());
REQUIRE_FALSE(magic_enum::enum_name(static_cast<Color>(-MAGIC_ENUM_RANGE)).has_value());
constexpr Numbers no = Numbers::one;
constexpr auto no_name = magic_enum::enum_name(no);
REQUIRE(no_name.value() == "one");
REQUIRE(magic_enum::enum_name(Numbers::two).value() == "two");
REQUIRE(magic_enum::enum_name(Numbers::three).value() == "three");
REQUIRE_FALSE(magic_enum::enum_name(static_cast<Numbers>(MAGIC_ENUM_RANGE)).has_value());
REQUIRE_FALSE(magic_enum::enum_name(static_cast<Numbers>(-MAGIC_ENUM_RANGE)).has_value());
constexpr Directions dr = Directions::Right;
constexpr auto dr_name = magic_enum::enum_name(dr);
REQUIRE(magic_enum::enum_name(Directions::Up).value() == "Up");
REQUIRE(magic_enum::enum_name(Directions::Down).value() == "Down");
REQUIRE(dr_name.value() == "Right");
REQUIRE(magic_enum::enum_name(Directions::Left).value() == "Left");
REQUIRE_FALSE(magic_enum::enum_name(static_cast<Directions>(MAGIC_ENUM_RANGE)).has_value());
REQUIRE_FALSE(magic_enum::enum_name(static_cast<Directions>(-MAGIC_ENUM_RANGE)).has_value());
constexpr number nt = number::three;
constexpr auto nt_name = magic_enum::enum_name(nt);
REQUIRE(magic_enum::enum_name(number::one).value() == "one");
REQUIRE(magic_enum::enum_name(number::two).value() == "two");
REQUIRE(nt_name.value() == "three");
REQUIRE_FALSE(magic_enum::enum_name(static_cast<number>(MAGIC_ENUM_RANGE)).has_value());
REQUIRE_FALSE(magic_enum::enum_name(static_cast<number>(-MAGIC_ENUM_RANGE)).has_value());
}
TEST_CASE("enum_names") {
constexpr auto mge_s1 = magic_enum::enum_names<Color>();
REQUIRE(mge_s1 == std::array<std::string_view, 3>{"RED", "GREEN", "BLUE"});
constexpr auto mge_s2 = magic_enum::enum_to_string_sequence<Numbers>();
constexpr auto mge_s2 = magic_enum::enum_names<Numbers>();
REQUIRE(mge_s2 == std::array<std::string_view, 3>{"one", "two", "three"});
constexpr auto mge_s3 = magic_enum::enum_to_string_sequence<Directions>();
constexpr auto mge_s3 = magic_enum::enum_names<Directions>();
REQUIRE(mge_s3 == std::array<std::string_view, 4>{"Left", "Down", "Up", "Right"});
constexpr auto mge_s4 = magic_enum::enum_to_string_sequence<number>();
constexpr auto mge_s4 = magic_enum::enum_names<number>();
REQUIRE(mge_s4 == std::array<std::string_view, 3>{"one", "two", "three"});
}
TEST_CASE("operator<<") {
auto test_ostream = [](auto e, std::string_view name) {
using namespace magic_enum::ops;
std::stringstream ss;
ss << e;
REQUIRE(ss.str() == name);
};
test_ostream(Color::RED, "RED");
test_ostream(Color::GREEN, "GREEN");
test_ostream(Color::BLUE, "BLUE");
test_ostream((Color)0, "");
test_ostream(Numbers::one, "one");
test_ostream(Numbers::two, "two");
test_ostream(Numbers::three, "three");
test_ostream((Numbers)0, "");
test_ostream(Directions::Up, "Up");
test_ostream(Directions::Down, "Down");
test_ostream(Directions::Right, "Right");
test_ostream(Directions::Left, "Left");
test_ostream((Directions)0, "");
test_ostream(number::one, "one");
test_ostream(number::two, "two");
test_ostream(number::three, "three");
test_ostream((number)0, "");
}