Generators#
Generators are at the core of the generators
submodule's business logic. They are responsible for generating expectations based on the values object and the given definition.
Each generator must adhere to the following protocol:
Generator Factories#
When generating expectations, generators are created from definitions using factories. The factory maintains an ordered sequence of generator classes. When creating a generator for a specific definition, the factory iterates over the classes and uses the first one for which the matches
method returns True
.
The module provides a BuiltInGeneratorFactory
that includes all out-of-the-box generators. Additionally, there is a blank factory called GeneratorFactory
that does not have any generators registered.
To add a generator to the factory, use the register_generator
class method with the following signature:
Python | |
---|---|
The generator is registered at the earliest possible position. If after
and before
parameters are not set, it is added to the beginning of the list to be matched when creating a generator for a definition. The after
and before
parameters allow you to specify constraints on the generator's position.
When creating a subclass of a factory, it initially uses the same generators collection as its base class. However, once the first generator is registered, the collection from the base class is copied before adding new generators.