-
Notifications
You must be signed in to change notification settings - Fork 11
/
introspect.cpp
180 lines (148 loc) · 5.97 KB
/
introspect.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <algorithm>
#include <catch2/catch_all.hpp>
#include <functional>
#include <stdexcept>
#include <tsmp/introspect.hpp>
#include <variant>
int print_counter = 0;
int print2_counter = 0;
struct foo_t
{
int i;
float f;
void print() const { ++print_counter; }
void print2(int) const { ++print2_counter; }
constexpr int add(int lhs, int rhs) const { return lhs + rhs; }
constexpr auto& inc()
{
++i;
return *this;
}
};
TEST_CASE("member field&function index calculation test", "[unit]")
{
foo_t foo{0, 0.0f};
tsmp::introspect foo_introspecter(foo);
static_assert(tsmp::introspect<foo_t>::field_id("i") == 0, "Index of i is wrong.");
static_assert(tsmp::introspect<foo_t>::field_id("f") == 1, "Index of f is wrong.");
static_assert(foo_introspecter.field_id("i") == 0, "Index of i is wrong.");
static_assert(foo_introspecter.field_id("f") == 1, "Index of f is wrong.");
static_assert(tsmp::introspect<foo_t>::function_id("print") == 0, "Index of i is wrong.");
static_assert(tsmp::introspect<foo_t>::function_id("print2") == 1, "Index of f is wrong.");
static_assert(tsmp::introspect<foo_t>::function_id("add") == 2, "Index of f is wrong.");
static_assert(tsmp::introspect<foo_t>::function_id("inc") == 3, "Index of f is wrong.");
static_assert(foo_introspecter.function_id("print") == 0, "Index of i is wrong.");
static_assert(foo_introspecter.function_id("print2") == 1, "Index of f is wrong.");
static_assert(foo_introspecter.function_id("add") == 2, "Index of f is wrong.");
static_assert(foo_introspecter.function_id("inc") == 3, "Index of f is wrong.");
}
TEST_CASE("compile-time get member attribute test", "[unit]")
{
foo_t foo{0, 0.0f};
tsmp::introspect foo_introspecter(foo);
REQUIRE(foo.i == 0);
foo_introspecter.get<0>() = 13;
REQUIRE(foo.i == 13);
foo_introspecter.get<"i">() = 42;
REQUIRE(foo.i == 42);
REQUIRE(foo.f == Catch::Approx(0.0f));
foo_introspecter.get<1>() = 13.0f;
REQUIRE(foo.f == Catch::Approx(13.0f));
foo_introspecter.get<"f">() = 42.0f;
REQUIRE(foo.f == Catch::Approx(42.0f));
}
TEST_CASE("run-time get member attribute test", "[unit]")
{
foo_t foo{42, 43.0f};
tsmp::introspect foo_introspecter(foo);
REQUIRE(std::holds_alternative<int>(foo_introspecter.fetch("i")));
REQUIRE(std::get<int>(foo_introspecter.fetch("i")) == 42);
foo_introspecter.set("i", 43);
REQUIRE(foo.i == 43);
REQUIRE_THROWS(foo_introspecter.set("i", "wrong type"));
REQUIRE_THROWS(foo_introspecter.set("i", 43.0f));
REQUIRE(std::holds_alternative<float>(foo_introspecter.fetch("f")));
REQUIRE(std::get<float>(foo_introspecter.fetch("f")) == Catch::Approx(43.0f));
foo_introspecter.set("f", 43.0f);
REQUIRE(foo.f == Catch::Approx(43.0f));
REQUIRE_THROWS(foo_introspecter.set("f", "wrong type"));
REQUIRE_THROWS(foo_introspecter.set("f", 43));
}
TEST_CASE("run-time visit member attributes test", "[unit]")
{
foo_t foo{42, 43.0f};
tsmp::introspect foo_introspecter(foo);
foo_introspecter.visit_fields([](auto, auto name, auto& field) {
if (name == "i") {
REQUIRE(field == Catch::Approx(42));
REQUIRE(std::is_same_v<decltype(field), int&>);
field = 4;
} else if (name == "f") {
REQUIRE(field == Catch::Approx(43.0f));
REQUIRE(std::is_same_v<decltype(field), float&>);
field = 5.0f;
}
});
REQUIRE(foo.i == 4);
REQUIRE(foo.f == Catch::Approx(5.0f));
}
TEST_CASE("member function execution test, no arguments, no result", "[unit]")
{
const foo_t foo{43, 0.0f};
tsmp::introspect foo_introspecter(foo);
print_counter = 0;
foo_introspecter.call<0>();
REQUIRE(print_counter == 1);
foo_introspecter.call<"print">();
REQUIRE(print_counter == 2);
const auto invoke_result = foo_introspecter.invoke("print");
REQUIRE(std::holds_alternative<std::monostate>(invoke_result));
REQUIRE(print_counter == 3);
}
TEST_CASE("member function execution test, with arguments, no result", "[unit]")
{
const foo_t foo{43, 0.0f};
tsmp::introspect foo_introspecter(foo);
print2_counter = 0;
foo_introspecter.call<1>(42);
REQUIRE(print2_counter == 1);
foo_introspecter.call<"print2">(42);
REQUIRE(print2_counter == 2);
const auto invoke_result = foo_introspecter.invoke("print2", 5);
REQUIRE(std::holds_alternative<std::monostate>(invoke_result));
REQUIRE(print2_counter == 3);
}
TEST_CASE("member function execution test, with arguments and result", "[unit]")
{
const foo_t foo{43, 0.0f};
tsmp::introspect foo_introspecter(foo);
REQUIRE(foo_introspecter.call<2>(5, 2) == 7);
REQUIRE(foo_introspecter.call<"add">(5, 2) == 7);
const auto invoke_result = foo_introspecter.invoke("add", 5, 2);
REQUIRE(std::holds_alternative<int>(invoke_result));
REQUIRE(std::get<int>(invoke_result) == 7);
}
TEST_CASE("member function execution test with state change", "")
{
foo_t foo{43, 0.0f};
tsmp::introspect foo_introspecter(foo);
print_counter = 0;
print2_counter = 0;
REQUIRE(foo.i == 43);
foo_introspecter.call<3>();
REQUIRE(foo.i == 44);
foo_introspecter.call<"inc">();
REQUIRE(foo.i == 45);
const auto invoke_result = foo_introspecter.invoke("inc");
REQUIRE(std::holds_alternative<std::reference_wrapper<foo_t>>(invoke_result) == true);
REQUIRE(&(std::get<std::reference_wrapper<foo_t>>(invoke_result).get()) == &foo);
REQUIRE(foo.i == 46);
}
TEST_CASE("constexpr member function execution test", "[unit]")
{
constexpr auto foo_2 = foo_t{0, 0}.inc();
// static_assert(foo_2.i == 1, "i should be 1");
// static_assert(tsmp::introspect{ foo_2 }.call<0>(5, 2) == 7, "2+5 must be equal to 7");
static_assert(std::get<int>(tsmp::introspect{foo_2}.invoke("add", 5, 2)) == 7, "2+5 must be equal to 7");
// static_assert(std::holds_alternative<int>(tsmp::introspect{ foo_2 }.get("i")), "Index i must be of type int");
}