6#ifndef MIMICPP_EXPECTATION_POLICIES_HPP
7#define MIMICPP_EXPECTATION_POLICIES_HPP
19namespace mimicpp::expectation_policies::detail
22 inline StringT describe_times_state(
const std::size_t current,
const std::size_t min,
const std::size_t max)
24 const auto verbalizeValue = [](
const std::size_t value)->
StringT
35 return format::format(
"{} times", value);
41 return format::format(
42 "inapplicable: already saturated (matched {})",
43 verbalizeValue(current));
48 return format::format(
49 "applicable: accepts further matches (matched {} out of {} times)",
54 const auto verbalizeInterval = [verbalizeValue](
const std::size_t start,
const std::size_t end)
58 return format::format(
59 "between {} and {} times",
64 return format::format(
69 return format::format(
70 "unsatisfied: matched {} - {} is expected",
71 verbalizeValue(current),
72 verbalizeInterval(min, max));
81 template <
typename Return,
typename... Args>
91 constexpr explicit Times(
const std::size_t min,
const std::size_t max)
97 throw std::invalid_argument{
"min must be less or equal to max."};
104 return m_Min <= m_Count
111 return m_Count < m_Max;
117 return detail::describe_times_state(
131 std::size_t m_Count{};
145 template <ValueCategory expected>
154 template <
typename Return,
typename... Args>
160 template <
typename Return,
typename... Args>
169 return format::format(
170 "expect: from {} category overload",
175 template <Constness constness>
184 template <
typename Return,
typename... Args>
190 template <
typename Return,
typename... Args>
199 return format::format(
200 "expect: from {} qualified overload",
205 template <
typename Action>
206 requires std::same_as<Action, std::remove_cvref_t<Action>>
207 && std::is_move_constructible_v<Action>
214 )
noexcept(std::is_nothrow_move_constructible_v<Action>)
215 : m_Action{std::move(action)}
219 template <
typename Return,
typename... Args>
220 requires std::invocable<Action&,
const call::Info<Return, Args...>&>
222 std::invoke_result_t<Action&,
const call::Info<Return, Args...>&>,
228 std::is_nothrow_invocable_v<Action&,
const call::Info<Return, Args...>&>
230 std::invoke_result_t<Action&,
const call::Info<Return, Args...>&>,
233 return static_cast<Return
>(
234 std::invoke(m_Action, call));
241 template <
typename Exception>
242 requires (!std::is_reference_v<Exception>)
243 && std::copyable<Exception>
248 explicit constexpr Throws(Exception exception)
noexcept(std::is_nothrow_move_constructible_v<Exception>)
249 : m_Exception{std::move(exception)}
253 template <
typename Return,
typename... Args>
262 Exception m_Exception;
265 template <
typename Matcher,
typename Projection,
typename Describer>
266 requires std::same_as<Matcher, std::remove_cvref_t<Matcher>>
267 && std::same_as<Projection, std::remove_cvref_t<Projection>>
268 && std::same_as<Describer, std::remove_cvref_t<Describer>>
269 && std::is_move_constructible_v<Matcher>
270 && std::is_move_constructible_v<Projection>
271 && std::is_move_constructible_v<Describer>
278 Projection projection = Projection{},
279 Describer describer = Describer{}
281 std::is_nothrow_move_constructible_v<Matcher>
282 && std::is_nothrow_move_constructible_v<Projection>
283 && std::is_nothrow_move_constructible_v<Describer>)
284 : m_Matcher{std::move(matcher)},
285 m_Projection{std::move(projection)},
286 m_Describer{std::move(describer)}
295 template <
typename Return,
typename... Args>
296 requires std::invocable<
const Projection&,
const call::Info<Return, Args...>&>
299 std::invoke_result_t<
const Projection&,
const call::Info<Return, Args...>&>>
303 return m_Matcher.matches(
304 std::invoke(m_Projection, info));
307 template <
typename Return,
typename... Args>
315 if (
const std::optional<StringT> description = m_Matcher.describe())
326 [[no_unique_address]] Matcher m_Matcher;
327 [[no_unique_address]] Projection m_Projection;
328 [[no_unique_address]] Describer m_Describer;
331 template <
typename Action>
340 )
noexcept(std::is_nothrow_move_constructible_v<Action>)
341 : m_Action{std::move(action)}
357 template <
typename Return,
typename... Args>
370 template <
typename Return,
typename... Args>
371 requires std::invocable<Action&,
const call::Info<Return, Args...>&>
374 )
noexcept(std::is_nothrow_invocable_v<Action&,
const call::Info<Return, Args...>&>)
376 std::invoke(m_Action, info);
383 template <
typename Action,
template <
typename>
typename Projection>
384 requires std::same_as<Action, std::remove_cvref_t<Action>>
390 Action action = Action{}
391 )
noexcept(std::is_nothrow_move_constructible_v<Action>)
392 : m_Action{std::move(action)}
396 template <
typename Arg>
399 template <
typename Return,
typename... Args>
400 requires std::invocable<
403 constexpr decltype(
auto)
operator ()(
406 std::is_nothrow_invocable_v<
412 "Projection can not be applied.");
415 [
this](
auto&... args) ->
decltype(
auto)
429 template <
typename Action,
template <
typename>
typename Projection, std::size_t... indices>
430 requires std::same_as<Action, std::remove_cvref_t<Action>>
436 Action action = Action{}
437 )
noexcept(std::is_nothrow_move_constructible_v<Action>)
438 : m_Action{std::move(action)}
442 template <std::size_t index,
typename... Args>
445 template <std::size_t index,
typename... Args>
448 template <
typename Return,
typename... Args>
449 requires (... && (indices <
sizeof...(Args)))
456 std::is_nothrow_invocable_v<
465 "Projection can not be applied.");
470 std::get<indices>(callInfo.args).get())...);
478namespace mimicpp::expectation_policies::detail
482 template <
typename T>
484 constexpr T&& operator ()(T&& obj)
const noexcept
486 return std::forward<T>(obj);
515 template <std::
size_t min, std::
size_t max = min>
516 requires (min <= max)
535 constexpr auto times(
const std::size_t min,
const std::size_t max)
548 constexpr auto times(
const std::size_t exactly)
noexcept
550 return times(exactly, exactly);
560 template <std::
size_t min>
566 std::numeric_limits<std::size_t>::max()
578 constexpr auto at_least(
const std::size_t min)
noexcept
589 template <std::
size_t max>
606 constexpr auto at_most(
const std::size_t max)
noexcept
647 template <std::
size_t index>
648 struct arg_requirement_describer
655 return format::format(
656 "expect: arg[{}] {}",
689 template <std::
size_t index,
typename Matcher>
693 )
noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<Matcher>, Matcher&&>)
696 std::remove_cvref_t<Matcher>,
698 expectation_policies::detail::forward_fn,
699 std::add_lvalue_reference_t,
701 detail::arg_requirement_describer<index>>{
702 std::forward<Matcher>(matcher),
744 template <
typename Fun>
745 requires std::invocable<std::remove_cvref_t<Fun>&>
746 && (!std::is_void_v<std::invoke_result_t<std::remove_cvref_t<Fun>&>>)
750 )
noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<Fun>, Fun>)
754 fun = std::forward<Fun>(fun)
755 ]([[maybe_unused]]
const auto& call)
mutable noexcept(std::is_nothrow_invocable_v<
decltype(fun)>) ->
decltype(
auto)
757 return std::invoke(fun);
779 template <
typename T>
780 requires std::copyable<std::remove_cvref_t<T>>
784 )
noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<T>, T>)
787 [v = std::forward<T>(value)]([[maybe_unused]]
const auto& call)
mutable noexcept ->
auto& {
788 return static_cast<std::unwrap_reference_t<decltype(v)
>&>(v);
805 template <std::size_t index, std::size_t... otherIndices,
typename Action>
809 )
noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<Action>, Action>)
813 std::remove_cvref_t<Action>,
814 std::add_lvalue_reference_t,
817 std::forward<Action>(action)
832 template <
typename Action>
836 )
noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<Action>, Action>)
840 std::remove_cvref_t<Action>,
841 std::add_lvalue_reference_t>{
842 std::forward<Action>(action)
854 template <std::
size_t index>
860 expectation_policies::detail::forward_fn,
861 std::add_rvalue_reference_t,
863 expectation_policies::detail::forward_fn{}
877 template <
typename T>
878 requires std::copyable<std::remove_cvref_t<T>>
882 )
noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<T>, T>)
885 std::forward<T>(exception)
926 template <std::
size_t index,
typename Action>
930 )
noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<Action>, Action>)
934 std::remove_cvref_t<Action>,
935 std::add_lvalue_reference_t,
937 std::forward<Action>(action)
952 template <std::size_t index, std::size_t... additionalIndices,
typename Action>
956 )
noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<Action>, Action>)
960 std::remove_cvref_t<Action>,
961 std::add_lvalue_reference_t,
963 additionalIndices...>{
964 std::forward<Action>(action)
975 template <
typename Action>
979 )
noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<Action>, Action>)
983 std::remove_cvref_t<Action>,
984 std::add_lvalue_reference_t>{
985 std::forward<Action>(action)
996 template <std::invocable Action>
1000 )
noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<Action>, Action>)
1004 action = std::forward<Action>(action)
1005 ]([[maybe_unused]]
const auto& call)
mutable noexcept(std::is_nothrow_invocable_v<Action&>)
1007 std::invoke(action);
Definition ExpectationPolicies.hpp:386
constexpr ApplyAllArgsAction(Action action=Action{}) noexcept(std::is_nothrow_move_constructible_v< Action >)
Definition ExpectationPolicies.hpp:389
Projection< Arg > ProjectedArgT
Definition ExpectationPolicies.hpp:397
Definition ExpectationPolicies.hpp:432
constexpr decltype(auto) operator()(const call::Info< Return, Args... > &callInfo) const noexcept(std::is_nothrow_invocable_v< const Action &, ProjectedArgListElementT< indices, Args... >... >)
Definition ExpectationPolicies.hpp:453
constexpr ApplyArgsAction(Action action=Action{}) noexcept(std::is_nothrow_move_constructible_v< Action >)
Definition ExpectationPolicies.hpp:435
std::tuple_element_t< index, std::tuple< Args... > > ArgListElementT
Definition ExpectationPolicies.hpp:443
Projection< ArgListElementT< index, Args... > > ProjectedArgListElementT
Definition ExpectationPolicies.hpp:446
Definition ExpectationPolicies.hpp:147
static constexpr bool is_satisfied() noexcept
Definition ExpectationPolicies.hpp:149
static constexpr void consume(const call::Info< Return, Args... > &info) noexcept
Definition ExpectationPolicies.hpp:161
static StringT describe()
Definition ExpectationPolicies.hpp:167
static constexpr bool matches(const call::Info< Return, Args... > &info) noexcept
Definition ExpectationPolicies.hpp:155
Definition ExpectationPolicies.hpp:177
static constexpr bool is_satisfied() noexcept
Definition ExpectationPolicies.hpp:179
static constexpr void consume(const call::Info< Return, Args... > &info) noexcept
Definition ExpectationPolicies.hpp:191
static constexpr bool matches(const call::Info< Return, Args... > &info) noexcept
Definition ExpectationPolicies.hpp:185
static StringT describe()
Definition ExpectationPolicies.hpp:197
Definition ExpectationPolicies.hpp:79
static constexpr void finalize_call(const call::Info< Return, Args... > &) noexcept
Definition ExpectationPolicies.hpp:82
Definition ExpectationPolicies.hpp:136
consteval InitTimes() noexcept
Definition ExpectationPolicies.hpp:139
Definition ExpectationPolicies.hpp:273
static constexpr void consume(const call::Info< Return, Args... > &info) noexcept
Definition ExpectationPolicies.hpp:308
static constexpr bool is_satisfied() noexcept
Definition ExpectationPolicies.hpp:290
constexpr bool matches(const call::Info< Return, Args... > &info) const
Definition ExpectationPolicies.hpp:301
constexpr Requirement(Matcher matcher, Projection projection=Projection{}, Describer describer=Describer{}) noexcept(std::is_nothrow_move_constructible_v< Matcher > &&std::is_nothrow_move_constructible_v< Projection > &&std::is_nothrow_move_constructible_v< Describer >)
Definition ExpectationPolicies.hpp:276
std::optional< StringT > describe() const
Definition ExpectationPolicies.hpp:313
Definition ExpectationPolicies.hpp:209
constexpr Return finalize_call(const call::Info< Return, Args... > &call) noexcept(std::is_nothrow_invocable_v< Action &, const call::Info< Return, Args... > & > &¬hrow_explicitly_convertible_to< std::invoke_result_t< Action &, const call::Info< Return, Args... > & >, Return >)
Definition ExpectationPolicies.hpp:225
constexpr ReturnsResultOf(Action &&action) noexcept(std::is_nothrow_move_constructible_v< Action >)
Definition ExpectationPolicies.hpp:212
Definition ExpectationPolicies.hpp:333
static constexpr bool matches(const call::Info< Return, Args... > &) noexcept
Definition ExpectationPolicies.hpp:359
static std::nullopt_t describe() noexcept
Definition ExpectationPolicies.hpp:365
~SideEffectAction()=default
constexpr void consume(const call::Info< Return, Args... > &info) noexcept(std::is_nothrow_invocable_v< Action &, const call::Info< Return, Args... > & >)
Definition ExpectationPolicies.hpp:372
SideEffectAction & operator=(const SideEffectAction &)=delete
static constexpr bool is_satisfied() noexcept
Definition ExpectationPolicies.hpp:352
SideEffectAction(SideEffectAction &&)=default
constexpr SideEffectAction(Action &&action) noexcept(std::is_nothrow_move_constructible_v< Action >)
Definition ExpectationPolicies.hpp:338
SideEffectAction(const SideEffectAction &)=delete
Definition ExpectationPolicies.hpp:245
constexpr Return finalize_call(const call::Info< Return, Args... > &call)
Definition ExpectationPolicies.hpp:254
constexpr Throws(Exception exception) noexcept(std::is_nothrow_move_constructible_v< Exception >)
Definition ExpectationPolicies.hpp:248
Definition ExpectationPolicies.hpp:88
StringT describe_state() const
Definition ExpectationPolicies.hpp:115
constexpr bool is_satisfied() const noexcept
Definition ExpectationPolicies.hpp:102
constexpr Times(const std::size_t min, const std::size_t max)
Definition ExpectationPolicies.hpp:91
constexpr void consume() noexcept
Definition ExpectationPolicies.hpp:123
constexpr bool is_applicable() const noexcept
Definition ExpectationPolicies.hpp:109
Definition Utility.hpp:128
Definition Matcher.hpp:25
Definition Utility.hpp:135
constexpr auto returns_apply_all_result_of(Action &&action) noexcept(std::is_nothrow_constructible_v< std::remove_cvref_t< Action >, Action >)
During the finalization step, all call arguments are applied on the given action.
Definition ExpectationPolicies.hpp:834
constexpr auto returns_apply_result_of(Action &&action) noexcept(std::is_nothrow_constructible_v< std::remove_cvref_t< Action >, Action >)
During the finalization step, the selected call arguments are applied on the given action.
Definition ExpectationPolicies.hpp:807
constexpr auto returns(T &&value) noexcept(std::is_nothrow_constructible_v< std::remove_cvref_t< T >, T >)
During the finalization step, the stored value is returned.
Definition ExpectationPolicies.hpp:782
constexpr auto returns_arg() noexcept
During the finalization step, the selected call argument is returned.
Definition ExpectationPolicies.hpp:856
constexpr auto returns_result_of(Fun &&fun) noexcept(std::is_nothrow_constructible_v< std::remove_cvref_t< Fun >, Fun >)
During the finalization step, the invocation result of the given function is returned.
Definition ExpectationPolicies.hpp:748
constexpr auto arg(Matcher &&matcher) noexcept(std::is_nothrow_constructible_v< std::remove_cvref_t< Matcher >, Matcher && >)
Checks, whether the selected argument matches the given matcher.
Definition ExpectationPolicies.hpp:691
constexpr auto apply_all(Action &&action) noexcept(std::is_nothrow_constructible_v< std::remove_cvref_t< Action >, Action >)
Applies all arguments on the given action.
Definition ExpectationPolicies.hpp:977
constexpr auto apply_arg(Action &&action) noexcept(std::is_nothrow_constructible_v< std::remove_cvref_t< Action >, Action >)
Applies the argument at the specified index on the given action.
Definition ExpectationPolicies.hpp:928
constexpr auto invoke(Action &&action) noexcept(std::is_nothrow_constructible_v< std::remove_cvref_t< Action >, Action >)
Invokes the given function.
Definition ExpectationPolicies.hpp:998
constexpr auto apply_args(Action &&action) noexcept(std::is_nothrow_constructible_v< std::remove_cvref_t< Action >, Action >)
Applies the arguments at the specified index and in that order on the given action.
Definition ExpectationPolicies.hpp:954
consteval auto at_least() noexcept
Specifies a times policy with just a lower limit.
Definition ExpectationPolicies.hpp:562
consteval auto twice() noexcept
Specifies a times policy with both limits set to 2.
Definition ExpectationPolicies.hpp:633
consteval auto once() noexcept
Specifies a times policy with both limits set to 1.
Definition ExpectationPolicies.hpp:618
consteval auto times() noexcept
Specifies arbitrary limits with compile time arguments.
Definition ExpectationPolicies.hpp:518
consteval auto at_most() noexcept
Specifies a times policy with just an upper limit.
Definition ExpectationPolicies.hpp:591
Definition ExpectationPolicies.hpp:492
Definition ExpectationPolicies.hpp:20
Definition ExpectationPolicies.hpp:712
Definition ExpectationPolicies.hpp:897
constexpr bool is_matching(const Constness lhs, const Constness rhs) noexcept
Definition Utility.hpp:28
std::basic_string_view< CharT, CharTraitsT > StringViewT
Definition Printer.hpp:23
std::basic_string< CharT, CharTraitsT > StringT
Definition Fwd.hpp:37