|
Hegel 0.18.2
Property-based testing for C++
|
A generator: a handle to an IGenerator. More...
#include <core.h>
Public Types | |
| using | value_type = T |
| The type of value this generator produces. | |
Public Member Functions | |
| template<typename F> | |
| Mapped< std::invoke_result_t< F, T >, false > | map (F &&f) const |
| Transform generated values with a function. | |
| template<typename F> | |
| auto | flat_map (F &&f) const |
| Chain generators for dependent generation. | |
| AnyGenerator< T > | filter (std::function< bool(const T &)> pred) const |
| Filter generated values by a predicate. | |
| template<typename F> | |
| Generator< T > | print_with (F &&print) const |
Make a printable generator that prints its values with print. | |
| Generator< T > | print_as_repr () const |
| Make a printable generator that prints its values by their C++ representation (Printer::repr()). | |
A generator: a handle to an IGenerator.
Copies share the generator. An AnyGenerator may not be printable: draw it with TestCase::draw_silent(), or make it printable with print_with() or print_as_repr(). Generator, the printable handle, derives from it, so every generator is an AnyGenerator.
A function that takes or returns a generator that may not be printable spells its type AnyGenerator<T>. Converting a Generator<T> to AnyGenerator<T> drops its printing.
| T | The type to generate values for |
| AnyGenerator< T > hegel::generators::AnyGenerator< T >::filter | ( | std::function< bool(const T &)> | pred | ) | const |
Filter generated values by a predicate.
Each draw tries a few values. If none satisfies the predicate, the test case is rejected, as by TestCase::reject(). So a predicate that few values satisfy makes most test cases fail to generate. For example, if you want sorted lists of length N, you should generate lists of length N and sort them, not generate random lists and filter by a predicate of 'length == N && is_sorted'.
The result prints the accepted value as this generator prints it.
| pred | Predicate that values must satisfy |
| auto hegel::generators::AnyGenerator< T >::flat_map | ( | F && | f | ) | const |
Chain generators for dependent generation.
Given a function T -> generator of U, makes a generator of U. Useful when generation parameters depend on previously generated values. The value drawn from this generator is not printed. The result prints what the generator that f returns prints, so it is a Generator<U> when f returns a Generator<U>, and an AnyGenerator<U> otherwise.
| F | Function type (T -> generator of U) |
| f | Function that takes a T and returns a generator |
f returns | Mapped< std::invoke_result_t< F, T >, false > hegel::generators::AnyGenerator< T >::map | ( | F && | f | ) | const |
Transform generated values with a function.
Given a generator of T and a function T -> U, makes a generator of U. The result prints the value it made, by its C++ representation, so it is a printable Generator<U> when hegel::is_representable_v is true for U.
| F | Function type (T -> U) |
| f | Transformation function with signature U f(T) |
| Generator< T > hegel::generators::AnyGenerator< T >::print_as_repr | ( | ) | const |
Make a printable generator that prints its values by their C++ representation (Printer::repr()).
Requires hegel::is_representable_v for T.
| Generator< T > hegel::generators::AnyGenerator< T >::print_with | ( | F && | ) | const |
Make a printable generator that prints its values with print.
Draws as this generator does, then calls print with the value and the printer. Use it for a type with no C++ representation, to print a value another way, or to hide a secret.
| F | Callable taking const T& and hegel::Printer& |
| Writes the representation of a value. |