using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.Tests.Attributes { /// /// A custom implementation that also acts as a combinatorial /// instance. It has dedicated paths for target devices and resource types, and arbitrary inputs for the test methods. /// /// /// Use this attribute in combination with , , /// or , not other existing MSTest attributes. /// [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] public sealed class CombinatorialTestMethodAttribute : TestMethodAttribute, ITestDataSource { /// IEnumerable ITestDataSource.GetData(MethodInfo methodInfo) { Device[] devices; if (methodInfo.GetCustomAttribute() is not null) { devices = Enum.GetValues(); } else if (methodInfo.GetCustomAttributes().ToArray() is { Length: > 0 } values) { devices = values.Select(static value => value.Device).ToArray(); } else { devices = Array.Empty(); } Type[] resources = methodInfo.GetCustomAttributes().Select(static value => value.Type).ToArray(); object[][] data = methodInfo.GetCustomAttributes().Select(static value => value.Data).ToArray(); if (devices.Length > 0) { foreach (Device device in devices) { if (resources.Length > 0) { foreach (Type type in resources) { if (data.Length == 0) { yield return new object[] { device, type }; } else { foreach (object[] items in data) { yield return new object[] { device, type }.Concat(items).ToArray(); } } } } else { if (data.Length == 0) { yield return new object[] { device }; } else { foreach (object[] items in data) { yield return new object[] { device }.Concat(items).ToArray(); } } } } } else if (resources.Length > 0) { foreach (Type type in resources) { if (data.Length == 0) { yield return new object[] { type }; } else { foreach (object[] items in data) { yield return new object[] { type }.Concat(items).ToArray(); } } } } else { if (data.Length == 0) { yield return Array.Empty(); } else { foreach (object[] items in data) { yield return items; } } } } /// string ITestDataSource.GetDisplayName(MethodInfo methodInfo, object[] data) { return $"{methodInfo.Name} ({string.Join(", ", data)})"; } } }