-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypename_test.cpp
75 lines (66 loc) · 2.1 KB
/
typename_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <doctest/doctest.h>
#include <a4z/typename.hpp>
#include <string>
#include <vector>
// This shows a few use cases,
// should be clear that this is not a real reflection and has limits
SCENARIO("Check simple typename") {
GIVEN("the typename of an int") {
auto tn = a4z::type_name<int>();
WHEN("expecting the according typename") {
std::string expected = "int";
THEN("the typename matches the expected one") {
CHECK(std::string{tn.c_str()} == expected);
}
}
}
}
struct Foo {};
SCENARIO("Check struct typename") {
GIVEN("the typename of Foo") {
auto tn = a4z::type_name<Foo>();
WHEN("expecting the according typename") {
std::string expected = "Foo";
THEN("the typename matches the expected one") {
CHECK(std::string{tn.c_str()} == expected);
}
}
}
}
namespace bar {
struct Foo {};
} // namespace bar
SCENARIO("Check struct in a namespace typename") {
GIVEN("the typename of Foo") {
auto tn = a4z::type_name<bar::Foo>();
WHEN("expecting the according typename") {
std::string expected = "bar::Foo";
THEN("the typename matches the expected one") {
CHECK(std::string{tn.c_str()} == expected);
}
}
}
}
// This shows how to customize the typename for a type where compilers have
// different display names std::string could be something like
// basic_string<char, char_traits<char>, allocator<char>> and this is nothing
// you want to see (except for debug purposes)
namespace a4z {
template <>
constexpr auto type_name<std::string>() {
return a4z::astr<12>{"std::string"};
}
} // namespace a4z
// std::string is always a bit special , basic_string blab bla bla
SCENARIO("Check specialized typename") {
GIVEN("the typename of std::string") {
constexpr auto tn = a4z::type_name<std::string>();
static_assert(a4z::equal("std::string", tn.c_str())); // real compile time
WHEN("expecting the according typename") {
std::string expected = "std::string";
THEN("the typename matches the expected one") {
CHECK(std::string{tn.c_str()} == expected);
}
}
}
}