diff --git a/test/Common/AutoFixture/Attributes/BitAutoDataAttribute.cs b/test/Common/AutoFixture/Attributes/BitAutoDataAttribute.cs new file mode 100644 index 000000000..dfb06a254 --- /dev/null +++ b/test/Common/AutoFixture/Attributes/BitAutoDataAttribute.cs @@ -0,0 +1,29 @@ +using System; +using Xunit.Sdk; +using AutoFixture; +using System.Reflection; +using System.Collections.Generic; +using Bit.Test.Common.Helpers; + +namespace Bit.Test.Common.AutoFixture.Attributes +{ + public class BitAutoDataAttribute : DataAttribute + { + private readonly Func _createFixture; + private readonly object[] _fixedTestParameters; + + public BitAutoDataAttribute(params object[] fixedTestParameters) : + this(() => new Fixture(), fixedTestParameters) + { } + + public BitAutoDataAttribute(Func createFixture, params object[] fixedTestParameters) : + base() + { + _createFixture = createFixture; + _fixedTestParameters = fixedTestParameters; + } + + public override IEnumerable GetData(MethodInfo testMethod) + => BitAutoDataAttributeHelpers.GetData(testMethod, _createFixture(), _fixedTestParameters); + } +} diff --git a/test/Common/AutoFixture/Attributes/MemberAutoDataAttribute.cs b/test/Common/AutoFixture/Attributes/MemberAutoDataAttribute.cs index cded9a26c..a154128ae 100644 --- a/test/Common/AutoFixture/Attributes/MemberAutoDataAttribute.cs +++ b/test/Common/AutoFixture/Attributes/MemberAutoDataAttribute.cs @@ -1,69 +1,27 @@ using System; -using Xunit.Sdk; using Xunit; using AutoFixture; using System.Reflection; -using System.Collections.Generic; -using AutoFixture.Xunit2; using System.Linq; -using AutoFixture.Kernel; -using System.Collections; +using Bit.Test.Common.Helpers; namespace Bit.Test.Common.AutoFixture.Attributes { - public class MemberAutoDataAttribute : MemberDataAttributeBase + public class BitMemberAutoDataAttribute : MemberDataAttributeBase { private readonly Func _createFixture; - public MemberAutoDataAttribute(string memberName, params object[] parameters) : + public BitMemberAutoDataAttribute(string memberName, params object[] parameters) : this(() => new Fixture(), memberName, parameters) { } - public MemberAutoDataAttribute(Func createFixture, string memberName, params object[] parameters) : + public BitMemberAutoDataAttribute(Func createFixture, string memberName, params object[] parameters) : base(memberName, parameters) { _createFixture = createFixture; } - protected override object[] ConvertDataItem(MethodInfo testMethod, object item) - { - var methodParameters = testMethod.GetParameters(); - var classCustomizations = testMethod.DeclaringType.GetCustomAttributes().Select(attr => attr.GetCustomization()); - var methodCustomizations = testMethod.GetCustomAttributes().Select(attr => attr.GetCustomization()); - - var array = item as object[] ?? Array.Empty(); - - var fixture = ApplyCustomizations(ApplyCustomizations(_createFixture(), classCustomizations), methodCustomizations); - var missingParameters = methodParameters.Skip(array.Length).Select(p => CustomizeAndCreate(p, fixture)); - - return array.Concat(missingParameters).ToArray(); - } - - private static object CustomizeAndCreate(ParameterInfo p, IFixture fixture) - { - var customizations = p.GetCustomAttributes(typeof(CustomizeAttribute), false) - .OfType() - .Select(attr => attr.GetCustomization(p)); - - var context = new SpecimenContext(ApplyCustomizations(fixture, customizations)); - return context.Resolve(p); - } - - private static IFixture ApplyCustomizations(IFixture fixture, IEnumerable customizations) - { - var newFixture = new Fixture(); - - foreach (var customization in fixture.Customizations.Reverse().Select(b => b.ToCustomization())) - { - newFixture.Customize(customization); - } - - foreach (var customization in customizations) - { - newFixture.Customize(customization); - } - - return newFixture; - } + protected override object[] ConvertDataItem(MethodInfo testMethod, object item) => + BitAutoDataAttributeHelpers.GetData(testMethod, _createFixture(), item as object[]).First(); } } diff --git a/test/Common/Helpers/BitAutoDataAttributeHelpers.cs b/test/Common/Helpers/BitAutoDataAttributeHelpers.cs new file mode 100644 index 000000000..394a03ed8 --- /dev/null +++ b/test/Common/Helpers/BitAutoDataAttributeHelpers.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using AutoFixture; +using AutoFixture.Kernel; +using AutoFixture.Xunit2; +using Bit.Test.Common.AutoFixture.Attributes; + +namespace Bit.Test.Common.Helpers +{ + public static class BitAutoDataAttributeHelpers + { + public static IEnumerable GetData(MethodInfo testMethod, IFixture fixture, object[] fixedTestParamters) + { + var methodParameters = testMethod.GetParameters(); + var classCustomizations = testMethod.DeclaringType.GetCustomAttributes().Select(attr => attr.GetCustomization()); + var methodCustomizations = testMethod.GetCustomAttributes().Select(attr => attr.GetCustomization()); + + fixedTestParamters = fixedTestParamters ?? Array.Empty(); + + fixture = ApplyCustomizations(ApplyCustomizations(fixture, classCustomizations), methodCustomizations); + var missingParameters = methodParameters.Skip(fixedTestParamters.Length).Select(p => CustomizeAndCreate(p, fixture)); + + return new object[1][] { fixedTestParamters.Concat(missingParameters).ToArray() }; + } + + public static object CustomizeAndCreate(ParameterInfo p, IFixture fixture) + { + var customizations = p.GetCustomAttributes(typeof(CustomizeAttribute), false) + .OfType() + .Select(attr => attr.GetCustomization(p)); + + var context = new SpecimenContext(ApplyCustomizations(fixture, customizations)); + return context.Resolve(p); + } + + public static IFixture ApplyCustomizations(IFixture fixture, IEnumerable customizations) + { + var newFixture = new Fixture(); + + foreach (var customization in fixture.Customizations.Reverse().Select(b => b.ToCustomization())) + { + newFixture.Customize(customization); + } + + foreach (var customization in customizations) + { + newFixture.Customize(customization); + } + + return newFixture; + } + } +}