This commit is contained in:
Terik23 2018-03-26 17:02:43 +05:00
parent eb699c8a15
commit af49743a40
7 changed files with 13048 additions and 10 deletions

8
CMakeLists.txt Normal file
View file

@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.6.4)
project(nameof)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_subdirectory(example)
add_subdirectory(test)

View file

@ -1,13 +1,7 @@
cmake_minimum_required(VERSION 3.6.4) include_directories(${CMAKE_SOURCE_DIR}/src)
project(nameof_example)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include_directories(../src)
set(SRC set(SRC
example.cpp example.cpp
../src/nameof.hpp) ${CMAKE_SOURCE_DIR}/src/nameof.hpp)
add_executable(${PROJECT_NAME} ${SRC}) add_executable(${PROJECT_NAME}_example ${SRC})

View file

@ -102,7 +102,6 @@ void TestCase2() {
} catch (const std::exception& e) { } catch (const std::exception& e) {
std::cout << e.what() << std::endl; // y should not be zero! std::cout << e.what() << std::endl; // y should not be zero!
} }
} }
int main() { int main() {

23
test/3rdparty/Catch2/LICENSE vendored Normal file
View file

@ -0,0 +1,23 @@
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
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, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

12852
test/3rdparty/Catch2/catch.hpp vendored Normal file

File diff suppressed because it is too large Load diff

9
test/CMakeLists.txt Normal file
View file

@ -0,0 +1,9 @@
include_directories(${CMAKE_SOURCE_DIR}/src)
include_directories(3rdparty/Catch2)
set(SRC
test.cpp
3rdparty/Catch2/catch.hpp
${CMAKE_SOURCE_DIR}/src/nameof.hpp)
add_executable(${PROJECT_NAME}_test ${SRC})

153
test/test.cpp Normal file
View file

@ -0,0 +1,153 @@
// nameof() c++11 test
//
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// Copyright (c) 2018 Terik23 <neargye@gmail.com>.
//
// 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>
#include <nameof.hpp>
#include <cstring>
#include <string>
#include <iostream>
struct SomeStruct {
int SomeField;
void SomeMethod1() { std::cout << "No called!" << std::endl; }
int SomeMethod2() {
std::cout << "No called!" << std::endl;
return 1;
}
};
void SomeMethod3() { std::cout << "No called!" << std::endl; }
struct Long {
struct LL {
int LLLField;
};
LL LLField;
};
int someVar = 0;
TEST_CASE("NAMEOF") {
SomeStruct someVar{1};
Long otherVar{2};
int intValue{3};
SomeStruct* ptrVar = &someVar;
SomeStruct** ptrptrVar = &ptrVar;
SECTION("constexpr") {
constexpr auto constexpr_work_fine = NAMEOF(intValue);
REQUIRE(std::strcmp(constexpr_work_fine, "intValue") == 0);
}
SECTION("NAMEOF") {
REQUIRE(std::strcmp(NAMEOF(someVar), "someVar") == 0);
REQUIRE(std::strcmp(NAMEOF(someVar.SomeField), "SomeField") == 0);
REQUIRE(std::strcmp(NAMEOF((&someVar)->SomeField), "SomeField") == 0);
REQUIRE(std::strcmp(NAMEOF(::someVar), "someVar") == 0);
REQUIRE(std::strcmp(NAMEOF(otherVar.LLField.LLLField), "LLLField") == 0);
REQUIRE(std::strcmp(NAMEOF(&someVar), "someVar") == 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(+intValue), "intValue") == 0);
REQUIRE(std::strcmp(NAMEOF(-intValue), "intValue") == 0);
REQUIRE(std::strcmp(NAMEOF(~intValue), "intValue") == 0);
REQUIRE(std::strcmp(NAMEOF(!intValue), "intValue") == 0);
REQUIRE(std::strcmp(NAMEOF(someVar.SomeMethod1()), "SomeMethod1()") == 0);
REQUIRE(std::strcmp(NAMEOF(&SomeStruct::SomeMethod2), "SomeMethod2") == 0);
REQUIRE(std::strcmp(NAMEOF(SomeMethod3), "SomeMethod3") == 0);
}
SECTION("NAMEOF_VARIABLE") {
REQUIRE(std::strcmp(NAMEOF_VARIABLE(someVar.SomeField), "SomeField") == 0);
REQUIRE(std::strcmp(NAMEOF_VARIABLE((&someVar)->SomeField), "SomeField") == 0);
REQUIRE(std::strcmp(NAMEOF_VARIABLE(::someVar), "someVar") == 0);
}
SECTION("NAMEOF_FUNCTION") {
REQUIRE(std::strcmp(NAMEOF_FUNCTION(someVar.SomeMethod1()), "SomeMethod1()") == 0);
REQUIRE(std::strcmp(NAMEOF_FUNCTION(&SomeStruct::SomeMethod2), "SomeMethod2") == 0);
REQUIRE(std::strcmp(NAMEOF_FUNCTION(SomeMethod3), "SomeMethod3") == 0);
}
}
TEST_CASE("NAMEOF_TYPE") {
REQUIRE(std::strcmp(NAMEOF_TYPE(int[]), "int[]") == 0);
REQUIRE(std::strcmp(NAMEOF_TYPE(int), "int") == 0);
REQUIRE(std::strcmp(NAMEOF_TYPE(std::string), "string") == 0);
REQUIRE(std::strcmp(NAMEOF_TYPE(SomeStruct), "SomeStruct") == 0);
REQUIRE(std::strcmp(NAMEOF_TYPE(Long::LL), "LL") == 0);
}
TEST_CASE("NAMEOF_FULL") {
SomeStruct someVar{1};
Long otherVar{2};
int intValue{3};
SomeStruct* ptrVar = &someVar;
SomeStruct** ptrptrVar = &ptrVar;
SECTION("NAMEOF_FULL") {
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.LLField.LLLField), "otherVar.LLField.LLLField") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(&someVar), "&someVar") == 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(+intValue), "+intValue") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(-intValue), "-intValue") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(~intValue), "~intValue") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(!intValue), "!intValue") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(someVar.SomeMethod1()), "someVar.SomeMethod1()") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(&SomeStruct::SomeMethod2), "&SomeStruct::SomeMethod2") == 0);
REQUIRE(std::strcmp(NAMEOF_FULL(SomeMethod3), "SomeMethod3") == 0);
}
SECTION("NAMEOF_VARIABLE_FULL") {
REQUIRE(std::strcmp(NAMEOF_VARIABLE_FULL(someVar.SomeField), "someVar.SomeField") == 0);
REQUIRE(std::strcmp(NAMEOF_VARIABLE_FULL((&someVar)->SomeField), "(&someVar)->SomeField") == 0);
REQUIRE(std::strcmp(NAMEOF_VARIABLE_FULL(::someVar), "::someVar") == 0);
}
SECTION("NAMEOF_FUNCTION_FULL") {
REQUIRE(std::strcmp(NAMEOF_FUNCTION_FULL(someVar.SomeMethod1()), "someVar.SomeMethod1()") == 0);
REQUIRE(std::strcmp(NAMEOF_FUNCTION_FULL(&SomeStruct::SomeMethod2), "&SomeStruct::SomeMethod2") == 0);
REQUIRE(std::strcmp(NAMEOF_FUNCTION_FULL(SomeMethod3), "SomeMethod3") == 0);
}
}
TEST_CASE("NAMEOF_TYPE_FULL") {
REQUIRE(std::strcmp(NAMEOF_TYPE_FULL(int[]), "int[]") == 0);
REQUIRE(std::strcmp(NAMEOF_TYPE_FULL(int), "int") == 0);
REQUIRE(std::strcmp(NAMEOF_TYPE_FULL(std::string), "std::string") == 0);
REQUIRE(std::strcmp(NAMEOF_TYPE_FULL(SomeStruct), "SomeStruct") == 0);
REQUIRE(std::strcmp(NAMEOF_TYPE_FULL(Long::LL), "Long::LL") == 0);
}