srctree

Robin Linden parent f0763134 abb09c0a
etest: Clean up compatibility with less blessed stdlib versions

inlinesplit
etest/BUILD added: 53, removed: 43, total 10
@@ -3,7 +3,10 @@ load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
cc_library(
name = "etest",
srcs = ["etest.cpp"],
hdrs = ["etest.h"],
hdrs = [
"cxx_compat.h",
"etest.h",
],
visibility = ["//visibility:public"],
)
 
 
filename was Deleted added: 53, removed: 43, total 10
@@ -0,0 +1,27 @@
#ifndef ETEST_CXX_COMPAT_H_
#define ETEST_CXX_COMPAT_H_
 
// Clang has the header, but it doesn't yet contain std::source_location.
#if __has_include(<source_location>) && !defined(__clang__)
 
#include <source_location>
namespace etest {
using source_location = std::source_location;
} // namespace etest
 
#else
 
#include <cstdint>
namespace etest {
struct source_location {
static consteval source_location current() noexcept { return source_location{}; }
constexpr std::uint_least32_t line() const noexcept { return 0; }
constexpr std::uint_least32_t column() const noexcept { return 0; }
constexpr char const *file_name() const noexcept { return ""; }
constexpr char const *function_name() const noexcept { return ""; }
};
} // namespace etest
 
#endif
 
#endif
 
etest/etest.cpp added: 53, removed: 43, total 10
@@ -61,34 +61,29 @@ int test(std::string_view name, std::function<void()> body) noexcept {
return 0;
}
 
#ifndef ETEST_WITH_SRC_LOC
void expect(bool b) noexcept {
if (!b) { ++assertion_failures; }
}
 
void require(bool b) {
if (!b) { throw test_failure{}; }
}
#else
void expect(bool b, std::source_location const &loc) noexcept {
void expect(bool b, etest::source_location const &loc) noexcept {
if (!b) {
++assertion_failures;
test_log << " expectation failure at "
<< loc.file_name() << "("
<< loc.line() << ":"
<< loc.column() << ")\n";
// Check if we're using the real source_location by checking for line == 0.
if (loc.line() != 0) {
test_log << " expectation failure at "
<< loc.file_name() << "("
<< loc.line() << ":"
<< loc.column() << ")\n";
}
}
}
 
void require(bool b, std::source_location const &loc) {
void require(bool b, etest::source_location const &loc) {
if (!b) {
test_log << " requirement failure at "
<< loc.file_name() << "("
<< loc.line() << ":"
<< loc.column() << ")\n";
if (loc.line() != 0) {
test_log << " requirement failure at "
<< loc.file_name() << "("
<< loc.line() << ":"
<< loc.column() << ")\n";
}
throw test_failure{};
}
}
#endif
 
} // namespace etest
 
etest/etest.h added: 53, removed: 43, total 10
@@ -1,36 +1,21 @@
#ifndef ETEST_TEST_H_
#define ETEST_TEST_H_
 
// Clang has the header, but it doesn't yet contain std::source_location.
#if __has_include(<source_location>) && !defined(__clang__)
#define ETEST_WITH_SRC_LOC
#endif
#include "etest/cxx_compat.h"
 
#include <functional>
#include <string_view>
 
#ifdef ETEST_WITH_SRC_LOC
#include <source_location>
#endif
 
namespace etest {
 
int run_all_tests() noexcept;
int test(std::string_view name, std::function<void()> body) noexcept;
 
// Weak test requirement. Allows the test to continue even if the check fails.
#ifndef ETEST_WITH_SRC_LOC
void expect(bool) noexcept;
#else
void expect(bool, std::source_location const &loc = std::source_location::current()) noexcept;
#endif
void expect(bool, etest::source_location const &loc = etest::source_location::current()) noexcept;
 
// Hard test requirement. Stops the test (using an exception) if the check fails.
#ifndef ETEST_WITH_SRC_LOC
void require(bool);
#else
void require(bool, std::source_location const &loc = std::source_location::current());
#endif
void require(bool, etest::source_location const &loc = etest::source_location::current());
 
} // namespace etest