Skip to content

Commit caf0d21

Browse files
committed
🌍 #387 Add support for creating generators of the given Type (non-generic)
1 parent 3d0a166 commit caf0d21

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace GalaxyCheck.Tests.Features.Generators.Reflection;
2+
3+
public class AboutDynamicGeneration
4+
{
5+
public static TheoryData<Type> Types => new()
6+
{
7+
typeof(int),
8+
typeof(string)
9+
};
10+
11+
[Theory]
12+
[MemberData(nameof(Types))]
13+
public void ItCanGenerateTheType(Type type)
14+
{
15+
// Arrange
16+
var gen = Gen.Create(type);
17+
18+
// Act
19+
var sample = gen.Sample();
20+
21+
// Assert
22+
sample.Should().AllBeAssignableTo(type);
23+
}
24+
}

‎src/GalaxyCheck/Gens/CreateGen.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ public static partial class Gen
2020
/// <returns>A generator for the given type.</returns>
2121
public static IReflectedGen<T> Create<T>(NullabilityInfo? nullabilityInfo = null) where T : notnull => Factory().Create<T>(nullabilityInfo);
2222

23+
/// <summary>
24+
/// Generates instances of the given type, using the default <see cref="IGenFactory"/>. The auto-generator
25+
/// can not be configured as precisely as more specialized generators can be, but it can create complex types
26+
/// with minimal configuration through reflection.
27+
/// </summary>
28+
/// <returns>A generator for the given type.</returns>
29+
public static IGen<object> Create(Type type, NullabilityInfo? nullabilityInfo = null) => Factory().Create(type, nullabilityInfo);
30+
2331
/// <summary>
2432
/// Generates instances by the given function. Instances will not shrink by default, to enable shrinking on the
2533
/// generator, use <see cref="Extensions.Unfold{T}(IGen{T}, Func{T, IExampleSpace{T}})"/>. The function must

‎src/GalaxyCheck/Gens/FactoryGen.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
namespace GalaxyCheck
1+
using System;
2+
using System.Reflection;
3+
4+
namespace GalaxyCheck
25
{
36
using Gens;
47

@@ -12,6 +15,20 @@ public static partial class Gen
1215
/// <returns>A factory for auto-generators.</returns>
1316
public static IGenFactory Factory() => new GenFactory();
1417
}
18+
19+
public static partial class Extensions
20+
{
21+
/// <summary>
22+
/// Creates an auto-generator for the given type, using the configuration that was specified on this factory.
23+
/// </summary>
24+
/// <returns>A generator for the given type.</returns>
25+
public static IGen<object> Create(this IGenFactory factory, Type type, NullabilityInfo? nullabilityInfo = null)
26+
{
27+
var method = typeof(IGenFactory).GetMethod(nameof(IGenFactory.Create), BindingFlags.Public | BindingFlags.Instance);
28+
var genericMethod = method.MakeGenericMethod(type);
29+
return ((IGen)genericMethod.Invoke(obj: factory, new object?[] { nullabilityInfo })).Cast<object>();
30+
}
31+
}
1532
}
1633

1734
namespace GalaxyCheck.Gens

0 commit comments

Comments
 (0)