From ae7d3261b2a482c7c80ac59d3c25ffb6e9f33094 Mon Sep 17 00:00:00 2001 From: nth-commit Date: Fri, 26 May 2023 17:02:45 +1200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=8D=20#387=20Add=20support=20for=20cre?= =?UTF-8?q?ating=20generators=20of=20the=20given=20`Type`=20(non-generic)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Reflection/AboutDynamicGeneration.cs | 24 +++++++++++++++++++ src/GalaxyCheck/Gens/CreateGen.cs | 8 +++++++ src/GalaxyCheck/Gens/FactoryGen.cs | 19 ++++++++++++++- 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/GalaxyCheck.Tests.V3/Features/Generators/Reflection/AboutDynamicGeneration.cs 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