using System.Reflection;
using MiniTest.Attributes;
namespace MiniTestRunner;
///
/// Entry point for the MiniTestRunner application.
/// Responsible for loading test assemblies, discovering test classes and methods, and executing them.
///
class Program
{
///
/// Main method that runs the test runner.
/// Iterates over provided assembly paths, loads each assembly, discovers tests, and executes them.
///
/// An array of file paths to test assemblies.
static void Main(string[] args)
{
foreach (var path in args)
{
TestLoadContext? context = null;
try
{
(context, var assembly) = LoadTestAssembly(path);
var testClasses = FindAllTests(assembly);
var results = new TestResults();
foreach (var testClass in testClasses)
{
results += testClass.Run();
Console.WriteLine(new string('#', Math.Min(80, Console.WindowWidth)));
}
Console.WriteLine($"Summary of running tests from {assembly.GetName().Name}:");
results.Summarize();
}
finally
{
context?.Unload();
}
}
}
///
/// Loads a test assembly from the specified path using a custom .
///
/// The file path to the test assembly.
/// A tuple containing the and the loaded .
public static (TestLoadContext, Assembly) LoadTestAssembly(string path)
{
var context = new TestLoadContext(path);
var assembly = context.LoadFromAssemblyPath(Path.GetFullPath(path));
return (context, assembly);
}
///
/// Discovers all test classes and their associated test methods in the given assembly.
///
/// The assembly to scan for test classes.
/// A list of instances representing discovered tests.
public static List FindAllTests(Assembly assembly)
{
using var _ = new ConsoleColoring(ConsoleColor.Yellow);
var testClasses = new List();
foreach (var type in assembly.ExportedTypes)
{
var testClass = type.GetCustomAttribute();
if (type.GetConstructor(Type.EmptyTypes) is null)
{
Console.WriteLine($"Warning: Skipping test class {type.FullName}, because it has no parameterless constructor.");
continue;
}
if (testClass is not null)
{
var testClassDescription = type.GetCustomAttribute();
var tests = new List();
TestSetup? beforeEach = null;
TestSetup? afterEach = null;
foreach (var method in type.GetMethods())
{
var beforeEachAttribute = method.GetCustomAttribute();
var afterEachAttribute = method.GetCustomAttribute();
var testMethod = method.GetCustomAttribute();
if (testMethod is not null)
{
var description = method.GetCustomAttribute();
var priority = method.GetCustomAttribute();
var parameters = method
.GetCustomAttributes()
.Where(attribute =>
{
var compatible = ValidateTypes(attribute.Data, method
.GetParameters()
.Select(param => param.GetModifiedParameterType())
.ToArray());
if (!compatible)
{
Console.WriteLine($"Skipping test {method.Name}({attribute.Description}), because it has incompatible parameters.");
}
return compatible;
})
.Select(attribute => new TestParameters(attribute.Description, attribute.Data))
.ToList();
tests.Add(new TestMethod(method, parameters, description?.Description, priority?.Priority ?? 0));
}
if (beforeEachAttribute is not null)
{
if (method.GetParameters().Length != 0)
{
Console.WriteLine($"Warning: Setup method {method.Name} takes {method.GetParameters().Length} parameters.");
continue;
}
beforeEach = new TestSetup(method);
}
if (afterEachAttribute is not null)
{
if (method.GetParameters().Length != 0)
{
Console.WriteLine($"Warning: Setup method {method.Name} takes {method.GetParameters().Length} parameters.");
continue;
}
afterEach = new TestSetup(method);
}
}
testClasses.Add(new TestClass(type, tests, beforeEach, afterEach, testClassDescription?.Description));
}
}
return testClasses;
}
///
/// Validates that the provided objects match the expected parameter types.
///
/// The array of objects to validate.
/// The array of expected parameter types.
/// true if all objects match the expected types; otherwise, false.
private static bool ValidateTypes(object?[] objects, Type[] types)
{
if (objects.Length != types.Length)
{
return false;
}
for (var index = 0; index < objects.Length; index++)
{
var obj = objects[index];
var type = types[index];
if (!type.IsInstanceOfType(obj))
{
return false;
}
}
return true;
}
}