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 = (Device[])Enum.GetValues(typeof(Device));
}
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();
Type[] additionalResources = methodInfo.GetCustomAttributes().Select(static value => value.Type).ToArray();
object[][] data = methodInfo.GetCustomAttributes().Select(static value => value.Data).ToArray();
object[][] additionalData = 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 (additionalResources.Length > 0)
{
foreach (Type additionalType in additionalResources)
{
if (data.Length == 0)
{
if (additionalData.Length > 0)
{
Assert.Fail("Invalid usage of [AdditionalData]");
}
yield return new object[] { device, type, additionalType };
}
else
{
if (additionalData.Length > 0)
{
foreach (object[] items in data)
{
foreach (object[] additionalItems in additionalData)
{
yield return new object[] { device, type, additionalType }.Concat(items).Concat(additionalItems).ToArray();
}
}
}
else
{
foreach (object[] items in data)
{
yield return new object[] { device, type, additionalType }.Concat(items).ToArray();
}
}
}
}
}
else
{
if (data.Length == 0)
{
if (additionalData.Length > 0)
{
Assert.Fail("Invalid usage of [AdditionalData]");
}
yield return new object[] { device, type };
}
else
{
if (additionalData.Length > 0)
{
foreach (object[] items in data)
{
foreach (object[] additionalItems in additionalData)
{
yield return new object[] { device, type }.Concat(items).Concat(additionalItems).ToArray();
}
}
}
else
{
foreach (object[] items in data)
{
yield return new object[] { device, type }.Concat(items).ToArray();
}
}
}
}
}
}
else
{
if (additionalResources.Length > 0)
{
Assert.Fail("Invalid usage of [AdditionalResource]");
}
if (data.Length == 0)
{
if (additionalData.Length > 0)
{
Assert.Fail("Invalid usage of [AdditionalData]");
}
yield return new object[] { device };
}
else
{
if (additionalData.Length > 0)
{
foreach (object[] items in data)
{
foreach (object[] additionalItems in additionalData)
{
yield return new object[] { device }.Concat(items).Concat(additionalItems).ToArray();
}
}
}
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 (additionalResources.Length > 0)
{
foreach (Type additionalType in additionalResources)
{
if (data.Length == 0)
{
if (additionalData.Length > 0)
{
Assert.Fail("Invalid usage of [AdditionalData]");
}
yield return new object[] { type, additionalType };
}
else
{
if (additionalData.Length > 0)
{
foreach (object[] items in data)
{
foreach (object[] additionalItems in additionalData)
{
yield return new object[] { type, additionalType }.Concat(items).Concat(additionalItems).ToArray();
}
}
}
else
{
foreach (object[] items in data)
{
yield return new object[] { type, additionalType }.Concat(items).ToArray();
}
}
}
}
}
else
{
if (data.Length == 0)
{
if (additionalData.Length > 0)
{
Assert.Fail("Invalid usage of [AdditionalData]");
}
yield return new object[] { type };
}
else
{
if (additionalData.Length > 0)
{
foreach (object[] items in data)
{
foreach (object[] additionalItems in additionalData)
{
yield return new object[] { type }.Concat(items).Concat(additionalItems).ToArray();
}
}
}
else
{
foreach (object[] items in data)
{
yield return new object[] { type }.Concat(items).ToArray();
}
}
}
}
}
}
else
{
if (additionalResources.Length > 0)
{
Assert.Fail("Invalid usage of [AdditionalResource]");
}
if (data.Length == 0)
{
if (additionalData.Length > 0)
{
Assert.Fail("Invalid usage of [AdditionalData]");
}
yield return Array.Empty();
}
else
{
if (additionalData.Length > 0)
{
foreach (object[] items in data)
{
foreach (object[] additionalItems in additionalData)
{
yield return items.Concat(additionalItems).ToArray();
}
}
}
else
{
foreach (object[] items in data)
{
yield return items;
}
}
}
}
}
///
string ITestDataSource.GetDisplayName(MethodInfo methodInfo, object[] data)
{
return $"{methodInfo.Name} ({string.Join(", ", data)})";
}
}