Function getArbitrary
Get a random value of type T
.
Randomly generates necessary ctor arguments.
Depending on Config
, all fields of structs and classes are random initialized.
Parameters
Name | Description |
---|---|
Generators | custom factory functions for specific types |
config | Configuration for random generation |
Throws
CyclicDependencyException - when ctor arguments cyclically depend on each other
Example
basic usage
auto sarray = getArbitrary!(int[4])();
auto array = getArbitrary!(float[])();
auto aarray = getArbitrary!(int[string])();
Example
fields not randomized
static struct Tuple { TypeTuple!(uint, float) vals; alias vals this; }
Config config;
config .randomizeFields = false;
auto val = getArbitrary!(Tuple)(config);
assert(val[0] == uint .init);
assert(isNaN(val[1]));
Example
complex example with ctor arguments and generators
static struct UserStruct
{
this(int val) { this .val = val; }
int val;
}
static struct UserStructHolder
{
this(UserStruct val) { this .val = val; }
UserStruct val;
}
static struct UserStructInit
{
UserStruct val;
}
static UserStruct generator()
{
return UserStruct(10);
}
auto val = getArbitrary!(UserStruct, generator)();
assert(val .val == 10);
auto val2 = getArbitrary!(UserStructHolder, generator)();
assert(val2 .val .val == 10);
auto val3 = getArbitrary!(UserStructInit, generator)();
assert(val3 .val .val == 10);
Example
picks a random ctor
static class MultiCtors
{
uint val;
this(uint)
{
this .val = 1;
}
this(uint, uint)
{
this .val = 2;
}
this(uint, uint, uint)
{
this .val = 3;
}
this(uint, uint, uint, uint)
{
this .val = 4;
}
}
auto initial = getArbitrary!(MultiCtors)();
auto i = 0;
while (initial .val == getArbitrary!(MultiCtors)() .val)
assert(++i < 100);
Example
field can be configured to be non-random
static class ClassWCtor
{
this()
{
}
int m;
}
static class TestClass
{
ClassWCtor val;
}
static struct TestStruct
{
ClassWCtor val;
}
static class ClassWOCtor
{
int m;
}
Config config;
config .randomizeFields = false;
auto cl = getArbitrary!TestClass(config);
assert(cl .val is null);
cl = getArbitrary!(TestClass)();
assert(!(cl .val is null));