xo-umbrella2/xo-indentlog2/utest/scope.test.cpp
Roland Conybeare fb013ea267
All checks were successful
cmake-docker / cmake-build (clang, clang++) (push) Successful in 30m28s
cmake-docker / cmake-build (gcc, g++) (push) Successful in 29m12s
CI / smoke-test (push) Successful in 53m49s
xo-ppsink +: use appcx for pretty printer config [REFACTOR]
2026-09-02 00:10:49 -04:00

77 lines
2.3 KiB
C++

/** @file scope.test.cpp
*
* @author Roland Conybeare, Jul 2026
*
* Verify the ppsink <-> indentlog2 seam: the arena-free xo::scope (from
* xo-ppsink) drives an arena-backed PrettySink (from xo-indentlog2) exactly
* as it drives a FlatSink, when the PrettySink is installed as the thread's
* active sink.
**/
#include "print/PrettySink.hpp"
#include <xo/arena/ArenaConfig.hpp>
#include <xo/ppsink/color.hpp>
#include <xo/ppsink/scope.hpp>
#include <catch2/catch.hpp>
#include <sstream>
#include <string>
#include <memory>
namespace ut {
using xo::pp::scope;
using xo::pp::scope_config;
using xo::pp::PrettySink;
using xo::pp::PpConfig;
using xo::pp::PpStyle;
using xo::pp::ThreadLogState;
using xo::mm::ArenaConfig;
/** run @p fn with a PrettySink installed as the thread's active sink;
* return the pretty-printed output.
*
* Capture via a dest streambuf (not pp.output()): PrettySink drains each
* completed record to the attached sink and reclaims its buffer, so
* output() only ever holds the current (unfinished) record.
**/
template <typename Fn>
static std::string scoped_pretty(std::uint32_t margin, Fn && fn) {
std::ostringstream oss;
auto pp = std::make_unique<PrettySink>(PpConfig::scratch_plain(margin), oss.rdbuf());
ThreadLogState::log_set_sink(std::move(pp));
fn();
ThreadLogState::log_set_sink(nullptr); /* restore default (clog) */
return oss.str();
}
/** the same nested-scope sequence exercised by xo-ppsink's FlatSink test **/
static void nested_scopes() {
scope outer("outer");
outer.log("hello");
{
scope inner("inner");
inner.log("world", 42);
}
outer.log("bye");
}
TEST_CASE("scope-over-prettysink", "[scope][prettysink]")
{
scope_config::time_enabled = false;
/* wide (default) margin: nothing breaks, so pretty output matches the
* flat output produced by the same scope code in xo-ppsink's test.
*/
REQUIRE(scoped_pretty(135, nested_scopes) ==
"+(0) outer\n"
" hello\n"
" +(1) inner\n"
" world42\n"
" -(1) inner\n"
" bye\n"
"-(0) outer\n");
}
}
/* end scope.test.cpp */