using System.Reflection;
namespace Chronos.Testing;
/// One discovered executable test (a method, or one case of a theory).
public sealed class TestCase
{
internal TestCase(Type type, MethodInfo method, object?[]? arguments, string? caseName)
{
Type = type;
Method = method;
Arguments = arguments;
var suffix = arguments is null ? string.Empty : "(" + (caseName ?? string.Join(", ", arguments.Select(Assert.Format))) + ")";
FullName = $"{type.FullName}.{method.Name}{suffix}";
Categories = type.GetCustomAttributes()
.Concat(method.GetCustomAttributes())
.Select(c => c.Name)
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToArray();
TimeoutMs = method.GetCustomAttribute()?.Milliseconds
?? type.GetCustomAttribute()?.Milliseconds
?? 300_000;
Serial = type.GetCustomAttribute() is not null;
StaticSkipReason = ComputeSkipReason(type, method);
}
public Type Type { get; }
public MethodInfo Method { get; }
public object?[]? Arguments { get; }
public string FullName { get; }
public IReadOnlyList Categories { get; }
public int TimeoutMs { get; }
public bool Serial { get; }
public string? StaticSkipReason { get; }
private static string? ComputeSkipReason(Type type, MethodInfo method)
{
var skip = method.GetCustomAttribute() ?? type.GetCustomAttribute();
if (skip is not null) return skip.Reason;
if ((method.GetCustomAttribute() ?? type.GetCustomAttribute()) is not null
&& !OperatingSystem.IsWindows())
return "Windows only";
foreach (var env in type.GetCustomAttributes().Concat(method.GetCustomAttributes()))
{
if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(env.Variable)))
return $"{env.Variable} not set";
}
return null;
}
}
public enum TestOutcome
{
Passed,
Failed,
Skipped,
}
public sealed record TestResult(TestCase Test, TestOutcome Outcome, TimeSpan Duration, string? Message, string? StackTrace, string Log);