test_wchar_t.cpp
Go to the documentation of this file.
1 // Licensed under the MIT License <http://opensource.org/licenses/MIT>.
2 // SPDX-License-Identifier: MIT
3 // Copyright (c) 2019 - 2024 Daniil Goncharov <neargye@gmail.com>.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #define CATCH_CONFIG_MAIN
24 #include <catch2/catch.hpp>
25 
26 #define MAGIC_ENUM_USING_ALIAS_STRING_VIEW using string_view = std::wstring_view;
27 #define MAGIC_ENUM_USING_ALIAS_STRING using string = std::wstring;
30 
31 #include <array>
32 #include <cctype>
33 #include <string_view>
34 #include <sstream>
35 
36 enum class Color { RED = -12, GREEN = 7, BLUE = 15 };
37 template <>
38 constexpr magic_enum::customize::customize_t magic_enum::customize::enum_name<Color>(Color value) noexcept {
39  switch (value) {
40  case Color::RED:
41  return L"red";
42  default:
43  return default_tag;
44  }
45 }
46 
47 using namespace magic_enum;
48 
49 static_assert(is_magic_enum_supported, "magic_enum: Unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility).");
50 
51 TEST_CASE("enum_cast") {
52  SECTION("string") {
53  constexpr auto cr = enum_cast<Color>(L"red");
54  REQUIRE(cr.value() == Color::RED);
55  REQUIRE(enum_cast<Color&>(L"GREEN").value() == Color::GREEN);
56  REQUIRE(enum_cast<Color>(L"blue", [](wchar_t lhs, wchar_t rhs) { return std::tolower(lhs) == std::tolower(rhs); }).value() == Color::BLUE);
57  REQUIRE_FALSE(enum_cast<Color>(L"None").has_value());
58  }
59 
60  SECTION("integer") {
62  constexpr auto cr = enum_cast<Color>(-12);
63  REQUIRE(cr.value() == Color::RED);
64  REQUIRE(enum_cast<Color&>(7).value() == Color::GREEN);
65  REQUIRE(enum_cast<Color>(static_cast<int>(cm[2])).value() == Color::BLUE);
66  REQUIRE_FALSE(enum_cast<Color>(0).has_value());
67  }
68 }
69 
70 TEST_CASE("enum_values") {
71  REQUIRE(std::is_same_v<decltype(enum_values<Color>()), const std::array<Color, 3>&>);
72 
73  constexpr auto& s1 = enum_values<Color&>();
74  REQUIRE(s1 == std::array<Color, 3>{{Color::RED, Color::GREEN, Color::BLUE}});
75 }
76 
77 TEST_CASE("enum_count") {
78  constexpr auto s1 = enum_count<Color&>();
79  REQUIRE(s1 == 3);
80 }
81 
82 TEST_CASE("enum_name") {
83  SECTION("automatic storage") {
84  constexpr Color cr = Color::RED;
85  constexpr auto cr_name = enum_name(cr);
87  Color cb = Color::BLUE;
88  REQUIRE(cr_name == L"red");
89  REQUIRE(enum_name<Color&>(cb) == L"BLUE");
90  REQUIRE(enum_name<as_flags<false>>(cm[1]) == L"GREEN");
91  REQUIRE(enum_name<as_common<true>>(cm[1]) == L"GREEN");
92  REQUIRE(enum_name<as_flags<false>>(static_cast<Color>(0)).empty());
93  }
94 
95  SECTION("static storage") {
96  constexpr Color cr = Color::RED;
97  constexpr auto cr_name = enum_name<cr>();
98  constexpr Color cm[3] = {Color::RED, Color::GREEN, Color::BLUE};
99  REQUIRE(cr_name == L"red");
100  REQUIRE(enum_name<Color::BLUE>() == L"BLUE");
101  REQUIRE(enum_name<cm[1]>() == L"GREEN");
102  }
103 }
104 
105 TEST_CASE("enum_names") {
106  REQUIRE(std::is_same_v<decltype(enum_names<Color>()), const std::array<std::wstring_view, 3>&>);
107 
108  constexpr auto& s1 = enum_names<Color&>();
109  REQUIRE(s1 == std::array<std::wstring_view, 3>{{L"red", L"GREEN", L"BLUE"}});
110 }
111 
112 TEST_CASE("enum_entries") {
113  REQUIRE(std::is_same_v<decltype(enum_entries<Color>()), const std::array<std::pair<Color, std::wstring_view>, 3>&>);
114 
115  constexpr auto& s1 = enum_entries<Color&>();
116  REQUIRE(s1 == std::array<std::pair<Color, std::wstring_view>, 3>{{{Color::RED, L"red"}, {Color::GREEN, L"GREEN"}, {Color::BLUE, L"BLUE"}}});
117 }
118 
119 TEST_CASE("ostream_operators") {
120  auto test_ostream = [](auto e, std::wstring name) {
121  using namespace magic_enum::ostream_operators;
122  std::wstringstream ss;
123  ss << e;
124  REQUIRE(ss);
125  REQUIRE(ss.str() == name);
126  };
127 
128  test_ostream(std::make_optional(Color::RED), L"red");
129  test_ostream(Color::GREEN, L"GREEN");
130  test_ostream(Color::BLUE, L"BLUE");
131  test_ostream(static_cast<Color>(0), L"0");
132  test_ostream(std::make_optional(static_cast<Color>(0)), L"0");
133 }
134 
135 TEST_CASE("istream_operators") {
136  auto test_istream = [](const auto e, std::wstring name) {
137  using namespace magic_enum::istream_operators;
138  std::wistringstream ss(name);
139  std::decay_t<decltype(e)> v;
140  ss >> v;
141  REQUIRE(ss);
142  REQUIRE(v == e);
143  };
144 
145  test_istream(Color::GREEN, L"GREEN");
146  test_istream(Color::BLUE, L"BLUE");
147 }
Color::GREEN
@ GREEN
s1
@ s1
Definition: test.cpp:452
magic_enum.hpp
magic_enum_iostream.hpp
magic_enum::detail::value
constexpr E value(std::size_t i) noexcept
Definition: magic_enum.hpp:679
magic_enum::ostream_operators
Definition: magic_enum_iostream.hpp:44
REQUIRE
#define REQUIRE(...)
Definition: catch.hpp:17637
SECTION
#define SECTION(...)
Definition: catch.hpp:17677
magic_enum::is_magic_enum_supported
constexpr bool is_magic_enum_supported
Definition: magic_enum.hpp:1141
magic_enum::istream_operators
Definition: magic_enum_iostream.hpp:78
Color::RED
@ RED
catch.hpp
magic_enum::customize::customize_t
Definition: magic_enum.hpp:189
magic_enum::enum_name
constexpr auto enum_name() noexcept -> detail::enable_if_t< decltype(V), string_view >
Definition: magic_enum.hpp:1290
REQUIRE_FALSE
#define REQUIRE_FALSE(...)
Definition: catch.hpp:17638
Color
Color
Definition: example.cpp:28
magic_enum::customize::default_tag
constexpr auto default_tag
Definition: magic_enum.hpp:199
Color::BLUE
@ BLUE
magic_enum
Definition: magic_enum.hpp:126
TEST_CASE
TEST_CASE("enum_cast")
Definition: test_wchar_t.cpp:51


magic_enum
Author(s):
autogenerated on Fri Feb 21 2025 03:20:19