diff --git a/Soenneker.Utils.AutoBogus.sln b/Soenneker.Utils.AutoBogus.sln index 87ba25e..5129e2d 100644 --- a/Soenneker.Utils.AutoBogus.sln +++ b/Soenneker.Utils.AutoBogus.sln @@ -22,7 +22,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{E355F0FD-6 EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{DD733813-BAA4-429B-90A7-8E7ADE196683}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Soenneker.Utils.AutoBogus.Console", "test\Soenneker.Utils.AutoBogus.Console\Soenneker.Utils.AutoBogus.Console.csproj", "{4BC4BD23-23A3-407D-8EA7-261FAD46E653}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Soenneker.Utils.AutoBogus.Tests.Console", "test\Soenneker.Utils.AutoBogus.Console\Soenneker.Utils.AutoBogus.Tests.Console.csproj", "{4BC4BD23-23A3-407D-8EA7-261FAD46E653}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/src/AutoFaker.cs b/src/AutoFaker.cs index 132d933..d0a462f 100644 --- a/src/AutoFaker.cs +++ b/src/AutoFaker.cs @@ -31,7 +31,6 @@ List IAutoFaker.Generate(int count, Action(count); } - /// /// Configures all faker instances and generate requests. /// @@ -54,12 +53,12 @@ public static AutoFaker Create(Action? configure = n { var config = new AutoFakerConfig(DefaultConfigService.Config); - if (configure != null) - { - var builder = new AutoFakerConfigBuilder(config); + if (configure == null) + return new AutoFaker(config); + + var builder = new AutoFakerConfigBuilder(config); - configure.Invoke(builder); - } + configure.Invoke(builder); return new AutoFaker(config); } @@ -93,11 +92,11 @@ private AutoFakerContext CreateContext(Action? confi { var config = new AutoFakerConfig(FakerConfig); - if (configure != null) - { - var builder = new AutoFakerConfigBuilder(config); - configure.Invoke(builder); - } + if (configure == null) + return new AutoFakerContext(config); + + var builder = new AutoFakerConfigBuilder(config); + configure.Invoke(builder); return new AutoFakerContext(config); } diff --git a/src/AutoFakerBinder.cs b/src/AutoFakerBinder.cs index cc1956e..899faa6 100644 --- a/src/AutoFakerBinder.cs +++ b/src/AutoFakerBinder.cs @@ -28,11 +28,12 @@ public class AutoFakerBinder : Binder, IAutoFakerBinder /// The created instance of . public virtual TType? CreateInstance(AutoFakerContext? context) { - if (context == null) + if (context == null) return default; Type type = typeof(TType); - CachedConstructor? constructor = GetConstructor(); + + CachedConstructor? constructor = GetConstructor(context.CachedType); if (constructor == null) return default; @@ -60,7 +61,7 @@ public class AutoFakerBinder : Binder, IAutoFakerBinder /// Due to the boxing nature of value types, the parameter is an object. This means the populated /// values are applied to the provided instance and not a copy. /// - public virtual void PopulateInstance(object instance, AutoFakerContext context, MemberInfo[]? members = null) + public virtual void PopulateInstance(object? instance, AutoFakerContext? context, MemberInfo[]? members = null) { // We can only populate non-null instances if (instance == null || context == null) @@ -86,9 +87,7 @@ public virtual void PopulateInstance(object instance, AutoFakerContext co continue; } - context.ParentType = type; - context.GenerateType = member.Type; - context.GenerateName = member.Name; + context.Setup(type, member.Type, member.Name); context.TypesStack.Push(member.Type); @@ -102,11 +101,11 @@ public virtual void PopulateInstance(object instance, AutoFakerContext co { member.Setter.Invoke(instance, value); } - else if (member.Type.IsDictionary()) + else if (member.CachedType.IsDictionary()) { PopulateDictionary(value, instance, member); } - else if (member.Type.IsCollection()) + else if (member.CachedType.IsCollection()) { PopulateCollection(value, instance, member); } @@ -124,19 +123,19 @@ public virtual void PopulateInstance(object instance, AutoFakerContext co private static bool ShouldSkip(Type type, string path, AutoFakerContext context) { // Skip if the type is found - if (context.FakerConfig.SkipTypes.Contains(type)) + if (context.AutoFakerConfig.SkipTypes.Contains(type)) { return true; } // Skip if the path is found - if (context.FakerConfig.SkipPaths.Contains(path)) + if (context.AutoFakerConfig.SkipPaths.Contains(path)) { return true; } //check if tree depth is reached - int? treeDepth = context.FakerConfig.TreeDepth.Invoke(context); + int? treeDepth = context.AutoFakerConfig.TreeDepth.Invoke(context); if (treeDepth.HasValue && context.TypesStack.Count >= treeDepth) return true; @@ -144,42 +143,14 @@ private static bool ShouldSkip(Type type, string path, AutoFakerContext context) // Finally check if the recursive depth has been reached int count = context.TypesStack.Count(t => t == type); - int recursiveDepth = context.FakerConfig.RecursiveDepth.Invoke(context); + int recursiveDepth = context.AutoFakerConfig.RecursiveDepth.Invoke(context); return count >= recursiveDepth; } - //private ConstructorInfo GetConstructor() - //{ - // Type type = typeof(TType); - // ConstructorInfo[] constructors = type.GetConstructors(); - - // // For dictionaries and enumerables locate a constructor that is used for populating as well - // if (type.IsDictionary()) - // { - // return ResolveTypedConstructor(typeof(IDictionary<,>), constructors); - // } - - // if (type.IsEnumerable()) - // { - // return ResolveTypedConstructor(typeof(IEnumerable<>), constructors); - // } - - // // Attempt to find a default constructor - // // If one is not found, simply use the first in the list - // ConstructorInfo? defaultConstructor = (from c in constructors - // let p = c.GetParameters() - // where p.Count() == 0 - // select c).SingleOrDefault(); - - // return defaultConstructor ?? constructors.FirstOrDefault(); - //} - - private static CachedConstructor? GetConstructor() + private static CachedConstructor? GetConstructor(CachedType type) { - Type type = typeof(TType); - - CachedConstructor[]? constructors = CacheService.Cache.GetCachedType(type).GetCachedConstructors(); + CachedConstructor[]? constructors = type.GetCachedConstructors(); if (type.IsDictionary()) { @@ -202,19 +173,6 @@ private static bool ShouldSkip(Type type, string path, AutoFakerContext context) return constructors.Length > 0 ? constructors[0] : null; } - //private static ConstructorInfo ResolveTypedConstructor(Type type, IEnumerable constructors) - //{ - // // Find the first constructor that matches the passed generic definition - // return (from c in constructors - // let p = c.GetParameters() - // where p.Count() == 1 - // let m = p.Single() - // where m.ParameterType.IsGenericType() - // let d = m.ParameterType.GetGenericTypeDefinition() - // where d == type - // select c).SingleOrDefault(); - //} - private static CachedConstructor? ResolveTypedConstructor(Type type, CachedConstructor[] constructors) { for (int i = 0; i < constructors.Length; i++) @@ -245,9 +203,7 @@ private static bool ShouldSkip(Type type, string path, AutoFakerContext context) private static IAutoFakerGenerator GetParameterGenerator(Type type, ParameterInfo parameter, AutoFakerContext context) { - context.ParentType = type; - context.GenerateType = parameter.ParameterType; - context.GenerateName = parameter.Name; + context.Setup(type, parameter.ParameterType, parameter.Name); return GeneratorFactory.GetGenerator(context); } @@ -258,6 +214,7 @@ private List GetMembersToPopulate(CachedType type, MemberInfo[]? mem if (members != null) { var autoMembersList = new List(members.Length); + for (int i = 0; i < members.Length; i++) { autoMembersList.Add(new AutoMember(members[i])); @@ -297,11 +254,11 @@ private List GetMembersToPopulate(CachedType type, MemberInfo[]? mem if (!found) { // A readonly dictionary or collection member can use the Add() method - if (autoMember.IsReadOnly && autoMember.Type.IsDictionary()) + if (autoMember.IsReadOnly && autoMember.CachedType.IsDictionary()) { autoMembers.Add(autoMember); } - else if (autoMember.IsReadOnly && autoMember.Type.IsCollection()) + else if (autoMember.IsReadOnly && autoMember.CachedType.IsCollection()) { autoMembers.Add(autoMember); } diff --git a/src/Config/AutoFakerConfigBuilder.cs b/src/Config/AutoFakerConfigBuilder.cs index 9594466..c79c5f7 100644 --- a/src/Config/AutoFakerConfigBuilder.cs +++ b/src/Config/AutoFakerConfigBuilder.cs @@ -1,7 +1,6 @@ using System; using System.Linq; using Bogus; -using Soenneker.Utils.AutoBogus.Abstract; using Soenneker.Utils.AutoBogus.Config.Abstract; using Soenneker.Utils.AutoBogus.Config.Base; using Soenneker.Utils.AutoBogus.Context; diff --git a/src/Context/AutoFakerContext.cs b/src/Context/AutoFakerContext.cs index 9ff55dd..8ed0ea9 100644 --- a/src/Context/AutoFakerContext.cs +++ b/src/Context/AutoFakerContext.cs @@ -1,9 +1,11 @@ using System; using System.Collections.Generic; using Bogus; +using Soenneker.Reflection.Cache.Types; using Soenneker.Utils.AutoBogus.Abstract; using Soenneker.Utils.AutoBogus.Config; using Soenneker.Utils.AutoBogus.Generators; +using Soenneker.Utils.AutoBogus.Services; namespace Soenneker.Utils.AutoBogus.Context; @@ -15,17 +17,19 @@ public sealed class AutoFakerContext /// /// The parent type of the type associated with the current generate request. /// - public Type ParentType { get; set; } + public Type ParentType { get; private set; } /// /// The type associated with the current generate request. /// - public Type GenerateType { get; internal set; } + public Type GenerateType { get; private set; } + + public CachedType CachedType { get; private set; } /// /// The name associated with the current generate request. /// - public string GenerateName { get; internal set; } + public string GenerateName { get; private set; } /// /// The underlying instance used to generate random values. @@ -37,23 +41,41 @@ public sealed class AutoFakerContext /// public List RuleSets { get; internal set; } - internal AutoFakerConfig FakerConfig { get; } + internal AutoFakerConfig AutoFakerConfig { get; } + internal Stack TypesStack { get; } internal object Instance { get; set; } - internal IAutoFakerBinder FakerBinder => FakerConfig.FakerBinder; + internal IAutoFakerBinder FakerBinder => AutoFakerConfig.FakerBinder; - internal List Overrides => FakerConfig.Overrides; + internal List Overrides => AutoFakerConfig.Overrides; - internal AutoFakerContext(AutoFakerConfig fakerConfig) + internal AutoFakerContext(AutoFakerConfig autoFakerConfig, Type? type = null) { - FakerConfig = fakerConfig; + AutoFakerConfig = autoFakerConfig; - Faker = fakerConfig.Faker!; + Faker = autoFakerConfig.Faker!; TypesStack = new Stack(); RuleSets = []; + + if (type != null) + Setup(type); + } + + internal void Setup(Type parentType, Type generateType, string name) + { + ParentType = parentType; + GenerateType = generateType; + GenerateName = name; + CachedType = CacheService.Cache.GetCachedType(generateType); + } + + internal void Setup(Type generateType) + { + GenerateType = generateType; + CachedType = CacheService.Cache.GetCachedType(generateType); } } \ No newline at end of file diff --git a/src/Extensions/AutoGenerateContextExtension.cs b/src/Extensions/AutoGenerateContextExtension.cs index 53db5ad..fbabf17 100644 --- a/src/Extensions/AutoGenerateContextExtension.cs +++ b/src/Extensions/AutoGenerateContextExtension.cs @@ -19,19 +19,17 @@ public static class AutoGenerateContextExtension /// The instance type to generate. /// The instance for the current generate request. /// The generated instance. - public static TType Generate(this AutoFakerContext context) + public static TType? Generate(this AutoFakerContext? context) { - if (context != null) - { - // Set the generate type for the current request - context.GenerateType = typeof(TType); + if (context == null) + return default; - // Get the type generator and return a value - IAutoFakerGenerator generator = GeneratorFactory.GetGenerator(context); - return (TType)generator.Generate(context); - } + // Set the generate type for the current request + context.Setup(typeof(TType)); - return default; + // Get the type generator and return a value + IAutoFakerGenerator generator = GeneratorFactory.GetGenerator(context); + return (TType) generator.Generate(context); } /// @@ -41,14 +39,14 @@ public static TType Generate(this AutoFakerContext context) /// The instance for the current generate request. /// The number of instances to generate. /// The generated collection of instances. - public static List GenerateMany(this AutoFakerContext context, int? count = null) + public static List GenerateMany(this AutoFakerContext? context, int? count = null) { var items = new List(); - if (context != null) - { - GenerateMany(context, count, items, false); - } + if (context == null) + return items; + + GenerateMany(context, count, items, false); return items; } @@ -60,30 +58,27 @@ public static List GenerateMany(this AutoFakerContext context, int /// The instance for the current generate request. /// The number of instances to generate. /// The generated collection of unique instances. - public static List GenerateUniqueMany(this AutoFakerContext context, int? count = null) + public static List GenerateUniqueMany(this AutoFakerContext? context, int? count = null) { var items = new List(); - if (context != null) - { - GenerateMany(context, count, items, true); - } + if (context == null) + return items; + + GenerateMany(context, count, items, true); return items; } - + /// /// Populates the provided instance with generated values. /// /// The type of instance to populate. /// The instance for the current generate request. /// The instance to populate. - public static void Populate(this AutoFakerContext context, TType instance) + public static void Populate(this AutoFakerContext? context, TType instance) { - if (context != null) - { - context.FakerBinder.PopulateInstance(instance, context); - } + context?.FakerBinder.PopulateInstance(instance, context); } internal static void GenerateMany(AutoFakerContext context, int? count, List items, bool unique, int attempt = 1, Func? generate = null) @@ -91,10 +86,7 @@ internal static void GenerateMany(AutoFakerContext context, int? count, L while (true) { // Apply any defaults - if (count == null) - { - count = context.FakerConfig.RepeatCount.Invoke(context); - } + count ??= context.AutoFakerConfig.RepeatCount.Invoke(context); generate ??= context.Generate; diff --git a/src/Extensions/TypeExtension.cs b/src/Extensions/TypeExtension.cs index 29315c0..9654220 100644 --- a/src/Extensions/TypeExtension.cs +++ b/src/Extensions/TypeExtension.cs @@ -1,72 +1,43 @@ using System; using System.Dynamic; +using Soenneker.Reflection.Cache.Types; using Soenneker.Utils.AutoBogus.Services; namespace Soenneker.Utils.AutoBogus.Extensions; public static class TypeExtension { - internal static bool IsNullable(this Type type) + internal static bool IsExpandoObject(this CachedType type) { - return CacheService.Cache.GetCachedType(type).IsNullable; - //return Nullable.GetUnderlyingType(type) != null; + return type.Type == typeof(ExpandoObject); } - internal static bool IsEnum(this Type type) - { - return CacheService.Cache.GetCachedType(type).IsEnum; - //return type.IsEnum; - } - - internal static bool IsAbstract(this Type type) - { - return CacheService.Cache.GetCachedType(type).IsAbstract; - //return type.IsAbstract; - } - - internal static bool IsInterface(this Type type) - { - return CacheService.Cache.GetCachedType(type).IsInterface; - //return type.IsInterface; - } - - internal static bool IsGenericType(this Type type) - { - return CacheService.Cache.GetCachedType(type).IsGenericType; - // return type.IsGenericType; - } - - internal static bool IsExpandoObject(this Type type) - { - return type == typeof(ExpandoObject); - } - - internal static bool IsDictionary(this Type type) + internal static bool IsDictionary(this CachedType type) { return IsGenericType(type, "IDictionary`2"); } - internal static bool IsReadOnlyDictionary(this Type type) + internal static bool IsReadOnlyDictionary(this CachedType type) { return IsGenericType(type, "IReadOnlyDictionary`2"); } - internal static bool IsCollection(this Type type) + internal static bool IsCollection(this CachedType type) { return IsGenericType(type, "ICollection`1"); } - internal static bool IsEnumerable(this Type type) + internal static bool IsEnumerable(this CachedType type) { return IsGenericType(type, "IEnumerable`1"); } - private static bool IsGenericType(Type type, string interfaceTypeName) + private static bool IsGenericType(CachedType cachedType, string interfaceTypeName) { - if (type.Name == interfaceTypeName) + if (cachedType.Type.Name == interfaceTypeName) return true; - Type[] interfaces = CacheService.Cache.GetCachedType(type).GetInterfaces()!; + Type[] interfaces = cachedType.GetInterfaces()!; foreach (Type i in interfaces) { diff --git a/src/Generators/GeneratorFactory.cs b/src/Generators/GeneratorFactory.cs index 01fde3f..e37fb7a 100644 --- a/src/Generators/GeneratorFactory.cs +++ b/src/Generators/GeneratorFactory.cs @@ -7,7 +7,6 @@ using Soenneker.Utils.AutoBogus.Extensions; using Soenneker.Utils.AutoBogus.Generators.Abstract; using Soenneker.Utils.AutoBogus.Generators.Types; -using Soenneker.Utils.AutoBogus.Services; using Soenneker.Utils.AutoBogus.Utils; namespace Soenneker.Utils.AutoBogus.Generators; @@ -64,8 +63,6 @@ internal static IAutoFakerGenerator ResolveGenerator(AutoFakerContext context) { Type? type = context.GenerateType; - CachedType cachedType = CacheService.Cache.GetCachedType(type); - // Need check if the type is an in/out parameter and adjusted accordingly if (type.IsByRef) { @@ -74,7 +71,7 @@ internal static IAutoFakerGenerator ResolveGenerator(AutoFakerContext context) // Check if an expando object needs to generator // This actually means an existing dictionary needs to be populated - if (type.IsExpandoObject()) + if (context.CachedType.IsExpandoObject()) { return new ExpandoObjectGenerator(); } @@ -86,28 +83,28 @@ internal static IAutoFakerGenerator ResolveGenerator(AutoFakerContext context) return CreateGenericGenerator(typeof(ArrayGenerator<>), type); } - if (DataTableGenerator.TryCreateGenerator(cachedType, out DataTableGenerator dataTableGenerator)) + if (DataTableGenerator.TryCreateGenerator(context.CachedType, out DataTableGenerator dataTableGenerator)) { return dataTableGenerator; } - if (DataSetGenerator.TryCreateGenerator(cachedType, out DataSetGenerator dataSetGenerator)) + if (DataSetGenerator.TryCreateGenerator(context.CachedType, out DataSetGenerator dataSetGenerator)) { return dataSetGenerator; } - if (cachedType.IsEnum) + if (context.CachedType.IsEnum) { return CreateGenericGenerator(typeof(EnumGenerator<>), type); } - if (cachedType.IsNullable) + if (context.CachedType.IsNullable) { - type = cachedType.GetGenericArguments()![0]; + type = context.CachedType.GetGenericArguments()![0]; return CreateGenericGenerator(typeof(NullableGenerator<>), type); } - (CachedType? collectionType, GenericCollectionType? genericCollectionType) = GenericTypeUtil.GetGenericCollectionType(cachedType); + (CachedType? collectionType, GenericCollectionType? genericCollectionType) = GenericTypeUtil.GetGenericCollectionType(context.CachedType); if (collectionType != null) { diff --git a/src/Generators/GeneratorTypeOverride.cs b/src/Generators/GeneratorTypeOverride.cs index 44dd3b3..0956c43 100644 --- a/src/Generators/GeneratorTypeOverride.cs +++ b/src/Generators/GeneratorTypeOverride.cs @@ -3,8 +3,7 @@ namespace Soenneker.Utils.AutoBogus.Generators; -internal sealed class GeneratorTypeOverride - : GeneratorOverride +internal sealed class GeneratorTypeOverride : GeneratorOverride { internal GeneratorTypeOverride(Func generator) { @@ -13,6 +12,7 @@ internal GeneratorTypeOverride(Func generator) } private Type Type { get; } + private Func Generator { get; } public override bool CanOverride(AutoFakerContext context) diff --git a/src/Generators/Types/DataTableGenerator.cs b/src/Generators/Types/DataTableGenerator.cs index 0d9bb8d..332afb1 100644 --- a/src/Generators/Types/DataTableGenerator.cs +++ b/src/Generators/Types/DataTableGenerator.cs @@ -73,10 +73,10 @@ public void PopulateRows(DataTable table, AutoFakerContext context) int rowCount = -1; - if (context.FakerConfig.DataTableRowCount != null) + if (context.AutoFakerConfig.DataTableRowCount != null) { rowCountIsSpecified = true; - rowCount = context.FakerConfig.DataTableRowCount(context); + rowCount = context.AutoFakerConfig.DataTableRowCount(context); } if (rowCount < 0) diff --git a/src/Generators/Types/ExpandoObjectGenerator.cs b/src/Generators/Types/ExpandoObjectGenerator.cs index 389fe26..4680b1b 100644 --- a/src/Generators/Types/ExpandoObjectGenerator.cs +++ b/src/Generators/Types/ExpandoObjectGenerator.cs @@ -4,6 +4,7 @@ using Soenneker.Utils.AutoBogus.Context; using Soenneker.Utils.AutoBogus.Extensions; using Soenneker.Utils.AutoBogus.Generators.Abstract; +using Soenneker.Utils.AutoBogus.Services; namespace Soenneker.Utils.AutoBogus.Generators.Types; @@ -23,11 +24,11 @@ object IAutoFakerGenerator.Generate(AutoFakerContext context) // Configure the context Type type = property.Value.GetType(); - context.ParentType = context.GenerateType; - context.GenerateType = type; - context.GenerateName = property.Key; + context.Setup(context.GenerateType, type, property.Key); - if (type.IsExpandoObject()) + var cachedType = CacheService.Cache.GetCachedType(type); + + if (cachedType.IsExpandoObject()) { context.Instance = property.Value; } diff --git a/src/Generators/Types/ListGenerator.cs b/src/Generators/Types/ListGenerator.cs index 1a7ce47..00496e6 100644 --- a/src/Generators/Types/ListGenerator.cs +++ b/src/Generators/Types/ListGenerator.cs @@ -16,7 +16,7 @@ object IAutoFakerGenerator.Generate(AutoFakerContext context) { list = (List)Activator.CreateInstance(context.GenerateType); } - catch + catch (Exception ex) { list = []; } diff --git a/src/Generators/Types/ReadOnlyDictionaryGenerator.cs b/src/Generators/Types/ReadOnlyDictionaryGenerator.cs index 27a2ddf..6477c19 100644 --- a/src/Generators/Types/ReadOnlyDictionaryGenerator.cs +++ b/src/Generators/Types/ReadOnlyDictionaryGenerator.cs @@ -14,7 +14,7 @@ object IAutoFakerGenerator.Generate(AutoFakerContext context) Type generateType = context.GenerateType; - if (generateType.IsInterface()) + if (context.CachedType.IsInterface) generateType = typeof(Dictionary); // Generate a standard dictionary and create the read only dictionary diff --git a/src/Generators/Types/TypeGenerator.cs b/src/Generators/Types/TypeGenerator.cs index 7ab2a96..f7f7bdd 100644 --- a/src/Generators/Types/TypeGenerator.cs +++ b/src/Generators/Types/TypeGenerator.cs @@ -3,8 +3,7 @@ namespace Soenneker.Utils.AutoBogus.Generators.Types; -internal sealed class TypeGenerator - : IAutoFakerGenerator +internal sealed class TypeGenerator : IAutoFakerGenerator { object IAutoFakerGenerator.Generate(AutoFakerContext context) { diff --git a/src/Soenneker.Utils.AutoBogus.csproj b/src/Soenneker.Utils.AutoBogus.csproj index d92fd37..a24adc3 100644 --- a/src/Soenneker.Utils.AutoBogus.csproj +++ b/src/Soenneker.Utils.AutoBogus.csproj @@ -47,7 +47,7 @@ - + diff --git a/src/Utils/GenericTypeUtil.cs b/src/Utils/GenericTypeUtil.cs index 3578d15..cfebaac 100644 --- a/src/Utils/GenericTypeUtil.cs +++ b/src/Utils/GenericTypeUtil.cs @@ -10,7 +10,6 @@ internal static class GenericTypeUtil internal static (CachedType?, GenericCollectionType?) GetGenericCollectionType(CachedType cachedType) { CachedType[] interfaces = cachedType.GetCachedInterfaces()!; - // Type[] interfaces = type.GetInterfaces(); var interfacesList = new List(interfaces.Length + 1); diff --git a/test/Soenneker.Utils.AutoBogus.Console/Program.cs b/test/Soenneker.Utils.AutoBogus.Console/Program.cs index e988700..fc02373 100644 --- a/test/Soenneker.Utils.AutoBogus.Console/Program.cs +++ b/test/Soenneker.Utils.AutoBogus.Console/Program.cs @@ -1,6 +1,6 @@ using Soenneker.Utils.AutoBogus.Abstract; -namespace Soenneker.Utils.AutoBogus.Console; +namespace Soenneker.Utils.AutoBogus.Tests.Console; public class Program { diff --git a/test/Soenneker.Utils.AutoBogus.Console/Soenneker.Utils.AutoBogus.Console.csproj b/test/Soenneker.Utils.AutoBogus.Console/Soenneker.Utils.AutoBogus.Tests.Console.csproj similarity index 100% rename from test/Soenneker.Utils.AutoBogus.Console/Soenneker.Utils.AutoBogus.Console.csproj rename to test/Soenneker.Utils.AutoBogus.Console/Soenneker.Utils.AutoBogus.Tests.Console.csproj diff --git a/test/Soenneker.Utils.AutoBogus.Tests/AutoFakerFixture.cs b/test/Soenneker.Utils.AutoBogus.Tests/AutoFakerFixture.cs index d932f58..5597827 100644 --- a/test/Soenneker.Utils.AutoBogus.Tests/AutoFakerFixture.cs +++ b/test/Soenneker.Utils.AutoBogus.Tests/AutoFakerFixture.cs @@ -24,7 +24,7 @@ private class TestFakerBinder { } private const string _name = "Generate"; - private static Type _type = typeof(AutoFaker); + private static readonly Type _type = typeof(AutoFaker); public class Configure : AutoFakerFixture diff --git a/test/Soenneker.Utils.AutoBogus.Tests/AutoFakerTests.cs b/test/Soenneker.Utils.AutoBogus.Tests/AutoFakerTests.cs index 0ec120b..fa0e33f 100644 --- a/test/Soenneker.Utils.AutoBogus.Tests/AutoFakerTests.cs +++ b/test/Soenneker.Utils.AutoBogus.Tests/AutoFakerTests.cs @@ -23,7 +23,9 @@ public void Generate_Product_should_generate() var product = faker.Generate(); product.Should().NotBeNull(); - product.Reviews.Count.Should().BeGreaterThan(0); + product.GetRevisions.Should().NotBeNullOrEmpty(); + // product.Reviews.Count.Should().BeGreaterThan(0); + // product. } [Fact] diff --git a/test/Soenneker.Utils.AutoBogus.Tests/AutoGeneratorsFixture.cs b/test/Soenneker.Utils.AutoBogus.Tests/AutoGeneratorsFixture.cs index 6f51e33..15e790f 100644 --- a/test/Soenneker.Utils.AutoBogus.Tests/AutoGeneratorsFixture.cs +++ b/test/Soenneker.Utils.AutoBogus.Tests/AutoGeneratorsFixture.cs @@ -4,7 +4,6 @@ using System.Dynamic; using System.Linq; using System.Reflection; -using Bogus; using FluentAssertions; using Soenneker.Utils.AutoBogus.Config; using Soenneker.Utils.AutoBogus.Context; @@ -14,6 +13,7 @@ using Soenneker.Utils.AutoBogus.Extensions; using Soenneker.Utils.AutoBogus.Generators.Abstract; using Soenneker.Utils.AutoBogus.Generators.Types; +using Soenneker.Utils.AutoBogus.Tests.Extensions; namespace Soenneker.Utils.AutoBogus.Tests; @@ -82,9 +82,7 @@ public void Should_Handle_Subclasses(Type readOnlyDictionaryType) // Arrange var config = new AutoFakerConfig(); - var context = new AutoFakerContext(config); - - context.GenerateType = readOnlyDictionaryType; + var context = new AutoFakerContext(config, readOnlyDictionaryType); // Act IAutoFakerGenerator generator = GeneratorFactory.ResolveGenerator(context); @@ -129,9 +127,7 @@ public void Should_Handle_Subclasses(Type dictionaryType) // Arrange var config = new AutoFakerConfig(); - var context = new AutoFakerContext(config); - - context.GenerateType = dictionaryType; + var context = new AutoFakerContext(config, dictionaryType); // Act IAutoFakerGenerator generator = GeneratorFactory.ResolveGenerator(context); @@ -174,7 +170,7 @@ public void Should_Handle_Subclasses(Type setType) var context = new AutoFakerContext(config); - context.GenerateType = setType; + context.Setup(setType); // Act IAutoFakerGenerator generator = GeneratorFactory.ResolveGenerator(context); @@ -217,7 +213,7 @@ public void Should_Handle_Subclasses(Type listType) var context = new AutoFakerContext(config); - context.GenerateType = listType; + context.Setup(listType); // Act IAutoFakerGenerator generator = GeneratorFactory.ResolveGenerator(context); @@ -701,7 +697,7 @@ public void Should_Invoke_Generator() } } - private object InvokeGenerator(Type type, IAutoFakerGenerator generator, object instance = null) + private static object InvokeGenerator(Type type, IAutoFakerGenerator generator, object? instance = null) { AutoFakerContext context = CreateContext(type); context.Instance = instance; @@ -720,9 +716,8 @@ private static IAutoFakerGenerator CreateGenerator(Type type, params Type[] type return (IAutoFakerGenerator)Activator.CreateInstance(type); } - private AutoFakerContext CreateContext(Type type, List generatorOverrides = null, Func dataTableRowCountFunctor = null) + private static AutoFakerContext CreateContext(Type type, List? generatorOverrides = null, Func? dataTableRowCountFunctor = null) { - var faker = new Faker(); var config = new AutoFakerConfig(); if (generatorOverrides != null) @@ -735,9 +730,6 @@ private AutoFakerContext CreateContext(Type type, List genera config.DataTableRowCount = dataTableRowCountFunctor; } - return new AutoFakerContext(config) - { - GenerateType = type - }; + return new AutoFakerContext(config, type); } } \ No newline at end of file diff --git a/test/Soenneker.Utils.AutoBogus.Tests/Dtos/Complex/Product.cs b/test/Soenneker.Utils.AutoBogus.Tests/Dtos/Complex/Product.cs index 214b701..cee235d 100644 --- a/test/Soenneker.Utils.AutoBogus.Tests/Dtos/Complex/Product.cs +++ b/test/Soenneker.Utils.AutoBogus.Tests/Dtos/Complex/Product.cs @@ -1,15 +1,16 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; namespace Soenneker.Utils.AutoBogus.Tests.Dtos.Complex; public class Product { - //private readonly IList _revisions = new List(); + private readonly IList _revisions = new List(); public Product(int id) { //Id = id; - Reviews = new Dictionary(); + // Reviews = new Dictionary(); } //public int Id { get; } @@ -17,7 +18,9 @@ public Product(int id) //public Price Price { get; set; } //public Uri ImageUrl { get; set; } - public IDictionary Reviews { get; } + //public IDictionary Reviews { get; } - //protected ICollection Revisions => _revisions; + protected ICollection Revisions => _revisions; + + public ICollection GetRevisions => Revisions; } \ No newline at end of file diff --git a/test/Soenneker.Utils.AutoBogus.Tests/Dtos/GenerateAssertions.cs b/test/Soenneker.Utils.AutoBogus.Tests/Dtos/GenerateAssertions.cs index a5d6b62..a8e61b9 100644 --- a/test/Soenneker.Utils.AutoBogus.Tests/Dtos/GenerateAssertions.cs +++ b/test/Soenneker.Utils.AutoBogus.Tests/Dtos/GenerateAssertions.cs @@ -8,11 +8,11 @@ using FluentAssertions.Execution; using FluentAssertions.Primitives; using Soenneker.Utils.AutoBogus.Extensions; +using Soenneker.Utils.AutoBogus.Tests.Extensions; namespace Soenneker.Utils.AutoBogus.Tests.Dtos; -public sealed class GenerateAssertions - : ReferenceTypeAssertions +public sealed class GenerateAssertions : ReferenceTypeAssertions { private MethodInfo DefaultValueFactory; private IDictionary, Func> Assertions = new Dictionary, Func>(); @@ -180,7 +180,9 @@ private void AssertDefaultValue(MemberInfo memberInfo) private static bool IsUri(Type type) => type == typeof(Uri); private static bool IsUShort(Type type) => type == typeof(ushort); private static bool IsArray(Type type) => type.IsArray; + private static bool IsEnum(Type type) => type.IsEnum(); + private static bool IsDictionary(Type type) => IsType(type, typeof(IDictionary<,>)); private static bool IsEnumerable(Type type) => IsType(type, typeof(IEnumerable<>)); private static bool IsNullable(Type type) => type.IsGenericType() && type.GetGenericTypeDefinition() == typeof(Nullable<>); diff --git a/test/Soenneker.Utils.AutoBogus.Tests/Extensions/TypeExtensionTests.cs b/test/Soenneker.Utils.AutoBogus.Tests/Extensions/TypeExtensionTests.cs index d3bd190..8d6a264 100644 --- a/test/Soenneker.Utils.AutoBogus.Tests/Extensions/TypeExtensionTests.cs +++ b/test/Soenneker.Utils.AutoBogus.Tests/Extensions/TypeExtensionTests.cs @@ -18,7 +18,9 @@ public void IsDictionary_should_be_true() { Type derivedType = typeof(DerivedDictionary); - bool result = derivedType.IsDictionary(); + var cachedType = CacheService.Cache.GetCachedType(derivedType); + + bool result = cachedType.IsDictionary(); result.Should().BeTrue(); } @@ -60,8 +62,9 @@ public void GetTypeOfGenericCollectionFromInterfaceTypes_should_return_dictionar public void IsReadOnlyDictionary_should_be_true() { Type derivedType = typeof(DerivedReadOnlyDictionary); + var cachedType = CacheService.Cache.GetCachedType(derivedType); - bool result = derivedType.IsReadOnlyDictionary(); + bool result = cachedType.IsReadOnlyDictionary(); result.Should().BeTrue(); } } \ No newline at end of file diff --git a/test/Soenneker.Utils.AutoBogus.Tests/Extensions/TypeExtensions.cs b/test/Soenneker.Utils.AutoBogus.Tests/Extensions/TypeExtensions.cs new file mode 100644 index 0000000..a798567 --- /dev/null +++ b/test/Soenneker.Utils.AutoBogus.Tests/Extensions/TypeExtensions.cs @@ -0,0 +1,27 @@ +using Soenneker.Utils.AutoBogus.Services; +using System; + +namespace Soenneker.Utils.AutoBogus.Tests.Extensions; + +public static class TypeExtensions +{ + internal static bool IsEnum(this Type type) + { + return CacheService.Cache.GetCachedType(type).IsEnum; + } + + internal static bool IsAbstract(this Type type) + { + return CacheService.Cache.GetCachedType(type).IsAbstract; + } + + internal static bool IsInterface(this Type type) + { + return CacheService.Cache.GetCachedType(type).IsInterface; + } + + internal static bool IsGenericType(this Type type) + { + return CacheService.Cache.GetCachedType(type).IsGenericType; + } +} \ No newline at end of file diff --git a/test/Soenneker.Utils.AutoBogus.Tests/Soenneker.Utils.AutoBogus.Tests.csproj b/test/Soenneker.Utils.AutoBogus.Tests/Soenneker.Utils.AutoBogus.Tests.csproj index ab9c2ea..27013c9 100644 --- a/test/Soenneker.Utils.AutoBogus.Tests/Soenneker.Utils.AutoBogus.Tests.csproj +++ b/test/Soenneker.Utils.AutoBogus.Tests/Soenneker.Utils.AutoBogus.Tests.csproj @@ -20,8 +20,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive