116 lines
3.7 KiB
C#
116 lines
3.7 KiB
C#
namespace GraphTUI.Tests;
|
|
|
|
public static class Assert
|
|
{
|
|
public static void True(bool condition, string? message = null)
|
|
{
|
|
if (!condition)
|
|
throw new Exception(message ?? "Assertion failed: expected true, got false.");
|
|
}
|
|
|
|
public static void False(bool condition, string? message = null)
|
|
{
|
|
if (condition)
|
|
throw new Exception(message ?? "Assertion failed: expected false, got true.");
|
|
}
|
|
|
|
public static void Equal<T>(T expected, T actual, string? message = null)
|
|
{
|
|
if (!EqualityComparer<T>.Default.Equals(expected, actual))
|
|
throw new Exception(message ?? $"Assertion failed: expected '{expected}', got '{actual}'.");
|
|
}
|
|
|
|
public static void NotNull(object? obj, string? message = null)
|
|
{
|
|
if (obj is null)
|
|
throw new Exception(message ?? "Assertion failed: expected non-null, got null.");
|
|
}
|
|
|
|
public static void Within(double expected, double actual, double tolerance, string? message = null)
|
|
{
|
|
double diff = System.Math.Abs(expected - actual);
|
|
if (diff > tolerance || double.IsNaN(actual))
|
|
throw new Exception(message ?? $"Assertion failed: expected '{expected}' +/- {tolerance}, got '{actual}' (diff={diff}).");
|
|
}
|
|
|
|
public static void Throws<TException>(Action action, string? message = null) where TException : Exception
|
|
{
|
|
try
|
|
{
|
|
action();
|
|
}
|
|
catch (TException)
|
|
{
|
|
return;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception(message ?? $"Assertion failed: expected {typeof(TException).Name}, but got {ex.GetType().Name}: {ex.Message}");
|
|
}
|
|
|
|
throw new Exception(message ?? $"Assertion failed: expected {typeof(TException).Name}, but no exception was thrown.");
|
|
}
|
|
}
|
|
|
|
public sealed class TestRunner
|
|
{
|
|
private readonly List<(string Name, Action TestAction)> _tests = [];
|
|
|
|
public void Add(string name, Action testAction)
|
|
{
|
|
_tests.Add((name, testAction));
|
|
}
|
|
|
|
public int RunAll()
|
|
{
|
|
int passed = 0;
|
|
int failed = 0;
|
|
var failedTests = new List<(string Name, Exception Error)>();
|
|
|
|
Console.ForegroundColor = ConsoleColor.Cyan;
|
|
Console.WriteLine($"\n================ Running {_tests.Count} Unit Tests ================");
|
|
Console.ResetColor();
|
|
|
|
foreach (var (name, action) in _tests)
|
|
{
|
|
try
|
|
{
|
|
action();
|
|
Console.ForegroundColor = ConsoleColor.Green;
|
|
Console.Write(" [PASS] ");
|
|
Console.ResetColor();
|
|
Console.WriteLine(name);
|
|
passed++;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
Console.Write(" [FAIL] ");
|
|
Console.ResetColor();
|
|
Console.WriteLine(name);
|
|
failedTests.Add((name, ex));
|
|
failed++;
|
|
}
|
|
}
|
|
|
|
Console.WriteLine("\n-------------------------------------------------------------");
|
|
if (failed == 0)
|
|
{
|
|
Console.ForegroundColor = ConsoleColor.Green;
|
|
Console.WriteLine($"All tests passed! ({passed}/{_tests.Count})");
|
|
Console.ResetColor();
|
|
return 0;
|
|
}
|
|
else
|
|
{
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
Console.WriteLine($"FAILED {failed} test(s):");
|
|
foreach (var (name, error) in failedTests)
|
|
{
|
|
Console.WriteLine($" - {name}: {error.Message}");
|
|
}
|
|
Console.ResetColor();
|
|
return 1;
|
|
}
|
|
}
|
|
}
|