genericArbitrary

Pick a constructor with a given distribution, and fill its fields with recursive calls to arbitrary.

Example

genericArbitrary (2 % 3 % 5 % ()) :: Gen a
Picks the first constructor with probability 2/10, the second with probability 3/10, the third with probability 5/10.
Create a arbitrary generator for a specified a type in a naive way. Please be careful when using this function, particularly for recursive types.
Pick a constructor with a given distribution, and fill its fields with recursive calls to arbitrary.

Example

data X = ...
deriving Arbitrary via (GenericArbitrary '[2, 3, 5] X)
Picks the first constructor with probability 2/10, the second with probability 3/10, the third with probability 5/10. This newtype does no shrinking. To add generic shrinking, use AndShrinking. Uses genericArbitrary.
Newtype for DerivingVia Usage:
data Foo = Foo
{ _fooX :: X
, _fooY :: Y
} deriving (Generic)
deriving (Arbitrary) via GenericArbitrary Foo
Decrease size to ensure termination for recursive types, looking for base cases once the size reaches 0.
genericArbitrary' (17 % 19 % 23 % ()) :: Gen a
N.B.: This replaces the generator for fields of type [t] with listOf' arbitrary instead of listOf arbitrary (i.e., arbitrary for lists).
genericArbitrary with explicit generators.

Example

genericArbitraryG customGens (17 % 19 % ())
where, the generators for String and Int fields are overridden as follows, for example:
customGens :: Gen String :+ Gen Int
customGens =
(filter (/= 'NUL') <$> arbitrary) :+
(getNonNegative <$> arbitrary)

Note on multiple matches

Multiple generators may match a given field: the first will be chosen.
Decrease size at every recursive call, but don't do anything different at size 0.
genericArbitraryRec (7 % 11 % 13 % ()) :: Gen a
N.B.: This replaces the generator for fields of type [t] with listOf' arbitrary instead of listOf arbitrary (i.e., arbitrary for lists).
genericArbitraryRec with explicit generators. See also genericArbitraryG.
arbitrary for types with one constructor. Equivalent to genericArbitraryU, with a stricter type.
genericArbitrarySingle :: Gen a
genericArbitrarySingle with explicit generators. See also genericArbitraryG.
Pick every constructor with equal probability. Equivalent to genericArbitrary uniform.
genericArbitraryU :: Gen a
Equivalent to genericArbitrary' uniform.
genericArbitraryU' :: Gen a
N.B.: This replaces the generator for fields of type [t] with listOf' arbitrary instead of listOf arbitrary (i.e., arbitrary for lists).
genericArbitraryU with explicit generators. See also genericArbitraryG.
General generic generator with custom options.
GenericArbitrary with explicit generators.

Example

data X = ...
deriving Arbitrary via (GenericArbitraryG CustomGens '[2, 3, 5] X)
where, for example, custom generators to override String and Int fields might look as follows:
type CustomGens = CustomString :+ CustomInt

Note on multiple matches

Multiple generators may match a given field: the first will be chosen. This newtype does no shrinking. To add generic shrinking, use AndShrinking. Uses genericArbitraryG.
Decrease size at every recursive call, but don't do anything different at size 0.
data X = ...
deriving Arbitrary via (GenericArbitraryRec '[2, 3, 5] X)
N.B.: This replaces the generator for fields of type [t] with listOf' arbitrary instead of listOf arbitrary (i.e., arbitrary for lists). This newtype does no shrinking. To add generic shrinking, use AndShrinking. Uses genericArbitraryRec.
genericArbitraryRec with explicit generators. See also genericArbitraryG. This newtype does no shrinking. To add generic shrinking, use AndShrinking. Uses genericArbitraryRecG.
arbitrary for types with one constructor. Equivalent to GenericArbitraryU, with a stricter type. This newtype does no shrinking. To add generic shrinking, use AndShrinking. Uses genericArbitrarySingle.