Files
Library/tests/internal/CrossModule.cpp
T
2026-05-03 18:09:46 +03:00

116 lines
4.1 KiB
C++

// Cross-module integration tests for the kitchen-sink VoidInternalTests
// binary. Exercises Logging + Result + Stacktrace + FileSystem working
// together — the kind of thing the standalone per-module tests can't
// reach because they only link one module.
#include <Void/FileSystem/Path.hpp>
#include <Void/FileSystem/TempDir.hpp>
#include <Void/Logging/ConsoleLogger.hpp>
#include <Void/Logging/FileLogger.hpp>
#include <Void/Logging/Level.hpp>
#include <Void/Result/Error.hpp>
#include <Void/Result/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);
}