using System.Collections.Generic;
using static JerryUnit.UnitTestAttribute;
public static void Main()
TestRunner.Run(new Custom.MyLibraryTests());
Console.WriteLine($"{Environment.NewLine}End");
public static class MyLibrary
public static string Reverse(string text)
throw new ArgumentNullException(nameof(text));
var array = text.ToCharArray();
return new String(array);
public class MyLibraryTests
public void Reverse_String_Reversed() =>
Reverse("Jerry", "yrreJ");
public void Reverse_Empty_Empty() =>
Reverse(string.Empty, string.Empty);
public void Reverse_Null_ArgumentNullException()
try { MyLibrary.Reverse(null); }
catch (ArgumentNullException) { return; }
Assert(false, "Expected ArgumentNullException");
private static void Reverse(string original, string expected)
var actual = MyLibrary.Reverse(original);
Assert(expected == actual, $"Expected '{expected}' == '{actual}'");
public class UnitTestAttribute : Attribute
public static void Assert(bool result, string message = null)
if (!result) throw new Exception(message);
public static class TestRunner
public static void Run(object list)
throw new ArgumentNullException(nameof(list));
var tests = FindTests(list);
throw new System.ArgumentException("Test list is empty.");
RunTests(list, tests).ToList().ForEach(x => Console.WriteLine(((x.Success)
? $"✅ Pass: {(list)}.{x.Name}"
: $"❌ Fail: {(list)}.{x.Name} > Error: {x.Message}")));
static IEnumerable<System.Reflection.MethodInfo> FindTests(object list) =>
list.GetType().GetMethods().Where(x => x.GetCustomAttributes(typeof(UnitTestAttribute), false).Any());
static IEnumerable<(bool Success, string Name, string Message)> RunTests(object list, IEnumerable<System.Reflection.MethodInfo> tests)
foreach (var test in tests)
var message = string.Empty;
try { test.Invoke(list, null); }
catch (Exception e) { message = (e.InnerException is not null) ? e.InnerException.Message : e.Message; }
yield return (string.IsNullOrEmpty(message), test.Name, message);