-
Notifications
You must be signed in to change notification settings - Fork 0
Customization
akovanev edited this page Nov 21, 2022
·
4 revisions
public class UIntGenerator : GeneratorBase
{
protected override object CreateImpl(PropertyObject propertyObject)
{
Random random = GetRandomInstance(propertyObject, nameof(CreateImpl));
return random.GetInt(0, 1000);
}
protected override object CreateRangeFailureImpl(PropertyObject propertyObject)
{
Random random = GetRandomInstance(propertyObject, nameof(CreateRangeFailureImpl));
return random.GetInt(-100, -1);
}
}
public class ExtendedGeneratorFactory : GeneratorFactory
{
public override Dictionary GetGeneratorDictionary()
{
Dictionary generators = base.GetGeneratorDictionary();
generators.Add("uint", new UIntGenerator());
return generators;
}
}
//...
var dg = new DG(new ExtendedGeneratorFactory());
DataScheme scheme = dg.GetFromFile("data.json");
string output = dg.GenerateJson(scheme);
Calling the GetRandomInstance
method, and passing nameof(method)
as a parameter, should return the unique Random
instance. Please do use it, rather than create your own Random
object, as in this case, the randomness of the data will be lost at some extent.
public class StudentCalcGenerator : CalcGeneratorBase
{
protected override object CreateImpl(CalcPropertyObject propertyObject)
{
if (propertyObject.Owns(nameof(Student.FullName), typeof(DgStudent)))
{
return $"{propertyObject.ValueOf(nameof(Student.FirstName))} {propertyObject.ValueOf(nameof(Student.LastName))}";
}
if(propertyObject.Owns(nameof(StudentCollection.Count), typeof(DgStudentCollection), typeof(StudentCollection)))
{
return (propertyObject.ValueOf(nameof(StudentCollection.Students)) as List<NameValueObject>)!.Count;
}
throw new NotSupportedException("Not expected calculated property");
}
protected override object CreateRangeFailureImpl(CalcPropertyObject propertyObject)
{
throw new NotSupportedException("Range failure not supported");
}
}
public class StudentGeneratorFactory : GeneratorFactory
{
protected override Dictionary<string, GeneratorBase> GetGeneratorDictionary()
{
Dictionary<string, GeneratorBase> generators = base.GetGeneratorDictionary();
generators.Add(TemplateType.Calc, new StudentCalcGenerator());
return generators;
}
}
//
var dg = new DG(new StudentGeneratorFactory());
string output = dg.GenerateJson<StudentCollection>(new StudentTestsProfiles());