-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
etest: Print args using to_string if available
- Loading branch information
1 parent
1a90db1
commit d96c54b
Showing
1 changed file
with
29 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
@@ -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; | ||
|
@@ -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); | ||
} | ||
|
||
|
@@ -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); | ||
} | ||
|
||
|