Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce ClassAutoNSubstituteDataAttribute #39

Merged
merged 1 commit into from
Sep 14, 2024
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
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