6#ifndef MIMICPP_MATCHERS_GENERAL_MATCHERS_HPP
7#define MIMICPP_MATCHERS_GENERAL_MATCHERS_HPP
19#ifndef MIMICPP_DETAIL_IS_MODULE
22 #include <type_traits>
26namespace mimicpp::detail
28 template <
typename Arg,
typename MatchesProjection = std::
identity,
typename DescribeProjection = pr
inting::Pr
intFn>
31 using matches_reference = std::invoke_result_t<MatchesProjection, const Arg&>;
32 using describe_reference = std::invoke_result_t<DescribeProjection, const Arg&>;
36 decltype(
auto) as_matches_arg()
const noexcept(std::is_nothrow_invocable_v<MatchesProjection, const Arg&>)
38 return std::invoke(MatchesProjection{}, arg);
41 decltype(
auto) as_describe_arg()
const noexcept(std::is_nothrow_invocable_v<DescribeProjection, const Arg&>)
43 return std::invoke(DescribeProjection{}, arg);
50 using type = arg_storage<T>;
53 template <
typename Arg,
typename MatchesProjection,
typename DescribeProjection>
54 struct to_arg_storage<arg_storage<Arg, MatchesProjection, DescribeProjection>>
56 using type = arg_storage<Arg, MatchesProjection, DescribeProjection>;
60 using to_arg_storage_t =
typename to_arg_storage<T>::type;
71 template <
typename Predicate,
typename... AdditionalArgs>
72 requires std::is_move_constructible_v<Predicate>
73 && (... && std::is_move_constructible_v<AdditionalArgs>)
77 using storage_t = std::tuple<detail::to_arg_storage_t<AdditionalArgs>...>;
79 using matches_reference_t =
typename detail::to_arg_storage_t<T>::matches_reference;
87 std::tuple<AdditionalArgs...> additionalArgs = {})
89 std::is_nothrow_move_constructible_v<Predicate>
90 && (... && std::is_nothrow_move_constructible_v<AdditionalArgs>))
91 : m_Predicate{std::move(predicate)},
92 m_FormatString{std::move(fmt)},
93 m_InvertedFormatString{std::move(invertedFmt)},
94 m_AdditionalArgs{std::move(additionalArgs)}
98 template <
typename First,
typename... Others>
99 requires std::predicate<
103 matches_reference_t<AdditionalArgs>...>
107 Others&... others)
const
109 std::is_nothrow_invocable_v<
113 matches_reference_t<AdditionalArgs>...>)
116 [&,
this](
auto&... additionalArgs) {
121 additionalArgs.as_matches_arg()...);
130 [&,
this](
auto&... additionalArgs) {
131 return format::vformat(
133 format::make_format_args(
136 [](
auto&& val)
noexcept ->
const auto& {
return val; },
137 additionalArgs.as_describe_arg())...));
144 requires std::is_copy_constructible_v<Predicate>
145 && std::is_copy_constructible_v<storage_t>
147 return make_inverted(
149 m_InvertedFormatString,
157 return make_inverted(
158 std::move(m_Predicate),
159 std::move(m_InvertedFormatString),
160 std::move(m_FormatString),
161 std::move(m_AdditionalArgs));
165 [[no_unique_address]] Predicate m_Predicate;
168 storage_t m_AdditionalArgs;
170 template <
typename Fn>
172 static constexpr auto make_inverted(
178 using NotFnT =
decltype(std::not_fn(std::forward<Fn>(fn)));
180 std::not_fn(std::forward<Fn>(fn)),
182 std::move(invertedFmt),
196 static constexpr bool matches([[maybe_unused]]
auto&& target)
noexcept
202 static constexpr std::nullopt_t
describe() noexcept
213 template <
typename T>
217 template <
typename U>
221 static constexpr bool matches([[maybe_unused]]
auto&& target)
noexcept
227 static constexpr std::nullopt_t
describe() noexcept
296 template <
typename T>
304 template <
typename T>
306 constexpr auto eq(T&& value)
312 std::make_tuple(std::forward<T>(value))};
320 template <
typename T>
322 constexpr auto ne(T&& value)
328 std::make_tuple(std::forward<T>(value))};
336 template <
typename T>
338 constexpr auto lt(T&& value)
344 std::make_tuple(std::forward<T>(value))};
352 template <
typename T>
354 constexpr auto le(T&& value)
360 std::make_tuple(std::forward<T>(value))};
368 template <
typename T>
370 constexpr auto gt(T&& value)
376 std::make_tuple(std::forward<T>(value))};
384 template <
typename T>
386 constexpr auto ge(T&& value)
389 std::greater_equal{},
392 std::make_tuple(std::forward<T>(value))};
403 template <
typename UnaryPredicate>
408 StringViewT invertedDescription =
"fails predicate")
412 std::move(description),
413 std::move(invertedDescription),
423 template <util::satisfies<std::is_lvalue_reference> T>
428 []<
typename Other>(Other
const& target,
auto const* instancePtr)
noexcept
429 requires std::is_convertible_v<std::remove_cvref_t<T>
const volatile*, Other
const volatile*>
431 return std::addressof(target) == instancePtr;
434 "is not instance at {}",
435 std::make_tuple(std::addressof(
instance))};
#define MIMICPP_DETAIL_MODULE_EXPORT
Definition Config.hpp:19
Generic matcher and the basic building block of most of the built-in matchers.
Definition GeneralMatchers.hpp:75
constexpr bool matches(First &first, Others &... others) const noexcept(std::is_nothrow_invocable_v< const Predicate &, First &, Others &..., matches_reference_t< AdditionalArgs >... >)
Definition GeneralMatchers.hpp:105
constexpr auto operator!() const &
Definition GeneralMatchers.hpp:143
constexpr auto operator!() &&
Definition GeneralMatchers.hpp:155
constexpr StringT describe() const
Definition GeneralMatchers.hpp:127
constexpr PredicateMatcher(Predicate predicate, StringViewT fmt, StringViewT invertedFmt, std::tuple< AdditionalArgs... > additionalArgs={}) noexcept(std::is_nothrow_move_constructible_v< Predicate > &&(... &&std::is_nothrow_move_constructible_v< AdditionalArgs >))
Definition GeneralMatchers.hpp:83
Matcher, which can be used to disambiguate between similar overloads.
Definition GeneralMatchers.hpp:215
std::is_same< T, U > is_accepting
Definition GeneralMatchers.hpp:218
static constexpr std::nullopt_t describe() noexcept
Definition GeneralMatchers.hpp:227
static constexpr bool matches(auto &&target) noexcept
Definition GeneralMatchers.hpp:221
Matcher, which never fails.
Definition GeneralMatchers.hpp:193
static constexpr bool matches(auto &&target) noexcept
Definition GeneralMatchers.hpp:196
static constexpr std::nullopt_t describe() noexcept
Definition GeneralMatchers.hpp:202
constexpr auto lt(T &&value)
Tests, whether the target is less than the expected value.
Definition GeneralMatchers.hpp:338
constexpr auto instance(T &&instance)
Tests, whether the target is the expected instance.
Definition GeneralMatchers.hpp:425
constexpr auto le(T &&value)
Tests, whether the target is less than or equal to the expected value.
Definition GeneralMatchers.hpp:354
constexpr TypeMatcher< T > type
Matcher, which can be used as a last resort to disambiguate similar overloads.
Definition GeneralMatchers.hpp:297
constexpr WildcardMatcher _
The wildcard matcher, always matching.
Definition GeneralMatchers.hpp:289
constexpr auto eq(T &&value)
Tests, whether the target compares equal to the expected value.
Definition GeneralMatchers.hpp:306
constexpr auto gt(T &&value)
Tests, whether the target is greater than the expected value.
Definition GeneralMatchers.hpp:370
constexpr auto predicate(UnaryPredicate &&predicate, StringViewT description="passes predicate", StringViewT invertedDescription="fails predicate")
Tests, whether the target fulfills the given predicate.
Definition GeneralMatchers.hpp:405
constexpr auto ge(T &&value)
Tests, whether the target is greater than or equal to the expected value.
Definition GeneralMatchers.hpp:386
constexpr auto ne(T &&value)
Tests, whether the target compares not equal to the expected value.
Definition GeneralMatchers.hpp:322
Definition FloatingPointMatchers.hpp:25
std::basic_string_view< CharT, CharTraitsT > StringViewT
Definition Fwd.hpp:392
std::basic_string< CharT, CharTraitsT > StringT
Definition Fwd.hpp:391