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

A generator: a handle to an IGenerator. More...

#include <core.h>

Inheritance diagram for hegel::generators::AnyGenerator< T >:
hegel::generators::Generator< T >

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()).

Detailed Description

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

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.

Template Parameters
TThe type to generate values for

Member Function Documentation

◆ filter()

template<typename T>
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.

auto even = integers<int>({.min_value = 0, .max_value = 100})
.filter([](int x) { return x % 2 == 0; });
// even is Generator<int>
AnyGenerator< T > filter(std::function< bool(const T &)> pred) const
Filter generated values by a predicate.
Definition core.h:874
Generator< T > integers(IntegersParams< T > params={})
Generate random integers. For a given integral type T, produces values in the range [std::numeric_lim...
Definition numeric.h:301
Parameters
predPredicate that values must satisfy
Returns
A generator producing only values satisfying pred

◆ flat_map()

template<typename T>
template<typename F>
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.

auto sized_string =
integers<size_t>({.min_value = 1, .max_value = 10})
.flat_map([](size_t len) {
return text({.min_size = len, .max_size = len});
});
// sized_string is Generator<std::string>
auto flat_map(F &&f) const
Chain generators for dependent generation.
Definition core.h:862
Generator< std::string > text(const TextParams &params={})
Generate random text strings.
Template Parameters
FFunction type (T -> generator of U)
Parameters
fFunction that takes a T and returns a generator
Returns
A generator of the values drawn from the generators f returns
See also
map(), text()

◆ map()

template<typename T>
template<typename F>
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.

auto halved = integers<int>().map(
[](int x) { return double(x) / 2.0; }
);
Template Parameters
FFunction type (T -> U)
Parameters
fTransformation function with signature U f(T)
Returns
A Mapped handle, which derives from Generator<U> or AnyGenerator<U>
See also
Mapped::print_as_call(), flat_map()

◆ print_as_repr()

template<typename 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.

Returns
A Generator<T>

◆ print_with()

template<typename T>
template<typename F>
Generator< T > hegel::generators::AnyGenerator< T >::print_with ( F && print) 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.

auto ids = integers<int>({.min_value = 0}).print_with(
[](int id, hegel::Printer& p) {
p.text("UserId(" + std::to_string(id) + ")");
});
Writes to the document that a failure report shows.
Definition printer.h:55
void text(std::string_view text)
Write text that does not break.
Generator< T > print_with(F &&print) const
Make a printable generator that prints its values with print.
Definition core.h:888
Template Parameters
FCallable taking const T& and hegel::Printer&
Parameters
printWrites the representation of a value.
Returns
A Generator<T>

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