Skip to content

Commit

Permalink
🎨 [example] any_of matcher example
Browse files Browse the repository at this point in the history
Problem:
- Matcher example doesn't show how to make a custom UT operator.

Solution:
- Add `any_of` matcher which demonstrates how to use `ut::op` in order to create a custom matcher.
  • Loading branch information
kris-jusiak committed Jan 6, 2020
1 parent bca7b97 commit 9c61945
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions example/matcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <algorithm>
#include <boost/ut.hpp>
#include <string_view>
#include <tuple>
#include <type_traits>

struct expr {
const bool result{};
Expand All @@ -19,6 +21,29 @@ struct expr {
}
};

template <class... Ts>
class any_of {
public:
constexpr explicit any_of(Ts... ts) : ts_{ts...} {}

constexpr auto operator==(std::common_type_t<Ts...> t) const {
return std::apply([t](auto... args) { return eq(t, args...); }, ts_);
}

private:
template <class T, class U, class... TArgs>
static constexpr auto eq(const T& t, const U& u, const TArgs&... args) {
using namespace boost::ut;
if constexpr (sizeof...(args) > 0) {
return (that % detail::value{u} == t) or eq(t, args...);
} else {
return (that % detail::value{u} == t);
}
}

std::tuple<Ts...> ts_;
};

int main() {
using namespace boost::ut;
using namespace std::literals::string_view_literals;
Expand All @@ -45,5 +70,6 @@ int main() {

expect(is_between(1, 100)(value) and ends_with(str, ".test"sv));
expect(not is_between(1, 100)(0));
expect(any_of{1, 2, 3} == 2 or any_of{42, 43} == 44);
};
}

0 comments on commit 9c61945

Please sign in to comment.