using System.Reflection; namespace MiniTestRunner; /// /// Represents a test method with optional parameters, description, and priority. /// Provides functionality to execute the test and report results. /// /// The reflection metadata for the test method. /// Optional list of parameter sets for data-driven tests. /// An optional description of the test method. /// The priority value used to order test execution. public sealed record TestMethod( MethodInfo MethodInfo, List? Parameters, string? Description, int Priority = int.MinValue) { /// /// Executes the test method on the given instance. /// If parameters are provided, runs the method for each parameter set. /// /// The instance of the test class on which to invoke the method. /// A object containing the outcome of the test execution. public TestResults Run(object instance) { return this.Parameters is { Count: > 0 } ? this.RunParameterizedTest(instance) : this.RunTest(instance); } /// /// Executes a non-parameterized test method and prints the result to the console. /// /// The instance of the test class. /// A object representing the result of the test. private TestResults RunTest(object instance) { Console.Write($"{this.MethodInfo.Name,-60}: "); TestResults results; try { using var _ = new ConsoleColoring(ConsoleColor.Green); this.MethodInfo.Invoke(instance, []); Console.WriteLine("PASSED"); results = new TestResults(1, 1); } catch (Exception e) { using var _ = new ConsoleColoring(ConsoleColor.Red); Console.WriteLine("FAILED"); Console.WriteLine(e.InnerException?.Message); results = new TestResults(0, 1); } if (this.Description is not null) { Console.WriteLine(this.Description); } return results; } /// /// Executes a parameterized test method for each set of parameters and prints the results. /// /// The instance of the test class. /// A object representing the aggregated results of all parameterized test runs. private TestResults RunParameterizedTest(object obj) { var results = new TestResults(); Console.WriteLine(this.MethodInfo.Name); foreach (var param in this.Parameters!) { Console.Write($" - {param.Description,-57}: "); try { using var _ = new ConsoleColoring(ConsoleColor.Green); this.MethodInfo.Invoke(obj, param.Parameters); Console.WriteLine("PASSED"); results += new TestResults(1, 1); } catch (Exception e) { using var _ = new ConsoleColoring(ConsoleColor.Red); Console.WriteLine("FAILED"); Console.WriteLine(e.InnerException?.Message); results += new TestResults(0, 1); } } if (this.Description is not null) { Console.WriteLine(this.Description); } return results; } }