Skip to content

Commit 36b19f4

Browse files
authored
Merge pull request #39 from atc-net/feature/classdata
Introduce ClassAutoNSubstituteDataAttribute
2 parents b9e8d9c + d939db8 commit 36b19f4

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
namespace Atc.Test;
2+
3+
/// <summary>
4+
/// Provides a data source for a data theory, with the data coming from
5+
/// a class implementing IEnumerable&lt;object[]&gt;, combined with auto-generated data
6+
/// specimens generated by AutoFixture and NSubstitute.
7+
/// </summary>
8+
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
9+
public sealed class ClassAutoNSubstituteDataAttribute : ClassDataAttribute
10+
{
11+
public ClassAutoNSubstituteDataAttribute(Type @class)
12+
: base(@class)
13+
{
14+
}
15+
16+
public override IEnumerable<object[]> GetData(MethodInfo testMethod)
17+
{
18+
var data = base.GetData(testMethod);
19+
foreach (var values in data)
20+
{
21+
var fixture = FixtureFactory.Create();
22+
yield return values
23+
.Concat(testMethod
24+
.GetParameters()
25+
.Skip(values.Length)
26+
.Select(p => GetSpecimen(fixture, p)))
27+
.ToArray();
28+
}
29+
}
30+
31+
private static object GetSpecimen(
32+
IFixture fixture,
33+
ParameterInfo parameter)
34+
{
35+
var attributes = parameter
36+
.GetCustomAttributes()
37+
.OfType<IParameterCustomizationSource>()
38+
.OrderBy(x => x is FrozenAttribute);
39+
40+
foreach (var attribute in attributes)
41+
{
42+
attribute
43+
.GetCustomization(parameter)
44+
.Customize(fixture);
45+
}
46+
47+
return new SpecimenContext(fixture)
48+
.Resolve(parameter);
49+
}
50+
}

src/Atc.Test/MemberAutoNSubstituteDataAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ namespace Atc.Test;
33
/// <summary>
44
/// Provides a data source for a data theory, with the data coming from
55
/// one of the following sources and combined with auto-generated data
6-
/// specimens generated by AutoFixture and NSubstitute:
6+
/// specimens generated by AutoFixture and NSubstitute.
77
/// <list type="number">
88
/// <item>A static property</item>
99
/// <item>A static field</item>

0 commit comments

Comments
 (0)