diff --git a/src/GalaxyCheck.Tests.V3/Features/Generators/Reflection/AboutDynamicGeneration.cs b/src/GalaxyCheck.Tests.V3/Features/Generators/Reflection/AboutDynamicGeneration.cs new file mode 100644 index 00000000..b8c7701f --- /dev/null +++ b/src/GalaxyCheck.Tests.V3/Features/Generators/Reflection/AboutDynamicGeneration.cs @@ -0,0 +1,24 @@ +namespace GalaxyCheck.Tests.Features.Generators.Reflection; + +public class AboutDynamicGeneration +{ + public static TheoryData Types => new() + { + typeof(int), + typeof(string) + }; + + [Theory] + [MemberData(nameof(Types))] + public void ItCanGenerateTheType(Type type) + { + // Arrange + var gen = Gen.Create(type); + + // Act + var sample = gen.Sample(); + + // Assert + sample.Should().AllBeAssignableTo(type); + } +} diff --git a/src/GalaxyCheck/Gens/CreateGen.cs b/src/GalaxyCheck/Gens/CreateGen.cs index 872762c7..bd881709 100644 --- a/src/GalaxyCheck/Gens/CreateGen.cs +++ b/src/GalaxyCheck/Gens/CreateGen.cs @@ -20,6 +20,14 @@ public static partial class Gen /// A generator for the given type. public static IReflectedGen Create(NullabilityInfo? nullabilityInfo = null) where T : notnull => Factory().Create(nullabilityInfo); + /// + /// Generates instances of the given type, using the default . The auto-generator + /// can not be configured as precisely as more specialized generators can be, but it can create complex types + /// with minimal configuration through reflection. + /// + /// A generator for the given type. + public static IGen Create(Type type, NullabilityInfo? nullabilityInfo = null) => Factory().Create(type, nullabilityInfo); + /// /// Generates instances by the given function. Instances will not shrink by default, to enable shrinking on the /// generator, use . The function must diff --git a/src/GalaxyCheck/Gens/FactoryGen.cs b/src/GalaxyCheck/Gens/FactoryGen.cs index 6bd5488c..60d86325 100644 --- a/src/GalaxyCheck/Gens/FactoryGen.cs +++ b/src/GalaxyCheck/Gens/FactoryGen.cs @@ -1,4 +1,7 @@ -namespace GalaxyCheck +using System; +using System.Reflection; + +namespace GalaxyCheck { using Gens; @@ -12,6 +15,20 @@ public static partial class Gen /// A factory for auto-generators. public static IGenFactory Factory() => new GenFactory(); } + + public static partial class Extensions + { + /// + /// Creates an auto-generator for the given type, using the configuration that was specified on this factory. + /// + /// A generator for the given type. + public static IGen Create(this IGenFactory factory, Type type, NullabilityInfo? nullabilityInfo = null) + { + var method = typeof(IGenFactory).GetMethod(nameof(IGenFactory.Create), BindingFlags.Public | BindingFlags.Instance); + var genericMethod = method.MakeGenericMethod(type); + return ((IGen)genericMethod.Invoke(obj: factory, new object?[] { nullabilityInfo })).Cast(); + } + } } namespace GalaxyCheck.Gens