Base class for builders to produce instance of objects with a fluent builder-like syntax. Also includes utilities like randomizing property values.
More...
|
IGenericBuilder | GenericWithRandomProps () |
| Sets up the builder to build with random property values unless the level specified is too deep, in which case it bails out.- Returns
- The current builder instance
|
IGenericBuilder | WithBuildLevel (int level) |
| Sets the maximum depth to traverse when randomizing properties.- Parameters
-
level | How far down the rabbit-hole to go |
- Returns
- The builder
|
object | GenericBuild () |
| Builds an instance of the object this builder builds.- Returns
- Instance of object for which this builder is designed, with all properties as default values
|
object | GenericDeepBuild () |
| Builds an instance of the object this builder builds.- Returns
- Instance of object for which this builder is designed, with properties default but complex objects filled in default
|
TBuilder | WithProp (Action< TEntity > action) |
| Generic method to set a property on the entity.
|
TBuilder | WithProp (ActionRef< TEntity > action) |
| Generic method to set a property on an entity when that entity is a struct type.
|
virtual TEntity | ConstructEntity () |
| Constructs a new instance of the entity. Mostly, an inheritor won't have to care, but if your entity has no parameterless constructor, you'll want to override this in your derived builder.
|
virtual TEntity | Build () |
| Builds the instance of TEntity, applying all builder actions in order to provide the required entity.
|
virtual TBuilder | WithDefaultDateTimeKind (DateTimeKind dateTimeKind) |
| Sets the default DateTimeKind to be expected on DateTime properties randomly generated by this builder.
|
virtual TBuilder | WithRandomProps () |
| Randomizes all properties on the instance of TEntity being built. This method will use methods from RandomValueGen and may generate new GenericBuilder types for generating more complex properties.
|
virtual TBuilder | WithFilledCollections () |
| Attempts to fill collections with random data. May fail with stack-overflows on complex, cyclic-referencing objects. Not enabled by default on random builds, use with caution – may lead to a stack overflow with collections which contain items whose types which have collections with items whose types... and so on.
|
|
static TBuilder | Create () |
| Creates a new instance of the builder; used to provide a fluent syntax NB: You must implement anew this in a derived class if you expect decorations (eg [RequireNonNull]) to be properly applied at all levels after using syntax like DerivedBuilder.BuildRandom() or DerivedBuilder.Create().WithRandomProps().Build()
|
static TEntity | BuildDefault () |
| Builds a default instance of the entity.
|
static TEntity | BuildRandom () |
| Convenience method: Creates a builder, sets random properties, returns a new instance of the entity.
|
static void | WithDefaultProp (Action< TEntity > action) |
| Adds a default property setter, shared amongst all instances of this particular builder type.
|
Static Public Member Functions inherited from PeanutButter.RandomGenerators.GenericBuilderBase |
static bool | TryCreateSubstituteFor< T > (bool callThrough, out T result) |
| Attempt to create a substitute for the given type.
|
static bool | TryCreateSubstituteFor< T > (bool callThrough, object[] constructorParameters, out T result) |
| Attempt to create a substitute for the given type with parameters.
|
static bool | TryCreateSubstituteFor< T > (bool throwOnError, bool callThrough, out T result) |
| Attempt to create a substitute from a type with a parameterless constructor.
|
static bool | TryCreateSubstituteFor< T > (bool throwOnError, bool callThrough, object[] constructorParameters, out T result) |
| Attempts perform Substitute.For<T>.
|
|
readonly List< object > | CreatedValues = new() |
|
object[] | ParameterValues [get] |
bool | Success = true [get, set] |
Properties inherited from PeanutButter.RandomGenerators.GenericBuilderBase |
static int | MaxRandomPropsLevel = 10 [get, set] |
| Sets the maximum level to go to when generating random properties of random properties, to prevent stack and memory overflows.
|
|
Static Public Attributes inherited from PeanutButter.RandomGenerators.GenericBuilderBase |
const string | ENV_FORCE_NSUBSTITUTE_FOR_RANDOMVALUEGEN = "FORCE_NSUBSTITUTE_FOR_RANDOMVALUEGEN" |
| The environment variable which is observed to force using NSubstitute for random value generation of interface types.
|
Static Protected Member Functions inherited from PeanutButter.RandomGenerators.GenericBuilderBase |
static Assembly | FindOrLoadAssembly< T > (string name, bool retrying) |
| Attempts to load the assembly alongside the Type T.
|
Static Protected Attributes inherited from PeanutButter.RandomGenerators.GenericBuilderBase |
static readonly Dictionary< Type, Type > | DynamicBuilders = new Dictionary<Type, Type>() |
| Holds a lookup of all GenericBuilder classes which have been generated to facilitate automatic property building. Searched when looking for a builder to generate a property before attempting to generate a builder type (re-use)
|
static readonly Dictionary< Type, Type > | UserBuilders = new Dictionary<Type, Type>() |
| Holds a lookup of GenericBuilder classes which were provided by consuming code. Searched before searching the DynamicBuilders lookup or attempting to create an auto-generated builder type.
|
static readonly Type | NullableGeneric = typeof(Nullable<>) |
| Provides a lookup to the type which is the generic Nulllable.
|
static readonly Type | GenericBuilderBaseType = typeof(GenericBuilderBase) |
| Provides a lookup to the type which is the GenericBuilder.
|
Base class for builders to produce instance of objects with a fluent builder-like syntax. Also includes utilities like randomizing property values.
- Template Parameters
-
TBuilder | Concrete type of the current builder, required to be able to return the builder from all With* methods |
TEntity | Type of entity this builder builds |
- Type Constraints
-
TBuilder | : | GenericBuilder | |
TBuilder | : | TBuilder | |
TBuilder | : | TEntity | |
◆ Build()
virtual TEntity PeanutButter.RandomGenerators.GenericBuilder< TBuilder, TEntity >.Build |
( |
| ) |
|
|
virtual |
◆ BuildDefault()
TEntity PeanutButter.RandomGenerators.GenericBuilder< TBuilder, TEntity >.BuildDefault |
( |
| ) |
|
|
static |
Builds a default instance of the entity.
- Returns
- New instance of the builder entity
◆ BuildRandom()
TEntity PeanutButter.RandomGenerators.GenericBuilder< TBuilder, TEntity >.BuildRandom |
( |
| ) |
|
|
static |
Convenience method: Creates a builder, sets random properties, returns a new instance of the entity.
- Returns
- New instance of TEntity with all randomizable properties randomized
◆ ConstructEntity()
virtual TEntity PeanutButter.RandomGenerators.GenericBuilder< TBuilder, TEntity >.ConstructEntity |
( |
| ) |
|
|
virtual |
Constructs a new instance of the entity. Mostly, an inheritor won't have to care, but if your entity has no parameterless constructor, you'll want to override this in your derived builder.
- Returns
- New instance of TEntity, constructed from the parameterless constructor, when possible
- Exceptions
-
◆ Create()
TBuilder PeanutButter.RandomGenerators.GenericBuilder< TBuilder, TEntity >.Create |
( |
| ) |
|
|
static |
Creates a new instance of the builder; used to provide a fluent syntax NB: You must implement anew this in a derived class if you expect decorations (eg [RequireNonNull]) to be properly applied at all levels after using syntax like DerivedBuilder.BuildRandom() or DerivedBuilder.Create().WithRandomProps().Build()
- this may not seem obvious, but the base class' static method will be called otherwise. This does not apply if you're using GetRandom<T>() as this will use new DerivedBuilder() which will ensure that decorations are read through the entire hierarchy
- Returns
- New instance of the builder
◆ GenericBuild()
object PeanutButter.RandomGenerators.GenericBuilder< TBuilder, TEntity >.GenericBuild |
( |
| ) |
|
◆ GenericDeepBuild()
object PeanutButter.RandomGenerators.GenericBuilder< TBuilder, TEntity >.GenericDeepBuild |
( |
| ) |
|
Builds an instance of the object this builder builds.
- Returns
- Instance of object for which this builder is designed, with properties default but complex objects filled in default
Implements PeanutButter.RandomGenerators.IGenericBuilder.
◆ GenericWithRandomProps()
IGenericBuilder PeanutButter.RandomGenerators.GenericBuilder< TBuilder, TEntity >.GenericWithRandomProps |
( |
| ) |
|
◆ WithBuildLevel()
IGenericBuilder PeanutButter.RandomGenerators.GenericBuilder< TBuilder, TEntity >.WithBuildLevel |
( |
int | level | ) |
|
◆ WithDefaultDateTimeKind()
virtual TBuilder PeanutButter.RandomGenerators.GenericBuilder< TBuilder, TEntity >.WithDefaultDateTimeKind |
( |
DateTimeKind | dateTimeKind | ) |
|
|
virtual |
Sets the default DateTimeKind to be expected on DateTime properties randomly generated by this builder.
- Parameters
-
dateTimeKind | Expected DateTimeKind. Setting Unspecified will result in the default DateTimeKind (Local) |
- Returns
◆ WithDefaultProp()
void PeanutButter.RandomGenerators.GenericBuilder< TBuilder, TEntity >.WithDefaultProp |
( |
Action< TEntity > | action | ) |
|
|
static |
Adds a default property setter, shared amongst all instances of this particular builder type.
- Parameters
-
action | Action to perform on the entity being built, will run before any actions specified on the instance |
◆ WithFilledCollections()
virtual TBuilder PeanutButter.RandomGenerators.GenericBuilder< TBuilder, TEntity >.WithFilledCollections |
( |
| ) |
|
|
virtual |
Attempts to fill collections with random data. May fail with stack-overflows on complex, cyclic-referencing objects. Not enabled by default on random builds, use with caution – may lead to a stack overflow with collections which contain items whose types which have collections with items whose types... and so on.
- Returns
- The current instance of the builder
◆ WithProp() [1/2]
TBuilder PeanutButter.RandomGenerators.GenericBuilder< TBuilder, TEntity >.WithProp |
( |
Action< TEntity > | action | ) |
|
◆ WithProp() [2/2]
TBuilder PeanutButter.RandomGenerators.GenericBuilder< TBuilder, TEntity >.WithProp |
( |
ActionRef< TEntity > | action | ) |
|
◆ WithRandomProps()
virtual TBuilder PeanutButter.RandomGenerators.GenericBuilder< TBuilder, TEntity >.WithRandomProps |
( |
| ) |
|
|
virtual |
Randomizes all properties on the instance of TEntity being built. This method will use methods from RandomValueGen and may generate new GenericBuilder types for generating more complex properties.
- Returns
- The current builder instance
The documentation for this class was generated from the following file:
- source/TestUtils/PeanutButter.RandomGenerators/GenericBuilder.cs