diff --git a/.travis.yml b/.travis.yml index 2c000ad..72127a2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,6 +9,14 @@ matrix: dist: trusty addons: { apt: { sources: ubuntu-toolchain-r-test, packages: ["xorg-dev", "g++-8"] } } env: MATRIX_EVAL="CC=gcc-8 && CXX=g++-8" + - os: linux + dist: trusty + addons: { apt: { sources: ["ubuntu-toolchain-r-test", "llvm-toolchain-trusty-5.0"], packages: ["xorg-dev", "clang-5.0", "g++-7"] } } + env: MATRIX_EVAL="CC=clang-5.0 && CXX=clang++-5.0" + - os: linux + dist: trusty + addons: { apt: { sources: ["ubuntu-toolchain-r-test", "llvm-toolchain-trusty-6.0"], packages: ["xorg-dev", "clang-6.0", "g++-7"] } } + env: MATRIX_EVAL="CC=clang-6.0 && CXX=clang++-6.0" - os: linux dist: trusty addons: { apt: { sources: ["ubuntu-toolchain-r-test", "llvm-toolchain-trusty-7"], packages: ["xorg-dev", "clang-7", "g++-7"] } } diff --git a/README.md b/README.md index 2d6d68e..6dee0d1 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ ## Requirements - [gcc](https://www.gnu.org/software/gcc/) **>= 7** -- [clang](https://clang.llvm.org/) **>= 7.0** +- [clang](https://clang.llvm.org/) **>= 5.0** - [msvc](https://visualstudio.microsoft.com/) **>= 2017** ## Installation @@ -63,38 +63,7 @@ enum debug_level : int { }; struct debug_level_traits { - using underlying_type = int; - static constexpr std::size_t size = 3; - - static constexpr const debug_level values[] = { - level_info, - level_warning, - level_warning - }; - - static constexpr const std::string_view names[] = { - "level_info", - "level_warning", - "level_warning" - }; - - static constexpr underlying_type to_underlying(debug_level e) noexcept; - - static constexpr std::optional to_string(debug_level e) noexcept; - static constexpr std::string_view to_string_or_empty(Enum e) noexcept; - static std::string_view to_string_or_throw(Enum e); - - static constexpr std::optional from_string(std::string_view name) noexcept; - static constexpr debug_level from_string_or_default(std::string_view name, debug_level def) noexcept; - static debug_level from_string_or_throw(std::string_view name); - - static constexpr std::optional to_index(debug_level e) noexcept; - static constexpr std::size_t to_index_or_invalid(debug_level e) noexcept; - static std::size_t to_index_or_throw(debug_level e); - - static constexpr std::optional from_index(std::size_t index) noexcept; - static constexpr debug_level from_index_or_default(std::size_t index, debug_level def) noexcept; - static debug_level from_index_or_throw(std::size_t index); + /*...*/ }; ``` @@ -115,40 +84,7 @@ enum class color : unsigned { }; struct color_traits { - using underlying_type = unsigned; - static constexpr std::size_t size = 4; - - static constexpr const color values[] = { - color::red, - color::green, - color::blue, - color::white - }; - - static constexpr const std::string_view names[] = { - "red", - "green", - "blue", - "white" - }; - - static constexpr underlying_type to_underlying(color e) noexcept; - - static constexpr std::optional to_string(color e) noexcept; - static constexpr std::string_view to_string_or_empty(Enum e) noexcept; - static std::string_view to_string_or_throw(Enum e); - - static constexpr std::optional from_string(std::string_view name) noexcept; - static constexpr color from_string_or_default(std::string_view name, color def) noexcept; - static color from_string_or_throw(std::string_view name); - - static constexpr std::optional to_index(color e) noexcept; - static constexpr std::size_t to_index_or_invalid(color e) noexcept; - static std::size_t to_index_or_throw(color e); - - static constexpr std::optional from_index(std::size_t index) noexcept; - static constexpr color from_index_or_default(std::size_t index, color def) noexcept; - static color from_index_or_throw(std::size_t index); + /*...*/ }; ``` @@ -196,6 +132,180 @@ int main() { } ``` +### Generic context + +```cpp +namespace some_namespace +{ + ENUM_HPP_CLASS_DECL(color, unsigned, + (red = 0xFF0000) + (green = 0x00FF00) + (blue = 0x0000FF) + (white = red | green | blue)) +} + +// register traits in global namespace to generic access +ENUM_HPP_REGISTER_TRAITS(color) + +int main() { + using color = some_namespace::color; + + // to string + static_assert(enum_hpp::to_string(color::red) == "red"); + + // from string + static_assert(enum_hpp::from_string("red") == color::red); + + return 0; +} +``` + +### Adapting external enums + +```cpp +namespace external_ns +{ + enum class external_enum : unsigned short { + a = 10, + b, + c = a + b + }; + + // should be in the same namespace + ENUM_HPP_TRAITS_DECL(external_enum, + (a) + (b) + (c)) +} + +ENUM_HPP_REGISTER_TRAITS(external_ns::external_enum) + +int main() { + using ee = external_ns::external_enum; + static_assert(enum_hpp::to_string(ee::a) == "a"); + static_assert(enum_hpp::from_string("c") == ee::c); + return 0; +} +``` + +## API + +### Enum traits + +```cpp +// declare enum +ENUM_HPP_DECL( + /*enum_name*/, + /*underlying_type*/, + /*fields*/) + +// declare enum class +ENUM_HPP_CLASS_DECL( + /*enum_name*/, + /*underlying_type*/, + /*fields*/) + +// declare only traits +ENUM_HPP_TRAITS_DECL( + /*enum_name*/, + /*fields*/) + +struct /*enum_name*/_traits { + using underlying_type = /*underlying_type*/; + static constexpr std::size_t size = /*field_count*/; + + static constexpr const std::array values = { + /*enum values*/ + }; + + static constexpr const std::array names = { + /*enum names*/ + }; + + static constexpr /*underlying_type*/ to_underlying(/*enum_name*/ e) noexcept; + + static constexpr std::optional to_string(/*enum_name*/ e) noexcept; + static constexpr std::string_view to_string_or_empty(/*enum_name*/ e) noexcept; + static std::string_view to_string_or_throw(/*enum_name*/ e); + + static constexpr std::optional from_string(std::string_view name) noexcept; + static constexpr /*enum_name*/ from_string_or_default(std::string_view name, /*enum_name*/ def) noexcept; + static /*enum_name*/ from_string_or_throw(std::string_view name); + + static constexpr std::optional to_index(/*enum_name*/ e) noexcept; + static constexpr std::size_t to_index_or_invalid(/*enum_name*/ e) noexcept; + static std::size_t to_index_or_throw(/*enum_name*/ e); + + static constexpr std::optional from_index(std::size_t index) noexcept; + static constexpr /*enum_name*/ from_index_or_default(std::size_t index, /*enum_name*/ def) noexcept; + static /*enum_name*/ from_index_or_throw(std::size_t index); +}; +``` + +### Generic functions + +```cpp +// should be in global namespace +ENUM_HPP_REGISTER_TRAITS(/*declared_enum_name*/) + +namespace enum_hpp +{ + template < typename Enum > + using traits_t = typename traits::type; + + template < typename Enum > + using underlying_type = typename traits_t::underlying_type; + + template < typename Enum > + constexpr std::size_t size() noexcept; + + template < typename Enum > + constexpr const std::array()>& values() noexcept; + + template < typename Enum > + constexpr const std::array()>& names() noexcept; + + template < typename Enum > + constexpr typename traits_t::underlying_type to_underlying(Enum e) noexcept; + + template < typename Enum > + constexpr std::optional to_string(Enum e) noexcept; + + template < typename Enum > + constexpr std::string_view to_string_or_empty(Enum e) noexcept; + + template < typename Enum > + std::string_view to_string_or_throw(Enum e); + + template < typename Enum > + constexpr std::optional from_string(std::string_view name) noexcept; + + template < typename Enum > + constexpr Enum from_string_or_default(std::string_view name, Enum def) noexcept; + + template < typename Enum > + Enum from_string_or_throw(std::string_view name); + + template < typename Enum > + constexpr std::optional to_index(Enum e) noexcept; + + template < typename Enum > + constexpr std::size_t to_index_or_invalid(Enum e) noexcept; + + template < typename Enum > + std::size_t to_index_or_throw(Enum e); + + template < typename Enum > + constexpr std::optional from_index(std::size_t index) noexcept; + + template < typename Enum > + constexpr Enum from_index_or_default(std::size_t index, Enum def) noexcept; + + template < typename Enum > + Enum from_index_or_throw(std::size_t index); +} +``` + ## Alternatives [Better Enums](https://github.com/aantron/better-enums) diff --git a/headers/enum.hpp/enum.hpp b/headers/enum.hpp/enum.hpp index c3318e1..a2d460e 100644 --- a/headers/enum.hpp/enum.hpp +++ b/headers/enum.hpp/enum.hpp @@ -6,6 +6,7 @@ #pragma once +#include #include #include #include @@ -23,6 +24,98 @@ namespace enum_hpp }; } +namespace enum_hpp +{ + template < typename Enum > + struct traits; + + template < typename Enum > + using traits_t = typename traits::type; + + template < typename Enum > + using underlying_type = typename traits_t::underlying_type; + + template < typename Enum > + constexpr std::size_t size() noexcept { + return traits_t::size; + } + + template < typename Enum > + constexpr const std::array()>& values() noexcept { + return traits_t::values; + } + + template < typename Enum > + constexpr const std::array()>& names() noexcept { + return traits_t::names; + } + + template < typename Enum > + constexpr typename traits_t::underlying_type to_underlying(Enum e) noexcept { + return traits_t::to_underlying(e); + } + + template < typename Enum > + constexpr std::optional to_string(Enum e) noexcept { + return traits_t::to_string(e); + } + + template < typename Enum > + constexpr std::string_view to_string_or_empty(Enum e) noexcept { + return traits_t::to_string_or_empty(e); + } + + template < typename Enum > + std::string_view to_string_or_throw(Enum e) { + return traits_t::to_string_or_throw(e); + } + + template < typename Enum > + constexpr std::optional from_string(std::string_view name) noexcept { + return traits_t::from_string(name); + } + + template < typename Enum > + constexpr Enum from_string_or_default(std::string_view name, Enum def) noexcept { + return traits_t::from_string_or_default(name, def); + } + + template < typename Enum > + Enum from_string_or_throw(std::string_view name) { + return traits_t::from_string_or_throw(name); + } + + template < typename Enum > + constexpr std::optional to_index(Enum e) noexcept { + return traits_t::to_index(e); + } + + template < typename Enum > + constexpr std::size_t to_index_or_invalid(Enum e) noexcept { + return traits_t::to_index_or_invalid(e); + } + + template < typename Enum > + std::size_t to_index_or_throw(Enum e) { + return traits_t::to_index_or_throw(e); + } + + template < typename Enum > + constexpr std::optional from_index(std::size_t index) noexcept { + return traits_t::from_index(index); + } + + template < typename Enum > + constexpr Enum from_index_or_default(std::size_t index, Enum def) noexcept { + return traits_t::from_index_or_default(index, def); + } + + template < typename Enum > + Enum from_index_or_throw(std::size_t index) { + return traits_t::from_index_or_throw(index); + } +} + namespace enum_hpp::detail { template < typename Enum > @@ -118,12 +211,20 @@ namespace enum_hpp::detail #define ENUM_HPP_TRAITS_DECL(Enum, Fields)\ struct Enum##_traits {\ private:\ - enum enum_names_for_this_score_ { ENUM_HPP_GENERATE_ENUM_FIELDS(Fields) };\ + enum enum_names_for_this_score_ {\ + ENUM_HPP_GENERATE_ENUM_FIELDS(Fields)\ + };\ public:\ using underlying_type = std::underlying_type_t;\ static constexpr std::size_t size = ENUM_HPP_PP_SEQ_SIZE(Fields);\ - static constexpr const Enum values[] = { ENUM_HPP_GENERATE_VALUES(Enum, Fields) };\ - static constexpr const std::string_view names[] = { ENUM_HPP_GENERATE_NAMES(Fields) };\ + \ + static constexpr const std::array values = {\ + ENUM_HPP_GENERATE_VALUES(Enum, Fields)\ + };\ + \ + static constexpr const std::array names = {\ + ENUM_HPP_GENERATE_NAMES(Fields)\ + };\ public:\ static constexpr underlying_type to_underlying(Enum e) noexcept {\ return static_cast(e);\ @@ -213,6 +314,16 @@ namespace enum_hpp::detail }\ }; +// +// ENUM_HPP_REGISTER_TRAITS +// + +#define ENUM_HPP_REGISTER_TRAITS(Enum)\ + template <>\ + struct enum_hpp::traits {\ + using type = Enum##_traits;\ + }; + // ----------------------------------------------------------------------------- // // ENUM_HPP_PP diff --git a/untests/enum_examples.cpp b/untests/enum_examples.cpp index e260e3f..e49598d 100644 --- a/untests/enum_examples.cpp +++ b/untests/enum_examples.cpp @@ -21,6 +21,8 @@ namespace (white = red | green | blue)) } +ENUM_HPP_REGISTER_TRAITS(color) + TEST_CASE("examples") { SECTION("traits_using") { // size @@ -50,4 +52,12 @@ TEST_CASE("examples") { std::cout << n << ","; } // stdout: red,green,blue, } + + SECTION("generic_context") { + // to string + static_assert(enum_hpp::to_string(color::red) == "red"); + + // from string + static_assert(enum_hpp::from_string("red") == color::red); + } } diff --git a/untests/enum_tests.cpp b/untests/enum_tests.cpp index d3756bf..bd67a75 100644 --- a/untests/enum_tests.cpp +++ b/untests/enum_tests.cpp @@ -53,8 +53,26 @@ namespace some_namespace (_181)(_182)(_183)(_184)(_185)(_186)(_187)(_188)(_189)(_190)(_191)(_192)(_193)(_194)(_195)(_196)(_197)(_198)(_199)(_200) (_201)(_202)(_203)(_204)(_205)(_206)(_207)(_208)(_209)(_210)(_211)(_212)(_213)(_214)(_215)(_216)(_217)(_218)(_219)(_220) (_221)(_222)(_223)(_224)(_225)(_226)(_227)(_228)(_229)(_230)(_231)(_232)(_233)(_234)(_235)(_236)(_237)(_238)(_239)(_240)) + + namespace exns + { + enum class external_enum : unsigned short { + a = 10, + b, + c = a + b + }; + + ENUM_HPP_TRAITS_DECL(external_enum, + (a) + (b) + (c)) + } } +ENUM_HPP_REGISTER_TRAITS(some_namespace::color) +ENUM_HPP_REGISTER_TRAITS(some_namespace::numbers) +ENUM_HPP_REGISTER_TRAITS(some_namespace::exns::external_enum) + TEST_CASE("enum") { namespace sn = some_namespace; @@ -64,6 +82,14 @@ TEST_CASE("enum") { std::underlying_type_t, unsigned>); + STATIC_REQUIRE(std::is_same_v< + sn::color_traits::underlying_type, + enum_hpp::underlying_type>); + + STATIC_REQUIRE(std::is_same_v< + sn::color_traits, + enum_hpp::traits_t>); + REQUIRE(enum_to_underlying(sn::color::red) == 2u); REQUIRE(enum_to_underlying(sn::color::green) == 3u); REQUIRE(enum_to_underlying(sn::color::blue) == 6u); @@ -83,6 +109,14 @@ TEST_CASE("enum") { std::underlying_type_t, int>); + STATIC_REQUIRE(std::is_same_v< + sn::numbers_traits::underlying_type, + enum_hpp::underlying_type>); + + STATIC_REQUIRE(std::is_same_v< + sn::numbers_traits, + enum_hpp::traits_t>); + REQUIRE(enum_to_underlying(sn::_0) == 0); REQUIRE(enum_to_underlying(sn::_100) == 100); REQUIRE(enum_to_underlying(sn::_240) == 240); @@ -91,19 +125,29 @@ TEST_CASE("enum") { SECTION("size") { { - REQUIRE(sn::color_traits::size == 3u); - REQUIRE(sn::color_traits::size == std::size(sn::color_traits::names)); - REQUIRE(sn::color_traits::size == std::size(sn::color_traits::values)); + STATIC_REQUIRE(sn::color_traits::size == 3u); + STATIC_REQUIRE(sn::color_traits::size == std::size(sn::color_traits::names)); + STATIC_REQUIRE(sn::color_traits::size == std::size(sn::color_traits::values)); + + STATIC_REQUIRE(enum_hpp::size() == 3u); + STATIC_REQUIRE(enum_hpp::traits_t::size == 3u); + STATIC_REQUIRE(std::size(enum_hpp::names()) == 3u); + STATIC_REQUIRE(std::size(enum_hpp::values()) == 3u); } { - REQUIRE(sn::render::mask_traits::size == 4u); - REQUIRE(sn::render::mask_traits::size == std::size(sn::render::mask_traits::names)); - REQUIRE(sn::render::mask_traits::size == std::size(sn::render::mask_traits::values)); + STATIC_REQUIRE(sn::render::mask_traits::size == 4u); + STATIC_REQUIRE(sn::render::mask_traits::size == std::size(sn::render::mask_traits::names)); + STATIC_REQUIRE(sn::render::mask_traits::size == std::size(sn::render::mask_traits::values)); } { - REQUIRE(sn::numbers_traits::size == 241u); - REQUIRE(sn::numbers_traits::size == std::size(sn::numbers_traits::names)); - REQUIRE(sn::numbers_traits::size == std::size(sn::numbers_traits::values)); + STATIC_REQUIRE(sn::numbers_traits::size == 241u); + STATIC_REQUIRE(sn::numbers_traits::size == std::size(sn::numbers_traits::names)); + STATIC_REQUIRE(sn::numbers_traits::size == std::size(sn::numbers_traits::values)); + + STATIC_REQUIRE(enum_hpp::size() == 241u); + STATIC_REQUIRE(enum_hpp::traits_t::size == 241u); + STATIC_REQUIRE(std::size(enum_hpp::names()) == 241u); + STATIC_REQUIRE(std::size(enum_hpp::values()) == 241u); } } @@ -113,6 +157,11 @@ TEST_CASE("enum") { STATIC_REQUIRE(sn::color_traits::to_underlying(sn::color::green) == enum_to_underlying(sn::color::green)); STATIC_REQUIRE(sn::color_traits::to_underlying(sn::color::blue) == enum_to_underlying(sn::color::blue)); STATIC_REQUIRE(sn::color_traits::to_underlying(sn::color(42)) == 42); + + STATIC_REQUIRE(enum_hpp::to_underlying(sn::color::red) == enum_to_underlying(sn::color::red)); + STATIC_REQUIRE(enum_hpp::to_underlying(sn::color::green) == enum_to_underlying(sn::color::green)); + STATIC_REQUIRE(enum_hpp::to_underlying(sn::color::blue) == enum_to_underlying(sn::color::blue)); + STATIC_REQUIRE(enum_hpp::to_underlying(sn::color(42)) == 42); } { STATIC_REQUIRE(sn::render::mask_traits::to_underlying(sn::render::mask::none) == enum_to_underlying(sn::render::mask::none)); @@ -125,6 +174,11 @@ TEST_CASE("enum") { STATIC_REQUIRE(sn::numbers_traits::to_underlying(sn::_180) == enum_to_underlying(sn::_180)); STATIC_REQUIRE(sn::numbers_traits::to_underlying(sn::_240) == enum_to_underlying(sn::_240)); STATIC_REQUIRE(sn::numbers_traits::to_underlying(sn::numbers(100500)) == 100500); + + STATIC_REQUIRE(enum_hpp::to_underlying(sn::_0) == enum_to_underlying(sn::_0)); + STATIC_REQUIRE(enum_hpp::to_underlying(sn::_180) == enum_to_underlying(sn::_180)); + STATIC_REQUIRE(enum_hpp::to_underlying(sn::_240) == enum_to_underlying(sn::_240)); + STATIC_REQUIRE(enum_hpp::to_underlying(sn::numbers(100500)) == 100500); } } @@ -145,6 +199,11 @@ TEST_CASE("enum") { STATIC_REQUIRE_FALSE(sn::color_traits::to_string(sn::color(42))); STATIC_REQUIRE(sn::color_traits::to_string_or_empty(sn::color(42)) == ""); REQUIRE_THROWS_AS(sn::color_traits::to_string_or_throw(sn::color(42)), enum_hpp::exception); + + STATIC_REQUIRE(enum_hpp::to_string(sn::color::green) == "green"); + STATIC_REQUIRE(enum_hpp::to_string_or_empty(sn::color::green) == "green"); + REQUIRE(enum_hpp::to_string_or_throw(sn::color::green) == "green"); + REQUIRE_THROWS_AS(enum_hpp::to_string_or_throw(sn::color(42)), enum_hpp::exception); } { STATIC_REQUIRE(sn::render::mask_traits::to_string(sn::render::mask::none) == "none"); @@ -178,6 +237,11 @@ TEST_CASE("enum") { STATIC_REQUIRE_FALSE(sn::numbers_traits::to_string(sn::numbers(100500))); STATIC_REQUIRE(sn::numbers_traits::to_string_or_empty(sn::numbers(100500)) == ""); REQUIRE_THROWS_AS(sn::numbers_traits::to_string_or_throw(sn::numbers(100500)), enum_hpp::exception); + + STATIC_REQUIRE(enum_hpp::to_string(sn::_180) == "_180"); + STATIC_REQUIRE(enum_hpp::to_string_or_empty(sn::_180) == "_180"); + REQUIRE(enum_hpp::to_string_or_throw(sn::_180) == "_180"); + REQUIRE_THROWS_AS(enum_hpp::to_string_or_throw(sn::numbers(100500)), enum_hpp::exception); } } @@ -198,6 +262,11 @@ TEST_CASE("enum") { STATIC_REQUIRE_FALSE(sn::color_traits::from_string("42")); STATIC_REQUIRE(sn::color_traits::from_string_or_default("42", sn::color::red) == sn::color::red); REQUIRE_THROWS_AS(sn::color_traits::from_string_or_throw("42"), enum_hpp::exception); + + STATIC_REQUIRE(enum_hpp::from_string("green") == sn::color::green); + STATIC_REQUIRE(enum_hpp::from_string_or_default("green", sn::color::red) == sn::color::green); + REQUIRE(enum_hpp::from_string_or_throw("green") == sn::color::green); + REQUIRE_THROWS_AS(enum_hpp::from_string_or_throw("42"), enum_hpp::exception); } { STATIC_REQUIRE(sn::render::mask_traits::from_string("none") == sn::render::mask::none); @@ -210,6 +279,10 @@ TEST_CASE("enum") { STATIC_REQUIRE(sn::numbers_traits::from_string("_10") == sn::_10); STATIC_REQUIRE(sn::numbers_traits::from_string("_240") == sn::_240); STATIC_REQUIRE_FALSE(sn::numbers_traits::from_string("error")); + + STATIC_REQUIRE(enum_hpp::from_string("_10") == sn::_10); + STATIC_REQUIRE(enum_hpp::from_string("_240") == sn::_240); + STATIC_REQUIRE_FALSE(enum_hpp::from_string("error")); } } @@ -230,6 +303,11 @@ TEST_CASE("enum") { STATIC_REQUIRE_FALSE(sn::color_traits::to_index(sn::color(42))); STATIC_REQUIRE(sn::color_traits::to_index_or_invalid(sn::color(42)) == enum_hpp::invalid_index); REQUIRE_THROWS_AS(sn::color_traits::to_index_or_throw(sn::color(42)), enum_hpp::exception); + + STATIC_REQUIRE(enum_hpp::to_index(sn::color::green) == 1u); + STATIC_REQUIRE(enum_hpp::to_index_or_invalid(sn::color::green) == 1u); + REQUIRE(enum_hpp::to_index_or_throw(sn::color::green) == 1u); + REQUIRE_THROWS_AS(enum_hpp::to_index_or_throw(sn::color(42)), enum_hpp::exception); } { STATIC_REQUIRE(sn::render::mask_traits::to_index(sn::render::mask::none) == 0u); @@ -263,6 +341,11 @@ TEST_CASE("enum") { STATIC_REQUIRE_FALSE(sn::numbers_traits::to_index(sn::numbers(100500))); STATIC_REQUIRE(sn::numbers_traits::to_index_or_invalid(sn::numbers(100500)) == enum_hpp::invalid_index); REQUIRE_THROWS_AS(sn::numbers_traits::to_index_or_throw(sn::numbers(100500)), enum_hpp::exception); + + STATIC_REQUIRE(enum_hpp::to_index(sn::_180) == 180u); + STATIC_REQUIRE(enum_hpp::to_index_or_invalid(sn::_180) == 180u); + REQUIRE(enum_hpp::to_index_or_throw(sn::_180) == 180u); + REQUIRE_THROWS_AS(enum_hpp::to_index_or_throw(sn::numbers(100500)), enum_hpp::exception); } } @@ -283,6 +366,11 @@ TEST_CASE("enum") { STATIC_REQUIRE_FALSE(sn::color_traits::from_index(42)); STATIC_REQUIRE(sn::color_traits::from_index_or_default(42, sn::color::red) == sn::color::red); REQUIRE_THROWS_AS(sn::color_traits::from_index_or_throw(42), enum_hpp::exception); + + STATIC_REQUIRE(enum_hpp::from_index(1) == sn::color::green); + STATIC_REQUIRE(enum_hpp::from_index_or_default(1, sn::color::red) == sn::color::green); + REQUIRE(enum_hpp::from_index_or_throw(1) == sn::color::green); + REQUIRE_THROWS_AS(enum_hpp::from_index_or_throw(42), enum_hpp::exception); } { STATIC_REQUIRE(sn::render::mask_traits::from_index(0) == sn::render::mask::none); @@ -295,6 +383,25 @@ TEST_CASE("enum") { STATIC_REQUIRE(sn::numbers_traits::from_index(10) == sn::_10); STATIC_REQUIRE(sn::numbers_traits::from_index(240) == sn::_240); STATIC_REQUIRE_FALSE(sn::numbers_traits::from_index(100500)); + + STATIC_REQUIRE(enum_hpp::from_index(10) == sn::_10); + STATIC_REQUIRE(enum_hpp::from_index(240) == sn::_240); + STATIC_REQUIRE_FALSE(enum_hpp::from_index(100500)); } } } + +TEST_CASE("external_enum") { + using ee = some_namespace::exns::external_enum; + STATIC_REQUIRE(std::is_same_v, unsigned short>); + STATIC_REQUIRE(enum_hpp::size() == 3); + + STATIC_REQUIRE(enum_hpp::names()[0] == "a"); + STATIC_REQUIRE(enum_hpp::names()[2] == "c"); + + STATIC_REQUIRE(enum_hpp::values()[0] == ee::a); + STATIC_REQUIRE(enum_hpp::values()[2] == ee::c); + + STATIC_REQUIRE(enum_hpp::to_string(ee::c) == "c"); + STATIC_REQUIRE(enum_hpp::from_string("b") == ee::b); +}