feat: complete GraphTUI standalone graphing terminal application in .NET 10
This commit is contained in:
commit
d70b0390b7
41 changed files with 5131 additions and 0 deletions
110
tests/GraphTUI.Tests/TestFramework.cs
Normal file
110
tests/GraphTUI.Tests/TestFramework.cs
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
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 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue