// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. using System; using System.Linq; using System.Reflection; namespace SixLabors.ImageSharp.Tests { /// /// Triggers passing instances which return the image produced by the given test class member method /// instances will be passed for each the pixel format defined by the pixelTypes parameter /// The parameter of the factory method must be a instance /// public class WithMemberFactoryAttribute : ImageDataAttributeBase { private readonly string memberMethodName; /// /// Triggers passing instances which return the image produced by the given test class member method /// instances will be passed for each the pixel format defined by the pixelTypes parameter /// /// The name of the static test class which returns the image /// The requested pixel types /// Additional theory parameter values public WithMemberFactoryAttribute(string memberMethodName, PixelTypes pixelTypes, params object[] additionalParameters) : base(null, pixelTypes, additionalParameters) { this.memberMethodName = memberMethodName; } protected override object[] GetFactoryMethodArgs(MethodInfo testMethod, Type factoryType) { MethodInfo m = testMethod.DeclaringType.GetMethod(this.memberMethodName); Type[] args = factoryType.GetGenericArguments(); Type colorType = args.Single(); Type imgType = typeof(Image<>).MakeGenericType(colorType); Type funcType = typeof(Func<>).MakeGenericType(imgType); MethodInfo genericMethod = m.MakeGenericMethod(args); Delegate d = genericMethod.CreateDelegate(funcType); return new object[] { d }; } protected override string GetFactoryMethodName(MethodInfo testMethod) => "Lambda"; } }