nameof_module/test/test.cpp

205 lines
7.1 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 <cstring>
#include <string>
2018-04-29 20:37:25 +00:00
#include <stdexcept>
2018-03-26 12:02:43 +00:00
struct SomeStruct {
2018-04-10 18:09:44 +00:00
int somefield;
void SomeMethod1(const int i) {
somefield = i;
2018-04-29 20:37:25 +00:00
throw std::exception{};
2018-04-10 18:09:44 +00:00
}
int SomeMethod2() const {
2018-04-29 20:37:25 +00:00
throw std::exception{};
2018-04-10 18:09:44 +00:00
return somefield;
2018-03-26 12:02:43 +00:00
}
};
2018-04-29 20:37:25 +00:00
void SomeMethod3() { throw std::exception{}; }
2018-03-26 12:02:43 +00:00
struct Long {
struct LL {
2018-04-10 18:09:44 +00:00
int field;
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;
int intvar;
SomeStruct* ptrvar;
SomeStruct** ptrptrvar;
2018-04-02 11:25:43 +00:00
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
constexpr auto cx1 = NAMEOF((&somevar)->somefield);
REQUIRE(std::strcmp(cx1, "somefield") == 0);
// type
constexpr auto cx2 = NAMEOF(std::string);
REQUIRE(std::strcmp(cx2, "string") == 0);
// function
constexpr auto cx3 = NAMEOF(&SomeStruct::SomeMethod2);
REQUIRE(std::strcmp(cx3, "SomeMethod2") == 0);
// enum
constexpr auto cx4 = NAMEOF(Color::RED);
REQUIRE(std::strcmp(cx4, "RED") == 0);
2018-04-14 19:12:32 +00:00
// macros
constexpr auto cx5 = NAMEOF(__cplusplus);
REQUIRE(std::strcmp(cx5, "__cplusplus") == 0);
2018-04-02 11:25:43 +00:00
}
SECTION("NAMEOF_FULL") {
2018-04-14 17:51:07 +00:00
// variable
constexpr auto cx1 = NAMEOF_FULL((&somevar)->somefield);
REQUIRE(std::strcmp(cx1, "(&somevar)->somefield") == 0);
// type
constexpr auto cx2 = NAMEOF_FULL(std::string);
REQUIRE(std::strcmp(cx2, "std::string") == 0);
// function
constexpr auto cx3 = NAMEOF_FULL(&SomeStruct::SomeMethod2);
REQUIRE(std::strcmp(cx3, "&SomeStruct::SomeMethod2") == 0);
// enum
constexpr auto cx4 = NAMEOF_FULL(Color::RED);
REQUIRE(std::strcmp(cx4, "Color::RED") == 0);
2018-04-14 19:12:32 +00:00
// macros
constexpr auto cx5 = NAMEOF_FULL(__cplusplus);
REQUIRE(std::strcmp(cx5, "__cplusplus") == 0);
2018-04-02 11:25:43 +00:00
}
}
2018-03-26 12:02:43 +00:00
TEST_CASE("NAMEOF") {
2018-04-14 17:51:07 +00:00
SECTION("variable") {
2018-04-10 18:09:44 +00:00
REQUIRE(std::strcmp(NAMEOF(somevar), "somevar") == 0);
REQUIRE(std::strcmp(NAMEOF(&somevar), "somevar") == 0);
2018-04-10 19:32:51 +00:00
REQUIRE(std::strcmp(NAMEOF(::somevar), "somevar") == 0);
2018-04-10 18:09:44 +00:00
REQUIRE(std::strcmp(NAMEOF(somevar.somefield), "somefield") == 0);
REQUIRE(std::strcmp(NAMEOF((&somevar)->somefield), "somefield") == 0);
REQUIRE(std::strcmp(NAMEOF(othervar.ll.field), "field") == 0);
REQUIRE(std::strcmp(NAMEOF(ptrvar), "ptrvar") == 0);
REQUIRE(std::strcmp(NAMEOF(*ptrvar), "ptrvar") == 0);
REQUIRE(std::strcmp(NAMEOF(ptrptrvar), "ptrptrvar") == 0);
REQUIRE(std::strcmp(NAMEOF(*ptrptrvar), "ptrptrvar") == 0);
REQUIRE(std::strcmp(NAMEOF(**ptrptrvar), "ptrptrvar") == 0);
REQUIRE(std::strcmp(NAMEOF(+intvar), "intvar") == 0);
REQUIRE(std::strcmp(NAMEOF(-intvar), "intvar") == 0);
REQUIRE(std::strcmp(NAMEOF(~intvar), "intvar") == 0);
REQUIRE(std::strcmp(NAMEOF(!intvar), "intvar") == 0);
2018-03-26 12:02:43 +00:00
}
2018-04-14 17:51:07 +00:00
SECTION("type") {
2018-04-04 12:19:02 +00:00
REQUIRE(std::strcmp(NAMEOF(int[]), "int[]") == 0);
REQUIRE(std::strcmp(NAMEOF(int), "int") == 0);
REQUIRE(std::strcmp(NAMEOF(const volatile int[]), "const volatile int[]") == 0);
REQUIRE(std::strcmp(NAMEOF(std::string), "string") == 0);
REQUIRE(std::strcmp(NAMEOF(SomeStruct), "SomeStruct") == 0);
REQUIRE(std::strcmp(NAMEOF(Long::LL), "LL") == 0);
2018-04-10 18:09:44 +00:00
REQUIRE(std::strcmp(NAMEOF_FULL(Color), "Color") == 0);
2018-04-04 12:19:02 +00:00
}
2018-04-14 17:51:07 +00:00
SECTION("function") {
2018-03-30 19:29:53 +00:00
REQUIRE(std::strcmp(NAMEOF(&SomeStruct::SomeMethod2), "SomeMethod2") == 0);
REQUIRE(std::strcmp(NAMEOF(SomeMethod3), "SomeMethod3") == 0);
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-04-02 11:25:43 +00:00
REQUIRE(std::strcmp(NAMEOF(Color::RED), "RED") == 0);
REQUIRE(std::strcmp(NAMEOF(Color::BLUE), "BLUE") == 0);
}
2018-04-14 19:12:32 +00:00
SECTION("macros") {
REQUIRE(std::strcmp(NAMEOF(__cplusplus), "__cplusplus") == 0);
REQUIRE(std::strcmp(NAMEOF(__LINE__), "__LINE__") == 0);
REQUIRE(std::strcmp(NAMEOF(__FILE__), "__FILE__") == 0);
}
2018-03-26 12:02:43 +00:00
}
TEST_CASE("NAMEOF_FULL") {
2018-04-14 17:51:07 +00:00
SECTION("variable") {
2018-04-10 18:09:44 +00:00
REQUIRE(std::strcmp(NAMEOF_FULL(somevar), "somevar") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(&somevar), "&somevar") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(somevar.somefield), "somevar.somefield") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL((&somevar)->somefield), "(&somevar)->somefield") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(::somevar), "::somevar") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(othervar.ll.field), "othervar.ll.field") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(ptrvar), "ptrvar") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(*ptrvar), "*ptrvar") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(ptrptrvar), "ptrptrvar") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(*ptrptrvar), "*ptrptrvar") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(**ptrptrvar), "**ptrptrvar") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(+intvar), "+intvar") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(-intvar), "-intvar") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(~intvar), "~intvar") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(!intvar), "!intvar") == 0);
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-04-04 12:19:02 +00:00
REQUIRE(std::strcmp(NAMEOF_FULL(int[]), "int[]") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(int), "int") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(const volatile int[]), "const volatile int[]") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(std::string), "std::string") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(SomeStruct), "SomeStruct") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(Long::LL), "Long::LL") == 0);
2018-04-10 18:09:44 +00:00
REQUIRE(std::strcmp(NAMEOF_FULL(Color), "Color") == 0);
2018-04-04 12:19:02 +00:00
}
2018-04-14 17:51:07 +00:00
SECTION("function") {
2018-03-30 19:29:53 +00:00
REQUIRE(std::strcmp(NAMEOF_FULL(&SomeStruct::SomeMethod2), "&SomeStruct::SomeMethod2") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(SomeMethod3), "SomeMethod3") == 0);
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-04-02 11:25:43 +00:00
REQUIRE(std::strcmp(NAMEOF_FULL(Color::RED), "Color::RED") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(Color::BLUE), "Color::BLUE") == 0);
}
2018-04-14 19:12:32 +00:00
SECTION("macros") {
REQUIRE(std::strcmp(NAMEOF_FULL(__cplusplus), "__cplusplus") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(__LINE__), "__LINE__") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(__FILE__), "__FILE__") == 0);
}
2018-03-26 12:02:43 +00:00
}