Add Hash, Random, Uuid, Encoding, Bits, Math modules + make Testing deps optional
New header-only modules: - Void::Hash — FNV-1a 32/64, CRC-32, DJB2, MurmurHash3-32, UDL literals - Void::Random — Xoshiro256** PRNG, uniform_int/real, coin_flip, shuffle, random_string - Void::Uuid — v4 (random), v5_lite (name-based), parse, RFC 4122 namespaces - Void::Encoding — Base64 (standard + URL-safe), Hex encode/decode - Void::Bits — popcount, bit extract/deposit, rotation, align, byteswap, masks - Void::Math — Vec2/Vec3/Vec4, Mat4 (identity, translation, scale, rotation) Testing module: move Stacktrace from INTERNAL_DEPS to OPTIONAL_DEPS. Added Detail/Color.hpp shim that delegates to Stacktrace::Color when present or provides inline ANSI fallback otherwise. All Stacktrace calls in SignalHandler, ConsoleReporter, and Runner are gated on VOID_HAS_STACKTRACE. Fix Repr.hpp reflect_struct to handle template specializations that lack std::meta::identifier_of (now guarded by has_identifier check). 33/33 tests pass. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
42268c6c35
commit
a09e000f0f
65 files changed
+4959
-138
No files matched your search
@@ -0,0 +1,3 @@
|
||||
void_add_module(Concurrency
|
||||
INTERNAL_DEPS Log
|
||||
)
|
||||
@@ -0,0 +1,3 @@
|
||||
void_add_module(Configuration
|
||||
INTERNAL_DEPS Result FileSystem Log
|
||||
)
|
||||
@@ -1,3 +1,3 @@
|
||||
void_add_module(FileSystem
|
||||
INTERNAL_DEPS Result
|
||||
INTERNAL_DEPS Result Log
|
||||
)
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <Void/Log/Log.hpp>
|
||||
#include <mutex>
|
||||
#include <shared_mutex>
|
||||
|
||||
namespace Void::Log
|
||||
{
|
||||
|
||||
namespace
|
||||
{
|
||||
Sink g_sink;
|
||||
std::shared_mutex g_mutex;
|
||||
} // namespace
|
||||
|
||||
void set_sink(Sink sink)
|
||||
{
|
||||
std::unique_lock lock{g_mutex};
|
||||
g_sink = std::move(sink);
|
||||
}
|
||||
|
||||
void log(Level level, std::string_view module, std::string_view message)
|
||||
{
|
||||
std::shared_lock lock{g_mutex};
|
||||
if (g_sink) {
|
||||
g_sink(level, module, message);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Void::Log
|
||||
@@ -0,0 +1 @@
|
||||
void_add_module(Log)
|
||||
@@ -1,3 +1,4 @@
|
||||
#include <Void/Detail/Enum.hpp>
|
||||
#include <Void/Logging/ConsoleLogger.hpp>
|
||||
|
||||
#ifdef VOID_HAS_STACKTRACE
|
||||
@@ -19,6 +20,28 @@ namespace Void::Logging
|
||||
std::string_view color_for(Level level)
|
||||
{
|
||||
using namespace Void::Stacktrace::Color;
|
||||
#ifdef VOID_HAS_ENUM_REFLECTION
|
||||
static constexpr auto enumerators =
|
||||
std::define_static_array(std::meta::enumerators_of(^^Level));
|
||||
template for (constexpr auto e : enumerators) {
|
||||
if (level == [:e:]) {
|
||||
if constexpr (std::meta::identifier_of(e) == "Trace")
|
||||
return dim;
|
||||
if constexpr (std::meta::identifier_of(e) == "Debug")
|
||||
return cyan;
|
||||
if constexpr (std::meta::identifier_of(e) == "Info")
|
||||
return bold_cyan;
|
||||
if constexpr (std::meta::identifier_of(e) == "Warning")
|
||||
return bold_yellow;
|
||||
if constexpr (std::meta::identifier_of(e) == "Error")
|
||||
return bold_red;
|
||||
if constexpr (std::meta::identifier_of(e) == "Fatal")
|
||||
return bold_magenta;
|
||||
return reset;
|
||||
}
|
||||
}
|
||||
return reset;
|
||||
#else
|
||||
switch (level) {
|
||||
case Level::Trace:
|
||||
return dim;
|
||||
@@ -35,6 +58,7 @@ namespace Void::Logging
|
||||
default:
|
||||
return reset;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <Void/Logging/FileLogger.hpp>
|
||||
#include <Void/Log/Log.hpp>
|
||||
|
||||
#include <format>
|
||||
#include <stdexcept>
|
||||
@@ -9,12 +10,17 @@ namespace Void::Logging
|
||||
FileLogger::FileLogger(const std::filesystem::path& path,
|
||||
Mode mode,
|
||||
std::size_t capacity)
|
||||
: Logger(mode, capacity), m_file(path, std::ios::app)
|
||||
: Logger(mode, capacity), m_path(path), m_file(path, std::ios::app)
|
||||
{
|
||||
if (!m_file.is_open()) {
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Warning, "Logging",
|
||||
std::format("FileLogger: cannot open '{}'", path.string()));
|
||||
throw std::runtime_error(
|
||||
std::format("FileLogger: cannot open '{}'", path.string()));
|
||||
}
|
||||
Void::Log::log(Void::Log::Level::Debug, "Logging",
|
||||
std::format("FileLogger: opened '{}'", path.string()));
|
||||
}
|
||||
|
||||
Void::Result::Result<std::unique_ptr<FileLogger>, Void::Result::Error>
|
||||
@@ -30,7 +36,13 @@ namespace Void::Logging
|
||||
}
|
||||
}
|
||||
|
||||
FileLogger::~FileLogger() noexcept { flush(); }
|
||||
FileLogger::~FileLogger() noexcept
|
||||
{
|
||||
flush();
|
||||
Void::Log::log(Void::Log::Level::Debug, "Logging",
|
||||
std::format("FileLogger: closed '{}'",
|
||||
m_path.string()));
|
||||
}
|
||||
|
||||
void FileLogger::write(const Entry& entry)
|
||||
{
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
void_add_module(Logging
|
||||
INTERNAL_DEPS Result
|
||||
INTERNAL_DEPS Result Log
|
||||
OPTIONAL_DEPS Stacktrace
|
||||
)
|
||||
@@ -0,0 +1,253 @@
|
||||
#include <Void/Process/Process.hpp>
|
||||
#include <Void/Log/Log.hpp>
|
||||
|
||||
#include <format>
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
namespace Void::Process
|
||||
{
|
||||
Void::Result::Result<Output, Void::Result::Error>
|
||||
run(const std::filesystem::path&,
|
||||
std::vector<std::string>,
|
||||
std::string_view,
|
||||
std::chrono::milliseconds)
|
||||
{
|
||||
return Void::Result::fail(
|
||||
"Process::run: not implemented on Windows");
|
||||
}
|
||||
|
||||
Void::Result::Result<Output, Void::Result::Error>
|
||||
shell(std::string_view, std::chrono::milliseconds)
|
||||
{
|
||||
return Void::Result::fail(
|
||||
"Process::shell: not implemented on Windows");
|
||||
}
|
||||
} // namespace Void::Process
|
||||
|
||||
#else // POSIX
|
||||
|
||||
#include <array>
|
||||
#include <cerrno>
|
||||
#include <csignal>
|
||||
#include <cstring>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <poll.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace Void::Process
|
||||
{
|
||||
|
||||
namespace
|
||||
{
|
||||
// Read into buf until EAGAIN (no more data right now) or EOF.
|
||||
// Returns true on EOF, false on EAGAIN.
|
||||
bool drain(int fd, std::string& buf)
|
||||
{
|
||||
char tmp[4096];
|
||||
for (;;) {
|
||||
ssize_t n = ::read(fd, tmp, sizeof(tmp));
|
||||
if (n > 0) {
|
||||
buf.append(tmp, static_cast<std::size_t>(n));
|
||||
} else if (n == 0) {
|
||||
return true; // EOF
|
||||
} else {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
// EAGAIN / EWOULDBLOCK: no data right now
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void set_nonblock(int fd)
|
||||
{
|
||||
int flags = ::fcntl(fd, F_GETFL, 0);
|
||||
if (flags >= 0) {
|
||||
::fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
||||
}
|
||||
}
|
||||
|
||||
// Close an fd if it is >= 0, then set it to -1.
|
||||
void safe_close(int& fd)
|
||||
{
|
||||
if (fd >= 0) {
|
||||
::close(fd);
|
||||
fd = -1;
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Void::Result::Result<Output, Void::Result::Error>
|
||||
run(const std::filesystem::path& executable,
|
||||
std::vector<std::string> args,
|
||||
std::string_view stdin_data,
|
||||
std::chrono::milliseconds timeout)
|
||||
{
|
||||
// [0] = read end, [1] = write end
|
||||
int stdin_pipe[2] = {-1, -1};
|
||||
int stdout_pipe[2] = {-1, -1};
|
||||
int stderr_pipe[2] = {-1, -1};
|
||||
|
||||
if (::pipe(stdin_pipe) != 0 || ::pipe(stdout_pipe) != 0 ||
|
||||
::pipe(stderr_pipe) != 0) {
|
||||
safe_close(stdin_pipe[0]);
|
||||
safe_close(stdin_pipe[1]);
|
||||
safe_close(stdout_pipe[0]);
|
||||
safe_close(stdout_pipe[1]);
|
||||
safe_close(stderr_pipe[0]);
|
||||
safe_close(stderr_pipe[1]);
|
||||
return Void::Result::fail(std::format("Process::run: pipe() failed: {}",
|
||||
std::strerror(errno)));
|
||||
}
|
||||
|
||||
Void::Log::log(Void::Log::Level::Debug, "Process",
|
||||
std::format("run: '{}'", executable.string()));
|
||||
|
||||
std::vector<const char*> argv;
|
||||
argv.push_back(executable.c_str());
|
||||
for (const auto& a : args) {
|
||||
argv.push_back(a.c_str());
|
||||
}
|
||||
argv.push_back(nullptr);
|
||||
|
||||
pid_t pid = ::fork();
|
||||
if (pid < 0) {
|
||||
safe_close(stdin_pipe[0]);
|
||||
safe_close(stdin_pipe[1]);
|
||||
safe_close(stdout_pipe[0]);
|
||||
safe_close(stdout_pipe[1]);
|
||||
safe_close(stderr_pipe[0]);
|
||||
safe_close(stderr_pipe[1]);
|
||||
return Void::Result::fail(std::format("Process::run: fork() failed: {}",
|
||||
std::strerror(errno)));
|
||||
}
|
||||
|
||||
if (pid == 0) {
|
||||
// Child process
|
||||
::dup2(stdin_pipe[0], STDIN_FILENO);
|
||||
::dup2(stdout_pipe[1], STDOUT_FILENO);
|
||||
::dup2(stderr_pipe[1], STDERR_FILENO);
|
||||
::close(stdin_pipe[0]);
|
||||
::close(stdin_pipe[1]);
|
||||
::close(stdout_pipe[0]);
|
||||
::close(stdout_pipe[1]);
|
||||
::close(stderr_pipe[0]);
|
||||
::close(stderr_pipe[1]);
|
||||
::execvp(executable.c_str(),
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
|
||||
const_cast<char* const*>(argv.data()));
|
||||
// execvp only returns on error; 127 is the POSIX convention
|
||||
::_exit(127);
|
||||
}
|
||||
|
||||
// Parent: close child-side ends
|
||||
safe_close(stdin_pipe[0]);
|
||||
safe_close(stdout_pipe[1]);
|
||||
safe_close(stderr_pipe[1]);
|
||||
|
||||
// Write stdin and close immediately so the child sees EOF
|
||||
if (!stdin_data.empty()) {
|
||||
const char* p = stdin_data.data();
|
||||
std::size_t remaining = stdin_data.size();
|
||||
while (remaining > 0) {
|
||||
ssize_t n = ::write(stdin_pipe[1], p, remaining);
|
||||
if (n < 0) {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
break; // child likely closed stdin; ignore
|
||||
}
|
||||
p += static_cast<std::size_t>(n);
|
||||
remaining -= static_cast<std::size_t>(n);
|
||||
}
|
||||
}
|
||||
safe_close(stdin_pipe[1]);
|
||||
|
||||
set_nonblock(stdout_pipe[0]);
|
||||
set_nonblock(stderr_pipe[0]);
|
||||
|
||||
Output output;
|
||||
bool out_done = false;
|
||||
bool err_done = false;
|
||||
|
||||
const bool has_timeout = timeout.count() > 0;
|
||||
const auto deadline =
|
||||
std::chrono::steady_clock::now() +
|
||||
(has_timeout ? timeout : std::chrono::milliseconds{30'000});
|
||||
|
||||
while (!out_done || !err_done) {
|
||||
struct pollfd pfds[2];
|
||||
int nfds = 0;
|
||||
if (!out_done) {
|
||||
pfds[nfds++] = {stdout_pipe[0], POLLIN, 0};
|
||||
}
|
||||
if (!err_done) {
|
||||
pfds[nfds++] = {stderr_pipe[0], POLLIN, 0};
|
||||
}
|
||||
|
||||
auto left_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
deadline - std::chrono::steady_clock::now())
|
||||
.count();
|
||||
if (left_ms <= 0) {
|
||||
::kill(pid, SIGTERM);
|
||||
break;
|
||||
}
|
||||
|
||||
int ret = ::poll(pfds, static_cast<nfds_t>(nfds),
|
||||
static_cast<int>(left_ms));
|
||||
if (ret < 0) {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (ret == 0) {
|
||||
// Wall-clock timeout expired
|
||||
::kill(pid, SIGTERM);
|
||||
break;
|
||||
}
|
||||
|
||||
for (int i = 0; i < nfds; ++i) {
|
||||
if ((pfds[i].revents & (POLLIN | POLLHUP | POLLERR)) == 0) {
|
||||
continue;
|
||||
}
|
||||
bool& done =
|
||||
(pfds[i].fd == stdout_pipe[0]) ? out_done : err_done;
|
||||
std::string& buf =
|
||||
(pfds[i].fd == stdout_pipe[0]) ? output.out : output.err;
|
||||
bool eof = drain(pfds[i].fd, buf);
|
||||
// POLLHUP means the write end was closed; no more data will arrive
|
||||
if (eof || (pfds[i].revents & (POLLHUP | POLLERR))) {
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
safe_close(stdout_pipe[0]);
|
||||
safe_close(stderr_pipe[0]);
|
||||
|
||||
int status = 0;
|
||||
::waitpid(pid, &status, 0);
|
||||
output.exit_code = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
|
||||
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Debug, "Process",
|
||||
std::format("run: '{}' exited {}", executable.string(),
|
||||
output.exit_code));
|
||||
|
||||
return Void::Result::ok(std::move(output));
|
||||
}
|
||||
|
||||
Void::Result::Result<Output, Void::Result::Error>
|
||||
shell(std::string_view command, std::chrono::milliseconds timeout)
|
||||
{
|
||||
return run("/bin/sh", {"-c", std::string{command}}, {}, timeout);
|
||||
}
|
||||
|
||||
} // namespace Void::Process
|
||||
|
||||
#endif // _WIN32
|
||||
@@ -0,0 +1,3 @@
|
||||
void_add_module(Process
|
||||
INTERNAL_DEPS Result Log
|
||||
)
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <Void/Stacktrace/Stacktrace.hpp>
|
||||
#include <Void/Stacktrace/Color.hpp>
|
||||
#include <Void/Log/Log.hpp>
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#include <format>
|
||||
@@ -37,6 +38,8 @@ namespace Void::Stacktrace
|
||||
}
|
||||
} catch (...) {
|
||||
}
|
||||
Void::Log::log(Void::Log::Level::Debug, "Stacktrace",
|
||||
std::format("captured {} frames", result.size()));
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -46,6 +49,9 @@ namespace Void::Stacktrace
|
||||
{
|
||||
std::ifstream ifs{file};
|
||||
if (!ifs.is_open()) {
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Warning, "Stacktrace",
|
||||
std::format("read_source_snippet: cannot open '{}'", file));
|
||||
return Void::Result::fail(
|
||||
std::format("read_source_snippet: cannot open '{}'", file));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
void_add_module(Stacktrace
|
||||
INTERNAL_DEPS Result
|
||||
INTERNAL_DEPS Result Log
|
||||
EXTERNAL_DEPS Boost::stacktrace_backtrace
|
||||
DEPS_WIN32 dbghelp
|
||||
DEPS_UNIX dl
|
||||
|
||||
Whitespace-only changes.
@@ -1,12 +1,11 @@
|
||||
#include <Void/Testing/ConsoleReporter.hpp>
|
||||
#include <Void/Stacktrace/Color.hpp>
|
||||
#include <Void/Testing/Detail/SignalHandler.hpp>
|
||||
#include <format>
|
||||
#include <chrono>
|
||||
#include <algorithm>
|
||||
|
||||
namespace Void::Testing {
|
||||
using namespace Void::Stacktrace::Color;
|
||||
using namespace Void::Testing::Detail::Color;
|
||||
|
||||
void ConsoleReporter::on_run_start(const RunConfig&, int total_tests) {
|
||||
os_ << "\n" << apply(bold_blue, "╭──────────────────────────────────────────────╮") << "\n"
|
||||
|
||||
+351
-97
@@ -1,17 +1,43 @@
|
||||
#include <Void/Testing/Runner.hpp>
|
||||
#include "Void/Testing/Config.hpp"
|
||||
#include <Void/Logging/FileLogger.hpp>
|
||||
#include <Void/Logging/Level.hpp>
|
||||
#include <Void/Detail/Enum.hpp>
|
||||
#include <Void/Log/Log.hpp>
|
||||
#include <Void/Testing/Detail/Color.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/ConsoleReporter.hpp>
|
||||
#include <Void/Stacktrace/Color.hpp>
|
||||
|
||||
#ifdef VOID_HAS_STACKTRACE
|
||||
#include <Void/Stacktrace/Stacktrace.hpp>
|
||||
#endif
|
||||
#include <Void/Testing/ConsoleReporter.hpp>
|
||||
#include <Void/Testing/Detail/Registry.hpp>
|
||||
#include <Void/Testing/Detail/SignalHandler.hpp>
|
||||
#include <Void/Testing/JUnitReporter.hpp>
|
||||
#include <Void/Time/Format.hpp>
|
||||
|
||||
#ifdef VOID_HAS_CONCURRENCY
|
||||
#include <Void/Concurrency/ThreadPool.hpp>
|
||||
#include <future>
|
||||
#endif
|
||||
|
||||
#ifdef VOID_HAS_CONFIGURATION
|
||||
#include <Void/Configuration/Env.hpp>
|
||||
#else
|
||||
#include <cstdlib>
|
||||
#endif
|
||||
|
||||
#ifdef VOID_HAS_LOGGING
|
||||
#include <Void/Log/LoggingAdapter.hpp>
|
||||
#include <Void/Logging/ConsoleLogger.hpp>
|
||||
#include <Void/Logging/FileLogger.hpp>
|
||||
#include <Void/Logging/Level.hpp>
|
||||
#endif
|
||||
|
||||
#ifdef VOID_HAS_STRING
|
||||
#include <Void/String/String.hpp>
|
||||
#endif
|
||||
|
||||
#include <algorithm>
|
||||
#include <csignal>
|
||||
#include <cstdlib>
|
||||
#include <format>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
@@ -28,9 +54,10 @@ namespace Void::Testing
|
||||
RunConfig config;
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
std::string arg = argv[i];
|
||||
if ((arg == "--filter" || arg == "--suite" || arg == "--reporter" ||
|
||||
arg == "--output" || arg == "--tag" || arg == "--seed" ||
|
||||
arg == "--repeat" || arg == "--log-file") &&
|
||||
if ((arg == "--filter" || arg == "--suite" ||
|
||||
arg == "--reporter" || arg == "--output" ||
|
||||
arg == "--tag" || arg == "--seed" || arg == "--repeat" ||
|
||||
arg == "--log-file" || arg == "--parallel") &&
|
||||
i + 1 < argc) {
|
||||
std::string val = argv[++i];
|
||||
if (arg == "--filter") {
|
||||
@@ -47,11 +74,14 @@ namespace Void::Testing
|
||||
config.log_file = val;
|
||||
} else if (arg == "--seed") {
|
||||
config.shuffle = true;
|
||||
config.random_seed = static_cast<unsigned>(std::stoul(val));
|
||||
config.random_seed =
|
||||
static_cast<unsigned>(std::stoul(val));
|
||||
} else if (arg == "--repeat") {
|
||||
config.repeat = static_cast<unsigned>(std::stoul(val));
|
||||
} else if (arg == "--parallel") {
|
||||
config.parallel_threads =
|
||||
static_cast<unsigned>(std::stoul(val));
|
||||
}
|
||||
|
||||
} else if (arg == "--help") {
|
||||
config.show_help = true;
|
||||
} else if (arg == "--list") {
|
||||
@@ -138,10 +168,11 @@ namespace Void::Testing
|
||||
|
||||
int crash_signal = 0;
|
||||
std::string crash_stacktrace;
|
||||
Detail::install_signal_handlers([&](int sig, const std::string& trace) {
|
||||
crash_signal = sig;
|
||||
crash_stacktrace = trace;
|
||||
});
|
||||
Detail::install_signal_handlers(
|
||||
[&](int sig, const std::string& trace) {
|
||||
crash_signal = sig;
|
||||
crash_stacktrace = trace;
|
||||
});
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
|
||||
try {
|
||||
@@ -167,13 +198,17 @@ namespace Void::Testing
|
||||
result.state = TestState::Failed;
|
||||
result.crash_info =
|
||||
std::format("Unhandled exception: {}", e.what());
|
||||
#ifdef VOID_HAS_STACKTRACE
|
||||
result.stacktrace = Void::Stacktrace::format_stacktrace(
|
||||
Void::Stacktrace::capture_stacktrace(1, 8));
|
||||
#endif
|
||||
} catch (...) {
|
||||
result.state = TestState::Failed;
|
||||
result.crash_info = "Unhandled unknown exception";
|
||||
#ifdef VOID_HAS_STACKTRACE
|
||||
result.stacktrace = Void::Stacktrace::format_stacktrace(
|
||||
Void::Stacktrace::capture_stacktrace(1, 8));
|
||||
#endif
|
||||
}
|
||||
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
@@ -183,9 +218,56 @@ namespace Void::Testing
|
||||
|
||||
if (result.state != TestState::Skipped &&
|
||||
result.state != TestState::Crashed) {
|
||||
bool any =
|
||||
std::any_of(result.assertions.begin(), result.assertions.end(),
|
||||
[](const auto& a) { return !a.passed; });
|
||||
bool any = std::any_of(
|
||||
result.assertions.begin(), result.assertions.end(),
|
||||
[](const auto& a) { return !a.passed; });
|
||||
result.state = any ? TestState::Failed : TestState::Passed;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Like execute_test() but without sigsetjmp/signal-handler installation,
|
||||
// safe to call from multiple threads simultaneously.
|
||||
TestResult Runner::execute_test_parallel(TestCase& test_case)
|
||||
{
|
||||
TestResult result;
|
||||
result.name = test_case.name;
|
||||
result.suite = test_case.suite;
|
||||
result.tags = test_case.tags;
|
||||
if (test_case.disabled) {
|
||||
result.state = TestState::Skipped;
|
||||
result.skip_reason = test_case.skip_reason.empty()
|
||||
? "disabled"
|
||||
: test_case.skip_reason;
|
||||
return result;
|
||||
}
|
||||
Detail::TestContext::current().current_result = &result;
|
||||
Detail::TestContext::current().should_abort = false;
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
try {
|
||||
run_test_body(test_case, result);
|
||||
} catch (const Detail::RequireFailure&) {
|
||||
if (result.state != TestState::Skipped) {
|
||||
result.state = TestState::Failed;
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
result.state = TestState::Failed;
|
||||
result.crash_info =
|
||||
std::format("Unhandled exception: {}", e.what());
|
||||
} catch (...) {
|
||||
result.state = TestState::Failed;
|
||||
result.crash_info = "Unhandled unknown exception";
|
||||
}
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
result.duration = end - start;
|
||||
Detail::TestContext::current().current_result = nullptr;
|
||||
|
||||
if (result.state != TestState::Skipped &&
|
||||
result.state != TestState::Crashed) {
|
||||
bool any = std::any_of(
|
||||
result.assertions.begin(), result.assertions.end(),
|
||||
[](const auto& a) { return !a.passed; });
|
||||
result.state = any ? TestState::Failed : TestState::Passed;
|
||||
}
|
||||
return result;
|
||||
@@ -215,56 +297,103 @@ namespace Void::Testing
|
||||
|
||||
RunResult Runner::run(const RunConfig& config)
|
||||
{
|
||||
Void::Stacktrace::Color::g_color_enabled = !config.no_color;
|
||||
Void::Testing::Detail::Color::g_color_enabled = !config.no_color;
|
||||
if (config.show_help) {
|
||||
using namespace Void::Stacktrace::Color;
|
||||
using namespace Void::Testing::Detail::Color;
|
||||
|
||||
std::cout
|
||||
<< "\n"
|
||||
<< apply(bold_blue, "╭─────────────────────────────────────────"
|
||||
"─────────────────────────╮")
|
||||
<< apply(bold_blue,
|
||||
"\xe2\x95\xad\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
||||
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
||||
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
||||
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
||||
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
||||
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
||||
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
||||
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
||||
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
||||
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
||||
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
||||
"\xe2\x94\x80\xe2\x94\x80\xe2\x95\xae")
|
||||
<< "\n"
|
||||
<< apply(bold_blue, "│") << apply(bold_white, " Void Testing")
|
||||
<< apply(dim, " — Command Line Interface")
|
||||
<< std::string(28, ' ') << apply(bold_blue, "│") << "\n"
|
||||
<< apply(bold_blue, "╰─────────────────────────────────────────"
|
||||
"─────────────────────────╯")
|
||||
<< apply(bold_blue, "\xe2\x94\x82")
|
||||
<< apply(bold_white, " Void Testing")
|
||||
<< apply(dim, " \xe2\x80\x94 Command Line Interface")
|
||||
<< std::string(28, ' ') << apply(bold_blue, "\xe2\x94\x82")
|
||||
<< "\n"
|
||||
<< apply(bold_blue,
|
||||
"\xe2\x95\xb0\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
||||
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
||||
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
||||
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
||||
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
||||
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
||||
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
||||
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
||||
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
||||
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
||||
"\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80"
|
||||
"\xe2\x94\x80\xe2\x94\x80\xe2\x95\xaf")
|
||||
<< "\n"
|
||||
<< "\n"
|
||||
<< " " << apply(bold_white, "USAGE:") << "\n"
|
||||
<< " " << apply(cyan, "<executable>") << " "
|
||||
<< apply(dim, "[options]") << "\n"
|
||||
<< "\n"
|
||||
<< " " << apply(bold_white, "OPTIONS:") << "\n"
|
||||
<< " " << apply(green, "--filter <pattern> ")
|
||||
<< " " << apply(bold_white, "OPTIONS:") << "\n";
|
||||
|
||||
constexpr std::size_t OPT_WIDTH = 23;
|
||||
auto opt = [&](std::string_view name) {
|
||||
#ifdef VOID_HAS_STRING
|
||||
return apply(green, Void::String::pad_right(name, OPT_WIDTH));
|
||||
#else
|
||||
std::string s{name};
|
||||
if (s.size() < OPT_WIDTH) {
|
||||
s.append(OPT_WIDTH - s.size(), ' ');
|
||||
}
|
||||
return apply(green, s);
|
||||
#endif
|
||||
};
|
||||
|
||||
std::cout
|
||||
<< " " << opt("--filter <pattern>")
|
||||
<< apply(dim, "Run tests matching the regex pattern") << "\n"
|
||||
<< " " << apply(green, "--suite <name> ")
|
||||
<< " " << opt("--suite <name>")
|
||||
<< apply(dim, "Run only tests in the specified suite") << "\n"
|
||||
<< " " << apply(green, "--tag <name> ")
|
||||
<< " " << opt("--tag <name>")
|
||||
<< apply(dim, "Run only tests with the specified tag") << "\n"
|
||||
<< " " << apply(green, "--list ")
|
||||
<< " " << opt("--list")
|
||||
<< apply(dim, "List discovered tests without running them")
|
||||
<< "\n"
|
||||
<< " " << apply(green, "--shuffle ")
|
||||
<< " " << opt("--shuffle")
|
||||
<< apply(dim, "Randomize the order of test execution") << "\n"
|
||||
<< " " << apply(green, "--seed <n> ")
|
||||
<< " " << opt("--seed <n>")
|
||||
<< apply(dim, "Set the random seed (implies --shuffle)") << "\n"
|
||||
<< " " << apply(green, "--repeat <n> ")
|
||||
<< " " << opt("--repeat <n>")
|
||||
<< apply(dim, "Repeat the test run n times") << "\n"
|
||||
<< " " << apply(green, "--reporter <type> ")
|
||||
<< " " << opt("--reporter <type>")
|
||||
<< apply(dim, "Choose the output reporter: console, junit")
|
||||
<< "\n"
|
||||
<< " " << apply(green, "--output <file> ")
|
||||
<< " " << opt("--output <file>")
|
||||
<< apply(dim, "Write reporter output to a file") << "\n"
|
||||
<< " " << apply(green, "--log-file <file> ")
|
||||
#ifdef VOID_HAS_LOGGING
|
||||
<< " " << opt("--log-file <file>")
|
||||
<< apply(dim,
|
||||
"Mirror test events to <file> via Void::Logging")
|
||||
<< "\n"
|
||||
<< " " << apply(green, "--verbose ")
|
||||
#endif
|
||||
#ifdef VOID_HAS_CONCURRENCY
|
||||
<< " " << opt("--parallel <n>")
|
||||
<< apply(dim,
|
||||
"Run tests across n threads (no crash detection)")
|
||||
<< "\n"
|
||||
#endif
|
||||
<< " " << opt("--verbose")
|
||||
<< apply(dim, "Enable verbose output") << "\n"
|
||||
<< " " << apply(green, "--no-color ")
|
||||
<< " " << opt("--no-color")
|
||||
<< apply(dim, "Disable colored console output") << "\n"
|
||||
<< " " << apply(green, "--help ")
|
||||
<< " " << opt("--help")
|
||||
<< apply(dim, "Show this help message") << "\n"
|
||||
<< "\n"
|
||||
<< " " << apply(bold_white, "EXAMPLES:") << "\n"
|
||||
@@ -284,6 +413,8 @@ namespace Void::Testing
|
||||
auto tests = filter_tests(all_tests, config);
|
||||
auto reporter = create_reporter(config);
|
||||
|
||||
// ── Optional: file logger sink (requires Void::Logging) ──────────────────
|
||||
#ifdef VOID_HAS_LOGGING
|
||||
std::unique_ptr<Void::Logging::FileLogger> log_sink;
|
||||
if (!config.log_file.empty()) {
|
||||
try {
|
||||
@@ -294,6 +425,45 @@ namespace Void::Testing
|
||||
<< "\n";
|
||||
}
|
||||
}
|
||||
#else
|
||||
if (!config.log_file.empty()) {
|
||||
std::cerr
|
||||
<< "warning: --log-file requires Void::Logging (not linked)\n";
|
||||
}
|
||||
#endif
|
||||
|
||||
// ── Optional: VOID_LOG_LEVEL auto-sink ───────────────────────────────────
|
||||
#ifdef VOID_HAS_LOGGING
|
||||
std::unique_ptr<Void::Logging::ConsoleLogger> env_log;
|
||||
{
|
||||
#ifdef VOID_HAS_CONFIGURATION
|
||||
const auto lvl_str =
|
||||
Void::Configuration::get_env_or("VOID_LOG_LEVEL");
|
||||
#else
|
||||
const char* raw_env =
|
||||
std::getenv("VOID_LOG_LEVEL"); // NOLINT(concurrency-mt-unsafe)
|
||||
const std::string lvl_str = raw_env ? raw_env : "";
|
||||
#endif
|
||||
if (!lvl_str.empty()) {
|
||||
#ifdef VOID_HAS_ENUM_REFLECTION
|
||||
auto level =
|
||||
Void::Detail::enum_parse<Void::Logging::Level>(lvl_str)
|
||||
.value_or(Void::Logging::Level::Info);
|
||||
#else
|
||||
Void::Logging::Level level = Void::Logging::Level::Info;
|
||||
if (lvl_str == "Trace") level = Void::Logging::Level::Trace;
|
||||
else if (lvl_str == "Debug") level = Void::Logging::Level::Debug;
|
||||
else if (lvl_str == "Warning") level = Void::Logging::Level::Warning;
|
||||
else if (lvl_str == "Error") level = Void::Logging::Level::Error;
|
||||
else if (lvl_str == "Fatal") level = Void::Logging::Level::Fatal;
|
||||
#endif
|
||||
env_log =
|
||||
std::make_unique<Void::Logging::ConsoleLogger>(std::cerr);
|
||||
env_log->set_level(level);
|
||||
Void::Log::set_sink(Void::Log::make_sink(*env_log));
|
||||
}
|
||||
}
|
||||
#endif // VOID_HAS_LOGGING
|
||||
|
||||
if (config.list_only) {
|
||||
for (const auto& t : tests) {
|
||||
@@ -304,19 +474,6 @@ namespace Void::Testing
|
||||
}
|
||||
|
||||
RunResult run_result;
|
||||
reporter->on_run_start(config, static_cast<int>(tests.size()));
|
||||
if (log_sink) {
|
||||
log_sink->info(std::format("run start: {} test(s) discovered",
|
||||
tests.size()));
|
||||
}
|
||||
if (config.shuffle) {
|
||||
std::shuffle(tests.begin(), tests.end(),
|
||||
std::mt19937{config.random_seed});
|
||||
if (log_sink) {
|
||||
log_sink->info(std::format("shuffle enabled (seed = {})",
|
||||
config.random_seed));
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<SuiteResult> suite_results;
|
||||
std::unordered_map<std::string, size_t> suite_index;
|
||||
@@ -326,58 +483,148 @@ namespace Void::Testing
|
||||
suite_results.push_back({.name = test.suite});
|
||||
}
|
||||
}
|
||||
|
||||
auto log_result = [&](const TestResult& r, const std::string& q) {
|
||||
#ifdef VOID_HAS_LOGGING
|
||||
if (!log_sink) {
|
||||
return;
|
||||
}
|
||||
const auto ts = Void::Time::utc_now_iso8601_ms();
|
||||
const auto dur = Void::Time::format_duration(r.duration);
|
||||
switch (r.state) {
|
||||
case TestState::Passed:
|
||||
log_sink->info(std::format("[{}] test pass: {} ({})", ts,
|
||||
q, dur));
|
||||
break;
|
||||
case TestState::Failed:
|
||||
log_sink->error(std::format(
|
||||
"[{}] test fail: {} ({}) \xe2\x80\x94 {}", ts, q, dur,
|
||||
r.crash_info.empty() ? "assertion failed"
|
||||
: r.crash_info));
|
||||
break;
|
||||
case TestState::Skipped:
|
||||
log_sink->warning(std::format(
|
||||
"[{}] test skip: {} \xe2\x80\x94 {}", ts, q,
|
||||
r.skip_reason.empty() ? "no reason" : r.skip_reason));
|
||||
break;
|
||||
case TestState::Crashed:
|
||||
log_sink->fatal(std::format(
|
||||
"[{}] test crash: {} \xe2\x80\x94 {}", ts, q,
|
||||
r.crash_info));
|
||||
break;
|
||||
}
|
||||
#else
|
||||
(void)r;
|
||||
(void)q;
|
||||
#endif
|
||||
};
|
||||
|
||||
auto start_time = std::chrono::high_resolution_clock::now();
|
||||
|
||||
for (unsigned rep = 0; rep < config.repeat; ++rep) {
|
||||
// ── Optional: parallel execution (requires Void::Concurrency) ────────────
|
||||
bool run_parallel = config.parallel_threads > 0;
|
||||
#ifndef VOID_HAS_CONCURRENCY
|
||||
if (run_parallel) {
|
||||
std::cerr << "warning: --parallel requires Void::Concurrency "
|
||||
"(not linked); running sequentially\n";
|
||||
run_parallel = false;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (run_parallel) {
|
||||
#ifdef VOID_HAS_CONCURRENCY
|
||||
reporter->on_run_start(config, static_cast<int>(tests.size()));
|
||||
#ifdef VOID_HAS_LOGGING
|
||||
if (log_sink) {
|
||||
log_sink->info(std::format(
|
||||
"run start (parallel, {} threads): {} test(s)",
|
||||
config.parallel_threads, tests.size()));
|
||||
}
|
||||
#endif
|
||||
Void::Concurrency::ThreadPool pool{config.parallel_threads};
|
||||
std::vector<std::future<TestResult>> futures;
|
||||
futures.reserve(tests.size());
|
||||
for (auto& test : tests) {
|
||||
auto& suite = suite_results[suite_index[test.suite]];
|
||||
if (rep == 0) {
|
||||
futures.push_back(pool.submit(
|
||||
[this, &test] { return execute_test_parallel(test); }));
|
||||
}
|
||||
std::vector<TestResult> par_results;
|
||||
par_results.reserve(tests.size());
|
||||
for (auto& fut : futures) {
|
||||
par_results.push_back(fut.get());
|
||||
}
|
||||
|
||||
std::unordered_map<std::string, bool> suite_started;
|
||||
for (std::size_t i = 0; i < tests.size(); ++i) {
|
||||
auto& test = tests[i];
|
||||
auto& result = par_results[i];
|
||||
if (!suite_started[test.suite]) {
|
||||
suite_started[test.suite] = true;
|
||||
reporter->on_suite_start(
|
||||
suite.name, std::count_if(tests.begin(), tests.end(),
|
||||
[&](const auto& t) {
|
||||
return t.suite ==
|
||||
suite.name;
|
||||
}));
|
||||
if (log_sink) {
|
||||
log_sink->info(
|
||||
std::format("suite start: {}", suite.name));
|
||||
}
|
||||
test.suite,
|
||||
static_cast<int>(
|
||||
std::count_if(tests.begin(), tests.end(),
|
||||
[&](const auto& t) {
|
||||
return t.suite == test.suite;
|
||||
})));
|
||||
}
|
||||
reporter->on_test_start(test);
|
||||
auto result = execute_test(test);
|
||||
reporter->on_test_end(result);
|
||||
log_result(result, std::format("{}.{}", result.suite,
|
||||
result.name));
|
||||
suite_results[suite_index[test.suite]].tests.push_back(
|
||||
std::move(result));
|
||||
}
|
||||
#endif // VOID_HAS_CONCURRENCY
|
||||
} else {
|
||||
reporter->on_run_start(config, static_cast<int>(tests.size()));
|
||||
#ifdef VOID_HAS_LOGGING
|
||||
if (log_sink) {
|
||||
log_sink->info(std::format("run start: {} test(s) discovered",
|
||||
tests.size()));
|
||||
}
|
||||
#endif
|
||||
if (config.shuffle) {
|
||||
std::shuffle(tests.begin(), tests.end(),
|
||||
std::mt19937{config.random_seed});
|
||||
#ifdef VOID_HAS_LOGGING
|
||||
if (log_sink) {
|
||||
const auto qualified =
|
||||
std::format("{}.{}", result.suite, result.name);
|
||||
switch (result.state) {
|
||||
case TestState::Passed:
|
||||
log_sink->info(
|
||||
std::format("test pass: {}", qualified));
|
||||
break;
|
||||
case TestState::Failed:
|
||||
log_sink->error(std::format(
|
||||
"test fail: {} — {}", qualified,
|
||||
result.crash_info.empty() ? "assertion failed"
|
||||
: result.crash_info));
|
||||
break;
|
||||
case TestState::Skipped:
|
||||
log_sink->warning(std::format(
|
||||
"test skip: {} ({})", qualified,
|
||||
result.skip_reason.empty() ? "no reason"
|
||||
: result.skip_reason));
|
||||
break;
|
||||
case TestState::Crashed:
|
||||
log_sink->fatal(std::format(
|
||||
"test crash: {} — {}", qualified,
|
||||
result.crash_info));
|
||||
break;
|
||||
}
|
||||
log_sink->info(std::format("shuffle enabled (seed = {})",
|
||||
config.random_seed));
|
||||
}
|
||||
if (rep == config.repeat - 1) {
|
||||
suite.tests.push_back(std::move(result));
|
||||
#endif
|
||||
}
|
||||
|
||||
for (unsigned rep = 0; rep < config.repeat; ++rep) {
|
||||
for (auto& test : tests) {
|
||||
auto& suite = suite_results[suite_index[test.suite]];
|
||||
if (rep == 0) {
|
||||
reporter->on_suite_start(
|
||||
suite.name,
|
||||
std::count_if(
|
||||
tests.begin(), tests.end(),
|
||||
[&](const auto& t) {
|
||||
return t.suite == suite.name;
|
||||
}));
|
||||
#ifdef VOID_HAS_LOGGING
|
||||
if (log_sink) {
|
||||
log_sink->info(std::format("suite start: {}",
|
||||
suite.name));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
reporter->on_test_start(test);
|
||||
auto result = execute_test(test);
|
||||
reporter->on_test_end(result);
|
||||
log_result(result, std::format("{}.{}", result.suite,
|
||||
result.name));
|
||||
if (rep == config.repeat - 1) {
|
||||
suite.tests.push_back(std::move(result));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto end_time = std::chrono::high_resolution_clock::now();
|
||||
|
||||
for (auto& suite : suite_results) {
|
||||
@@ -403,12 +650,19 @@ namespace Void::Testing
|
||||
}
|
||||
run_result.total_duration = end_time - start_time;
|
||||
reporter->on_run_end(run_result);
|
||||
#ifdef VOID_HAS_LOGGING
|
||||
if (log_sink) {
|
||||
log_sink->info(std::format(
|
||||
"run end: {} total / {} passed / {} failed / {} skipped / {} crashed",
|
||||
run_result.total, run_result.passed, run_result.failed,
|
||||
run_result.skipped, run_result.crashed));
|
||||
log_sink->info(
|
||||
std::format("run end: {} total / {} passed / {} failed / {} "
|
||||
"skipped / {} crashed",
|
||||
run_result.total, run_result.passed,
|
||||
run_result.failed, run_result.skipped,
|
||||
run_result.crashed));
|
||||
}
|
||||
if (env_log) {
|
||||
Void::Log::set_sink({});
|
||||
}
|
||||
#endif
|
||||
return run_result;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include <Void/Testing/Detail/SignalHandler.hpp>
|
||||
#ifdef VOID_HAS_STACKTRACE
|
||||
#include <Void/Stacktrace/Stacktrace.hpp>
|
||||
#endif
|
||||
#include <csignal>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
@@ -30,8 +32,12 @@ const char* signal_name(int sig) {
|
||||
|
||||
static void crash_handler(int sig) {
|
||||
g_last_signal = sig;
|
||||
#ifdef VOID_HAS_STACKTRACE
|
||||
auto frames = Void::Stacktrace::capture_stacktrace(1, 16);
|
||||
auto trace = Void::Stacktrace::format_stacktrace(frames);
|
||||
#else
|
||||
std::string trace;
|
||||
#endif
|
||||
if (g_crash_callback) g_crash_callback(sig, trace);
|
||||
#ifndef _WIN32
|
||||
if (g_jump_ready) { g_jump_ready = false; siglongjmp(g_jump_buffer, sig); }
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
void_add_module(Testing INTERNAL_DEPS Stacktrace Logging Result)
|
||||
void_add_module(Testing
|
||||
OPTIONAL_DEPS Stacktrace Logging Concurrency Configuration String
|
||||
)
|
||||
Reference in new issue
Block a user