using System;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.Tests.Helpers;
///
/// A helper class for test methods.
///
public static class TestHelper
{
///
/// Runs a generic test methods with the specified arguments.
///
/// A instance wrapping the target method.
/// The type argument to use to invoke .
/// The arguments to use to invoke .
public static void Run(Delegate @delegate, Type type, params object[] arguments)
{
if (@delegate.Method is not { IsStatic: true, IsGenericMethod: true })
{
Assert.Fail("The input delegate must point to a generic static method");
}
try
{
_ = @delegate.Method.GetGenericMethodDefinition().MakeGenericMethod(type).Invoke(null, arguments);
}
catch (TargetInvocationException e) when (e.InnerException is not null)
{
throw e.InnerException;
}
}
///
/// Runs a generic test methods with the specified arguments.
///
/// A instance wrapping the target method.
/// The first type argument to use to invoke .
/// The second type argument to use to invoke .
/// The arguments to use to invoke .
public static void Run(Delegate @delegate, Type type1, Type type2, params object[] arguments)
{
if (@delegate.Method is not { IsStatic: true, IsGenericMethod: true })
{
Assert.Fail("The input delegate must point to a generic static method");
}
try
{
_ = @delegate.Method.GetGenericMethodDefinition().MakeGenericMethod(type1, type2).Invoke(null, arguments);
}
catch (TargetInvocationException e) when (e.InnerException is not null)
{
throw e.InnerException;
}
}
}