From e3c79f41700c0534fed77c80768c4c847bce9173 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 13 Nov 2021 17:07:27 +0100 Subject: [PATCH] Add [AdditionalData] type to test projects --- .../ComputeSharp.Tests.Internals.csproj | 1 + .../Attributes/AdditionalDataAttribute.cs | 24 ++++ .../Attributes/AdditionalResourceAttribute.cs | 7 + .../CombinatorialTestMethodAttribute.cs | 133 ++++++++++++++++-- .../Attributes/DataAttribute.cs | 4 + .../Attributes/DeviceAttribute.cs | 4 + .../Attributes/ResourceAttribute.cs | 7 + 7 files changed, 168 insertions(+), 12 deletions(-) create mode 100644 tests/ComputeSharp.Tests/Attributes/AdditionalDataAttribute.cs diff --git a/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj b/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj index 5828f8b3..4faf1cc4 100644 --- a/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj +++ b/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj @@ -20,6 +20,7 @@ + diff --git a/tests/ComputeSharp.Tests/Attributes/AdditionalDataAttribute.cs b/tests/ComputeSharp.Tests/Attributes/AdditionalDataAttribute.cs new file mode 100644 index 00000000..6922c6ee --- /dev/null +++ b/tests/ComputeSharp.Tests/Attributes/AdditionalDataAttribute.cs @@ -0,0 +1,24 @@ +using System; + +namespace ComputeSharp.Tests.Attributes; + +/// +/// An attribute to use with adding additional arbitrary input data. +/// +[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] +public sealed class AdditionalDataAttribute : Attribute +{ + /// + /// Creates a new instance with the specified parameters. + /// + /// The additional input data. + public AdditionalDataAttribute(params object[] data) + { + Data = data; + } + + /// + /// Gets the additional input data for the current attribute. + /// + public object[] Data { get; } +} diff --git a/tests/ComputeSharp.Tests/Attributes/AdditionalResourceAttribute.cs b/tests/ComputeSharp.Tests/Attributes/AdditionalResourceAttribute.cs index 342db016..bc2cedab 100644 --- a/tests/ComputeSharp.Tests/Attributes/AdditionalResourceAttribute.cs +++ b/tests/ComputeSharp.Tests/Attributes/AdditionalResourceAttribute.cs @@ -8,10 +8,17 @@ namespace ComputeSharp.Tests.Attributes; [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] public sealed class AdditionalResourceAttribute : Attribute { + /// + /// Creates a new instance with the specified parameters. + /// + /// The additional resource type. public AdditionalResourceAttribute(Type type) { Type = type; } + /// + /// Gets the additional resource type for the current attribute. + /// public Type Type { get; } } diff --git a/tests/ComputeSharp.Tests/Attributes/CombinatorialTestMethodAttribute.cs b/tests/ComputeSharp.Tests/Attributes/CombinatorialTestMethodAttribute.cs index 0e85b112..b0465feb 100644 --- a/tests/ComputeSharp.Tests/Attributes/CombinatorialTestMethodAttribute.cs +++ b/tests/ComputeSharp.Tests/Attributes/CombinatorialTestMethodAttribute.cs @@ -39,6 +39,7 @@ public sealed class CombinatorialTestMethodAttribute : TestMethodAttribute, ITes 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) { @@ -54,13 +55,31 @@ public sealed class CombinatorialTestMethodAttribute : TestMethodAttribute, ITes { if (data.Length == 0) { + if (additionalData.Length > 0) + { + Assert.Fail("Invalid usage of [AdditionalData]"); + } + yield return new object[] { device, type, additionalType }; } else { - foreach (object[] items in data) + if (additionalData.Length > 0) { - yield return new object[] { device, type, additionalType }.Concat(items).ToArray(); + 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(); + } } } } @@ -69,13 +88,31 @@ public sealed class CombinatorialTestMethodAttribute : TestMethodAttribute, ITes { if (data.Length == 0) { + if (additionalData.Length > 0) + { + Assert.Fail("Invalid usage of [AdditionalData]"); + } + yield return new object[] { device, type }; } else { - foreach (object[] items in data) + if (additionalData.Length > 0) { - yield return new object[] { device, type }.Concat(items).ToArray(); + 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(); + } } } } @@ -90,13 +127,31 @@ public sealed class CombinatorialTestMethodAttribute : TestMethodAttribute, ITes if (data.Length == 0) { + if (additionalData.Length > 0) + { + Assert.Fail("Invalid usage of [AdditionalData]"); + } + yield return new object[] { device }; } else { - foreach (object[] items in data) + if (additionalData.Length > 0) { - yield return new object[] { device }.Concat(items).ToArray(); + 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(); + } } } } @@ -112,13 +167,31 @@ public sealed class CombinatorialTestMethodAttribute : TestMethodAttribute, ITes { if (data.Length == 0) { + if (additionalData.Length > 0) + { + Assert.Fail("Invalid usage of [AdditionalData]"); + } + yield return new object[] { type, additionalType }; } else { - foreach (object[] items in data) + if (additionalData.Length > 0) { - yield return new object[] { type, additionalType }.Concat(items).ToArray(); + 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(); + } } } } @@ -127,13 +200,31 @@ public sealed class CombinatorialTestMethodAttribute : TestMethodAttribute, ITes { if (data.Length == 0) { + if (additionalData.Length > 0) + { + Assert.Fail("Invalid usage of [AdditionalData]"); + } + yield return new object[] { type }; } else { - foreach (object[] items in data) + if (additionalData.Length > 0) { - yield return new object[] { type }.Concat(items).ToArray(); + 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(); + } } } } @@ -148,13 +239,31 @@ public sealed class CombinatorialTestMethodAttribute : TestMethodAttribute, ITes if (data.Length == 0) { + if (additionalData.Length > 0) + { + Assert.Fail("Invalid usage of [AdditionalData]"); + } + yield return Array.Empty(); } else { - foreach (object[] items in data) + if (additionalData.Length > 0) { - yield return items; + 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; + } } } } diff --git a/tests/ComputeSharp.Tests/Attributes/DataAttribute.cs b/tests/ComputeSharp.Tests/Attributes/DataAttribute.cs index 806263ab..c4f63acf 100644 --- a/tests/ComputeSharp.Tests/Attributes/DataAttribute.cs +++ b/tests/ComputeSharp.Tests/Attributes/DataAttribute.cs @@ -8,6 +8,10 @@ namespace ComputeSharp.Tests.Attributes; [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] public sealed class DataAttribute : Attribute { + /// + /// Creates a new instance with the specified parameters. + /// + /// The input data. public DataAttribute(params object[] data) { Data = data; diff --git a/tests/ComputeSharp.Tests/Attributes/DeviceAttribute.cs b/tests/ComputeSharp.Tests/Attributes/DeviceAttribute.cs index aa100468..7c249b42 100644 --- a/tests/ComputeSharp.Tests/Attributes/DeviceAttribute.cs +++ b/tests/ComputeSharp.Tests/Attributes/DeviceAttribute.cs @@ -9,6 +9,10 @@ namespace ComputeSharp.Tests.Attributes; [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] public sealed class DeviceAttribute : Attribute { + /// + /// Creates a new instance with the specified parameters. + /// + /// The target device type to use.. public DeviceAttribute(Device device) { Device = device; diff --git a/tests/ComputeSharp.Tests/Attributes/ResourceAttribute.cs b/tests/ComputeSharp.Tests/Attributes/ResourceAttribute.cs index 2cd0d476..72f9d75a 100644 --- a/tests/ComputeSharp.Tests/Attributes/ResourceAttribute.cs +++ b/tests/ComputeSharp.Tests/Attributes/ResourceAttribute.cs @@ -8,10 +8,17 @@ namespace ComputeSharp.Tests.Attributes; [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] public sealed class ResourceAttribute : Attribute { + /// + /// Creates a new instance with the specified parameters. + /// + /// The resource type. public ResourceAttribute(Type type) { Type = type; } + /// + /// Gets the resource type for the current attribute. + /// public Type Type { get; } }