Hegel 0.18.2
Property-based testing for C++
Loading...
Searching...
No Matches
hegel::Printer Class Reference

Writes to the document that a failure report shows. More...

#include <printer.h>

Public Member Functions

 Printer (Printer &&other) noexcept
 Move-constructs, transferring the region.
Printer & operator= (Printer &&other) noexcept
 Move-assigns, transferring the region.
 Printer (const Printer &)=delete
Printer & operator= (const Printer &)=delete
bool should_print () const
 Whether printing to this printer gives output.
void text (std::string_view text)
 Write text that does not break.
template<typename T>
void repr (const T &value)
 Write the C++ representation of a value.
void reflow (std::string_view text)
 Write text that some other formatter made, with its bracket structure recovered.
void breakable (std::string_view separator=" ")
 Write a point where the line can break.
void if_break (std::string_view text)
 Write text only when the innermost group breaks.
void hard_break ()
 Write a new line, then the current indentation.
void begin_group (std::size_t indent, std::string_view open)
 Write open and start a group.
void end_group (std::string_view close)
 End the innermost group and write close.
template<typename F>
void group (std::size_t indent, std::string_view open, std::string_view close, F &&body)
 Write a group: open, what body writes, then close.
template<typename Range, typename F>
void seq (std::string_view open, std::string_view close, const Range &items, F &&print_item)
 Write a list of items, separated by commas.
void separator ()
 Write the separator between two items: , and breakable(" ").
void shift_indent (std::ptrdiff_t delta)
 Change the indentation of the break points that follow.
void comment (std::string_view text)
 Write // <text> at the end of the current line.
Speculation speculate ()
 Start a speculative region on this printer.
Printer deferred ()
 Make a child region at the current position.

Static Public Member Functions

static Printer noop ()
 A printer that discards everything.

Detailed Description

Writes to the document that a failure report shows.

When a test fails, Hegel prints each drawn value as a C++ declaration, auto x = <value>;. A printable generator writes the <value> part itself, through a Printer, while it draws: see generators::IPrintableGenerator and generators::AnyGenerator::print_with.

Hegel for C++ only says what to print and where a line can break. libhegel lays the document out: a group fits on one line, or each of its break points becomes a new line.

hegel::Document document(12);
document.printer().seq("{", "}", std::vector<int>{1000, 2000, 3000},
[](hegel::Printer& p, int n) { p.repr(n); });
std::string out = std::move(document).finish();
// {
// 1000,
// 2000,
// 3000
// }
A document of its own, outside a test case.
Definition printer.h:326
Writes to the document that a failure report shows.
Definition printer.h:55
void repr(const T &value)
Write the C++ representation of a value.
Definition repr.h:713

A Printer is move-only and has one owner (one thread) at a time. A no-op printer (noop()) discards everything, which lets one drawing body serve a silent draw and a printed draw. A printer whose region is dead (see deferred()) also discards everything. A misuse of the protocol, such as end_group() with no open group, throws std::logic_error.

Constructor & Destructor Documentation

◆ Printer()

hegel::Printer::Printer ( Printer && other)
noexcept

Move-constructs, transferring the region.

Parameters
otherThe printer to move from. It becomes a no-op printer.

Member Function Documentation

◆ begin_group()

void hegel::Printer::begin_group ( std::size_t indent,
std::string_view open )

Write open and start a group.

The break points of the group indent their new lines indent columns more.

Parameters
indentExtra indentation of the group's broken lines.
openText written before the group.

◆ breakable()

void hegel::Printer::breakable ( std::string_view separator = " ")

Write a point where the line can break.

Parameters
separatorThe text written when the group fits on one line.

◆ comment()

void hegel::Printer::comment ( std::string_view text)

Write // <text> at the end of the current line.

Every group open at this point breaks, so nothing else is written after the comment on its line.

Parameters
textThe comment. It must not contain a newline.

◆ deferred()

Printer hegel::Printer::deferred ( )

Make a child region at the current position.

