-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-reflection-cpp.cpp
247 lines (207 loc) · 6.65 KB
/
test-reflection-cpp.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// SPDX-License-Identifier: Apache-2.0
#include <reflection-cpp/reflection.hpp>
#include <catch2/catch_test_macros.hpp>
#include <iostream>
#include <string>
#include <string_view>
struct Person
{
std::string_view name;
std::string email;
int age;
};
struct TestStruct
{
int a;
float b;
double c;
std::string d;
Person e;
};
enum Color
{
Red,
Green,
Blue
};
struct SingleValueRecord
{
int value;
};
TEST_CASE("GetName", "[reflection]")
{
auto const enumValue = Reflection::GetName<Color::Red>();
CHECK(enumValue == "Red");
auto const enumValue2 = Reflection::GetName<Color::Green>();
CHECK(enumValue2 == "Green");
auto const memberName1 = Reflection::GetName<&Person::email>();
CHECK(memberName1 == "email");
auto const singleValueField = Reflection::GetName<&SingleValueRecord::value>();
CHECK(singleValueField == "value");
}
TEST_CASE("single value record", "[reflection]")
{
static_assert(Reflection::CountMembers<SingleValueRecord> == 1);
auto const s = SingleValueRecord { 42 };
auto const t = Reflection::ToTuple(s);
CHECK(std::get<0>(t) == 42);
CHECK(Reflection::GetMemberAt<0>(s) == 42);
Reflection::CallOnMembers(s, [](auto&& name, auto&& value) {
CHECK(name == "value");
CHECK(value == 42);
});
}
TEST_CASE("core", "[reflection]")
{
auto s = SingleValueRecord { 42 };
CHECK(Reflection::Inspect(s) == "value=42");
auto p = Person { "John Doe", "john@doe.com", 42 };
auto const result = Reflection::Inspect(p);
CHECK(result == R"(name="John Doe" email="john@doe.com" age=42)");
}
TEST_CASE("vector", "[reflection]")
{
auto v = std::vector<Person> {};
v.emplace_back("John Doe", "john@doe.com", 42);
v.emplace_back("John Doe", "john@doe.com", 43);
auto const result = Reflection::Inspect(v);
CHECK(result == R"(name="John Doe" email="john@doe.com" age=42
name="John Doe" email="john@doe.com" age=43
)");
}
TEST_CASE("nested", "[reflection]")
{
auto ts = TestStruct { 1, 2.0f, 3.0, "hello", { "John Doe", "john@doe.com", 42 } };
auto const result = Reflection::Inspect(ts);
CHECK(result == R"(a=1 b=2 c=3 d="hello" e={name="John Doe" email="john@doe.com" age=42})");
}
TEST_CASE("EnumerateMembers.index_and_value", "[reflection]")
{
auto ps = Person { "John Doe", "john@doe.com", 42 };
Reflection::EnumerateMembers(ps, []<size_t I>(auto&& value) {
if constexpr (I == 0)
{
CHECK(value == "John Doe");
}
else if constexpr (I == 1)
{
CHECK(value == "john@doe.com");
}
else if constexpr (I == 2)
{
CHECK(value == 42);
}
});
}
TEST_CASE("EnumerateMembers.index_and_type", "[reflection]")
{
Reflection::EnumerateMembers<Person>([]<auto I, typename T>() {
if constexpr (I == 0)
{
static_assert(std::same_as<T,std::string_view>);
}
if constexpr (I == 1)
{
static_assert(std::same_as<T,std::string>);
}
if constexpr (I == 2)
{
static_assert(std::same_as<T,int>);
}
});
}
TEST_CASE("CallOnMembers", "[reflection]")
{
auto ps = Person { "John Doe", "john@doe.com", 42 };
std::string result;
Reflection::CallOnMembers(ps, [&result](auto&& name, auto&& value) {
result += name;
result += "=";
result += std::format("{}", value);
result += " ";
});
CHECK(result == R"(name=John Doe email=john@doe.com age=42 )");
}
TEST_CASE("FoldMembers.type", "[reflection]")
{
// clang-format off
auto const result = Reflection::FoldMembers<TestStruct>(size_t{0}, []<size_t I, typename T>(auto&& result) {
return result + I;
});
// clang-format on
CHECK(result == 0 + 0 + 1 + 2 + 3 + 4);
}
struct S
{
int a {};
int b {};
int c {};
};
TEST_CASE("FoldMembers.value", "[reflection]")
{
auto const s = S { 1, 2, 3 };
auto const result = Reflection::FoldMembers(
s, 0, [](auto&& /*name*/, auto&& memberValue, auto&& accum) { return accum + memberValue; });
CHECK(result == 6);
}
TEST_CASE("MemberTypeOf", "[reflection]")
{
static_assert(std::same_as<Reflection::MemberTypeOf<0, TestStruct>, int>);
static_assert(std::same_as<Reflection::MemberTypeOf<1, TestStruct>, float>);
static_assert(std::same_as<Reflection::MemberTypeOf<2, TestStruct>, double>);
static_assert(std::same_as<Reflection::MemberTypeOf<3, TestStruct>, std::string>);
static_assert(std::same_as<Reflection::MemberTypeOf<4, TestStruct>, Person>);
}
struct Record
{
int id;
std::string name;
int age;
};
TEST_CASE("Compare.simple", "[reflection]")
{
auto const r1 = Record { .id = 1, .name = "John Doe", .age = 42 };
auto const r2 = Record { .id = 1, .name = "John Doe", .age = 42 };
auto const r3 = Record { .id = 2, .name = "Jane Doe", .age = 43 };
std::string diff;
auto differenceCallback = [&](std::string_view name, auto const& lhs, auto const& rhs) {
diff += std::format("{}: {} != {}\n", name, lhs, rhs);
};
Reflection::CollectDifferences(r1, r2, differenceCallback);
CHECK(diff.empty());
Reflection::CollectDifferences(r1, r3, differenceCallback);
CHECK(diff == "id: 1 != 2\nname: John Doe != Jane Doe\nage: 42 != 43\n");
}
TEST_CASE("Compare.simple_with_indexing", "[reflection]")
{
auto const r1 = Record { .id = 1, .name = "John Doe", .age = 42 };
auto const r2 = Record { .id = 2, .name = "John Doe", .age = 42 };
size_t check = -1;
auto differenceCallback = [&](size_t ind, auto const& lhs, auto const& rhs) {
check = ind;
};
Reflection::CollectDifferences(r1, r2, differenceCallback);
CHECK(check == 0);
}
struct Table
{
Record first;
Record second;
};
TEST_CASE("Compare.nested", "[reflection]")
{
auto const t1 = Table { .first = { .id = 1, .name = "John Doe", .age = 42 },
.second = { .id = 2, .name = "Jane Doe", .age = 43 } };
auto const t2 = Table { .first = { .id = 1, .name = "John Doe", .age = 42 },
.second = { .id = 2, .name = "Jane Doe", .age = 43 } };
auto const t3 = Table { .first = { .id = 1, .name = "John Doe", .age = 42 },
.second = { .id = 3, .name = "Jane Doe", .age = 43 } };
std::string diff;
auto differenceCallback = [&](std::string_view name, auto const& lhs, auto const& rhs) {
diff += std::format("{}: {} != {}\n", name, lhs, rhs);
};
Reflection::CollectDifferences(t1, t2, differenceCallback);
CHECK(diff.empty());
Reflection::CollectDifferences(t1, t3, differenceCallback);
CHECK(diff == "id: 2 != 3\n");
}