Skip to content

Commit b6e6ba3

Browse files
committed
Add tests and concepts for some functions
1 parent 788bb64 commit b6e6ba3

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

include/reflection-cpp/reflection.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,7 @@ constexpr void EnumerateMembers(Callable&& callable)
715715
}
716716

717717
template <typename Object, typename Callable>
718+
requires std::same_as<void, std::invoke_result_t<Callable, std::string, MemberTypeOf<1, Object>>>
718719
void CallOnMembers(Object& object, Callable&& callable)
719720
{
720721
EnumerateMembers<Object>(object,
@@ -751,6 +752,7 @@ constexpr ResultType FoldMembers(ResultType initialValue, Callable const& callab
751752
///
752753
/// @return The result of the fold
753754
template <typename Object, typename Callable, typename ResultType>
755+
requires std::same_as<ResultType, std::invoke_result_t<Callable, std::string, MemberTypeOf<1, Object>, ResultType>>
754756
constexpr ResultType FoldMembers(Object& object, ResultType initialValue, Callable const& callable)
755757
{
756758
// clang-format off

test-reflection-cpp.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,61 @@ TEST_CASE("nested", "[reflection]")
6868
CHECK(result == R"(a=1 b=2 c=3 d="hello" e={name="John Doe" email="[email protected]" age=42})");
6969
}
7070

71+
TEST_CASE("EnumerateMembers.ind_and_value", "[reflection]")
72+
{
73+
auto ps = Person { "John Doe", "[email protected]", 42 };
74+
Reflection::EnumerateMembers(ps, []<size_t I>(auto&& value) {
75+
if constexpr (I == 0)
76+
{
77+
CHECK(value == "John Doe");
78+
}
79+
else if constexpr (I == 1)
80+
{
81+
CHECK(value == "[email protected]");
82+
}
83+
else if constexpr (I == 2)
84+
{
85+
CHECK(value == 42);
86+
}
87+
});
88+
89+
}
90+
91+
92+
TEST_CASE("EnumerateMembers.ind_and_type", "[reflection]")
93+
{
94+
Reflection::EnumerateMembers<Person>([]<auto I, typename T>() {
95+
if constexpr(I==0)
96+
{
97+
static_assert(T{} == "");
98+
}
99+
if constexpr(I==1)
100+
{
101+
static_assert(T{} == "");
102+
}
103+
if constexpr(I==2)
104+
{
105+
static_assert(T{} == 0);
106+
}
107+
});
108+
109+
}
110+
111+
112+
113+
TEST_CASE("CallOnMembers", "[reflection]")
114+
{
115+
auto ps = Person { "John Doe", "[email protected]", 42 };
116+
std::string result;
117+
Reflection::CallOnMembers(ps, [&result](auto&& name, auto&& value) {
118+
result += name;
119+
result += "=";
120+
result += std::format("{}", value);
121+
result += " ";
122+
});
123+
CHECK(result == R"(name=John Doe [email protected] age=42 )");
124+
}
125+
71126
TEST_CASE("FoldMembers.type", "[reflection]")
72127
{
73128
// clang-format off

0 commit comments

Comments
 (0)