srctree

Robin Linden parent e0ef6ddb dd11b09d
etest: Support printing the objects that compared unequal

etest/etest.cpp added: 93, removed: 5, total 88
@@ -87,4 +87,8 @@ void require(bool b, etest::source_location const &loc) {
}
}
 
std::ostream &log() {
return test_log;
}
 
} // namespace etest
 
etest/etest.h added: 93, removed: 5, total 88
@@ -7,11 +7,18 @@
 
#include "etest/cxx_compat.h"
 
#include <concepts>
#include <functional>
#include <ostream>
#include <string_view>
 
namespace etest {
 
template<typename T>
concept Printable = requires(std::ostream &os, T t) {
{ os << t } -> std::same_as<std::ostream &>;
};
 
int run_all_tests() noexcept;
int test(std::string_view name, std::function<void()> body) noexcept;
 
@@ -21,6 +28,41 @@ void expect(bool, etest::source_location const &loc = etest::source_location::cu
// Hard test requirement. Stops the test (using an exception) if the check fails.
void require(bool, etest::source_location const &loc = etest::source_location::current());
 
// Access the internal test log.
std::ostream &log();
 
// Weak test requirement. Prints the types compared on failure (if printable).
template<Printable T, Printable U>
void expect_eq(T const &a, U const &b, etest::source_location const &loc = etest::source_location::current()) noexcept {
if (a != b) {
expect(false, loc);
log() << a << " !=\n" << b << "\n\n";
}
}
 
template<typename T, typename U>
void expect_eq(T const &a, U const &b, etest::source_location const &loc = etest::source_location::current()) noexcept {
expect(a == b, loc);
}
 
// Hard test requirement. Prints the types compared on failure (if printable).
template<Printable T, Printable U>
void require_eq(T const &a, U const &b, etest::source_location const &loc = etest::source_location::current()) {
if (a != b) {
try {
require(false, loc);
} catch (...) {
log() << a << " !=\n" << b << "\n\n";
throw;
}
}
}
 
template<typename T, typename U>
void require_eq(T const &a, U const &b, etest::source_location const &loc = etest::source_location::current()) {
require(a == b, loc);
}
 
} // namespace etest
 
#endif
 
filename was Deleted added: 93, removed: 5, total 88
@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2021 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
#include "etest/etest.h"
 
int main() {
etest::test("this should fail", [] { etest::expect_eq(1, 2); });
 
// Invert to return success on failure.
return !etest::run_all_tests();
}
 
filename was Deleted added: 93, removed: 5, total 88
@@ -0,0 +1,17 @@
// SPDX-FileCopyrightText: 2021 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
#include "etest/etest.h"
 
#include <cstdlib>
 
int main() {
etest::test("this should fail", [] {
etest::require_eq(1, 2);
std::exit(1); // Exit w/ failure if this line runs.
});
 
// Invert to return success on failure.
return !etest::run_all_tests();
}
 
filename was Deleted added: 93, removed: 5, total 88
@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2021 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
#include "etest/etest.h"
 
int main() {
etest::test("expect", [] { etest::expect(true); });
etest::test("expect_eq", [] { etest::expect_eq(1, 1); });
etest::test("require", [] { etest::require(true); });
etest::test("require_eq", [] { etest::require_eq(1, 1); });
return etest::run_all_tests();
}