diff --git a/src/GenFu/FillerManager.cs b/src/GenFu/FillerManager.cs index 2c4dc7c..d88b034 100644 --- a/src/GenFu/FillerManager.cs +++ b/src/GenFu/FillerManager.cs @@ -14,11 +14,18 @@ public class FillerManager private IDictionary _genericPropertyFillersByPropertyType; private IList _propertyFillers; - static ReaderWriterLockSlim rwl = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); - + private ReaderWriterLockSlim rwl = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); + public GenFuInstance GenFu { get; } public FillerManager() { + GenFu = A.GenFuInstance; + ResetFillers(); + } + + public FillerManager(GenFuInstance genFu) + { + GenFu = genFu; ResetFillers(); } @@ -31,68 +38,68 @@ public void ResetFillers() { _propertyFillers = new List(); - _propertyFillers.Add(new CharFiller()); - _propertyFillers.Add(new NullableCharFiller()); - _propertyFillers.Add(new IntFiller()); - _propertyFillers.Add(new NullableIntFiller()); - _propertyFillers.Add(new NullableUIntFiller()); - _propertyFillers.Add(new LongFiller()); - _propertyFillers.Add(new NullableLongFiller()); - _propertyFillers.Add(new NullableULongFiller()); - _propertyFillers.Add(new DecimalFiller()); - _propertyFillers.Add(new NullableDecimalFiller()); - _propertyFillers.Add(new ShortFiller()); - _propertyFillers.Add(new NullableShortFiller()); - _propertyFillers.Add(new NullableUShortFiller()); - _propertyFillers.Add(new BooleanFiller()); - _propertyFillers.Add(new NullableBooleanFiller()); - - _propertyFillers.Add(new AgeFiller()); - _propertyFillers.Add(new PriceFiller()); - - _propertyFillers.Add(new CompanyNameFiller()); - - _propertyFillers.Add(new CookingFiller.IngredientFiller()); - - - DateTimeNullableFiller DateTimeNullableFiller = new DateTimeAdapterFiller(); - _propertyFillers.Add(new DateTimeFiller()); - _propertyFillers.Add(new DateTimeOffsetFiller()); + _propertyFillers.Add(new CharFiller(GenFu)); + _propertyFillers.Add(new NullableCharFiller(GenFu)); + _propertyFillers.Add(new IntFiller(GenFu)); + _propertyFillers.Add(new NullableIntFiller(GenFu)); + _propertyFillers.Add(new NullableUIntFiller(GenFu)); + _propertyFillers.Add(new LongFiller(GenFu)); + _propertyFillers.Add(new NullableLongFiller(GenFu)); + _propertyFillers.Add(new NullableULongFiller(GenFu)); + _propertyFillers.Add(new DecimalFiller(GenFu)); + _propertyFillers.Add(new NullableDecimalFiller(GenFu)); + _propertyFillers.Add(new ShortFiller(GenFu)); + _propertyFillers.Add(new NullableShortFiller(GenFu)); + _propertyFillers.Add(new NullableUShortFiller(GenFu)); + _propertyFillers.Add(new BooleanFiller(GenFu)); + _propertyFillers.Add(new NullableBooleanFiller(GenFu)); + + _propertyFillers.Add(new AgeFiller(GenFu)); + _propertyFillers.Add(new PriceFiller(GenFu)); + + _propertyFillers.Add(new CompanyNameFiller(GenFu)); + + _propertyFillers.Add(new CookingFiller.IngredientFiller(GenFu)); + + + DateTimeNullableFiller DateTimeNullableFiller = new DateTimeAdapterFiller(GenFu); + _propertyFillers.Add(new DateTimeFiller(GenFu)); + _propertyFillers.Add(new DateTimeOffsetFiller(GenFu)); _propertyFillers.Add(DateTimeNullableFiller); - _propertyFillers.Add(new BirthDateFiller()); - _propertyFillers.Add(new GuidFiller()); - _propertyFillers.Add(new ArticleTitleFiller()); + _propertyFillers.Add(new BirthDateFiller(GenFu)); + _propertyFillers.Add(new GuidFiller(GenFu)); + _propertyFillers.Add(new ArticleTitleFiller(GenFu)); - _propertyFillers.Add(new FirstNameFiller()); - _propertyFillers.Add(new LastNameFiller()); - _propertyFillers.Add(new EmailFiller()); - _propertyFillers.Add(new PersonTitleFiller()); + _propertyFillers.Add(new FirstNameFiller(GenFu)); + _propertyFillers.Add(new LastNameFiller(GenFu)); + _propertyFillers.Add(new EmailFiller(GenFu)); + _propertyFillers.Add(new PersonTitleFiller(GenFu)); - _propertyFillers.Add(new TwitterFiller()); + _propertyFillers.Add(new TwitterFiller(GenFu)); - _propertyFillers.Add(new AddressFiller()); - _propertyFillers.Add(new AddressLine2Filler()); - _propertyFillers.Add(new CityFiller()); - _propertyFillers.Add(new StateFiller()); - _propertyFillers.Add(new ProvinceFiller()); - _propertyFillers.Add(new ZipCodeFiller()); - _propertyFillers.Add(new PostalCodeFiller()); - _propertyFillers.Add(new PhoneNumberFiller()); + _propertyFillers.Add(new AddressFiller(GenFu)); + _propertyFillers.Add(new AddressLine2Filler(GenFu)); + _propertyFillers.Add(new CityFiller(GenFu)); + _propertyFillers.Add(new StateFiller(GenFu)); + _propertyFillers.Add(new ProvinceFiller(GenFu)); + _propertyFillers.Add(new ZipCodeFiller(GenFu)); + _propertyFillers.Add(new PostalCodeFiller(GenFu)); + _propertyFillers.Add(new PhoneNumberFiller(GenFu)); - _propertyFillers.Add(new MusicAlbumTitleFiller()); - _propertyFillers.Add(new MusicArtistNameFiller()); - _propertyFillers.Add(new MusicGenreDescriptionFiller()); - _propertyFillers.Add(new MusicGenreNameFiller()); + _propertyFillers.Add(new MusicAlbumTitleFiller(GenFu)); + _propertyFillers.Add(new MusicArtistNameFiller(GenFu)); + _propertyFillers.Add(new MusicGenreDescriptionFiller(GenFu)); + _propertyFillers.Add(new MusicGenreNameFiller(GenFu)); - _propertyFillers.Add(new CanadianSocialInsuranceNumberFiller()); - _propertyFillers.Add(new USASocialSecurityNumberFiller()); + _propertyFillers.Add(new CanadianSocialInsuranceNumberFiller(GenFu)); + _propertyFillers.Add(new USASocialSecurityNumberFiller(GenFu)); - _propertyFillers.Add(new DrugFiller()); - _propertyFillers.Add(new MedicalProcedureFiller()); + _propertyFillers.Add(new DrugFiller(GenFu)); + _propertyFillers.Add(new MedicalProcedureFiller(GenFu)); - _propertyFillers.Add(new StringFiller()); + _propertyFillers.Add(new StringFiller(GenFu)); } @@ -199,12 +206,12 @@ public IPropertyFiller GetFiller(PropertyInfo propertyInfo) } else if (propertyInfo.PropertyType.GetTypeInfo().BaseType == typeof(System.Enum)) { - result = new EnumFiller(propertyInfo.PropertyType); + result = new EnumFiller(this.GenFu, propertyInfo.PropertyType); } else { //TODO: Can we build a custom filler here for other value types that we have not explicitly implemented (eg. long, decimal, etc.) - result = new CustomFiller("*", typeof(object), true, () => null); + result = new CustomFiller(GenFu, "*", typeof(object), true, () => null); newRegistrations[propertyInfo.PropertyType] = result; } } @@ -328,12 +335,27 @@ private static IPropertyFiller GetMatchingMethodFiller(MethodInfo methodInfo, ID public Result GetGenericFiller() { var type = typeof(Input); - return (Result)_genericPropertyFillersByPropertyType[type]; + try + { + rwl.EnterReadLock(); + return (Result) _genericPropertyFillersByPropertyType[type]; + } + finally + { + rwl.ExitReadLock(); + } } public IPropertyFiller GetGenericFillerForType(Type t) { - return _genericPropertyFillersByPropertyType[t]; + try + { + rwl.EnterReadLock(); + return _genericPropertyFillersByPropertyType[t]; + } + finally + { + rwl.ExitReadLock(); + } } - } } diff --git a/src/GenFu/Fillers/BooleanFiller.cs b/src/GenFu/Fillers/BooleanFiller.cs index 973fc5f..bba4981 100644 --- a/src/GenFu/Fillers/BooleanFiller.cs +++ b/src/GenFu/Fillers/BooleanFiller.cs @@ -6,13 +6,16 @@ namespace GenFu.Fillers { public class BooleanFiller : PropertyFiller { + public BooleanFiller() : this(A.GenFuInstance) { } - public BooleanFiller() : base(new[] { "object" }, new[] { "*" }, true) + public BooleanFiller(Type objectType, string propertyName) : this(A.GenFuInstance, objectType, propertyName) { } + + public BooleanFiller(GenFuInstance genfu) : base(genfu, new[] { "object" }, new[] { "*" }, true) { } - public BooleanFiller(Type objectType, string propertyName) : base(new[] { objectType.FullName }, new[] { propertyName }) + public BooleanFiller(GenFuInstance genfu, Type objectType, string propertyName) : base(genfu, new[] { objectType.FullName }, new[] { propertyName }) { } @@ -25,13 +28,14 @@ public override object GetValue(object instance) public class NullableBooleanFiller : PropertyFiller { + public NullableBooleanFiller() : this(A.GenFuInstance) { } - public NullableBooleanFiller() : base(new[] { "object" }, new[] { "*" }, true) + public NullableBooleanFiller(GenFuInstance genfu) : base(genfu, new[] { "object" }, new[] { "*" }, true) { } - public NullableBooleanFiller(Type objectType, string propertyName) : base(new[] { objectType.FullName }, new[] { propertyName }) + public NullableBooleanFiller(GenFuInstance genfu,Type objectType, string propertyName) : base(genfu, new[] { objectType.FullName }, new[] { propertyName }) { } diff --git a/src/GenFu/Fillers/CanadianSocialInsuranceNumberFiller.cs b/src/GenFu/Fillers/CanadianSocialInsuranceNumberFiller.cs index 533326d..aec1f1e 100644 --- a/src/GenFu/Fillers/CanadianSocialInsuranceNumberFiller.cs +++ b/src/GenFu/Fillers/CanadianSocialInsuranceNumberFiller.cs @@ -9,7 +9,11 @@ namespace GenFu.Fillers class CanadianSocialInsuranceNumberFiller : PropertyFiller { public CanadianSocialInsuranceNumberFiller() - : base(new[] { "object" }, new[] { "SIN", "SocialInsuranceNumber" }) + : base(A.GenFuInstance, new[] { "object" }, new[] { "SIN", "SocialInsuranceNumber" }) + { } + + public CanadianSocialInsuranceNumberFiller(GenFuInstance genfu) + : base(genfu, new[] { "object" }, new[] { "SIN", "SocialInsuranceNumber" }) { } public override object GetValue(object instance) diff --git a/src/GenFu/Fillers/CharFiller.cs b/src/GenFu/Fillers/CharFiller.cs index 9bdbcb2..ad17b41 100644 --- a/src/GenFu/Fillers/CharFiller.cs +++ b/src/GenFu/Fillers/CharFiller.cs @@ -6,32 +6,36 @@ namespace GenFu.Fillers { public class CharFiller : PropertyFiller { + public CharFiller() : this(A.GenFuInstance) { } - public CharFiller() : base(new[] { "object" }, new[] { "*" }, true) + public CharFiller(Type objectType, string propertyName) : this(A.GenFuInstance, objectType, propertyName) { } + + public CharFiller(GenFuInstance genFu) : base(genFu, new[] { "object" }, new[] { "*" }, true) { } - public CharFiller(Type objectType, string propertyName) : base(new[] { objectType.FullName }, new[] { propertyName }) + public CharFiller(GenFuInstance genFu, Type objectType, string propertyName) : base(genFu, new[] { objectType.FullName }, new[] { propertyName }) { } public override object GetValue(object instance) { - return (char)GenFu.Random.Next(char.MinValue, char.MaxValue); + return (char)this.GenFu.Random.Next(char.MinValue, char.MaxValue); } } public class NullableCharFiller : PropertyFiller { + public NullableCharFiller() : this(A.GenFuInstance) { } - public NullableCharFiller() : base(new[] { "object" }, new[] { "*" }, true) + public NullableCharFiller(GenFuInstance genFu) : base(genFu, new[] { "object" }, new[] { "*" }, true) { } - public NullableCharFiller(Type objectType, string propertyName) : base(new[] { objectType.FullName }, new[] { propertyName }) + public NullableCharFiller(GenFuInstance genFu, Type objectType, string propertyName) : base(genFu, new[] { objectType.FullName }, new[] { propertyName }) { } diff --git a/src/GenFu/Fillers/CompanyNameFiller.cs b/src/GenFu/Fillers/CompanyNameFiller.cs index 038be27..f4f233b 100644 --- a/src/GenFu/Fillers/CompanyNameFiller.cs +++ b/src/GenFu/Fillers/CompanyNameFiller.cs @@ -7,7 +7,9 @@ namespace GenFu.Fillers { class CompanyNameFiller : PropertyFiller, IPropertyFiller { - public CompanyNameFiller(): base (new[] { "company" }, new[] { "name" }, false) + public CompanyNameFiller() : this(A.GenFuInstance) { } + + public CompanyNameFiller(GenFuInstance genFu) : base (genFu, new[] { "company" }, new[] { "name" }, false) {} public override object GetValue(object instance) diff --git a/src/GenFu/Fillers/CookingFiller.cs b/src/GenFu/Fillers/CookingFiller.cs index 21616d4..7c167cd 100644 --- a/src/GenFu/Fillers/CookingFiller.cs +++ b/src/GenFu/Fillers/CookingFiller.cs @@ -9,8 +9,10 @@ class CookingFiller { public class IngredientFiller : PropertyFiller { - public IngredientFiller() - : base(new[] { "object" }, new[] { "ingredient", "ingredients" }) + public IngredientFiller() : this(A.GenFuInstance) { } + + public IngredientFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "ingredient", "ingredients" }) { } @@ -19,6 +21,6 @@ public override object GetValue(object instance) return ValueGenerators.Cooking.Ingredients.Ingredient(); } } - + } } \ No newline at end of file diff --git a/src/GenFu/Fillers/CustomFillers.cs b/src/GenFu/Fillers/CustomFillers.cs index 22c5f10..b28a5d9 100644 --- a/src/GenFu/Fillers/CustomFillers.cs +++ b/src/GenFu/Fillers/CustomFillers.cs @@ -9,13 +9,15 @@ public class CustomFiller : PropertyFiller { private Func _filler; - public CustomFiller(string propertyName, Type objectType, Func filler) - : this(propertyName, objectType, false, filler) + public CustomFiller(string propertyName, Type objectType, Func filler) : this(A.GenFuInstance, propertyName, objectType, filler) { } + + public CustomFiller(GenFuInstance genfu, string propertyName, Type objectType, Func filler) + : this(genfu, propertyName, objectType, false, filler) { } - internal CustomFiller(string propertyName, Type objectType, bool isGeneric, Func filler) - : base(new[] { objectType.FullName }, new[] { propertyName }, isGeneric) + internal CustomFiller(GenFuInstance genfu, string propertyName, Type objectType, bool isGeneric, Func filler) + : base(genfu, new[] { objectType.FullName }, new[] { propertyName }, isGeneric) { if (objectType != typeof(Object)) AddAllBaseTypes(propertyName, objectType); @@ -46,12 +48,17 @@ public class CustomFiller : PropertyFiller private Func _filler; public CustomFiller(string propertyName, Type objectType, Func filler) - : this(propertyName, objectType, false, filler) + : this(A.GenFuInstance, propertyName, objectType, false, filler) + { + } + + public CustomFiller(GenFuInstance genfu, string propertyName, Type objectType, Func filler) + : this(genfu, propertyName, objectType, false, filler) { } - internal CustomFiller(string propertyName, Type objectType, bool isGeneric, Func filler) - : base(new[] { objectType.FullName }, new[] { propertyName }, isGeneric) + internal CustomFiller(GenFuInstance genfu, string propertyName, Type objectType, bool isGeneric, Func filler) + : base(genfu, new[] { objectType.FullName }, new[] { propertyName }, isGeneric) { _filler = filler; } diff --git a/src/GenFu/Fillers/DateTimeAdapterFiller.cs b/src/GenFu/Fillers/DateTimeAdapterFiller.cs index f8f3bc4..6e8c268 100644 --- a/src/GenFu/Fillers/DateTimeAdapterFiller.cs +++ b/src/GenFu/Fillers/DateTimeAdapterFiller.cs @@ -5,7 +5,20 @@ namespace GenFu.Fillers { public class DateTimeAdapterFiller : DateTimeNullableFiller { - DateTimeFiller dateTimeFiller = new DateTimeFiller(); + public DateTimeAdapterFiller(GenFuInstance genFu) : base(genFu) + { + + } + + DateTimeFiller _dateTimeFiller = null; + DateTimeFiller DateTimeFiller + { + get + { + _dateTimeFiller = _dateTimeFiller ?? new DateTimeFiller(this.GenFu); + return _dateTimeFiller; + } + } private Random rand = new Random(); public override object GetValue(object instance) @@ -18,11 +31,11 @@ public override object GetValue(object instance) } else { - return dateTimeFiller.GetValue(instance); + return DateTimeFiller.GetValue(instance); } } - return dateTimeFiller.GetValue(instance); + return DateTimeFiller.GetValue(instance); } } } diff --git a/src/GenFu/Fillers/DateTimeFillers.cs b/src/GenFu/Fillers/DateTimeFillers.cs index 360c1f3..0ce54cf 100644 --- a/src/GenFu/Fillers/DateTimeFillers.cs +++ b/src/GenFu/Fillers/DateTimeFillers.cs @@ -10,8 +10,10 @@ public class DateTimeFiller : PropertyFiller public DateTime Min { get; set; } = new DateTime(1900, 01, 01); public DateTime Max { get; set; } = new DateTime(2020, 12, 31); - public DateTimeFiller() - : base(new[] { "object" }, new[] { "*" }, true) + public DateTimeFiller() : this(A.GenFuInstance) { } + + public DateTimeFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "*" }, true) { } @@ -35,29 +37,33 @@ public class DateTimeOffsetFiller : PropertyFiller { private Random _random = new Random(); - public DateTimeOffsetFiller() - : base(new[] { "object" }, new[] { "*" }, true) + public DateTimeOffsetFiller() : this(A.GenFuInstance) { } + + public DateTimeOffsetFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "*" }, true) { } public override object GetValue(object instance) { - - return new DateTimeOffset(new DateTimeFiller().GetRandomDate()); + + return new DateTimeOffset(new DateTimeFiller(GenFu).GetRandomDate()); } } public class BirthDateFiller : PropertyFiller { - public BirthDateFiller() - : base(new[] { "object" }, new[] { "birthdate", "birth_date" }) + public BirthDateFiller() : this(A.GenFuInstance) { } + + public BirthDateFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "birthdate", "birth_date" }) { } public override object GetValue(object instance) { - return CalendarDate.Date(DateTime.Today.AddYears(-110), DateTime.Today); + return new CalendarDate(GenFu).Date(DateTime.Today.AddYears(-110), DateTime.Today); } } @@ -72,8 +78,8 @@ public static class DateTimeFillerExtensions public static GenFuConfigurator AsPastDate(this GenFuDateTimeConfigurator configurator) where T : new() { - CustomFiller filler = new CustomFiller(configurator.PropertyInfo.Name, typeof(T), - () => CalendarDate.Date(DateRules.PastDate)); + CustomFiller filler = new CustomFiller(configurator.GenFu, configurator.PropertyInfo.Name, typeof(T), + () => new CalendarDate(configurator.GenFu).Date(DateRules.PastDate)); configurator.Maggie.RegisterFiller(filler); return configurator; } @@ -87,8 +93,8 @@ public static GenFuConfigurator AsPastDate(this GenFuDateTimeConfigurator< public static GenFuConfigurator AsFutureDate(this GenFuDateTimeConfigurator configurator) where T : new() { - CustomFiller filler = new CustomFiller(configurator.PropertyInfo.Name, typeof(T), - () => CalendarDate.Date(DateRules.FutureDates)); + CustomFiller filler = new CustomFiller(configurator.GenFu, configurator.PropertyInfo.Name, typeof(T), + () => new CalendarDate(configurator.GenFu).Date(DateRules.FutureDates)); configurator.Maggie.RegisterFiller(filler); return configurator; } diff --git a/src/GenFu/Fillers/DateTimeNullableFillers.cs b/src/GenFu/Fillers/DateTimeNullableFillers.cs index dbe67a3..f0eb5fd 100644 --- a/src/GenFu/Fillers/DateTimeNullableFillers.cs +++ b/src/GenFu/Fillers/DateTimeNullableFillers.cs @@ -10,8 +10,10 @@ public class DateTimeNullableFiller : PropertyFiller public DateTime Max { get; set; } public double SeedPercentage { get; set; } = 1; - public DateTimeNullableFiller() - : base(new[] { "object" }, new[] { "*" }, true) + public DateTimeNullableFiller() : this(A.GenFuInstance) { } + + public DateTimeNullableFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "*" }, true) { } diff --git a/src/GenFu/Fillers/DrugFiller.cs b/src/GenFu/Fillers/DrugFiller.cs index 16b54bc..a65f303 100644 --- a/src/GenFu/Fillers/DrugFiller.cs +++ b/src/GenFu/Fillers/DrugFiller.cs @@ -6,8 +6,10 @@ namespace GenFu.Fillers { public class DrugFiller : PropertyFiller { - public DrugFiller() - : base(new[] { "object" }, new[] { "drug", "drugs", "Rx", "perscription" }) + public DrugFiller() : this(A.GenFuInstance) { } + + public DrugFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "drug", "drugs", "Rx", "perscription" }) { } diff --git a/src/GenFu/Fillers/EnumFiller.cs b/src/GenFu/Fillers/EnumFiller.cs index e6857ed..ac02876 100644 --- a/src/GenFu/Fillers/EnumFiller.cs +++ b/src/GenFu/Fillers/EnumFiller.cs @@ -6,8 +6,14 @@ namespace GenFu public class EnumFiller : PropertyFiller { readonly Type _type; + public EnumFiller(Type type) - : base(new[] { "object" }, new[] { "*" }, true) + : this(A.GenFuInstance, type) + { + } + + public EnumFiller(GenFuInstance genFu, Type type) + : base(genFu, new[] { "object" }, new[] { "*" }, true) { this._type = type; } diff --git a/src/GenFu/Fillers/GuidFiller.cs b/src/GenFu/Fillers/GuidFiller.cs index bf90810..7942c70 100644 --- a/src/GenFu/Fillers/GuidFiller.cs +++ b/src/GenFu/Fillers/GuidFiller.cs @@ -4,8 +4,10 @@ namespace GenFu.Fillers { class GuidFiller:PropertyFiller { - public GuidFiller() - : base(new[] { "object" }, new[] { "*" }, true) + public GuidFiller() : this(A.GenFuInstance) { } + + public GuidFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "*" }, true) { } public override object GetValue(object instance) diff --git a/src/GenFu/Fillers/InjuryFiller.cs b/src/GenFu/Fillers/InjuryFiller.cs index 8830117..cfe2600 100644 --- a/src/GenFu/Fillers/InjuryFiller.cs +++ b/src/GenFu/Fillers/InjuryFiller.cs @@ -6,8 +6,10 @@ namespace GenFu.Fillers { public class InjuryFiller : PropertyFiller { - public InjuryFiller() - : base(new[] { "object" }, new[] { "injury" }) + public InjuryFiller() : this(A.GenFuInstance) { } + + public InjuryFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "injury" }) { } diff --git a/src/GenFu/Fillers/MedicalProcedureFiller.cs b/src/GenFu/Fillers/MedicalProcedureFiller.cs index 5e2ace0..97b2c0b 100644 --- a/src/GenFu/Fillers/MedicalProcedureFiller.cs +++ b/src/GenFu/Fillers/MedicalProcedureFiller.cs @@ -6,8 +6,10 @@ namespace GenFu.Fillers { public class MedicalProcedureFiller : PropertyFiller { - public MedicalProcedureFiller() - : base(new[] { "object" }, new[] { "procedure", "MedicalProcedure" }) + public MedicalProcedureFiller() : this(A.GenFuInstance) { } + + public MedicalProcedureFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "procedure", "MedicalProcedure" }) { } diff --git a/src/GenFu/Fillers/NumericFillers.cs b/src/GenFu/Fillers/NumericFillers.cs index e993e3c..29037be 100644 --- a/src/GenFu/Fillers/NumericFillers.cs +++ b/src/GenFu/Fillers/NumericFillers.cs @@ -1,17 +1,22 @@ using System; +using GenfuStatic = GenFu.GenFu; + namespace GenFu { public class IntFiller : PropertyFiller { + public IntFiller() : this(A.GenFuInstance) { } + + public IntFiller(Type objectType, string propertyName, int min, int max) : this(A.GenFuInstance, objectType, propertyName, min, max) { } - public IntFiller() : base(new []{"object"}, new []{"*"}, true) + public IntFiller(GenFuInstance genFu) : base(genFu, new[] { "object" }, new[] { "*" }, true) { - Min = GenFu.Defaults.MIN_INT; - Max = GenFu.Defaults.MAX_INT; + Min = genFu.Defaults.MIN_INT; + Max = genFu.Defaults.MAX_INT; } - public IntFiller(Type objectType, string propertyName, int min, int max) : base(new []{objectType.FullName}, new []{propertyName}) + public IntFiller(GenFuInstance genFu, Type objectType, string propertyName, int min, int max) : base(genFu, new[] { objectType.FullName }, new[] { propertyName }) { Min = min; Max = max; @@ -22,20 +27,22 @@ public class IntFiller : PropertyFiller public override object GetValue(object instance) { - return GenFu.Random.Next(Min, Max); + return this.GenFu.Random.Next(Min, Max); } } public class NullableIntFiller : PropertyFiller { + public NullableIntFiller() : this(A.GenFuInstance) { } + public NullableIntFiller(Type objectType, string propertyName, int min, int max) : this(A.GenFuInstance, objectType, propertyName, min, max) { } - public NullableIntFiller() : base(new[] { "object" }, new[] { "*" }, true) + public NullableIntFiller(GenFuInstance genFu) : base(genFu, new[] { "object" }, new[] { "*" }, true) { - Min = GenFu.Defaults.MIN_INT; + Min = this.GenFu.Defaults.MIN_INT; Max = GenFu.Defaults.MAX_INT; } - public NullableIntFiller(Type objectType, string propertyName, int min, int max) : base(new[] { objectType.FullName }, new[] { propertyName }) + public NullableIntFiller(GenFuInstance genFu, Type objectType, string propertyName, int min, int max) : base(genFu, new[] { objectType.FullName }, new[] { propertyName }) { Min = min; Max = max; @@ -46,20 +53,23 @@ public NullableIntFiller(Type objectType, string propertyName, int min, int max) public override object GetValue(object instance) { - return new int?(GenFu.Random.Next(Min, Max)); + return new int?(this.GenFu.Random.Next(Min, Max)); } } public class NullableUIntFiller : PropertyFiller { + public NullableUIntFiller(): this(A.GenFuInstance) { } + + public NullableUIntFiller(Type objectType, string propertyName, uint min, uint max) : this(A.GenFuInstance, objectType, propertyName, min, max) { } - public NullableUIntFiller() : base(new[] { "object" }, new[] { "*" }, true) + public NullableUIntFiller(GenFuInstance genFu) : base(genFu, new[] { "object" }, new[] { "*" }, true) { Min = GenFu.Defaults.MIN_UINT; Max = GenFu.Defaults.MAX_UINT; } - public NullableUIntFiller(Type objectType, string propertyName, uint min, uint max) : base(new[] { objectType.FullName }, new[] { propertyName }) + public NullableUIntFiller(GenFuInstance genFu, Type objectType, string propertyName, uint min, uint max) : base(genFu, new[] { objectType.FullName }, new[] { propertyName }) { Min = min; Max = max; @@ -70,21 +80,24 @@ public NullableUIntFiller(Type objectType, string propertyName, uint min, uint m public override object GetValue(object instance) { - return new uint?((uint)GenFu.Random.Next((int)Min, (int)Max)); + return new uint?((uint)this.GenFu.Random.Next((int)Min, (int)Max)); } } public class ShortFiller : PropertyFiller { + public ShortFiller() : this(A.GenFuInstance) { } - public ShortFiller() : base(new[] { "object" }, new[] { "*" }, true) + public ShortFiller(Type objectType, string propertyName, short min, short max) : this(A.GenFuInstance, objectType, propertyName, min, max) { } + + public ShortFiller(GenFuInstance genFu) : base(genFu, new[] { "object" }, new[] { "*" }, true) { Min = GenFu.Defaults.MIN_SHORT; Max = GenFu.Defaults.MAX_SHORT; } - public ShortFiller(Type objectType, string propertyName, short min, short max) - : base(new[] { objectType.FullName }, new[] { propertyName }) + public ShortFiller(GenFuInstance genFu, Type objectType, string propertyName, short min, short max) + : base(genFu, new[] { objectType.FullName }, new[] { propertyName }) { Min = min; Max = max; @@ -96,21 +109,24 @@ public ShortFiller(Type objectType, string propertyName, short min, short max) public override object GetValue(object instance) { - return (short) GenFu.Random.Next(Min, Max); + return (short)this.GenFu.Random.Next(Min, Max); } } public class NullableShortFiller : PropertyFiller { + public NullableShortFiller() : this(A.GenFuInstance) { } + + public NullableShortFiller(Type objectType, string propertyName, short min, short max) : this(A.GenFuInstance, objectType, propertyName, min, max) { } - public NullableShortFiller() : base(new[] { "object" }, new[] { "*" }, true) + public NullableShortFiller(GenFuInstance genfu) : base(genfu, new[] { "object" }, new[] { "*" }, true) { Min = GenFu.Defaults.MIN_SHORT; Max = GenFu.Defaults.MAX_SHORT; } - public NullableShortFiller(Type objectType, string propertyName, short min, short max) - : base(new[] { objectType.FullName }, new[] { propertyName }) + public NullableShortFiller(GenFuInstance genfu, Type objectType, string propertyName, short min, short max) + : base(genfu, new[] { objectType.FullName }, new[] { propertyName }) { Min = min; Max = max; @@ -122,21 +138,24 @@ public NullableShortFiller(Type objectType, string propertyName, short min, shor public override object GetValue(object instance) { - return (short?)GenFu.Random.Next(Min, Max); + return (short?)this.GenFu.Random.Next(Min, Max); } } public class NullableUShortFiller : PropertyFiller { + public NullableUShortFiller() : this(A.GenFuInstance) { } - public NullableUShortFiller() : base(new[] { "object" }, new[] { "*" }, true) + public NullableUShortFiller(Type objectType, string propertyName, ushort min, ushort max) : this(A.GenFuInstance, objectType, propertyName, min, max) { } + + public NullableUShortFiller(GenFuInstance genFu) : base(genFu, new[] { "object" }, new[] { "*" }, true) { Min = GenFu.Defaults.MIN_USHORT; Max = GenFu.Defaults.MAX_USHORT; } - public NullableUShortFiller(Type objectType, string propertyName, ushort min, ushort max) - : base(new[] { objectType.FullName }, new[] { propertyName }) + public NullableUShortFiller(GenFuInstance genFu, Type objectType, string propertyName, ushort min, ushort max) + : base(genFu, new[] { objectType.FullName }, new[] { propertyName }) { Min = min; Max = max; @@ -148,20 +167,23 @@ public NullableUShortFiller(Type objectType, string propertyName, ushort min, us public override object GetValue(object instance) { - return (ushort?)GenFu.Random.Next(Min, Max); + return (ushort?)this.GenFu.Random.Next(Min, Max); } } public class LongFiller : PropertyFiller { + public LongFiller() : this(A.GenFuInstance) { } - public LongFiller() : base(new[] { "object" }, new[] { "*" }, true) + public LongFiller(Type objectType, string propertyName, int min, int max) : this(A.GenFuInstance, objectType, propertyName, min, max) { } + + public LongFiller(GenFuInstance genFu) : base(genFu, new[] { "object" }, new[] { "*" }, true) { Min = GenFu.Defaults.MIN_INT; Max = GenFu.Defaults.MAX_INT; } - public LongFiller(Type objectType, string propertyName, int min, int max) : base(new[] { objectType.FullName }, new[] { propertyName }) + public LongFiller(GenFuInstance genFu, Type objectType, string propertyName, int min, int max) : base(genFu, new[] { objectType.FullName }, new[] { propertyName }) { Min = min; Max = max; @@ -172,20 +194,23 @@ public LongFiller(Type objectType, string propertyName, int min, int max) : base public override object GetValue(object instance) { - return GenFu.Random.Next(Min, Max); + return this.GenFu.Random.Next(Min, Max); } } public class NullableLongFiller : PropertyFiller { + public NullableLongFiller() : this(A.GenFuInstance) { } + + public NullableLongFiller(Type objectType, string propertyName, int min, int max) : this(A.GenFuInstance, objectType, propertyName, min, max) { } - public NullableLongFiller() : base(new[] { "object" }, new[] { "*" }, true) + public NullableLongFiller(GenFuInstance genFu) : base(genFu, new[] { "object" }, new[] { "*" }, true) { Min = GenFu.Defaults.MIN_INT; Max = GenFu.Defaults.MAX_INT; } - public NullableLongFiller(Type objectType, string propertyName, int min, int max) : base(new[] { objectType.FullName }, new[] { propertyName }) + public NullableLongFiller(GenFuInstance genFu, Type objectType, string propertyName, int min, int max) : base(genFu, new[] { objectType.FullName }, new[] { propertyName }) { Min = min; Max = max; @@ -196,20 +221,23 @@ public NullableLongFiller(Type objectType, string propertyName, int min, int max public override object GetValue(object instance) { - return new long?(GenFu.Random.Next(Min, Max)); + return new long?(this.GenFu.Random.Next(Min, Max)); } } public class NullableULongFiller : PropertyFiller { + public NullableULongFiller() : this(A.GenFuInstance) { } - public NullableULongFiller() : base(new[] { "object" }, new[] { "*" }, true) + public NullableULongFiller(Type objectType, string propertyName, int min, int max) : this(A.GenFuInstance, objectType, propertyName, min, max) { } + + public NullableULongFiller(GenFuInstance genFu) : base(genFu, new[] { "object" }, new[] { "*" }, true) { Min = 0; Max = GenFu.Defaults.MAX_INT; } - public NullableULongFiller(Type objectType, string propertyName, int min, int max) : base(new[] { objectType.FullName }, new[] { propertyName }) + public NullableULongFiller(GenFuInstance genFu, Type objectType, string propertyName, int min, int max) : base(genFu, new[] { objectType.FullName }, new[] { propertyName }) { Min = min; Max = max; @@ -220,21 +248,24 @@ public NullableULongFiller(Type objectType, string propertyName, int min, int ma public override object GetValue(object instance) { - return new ulong?((ulong)GenFu.Random.Next(Min, Max)); + return new ulong?((ulong)this.GenFu.Random.Next(Min, Max)); } } public class DecimalFiller : PropertyFiller { - public DecimalFiller() - : base(new[] { "object" }, new[] { "*" }, true) + public DecimalFiller() : this(A.GenFuInstance) { } + public DecimalFiller(Type objectType, string propertyName, decimal min, decimal max) : this(A.GenFuInstance, objectType, propertyName, min, max) { } + + public DecimalFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "*" }, true) { Min = GenFu.Defaults.MIN_DECIMAL; Max = GenFu.Defaults.MAX_DECIMAL; } - public DecimalFiller(Type objectType, string propertyName, decimal min, decimal max) - : base(new[] { objectType.FullName }, new[] { propertyName }) + public DecimalFiller(GenFuInstance genFu, Type objectType, string propertyName, decimal min, decimal max) + : base(genFu, new[] { objectType.FullName }, new[] { propertyName }) { Min = min; Max = max; @@ -246,8 +277,8 @@ public DecimalFiller(Type objectType, string propertyName, decimal min, decimal public override object GetValue(object instance) { - var rnd = GenFu.Random.NextDouble() - 0.5f; - var baseValue = GenFu.Random.Next((int)Max - (int)Min) + rnd; + var rnd = this.GenFu.Random.NextDouble() - 0.5f; + var baseValue = this.GenFu.Random.Next((int)Max - (int)Min) + rnd; decimal result = ((decimal)baseValue + Min) * 1.035m; if (result < Min) result += 0.5m; @@ -259,15 +290,19 @@ public override object GetValue(object instance) public class NullableDecimalFiller : PropertyFiller { - public NullableDecimalFiller() - : base(new[] { "object" }, new[] { "*" }, true) + public NullableDecimalFiller() : this(A.GenFuInstance) { } + + public NullableDecimalFiller(Type objectType, string propertyName, decimal min, decimal max) : this(A.GenFuInstance, objectType, propertyName, min, max) { } + + public NullableDecimalFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "*" }, true) { Min = GenFu.Defaults.MIN_DECIMAL; Max = GenFu.Defaults.MAX_DECIMAL; } - public NullableDecimalFiller(Type objectType, string propertyName, decimal min, decimal max) - : base(new[] { objectType.FullName }, new[] { propertyName }) + public NullableDecimalFiller(GenFuInstance genFu, Type objectType, string propertyName, decimal min, decimal max) + : base(genFu, new[] { objectType.FullName }, new[] { propertyName }) { Min = min; Max = max; @@ -279,8 +314,8 @@ public NullableDecimalFiller(Type objectType, string propertyName, decimal min, public override object GetValue(object instance) { - var rnd = GenFu.Random.NextDouble() - 0.5f; - var baseValue = GenFu.Random.Next((int)Max - (int)Min) + rnd; + var rnd = this.GenFu.Random.NextDouble() - 0.5f; + var baseValue = this.GenFu.Random.Next((int)Max - (int)Min) + rnd; decimal result = ((decimal)baseValue + Min) * 1.035m; if (result < Min) result += 0.5m; @@ -293,15 +328,18 @@ public override object GetValue(object instance) public class DoubleFiller : PropertyFiller { + public DoubleFiller() : this(A.GenFuInstance) { } - public DoubleFiller() : base(new[] { "object" }, new[] { "*" }, true) + public DoubleFiller(Type objectType, string propertyName, double min, double max) : this(A.GenFuInstance, objectType, propertyName, min, max) { } + + public DoubleFiller(GenFuInstance genFu) : base(genFu, new[] { "object" }, new[] { "*" }, true) { Min = GenFu.Defaults.MIN_DOUBLE; Max = GenFu.Defaults.MAX_DOUBLE; } - public DoubleFiller(Type objectType, string propertyName, double min, double max) - : base(new[] { objectType.FullName }, new[] { propertyName }) + public DoubleFiller(GenFuInstance genFu, Type objectType, string propertyName, double min, double max) + : base(genFu, new[] { objectType.FullName }, new[] { propertyName }) { Min = min; Max = max; @@ -313,21 +351,24 @@ public DoubleFiller(Type objectType, string propertyName, double min, double max public override object GetValue(object instance) { - return (GenFu.Random.NextDouble() * (Max-Min)) + Min; + return (this.GenFu.Random.NextDouble() * (Max - Min)) + Min; } } public class NullableDoubleFiller : PropertyFiller { + public NullableDoubleFiller() : this(A.GenFuInstance) { } - public NullableDoubleFiller() : base(new[] { "object" }, new[] { "*" }, true) + public NullableDoubleFiller(Type objectType, string propertyName, double min, double max) : this(A.GenFuInstance, objectType, propertyName, min, max) { } + + public NullableDoubleFiller(GenFuInstance genFu) : base(genFu, new[] { "object" }, new[] { "*" }, true) { Min = GenFu.Defaults.MIN_DOUBLE; Max = GenFu.Defaults.MAX_DOUBLE; } - public NullableDoubleFiller(Type objectType, string propertyName, double min, double max) - : base(new[] { objectType.FullName }, new[] { propertyName }) + public NullableDoubleFiller(GenFuInstance genFu, Type objectType, string propertyName, double min, double max) + : base(genFu, new[] { objectType.FullName }, new[] { propertyName }) { Min = min; Max = max; @@ -339,7 +380,7 @@ public NullableDoubleFiller(Type objectType, string propertyName, double min, do public override object GetValue(object instance) { - return new double?((GenFu.Random.NextDouble() * (Max - Min)) + Min); + return new double?((this.GenFu.Random.NextDouble() * (Max - Min)) + Min); } } @@ -348,32 +389,36 @@ public class AgeFiller : PropertyFiller private const int _minAge = 1; private const int _maxAge = 93; - public AgeFiller() - : base(new[] { "object" }, new[] { "Age" }) + public AgeFiller() : this(A.GenFuInstance) { } + + public AgeFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "Age" }) { } public override object GetValue(object instance) { - return Math.Abs(GenFu.Random.Next(_minAge, _maxAge)); + return Math.Abs(this.GenFu.Random.Next(_minAge, _maxAge)); } } public class PriceFiller : PropertyFiller { private const int _maxPrice = 1000; + + public PriceFiller() : this(A.GenFuInstance) { } - public PriceFiller() - : base(new[] { "object" }, new[] { "price", "amount", "amt" }) + public PriceFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "price", "amount", "amt" }) { } public override object GetValue(object instance) { - decimal result = (decimal)(GenFu.Random.NextDouble() * _maxPrice); + decimal result = (decimal)(this.GenFu.Random.NextDouble() * _maxPrice); - return Math.Round(result,2); + return Math.Round(result, 2); } } diff --git a/src/GenFu/Fillers/PersonFiller.cs b/src/GenFu/Fillers/PersonFiller.cs index ac4fa53..23d8e38 100644 --- a/src/GenFu/Fillers/PersonFiller.cs +++ b/src/GenFu/Fillers/PersonFiller.cs @@ -9,8 +9,10 @@ public class PersonFiller { public class SexFiller : PropertyFiller { - public SexFiller() - : base(new[] { "object" }, new[] { "sex" }) + public SexFiller() : this(A.GenFuInstance) { } + + public SexFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "sex" }) { } @@ -23,8 +25,10 @@ public override object GetValue(object instance) public class GenderFiller : PropertyFiller { - public GenderFiller() - : base(new[] { "object" }, new[] { "gender" }) + public GenderFiller() : this(A.GenFuInstance) { } + + public GenderFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "gender" }) { } diff --git a/src/GenFu/Fillers/PropertyFiller.cs b/src/GenFu/Fillers/PropertyFiller.cs index 9fa08d6..f3693d1 100644 --- a/src/GenFu/Fillers/PropertyFiller.cs +++ b/src/GenFu/Fillers/PropertyFiller.cs @@ -10,16 +10,17 @@ public abstract class PropertyFiller : IPropertyFiller /// /// The names of the object types that this property filler will target /// The names of the properties that this property filler will target - protected PropertyFiller(string[] objectTypeNames, string[] propertyNames) - : this(objectTypeNames, propertyNames, false) + protected PropertyFiller(GenFuInstance genfu, string[] objectTypeNames, string[] propertyNames) + : this(genfu, objectTypeNames, propertyNames, false) { - + GenFu = genfu; } - internal PropertyFiller(string[] objectTypeNames, string[] propertyNames, bool isGenericFiller) + internal PropertyFiller(GenFuInstance genfu, string[] objectTypeNames, string[] propertyNames, bool isGenericFiller) { ObjectTypeNames = objectTypeNames.Select(o => o.ToLowerInvariant()).ToArray(); PropertyNames = propertyNames.Select(p => p.ToLowerInvariant()).ToArray(); + GenFu = genfu; IsGenericFiller = isGenericFiller; } @@ -29,7 +30,9 @@ internal PropertyFiller(string[] objectTypeNames, string[] propertyNames, bool i public bool IsGenericFiller { get; private set; } public Type PropertyType { get { return typeof(T); } } - + + public GenFuInstance GenFu { get; } + /// /// Returns a value that will be used to a particular property /// diff --git a/src/GenFu/Fillers/StringFillerExtensions.cs b/src/GenFu/Fillers/StringFillerExtensions.cs index bd11f16..9d0b792 100644 --- a/src/GenFu/Fillers/StringFillerExtensions.cs +++ b/src/GenFu/Fillers/StringFillerExtensions.cs @@ -18,7 +18,7 @@ public static class StringFillerExtensions /// A configurator for the specified object type public static GenFuConfigurator AsEmailAddress(this GenFuStringConfigurator configurator) where T : new() { - CustomFiller filler = new CustomFiller(configurator.PropertyInfo.Name, typeof(T), () => ContactInformation.Email()); + CustomFiller filler = new CustomFiller(configurator.GenFu,configurator.PropertyInfo.Name, typeof(T), () => ContactInformation.Email()); configurator.Maggie.RegisterFiller(filler); return configurator; } @@ -33,7 +33,7 @@ public static class StringFillerExtensions public static GenFuConfigurator AsEmailAddressForDomain(this GenFuStringConfigurator configurator, string domain) where T : new() { - CustomFiller filler = new CustomFiller(configurator.PropertyInfo.Name, typeof(T), () => ContactInformation.Email(domain)); + CustomFiller filler = new CustomFiller(configurator.GenFu,configurator.PropertyInfo.Name, typeof(T), () => ContactInformation.Email(domain)); configurator.Maggie.RegisterFiller(filler); return configurator; } @@ -46,7 +46,7 @@ public static class StringFillerExtensions /// A configurator for the specified object type public static GenFuConfigurator AsTwitterHandle(this GenFuStringConfigurator configurator) where T : new() { - CustomFiller filler = new CustomFiller(configurator.PropertyInfo.Name, typeof(T), () => ContactInformation.Twitter()); + CustomFiller filler = new CustomFiller(configurator.GenFu,configurator.PropertyInfo.Name, typeof(T), () => ContactInformation.Twitter()); configurator.Maggie.RegisterFiller(filler); return configurator; } @@ -59,7 +59,7 @@ public static class StringFillerExtensions /// A configurator for the specified object type public static GenFuConfigurator AsArticleTitle(this GenFuStringConfigurator configurator) where T : new() { - CustomFiller filler = new CustomFiller(configurator.PropertyInfo.Name, typeof(T), () => Names.Title()); + CustomFiller filler = new CustomFiller(configurator.GenFu,configurator.PropertyInfo.Name, typeof(T), () => Names.Title()); configurator.Maggie.RegisterFiller(filler); return configurator; } @@ -72,7 +72,7 @@ public static class StringFillerExtensions /// A configurator for the specified object type public static GenFuConfigurator AsPhoneNumber(this GenFuStringConfigurator configurator) where T : new() { - CustomFiller filler = new CustomFiller(configurator.PropertyInfo.Name, typeof(T), () => ContactInformation.PhoneNumber()); + CustomFiller filler = new CustomFiller(configurator.GenFu,configurator.PropertyInfo.Name, typeof(T), () => ContactInformation.PhoneNumber()); configurator.Maggie.RegisterFiller(filler); return configurator; } @@ -85,7 +85,7 @@ public static class StringFillerExtensions /// A configurator for the specified object type public static GenFuConfigurator AsFirstName(this GenFuStringConfigurator configurator) where T : new() { - CustomFiller filler = new CustomFiller(configurator.PropertyInfo.Name, typeof(T), () => Names.FirstName()); + CustomFiller filler = new CustomFiller(configurator.GenFu,configurator.PropertyInfo.Name, typeof(T), () => Names.FirstName()); configurator.Maggie.RegisterFiller(filler); return configurator; } @@ -98,7 +98,7 @@ public static class StringFillerExtensions /// A configurator for the specified object type public static GenFuConfigurator AsPersonTitle(this GenFuStringConfigurator configurator) where T : new() { - CustomFiller filler = new CustomFiller(configurator.PropertyInfo.Name, typeof(T), () => Names.PersonTitle()); + CustomFiller filler = new CustomFiller(configurator.GenFu,configurator.PropertyInfo.Name, typeof(T), () => Names.PersonTitle()); configurator.Maggie.RegisterFiller(filler); return configurator; } @@ -111,7 +111,7 @@ public static class StringFillerExtensions /// A configurator for the specified object type public static GenFuConfigurator AsLastName(this GenFuStringConfigurator configurator) where T : new() { - CustomFiller filler = new CustomFiller(configurator.PropertyInfo.Name, typeof(T), () => Names.LastName()); + CustomFiller filler = new CustomFiller(configurator.GenFu,configurator.PropertyInfo.Name, typeof(T), () => Names.LastName()); configurator.Maggie.RegisterFiller(filler); return configurator; } @@ -124,7 +124,7 @@ public static class StringFillerExtensions /// A configurator for the specified object type public static GenFuConfigurator AsAddress(this GenFuStringConfigurator configurator) where T : new() { - CustomFiller filler = new CustomFiller(configurator.PropertyInfo.Name, typeof(T), () => Address.AddressLine()); + CustomFiller filler = new CustomFiller(configurator.GenFu, configurator.PropertyInfo.Name, typeof(T), () => Address.AddressLine()); configurator.Maggie.RegisterFiller(filler); return configurator; } @@ -137,7 +137,7 @@ public static class StringFillerExtensions /// A configurator for the specified object type public static GenFuConfigurator AsAddressLine2(this GenFuStringConfigurator configurator) where T : new() { - CustomFiller filler = new CustomFiller(configurator.PropertyInfo.Name, typeof(T), () => Address.AddressLine2()); + CustomFiller filler = new CustomFiller(configurator.GenFu,configurator.PropertyInfo.Name, typeof(T), () => Address.AddressLine2()); configurator.Maggie.RegisterFiller(filler); return configurator; } @@ -150,7 +150,7 @@ public static class StringFillerExtensions /// A configurator for the specified object type public static GenFuConfigurator AsCity(this GenFuStringConfigurator configurator) where T : new() { - CustomFiller filler = new CustomFiller(configurator.PropertyInfo.Name, typeof(T), () => Address.City()); + CustomFiller filler = new CustomFiller(configurator.GenFu,configurator.PropertyInfo.Name, typeof(T), () => Address.City()); configurator.Maggie.RegisterFiller(filler); return configurator; } @@ -163,7 +163,7 @@ public static class StringFillerExtensions /// A configurator for the specified object type public static GenFuConfigurator AsCanadianProvince(this GenFuStringConfigurator configurator) where T : new() { - CustomFiller filler = new CustomFiller(configurator.PropertyInfo.Name, typeof(T), () => Address.CanadianProvince()); + CustomFiller filler = new CustomFiller(configurator.GenFu,configurator.PropertyInfo.Name, typeof(T), () => Address.CanadianProvince()); configurator.Maggie.RegisterFiller(filler); return configurator; } @@ -176,7 +176,7 @@ public static class StringFillerExtensions /// A configurator for the specified object type public static GenFuConfigurator AsUsaState(this GenFuStringConfigurator configurator) where T : new() { - CustomFiller filler = new CustomFiller(configurator.PropertyInfo.Name, typeof(T), () => Address.UsaState()); + CustomFiller filler = new CustomFiller(configurator.GenFu,configurator.PropertyInfo.Name, typeof(T), () => Address.UsaState()); configurator.Maggie.RegisterFiller(filler); return configurator; } @@ -189,7 +189,7 @@ public static class StringFillerExtensions /// A configurator for the specified object type public static GenFuConfigurator AsMusicArtistName(this GenFuStringConfigurator configurator) where T : new() { - CustomFiller filler = new CustomFiller(configurator.PropertyInfo.Name, typeof(T), () => ValueGenerators.Music.Artist.Name()); + CustomFiller filler = new CustomFiller(configurator.GenFu,configurator.PropertyInfo.Name, typeof(T), () => ValueGenerators.Music.Artist.Name()); configurator.Maggie.RegisterFiller(filler); return configurator; } @@ -202,7 +202,7 @@ public static class StringFillerExtensions /// A configurator for the specified object type public static GenFuConfigurator AsMusicGenreName(this GenFuStringConfigurator configurator) where T : new() { - CustomFiller filler = new CustomFiller(configurator.PropertyInfo.Name, typeof(T), () => ValueGenerators.Music.Genre.Name()); + CustomFiller filler = new CustomFiller(configurator.GenFu,configurator.PropertyInfo.Name, typeof(T), () => ValueGenerators.Music.Genre.Name()); configurator.Maggie.RegisterFiller(filler); return configurator; } @@ -215,7 +215,7 @@ public static class StringFillerExtensions /// A configurator for the specified object type public static GenFuConfigurator AsMusicGenreDescription(this GenFuStringConfigurator configurator) where T : new() { - CustomFiller filler = new CustomFiller(configurator.PropertyInfo.Name, typeof(T), () => ValueGenerators.Music.Genre.Description()); + CustomFiller filler = new CustomFiller(configurator.GenFu,configurator.PropertyInfo.Name, typeof(T), () => ValueGenerators.Music.Genre.Description()); configurator.Maggie.RegisterFiller(filler); return configurator; } @@ -232,7 +232,7 @@ public static class StringFillerExtensions public static GenFuConfigurator AsLoremIpsumWords(this GenFuStringConfigurator configurator, int numberOfWords = 1) where T : new() { - CustomFiller filler = new CustomFiller(configurator.PropertyInfo.Name, typeof(T), () => ValueGenerators.Lorem.Lorem.GenerateWords(numberOfWords)); + CustomFiller filler = new CustomFiller(configurator.GenFu,configurator.PropertyInfo.Name, typeof(T), () => ValueGenerators.Lorem.Lorem.GenerateWords(numberOfWords)); configurator.Maggie.RegisterFiller(filler); return configurator; } @@ -245,7 +245,7 @@ public static class StringFillerExtensions /// A configurator for the specified object type public static GenFuConfigurator AsLoremIpsumSentences(this GenFuStringConfigurator configurator, int numberOfSentences = 1) where T : new() { - CustomFiller filler = new CustomFiller(configurator.PropertyInfo.Name, typeof(T), () => ValueGenerators.Lorem.Lorem.GenerateSentences(numberOfSentences)); + CustomFiller filler = new CustomFiller(configurator.GenFu,configurator.PropertyInfo.Name, typeof(T), () => ValueGenerators.Lorem.Lorem.GenerateSentences(numberOfSentences)); configurator.Maggie.RegisterFiller(filler); return configurator; } @@ -260,8 +260,8 @@ public static GenFuConfigurator AsPlaceholderImage(this GenFuStringConfigu string textColor = null, ImgFormat format = ImgFormat.GIF) where T : new() { - CustomFiller filler = new CustomFiller(configurator.PropertyInfo.Name, typeof(T), - () => PlaceholditUrlBuilder.UrlFor(width,height, text ,backgroundColor,textColor, format)); + CustomFiller filler = new CustomFiller(configurator.GenFu,configurator.PropertyInfo.Name, typeof(T), + () => PlaceholditUrlBuilder.UrlFor(width, height, text, backgroundColor, textColor, format)); configurator.Maggie.RegisterFiller(filler); return configurator; } diff --git a/src/GenFu/Fillers/StringFillers.cs b/src/GenFu/Fillers/StringFillers.cs index 92654db..e1f7e0a 100644 --- a/src/GenFu/Fillers/StringFillers.cs +++ b/src/GenFu/Fillers/StringFillers.cs @@ -7,8 +7,10 @@ namespace GenFu { public class StringFiller : PropertyFiller { - public StringFiller() - : base(new[] { "object" }, new[] { "*" }, true) + public StringFiller(): this(A.GenFuInstance) { } + + public StringFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "*" }, true) { } @@ -19,8 +21,10 @@ public override object GetValue(object instance) } public class ArticleTitleFiller : PropertyFiller { - public ArticleTitleFiller() - : base(new[] { "object" }, new[] { "title" }) + public ArticleTitleFiller() : this(A.GenFuInstance) { } + + public ArticleTitleFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "title" }) { } @@ -32,8 +36,10 @@ public override object GetValue(object instance) public class FirstNameFiller : PropertyFiller { - public FirstNameFiller() - : base(new[] { "object" }, new[] { "firstname", "fname", "first_name" }) + public FirstNameFiller() : this(A.GenFuInstance) { } + + public FirstNameFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "firstname", "fname", "first_name" }) { } @@ -45,8 +51,10 @@ public override object GetValue(object instance) public class PersonTitleFiller : PropertyFiller { - public PersonTitleFiller() - : base(new[] { "person", "employee", "user" }, new[] { "title" }) + public PersonTitleFiller() : this(A.GenFuInstance) { } + + public PersonTitleFiller(GenFuInstance genFu) + : base(genFu, new[] { "person", "employee", "user" }, new[] { "title" }) { } @@ -58,8 +66,10 @@ public override object GetValue(object instance) public class LastNameFiller : PropertyFiller { - public LastNameFiller() - : base(new[] { "object" }, new[] { "lastname", "lname", "last_name" }) + public LastNameFiller() : this(A.GenFuInstance) { } + + public LastNameFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "lastname", "lname", "last_name" }) { } @@ -71,8 +81,10 @@ public override object GetValue(object instance) public class EmailFiller : PropertyFiller { - public EmailFiller() - : base(new[] { "object" }, new[] { "email", "emailaddress", "email_address" }) + public EmailFiller() : this(A.GenFuInstance) { } + + public EmailFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "email", "emailaddress", "email_address" }) { } @@ -84,8 +96,10 @@ public override object GetValue(object instance) public class TwitterFiller : PropertyFiller { - public TwitterFiller() - : base(new[] { "object" }, new[] { "twitter", "twitterhandle", "twitter_handle", "twittername" }) + public TwitterFiller(): this(A.GenFuInstance) { } + + public TwitterFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "twitter", "twitterhandle", "twitter_handle", "twittername" }) { } @@ -97,8 +111,10 @@ public override object GetValue(object instance) public class AddressFiller : PropertyFiller { - public AddressFiller() - : base(new[] { "object" }, new[] { "address", "address1", "address_1", "billingaddress", "billing_address" }) + public AddressFiller() : this(A.GenFuInstance) { } + + public AddressFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "address", "address1", "address_1", "billingaddress", "billing_address" }) { } @@ -110,8 +126,10 @@ public override object GetValue(object instance) public class AddressLine2Filler : PropertyFiller { - public AddressLine2Filler() - : base(new[] { "object" }, new[] { "address2", "address_2" }) + public AddressLine2Filler() : this(A.GenFuInstance) { } + + public AddressLine2Filler(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "address2", "address_2" }) { } @@ -123,8 +141,10 @@ public override object GetValue(object instance) public class CityFiller : PropertyFiller { - public CityFiller() - : base(new[] { "object" }, new[] { "city", "cityname", "city_name" }) + public CityFiller() : this(A.GenFuInstance) { } + + public CityFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "city", "cityname", "city_name" }) { } @@ -136,8 +156,10 @@ public override object GetValue(object instance) public class StateFiller : PropertyFiller { - public StateFiller() - : base(new[] { "object" }, new[] { "state", "statename", "state_name" }) + public StateFiller(): this(A.GenFuInstance) { } + + public StateFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "state", "statename", "state_name" }) { } @@ -149,8 +171,10 @@ public override object GetValue(object instance) public class StateAbreviationFiller : PropertyFiller { - public StateAbreviationFiller() - : base(new[] { "object" }, new[] { "stateAbreviation" }) + public StateAbreviationFiller() : this(A.GenFuInstance) { } + + public StateAbreviationFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "stateAbreviation" }) { } @@ -163,8 +187,10 @@ public override object GetValue(object instance) public class ProvinceFiller : PropertyFiller { - public ProvinceFiller() - : base(new[] { "object" }, new[] { "province", "provincename", "province_name" }) + public ProvinceFiller() : this(A.GenFuInstance) { } + + public ProvinceFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "province", "provincename", "province_name" }) { } @@ -176,8 +202,10 @@ public override object GetValue(object instance) public class ZipCodeFiller : PropertyFiller { - public ZipCodeFiller() - : base(new[] { "object" }, new[] { "zip", "zipcode", "zip_code" }) + public ZipCodeFiller(): this(A.GenFuInstance) { } + + public ZipCodeFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "zip", "zipcode", "zip_code" }) { } @@ -189,8 +217,10 @@ public override object GetValue(object instance) public class PostalCodeFiller : PropertyFiller { - public PostalCodeFiller() - : base(new[] { "object" }, new[] { "postalcode", "postal_code" }) + public PostalCodeFiller() : this(A.GenFuInstance) { } + + public PostalCodeFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "postalcode", "postal_code" }) { } @@ -202,8 +232,10 @@ public override object GetValue(object instance) public class PhoneNumberFiller : PropertyFiller { - public PhoneNumberFiller() - : base(new[] { "object" }, new[] { "fax", "phone", "phonenumber", "phone_number", "homenumber", "worknumber" }) + public PhoneNumberFiller() : this(A.GenFuInstance) { } + + public PhoneNumberFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "fax", "phone", "phonenumber", "phone_number", "homenumber", "worknumber" }) { } @@ -215,8 +247,10 @@ public override object GetValue(object instance) public class MusicAlbumTitleFiller : PropertyFiller { - public MusicAlbumTitleFiller() - : base(new[] { "album", "musicalbum", "music_album" }, new[] { "title", "albumname", "name" }) + public MusicAlbumTitleFiller() : this(A.GenFuInstance) { } + + public MusicAlbumTitleFiller(GenFuInstance genfu) + : base(genfu, new[] { "album", "musicalbum", "music_album" }, new[] { "title", "albumname", "name" }) { } @@ -228,8 +262,10 @@ public override object GetValue(object instance) public class MusicArtistNameFiller : PropertyFiller { - public MusicArtistNameFiller() - : base(new[] { "artist" }, new[] { "name", "artistname", "artist_name" }) + public MusicArtistNameFiller() : this(A.GenFuInstance) { } + + public MusicArtistNameFiller(GenFuInstance genfu) + : base(genfu, new[] { "artist" }, new[] { "name", "artistname", "artist_name" }) { } @@ -241,8 +277,10 @@ public override object GetValue(object instance) public class MusicGenreNameFiller : PropertyFiller { - public MusicGenreNameFiller() - : base(new[] { "genre", "musicgenre", "music_genre" }, new[] { "title", "name", "genre_title", "genre_name" }) + public MusicGenreNameFiller() : this(A.GenFuInstance) { } + + public MusicGenreNameFiller(GenFuInstance genfu) + : base(genfu, new[] { "genre", "musicgenre", "music_genre" }, new[] { "title", "name", "genre_title", "genre_name" }) { } @@ -254,8 +292,10 @@ public override object GetValue(object instance) public class MusicGenreDescriptionFiller : PropertyFiller { - public MusicGenreDescriptionFiller() - : base(new[] { "genre", "musicgenre", "music_genre" }, new[] { "description", "desc", "genre_description", "genre_desc" }) + public MusicGenreDescriptionFiller() : this(A.GenFuInstance) { } + + public MusicGenreDescriptionFiller(GenFuInstance genFu) + : base(genFu, new[] { "genre", "musicgenre", "music_genre" }, new[] { "description", "desc", "genre_description", "genre_desc" }) { } diff --git a/src/GenFu/Fillers/USASocialSecurityNumberFiller.cs b/src/GenFu/Fillers/USASocialSecurityNumberFiller.cs index e4d4c43..916ca0c 100644 --- a/src/GenFu/Fillers/USASocialSecurityNumberFiller.cs +++ b/src/GenFu/Fillers/USASocialSecurityNumberFiller.cs @@ -7,9 +7,10 @@ namespace GenFu.Fillers { public class USASocialSecurityNumberFiller : PropertyFiller { + public USASocialSecurityNumberFiller(): this(A.GenFuInstance) { } - public USASocialSecurityNumberFiller() - : base(new[] { "object" }, new[] { "SSN", "SocialSecurityNumber" }) + public USASocialSecurityNumberFiller(GenFuInstance genFu) + : base(genFu, new[] { "object" }, new[] { "SSN", "SocialSecurityNumber" }) { } public override object GetValue(object instance) diff --git a/src/GenFu/GenFu.cs b/src/GenFu/GenFu.cs index 59a65fc..40f0d7e 100644 --- a/src/GenFu/GenFu.cs +++ b/src/GenFu/GenFu.cs @@ -3,45 +3,38 @@ using System.Reflection; using System.Collections.Generic; using Angela.vNext.Reflection; +using StaticGenfu = GenFu.GenFu; namespace GenFu { - public class A : GenFu { } - - - public class Eh : GenFu { } - - public partial class GenFu + public partial class GenFuInstance { - private static GenFu _genfu = new GenFu(); - private static FillerManager _fillerManager = new FillerManager(); - - private static int _listCount = GenFu.Defaults.LIST_COUNT; + private FillerManager _fillerManager; + private int _listCount = StaticGenfu.Defaults.LIST_COUNT; - static GenFu() + public GenFuInstance() { - _fillerManager = new FillerManager(); + _fillerManager = new FillerManager(this); Random = new Random(); } - public static T New() where T : new() + public T New() where T : new() { - return (T) New(typeof(T)); + return (T)New(typeof(T)); } - public static object New(Type type) + public object New(Type type) { object instance = Activator.CreateInstance(type); return New(instance); } - public static T New(T instance) + public T New(T instance) { - return (T) New((object) instance); - + return (T)New((object)instance); } - public static object New(object instance) + public object New(object instance) { if (instance != null) { @@ -69,12 +62,12 @@ public static object New(object instance) } - public static List ListOf() where T : new() + public List ListOf() where T : new() { return ListOf(typeof(T)).Cast().ToList(); } - public static List ListOf(Type type) + public List ListOf(Type type) { return BuildList(type, _listCount); } @@ -84,46 +77,46 @@ public static List ListOf(Type type) /// /// Number of items to add /// - public static List ListOf(int itemCount) where T : new() + public List ListOf(int itemCount) where T : new() { return ListOf(typeof(T), itemCount).Cast().ToList(); } - public static List ListOf(Type type, int itemCount) + public List ListOf(Type type, int itemCount) { return BuildList(type, itemCount); } - private static List BuildList(Type type, int itemCount) + private List BuildList(Type type, int itemCount) { var result = new List(); for (int i = 0; i < itemCount; i++) { - result.Add(GenFu.New(type)); + result.Add(New(type)); } return result; } - private static void SetPropertyValue(object instance, PropertyInfo property) + private void SetPropertyValue(object instance, PropertyInfo property) { IPropertyFiller filler = _fillerManager.GetFiller(property); property.SetValue(instance, filler.GetValue(instance), null); } - - private static void CallSetterMethod(object instance, MethodInfo method) + + private void CallSetterMethod(object instance, MethodInfo method) { IPropertyFiller filler = _fillerManager.GetMethodFiller(method); if (filler != null) - method.Invoke(instance, new[] {filler.GetValue(instance)}); + method.Invoke(instance, new[] { filler.GetValue(instance) }); } - public static DateTime MinDateTime + public DateTime MinDateTime { - get { return new GenericFillerDefaults(_fillerManager).GetMinDateTime(); } + get { return new GenericFillerDefaults(_fillerManager).GetMinDateTime(); } set { @@ -131,7 +124,7 @@ public static DateTime MinDateTime } } - public static DateTime MaxDateTime + public DateTime MaxDateTime { get { return new GenericFillerDefaults(_fillerManager).GetMaxDateTime(); } set @@ -140,65 +133,11 @@ public static DateTime MaxDateTime } } - public static Random Random { get; private set; } + public Random Random { get; private set; } - public class Defaults - { - public const int MIN_INT = 1; - public const int MAX_INT = 100; - - public const uint MIN_UINT = 1; - public const uint MAX_UINT = 100; - - public static double MIN_DOUBLE = 1; - public static double MAX_DOUBLE = 10000; - - - public static short MIN_SHORT = 1; - public static short MAX_SHORT = Int16.MaxValue; - - public static ushort MIN_USHORT = 100; - public static ushort MAX_USHORT = UInt16.MaxValue; - - - public const decimal MIN_DECIMAL = 0; - public const decimal MAX_DECIMAL = 100; - - public const int LIST_COUNT = 25; - - public static DateTime MIN_DATETIME = DateTime.Now.AddYears(-30); - public static DateTime MAX_DATETIME = DateTime.Now.AddYears(30); - - public static double SEED_PERCENTAGE = 0.2; - - public const string FILE_DOMAIN_NAMES = "DomainNames"; - public const string FILE_FIRST_NAMES = "FirstNames"; - public const string FILE_LAST_NAMES = "LastNames"; - public const string FILE_PERSON_TITLES = "PersonTitles"; - public const string FILE_TITLES = "Titles"; - public const string FILE_WORDS = "Words"; - public const string FILE_STREET_NAMES = "StreetNames"; - public const string FILE_CITY_NAMES = "CityNames"; - public const string FILE_USA_STATE_NAMES = "USAStateNames"; - public const string FILE_USA_STATE_ABREVIATIONS = "USAStateAbreviations"; - public const string FILE_CDN_PROVINCE_NAMES = "CanadianProvinceNames"; - public const string FILE_CDN_PROVINCE_ABREVIATIONS = "CanadianProvinceAbreviations"; - public const string FILE_MUSIC_ARTIST = "MusicArtists"; - public const string FILE_MUSIC_ALBUM = "MusicAlbums"; - public const string FILE_INGREDIENTS = "Ingredients"; - public const string FILE_COMPANY_NAMES = "CompanyNames"; - public const string FILE_INDUSTRIES = "Industries"; - public const string FILE_INJURIES = "Injuries"; - public const string FILE_GENDERS = "Genders"; - public const string FILE_DRUGS = "Drugs"; - public const string FILE_LOREM = "Lorem"; - - public const string STRING_LOADFAIL = "The resource list for {0} failed to load."; - } + public GenFu.DefaultValues Defaults { get => _defaults; } + private GenFu.DefaultValues _defaults = new GenFu.DefaultValues(); } - - - } diff --git a/src/GenFu/GenFuComplexPropertyConfigurator.cs b/src/GenFu/GenFuComplexPropertyConfigurator.cs index 8bf34d9..7e5afc0 100644 --- a/src/GenFu/GenFuComplexPropertyConfigurator.cs +++ b/src/GenFu/GenFuComplexPropertyConfigurator.cs @@ -7,7 +7,7 @@ namespace GenFu { private MemberInfo _propertyInfo; - public GenFuComplexPropertyConfigurator(GenFu genfu, FillerManager fillerManager, MemberInfo propertyInfo) + public GenFuComplexPropertyConfigurator(GenFuInstance genfu, FillerManager fillerManager, MemberInfo propertyInfo) : base(genfu, fillerManager) { _propertyInfo = propertyInfo; @@ -20,7 +20,7 @@ public GenFuComplexPropertyConfigurator(GenFu genfu, FillerManager fillerManager /// A configurator for the target object type public GenFuConfigurator WithRandom(IList values) { - CustomFiller customFiller = new CustomFiller(PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); + CustomFiller customFiller = new CustomFiller(this.GenFu, PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); _fillerManager.RegisterFiller(customFiller); return this; } @@ -32,7 +32,7 @@ public GenFuConfigurator WithRandom(IList values) /// A configurator for the target object type public GenFuConfigurator WithRandom(IEnumerable values) { - CustomFiller customFiller = new CustomFiller(PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); + CustomFiller customFiller = new CustomFiller(this.GenFu, PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); _fillerManager.RegisterFiller(customFiller); return this; } @@ -44,7 +44,7 @@ public GenFuConfigurator WithRandom(IEnumerable values) /// A configurator for the target object type public GenFuConfigurator WithRandom(T2[] values) { - CustomFiller customFiller = new CustomFiller(PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); + CustomFiller customFiller = new CustomFiller(this.GenFu, PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); _fillerManager.RegisterFiller(customFiller); return this; } diff --git a/src/GenFu/GenFuConfigurator.cs b/src/GenFu/GenFuConfigurator.cs index 3a9f3fc..e2a830d 100644 --- a/src/GenFu/GenFuConfigurator.cs +++ b/src/GenFu/GenFuConfigurator.cs @@ -7,10 +7,10 @@ namespace GenFu { public class GenFuConfigurator { - protected GenFu _genfu; + protected GenFuInstance _genfu; protected FillerManager _fillerManager; - public GenFuConfigurator(GenFu genfu, FillerManager filterManager) + public GenFuConfigurator(GenFuInstance genfu, FillerManager filterManager) { _genfu = genfu; _fillerManager = filterManager; @@ -42,7 +42,7 @@ public GenFuConfigurator Fill(string propertyName, Func filler) public GenFuConfigurator Fill(Expression> expression, T2 value) { PropertyInfo propertyInfo = (expression.Body as MemberExpression).Member as PropertyInfo; - CustomFiller customFiller = new CustomFiller(propertyInfo.Name, typeof(T1), () => value); + CustomFiller customFiller = new CustomFiller(_genfu,propertyInfo.Name, typeof(T1), () => value); _fillerManager.RegisterFiller(customFiller); return this; } @@ -58,7 +58,7 @@ public GenFuConfigurator Fill(Expression> expression, T2 va public GenFuConfigurator Fill(Expression> expression, Func filler) { PropertyInfo propertyInfo = (expression.Body as MemberExpression).Member as PropertyInfo; - CustomFiller customFiller = new CustomFiller(propertyInfo.Name, typeof(T1), filler); + CustomFiller customFiller = new CustomFiller(_genfu, propertyInfo.Name, typeof(T1), filler); _fillerManager.RegisterFiller(customFiller); return this; } @@ -75,7 +75,7 @@ public GenFuConfigurator Data(PropertyType propertyType, string filename) return this; } - public GenFu GenFu + public GenFuInstance GenFu { get { return _genfu; } } @@ -88,7 +88,7 @@ public FillerManager Maggie public class GenFuConfigurator : GenFuConfigurator where T : new() { - public GenFuConfigurator(GenFu genfu, FillerManager fillerManager) + public GenFuConfigurator(GenFuInstance genfu, FillerManager fillerManager) : base(genfu, fillerManager) { } @@ -122,7 +122,7 @@ private MethodInfo GetMethodInfoFromExpression(Expression> expression) public GenFuConfigurator Fill(Expression> expression, T2 value) { PropertyInfo propertyInfo = GetPropertyInfoFromExpression(expression); - CustomFiller customFiller = new CustomFiller(propertyInfo.Name, typeof(T), () => value); + CustomFiller customFiller = new CustomFiller(_genfu, propertyInfo.Name, typeof(T), () => value); _fillerManager.RegisterFiller(customFiller); return this; } @@ -138,7 +138,7 @@ public GenFuConfigurator Fill(Expression> expression, T2 valu public GenFuConfigurator Fill(Expression> expression, Func filler) { PropertyInfo propertyInfo = GetPropertyInfoFromExpression(expression); - CustomFiller customFiller = new CustomFiller(propertyInfo.Name, typeof(T), filler); + CustomFiller customFiller = new CustomFiller(_genfu, propertyInfo.Name, typeof(T), filler); _fillerManager.RegisterFiller(customFiller); return this; } @@ -154,7 +154,7 @@ public GenFuConfigurator Fill(Expression> expression, Func Fill(Expression> expression, Func filler) { PropertyInfo propertyInfo = GetPropertyInfoFromExpression(expression); - CustomFiller customFiller = new CustomFiller(propertyInfo.Name, typeof(T), filler); + CustomFiller customFiller = new CustomFiller(_genfu, propertyInfo.Name, typeof(T), filler); _fillerManager.RegisterFiller(customFiller); return this; } @@ -246,7 +246,7 @@ public GenFuComplexPropertyConfigurator Fill(Expression> public GenFuConfigurator MethodFill(Expression> expression, Func filler) { MethodInfo methodInfo = GetMethodInfoFromExpression(expression); - CustomFiller customFiller = new CustomFiller(methodInfo.Name, typeof(T), filler); + CustomFiller customFiller = new CustomFiller(_genfu, methodInfo.Name, typeof(T), filler); _fillerManager.RegisterFiller(customFiller); return this; } @@ -263,7 +263,7 @@ public GenFuComplexPropertyConfigurator MethodFill(Expression customFiller = new CustomFiller(methodInfo.Name, typeof(T), () => (T2)filler.GetValue(null)); + PropertyFiller customFiller = new CustomFiller(_genfu, methodInfo.Name, typeof(T), () => (T2)filler.GetValue(null)); _fillerManager.RegisterFiller(customFiller); return new GenFuComplexPropertyConfigurator(_genfu, _fillerManager, methodInfo); } diff --git a/src/GenFu/GenFuDateTimeConfigurator.cs b/src/GenFu/GenFuDateTimeConfigurator.cs index 85f8ae6..a3d8c94 100644 --- a/src/GenFu/GenFuDateTimeConfigurator.cs +++ b/src/GenFu/GenFuDateTimeConfigurator.cs @@ -8,7 +8,7 @@ namespace GenFu { private MemberInfo _propertyInfo; - public GenFuDateTimeConfigurator(GenFu genfu, FillerManager fillerManager, MemberInfo propertyInfo) + public GenFuDateTimeConfigurator(GenFuInstance genfu, FillerManager fillerManager, MemberInfo propertyInfo) : base(genfu, fillerManager) { _propertyInfo = propertyInfo; diff --git a/src/GenFu/GenFuDecimalConfigurator.cs b/src/GenFu/GenFuDecimalConfigurator.cs index 457c5d0..d286e87 100644 --- a/src/GenFu/GenFuDecimalConfigurator.cs +++ b/src/GenFu/GenFuDecimalConfigurator.cs @@ -6,11 +6,13 @@ namespace GenFu { public class GenFuDecimalConfigurator : GenFuConfigurator where T : new() { + private readonly GenFuInstance genfu; private MemberInfo _propertyInfo; - public GenFuDecimalConfigurator(GenFu genfu, FillerManager fillerManager, MemberInfo propertyInfo) + public GenFuDecimalConfigurator(GenFuInstance genfu, FillerManager fillerManager, MemberInfo propertyInfo) : base(genfu, fillerManager) { + this.genfu = genfu; _propertyInfo = propertyInfo; } @@ -22,7 +24,7 @@ public GenFuDecimalConfigurator(GenFu genfu, FillerManager fillerManager, Member /// A configurator for the target object type public GenFuConfigurator WithinRange(int min, int max) { - DecimalFiller filler = new DecimalFiller(typeof(T), _propertyInfo.Name, min, max); + DecimalFiller filler = new DecimalFiller(genfu, typeof(T), _propertyInfo.Name, min, max); _fillerManager.RegisterFiller(filler); return this; } @@ -34,7 +36,7 @@ public GenFuConfigurator WithinRange(int min, int max) /// A configurator for the target object type public GenFuConfigurator WithRandom(decimal[] values) { - CustomFiller customFiller = new CustomFiller(PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); + CustomFiller customFiller = new CustomFiller(genfu, PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); _fillerManager.RegisterFiller(customFiller); return this; } @@ -46,7 +48,7 @@ public GenFuConfigurator WithRandom(decimal[] values) /// A configurator for the target object type public GenFuConfigurator WithRandom(List values) { - CustomFiller customFiller = new CustomFiller(PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); + CustomFiller customFiller = new CustomFiller(genfu, PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); _fillerManager.RegisterFiller(customFiller); return this; } @@ -58,7 +60,7 @@ public GenFuConfigurator WithRandom(List values) /// A configurator for the target object type public GenFuConfigurator WithRandom(IEnumerable values) { - CustomFiller customFiller = new CustomFiller(PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); + CustomFiller customFiller = new CustomFiller(genfu, PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); _fillerManager.RegisterFiller(customFiller); return this; } diff --git a/src/GenFu/GenFuDefaulturator.cs b/src/GenFu/GenFuDefaulturator.cs index 9bbda3e..0fb913b 100644 --- a/src/GenFu/GenFuDefaulturator.cs +++ b/src/GenFu/GenFuDefaulturator.cs @@ -4,10 +4,10 @@ namespace GenFu { public class GenFuDefaulturator { - protected GenFu _genfu; + protected GenFuInstance _genfu; protected FillerManager _fillerManager; - public GenFuDefaulturator(GenFu genfu, FillerManager maggie) + public GenFuDefaulturator(GenFuInstance genfu, FillerManager maggie) { _genfu = genfu; _fillerManager = maggie; @@ -77,7 +77,7 @@ public GenFuDefaulturator DateRange(DateTime minDateTime, DateTime maxDateTime) return this; } - public GenFu GenFu + public GenFuInstance GenFu { get { return _genfu; } } diff --git a/src/GenFu/GenFuFluent.cs b/src/GenFu/GenFuFluent.cs index 5af46bd..0a42222 100644 --- a/src/GenFu/GenFuFluent.cs +++ b/src/GenFu/GenFuFluent.cs @@ -3,17 +3,17 @@ namespace GenFu { //TODO: Consider merging this partial class. Not much here anymore - public partial class GenFu + public partial class GenFuInstance { /// /// Resets GenFu to default state for next generation /// and allows access to fluent interface. /// /// - public static GenFuConfigurator Configure() + public GenFuConfigurator Configure() { Reset(); - return new GenFuConfigurator(_genfu, _fillerManager); + return new GenFuConfigurator(this, _fillerManager); } /// @@ -22,16 +22,16 @@ public static GenFuConfigurator Configure() /// /// The target object type /// A configurator for the specified object type - public static GenFuConfigurator Configure() where T : new() + public GenFuConfigurator Configure() where T : new() { //see reference test: When_running_in_parallel.registrations_are_configurable //Reset(); remove the reset to make this operation atomic, GenFuConfigurator will replace the current registration, we don't need to remove it explicitly - return new GenFuConfigurator(_genfu, _fillerManager); + return new GenFuConfigurator(this, _fillerManager); } - public static GenFuConfigurator Set() + public GenFuConfigurator Set() { - return new GenFuConfigurator(_genfu, _fillerManager); + return new GenFuConfigurator(this, _fillerManager); } /// @@ -40,21 +40,21 @@ public static GenFuConfigurator Set() /// /// The target object type /// A configurator for the specified object type - public static GenFuConfigurator Set() where T : new() + public GenFuConfigurator Set() where T : new() { - return new GenFuConfigurator(_genfu, _fillerManager); + return new GenFuConfigurator(this, _fillerManager); } /// /// Set global defaults for GenFu /// /// The GenFu Defaulturator - public static GenFuDefaulturator Default() + public GenFuDefaulturator Default() { - return new GenFuDefaulturator(_genfu, _fillerManager); + return new GenFuDefaulturator(this, _fillerManager); } - public static void Reset() + public void Reset() { _fillerManager.ResetFillers(); var defaults = new GenericFillerDefaults(_fillerManager); @@ -62,23 +62,23 @@ public static void Reset() defaults.SetMinInt(GenFu.Defaults.MIN_INT); defaults.SetMaxInt(GenFu.Defaults.MAX_INT); - defaults.SetMinShort(GenFu.Defaults.MIN_SHORT); - defaults.SetMaxShort(GenFu.Defaults.MAX_SHORT); + defaults.SetMinShort(this.Defaults.MIN_SHORT); + defaults.SetMaxShort(this.Defaults.MAX_SHORT); defaults.SetMinDecimal(GenFu.Defaults.MIN_DECIMAL); defaults.SetMaxDecimal(GenFu.Defaults.MAX_DECIMAL); _listCount = GenFu.Defaults.LIST_COUNT; - defaults.SetMinDateTime(GenFu.Defaults.MIN_DATETIME); - defaults.SetMaxDateTime(GenFu.Defaults.MAX_DATETIME); + defaults.SetMinDateTime(this.Defaults.MIN_DATETIME); + defaults.SetMaxDateTime(this.Defaults.MAX_DATETIME); - defaults.SetSeedPercentage(GenFu.Defaults.SEED_PERCENTAGE); + defaults.SetSeedPercentage(this.Defaults.SEED_PERCENTAGE); ResourceLoader.PropertyFillers.Clear(); } - public static void Reset() + public void Reset() { _fillerManager.ResetFillers(); diff --git a/src/GenFu/GenFuIntegerConfigurator.cs b/src/GenFu/GenFuIntegerConfigurator.cs index faa8d15..0fbf93f 100644 --- a/src/GenFu/GenFuIntegerConfigurator.cs +++ b/src/GenFu/GenFuIntegerConfigurator.cs @@ -7,7 +7,7 @@ namespace GenFu { private MemberInfo _propertyInfo; - public GenFuIntegerConfigurator(GenFu genfu, FillerManager fillerManager, MemberInfo propertyInfo) + public GenFuIntegerConfigurator(GenFuInstance genfu, FillerManager fillerManager, MemberInfo propertyInfo) : base(genfu, fillerManager) { _propertyInfo = propertyInfo; @@ -21,11 +21,11 @@ public GenFuIntegerConfigurator(GenFu genfu, FillerManager fillerManager, Member /// A configurator for the target object type public GenFuConfigurator WithinRange(int min, int max) { - IntFiller filler = new IntFiller(typeof(T), _propertyInfo.Name, min, max); + IntFiller filler = new IntFiller(this.GenFu, typeof(T), _propertyInfo.Name, min, max); _fillerManager.RegisterFiller(filler); return this; } - + /// /// Fill the target property with a random value from the specified array /// @@ -33,7 +33,7 @@ public GenFuConfigurator WithinRange(int min, int max) /// A configurator for the target object type public GenFuConfigurator WithRandom(int[] values) { - CustomFiller customFiller = new CustomFiller(PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); + CustomFiller customFiller = new CustomFiller(this.GenFu, PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); _fillerManager.RegisterFiller(customFiller); return this; } @@ -45,7 +45,7 @@ public GenFuConfigurator WithRandom(int[] values) /// A configurator for the target object type public GenFuConfigurator WithRandom(List values) { - CustomFiller customFiller = new CustomFiller(PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); + CustomFiller customFiller = new CustomFiller(this.GenFu, PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); _fillerManager.RegisterFiller(customFiller); return this; } @@ -57,7 +57,7 @@ public GenFuConfigurator WithRandom(List values) /// A configurator for the target object type public GenFuConfigurator WithRandom(IEnumerable values) { - CustomFiller customFiller = new CustomFiller(PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); + CustomFiller customFiller = new CustomFiller(this.GenFu, PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); _fillerManager.RegisterFiller(customFiller); return this; } diff --git a/src/GenFu/GenFuShortConfigurator.cs b/src/GenFu/GenFuShortConfigurator.cs index 6825534..10c134d 100644 --- a/src/GenFu/GenFuShortConfigurator.cs +++ b/src/GenFu/GenFuShortConfigurator.cs @@ -5,11 +5,13 @@ namespace GenFu { public class GenFuShortConfigurator : GenFuConfigurator where T : new() { + private readonly GenFuInstance genfu; private MemberInfo _propertyInfo; - public GenFuShortConfigurator(GenFu genfu, FillerManager fillerManager, MemberInfo propertyInfo) + public GenFuShortConfigurator(GenFuInstance genfu, FillerManager fillerManager, MemberInfo propertyInfo) : base(genfu, fillerManager) { + this.genfu = genfu; _propertyInfo = propertyInfo; } @@ -21,11 +23,11 @@ public GenFuShortConfigurator(GenFu genfu, FillerManager fillerManager, MemberIn /// A configurator for the target object type public GenFuConfigurator WithinRange(short min, short max) { - ShortFiller filler = new ShortFiller(typeof(T), _propertyInfo.Name, min, max); + ShortFiller filler = new ShortFiller(this.genfu, typeof(T), _propertyInfo.Name, min, max); _fillerManager.RegisterFiller(filler); return this; } - + /// /// Fill the target property with a random value from the specified array /// @@ -33,7 +35,7 @@ public GenFuConfigurator WithinRange(short min, short max) /// A configurator for the target object type public GenFuConfigurator WithRandom(short[] values) { - CustomFiller customFiller = new CustomFiller(PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); + CustomFiller customFiller = new CustomFiller(genfu, PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); _fillerManager.RegisterFiller(customFiller); return this; } @@ -45,7 +47,7 @@ public GenFuConfigurator WithRandom(short[] values) /// A configurator for the target object type public GenFuConfigurator WithRandom(List values) { - CustomFiller customFiller = new CustomFiller(PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); + CustomFiller customFiller = new CustomFiller(genfu, PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); _fillerManager.RegisterFiller(customFiller); return this; } @@ -57,7 +59,7 @@ public GenFuConfigurator WithRandom(List values) /// A configurator for the target object type public GenFuConfigurator WithRandom(IEnumerable values) { - CustomFiller customFiller = new CustomFiller(PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); + CustomFiller customFiller = new CustomFiller(genfu, PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); _fillerManager.RegisterFiller(customFiller); return this; } diff --git a/src/GenFu/GenFuStatic.cs b/src/GenFu/GenFuStatic.cs new file mode 100644 index 0000000..e638c47 --- /dev/null +++ b/src/GenFu/GenFuStatic.cs @@ -0,0 +1,162 @@ +using System; +using System.Linq; +using System.Reflection; +using System.Collections.Generic; +using Angela.vNext.Reflection; + +namespace GenFu +{ + public abstract class A : GenFu { } + + + public abstract class Eh : GenFu { } + + public abstract partial class GenFu + { + private static GenFuInstance GenFuLocal; + private static object locker = new object(); + public static GenFuInstance GenFuInstance + { + get + { + if (GenFuLocal == null) + lock (locker) + { + if (GenFuLocal == null) + GenFuLocal = new GenFuInstance(); + } + + return GenFuLocal; + } + } + + public static GenFuInstance GetFiller() + { + return new GenFuInstance(); + } + + public static T New() where T : new() + { + return GenFuInstance.New(); + } + + public static object New(Type type) + { + return GenFuInstance.New(type); + } + + public static T New(T instance) + { + return GenFuInstance.New(instance); + } + + public static object New(object instance) + { + return GenFuInstance.New(instance); + } + + + public static List ListOf() where T : new() + { + return GenFuInstance.ListOf(); + } + + public static List ListOf(Type type) + { + return GenFuInstance.ListOf(type); + } + /// + /// Creates a new list of + /// + /// + /// Number of items to add + /// + public static List ListOf(int itemCount) where T : new() + { + return GenFuInstance.ListOf(itemCount); + } + + public static List ListOf(Type type, int itemCount) + { + return GenFuInstance.ListOf(type, itemCount); + } + + + public static DateTime MinDateTime + { + get { return GenFuInstance.MinDateTime; } + + set + { + GenFuInstance.MinDateTime = value; + } + } + + public static DateTime MaxDateTime + { + get { return GenFuInstance.MaxDateTime; } + set + { + GenFuInstance.MinDateTime = value; + } + } + + public static DefaultValues Defaults { get => _defaults; } + + private static DefaultValues _defaults = new DefaultValues(); + + public class DefaultValues + { + public int MIN_INT = 1; + public int MAX_INT = 100; + + public uint MIN_UINT = 1; + public uint MAX_UINT = 100; + + public double MIN_DOUBLE = 1; + public double MAX_DOUBLE = 10000; + + + public short MIN_SHORT = 1; + public short MAX_SHORT = Int16.MaxValue; + + public ushort MIN_USHORT = 100; + public ushort MAX_USHORT = UInt16.MaxValue; + + + public decimal MIN_DECIMAL = 0; + public decimal MAX_DECIMAL = 100; + + public int LIST_COUNT = 25; + + public DateTime MIN_DATETIME = DateTime.Now.AddYears(-30); + public DateTime MAX_DATETIME = DateTime.Now.AddYears(30); + + public double SEED_PERCENTAGE = 0.2; + + public string FILE_DOMAIN_NAMES = "DomainNames"; + public string FILE_FIRST_NAMES = "FirstNames"; + public string FILE_LAST_NAMES = "LastNames"; + public string FILE_PERSON_TITLES = "PersonTitles"; + public string FILE_TITLES = "Titles"; + public string FILE_WORDS = "Words"; + public string FILE_STREET_NAMES = "StreetNames"; + public string FILE_CITY_NAMES = "CityNames"; + public string FILE_USA_STATE_NAMES = "USAStateNames"; + public string FILE_USA_STATE_ABREVIATIONS = "USAStateAbreviations"; + public string FILE_CDN_PROVINCE_NAMES = "CanadianProvinceNames"; + public string FILE_CDN_PROVINCE_ABREVIATIONS = "CanadianProvinceAbreviations"; + public string FILE_MUSIC_ARTIST = "MusicArtists"; + public string FILE_MUSIC_ALBUM = "MusicAlbums"; + public string FILE_INGREDIENTS = "Ingredients"; + public string FILE_COMPANY_NAMES = "CompanyNames"; + public string FILE_INDUSTRIES = "Industries"; + public string FILE_INJURIES = "Injuries"; + public string FILE_GENDERS = "Genders"; + public string FILE_DRUGS = "Drugs"; + public string FILE_LOREM = "Lorem"; + + public string STRING_LOADFAIL = "The resource list for {0} failed to load."; + } + } +} diff --git a/src/GenFu/GenFuStaticFluent.cs b/src/GenFu/GenFuStaticFluent.cs new file mode 100644 index 0000000..799b6fd --- /dev/null +++ b/src/GenFu/GenFuStaticFluent.cs @@ -0,0 +1,71 @@ +using System; + +namespace GenFu +{ + //TODO: Consider merging this partial class. Not much here anymore + public partial class GenFu + { + /// + /// Resets GenFu to default state for next generation + /// and allows access to fluent interface. + /// + /// + public static GenFuConfigurator Configure() + { + return GenFuInstance.Configure(); + } + + /// + /// Reset and configure how the specified type is populated by GenFu + /// NOTE: Overwrites all previous configuration + /// + /// The target object type + /// A configurator for the specified object type + public static GenFuConfigurator Configure() where T : new() + { + return GenFuInstance.Configure(); + } + + public static GenFuConfigurator Set() + { + return GenFuInstance.Set(); + } + + /// + /// Configure how the specified type is populated by GenFu. + /// NOTE: Maintains previous configuration for the specified type. + /// + /// The target object type + /// A configurator for the specified object type + public static GenFuConfigurator Set() where T : new() + { + return GenFuInstance.Set(); + } + + /// + /// Set global defaults for GenFu + /// + /// The GenFu Defaulturator + public static GenFuDefaulturator Default() + { + //TODO: MARCO RESTAURAR ESTO ??? + return GenFuInstance.Default(); + //return new GenFuDefaulturator(_genfu, _fillerManager); + } + + public static void Reset() + { + GenFuInstance.Reset(); + } + + public static void Reset() + { + GenFuInstance.Reset(); + } + + public void ListCount(int count) + { + GenFuInstance.ListCount(count); + } + } +} \ No newline at end of file diff --git a/src/GenFu/GenFuStringConfigurator.cs b/src/GenFu/GenFuStringConfigurator.cs index 1c6f8bc..fe27de8 100644 --- a/src/GenFu/GenFuStringConfigurator.cs +++ b/src/GenFu/GenFuStringConfigurator.cs @@ -7,7 +7,7 @@ namespace GenFu { private MemberInfo _propertyInfo; - public GenFuStringConfigurator(GenFu genfu, FillerManager fillerManager, MemberInfo propertyInfo) + public GenFuStringConfigurator(GenFuInstance genfu, FillerManager fillerManager, MemberInfo propertyInfo) : base(genfu, fillerManager) { _propertyInfo = propertyInfo; @@ -20,7 +20,7 @@ public GenFuStringConfigurator(GenFu genfu, FillerManager fillerManager, MemberI /// A configurator for the target object type public GenFuConfigurator WithRandom(string[] values) { - CustomFiller customFiller = new CustomFiller(PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); + CustomFiller customFiller = new CustomFiller(this.GenFu, PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); _fillerManager.RegisterFiller(customFiller); return this; } @@ -32,7 +32,7 @@ public GenFuConfigurator WithRandom(string[] values) /// A configurator for the target object type public GenFuConfigurator WithRandom(List values) { - CustomFiller customFiller = new CustomFiller(PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); + CustomFiller customFiller = new CustomFiller(this.GenFu, PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); _fillerManager.RegisterFiller(customFiller); return this; } @@ -44,7 +44,7 @@ public GenFuConfigurator WithRandom(List values) /// A configurator for the target object type public GenFuConfigurator WithRandom(IEnumerable values) { - CustomFiller customFiller = new CustomFiller(PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); + CustomFiller customFiller = new CustomFiller(this.GenFu, PropertyInfo.Name, typeof(T), () => BaseValueGenerator.GetRandomValue(values)); _fillerManager.RegisterFiller(customFiller); return this; } diff --git a/src/GenFu/ValueGenerators/BaseValueGenerator.cs b/src/GenFu/ValueGenerators/BaseValueGenerator.cs index 302f680..f920ec1 100644 --- a/src/GenFu/ValueGenerators/BaseValueGenerator.cs +++ b/src/GenFu/ValueGenerators/BaseValueGenerator.cs @@ -9,7 +9,14 @@ public partial class BaseValueGenerator protected static Random _random = new Random(Environment.TickCount); + public GenFuInstance GenFu { get; } + public BaseValueGenerator() { } + + public BaseValueGenerator(GenFuInstance genFu) + { + GenFu = genFu; + } public static string Word() { @@ -17,7 +24,6 @@ public static string Word() int index = _random.Next(0, ResourceLoader.Data(PropertyType.Words).Count()); return ResourceLoader.Data(PropertyType.Words)[index]; } - public static T GetRandomValue(T[] values) { diff --git a/src/GenFu/ValueGenerators/Music/Genre.cs b/src/GenFu/ValueGenerators/Music/Genre.cs index 5a91430..9fcc8ff 100644 --- a/src/GenFu/ValueGenerators/Music/Genre.cs +++ b/src/GenFu/ValueGenerators/Music/Genre.cs @@ -7,6 +7,10 @@ namespace GenFu.ValueGenerators.Music public class Genre : BaseValueGenerator { + public Genre(GenFuInstance genfu) : base() + { + } + public static string Name() { var prefixes = new string[] { "Acid ", "Jazzy ", "Fresh ", "Classic ", "Free ", "Hard ", "Electronic ", "Modern ", "Brazillian ", "Caribbean ", "African ", "Asian ", "Avant-Garde ", "Kansas City ", "Glam ", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" }; diff --git a/src/GenFu/ValueGenerators/Temporal/CalendarDate.cs b/src/GenFu/ValueGenerators/Temporal/CalendarDate.cs index e3382b2..6aa47e6 100644 --- a/src/GenFu/ValueGenerators/Temporal/CalendarDate.cs +++ b/src/GenFu/ValueGenerators/Temporal/CalendarDate.cs @@ -7,13 +7,20 @@ namespace GenFu.ValueGenerators.Temporal { public class CalendarDate : BaseValueGenerator { + public CalendarDate() : base(A.GenFuInstance) + { + } + + public CalendarDate(GenFuInstance genfu) : base(genfu) + { + } - public static DateTime Date(DateTime earliestDate, DateTime latestDate) + public DateTime Date(DateTime earliestDate, DateTime latestDate) { return DateTimeFill(earliestDate, latestDate); } - public static DateTime Date(DateRules rules) + public DateTime Date(DateRules rules) { // apply rule restrictions if (rules == DateRules.Within1Year) diff --git a/tests/GenFu.Tests/GenFuTests.cs b/tests/GenFu.Tests/GenFuTests.cs index 9dba4ef..ddd764a 100644 --- a/tests/GenFu.Tests/GenFuTests.cs +++ b/tests/GenFu.Tests/GenFuTests.cs @@ -361,7 +361,7 @@ public void DateTimeFilledWithExpectedDateGivenRules() A .Configure() - .Fill(b => b.CommentDate, delegate () { return CalendarDate.Date(DateRules.FutureDates); }); + .Fill(b => b.CommentDate, delegate () { return new CalendarDate().Date(DateRules.FutureDates); }); var comments = A.ListOf(); foreach (var comment in comments) @@ -408,12 +408,12 @@ public void CustomPropertyFillsAreChainableUsingSet() A .Configure() - .Fill(b => b.CreateDate, delegate () { return CalendarDate.Date(DateRules.PastDate); }) + .Fill(b => b.CreateDate, delegate () { return new CalendarDate().Date(DateRules.PastDate); }) .Fill(b => b.Comments, delegate () { A .Set() - .Fill(b => b.CommentDate, delegate () { return CalendarDate.Date(DateRules.PastDate); }); + .Fill(b => b.CommentDate, delegate () { return new CalendarDate().Date(DateRules.PastDate); }); return A.ListOf(); }); var blogpost = A.New(); @@ -462,7 +462,7 @@ public void CustomPropertyFillsAreChainableUsingConfigure() { A .Configure() - .Fill(b => b.CommentDate, delegate () { return CalendarDate.Date(DateRules.PastDate); }); + .Fill(b => b.CommentDate, delegate () { return new CalendarDate().Date(DateRules.PastDate); }); return A.ListOf(); }); var blogposts = A.ListOf(); diff --git a/tests/GenFu.Tests/Properties/AssemblyInfo.cs b/tests/GenFu.Tests/Properties/AssemblyInfo.cs index aaee57e..f27bf04 100644 --- a/tests/GenFu.Tests/Properties/AssemblyInfo.cs +++ b/tests/GenFu.Tests/Properties/AssemblyInfo.cs @@ -1,6 +1,7 @@ using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using Xunit; // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information @@ -21,3 +22,6 @@ // The following GUID is for the ID of the typelib if this project is exposed to COM [assembly: Guid("8ff288de-1a52-4e3c-8bee-67f30a65be51")] + + +[assembly: CollectionBehavior(MaxParallelThreads = 1)] diff --git a/tests/GenFu.Tests/ValueGenerators/BaseValueGeneratorTests.cs b/tests/GenFu.Tests/ValueGenerators/BaseValueGeneratorTests.cs index 702a73c..52bb010 100644 --- a/tests/GenFu.Tests/ValueGenerators/BaseValueGeneratorTests.cs +++ b/tests/GenFu.Tests/ValueGenerators/BaseValueGeneratorTests.cs @@ -54,16 +54,17 @@ public void GetRandomValueFromEnumerable() [Fact] public void MakeDateRuleFutureIsCorrect() { - A.Reset(); - var date = CalendarDate.Date(DateRules.FutureDates); - Assert.True(date > DateTime.Now); + var genfu = new GenFuInstance(); + var date = new CalendarDate(genfu).Date(DateRules.FutureDates); + if (!(date > DateTime.Now)) + Assert.True(date > DateTime.Now); } [Fact] public void MakeDateRulePastIsCorrect() { A.Reset(); - var date = CalendarDate.Date(DateRules.PastDate); + var date = new CalendarDate().Date(DateRules.PastDate); Assert.True(DateTime.Now > date); } @@ -77,7 +78,7 @@ public void MakeDateWithinSpecifiedRange() for (int i = 0; i < 1000; i++) { - var date = CalendarDate.Date(minDate, maxDate); + var date = new CalendarDate().Date(minDate, maxDate); Assert.True(date >= minDate); Assert.True(date <= maxDate); } diff --git a/tests/GenFu.Tests/When_filling_using_plugin.cs b/tests/GenFu.Tests/When_filling_using_plugin.cs index 07d7777..8f683b6 100644 --- a/tests/GenFu.Tests/When_filling_using_plugin.cs +++ b/tests/GenFu.Tests/When_filling_using_plugin.cs @@ -6,8 +6,8 @@ namespace GenFu.Tests public class PluginPropertyFiller : PropertyFiller { public const string Value = "This is the expected value"; - public PluginPropertyFiller() : - base(new []{"TestClass"}, new []{"TestProperty"}) + public PluginPropertyFiller() : + base(A.GenFuInstance, new[] { "TestClass" }, new[] { "TestProperty" }) { } diff --git a/tests/GenFu.Tests/When_running_in_parallel.cs b/tests/GenFu.Tests/When_running_in_parallel.cs index ba1067c..5d3d4ef 100644 --- a/tests/GenFu.Tests/When_running_in_parallel.cs +++ b/tests/GenFu.Tests/When_running_in_parallel.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using GenFu.Tests.TestEntities; using Xunit; @@ -9,97 +10,85 @@ public class When_running_in_parallel [Fact] public void new_instances_are_issued() { + var genfu = new GenFuInstance(); Parallel.For(0, 10000, i => { - A.New(); + genfu.New(); }); } [Fact] public void registrations_can_be_reset() { + var genfu = new GenFuInstance(); Parallel.For(0, 10000, i => { - A.New(); - A.Reset(); + genfu.New(); + genfu.Reset(); }); } [Fact] public void registrations_can_be_reset_separatelly() { + var genfu = new GenFuInstance(); Parallel.For(0, 10000, i => { - A.New(); - A.New(); - A.Reset(); - A.Reset(); + genfu.New(); + genfu.New(); + genfu.Reset(); + genfu.Reset(); }); } [Fact] public void registrations_are_configurable() { - + var genfu = new GenFuInstance(); Parallel.For(0, 10000, i => { - A.Configure().Fill(x => x.Age).WithinRange(20, 30); - var person = A.New(); + genfu.Configure().Fill(x => x.Age).WithinRange(20, 30); + var person = genfu.New(); - //There is a race condition here: - // --> if some other test does a Reset o register a new rule this condition will not be met. Thankfully maxParallelThreads is 1 Assert.True(person.Age >= 20 && person.Age <= 30); }); Parallel.For(0, 10000, i => { - A.Configure().Fill(x => x.Age).WithinRange(40, 50); - var person = A.New(); + genfu.Configure().Fill(x => x.Age).WithinRange(40, 50); + var person = genfu.New(); Assert.True(person.Age >= 40 && person.Age <= 50); }); } - - [Fact] - public void registration_changes_are_non_deterministic() + public void genfu_instances_dont_interfere_each_other() { - /* - * this is not exactly a test - * we demosntrate here that, while the FillerManager is thread safe, - * it is not deterministic for multithreading - * because we have only one shared storage - */ - /* - * THIS TEST CAN BE SAFELY DELETED - */ + var childGenerator = new GenFuInstance(); + var youngGenerator = new GenFuInstance(); + + A.Configure().Fill(x => x.Age, () => 95); //Old style global reference + childGenerator.Configure().Fill(x => x.Age, () => 10); //instance #1 + youngGenerator.Configure().Fill(x => x.Age, () => 20); //instance #2 - var testOneGreaterThan100 = false; - var testOneSmallerThan100 = false; - var testTwoGreaterThan100 = false; - var testTwoSmallerThan100 = false; + var childAge = 0; + var youngAge = 0; + var oldAge = 0; Parallel.For(0, 1000, i => { - //conf #1 - A.Configure().Fill(x => x.Id).WithinRange(200, 300); - - var one = A.New(); - testOneGreaterThan100 = testOneGreaterThan100 || one.Id > 100; //comes from conf#1 in any thread - testOneSmallerThan100 = testOneSmallerThan100 || one.Id < 100; //comes from conf#2 in another thread - - //conf #2 - A.Configure().Fill(x => x.Id).WithinRange(20, 30); - - var two = A.New(); - testTwoGreaterThan100 = testTwoGreaterThan100 || two.Id > 100; //comes from conf#1 in any thread - testTwoSmallerThan100 = testTwoSmallerThan100 || two.Id < 100; //comes from conf#2 in another thread + var aged = A.New(); + var child = childGenerator.New(); + var young = youngGenerator.New(); + childAge = Math.Max(child.Age, childAge); + youngAge = Math.Max(young.Age, youngAge); + oldAge = Math.Max(aged.Age, oldAge); }); - //if it were ran in a single thread both assertions would fail - Assert.True(testOneGreaterThan100 && testOneSmallerThan100); //result is not deterministic - Assert.True(testTwoGreaterThan100 && testTwoSmallerThan100); //result is not deterministic + Assert.True(childAge == 10); + Assert.True(youngAge == 20); + Assert.True(oldAge == 95); } } } \ No newline at end of file