// Licensed under the MIT License . // SPDX-License-Identifier: MIT // Copyright (c) 2019 - 2023 Daniil Goncharov . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. #define CATCH_CONFIG_MAIN #include #define MAGIC_ENUM_USING_ALIAS_STRING_VIEW using string_view = std::wstring_view; #define MAGIC_ENUM_USING_ALIAS_STRING using string = std::wstring; #include #include #include #include #include #include enum class Color { RED = -12, GREEN = 7, BLUE = 15 }; template <> constexpr magic_enum::customize::customize_t magic_enum::customize::enum_name(Color value) noexcept { switch (value) { case Color::RED: return L"red"; default: return default_tag; } } using namespace magic_enum; static_assert(is_magic_enum_supported, "magic_enum: Unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); TEST_CASE("enum_cast") { SECTION("string") { constexpr auto cr = enum_cast(L"red"); REQUIRE(cr.value() == Color::RED); REQUIRE(enum_cast(L"GREEN").value() == Color::GREEN); REQUIRE(enum_cast(L"blue", [](wchar_t lhs, wchar_t rhs) { return std::tolower(lhs) == std::tolower(rhs); }).value() == Color::BLUE); REQUIRE_FALSE(enum_cast(L"None").has_value()); } SECTION("integer") { Color cm[3] = {Color::RED, Color::GREEN, Color::BLUE}; constexpr auto cr = enum_cast(-12); REQUIRE(cr.value() == Color::RED); REQUIRE(enum_cast(7).value() == Color::GREEN); REQUIRE(enum_cast(static_cast(cm[2])).value() == Color::BLUE); REQUIRE_FALSE(enum_cast(0).has_value()); } } TEST_CASE("enum_values") { REQUIRE(std::is_same_v()), const std::array&>); constexpr auto& s1 = enum_values(); REQUIRE(s1 == std::array{{Color::RED, Color::GREEN, Color::BLUE}}); } TEST_CASE("enum_count") { constexpr auto s1 = enum_count(); REQUIRE(s1 == 3); } TEST_CASE("enum_name") { SECTION("automatic storage") { constexpr Color cr = Color::RED; constexpr auto cr_name = enum_name(cr); Color cm[3] = {Color::RED, Color::GREEN, Color::BLUE}; Color cb = Color::BLUE; REQUIRE(cr_name == L"red"); REQUIRE(enum_name(cb) == L"BLUE"); REQUIRE(enum_name>(cm[1]) == L"GREEN"); REQUIRE(enum_name>(cm[1]) == L"GREEN"); REQUIRE(enum_name>(static_cast(0)).empty()); } SECTION("static storage") { constexpr Color cr = Color::RED; constexpr auto cr_name = enum_name(); constexpr Color cm[3] = {Color::RED, Color::GREEN, Color::BLUE}; REQUIRE(cr_name == L"red"); REQUIRE(enum_name() == L"BLUE"); REQUIRE(enum_name() == L"GREEN"); } } TEST_CASE("enum_names") { REQUIRE(std::is_same_v()), const std::array&>); constexpr auto& s1 = enum_names(); REQUIRE(s1 == std::array{{L"red", L"GREEN", L"BLUE"}}); } TEST_CASE("enum_entries") { REQUIRE(std::is_same_v()), const std::array, 3>&>); constexpr auto& s1 = enum_entries(); REQUIRE(s1 == std::array, 3>{{{Color::RED, L"red"}, {Color::GREEN, L"GREEN"}, {Color::BLUE, L"BLUE"}}}); } TEST_CASE("ostream_operators") { auto test_ostream = [](auto e, std::wstring name) { using namespace magic_enum::ostream_operators; std::wstringstream ss; ss << e; REQUIRE(ss); REQUIRE(ss.str() == name); }; test_ostream(std::make_optional(Color::RED), L"red"); test_ostream(Color::GREEN, L"GREEN"); test_ostream(Color::BLUE, L"BLUE"); test_ostream(static_cast(0), L"0"); test_ostream(std::make_optional(static_cast(0)), L"0"); } TEST_CASE("istream_operators") { auto test_istream = [](const auto e, std::wstring name) { using namespace magic_enum::istream_operators; std::wistringstream ss(name); std::decay_t v; ss >> v; REQUIRE(ss); REQUIRE(v == e); }; test_istream(Color::GREEN, L"GREEN"); test_istream(Color::BLUE, L"BLUE"); }