-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce ClassAutoNSubstituteDataAttribute
- Loading branch information
1 parent
b9e8d9c
commit d939db8
Showing
2 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
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
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<object[]>, 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); | ||
} | ||
} |
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