Skip to content

Commit

Permalink
Add support for building immutable collection objects
Browse files Browse the repository at this point in the history
  • Loading branch information
rickykaare committed Oct 1, 2024
1 parent e003947 commit dcf9af5
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 class ImmutableObjectBuilder(

Check failure on line 48 in src/Atc.Test/Customizations/ImmutableObjectCustomization.cs

View workflow job for this annotation

GitHub Actions / dotnet-build (ubuntu-latest)

Type 'ImmutableObjectBuilder' can be sealed because it has no subtypes in its containing assembly and is not externally visible (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1852)

Check failure on line 48 in src/Atc.Test/Customizations/ImmutableObjectCustomization.cs

View workflow job for this annotation

GitHub Actions / dotnet-build (ubuntu-latest)

Type 'ImmutableObjectBuilder' can be sealed because it has no subtypes in its containing assembly and is not externally visible (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1852)

Check failure on line 48 in src/Atc.Test/Customizations/ImmutableObjectCustomization.cs

View workflow job for this annotation

GitHub Actions / dotnet-build (macos-latest)

Type 'ImmutableObjectBuilder' can be sealed because it has no subtypes in its containing assembly and is not externally visible (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1852)

Check failure on line 48 in src/Atc.Test/Customizations/ImmutableObjectCustomization.cs

View workflow job for this annotation

GitHub Actions / dotnet-build (macos-latest)

Type 'ImmutableObjectBuilder' can be sealed because it has no subtypes in its containing assembly and is not externally visible (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1852)
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 dcf9af5

Please sign in to comment.