6#ifndef MIMICPP_EXPECTATION_POLICIES_ARG_REQUIREMENT_POLICIES_HPP
7#define MIMICPP_EXPECTATION_POLICIES_ARG_REQUIREMENT_POLICIES_HPP
19#ifndef MIMICPP_DETAIL_IS_MODULE
25 #include <type_traits>
31 template <
typename Matcher>
34 using matches_fn = mimicpp::detail::matches_hook::matches_fn;
39 template <
typename... Args>
40 requires std::invocable<
matches_fn, Matcher
const&, Args&...>
45 noexcept(std::is_nothrow_invocable_v<
matches_fn, Matcher
const&, Args&...>)
47 return mimicpp::detail::matches_hook::matches(
matcher, args...);
51 template <
typename Matcher,
typename MatchesStrategy,
typename DescribeStrategy>
56 explicit constexpr ArgsRequirement(Matcher matcher, MatchesStrategy matchesStrategy, DescribeStrategy describeStrategy)
58 std::is_nothrow_move_constructible_v<Matcher>
59 && std::is_nothrow_move_constructible_v<MatchesStrategy>
60 && std::is_nothrow_move_constructible_v<DescribeStrategy>)
61 : m_Matcher{std::move(matcher)},
62 m_MatchesStrategy{std::move(matchesStrategy)},
63 m_DescribeStrategy{std::move(describeStrategy)}
73 template <
typename Return,
typename... Args>
74 requires std::is_invocable_r_v<MatchResult, MatchesStrategy const&, matcher_matches_fn<Matcher>,
call::Info<Return, Args...>
const&>
77 noexcept(std::is_nothrow_invocable_v<MatchesStrategy const&, matcher_matches_fn<Matcher>,
call::Info<Return, Args...>
const&>)
80 auto result = std::invoke(m_MatchesStrategy,
matches, info);
83 if (
auto& description = inner.description)
85 description = std::invoke(m_DescribeStrategy, *description);
93 template <
typename Return,
typename... Args>
100 [[no_unique_address]] MatchesStrategy m_MatchesStrategy;
101 [[no_unique_address]] DescribeStrategy m_DescribeStrategy;
104 template <
typename Target,
typename Matcher>
120 noexcept(std::is_nothrow_move_constructible_v<Target> && std::is_nothrow_move_constructible_v<Matcher>)
121 : m_Target{std::move(target)},
122 m_Matcher{std::move(condition)}
132 template <
typename Return,
typename... Args>
136 return mimicpp::detail::matches_hook::matches(m_Matcher,
util::unwrap_ref(m_Target));
139 template <
typename Return,
typename... Args>
150namespace mimicpp::expectation::policies::detail
152 template <std::size_t index, std::size_t... others>
153 struct arg_requirement_describer
159 out <<
"expect: arg[" << index;
160 ((out <<
", " << others), ...);
161 out <<
"] " << matcherDescription;
162 return std::move(out).str();
166 struct all_args_requirement_describer
172 out <<
"expect: arg[all] " << matcherDescription;
173 return std::move(out).str();
178 std::size_t... indices,
180 typename... Projections>
182 constexpr auto make_args_policy(
184 std::tuple<Projections...>&& projections)
187 sizeof...(indices) ==
sizeof...(Projections),
188 "Indices and projections size mismatch.");
190 using arg_selector_t = args_selector_fn<
191 std::add_lvalue_reference_t,
192 std::index_sequence<indices...>>;
193 using apply_strategy_t = arg_list_indirect_apply_fn<std::remove_cvref_t<Projections>...>;
194 using describe_strategy_t = arg_requirement_describer<indices...>;
196 return ArgsRequirement{
197 std::forward<Matcher>(matcher),
200 apply_strategy_t{std::move(projections)}),
201 describe_strategy_t{}};
231 template <std::
size_t index,
typename Matcher,
typename Projection = std::
identity>
233 constexpr auto arg(Matcher&& matcher, Projection&& projection = {})
235 std::is_nothrow_constructible_v<std::remove_cvref_t<Matcher>, Matcher>
236 && std::is_nothrow_constructible_v<std::remove_cvref_t<Projection>, Projection>)
238 return expectation::policies::detail::make_args_policy<index>(
239 std::forward<Matcher>(matcher),
240 std::forward_as_tuple(std::forward<Projection>(projection)));
266 std::size_t... others,
268 typename... Projections>
270 constexpr auto args(Matcher&& matcher, Projections&&... projections)
272 std::is_nothrow_constructible_v<std::remove_cvref_t<Matcher>, Matcher>
273 && (... && std::is_nothrow_move_constructible_v<std::remove_cvref_t<Projections>>))
276 sizeof...(projections) <= 1u +
sizeof...(others),
277 "The projection count exceeds the amount of indices.");
279 return expectation::policies::detail::make_args_policy<first, others...>(
280 std::forward<Matcher>(matcher),
281 util::detail::expand_tuple<std::identity, 1u +
sizeof...(others)>(
282 std::forward_as_tuple(std::forward<Projections>(projections)...)));
297 template <
typename Matcher>
300 noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<Matcher>, Matcher>)
302 using arg_selector_t = expectation::policies::detail::all_args_selector_fn<std::add_lvalue_reference_t>;
303 using apply_strategy_t = expectation::policies::detail::arg_list_forward_apply_fn;
304 using describe_strategy_t = expectation::policies::detail::all_args_requirement_describer;
307 std::forward<Matcher>(matcher),
308 expectation::policies::detail::apply_args_fn(arg_selector_t{}, apply_strategy_t{}),
309 describe_strategy_t{}};
336 template <
typename Target, matcher_for<Target> Matcher>
338 constexpr auto that(Target&& target, Matcher&& matcher)
#define MIMICPP_DETAIL_MODULE_EXPORT
Definition Config.hpp:19
Definition RequirementPolicies.hpp:53
constexpr ArgsRequirement(Matcher matcher, MatchesStrategy matchesStrategy, DescribeStrategy describeStrategy) noexcept(std::is_nothrow_move_constructible_v< Matcher > &&std::is_nothrow_move_constructible_v< MatchesStrategy > &&std::is_nothrow_move_constructible_v< DescribeStrategy >)
Definition RequirementPolicies.hpp:56
static constexpr void consume(call::Info< Return, Args... > const &info) noexcept
Definition RequirementPolicies.hpp:94
MatchResult matches(call::Info< Return, Args... > const &info) const noexcept(std::is_nothrow_invocable_v< MatchesStrategy const &, matcher_matches_fn< Matcher >, call::Info< Return, Args... > const & >)
Definition RequirementPolicies.hpp:76
static constexpr bool is_satisfied() noexcept
Definition RequirementPolicies.hpp:68
Definition RequirementPolicies.hpp:107
ThatRequirement(ThatRequirement const &)=delete
static constexpr bool is_satisfied() noexcept
Definition RequirementPolicies.hpp:127
constexpr ThatRequirement(Target target, Matcher condition) noexcept(std::is_nothrow_move_constructible_v< Target > &&std::is_nothrow_move_constructible_v< Matcher >)
Definition RequirementPolicies.hpp:119
ThatRequirement & operator=(ThatRequirement &&)=default
~ThatRequirement()=default
ThatRequirement(ThatRequirement &&)=default
static constexpr void consume(call::Info< Return, Args... > const &) noexcept
Definition RequirementPolicies.hpp:140
MatchResult matches(call::Info< Return, Args... > const &) const
Definition RequirementPolicies.hpp:134
ThatRequirement & operator=(ThatRequirement const &)=delete
Definition Common.hpp:206
constexpr auto arg(Matcher &&matcher, Projection &&projection={}) noexcept(std::is_nothrow_constructible_v< std::remove_cvref_t< Matcher >, Matcher > &&std::is_nothrow_constructible_v< std::remove_cvref_t< Projection >, Projection >)
Checks whether the selected argument satisfies the given matcher.
Definition RequirementPolicies.hpp:233
constexpr auto all_args(Matcher &&matcher) noexcept(std::is_nothrow_constructible_v< std::remove_cvref_t< Matcher >, Matcher >)
Checks whether the all arguments satisfy the given matcher.
Definition RequirementPolicies.hpp:299
constexpr auto args(Matcher &&matcher, Projections &&... projections) noexcept(std::is_nothrow_constructible_v< std::remove_cvref_t< Matcher >, Matcher > &&(... &&std::is_nothrow_move_constructible_v< std::remove_cvref_t< Projections > >))
Checks whether the selected arguments satisfy the given matcher.
Definition RequirementPolicies.hpp:270
constexpr auto that(Target &&target, Matcher &&matcher)
Checks whether the given target satisfies the given matcher.
Definition RequirementPolicies.hpp:338
Definition ControlPolicies.hpp:254
Definition ArgumentList.hpp:24
std::variant< MatchSuccess, MatchFailure > MatchResult
Definition Common.hpp:41
constexpr T & unwrap_ref(std::reference_wrapper< T > const ref) noexcept
Unwraps the given std::reference_wrapper and returns the referenced object.
Definition UnwrapRef.hpp:29
std::basic_ostringstream< CharT, CharTraitsT > StringStreamT
Definition Format.hpp:38
std::basic_string_view< CharT, CharTraitsT > StringViewT
Definition Fwd.hpp:398
std::basic_string< CharT, CharTraitsT > StringT
Definition Fwd.hpp:397
Definition RequirementPolicies.hpp:33
mimicpp::detail::matches_hook::matches_fn matches_fn
Definition RequirementPolicies.hpp:34
Matcher const & matcher
Definition RequirementPolicies.hpp:37
MatchResult operator()(Args &&... args) const noexcept(std::is_nothrow_invocable_v< matches_fn, Matcher const &, Args &... >)
Definition RequirementPolicies.hpp:44