Skip to content

Commit

Permalink
🆕 [expect] Expose alternative eq/neq/gt/ge/lt/le for comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
kris-jusiak authored and krzysztof-jusiak committed Nov 20, 2019
1 parent d6ce85b commit 028f2af
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -79,6 +79,13 @@ All tests passed (3 asserts in 1 tests)
expect(that % 1 != 2 or 2_i > 3);
};

"eq/neq/gt/ge/lt/le"_test = [] {
expect(eq(42, sum(40, 2)));
expect(neq(1, 2));
expect(eq(sum(1), 1) and neq(sum(1, 2), 2));
expect(eq(1, 1) and that % 1 == 1 and 1_i == 1);
};

"floating points"_test = [] {
expect(42.1_d == 42.101) << "epsilon=0.1";
expect(42.10_d == 42.101) << "epsilon=0.01";
Expand Down
9 changes: 8 additions & 1 deletion example/expect.cpp
Expand Up @@ -23,6 +23,8 @@ int main() {
expect(sum(1) <= 1_i);
};

"message"_test = [] { expect(3_i == sum(1, 2)) << "wrong sum"; };

"expressions"_test = [] {
expect(0_i == sum() and 42_i == sum(40, 2));
expect(1_i == sum() or 0_i == sum());
Expand All @@ -35,7 +37,12 @@ int main() {
expect(that % 1 != 2 or 2_i > 3);
};

"message"_test = [] { expect(3_i == sum(1, 2)) << "wrong sum"; };
"eq/neq/gt/ge/lt/le"_test = [] {
expect(eq(42, sum(40, 2)));
expect(neq(1, 2));
expect(eq(sum(1), 1) and neq(sum(1, 2), 2));
expect(eq(1, 1) and that % 1 == 1 and 1_i == 1);
};

"floating points"_test = [] {
expect(42.1_d == 42.101) << "epsilon=0.1";
Expand Down
25 changes: 25 additions & 0 deletions include/boost/ut.hpp
Expand Up @@ -1790,6 +1790,31 @@ struct suite {
template <class T>
[[maybe_unused]] constexpr auto type = detail::type_<T>();

template <class TLhs, class TRhs>
constexpr auto eq(const TLhs& lhs, const TRhs& rhs) {
return detail::eq_{lhs, rhs};
}
template <class TLhs, class TRhs>
constexpr auto neq(const TLhs& lhs, const TRhs& rhs) {
return detail::neq_{lhs, rhs};
}
template <class TLhs, class TRhs>
constexpr auto gt(const TLhs& lhs, const TRhs& rhs) {
return detail::gt_{lhs, rhs};
}
template <class TLhs, class TRhs>
constexpr auto ge(const TLhs& lhs, const TRhs& rhs) {
return detail::ge_{lhs, rhs};
}
template <class TLhs, class TRhs>
constexpr auto lt(const TLhs& lhs, const TRhs& rhs) {
return detail::lt_{lhs, rhs};
}
template <class TLhs, class TRhs>
constexpr auto le(const TLhs& lhs, const TRhs& rhs) {
return detail::le_{lhs, rhs};
}

using literals::operator""_test;
using literals::operator""_i;
using literals::operator""_s;
Expand Down
26 changes: 26 additions & 0 deletions test/ut/ut.cpp
Expand Up @@ -682,6 +682,32 @@ int main() {
test_assert(not test_cfg.assertion_calls[2].result);
}

{
test_cfg = fake_cfg{};

expect(eq(42, 42));

test_assert(1 == std::size(test_cfg.assertion_calls));
test_assert("42 == 42" == test_cfg.assertion_calls[0].str);
test_assert(test_cfg.assertion_calls[0].result);
test_assert(0 == std::size(test_cfg.log_calls));

expect(eq(1, 2) or gt(3, 7)) << "msg";
test_assert(2 == std::size(test_cfg.assertion_calls));
test_assert("(1 == 2 or 3 > 7)" == test_cfg.assertion_calls[1].str);
test_assert(not test_cfg.assertion_calls[1].result);
test_assert(2 == std::size(test_cfg.log_calls));
test_assert(' ' == std::any_cast<char>(test_cfg.log_calls[0]));
test_assert("msg"sv == std::any_cast<const char*>(test_cfg.log_calls[1]));

const auto f = false;
expect(3_i > 5 and eq(not true, f));
test_assert(3 == std::size(test_cfg.assertion_calls));
test_assert("(3 > 5 and false == false)" ==
test_cfg.assertion_calls[2].str);
test_assert(not test_cfg.assertion_calls[2].result);
}

{
test_cfg = fake_cfg{};

Expand Down

0 comments on commit 028f2af

Please sign in to comment.