Skip to content

Commit

Permalink
Introduce ClassAutoNSubstituteDataAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
rickykaare committed Sep 14, 2024
1 parent b9e8d9c commit d939db8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
50 changes: 50 additions & 0 deletions src/Atc.Test/ClassAutoNSubstituteDataAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
namespace Atc.Test;

/// <summary>
/// Provides a data source for a data theory, with the data coming from
/// a class implementing IEnumerable&lt;object[]&gt;, combined with auto-generated data
/// specimens generated by AutoFixture and NSubstitute.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public sealed class ClassAutoNSubstituteDataAttribute : ClassDataAttribute
{
public ClassAutoNSubstituteDataAttribute(Type @class)
: base(@class)
{
}

public override IEnumerable<object[]> GetData(MethodInfo testMethod)
{
var data = base.GetData(testMethod);
foreach (var values in data)
{
var fixture = FixtureFactory.Create();
yield return values
.Concat(testMethod
.GetParameters()
.Skip(values.Length)
.Select(p => GetSpecimen(fixture, p)))
.ToArray();
}
}

private static object GetSpecimen(
IFixture fixture,
ParameterInfo parameter)
{
var attributes = parameter
.GetCustomAttributes()
.OfType<IParameterCustomizationSource>()
.OrderBy(x => x is FrozenAttribute);

foreach (var attribute in attributes)
{
attribute
.GetCustomization(parameter)
.Customize(fixture);
}

return new SpecimenContext(fixture)
.Resolve(parameter);
}
}
2 changes: 1 addition & 1 deletion src/Atc.Test/MemberAutoNSubstituteDataAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace Atc.Test;
/// <summary>
/// Provides a data source for a data theory, with the data coming from
/// one of the following sources and combined with auto-generated data
/// specimens generated by AutoFixture and NSubstitute:
/// specimens generated by AutoFixture and NSubstitute.
/// <list type="number">
/// <item>A static property</item>
/// <item>A static field</item>
Expand Down

0 comments on commit d939db8

Please sign in to comment.