6#ifndef MIMICPP_MATCHERS_GENERAL_MATCHERS_HPP
7#define MIMICPP_MATCHERS_GENERAL_MATCHERS_HPP
19namespace mimicpp::detail
21 template <
typename Arg,
typename MatchesProjection = std::
identity,
typename DescribeProjection = Pr
intFn>
24 using matches_reference = std::invoke_result_t<MatchesProjection, const Arg&>;
25 using describe_reference = std::invoke_result_t<DescribeProjection, const Arg&>;
29 decltype(
auto) as_matches_arg()
const noexcept(std::is_nothrow_invocable_v<MatchesProjection, const Arg&>)
31 return std::invoke(MatchesProjection{}, arg);
34 decltype(
auto) as_describe_arg()
const noexcept(std::is_nothrow_invocable_v<DescribeProjection, const Arg&>)
36 return std::invoke(DescribeProjection{}, arg);
43 using type = arg_storage<T>;
46 template <
typename Arg,
typename MatchesProjection,
typename DescribeProjection>
47 struct to_arg_storage<arg_storage<Arg, MatchesProjection, DescribeProjection>>
49 using type = arg_storage<Arg, MatchesProjection, DescribeProjection>;
53 using to_arg_storage_t =
typename to_arg_storage<T>::type;
64 template <
typename Predicate,
typename... AdditionalArgs>
65 requires std::is_move_constructible_v<Predicate>
66 && (... && std::is_move_constructible_v<AdditionalArgs>)
70 using storage_t = std::tuple<detail::to_arg_storage_t<AdditionalArgs>...>;
72 using matches_reference_t =
typename detail::to_arg_storage_t<T>::matches_reference;
80 std::tuple<AdditionalArgs...> additionalArgs = {})
82 std::is_nothrow_move_constructible_v<Predicate>
83 && (... && std::is_nothrow_move_constructible_v<AdditionalArgs>))
84 : m_Predicate{std::move(predicate)},
85 m_FormatString{std::move(fmt)},
86 m_InvertedFormatString{std::move(invertedFmt)},
87 m_AdditionalArgs{std::move(additionalArgs)}
91 template <
typename First,
typename... Others>
92 requires std::predicate<
96 matches_reference_t<AdditionalArgs>...>
100 Others&... others)
const
102 std::is_nothrow_invocable_v<
106 matches_reference_t<AdditionalArgs>...>)
109 [&,
this](
auto&... additionalArgs) {
114 additionalArgs.as_matches_arg()...);
123 [&,
this](
auto&... additionalArgs) {
124 return format::vformat(
126 format::make_format_args(
129 [](
auto&& val)
noexcept ->
const auto& {
return val; },
130 additionalArgs.as_describe_arg())...));
137 requires std::is_copy_constructible_v<Predicate>
138 && std::is_copy_constructible_v<storage_t>
140 return make_inverted(
142 m_InvertedFormatString,
150 return make_inverted(
151 std::move(m_Predicate),
152 std::move(m_InvertedFormatString),
153 std::move(m_FormatString),
154 std::move(m_AdditionalArgs));
158 [[no_unique_address]] Predicate m_Predicate;
161 storage_t m_AdditionalArgs;
163 template <
typename Fn>
165 static constexpr auto make_inverted(
171 using NotFnT =
decltype(std::not_fn(std::forward<Fn>(fn)));
173 std::not_fn(std::forward<Fn>(fn)),
175 std::move(invertedFmt),
188 static constexpr bool matches([[maybe_unused]]
auto&& target)
noexcept
195 return "has no constraints";
245 template <
typename T>
247 constexpr auto eq(T&& value)
253 std::make_tuple(std::forward<T>(value))};
261 template <
typename T>
263 constexpr auto ne(T&& value)
269 std::make_tuple(std::forward<T>(value))};
277 template <
typename T>
279 constexpr auto lt(T&& value)
285 std::make_tuple(std::forward<T>(value))};
293 template <
typename T>
295 constexpr auto le(T&& value)
301 std::make_tuple(std::forward<T>(value))};
309 template <
typename T>
311 constexpr auto gt(T&& value)
317 std::make_tuple(std::forward<T>(value))};
325 template <
typename T>
327 constexpr auto ge(T&& value)
330 std::greater_equal{},
333 std::make_tuple(std::forward<T>(value))};
344 template <
typename UnaryPredicate>
349 StringViewT invertedDescription =
"fails predicate")
353 std::move(description),
354 std::move(invertedDescription),
364 template <satisfies<std::is_lvalue_reference> T>
369 [](
const std::remove_cvref_t<T>& target,
const auto* instancePtr)
noexcept {
370 return std::addressof(target) == instancePtr;
373 "is not instance at {}",
374 std::make_tuple(std::addressof(
instance))};
Generic matcher and the basic building block of most of the built-in matchers.
Definition GeneralMatchers.hpp:68
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:98
constexpr auto operator!() const &
Definition GeneralMatchers.hpp:136
constexpr auto operator!() &&
Definition GeneralMatchers.hpp:148
constexpr StringT describe() const
Definition GeneralMatchers.hpp:120
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:76
Matcher, which never fails.
Definition GeneralMatchers.hpp:186
static constexpr bool matches(auto &&target) noexcept
Definition GeneralMatchers.hpp:188
static constexpr StringViewT describe() noexcept
Definition GeneralMatchers.hpp:193
constexpr auto lt(T &&value)
Tests, whether the target is less than the expected value.
Definition GeneralMatchers.hpp:279
constexpr auto instance(T &&instance)
Tests, whether the target is the expected instance.
Definition GeneralMatchers.hpp:366
constexpr auto le(T &&value)
Tests, whether the target is less than or equal to the expected value.
Definition GeneralMatchers.hpp:295
constexpr WildcardMatcher _
The wildcard matcher, always matching.
Definition GeneralMatchers.hpp:238
constexpr auto eq(T &&value)
Tests, whether the target compares equal to the expected value.
Definition GeneralMatchers.hpp:247
constexpr auto gt(T &&value)
Tests, whether the target is greater than the expected value.
Definition GeneralMatchers.hpp:311
constexpr auto predicate(UnaryPredicate &&predicate, StringViewT description="passes predicate", StringViewT invertedDescription="fails predicate")
Tests, whether the target fulfills the given predicate.
Definition GeneralMatchers.hpp:346
constexpr auto ge(T &&value)
Tests, whether the target is greater than or equal to the expected value.
Definition GeneralMatchers.hpp:327
constexpr auto ne(T &&value)
Tests, whether the target compares not equal to the expected value.
Definition GeneralMatchers.hpp:263
Definition FloatingPointMatchers.hpp:22
Definition BoostTest.hpp:20
std::basic_string_view< CharT, CharTraitsT > StringViewT
Definition Fwd.hpp:345
std::basic_string< CharT, CharTraitsT > StringT
Definition Fwd.hpp:344