Files
Library/tests/internal/CrossModule.cpp
T
faumarayandClaude Opus 4.8 2d3f9dc319 Log: carry call-site source_location through to the sink
Every log line showed LoggingAdapter.hpp as its source because make_sink
called Void::Logging::Logger::log() with a defaulted source_location captured
inside the adapter, discarding the real call site.

Capture std::source_location at each Void::Log::{info,warn,...} call via an
FmtLoc wrapper (the same trick Logger uses internally), thread it through a
4-argument Sink, and forward it from make_sink to logger.log(). Sinks now
receive the originating file/line/function.

Formatted emission is routed through a single non-overloaded Detail::emit():
forwarding a (level, module, std::string, source_location) call to the public
log() would otherwise also match the variadic log() overload, deducing
source_location as a format argument and failing in FmtLoc's consteval ctor.

Sink is now a 4-arg callable; updated the bundled tests accordingly.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-25 02:35:42 +03:00

174 lines
6.0 KiB
C++

// Cross-module integration tests for the kitchen-sink VoidInternalTests
// binary. Exercises Logging + Result + Stacktrace + FileSystem + Log working
// together — the kind of thing the standalone per-module tests can't
// reach because they only link one module.
#include <Void/System/Path.hpp>
#include <Void/System/TempDir.hpp>
#include <Void/Log/Log.hpp>
#include <Void/Log/LoggingAdapter.hpp>
#include <Void/Log/ConsoleLogger.hpp>
#include <Void/Log/FileLogger.hpp>
#include <Void/Log/Level.hpp>
#include <Void/Functional/Error.hpp>
#include <Void/Functional/Result.hpp>
#include <Void/Stacktrace/Stacktrace.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <filesystem>
#include <sstream>
#include <string>
namespace
{
struct ConfigError {
std::string file;
int line;
std::string detail;
};
} // namespace
// Reflection-based repr (Phase 2.6) renders user-defined error structs
// inside Testing assertions without any per-type formatter.
Void::Testing::Test repr_handles_user_error_struct()
{
const auto out = Void::Testing::Detail::repr(
ConfigError{"settings.toml", 17, "missing key 'name'"});
Void::Testing::expect(out.find("ConfigError") != std::string::npos);
Void::Testing::expect(out.find("file: settings.toml") != std::string::npos);
Void::Testing::expect(out.find("line: 17") != std::string::npos);
Void::Testing::expect(out.find("detail: missing key 'name'") !=
std::string::npos);
}
// Result + Stacktrace: read_source_snippet returns Result, and a missing
// file produces an Error whose chained message is rendered correctly.
Void::Testing::Test stacktrace_snippet_returns_result_on_missing_file()
{
auto r = Void::Stacktrace::read_source_snippet(
"/this/path/does/not/exist.cpp", 10);
Void::Testing::expect(!r.has_value());
const auto chained = Void::Result::with_context(r, "loading test fixture");
const auto rendered = chained.error().format();
Void::Testing::expect(rendered.find("loading test fixture") !=
std::string::npos);
Void::Testing::expect(rendered.find("read_source_snippet") !=
std::string::npos);
Void::Testing::expect(rendered.find("caused by:") != std::string::npos);
}
// Logging + Result: FileLogger::open returns Result; chained context
// surfaces meaningfully.
Void::Testing::Test file_logger_open_failure_chains_into_context()
{
auto r = Void::Logging::FileLogger::open("/no/such/dir/test.log");
Void::Testing::expect(!r.has_value());
auto wrapped =
Void::Result::with_context(std::move(r), "starting test runner");
const auto rendered = wrapped.error().format();
Void::Testing::expect(rendered.find("starting test runner") !=
std::string::npos);
Void::Testing::expect(rendered.find("FileLogger") != std::string::npos);
}
// Logging emits scope boundaries that survive into the output stream.
Void::Testing::Test logger_scope_entries_render_through_console_sink()
{
std::ostringstream out;
{
Void::Logging::ConsoleLogger log(out);
log.set_level(Void::Logging::Level::Trace);
auto scope = log.push_scope("worker");
log.info("doing work");
}
const auto content = out.str();
Void::Testing::expect(content.find("doing work") != std::string::npos);
Void::Testing::expect(content.find("worker") != std::string::npos);
}
// Logging + Result + FileSystem: roundtrip a chained error into a
// FileLogger sink and read it back through Void::FileSystem::read_file
// (no manual std::filesystem boilerplate, no manual cleanup).
Void::Testing::Test error_chain_logged_to_file_roundtrip()
{
Void::FileSystem::TempDir tmp;
const auto path = tmp / "log.txt";
{
auto sink_r = Void::Logging::FileLogger::open(
path, Void::Logging::Mode::Buffered, 8);
Void::Testing::expect(sink_r.has_value());
auto& sink = **sink_r;
Void::Result::Error inner{"socket closed"};
auto outer = inner.with_context("api request /users");
sink.error(outer.format());
}
auto read_back = Void::FileSystem::read_file(path);
Void::Testing::expect(read_back.has_value());
const auto& content = *read_back;
Void::Testing::expect(content.find("api request /users") !=
std::string::npos);
Void::Testing::expect(content.find("socket closed") != std::string::npos);
Void::Testing::expect(content.find("caused by:") != std::string::npos);
}
// Void::Log + Void::Logging: make_sink() bridges the two modules so that
// internal library events routed through Void::Log::log() surface in a
// Void::Logging::ConsoleLogger captured via a stringstream.
Void::Testing::Test log_adapter_routes_to_void_logging()
{
std::ostringstream out;
Void::Logging::ConsoleLogger logger{out};
logger.set_level(Void::Logging::Level::Trace);
Void::Log::set_sink(Void::Log::make_sink(logger));
Void::Log::log(
Void::Log::Level::Info, "TestModule", "hello from Void::Log");
Void::Log::log(Void::Log::Level::Warning, "TestModule", "a warning");
Void::Log::set_sink({});
logger.flush(); // ConsoleLogger is async by default
const auto text = out.str();
Void::Testing::expect(text.find("hello from Void::Log") !=
std::string::npos);
Void::Testing::expect(text.find("a warning") != std::string::npos);
Void::Testing::expect(text.find("[TestModule]") != std::string::npos);
}
// FileSystem operations now emit Void::Log events; verify they reach a sink.
Void::Testing::Test filesystem_ops_emit_log_events()
{
int debug_count = 0;
int warning_count = 0;
Void::Log::set_sink(
[&](Void::Log::Level l, std::string_view, std::string_view,
const std::source_location&) {
if (l == Void::Log::Level::Debug) {
++debug_count;
}
if (l == Void::Log::Level::Warning) {
++warning_count;
}
});
Void::FileSystem::TempDir tmp;
auto p = tmp / "test.txt";
(void)Void::FileSystem::write_file(p, "data");
(void)Void::FileSystem::read_file(p);
(void)Void::FileSystem::read_file(tmp / "nonexistent.txt");
Void::Log::set_sink({});
// TempDir creation + read success + write (no debug from write_file) = at
// least 2
Void::Testing::expect(debug_count >= 2);
// Missing file read = 1 warning
Void::Testing::expect(warning_count >= 1);
}