Files
Library/src/Testing/ConsoleReporter.cpp
T
faumarayandClaude Sonnet 4.6 a09e000f0f 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>
2026-05-14 21:52:22 +03:00

71 lines
5.6 KiB
C++

#include <Void/Testing/ConsoleReporter.hpp>
#include <Void/Testing/Detail/SignalHandler.hpp>
#include <format>
#include <chrono>
#include <algorithm>
namespace Void::Testing {
using namespace Void::Testing::Detail::Color;
void ConsoleReporter::on_run_start(const RunConfig&, int total_tests) {
os_ << "\n" << apply(bold_blue, "╭──────────────────────────────────────────────╮") << "\n"
<< apply(bold_blue, "│") << apply(bold_white, " Void Testing") << apply(dim, " v1.0") << std::string(31, ' ') << apply(bold_blue, "│") << "\n"
<< std::format("{}│{} Discovered {} test{}{}│\n", apply(bold_blue, "╰"), apply(dim, ""), apply(bold_white, std::to_string(total_tests)), total_tests != 1 ? "s" : "", std::string(total_tests < 10 ? 33 : 28, ' '))
<< apply(bold_blue, "╰──────────────────────────────────────────────╯") << "\n\n";
}
void ConsoleReporter::on_suite_start(const std::string& suite_name, int test_count) {
os_ << std::format(" {} {} [{} test{}]\n", apply(bold_blue, "◆"), apply(bold_white, suite_name), apply(dim, std::to_string(test_count)), test_count != 1 ? "s" : "");
}
void ConsoleReporter::on_test_start(const TestCase&) {}
void ConsoleReporter::on_suite_end(const SuiteResult&) {}
void ConsoleReporter::on_test_end(const TestResult& result) {
double ms = std::chrono::duration<double, std::milli>(result.duration).count();
std::string time_str = ms < 1.0 ? std::format("{:.3f}ms", ms) : std::format("{:.1f}ms", ms);
std::string tags_str;
if (!result.tags.empty()) { for(const auto& t : result.tags) tags_str += "[" + t + "]"; tags_str = " " + apply(dim, tags_str); }
switch (result.state) {
case TestState::Passed: os_ << std::format(" {} {:<40s}{}{}\n", apply(bold_green, "✔"), apply(green, result.name), tags_str, apply(dim, time_str)); break;
case TestState::Failed:
os_ << std::format(" {} {:<40s}{}{}\n", apply(bold_red, "✘"), apply(red, result.name), tags_str, apply(dim, time_str));
for (const auto& ar : result.assertions) if (!ar.passed) print_assertion_failure(ar);
if (!result.crash_info.empty()) print_crash_info(result);
break;
case TestState::Skipped: os_ << std::format(" {} {:<40s}{}\n", apply(bold_yellow, "⊘"), apply(yellow, result.name), apply(dim, std::format("[SKIPPED: {}]", result.skip_reason.empty() ? "no reason" : result.skip_reason))); break;
case TestState::Crashed: os_ << std::format(" {} {:<40s}{}\n", apply(bold_red, "⚡"), apply(red, result.name), apply(dim, time_str)); print_crash_info(result); break;
}
}
void ConsoleReporter::print_assertion_failure(const AssertionResult& ar) {
os_ << apply(red, " ╭─── Assertion Failed") << apply(dim, " ──────────────────────╮") << "\n";
std::filesystem::path p{ar.location.file_name()};
os_ << std::format(" │ {}:{}\n", apply(bold_white, p.filename().string()), ar.location.line());
if (!ar.expression.empty()) os_ << std::format(" │ {}\n", apply(cyan, ar.expression));
if (!ar.expected_repr.empty()) os_ << std::format(" │ {} {}\n", apply(bold_white, "expected:"), apply(green, ar.expected_repr));
if (!ar.actual_repr.empty()) os_ << std::format(" │ {} {}\n", apply(bold_white, "actual: "), apply(red, ar.actual_repr));
if (!ar.message.empty()) os_ << std::format(" │ {}\n", ar.message);
os_ << apply(red, " ╰────────────────────────────────────────────╯") << "\n";
}
void ConsoleReporter::print_crash_info(const TestResult& result) {
os_ << apply(bold_red, " ╭─── Crash") << apply(dim, " ───────────────────────────╮") << "\n";
if (!result.crash_info.empty()) os_ << std::format(" │ {}\n", apply(red, result.crash_info));
os_ << apply(bold_red, " ╰────────────────────────────────────────────╯") << "\n";
if (!result.stacktrace.empty()) os_ << apply(dim, " Stacktrace:\n") << result.stacktrace << "\n";
}
void ConsoleReporter::on_run_end(const RunResult& result) {
os_ << "\n" << apply(dim, "╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍") << "\n"
<< std::format(" {} tests: ", apply(bold_white, std::to_string(result.total)));
if (result.passed > 0) os_ << std::format("{} {} ", apply(bold_green, std::to_string(result.passed)), apply(green, "✔"));
if (result.failed + result.crashed > 0) os_ << std::format("{} {} ", apply(bold_red, std::to_string(result.failed + result.crashed)), apply(red, "✘"));
if (result.skipped > 0) os_ << std::format("{} {} ", apply(bold_yellow, std::to_string(result.skipped)), apply(yellow, "⊘"));
double total_ms = std::chrono::duration<double, std::milli>(result.total_duration).count();
os_ << std::format(" {} {}\n", apply(dim, "in"), apply(bold_cyan, total_ms < 1000 ? std::format("{:.1f}ms", total_ms) : std::format("{:.2f}s", total_ms / 1000.0)))
<< apply(dim, "╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍") << "\n\n";
}
} // namespace Void::Testing