nameof_module/test/test.cpp

306 lines
9.8 KiB
C++
Raw Normal View History

2018-04-28 13:04:30 +00:00
// nameof test
2018-03-26 12:02:43 +00:00
//
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
2018-04-01 20:48:03 +00:00
// Copyright (c) 2018 Daniil Goncharov <neargye@gmail.com>.
2018-03-26 12:02:43 +00:00
//
// 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 <catch.hpp>
2018-04-10 18:09:44 +00:00
2018-03-26 12:02:43 +00:00
#include <nameof.hpp>
2018-04-10 18:09:44 +00:00
2018-03-26 12:02:43 +00:00
#include <string>
2018-04-29 20:37:25 +00:00
#include <stdexcept>
2018-03-26 12:02:43 +00:00
struct SomeStruct {
2018-07-13 19:02:03 +00:00
int somefield = 0;
2018-04-10 18:09:44 +00:00
2018-08-02 14:51:44 +00:00
void SomeMethod1(const int i) { somefield = i; }
2018-04-10 18:09:44 +00:00
2018-08-02 14:51:44 +00:00
int SomeMethod2() const { return somefield; }
2018-03-26 12:02:43 +00:00
};
2018-08-02 14:51:44 +00:00
void SomeMethod3() {
std::cout << NAMEOF(SomeMethod3) << " no called!" << std::endl;
}
template <typename T>
T SomeMethod4() {
return T{};
}
template <typename T>
class SomeClass {
public:
2018-08-03 12:19:51 +00:00
void SomeMethod5() const {
2018-08-06 11:01:40 +00:00
std::cout << nameof::NameofType<T>() << std::endl;
2018-08-03 12:19:51 +00:00
}
2018-08-02 14:51:44 +00:00
template <typename C>
2018-08-03 12:19:51 +00:00
C SomeMethod6() const {
C t{};
std::cout << NAMEOF_TYPE(t) << std::endl;
return t;
}
2018-08-02 14:51:44 +00:00
};
2018-03-26 12:02:43 +00:00
struct Long {
struct LL {
2018-07-13 19:02:03 +00:00
int field = 0;
2018-03-26 12:02:43 +00:00
};
2018-04-10 18:09:44 +00:00
LL ll;
2018-03-26 12:02:43 +00:00
};
2018-03-27 14:00:06 +00:00
enum class Color { RED, GREEN, BLUE };
2018-05-03 19:46:41 +00:00
SomeStruct somevar;
Long othervar;
2018-08-03 12:19:51 +00:00
SomeStruct& refvar = somevar;
2018-07-13 19:02:03 +00:00
SomeStruct* ptrvar = &somevar;
2018-04-02 11:25:43 +00:00
2018-08-06 12:56:46 +00:00
#if (__cplusplus >= 201402L || (defined(_MSVC_LANG) && _MSC_VER >= 1910 && _MSVC_LANG >= 201402L))
2018-08-03 12:19:51 +00:00
// Compile-time supported by C++14.
2018-05-03 19:46:41 +00:00
TEST_CASE("constexpr") {
2018-04-02 11:25:43 +00:00
SECTION("NAMEOF") {
2018-04-14 17:51:07 +00:00
// variable
2018-08-02 14:51:44 +00:00
constexpr auto cx1 = NAMEOF(somevar);
static_assert(cx1 == "somevar", "");
// member
constexpr auto cx2 = NAMEOF((&somevar)->somefield);
static_assert(cx2 == "somefield", "");
2018-04-14 17:51:07 +00:00
// function
constexpr auto cx3 = NAMEOF(&SomeStruct::SomeMethod2);
2018-08-02 14:51:44 +00:00
static_assert(cx3 == "SomeMethod2", "");
2018-04-14 17:51:07 +00:00
// enum
constexpr auto cx4 = NAMEOF(Color::RED);
2018-08-02 14:51:44 +00:00
static_assert(cx4 == "RED", "");
2018-04-02 11:25:43 +00:00
}
2018-08-02 14:51:44 +00:00
SECTION("NAMEOF_RAW") {
2018-04-14 17:51:07 +00:00
// variable
2018-08-02 14:51:44 +00:00
constexpr auto cx1 = NAMEOF_RAW(somevar);
static_assert(cx1 == "somevar", "");
// member
constexpr auto cx2 = NAMEOF_RAW((&somevar)->somefield);
static_assert(cx2 == "(&somevar)->somefield", "");
2018-04-14 17:51:07 +00:00
// type
2018-08-02 14:51:44 +00:00
constexpr auto cx3 = NAMEOF_RAW(std::string);
static_assert(cx3 == "std::string", "");
2018-04-14 17:51:07 +00:00
// function
2018-08-02 14:51:44 +00:00
constexpr auto cx4 = NAMEOF_RAW(&SomeStruct::SomeMethod2);
static_assert(cx4 == "&SomeStruct::SomeMethod2", "");
2018-04-14 17:51:07 +00:00
// enum
2018-08-02 14:51:44 +00:00
constexpr auto cx5 = NAMEOF_RAW(Color::RED);
static_assert(cx5 == "Color::RED", "");
2018-04-14 19:12:32 +00:00
// macros
2018-08-02 14:51:44 +00:00
constexpr auto cx6 = NAMEOF_RAW(__cplusplus);
static_assert(cx6 == "__cplusplus", "");
2018-07-13 15:18:52 +00:00
}
2018-08-07 16:14:24 +00:00
2018-08-06 15:24:50 +00:00
#if 0
2018-08-03 12:19:51 +00:00
SECTION("NAMEOF_TYPE") {
SomeClass<int> a;
constexpr auto cx1 = NAMEOF_TYPE(a);
static_assert(cx1 == "SomeClass", "");
2018-08-06 11:01:40 +00:00
constexpr auto cx2 = nameof::NameofType<SomeClass<int>>();
2018-08-03 12:19:51 +00:00
static_assert(cx2 == "SomeClass", "");
2018-08-06 11:01:40 +00:00
constexpr auto cx3 = nameof::NameofType<decltype(a)>();
2018-08-03 12:19:51 +00:00
static_assert(cx3 == "SomeClass", "");
}
SECTION("NAMEOF_TYPE_RAW") {
SomeClass<int> a;
constexpr auto cx1 = NAMEOF_TYPE_RAW(a);
2018-08-06 11:01:40 +00:00
constexpr auto cx2 = nameof::NameofTypeRaw<SomeClass<int>>();
constexpr auto cx3 = nameof::NameofTypeRaw<decltype(a)>();
2018-08-03 12:19:51 +00:00
#if defined(_MSC_VER)
static_assert(cx1 == "class SomeClass<int>", "");
static_assert(cx2 == "class SomeClass<int>", "");
static_assert(cx3 == "class SomeClass<int>", "");
2018-08-06 12:42:46 +00:00
#elif defined(__GNUC__) || defined(__clang__)
2018-08-03 12:19:51 +00:00
static_assert(cx1 == "SomeClass<int>", "");
static_assert(cx2 == "SomeClass<int>", "");
static_assert(cx3 == "SomeClass<int>", "");
#endif
}
2018-08-06 15:24:50 +00:00
#endif
2018-04-02 11:25:43 +00:00
}
2018-08-03 12:19:51 +00:00
#endif
2018-04-02 11:25:43 +00:00
2018-07-13 15:18:52 +00:00
TEST_CASE("simple name") {
2018-04-14 17:51:07 +00:00
SECTION("variable") {
2018-08-02 14:51:44 +00:00
REQUIRE(NAMEOF(somevar) == "somevar");
REQUIRE(NAMEOF(::somevar) == "somevar");
REQUIRE(NAMEOF(ptrvar) == "ptrvar");
2018-03-26 12:02:43 +00:00
}
2018-08-02 14:51:44 +00:00
SECTION("member") {
REQUIRE(NAMEOF(somevar.somefield) == "somefield");
REQUIRE(NAMEOF((&somevar)->somefield) == "somefield");
REQUIRE(NAMEOF(othervar.ll.field) == "field");
2018-04-04 12:19:02 +00:00
}
2018-04-14 17:51:07 +00:00
SECTION("function") {
2018-08-02 14:51:44 +00:00
REQUIRE(NAMEOF(&SomeStruct::SomeMethod1) == "SomeMethod1");
REQUIRE(NAMEOF(&SomeStruct::SomeMethod2) == "SomeMethod2");
REQUIRE(NAMEOF(SomeMethod3) == "SomeMethod3");
REQUIRE(NAMEOF(SomeMethod4<int>) == "SomeMethod4");
REQUIRE(NAMEOF(&SomeClass<int>::SomeMethod5) == "SomeMethod5");
REQUIRE(NAMEOF(&SomeClass<int>::SomeMethod6<long int>) == "SomeMethod6");
2018-03-26 12:02:43 +00:00
}
2018-04-02 11:25:43 +00:00
2018-04-14 17:51:07 +00:00
SECTION("enum") {
2018-08-02 14:51:44 +00:00
REQUIRE(NAMEOF(Color::RED) == "RED");
REQUIRE(NAMEOF(Color::BLUE) == "BLUE");
2018-04-14 19:12:32 +00:00
}
2018-03-26 12:02:43 +00:00
}
2018-08-03 12:19:51 +00:00
2018-08-02 14:51:44 +00:00
TEST_CASE("raw name") {
2018-04-14 17:51:07 +00:00
SECTION("variable") {
2018-08-02 14:51:44 +00:00
REQUIRE(NAMEOF_RAW(somevar) == "somevar");
REQUIRE(NAMEOF_RAW(&somevar) == "&somevar");
REQUIRE(NAMEOF_RAW(::somevar) == "::somevar");
REQUIRE(NAMEOF_RAW(ptrvar) == "ptrvar");
REQUIRE(NAMEOF_RAW(*ptrvar) == "*ptrvar");
REQUIRE(NAMEOF_RAW(+somevar.somefield) == "+somevar.somefield");
REQUIRE(NAMEOF_RAW(-somevar.somefield) == "-somevar.somefield");
REQUIRE(NAMEOF_RAW(~somevar.somefield) == "~somevar.somefield");
REQUIRE(NAMEOF_RAW(!somevar.somefield) == "!somevar.somefield");
}
2018-03-26 12:02:43 +00:00
2018-08-02 14:51:44 +00:00
SECTION("member") {
REQUIRE(NAMEOF_RAW(somevar.somefield) == "somevar.somefield");
REQUIRE(NAMEOF_RAW((&somevar)->somefield) == "(&somevar)->somefield");
REQUIRE(NAMEOF_RAW(othervar.ll.field) == "othervar.ll.field");
2018-03-26 12:02:43 +00:00
}
2018-04-04 12:19:02 +00:00
2018-04-14 17:51:07 +00:00
SECTION("type") {
2018-08-02 14:51:44 +00:00
REQUIRE(NAMEOF_RAW(int[]) == "int[]");
REQUIRE(NAMEOF_RAW(int) == "int");
REQUIRE(NAMEOF_RAW(const volatile int[]) == "const volatile int[]");
REQUIRE(NAMEOF_RAW(std::string) == "std::string");
REQUIRE(NAMEOF_RAW(SomeStruct) == "SomeStruct");
REQUIRE(NAMEOF_RAW(Long::LL) == "Long::LL");
REQUIRE(NAMEOF_RAW(Color) == "Color");
2018-04-04 12:19:02 +00:00
}
2018-04-14 17:51:07 +00:00
SECTION("function") {
2018-08-02 14:51:44 +00:00
REQUIRE(NAMEOF_RAW(&SomeStruct::SomeMethod1) == "&SomeStruct::SomeMethod1");
REQUIRE(NAMEOF_RAW(&SomeStruct::SomeMethod2) == "&SomeStruct::SomeMethod2");
REQUIRE(NAMEOF_RAW(SomeMethod3) == "SomeMethod3");
2018-03-26 12:02:43 +00:00
}
2018-04-02 11:25:43 +00:00
2018-04-14 17:51:07 +00:00
SECTION("enum") {
2018-08-02 14:51:44 +00:00
REQUIRE(NAMEOF_RAW(Color::RED) == "Color::RED");
REQUIRE(NAMEOF_RAW(Color::BLUE) == "Color::BLUE");
2018-04-02 11:25:43 +00:00
}
2018-04-14 19:12:32 +00:00
SECTION("macros") {
2018-08-02 14:51:44 +00:00
REQUIRE(NAMEOF_RAW(__cplusplus) == "__cplusplus");
REQUIRE(NAMEOF_RAW(__LINE__) == "__LINE__");
REQUIRE(NAMEOF_RAW(__FILE__) == "__FILE__");
2018-04-14 19:12:32 +00:00
}
2018-03-26 12:02:43 +00:00
}
2018-05-03 19:50:08 +00:00
2018-08-03 12:19:51 +00:00
TEST_CASE("type name") {
REQUIRE(NAMEOF_TYPE(somevar) == "SomeStruct");
REQUIRE(NAMEOF_TYPE(ptrvar) == "SomeStruct");
REQUIRE(NAMEOF_TYPE(refvar) == "SomeStruct");
REQUIRE(NAMEOF_TYPE(othervar) == "Long");
REQUIRE(NAMEOF_TYPE(othervar.ll) == "LL");
REQUIRE(NAMEOF_TYPE(othervar.ll.field) == "int");
REQUIRE(NAMEOF_TYPE(Color::RED) == "Color");
REQUIRE(NAMEOF_TYPE(std::string{}) == "basic_string");
REQUIRE(NAMEOF_TYPE(std::declval<const SomeClass<int>>()) == "SomeClass");
}
TEST_CASE("type raw name") {
#if defined(_MSC_VER)
REQUIRE(NAMEOF_TYPE_RAW(somevar) == "struct SomeStruct");
REQUIRE(NAMEOF_TYPE_RAW(ptrvar) == "structSomeStruct*");
REQUIRE(NAMEOF_TYPE_RAW(refvar) == "structSomeStruct&");
REQUIRE(NAMEOF_TYPE_RAW(othervar) == "struct Long");
REQUIRE(NAMEOF_TYPE_RAW(othervar.ll) == "struct Long::LL");
REQUIRE(NAMEOF_TYPE_RAW(othervar.ll.field) == "int");
REQUIRE(NAMEOF_TYPE_RAW(Color::RED) == "enum Color");
REQUIRE(NAMEOF_TYPE_RAW(std::declval<const SomeClass<int>>()) == "const classSomeClass<int>&&");
2018-08-06 11:01:40 +00:00
#elif defined(__clang__)
REQUIRE(NAMEOF_TYPE_RAW(somevar) == "SomeStruct");
REQUIRE(NAMEOF_TYPE_RAW(ptrvar) == "SomeStruct *");
REQUIRE(NAMEOF_TYPE_RAW(refvar) == "SomeStruct &");
REQUIRE(NAMEOF_TYPE_RAW(othervar) == "Long");
REQUIRE(NAMEOF_TYPE_RAW(othervar.ll) == "Long::LL");
REQUIRE(NAMEOF_TYPE_RAW(othervar.ll.field) == "int");
REQUIRE(NAMEOF_TYPE_RAW(Color::RED) == "Color");
2018-08-06 12:42:46 +00:00
REQUIRE(NAMEOF_TYPE_RAW(std::declval<const SomeClass<int>>()) == "const SomeClass<int> &&");
2018-08-06 11:01:40 +00:00
#elif defined(__GNUC__)
2018-08-03 12:19:51 +00:00
REQUIRE(NAMEOF_TYPE_RAW(somevar) == "SomeStruct");
REQUIRE(NAMEOF_TYPE_RAW(ptrvar) == "SomeStruct*");
REQUIRE(NAMEOF_TYPE_RAW(refvar) == "SomeStruct&");
REQUIRE(NAMEOF_TYPE_RAW(othervar) == "Long");
REQUIRE(NAMEOF_TYPE_RAW(othervar.ll) == "Long::LL");
REQUIRE(NAMEOF_TYPE_RAW(othervar.ll.field) == "int");
REQUIRE(NAMEOF_TYPE_RAW(Color::RED) == "Color");
2018-08-06 12:42:46 +00:00
REQUIRE(NAMEOF_TYPE_RAW(std::declval<const SomeClass<int>>()) == "const SomeClass<int>&&");
2018-08-03 12:19:51 +00:00
#endif
}
2018-05-03 20:18:02 +00:00
TEST_CASE("Spaces and Tabs ignored") {
SECTION("Spaces") {
2018-08-02 14:51:44 +00:00
REQUIRE(NAMEOF( somevar ) == "somevar");
REQUIRE(NAMEOF_RAW( somevar ) == "somevar");
2018-08-06 11:01:40 +00:00
REQUIRE(NAMEOF_TYPE( somevar ) == "SomeStruct");
#if defined(_MSC_VER)
REQUIRE(NAMEOF_TYPE_RAW( somevar ) == "struct SomeStruct");
2018-08-06 12:42:46 +00:00
#elif defined(__GNUC__) || defined(__clang__)
2018-08-06 11:01:40 +00:00
REQUIRE(NAMEOF_TYPE_RAW( somevar ) == "SomeStruct");
#endif
}
2018-07-13 15:18:52 +00:00
SECTION("Tabs") {
2018-08-02 14:51:44 +00:00
REQUIRE(NAMEOF( somevar ) == "somevar");
REQUIRE(NAMEOF_RAW( somevar ) == "somevar");
2018-08-06 11:01:40 +00:00
REQUIRE(NAMEOF_TYPE( somevar ) == "SomeStruct");
#if defined(_MSC_VER)
REQUIRE(NAMEOF_TYPE_RAW( somevar ) == "struct SomeStruct");
2018-08-06 12:42:46 +00:00
#elif defined(__GNUC__) || defined(__clang__)
2018-08-06 11:01:40 +00:00
REQUIRE(NAMEOF_TYPE_RAW( somevar ) == "SomeStruct");
#endif
2018-07-13 15:18:52 +00:00
}
}