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

Make enum_fuse typesafe (fixes #143) (#145)

This commit is contained in:
Pavel I. Kryukov 2022-03-08 11:38:19 +03:00 committed by GitHub
parent 785b3f253d
commit 9d1cf196cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 7 deletions

View file

@ -1026,6 +1026,11 @@ TEST_CASE("constexpr_for") {
});
}
#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable : 4064)
#endif
static int switch_case_2d(Color color, Directions direction) {
switch (magic_enum::enum_fuse(color, direction).value()) {
case magic_enum::enum_fuse(Color::RED, Directions::Up).value():
@ -1043,21 +1048,24 @@ static int switch_case_3d(Color color, Directions direction, Index index) {
switch (magic_enum::enum_fuse(color, direction, index).value()) {
case magic_enum::enum_fuse(Color::RED, Directions::Up, Index::zero).value():
return 1;
// model accidental removal of last index, must not match anything
case magic_enum::enum_fuse(Color::BLUE, Directions::Up).value():
case magic_enum::enum_fuse(Color::BLUE, Directions::Up, Index::zero).value():
return 2;
default:
return 0;
}
}
#ifdef _MSC_VER
# pragma warning(pop)
#endif
TEST_CASE("multdimensional-switch-case") {
REQUIRE(switch_case_2d(Color::RED, Directions::Up) == 1);
REQUIRE(switch_case_2d(Color::RED, Directions::Down) == 0);
REQUIRE(switch_case_2d(Color::BLUE, Directions::Up) == 0);
REQUIRE(switch_case_2d(Color::BLUE, Directions::Down) == 2);
REQUIRE(switch_case_3d(Color::RED, Directions::Up, Index::zero) == 1);
REQUIRE(switch_case_3d(Color::BLUE, Directions::Up, Index::zero) == 0);
REQUIRE(switch_case_3d(Color::BLUE, Directions::Up, Index::zero) == 2);
REQUIRE(switch_case_3d(Color::BLUE, Directions::Up, Index::one) == 0);
REQUIRE(switch_case_3d(Color::BLUE, Directions::Up, Index::two) == 0);
}