Hegel 0.18.2
Property-based testing for C++
Loading...
Searching...
No Matches
hegel::generators::IPrintableGenerator< T > Class Template Referenceabstract

The interface of a printable generator: one that can print what it draws, as it draws it. More...

#include <core.h>

Inheritance diagram for hegel::generators::IPrintableGenerator< T >:
hegel::generators::IGenerator< T >

Public Member Functions

virtual T do_draw_and_print (const TestCase &tc, Printer &printer) const =0
 Draw a value and print it to printer.
T do_draw (const TestCase &tc) const override
 Draw a value without printing it.
virtual T do_draw_filtered_and_print (const TestCase &tc, const std::function< bool(const T &)> &predicate, Printer &printer) const
 Draw a value that predicate accepts and print it, for filter().
Public Member Functions inherited from hegel::generators::IGenerator< T >
virtual uint64_t label () const
 The label that identifies this generator to the engine.
virtual T do_draw_filtered (const TestCase &tc, const std::function< bool(const T &)> &predicate) const
 Draw a value that predicate accepts, for filter().

Additional Inherited Members

Public Types inherited from hegel::generators::IGenerator< T >
using value_type = T
 The type of value this generator makes.

Detailed Description

template<typename T>
class hegel::generators::IPrintableGenerator< T >

The interface of a printable generator: one that can print what it draws, as it draws it.

A printable generator writes the C++ representation of its value to a Printer while it draws. TestCase::draw() calls do_draw_and_print() with the printer of the failure report, so the report says what was drawn. A container prints each element as the element generator prints it, so printing that one generator chooses shows inside every value built from it.

Implement do_draw_and_print() only: do_draw() calls it with Printer::noop(), so the silent draw and the printed draw make the same choices. Draw other generators with TestCase::draw_and_print() (a printable generator) or TestCase::draw_silent().

struct Point { int x, y; };
class PointGenerator : public gs::IPrintableGenerator<Point> {
public:
Point do_draw_and_print(const hegel::TestCase& tc,
hegel::Printer& p) const override {
p.begin_group(6, "Point(");
int x = tc.draw_and_print(coordinate_, p);
p.separator();
int y = tc.draw_and_print(coordinate_, p);
p.end_group(")");
return {x, y};
}
private:
gs::Generator<int> coordinate_ =
gs::integers<int>({.min_value = 0, .max_value = 9});
};
auto points = gs::make_generator<PointGenerator>();
// A failure report prints `auto p = Point(0, 0);`
Writes to the document that a failure report shows.
Definition printer.h:55
void separator()
Write the separator between two items: , and breakable(" ").
void end_group(std::string_view close)
End the innermost group and write close.
void begin_group(std::size_t indent, std::string_view open)
Write open and start a group.
Handle to the currently-executing test case.
Definition test_case.h:46
T draw_and_print(const generators::Generator< T > &gen, Printer &printer) const
Draw a value from a printable generator and print it to printer.
Definition core.h:1063
Template Parameters
TThe type of value the generator makes

Member Function Documentation

◆ do_draw()

template<typename T>
T hegel::generators::IPrintableGenerator< T >::do_draw ( const TestCase & tc) const
inlineoverridevirtual

Draw a value without printing it.

Calls do_draw_and_print() with Printer::noop().

Parameters
tcThe test case to draw from.
Returns
The value.

Implements hegel::generators::IGenerator< T >.

◆ do_draw_and_print()

template<typename T>
virtual T hegel::generators::IPrintableGenerator< T >::do_draw_and_print ( const TestCase & tc,
Printer & printer ) const
pure virtual

Draw a value and print it to printer.

Must make the same draws as do_draw().

Parameters
tcThe test case to draw from.
printerThe printer to write the value's representation to.
Returns
The value.

◆ do_draw_filtered_and_print()

template<typename T>
virtual T hegel::generators::IPrintableGenerator< T >::do_draw_filtered_and_print ( const TestCase & tc,
const std::function< bool(const T &)> & predicate,
Printer & printer ) const
inlinevirtual

Draw a value that predicate accepts and print it, for filter().

Makes the same attempts as IGenerator::do_draw_filtered(), each in a speculative region of printer that is kept only for the accepted value.

Parameters
tcThe test case to draw from.
predicateThe condition the value must satisfy.
printerThe printer to write the value's representation to.
Returns
The value.

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