Skip to content

Commit

Permalink
etest: Don't require separate overloads for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
robinlinden committed Jan 19, 2025
1 parent d96c54b commit 270acb2
Showing 1 changed file with 9 additions and 24 deletions.
33 changes: 9 additions & 24 deletions etest/etest2.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ concept HasToString = requires(T t) {
template<typename T>
concept Printable = Ostreamable<T> || HasToString<T>;

template<typename T, typename U>
void print_to(std::ostream &, std::string_view, T const &, U const &) {}

Check warning on line 36 in etest/etest2.h

View check run for this annotation

Codecov / codecov/patch

etest/etest2.h#L36

Added line #L36 was not covered by tests

template<Printable T, Printable U>
void print_to(std::ostream &os, std::string_view actual_op, T const &a, U const &b) {
if constexpr (Ostreamable<T>) {
Expand Down Expand Up @@ -86,9 +89,8 @@ class IActions {
}

// Weak test requirement. Prints the types compared on failure (if printable).
template<Printable T, Printable U>
constexpr void expect_eq(T const &a,
U const &b,
constexpr void expect_eq(auto const &a,
auto const &b,
std::optional<std::string_view> log_message = std::nullopt,
std::source_location const &loc = std::source_location::current()) noexcept {
if (a == b) {
Expand All @@ -97,21 +99,12 @@ class IActions {

std::stringstream ss;
print_to(ss, "!=", a, b);
expect(false, log_message ? std::move(log_message) : std::move(ss).str(), loc);
}

template<typename T, typename U>
constexpr void expect_eq(T const &a,
U const &b,
std::optional<std::string_view> log_message = std::nullopt,
std::source_location const &loc = std::source_location::current()) noexcept {
expect(a == b, std::move(log_message), loc);
expect(false, log_message || ss.view().empty() ? std::move(log_message) : std::move(ss).str(), loc);
}

// Hard test requirement. Prints the types compared on failure (if printable).
template<Printable T, Printable U>
constexpr void require_eq(T const &a,
U const &b,
constexpr void require_eq(auto const &a,
auto const &b,
std::optional<std::string_view> log_message = std::nullopt,
std::source_location const &loc = std::source_location::current()) {
if (a == b) {
Expand All @@ -120,15 +113,7 @@ class IActions {

std::stringstream ss;
print_to(ss, "!=", a, b);
require(false, log_message ? std::move(log_message) : std::move(ss).str(), loc);
}

template<typename T, typename U>
constexpr void require_eq(T const &a,
U const &b,
std::optional<std::string_view> log_message = std::nullopt,
std::source_location const &loc = std::source_location::current()) {
require(a == b, std::move(log_message), loc);
require(false, log_message || ss.view().empty() ? std::move(log_message) : std::move(ss).str(), loc);
}
};

Expand Down

0 comments on commit 270acb2

Please sign in to comment.