Skip to content

Commit 74a6181

Browse files
authored
Merge pull request #13 from contour-terminal/improvement/tests_and_concepts_for_callable
Add tests and concepts for some functions
2 parents 788bb64 + 665a4b5 commit 74a6181

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

include/reflection-cpp/reflection.hpp

+2
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

+50
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,56 @@ 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.index_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+
TEST_CASE("EnumerateMembers.index_and_type", "[reflection]")
91+
{
92+
Reflection::EnumerateMembers<Person>([]<auto I, typename T>() {
93+
if constexpr (I == 0)
94+
{
95+
static_assert(std::same_as<T,std::string_view>);
96+
}
97+
if constexpr (I == 1)
98+
{
99+
static_assert(std::same_as<T,std::string>);
100+
}
101+
if constexpr (I == 2)
102+
{
103+
static_assert(std::same_as<T,int>);
104+
}
105+
});
106+
}
107+
108+
TEST_CASE("CallOnMembers", "[reflection]")
109+
{
110+
auto ps = Person { "John Doe", "[email protected]", 42 };
111+
std::string result;
112+
Reflection::CallOnMembers(ps, [&result](auto&& name, auto&& value) {
113+
result += name;
114+
result += "=";
115+
result += std::format("{}", value);
116+
result += " ";
117+
});
118+
CHECK(result == R"(name=John Doe [email protected] age=42 )");
119+
}
120+
71121
TEST_CASE("FoldMembers.type", "[reflection]")
72122
{
73123
// clang-format off

0 commit comments

Comments
 (0)