Created
January 30, 2025 21:49
-
-
Save skarllot/61867a46b20b3b0749a65e0b932dd44e to your computer and use it in GitHub Desktop.
AutoFixture support to ImmutableList
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Immutable; | |
using AutoFixture.Kernel; | |
public class ImmutableListSpecimenBuilder : ISpecimenBuilder | |
{ | |
public object Create(object request, ISpecimenContext context) | |
{ | |
if (context == null) | |
{ | |
throw new ArgumentNullException(nameof(context)); | |
} | |
if (request is not Type t) | |
{ | |
return new NoSpecimen(); | |
} | |
var typeArguments = t.GetGenericArguments(); | |
if (typeArguments.Length != 1 || typeof(ImmutableList<>) != t.GetGenericTypeDefinition()) | |
{ | |
return new NoSpecimen(); | |
} | |
dynamic list = context.Resolve(typeof(IList<>).MakeGenericType(typeArguments)); | |
return ImmutableList.ToImmutableList(list); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment