From d939db8115d85aefcfd4338184f021020805da28 Mon Sep 17 00:00:00 2001 From: Ricky Kaare Engelharth Date: Sat, 14 Sep 2024 09:47:15 +0200 Subject: [PATCH] Introduce ClassAutoNSubstituteDataAttribute --- .../ClassAutoNSubstituteDataAttribute.cs | 50 +++++++++++++++++++ .../MemberAutoNSubstituteDataAttribute.cs | 2 +- 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/Atc.Test/ClassAutoNSubstituteDataAttribute.cs diff --git a/src/Atc.Test/ClassAutoNSubstituteDataAttribute.cs b/src/Atc.Test/ClassAutoNSubstituteDataAttribute.cs new file mode 100644 index 0000000..9f4fa06 --- /dev/null +++ b/src/Atc.Test/ClassAutoNSubstituteDataAttribute.cs @@ -0,0 +1,50 @@ +namespace Atc.Test; + +/// +/// 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. +/// +[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] +public sealed class ClassAutoNSubstituteDataAttribute : ClassDataAttribute +{ + public ClassAutoNSubstituteDataAttribute(Type @class) + : base(@class) + { + } + + public override IEnumerable 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() + .OrderBy(x => x is FrozenAttribute); + + foreach (var attribute in attributes) + { + attribute + .GetCustomization(parameter) + .Customize(fixture); + } + + return new SpecimenContext(fixture) + .Resolve(parameter); + } +} \ No newline at end of file diff --git a/src/Atc.Test/MemberAutoNSubstituteDataAttribute.cs b/src/Atc.Test/MemberAutoNSubstituteDataAttribute.cs index c9a6b0b..8047494 100644 --- a/src/Atc.Test/MemberAutoNSubstituteDataAttribute.cs +++ b/src/Atc.Test/MemberAutoNSubstituteDataAttribute.cs @@ -3,7 +3,7 @@ namespace Atc.Test; /// /// 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. /// /// A static property /// A static field