// Licensed under the MIT License . // SPDX-License-Identifier: MIT // Copyright (c) 2018 - 2019 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 NAMEOF_ENUM_RANGE_MIN -120 #define NAMEOF_ENUM_RANGE_MAX 120 #include #include #include struct SomeStruct { int somefield = 0; void SomeMethod1(int) { throw std::runtime_error{"should not be called!"}; } int SomeMethod2() const { throw std::runtime_error{"should not be called!"}; } }; void SomeMethod3() { throw std::runtime_error{"should not be called!"}; } template std::string SomeMethod4(U) { throw std::runtime_error{"should not be called!"}; } template class SomeClass { public: void SomeMethod5() const { throw std::runtime_error{"should not be called!"}; } template C SomeMethod6() const { throw std::runtime_error{"should not be called!"}; } }; struct Long { struct LL { int field = 0; }; LL ll; }; enum class Color { RED = -12, GREEN = 7, BLUE = 15 }; enum class Numbers : char { one = 10, two = 20, three = 30, many = 127 }; enum Directions { Up = 85, Down = -42, Right = 120, Left = -120 }; enum number : unsigned long { zero = 0, one = 100, two = 200, three = 300, four = 400 }; namespace nameof { template <> struct enum_range { static_assert(std::is_enum_v, "nameof::enum_range requires enum type."); static constexpr int min = 100; static constexpr int max = 300; static_assert(max > min, "nameof::enum_range requires max > min."); }; } SomeStruct struct_var; Long othervar; SomeStruct * ptr_s = &struct_var; SomeStruct & ref_s = struct_var; SomeClass class_var; const SomeClass volatile * ptr_c = nullptr; const Color color = Color::RED; TEST_CASE("NAMEOF") { SECTION("variable") { constexpr auto name = NAMEOF(othervar); REQUIRE(name == "othervar"); REQUIRE(NAMEOF(struct_var) == "struct_var"); REQUIRE(NAMEOF(::struct_var) == "struct_var"); REQUIRE(NAMEOF(ptr_s) == "ptr_s"); REQUIRE(NAMEOF(color) == "color"); } SECTION("member") { REQUIRE(NAMEOF(struct_var.somefield) == "somefield"); REQUIRE(NAMEOF((&struct_var)->somefield) == "somefield"); REQUIRE(NAMEOF(othervar.ll.field) == "field"); } SECTION("function") { REQUIRE(NAMEOF(&SomeStruct::SomeMethod1) == "SomeMethod1"); REQUIRE(NAMEOF(struct_var.SomeMethod1(1)) == "SomeMethod1"); REQUIRE(NAMEOF(&SomeStruct::SomeMethod2) == "SomeMethod2"); REQUIRE(NAMEOF(struct_var.SomeMethod2()) == "SomeMethod2"); REQUIRE(NAMEOF(SomeMethod3) == "SomeMethod3"); REQUIRE(NAMEOF(SomeMethod3()) == "SomeMethod3"); REQUIRE(NAMEOF(SomeMethod4) == "SomeMethod4"); REQUIRE(NAMEOF(SomeMethod4(1.0f)) == "SomeMethod4"); REQUIRE(NAMEOF(&SomeClass::SomeMethod5) == "SomeMethod5"); REQUIRE(NAMEOF(class_var.SomeMethod5()) == "SomeMethod5"); REQUIRE(NAMEOF(&SomeClass::SomeMethod6) == "SomeMethod6"); REQUIRE(NAMEOF(class_var.SomeMethod6()) == "SomeMethod6"); } SECTION("enum") { REQUIRE(NAMEOF(Color::RED) == "RED"); REQUIRE(NAMEOF(Color::BLUE) == "BLUE"); } } TEST_CASE("NAMEOF_FULL") { SECTION("variable") { constexpr auto full_name = NAMEOF_FULL(othervar); REQUIRE(full_name == "othervar"); REQUIRE(NAMEOF_FULL(struct_var) == "struct_var"); REQUIRE(NAMEOF_FULL(::struct_var) == "struct_var"); REQUIRE(NAMEOF_FULL(ptr_s) == "ptr_s"); REQUIRE(NAMEOF_FULL(color) == "color"); } SECTION("member") { REQUIRE(NAMEOF_FULL(struct_var.somefield) == "somefield"); REQUIRE(NAMEOF_FULL((&struct_var)->somefield) == "somefield"); REQUIRE(NAMEOF_FULL(othervar.ll.field) == "field"); } SECTION("function") { REQUIRE(NAMEOF_FULL(&SomeStruct::SomeMethod1) == "SomeMethod1"); REQUIRE(NAMEOF_FULL(struct_var.SomeMethod1(1)) == "SomeMethod1"); REQUIRE(NAMEOF_FULL(&SomeStruct::SomeMethod2) == "SomeMethod2"); REQUIRE(NAMEOF_FULL(struct_var.SomeMethod2()) == "SomeMethod2"); REQUIRE(NAMEOF_FULL(SomeMethod3) == "SomeMethod3"); REQUIRE(NAMEOF_FULL(SomeMethod3()) == "SomeMethod3"); REQUIRE(NAMEOF_FULL(SomeMethod4) == "SomeMethod4"); REQUIRE(NAMEOF_FULL(SomeMethod4(1.0f)) == "SomeMethod4"); REQUIRE(NAMEOF_FULL(&SomeClass::SomeMethod5) == "SomeMethod5"); REQUIRE(NAMEOF_FULL(class_var.SomeMethod5()) == "SomeMethod5"); REQUIRE(NAMEOF_FULL(&SomeClass::SomeMethod6) == "SomeMethod6"); REQUIRE(NAMEOF_FULL(class_var.SomeMethod6()) == "SomeMethod6"); } SECTION("enum") { REQUIRE(NAMEOF_FULL(Color::RED) == "RED"); REQUIRE(NAMEOF_FULL(Color::BLUE) == "BLUE"); } } TEST_CASE("NAMEOF_RAW") { SECTION("variable") { constexpr auto raw_name = NAMEOF_RAW(othervar); REQUIRE(raw_name == "othervar"); REQUIRE(NAMEOF_RAW(struct_var) == "struct_var"); REQUIRE(NAMEOF_RAW(&struct_var) == "&struct_var"); REQUIRE(NAMEOF_RAW(::struct_var) == "::struct_var"); REQUIRE(NAMEOF_RAW(ptr_s) == "ptr_s"); REQUIRE(NAMEOF_RAW(*ptr_s) == "*ptr_s"); REQUIRE(NAMEOF_RAW(ptr_s[0]) == "ptr_s[0]"); REQUIRE(NAMEOF_RAW(color) == "color"); } SECTION("member") { REQUIRE(NAMEOF_RAW(struct_var.somefield) == "struct_var.somefield"); REQUIRE(NAMEOF_RAW(struct_var.somefield++) == "struct_var.somefield++"); REQUIRE(NAMEOF_RAW((&struct_var)->somefield) == "(&struct_var)->somefield"); REQUIRE(NAMEOF_RAW(othervar.ll.field) == "othervar.ll.field"); REQUIRE(NAMEOF_RAW(+struct_var.somefield) == "+struct_var.somefield"); REQUIRE(NAMEOF_RAW(-struct_var.somefield) == "-struct_var.somefield"); REQUIRE(NAMEOF_RAW(~struct_var.somefield) == "~struct_var.somefield"); REQUIRE(NAMEOF_RAW(!struct_var.somefield) == "!struct_var.somefield"); REQUIRE(NAMEOF_RAW(struct_var.somefield + ref_s.somefield) == "struct_var.somefield + ref_s.somefield"); } SECTION("function") { REQUIRE(NAMEOF_RAW(&SomeStruct::SomeMethod1) == "&SomeStruct::SomeMethod1"); REQUIRE(NAMEOF_RAW(struct_var.SomeMethod1(1)) == "struct_var.SomeMethod1(1)"); REQUIRE(NAMEOF_RAW(&SomeStruct::SomeMethod2) == "&SomeStruct::SomeMethod2"); REQUIRE(NAMEOF_RAW(struct_var.SomeMethod2()) == "struct_var.SomeMethod2()"); REQUIRE(NAMEOF_RAW(SomeMethod3) == "SomeMethod3"); REQUIRE(NAMEOF_RAW(SomeMethod3()) == "SomeMethod3()"); REQUIRE(NAMEOF_RAW(SomeMethod4) == "SomeMethod4"); REQUIRE(NAMEOF_RAW(SomeMethod4(1.0f)) == "SomeMethod4(1.0f)"); REQUIRE(NAMEOF_RAW(&SomeClass::SomeMethod5) == "&SomeClass::SomeMethod5"); REQUIRE(NAMEOF_RAW(class_var.SomeMethod5()) == "class_var.SomeMethod5()"); REQUIRE(NAMEOF_RAW(&SomeClass::SomeMethod6) == "&SomeClass::SomeMethod6"); REQUIRE(NAMEOF_RAW(class_var.SomeMethod6()) == "class_var.SomeMethod6()"); } SECTION("enum") { REQUIRE(NAMEOF_RAW(Color::RED) == "Color::RED"); REQUIRE(NAMEOF_RAW(Color::BLUE) == "Color::BLUE"); } SECTION("macro") { REQUIRE(NAMEOF_RAW(__cplusplus) == "__cplusplus"); REQUIRE(NAMEOF_RAW(__LINE__) == "__LINE__"); REQUIRE(NAMEOF_RAW(__FILE__) == "__FILE__"); } } TEST_CASE("NAMEOF_ENUM") { #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9) || defined(_MSC_VER) constexpr Color cr = Color::RED; constexpr auto cr_name = NAMEOF_ENUM(cr); Color cm[3] = {Color::RED, Color::GREEN, Color::BLUE}; REQUIRE(cr_name == "RED"); REQUIRE(NAMEOF_ENUM(Color::BLUE) == "BLUE"); REQUIRE(NAMEOF_ENUM(cm[1]) == "GREEN"); REQUIRE(NAMEOF_ENUM(static_cast(0)).empty()); constexpr Numbers no = Numbers::one; constexpr auto no_name = NAMEOF_ENUM(no); REQUIRE(no_name == "one"); REQUIRE(NAMEOF_ENUM(Numbers::two) == "two"); REQUIRE(NAMEOF_ENUM(Numbers::three) == "three"); REQUIRE(NAMEOF_ENUM(static_cast(0)).empty()); REQUIRE(NAMEOF_ENUM(static_cast(127)).empty()); constexpr Directions dr = Directions::Right; constexpr auto dr_name = NAMEOF_ENUM(dr); REQUIRE(NAMEOF_ENUM(Directions::Up) == "Up"); REQUIRE(NAMEOF_ENUM(Directions::Down) == "Down"); REQUIRE(dr_name == "Right"); REQUIRE(NAMEOF_ENUM(Directions::Left) == "Left"); REQUIRE(NAMEOF_ENUM(static_cast(0)).empty()); constexpr number nt = number::three; constexpr auto nt_name = NAMEOF_ENUM(nt); REQUIRE(NAMEOF_ENUM(number::one) == "one"); REQUIRE(NAMEOF_ENUM(number::two) == "two"); REQUIRE(nt_name == "three"); REQUIRE(NAMEOF_ENUM(static_cast(0)).empty()); REQUIRE(NAMEOF_ENUM(static_cast(400)).empty()); #endif } TEST_CASE("nameof::nameof_enum") { #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9) || defined(_MSC_VER) constexpr Color cr = Color::RED; constexpr auto cr_name = nameof::nameof_enum(cr); Color cm[3] = {Color::RED, Color::GREEN, Color::BLUE}; REQUIRE(cr_name == "RED"); REQUIRE(nameof::nameof_enum(Color::BLUE) == "BLUE"); REQUIRE(nameof::nameof_enum(cm[1]) == "GREEN"); REQUIRE(nameof::nameof_enum(static_cast(0)).empty()); constexpr Numbers no = Numbers::one; constexpr auto no_name = nameof::nameof_enum(no); REQUIRE(no_name == "one"); REQUIRE(nameof::nameof_enum(Numbers::two) == "two"); REQUIRE(nameof::nameof_enum(Numbers::three) == "three"); REQUIRE(nameof::nameof_enum(static_cast(0)).empty()); REQUIRE(nameof::nameof_enum(static_cast(127)).empty()); constexpr Directions dr = Directions::Right; constexpr auto dr_name = nameof::nameof_enum(dr); REQUIRE(nameof::nameof_enum(Directions::Up) == "Up"); REQUIRE(nameof::nameof_enum(Directions::Down) == "Down"); REQUIRE(dr_name == "Right"); REQUIRE(nameof::nameof_enum(Directions::Left) == "Left"); REQUIRE(nameof::nameof_enum(static_cast(0)).empty()); constexpr number nt = number::three; constexpr auto nt_name = nameof::nameof_enum(nt); REQUIRE(nameof::nameof_enum(number::one) == "one"); REQUIRE(nameof::nameof_enum(number::two) == "two"); REQUIRE(nt_name == "three"); REQUIRE(nameof::nameof_enum(static_cast(0)).empty()); REQUIRE(nameof::nameof_enum(static_cast(400)).empty()); #endif } TEST_CASE("NAMEOF_VAR_TYPE") { constexpr auto type_name = NAMEOF_VAR_TYPE(struct_var); #if defined(__clang__) REQUIRE(type_name == "SomeStruct"); REQUIRE(NAMEOF_VAR_TYPE(ptr_s) == "SomeStruct *"); REQUIRE(NAMEOF_VAR_TYPE(ref_s) == "SomeStruct &"); REQUIRE(NAMEOF_VAR_TYPE(ptr_c) == "const volatile SomeClass *"); REQUIRE(NAMEOF_VAR_TYPE(othervar) == "Long"); REQUIRE(NAMEOF_VAR_TYPE(othervar.ll) == "Long::LL"); REQUIRE(NAMEOF_VAR_TYPE(othervar.ll.field) == "int"); REQUIRE(NAMEOF_VAR_TYPE(Color::RED) == "Color"); REQUIRE(NAMEOF_VAR_TYPE(std::declval>()) == "const SomeClass &&"); #elif defined(_MSC_VER) REQUIRE(type_name == "struct SomeStruct"); REQUIRE(NAMEOF_VAR_TYPE(ptr_s) == "struct SomeStruct *"); REQUIRE(NAMEOF_VAR_TYPE(ref_s) == "struct SomeStruct &"); REQUIRE(NAMEOF_VAR_TYPE(ptr_c) == "class SomeClass const volatile *"); REQUIRE(NAMEOF_VAR_TYPE(othervar) == "struct Long"); REQUIRE(NAMEOF_VAR_TYPE(othervar.ll) == "struct Long::LL"); REQUIRE(NAMEOF_VAR_TYPE(othervar.ll.field) == "int"); REQUIRE(NAMEOF_VAR_TYPE(Color::RED) == "enum Color"); REQUIRE(NAMEOF_VAR_TYPE(std::declval>()) == "class SomeClass const &&"); #elif defined(__GNUC__) REQUIRE(type_name == "SomeStruct"); REQUIRE(NAMEOF_VAR_TYPE(ptr_s) == "SomeStruct*"); REQUIRE(NAMEOF_VAR_TYPE(ref_s) == "SomeStruct&"); REQUIRE(NAMEOF_VAR_TYPE(ptr_c) == "const volatile SomeClass*"); REQUIRE(NAMEOF_VAR_TYPE(othervar) == "Long"); REQUIRE(NAMEOF_VAR_TYPE(othervar.ll) == "Long::LL"); REQUIRE(NAMEOF_VAR_TYPE(othervar.ll.field) == "int"); REQUIRE(NAMEOF_VAR_TYPE(Color::RED) == "Color"); REQUIRE(NAMEOF_VAR_TYPE(std::declval>()) == "const SomeClass&&"); #endif } TEST_CASE("NAMEOF_TYPE") { constexpr auto type_name = NAMEOF_TYPE(decltype(struct_var)); #if defined(__clang__) REQUIRE(type_name == "SomeStruct"); REQUIRE(NAMEOF_TYPE(decltype(ptr_s)) == "SomeStruct *"); REQUIRE(NAMEOF_TYPE(decltype(ref_s)) == "SomeStruct &"); REQUIRE(NAMEOF_TYPE(SomeStruct) == "SomeStruct"); REQUIRE(NAMEOF_TYPE(SomeStruct *) == "SomeStruct *"); REQUIRE(NAMEOF_TYPE(SomeStruct &) == "SomeStruct &"); REQUIRE(NAMEOF_TYPE(const SomeStruct volatile *) == "const volatile SomeStruct *"); REQUIRE(NAMEOF_TYPE(SomeClass) == "SomeClass"); REQUIRE(NAMEOF_TYPE(const SomeClass volatile *) == "const volatile SomeClass *"); REQUIRE(NAMEOF_TYPE(decltype(othervar)) == "Long"); REQUIRE(NAMEOF_TYPE(Long) == "Long"); REQUIRE(NAMEOF_TYPE(Long::LL) == "Long::LL"); REQUIRE(NAMEOF_TYPE(Color) == "Color"); #elif defined(_MSC_VER) REQUIRE(type_name == "struct SomeStruct"); REQUIRE(NAMEOF_TYPE(decltype(ptr_s)) == "struct SomeStruct *"); REQUIRE(NAMEOF_TYPE(decltype(ref_s)) == "struct SomeStruct &"); REQUIRE(NAMEOF_TYPE(SomeStruct) == "struct SomeStruct"); REQUIRE(NAMEOF_TYPE(SomeStruct *) == "struct SomeStruct *"); REQUIRE(NAMEOF_TYPE(SomeStruct &) == "struct SomeStruct &"); REQUIRE(NAMEOF_TYPE(const SomeStruct volatile *) == "struct SomeStruct const volatile *"); REQUIRE(NAMEOF_TYPE(SomeClass) == "class SomeClass"); REQUIRE(NAMEOF_TYPE(const SomeClass volatile *) == "class SomeClass const volatile *"); REQUIRE(NAMEOF_TYPE(decltype(othervar)) == "struct Long"); REQUIRE(NAMEOF_TYPE(Long) == "struct Long"); REQUIRE(NAMEOF_TYPE(Long::LL) == "struct Long::LL"); REQUIRE(NAMEOF_TYPE(Color) == "enum Color"); #elif defined(__GNUC__) REQUIRE(type_name == "SomeStruct"); REQUIRE(NAMEOF_TYPE(decltype(ptr_s)) == "SomeStruct*"); REQUIRE(NAMEOF_TYPE(decltype(ref_s)) == "SomeStruct&"); REQUIRE(NAMEOF_TYPE(SomeStruct) == "SomeStruct"); REQUIRE(NAMEOF_TYPE(SomeStruct *) == "SomeStruct*"); REQUIRE(NAMEOF_TYPE(SomeStruct &) == "SomeStruct&"); REQUIRE(NAMEOF_TYPE(const SomeStruct volatile *) == "const volatile SomeStruct*"); REQUIRE(NAMEOF_TYPE(SomeClass) == "SomeClass"); REQUIRE(NAMEOF_TYPE(const SomeClass volatile *) == "const volatile SomeClass*"); REQUIRE(NAMEOF_TYPE(decltype(othervar)) == "Long"); REQUIRE(NAMEOF_TYPE(Long) == "Long"); REQUIRE(NAMEOF_TYPE(Long::LL) == "Long::LL"); REQUIRE(NAMEOF_TYPE(Color) == "Color"); #endif } TEST_CASE("nameof::nameof_type") { constexpr auto type_name = nameof::nameof_type(); #if defined(__clang__) REQUIRE(type_name == "SomeStruct"); REQUIRE(nameof::nameof_type() == "SomeStruct *"); REQUIRE(nameof::nameof_type() == "SomeStruct &"); REQUIRE(nameof::nameof_type() == "SomeStruct"); REQUIRE(nameof::nameof_type() == "SomeStruct *"); REQUIRE(nameof::nameof_type() == "SomeStruct &"); REQUIRE(nameof::nameof_type() == "const volatile SomeStruct *"); REQUIRE(nameof::nameof_type>() == "SomeClass"); REQUIRE(nameof::nameof_type volatile *>() == "const volatile SomeClass *"); REQUIRE(nameof::nameof_type() == "Long"); REQUIRE(nameof::nameof_type() == "Long"); REQUIRE(nameof::nameof_type() == "Long::LL"); REQUIRE(nameof::nameof_type() == "Color"); #elif defined(_MSC_VER) REQUIRE(type_name == "struct SomeStruct"); REQUIRE(nameof::nameof_type() == "struct SomeStruct *"); REQUIRE(nameof::nameof_type() == "struct SomeStruct &"); REQUIRE(nameof::nameof_type() == "struct SomeStruct"); REQUIRE(nameof::nameof_type() == "struct SomeStruct *"); REQUIRE(nameof::nameof_type() == "struct SomeStruct &"); REQUIRE(nameof::nameof_type() == "struct SomeStruct const volatile *"); REQUIRE(nameof::nameof_type>() == "class SomeClass"); REQUIRE(nameof::nameof_type volatile *>() == "class SomeClass const volatile *"); REQUIRE(nameof::nameof_type() == "struct Long"); REQUIRE(nameof::nameof_type() == "struct Long"); REQUIRE(nameof::nameof_type() == "struct Long::LL"); REQUIRE(nameof::nameof_type() == "enum Color"); #elif defined(__GNUC__) REQUIRE(type_name == "SomeStruct"); REQUIRE(nameof::nameof_type() == "SomeStruct*"); REQUIRE(nameof::nameof_type() == "SomeStruct&"); REQUIRE(nameof::nameof_type() == "SomeStruct"); REQUIRE(nameof::nameof_type() == "SomeStruct*"); REQUIRE(nameof::nameof_type() == "SomeStruct&"); REQUIRE(nameof::nameof_type() == "const volatile SomeStruct*"); REQUIRE(nameof::nameof_type>() == "SomeClass"); REQUIRE(nameof::nameof_type volatile *>() == "const volatile SomeClass*"); REQUIRE(nameof::nameof_type() == "Long"); REQUIRE(nameof::nameof_type() == "Long"); REQUIRE(nameof::nameof_type() == "Long::LL"); REQUIRE(nameof::nameof_type() == "Color"); #endif }