Files
Library/tests/standalone/Format/Format.cpp
T
faumarayandClaude Sonnet 4.6 b54f2156c5 Add Numeric and Format modules
Void::Numeric — checked_add/sub/mul/div returning std::expected<T,Error>;
saturating_add/sub/mul clamping at min/max; wrapping_add/sub/mul for unsigned;
clamp, parse_int (with base), parse_double, narrow_cast.

Void::Format — render() named-placeholder template engine ({key} syntax,
{{}} escaping); render_pos() positional ({0},{1}); indent() per-line prefix;
word_wrap() at whitespace boundaries; format_table() with auto column alignment.

44/44 tests pass.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-14 22:59:55 +03:00

132 lines
4.5 KiB
C++

#include <Void/Format/Format.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
#include <vector>
// ── render (named) ────────────────────────────────────────────────────────────
Void::Testing::Test render_single_placeholder()
{
const auto r = Void::Format::render("Hello, {name}!",
{{"name", "World"}});
Void::Testing::expect_eq(r, std::string{"Hello, World!"});
}
Void::Testing::Test render_multiple_placeholders()
{
const auto r = Void::Format::render("{a} + {b} = {c}",
{{"a","1"},{"b","2"},{"c","3"}});
Void::Testing::expect_eq(r, std::string{"1 + 2 = 3"});
}
Void::Testing::Test render_unknown_key_left_as_is()
{
const auto r = Void::Format::render("Hello {unknown}!", {});
Void::Testing::expect_eq(r, std::string{"Hello {unknown}!"});
}
Void::Testing::Test render_escaped_braces()
{
const auto r = Void::Format::render("{{not a placeholder}}", {});
Void::Testing::expect_eq(r, std::string{"{not a placeholder}"});
}
Void::Testing::Test render_no_placeholders()
{
const auto r = Void::Format::render("plain text", {});
Void::Testing::expect_eq(r, std::string{"plain text"});
}
// ── render_pos (positional) ───────────────────────────────────────────────────
Void::Testing::Test render_pos_basic()
{
const auto r = Void::Format::render_pos("{0} and {1}", {"hello", "world"});
Void::Testing::expect_eq(r, std::string{"hello and world"});
}
Void::Testing::Test render_pos_repeat_arg()
{
const auto r = Void::Format::render_pos("{0} {0}", {"echo"});
Void::Testing::expect_eq(r, std::string{"echo echo"});
}
Void::Testing::Test render_pos_out_of_range_left_as_is()
{
const auto r = Void::Format::render_pos("{5}", {"a"});
Void::Testing::expect_eq(r, std::string{"{5}"});
}
// ── indent ────────────────────────────────────────────────────────────────────
Void::Testing::Test indent_adds_spaces()
{
const auto r = Void::Format::indent("hello\nworld", 2);
Void::Testing::expect_eq(r, std::string{" hello\n world"});
}
Void::Testing::Test indent_empty_string()
{
Void::Testing::expect(Void::Format::indent("", 4).empty());
}
Void::Testing::Test indent_single_line()
{
const auto r = Void::Format::indent("hi", 3);
Void::Testing::expect_eq(r, std::string{" hi"});
}
// ── word_wrap ─────────────────────────────────────────────────────────────────
Void::Testing::Test word_wrap_short_text_unchanged()
{
const auto r = Void::Format::word_wrap("hello world", 80);
Void::Testing::expect_eq(r, std::string{"hello world"});
}
Void::Testing::Test word_wrap_breaks_at_width()
{
const auto r = Void::Format::word_wrap("hello world foo", 11);
Void::Testing::expect(r.find('\n') != std::string::npos);
}
Void::Testing::Test word_wrap_preserves_explicit_newlines()
{
const auto r = Void::Format::word_wrap("line1\nline2", 80);
Void::Testing::expect(r.find('\n') != std::string::npos);
}
// ── format_table ─────────────────────────────────────────────────────────────
Void::Testing::Test format_table_aligns_columns()
{
const std::vector<std::vector<std::string>> rows = {
{"Name", "Age"},
{"Alice", "30"},
{"Bob", "25"},
};
const auto t = Void::Format::format_table(rows);
Void::Testing::expect(t.find("Name") != std::string::npos);
Void::Testing::expect(t.find("Alice") != std::string::npos);
}
Void::Testing::Test format_table_empty_returns_empty()
{
Void::Testing::expect(
Void::Format::format_table({}).empty());
}
Void::Testing::Test format_table_row_count()
{
const std::vector<std::vector<std::string>> rows = {
{"a", "b"},
{"c", "d"},
};
const auto t = Void::Format::format_table(rows);
int newlines = 0;
for (char c : t) { if (c == '\n') ++newlines; }
Void::Testing::expect_eq(newlines, 2);
}