#include #include #include #include #include using Void::Logging::AnsiColor; using Void::Logging::ColorPolicy; using Void::Logging::Entry; using Void::Logging::Level; using Void::Logging::PatternFormatter; namespace { Entry make_entry() { Entry e; e.level = Level::Warning; e.message = "disk full"; e.fields = {{"service", "ingest"}, {"request_id", "abc123"}}; return e; } } // namespace // ── width / alignment // ───────────────────────────────────────────────────────── Void::Testing::Test pattern_left_pad_width() { PatternFormatter fmt{"[{level:<10}]"}; Void::Testing::expect_eq(fmt.format(make_entry()), std::string{"[Warning ]"}); } Void::Testing::Test pattern_right_pad_width() { PatternFormatter fmt{"[{level:>10}]"}; Void::Testing::expect_eq(fmt.format(make_entry()), std::string{"[ Warning]"}); } Void::Testing::Test pattern_center_pad_width() { PatternFormatter fmt{"[{level:^9}]"}; // "Warning" is 7 chars, width 9 → 1 left, 1 right Void::Testing::expect_eq(fmt.format(make_entry()), std::string{"[ Warning ]"}); } Void::Testing::Test pattern_width_smaller_than_text_is_noop() { PatternFormatter fmt{"{level:<3}"}; Void::Testing::expect_eq(fmt.format(make_entry()), std::string{"Warning"}); } // ── custom tokens & fields // ──────────────────────────────────────────────────── Void::Testing::Test pattern_reads_structured_field() { PatternFormatter fmt{"{service}/{request_id}: {message}"}; Void::Testing::expect_eq(fmt.format(make_entry()), std::string{"ingest/abc123: disk full"}); } Void::Testing::Test pattern_unknown_token_stays_literal() { PatternFormatter fmt{"{nope} {message}"}; Void::Testing::expect_eq(fmt.format(make_entry()), std::string{"{nope} disk full"}); } Void::Testing::Test pattern_custom_token_registered() { PatternFormatter fmt{"<{shout}> {message}"}; fmt.register_token("shout", [](const Entry& e) { std::string up = e.message; for (auto& c : up) { c = static_cast(std::toupper(static_cast(c))); } return up; }); Void::Testing::expect_eq(fmt.format(make_entry()), std::string{" disk full"}); } Void::Testing::Test pattern_field_with_alignment() { PatternFormatter fmt{"{service:>8}"}; Void::Testing::expect_eq(fmt.format(make_entry()), std::string{" ingest"}); } // ── color // ───────────────────────────────────────────────────────────────────── Void::Testing::Test pattern_no_color_by_default() { PatternFormatter fmt{"{level:,auto} {message}"}; // Policy disabled by default → no escape codes. const auto out = fmt.format(make_entry()); Void::Testing::expect(out.find('\x1b') == std::string::npos); Void::Testing::expect_eq(out, std::string{"Warning disk full"}); } Void::Testing::Test pattern_auto_color_uses_level_color() { PatternFormatter fmt{"{level:,auto}"}; fmt.set_color_policy(ColorPolicy::defaults()); const auto out = fmt.format(make_entry()); // Warning → bright yellow (93), wrapped + reset, text intact. Void::Testing::expect(out.find("\x1b[93m") != std::string::npos); Void::Testing::expect(out.find("Warning") != std::string::npos); Void::Testing::expect(out.find("\x1b[0m") != std::string::npos); } Void::Testing::Test pattern_fixed_color() { PatternFormatter fmt{"{message:,cyan}"}; ColorPolicy p; p.enabled = true; fmt.set_color_policy(p); const auto out = fmt.format(make_entry()); Void::Testing::expect(out.find("\x1b[36m") != std::string::npos); Void::Testing::expect(out.find("disk full") != std::string::npos); } Void::Testing::Test color_name_parsing() { Void::Testing::expect(Void::Logging::parse_color("red") == AnsiColor::Red); Void::Testing::expect(Void::Logging::parse_color("bright_magenta") == AnsiColor::BrightMagenta); Void::Testing::expect(Void::Logging::parse_color("dim") == AnsiColor::BrightBlack); Void::Testing::expect(Void::Logging::parse_color("notacolor") == AnsiColor::Default); }