Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace GalaxyCheck.Tests.Features.Generators.Reflection;

public class AboutDynamicGeneration
{
public static TheoryData<Type> 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);
}
}
8 changes: 8 additions & 0 deletions src/GalaxyCheck/Gens/CreateGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ public static partial class Gen
/// <returns>A generator for the given type.</returns>
public static IReflectedGen<T> Create<T>(NullabilityInfo? nullabilityInfo = null) where T : notnull => Factory().Create<T>(nullabilityInfo);

/// <summary>
/// Generates instances of the given type, using the default <see cref="IGenFactory"/>. 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.
/// </summary>
/// <returns>A generator for the given type.</returns>
public static IGen<object> Create(Type type, NullabilityInfo? nullabilityInfo = null) => Factory().Create(type, nullabilityInfo);

/// <summary>
/// Generates instances by the given function. Instances will not shrink by default, to enable shrinking on the
/// generator, use <see cref="Extensions.Unfold{T}(IGen{T}, Func{T, IExampleSpace{T}})"/>. The function must
Expand Down
19 changes: 18 additions & 1 deletion src/GalaxyCheck/Gens/FactoryGen.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace GalaxyCheck
using System;
using System.Reflection;

namespace GalaxyCheck
{
using Gens;

Expand All @@ -12,6 +15,20 @@ public static partial class Gen
/// <returns>A factory for auto-generators.</returns>
public static IGenFactory Factory() => new GenFactory();
}

public static partial class Extensions
{
/// <summary>
/// Creates an auto-generator for the given type, using the configuration that was specified on this factory.
/// </summary>
/// <returns>A generator for the given type.</returns>
public static IGen<object> 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<object>();
}
}
}

namespace GalaxyCheck.Gens
Expand Down