What the child gets later, from any thread that owns it, shows at this position when the document is read. A generator uses this to print a value whose representation is known only after the draw, as randoms() does with the values it gives. The child is dead (its writes are discarded) after the document is read, or when a speculative region that it is in is aborted. The child of a no-op or dead printer is a no-op printer.

Returns
The child region.

◆ end_group()

void hegel::Printer::end_group ( std::string_view close)

End the innermost group and write close.

Parameters
closeText written after the group.
Exceptions
std::logic_errorif no group is open.

◆ group()

template<typename F>
void hegel::Printer::group ( std::size_t indent,
std::string_view open,
std::string_view close,
F && body )
inline

Write a group: open, what body writes, then close.

The scoped form of begin_group() / end_group().

p.group(5, "make(", ")", [](hegel::Printer& p) {
p.text("1");
p.separator();
p.text("2");
});
void separator()
Write the separator between two items: , and breakable(" ").
void text(std::string_view text)
Write text that does not break.
Template Parameters
FA callable taking hegel::Printer&
Parameters
indentExtra indentation of the group's broken lines.
openText written before the group.
closeText written after the group.
bodyWrites the content of the group.

◆ if_break()

void hegel::Printer::if_break ( std::string_view text)

Write text only when the innermost group breaks.

Use it for text that only the broken form needs, such as a trailing comma.

Parameters
textThe text. It must not contain a newline.

◆ noop()

Printer hegel::Printer::noop ( )
static

A printer that discards everything.

Returns
A printer whose should_print() is false.

◆ operator=()

Printer & hegel::Printer::operator= ( Printer && other)
noexcept

Move-assigns, transferring the region.

Parameters
otherThe printer to move from. It takes the region this printer had, and releases it when it is destroyed.
Returns
Reference to this printer.

◆ reflow()

void hegel::Printer::reflow ( std::string_view text)

Write text that some other formatter made, with its bracket structure recovered.

The engine finds the groups of (...), [...] and {...} in text and their , separated items, so the text can break like structured output.

Parameters
textThe formatted text. It can contain newlines.

◆ repr()

template<typename T>
void hegel::Printer::repr ( const T & value)

Write the C++ representation of a value.

The representation is a C++ expression (std::vector<int>{1, 2}, std::string("a"), 1.5f) with groups and break points, so the engine can break a wide value across lines. A type prints itself through a void print_to(hegel::Printer&) const member, a reflectable aggregate (C++20) prints its fields, and other types print the text of their operator<<, through reflow(). See hegel::is_representable_v. Nothing is formatted unless should_print().

Template Parameters
TThe type of value
Parameters
valueThe value to write.

◆ seq()

template<typename Range, typename F>
void hegel::Printer::seq ( std::string_view open,
std::string_view close,
const Range & items,
F && print_item )
inline

Write a list of items, separated by commas.

On one line when it fits ({1, 2}). Else each item is on a line of its own, indented four columns, and close is on a line of its own. This is the layout of the containers that Hegel prints.

Template Parameters
RangeA range of items
FA callable taking hegel::Printer& and an item
Parameters
openText written before the items.
closeText written after the items.
itemsThe items.
print_itemWrites one item.

◆ shift_indent()

void hegel::Printer::shift_indent ( std::ptrdiff_t delta)

Change the indentation of the break points that follow.

Parameters
deltaColumns to add. Can be negative.

◆ should_print()

bool hegel::Printer::should_print ( ) const

Whether printing to this printer gives output.

False for noop() and for a dead region. Use it to skip work, such as formatting, whose only purpose is to be printed.

Returns
Whether writes to this printer are kept.

◆ speculate()

Speculation hegel::Printer::speculate ( )

Start a speculative region on this printer.

What is written to this printer while the region is open is kept only if Speculation::commit() is called. Speculation::abort(), or destroying the Speculation without a commit, discards it. Speculations nest. Close them in the reverse order of opening.

Returns
The open region.

◆ text()

void hegel::Printer::text ( std::string_view text)

Write text that does not break.

Each newline in text is a hard_break().

Parameters
textThe text to write.

The documentation for this class was generated from the following files: