Skip to content

Commit

Permalink
Merge pull request #41 from atc-net/feature/immutable-obj
Browse files Browse the repository at this point in the history
Add support for building immutable collection objects
  • Loading branch information
rickykaare authored Oct 1, 2024
2 parents e003947 + 85c2614 commit a5adede
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/Atc.Test/Customizations/ImmutableObjectCustomization.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#if NET5_0_OR_GREATER
using System.Collections.Immutable;

namespace Atc.Test.Customizations;

[AutoRegister]
public class ImmutableObjectCustomization : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Customizations.Add(
new ImmutableObjectBuilder(
typeof(ImmutableArray<>),
typeof(List<>),
o => ImmutableArray.ToImmutableArray(o)));

fixture.Customizations.Add(
new ImmutableObjectBuilder(
typeof(ImmutableList<>),
typeof(List<>),
o => ImmutableList.ToImmutableList(o)));

fixture.Customizations.Add(
new ImmutableObjectBuilder(
typeof(ImmutableDictionary<,>),
typeof(Dictionary<,>),
o => ImmutableDictionary.ToImmutableDictionary(o)));

fixture.Customizations.Add(
new ImmutableObjectBuilder(
typeof(ImmutableHashSet<>),
typeof(HashSet<>),
o => ImmutableHashSet.ToImmutableHashSet(o)));

fixture.Customizations.Add(
new ImmutableObjectBuilder(
typeof(ImmutableSortedSet<>),
typeof(SortedSet<>),
o => ImmutableSortedSet.ToImmutableSortedSet(o)));

fixture.Customizations.Add(
new ImmutableObjectBuilder(
typeof(ImmutableSortedDictionary<,>),
typeof(SortedDictionary<,>),
o => ImmutableSortedDictionary.ToImmutableSortedDictionary(o)));
}

private sealed class ImmutableObjectBuilder(
Type immutableType,
Type underlyingType,
Func<dynamic, object> converter)
: ISpecimenBuilder
{
public object Create(object request, ISpecimenContext context)
{
if (GetRequestType(request) is { } type
&& type.IsGenericType
&& type.GetGenericTypeDefinition() == immutableType
&& type.GetGenericArguments() is { Length: > 0 } args)
{
var listType = underlyingType.MakeGenericType(args);
dynamic list = context.Resolve(listType);

return converter.Invoke(list);
}

return new NoSpecimen();
}

private static Type? GetRequestType(object request)
=> request switch
{
ParameterInfo pi => pi.ParameterType,
Type t => t,
_ => null,
};
}
}
#endif

0 comments on commit a5adede

Please sign in to comment.