Skip to content

Commit

Permalink
etest: Print args using to_string if available
Browse files Browse the repository at this point in the history
  • Loading branch information
robinlinden committed Jan 19, 2025
1 parent 1a90db1 commit d96c54b
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions etest/etest2.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2021-2024 Robin Lindén <[email protected]>
// SPDX-FileCopyrightText: 2021-2025 Robin Lindén <[email protected]>
//
// SPDX-License-Identifier: BSD-2-Clause

Expand All @@ -20,10 +20,35 @@
namespace etest {

template<typename T>
concept Printable = requires(std::ostream &os, T t) {
concept Ostreamable = requires(std::ostream &os, T t) {
{ os << t } -> std::same_as<std::ostream &>;
};

template<typename T>
concept HasToString = requires(T t) {
{ to_string(t) } -> std::convertible_to<std::string_view>;
};

template<typename T>
concept Printable = Ostreamable<T> || HasToString<T>;

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>) {
os << a;
} else {
os << to_string(a);
}

os << ' ' << actual_op << '\n';

if constexpr (Ostreamable<U>) {
os << b;
} else {
os << to_string(b);
}
}

struct RunOptions {
bool run_disabled_tests{false};
std::optional<unsigned> rng_seed;
Expand Down Expand Up @@ -71,7 +96,7 @@ class IActions {
}

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

Expand All @@ -94,7 +119,7 @@ class IActions {
}

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

Expand Down

0 comments on commit d96c54b

Please sign in to comment.