Skip to content

Commit

Permalink
🔥 🎨 [ut] Remove matcher operator
Browse files Browse the repository at this point in the history
Problem:
- `matcher` operator is not required.

Solution:
- Remove it and update examples without using it.
  • Loading branch information
kris-jusiak authored and krzysztof-jusiak committed Jan 6, 2020
1 parent 9c61945 commit cb8a12a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 55 deletions.
20 changes: 2 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -756,9 +756,9 @@ asserts: 1 | 0 passed | 1 failed
```cpp
"matchers"_test = [] {
constexpr auto is_between = [](auto lhs, auto rhs) {
return matcher([=](auto value) {
return [=](auto value) {
return that % value >= lhs and that % value <= rhs;
});
};
};

expect(is_between(1, 100)(42));
Expand Down Expand Up @@ -973,22 +973,6 @@ namespace boost::ut::inline v1_1_5 {
[[nodiscard]] constexpr auto operator%(Expression expr) const;
} that{};
/**
* @example auto gt_0 = matcher([](auto arg){ return that % arg > 0; })
*/
struct matcher {
/**
* @param expr matcher expression
*/
constexpr explicit(true) matcher(Expression expr);
/**
* Executes matcher expression
* @param args arguments to be passed to a matcher expression
*/
[[nodiscard]] constexpr auto operator()(const Args&... args) const;
};
inline namespace literals {
/**
* User defined literals to represent constant values
Expand Down
36 changes: 20 additions & 16 deletions example/matcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@
#include <tuple>
#include <type_traits>

struct expr {
const bool result{};
const std::string str{};

constexpr explicit operator bool() const { return result; }
friend auto operator<<(std::ostream& os, const expr& self) -> std::ostream& {
return (os << self.str);
class matcher : boost::ut::detail::op {
public:
matcher(bool result, const std::string& str) : result_{result}, str_{str} {}
constexpr explicit operator bool() const { return result_; }
friend auto operator<<(std::ostream& os, const matcher& self)
-> std::ostream& {
return (os << self.str_);
}

private:
const bool result_{};
const std::string str_{};
};

template <class... Ts>
Expand Down Expand Up @@ -50,26 +54,26 @@ int main() {

"matcher"_test = [] {
constexpr auto is_between = [](auto lhs, auto rhs) {
return matcher([=](auto value) {
return [=](auto value) {
return that % value >= lhs and that % value <= rhs;
});
};
};

constexpr auto ends_with = matcher([](const auto& arg, const auto& ext) {
constexpr auto ends_with = [](const auto& arg, const auto& ext) {
std::stringstream str{};
str << '(' << arg << " ends with " << ext << ')';
if (ext.size() > arg.size()) {
return expr{{}, str.str()};
return matcher{{}, str.str()};
}
return expr{std::equal(ext.rbegin(), ext.rend(), arg.rbegin()),
str.str()};
});
return matcher{std::equal(ext.rbegin(), ext.rend(), arg.rbegin()),
str.str()};
};

auto value = 42;
auto str = "example.test"sv;

expect(is_between(1, 100)(value) and ends_with(str, ".test"sv));
expect(not is_between(1, 100)(0));
expect(is_between(1, 100)(value) and not is_between(1, 100)(0));
expect(ends_with(str, ".test"sv));
expect(any_of{1, 2, 3} == 2 or any_of{42, 43} == 44);
};
}
19 changes: 0 additions & 19 deletions include/boost/ut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1658,20 +1658,6 @@ class expect_ {
private:
bool result_{}, fatal_{};
};

template <class TExpr>
class matcher_ : op {
public:
constexpr explicit matcher_(const TExpr& expr) : expr_{expr} {}

template <class... TArgs>
[[nodiscard]] constexpr auto operator()(const TArgs&... args) const {
return expr_(args...);
}

private:
TExpr expr_{};
};
} // namespace detail

namespace literals {
Expand Down Expand Up @@ -1889,11 +1875,6 @@ template <bool Constant>
#endif
constexpr auto constant = Constant;

template <class TExpr>
[[nodiscard]] constexpr auto matcher(const TExpr& expr) {
return detail::matcher_<TExpr>{expr};
}

#if defined(__cpp_exceptions)
template <class TException, class TExpr>
[[nodiscard]] constexpr auto throws(const TExpr& expr) {
Expand Down
4 changes: 2 additions & 2 deletions test/ut/ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ constexpr auto test_assert =
if (not result) {
std::cerr << sl.file_name() << ':' << sl.line() << ":FAILED"
<< std::endl;
throw;
std::abort();
}
};

Expand Down Expand Up @@ -788,7 +788,7 @@ int main() {
test_cfg = fake_cfg{};

constexpr auto is_gt = [](auto N) {
return matcher([=](auto value) { return that % value > N; });
return [=](auto value) { return that % value > N; };
};

expect(is_gt(42)(99));
Expand Down

0 comments on commit cb8a12a

Please sign in to comment.