namespace MiniTestRunner;
///
/// Represents a test class containing multiple test methods and optional setup/teardown logic.
/// Responsible for executing all test methods and summarizing the results.
///
/// The of the test class.
/// A list of test methods to execute.
/// An optional setup method to run before each test.
/// An optional teardown method to run after each test.
/// An optional description of the test class.
public sealed record TestClass(
Type Type,
List TestMethods,
TestSetup? BeforeEach,
TestSetup? AfterEach,
string? Description)
{
///
/// Executes all test methods in the class, applying setup and teardown logic if provided.
/// Results are printed to the console and summarized at the end.
///
/// A object representing the aggregated results of all test executions.
public TestResults Run()
{
var results = new TestResults();
var instance = Activator.CreateInstance(Type);
if (instance is null)
{
using var _ = new ConsoleColoring(ConsoleColor.Yellow);
Console.WriteLine($"Failed to create a class {Type.FullName} instance.");
return results;
}
Console.WriteLine($"Running tests from class {this.Type.FullName}...");
if (this.Description is not null)
{
Console.WriteLine(this.Description);
}
foreach (var testMethod in this.TestMethods
.OrderBy(tm => tm.Priority)
.ThenBy(tm => tm.MethodInfo.Name))
{
this.BeforeEach?.Run(instance);
results += testMethod.Run(instance);
this.AfterEach?.Run(instance);
}
results.Summarize();
return results;
}
}