Skip to content

Commit

Permalink
feat: add static checks during mock setup
Browse files Browse the repository at this point in the history
  • Loading branch information
priyasiddharth committed Jan 23, 2024
1 parent 6833c23 commit 5c7c222
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/include/seamock.hh
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,15 @@ BOOST_HANA_CONSTEXPR_LAMBDA auto AND =
}); \
}

// Define a function to check if it's valid to call F with Args...
template <typename F, typename Tuple>
auto is_callable_with_args(F&& f, Tuple&& t) {
auto checker = hana::is_valid([&](auto&&... args) -> decltype(f(args...)) {});

// Unpack the tuple and apply to the checker
return hana::unpack(std::forward<Tuple>(t), checker);
}

#define MOCK_UTIL_WRAP_VAL(x) []() -> decltype(x) { return x; }

// ---------------------------------------------
Expand Down Expand Up @@ -270,9 +279,8 @@ static auto skeletal = [](auto &&expectations_map, auto &&args_tuple) {
auto ret_fn = hana::at_key(expectations_map, RETURN_FN);
auto capture_map = hana::at_key(expectations_map, CAPTURE_ARGS_MAPS);
// NOTE: INVARIANT: return fn should be callable
// static_assert(
// hana::is_valid([&ret_fn]() -> decltype(ret_fn()) { return 0;
// }));
BOOST_HANA_CONSTANT_ASSERT_MSG(hana::is_valid(ret_fn)(),
"Return function is not callable!");
// NOTE: (arg0, arg1, ..._N) -> (0, 1, ..._N)
auto args_range =
hana::make_range(hana::size_c<0>, hana::size(args_tuple));
Expand Down Expand Up @@ -320,6 +328,8 @@ static auto skeletal = [](auto &&expectations_map, auto &&args_tuple) {
},
[&](auto _) {
auto invoke_fn = _(invoke_fn_optional).value();
BOOST_HANA_CONSTANT_ASSERT_MSG(is_callable_with_args(invoke_fn, args_tuple),
"Unexpected invoke function signature!");
return hana::unpack(args_tuple, invoke_fn);
});
};
Expand Down

0 comments on commit 5c7c222

Please sign in to comment.