Files
Chronos/tests/Titano.Testing/ConsoleReporter.cs
T
Claude 83a54fb57b chore: bootstrap repository foundation
Solution layout, build rules with zero-package guard, in-house test
framework, localization runtime, brand mark rasteriser with ICO/PNG
generation, Core contracts (imaging, video, domain model, TIFF reader),
Titano.Tools (make-icon, i18n, xaml-lint, package-guard) and docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 08:59:40 +00:00

65 lines
2.6 KiB
C#

using System.Globalization;
namespace Titano.Testing;
internal sealed class ConsoleReporter(bool verbose) : IDisposable
{
private readonly Lock _gate = new();
public void Report(TestResult r)
{
lock (_gate)
{
switch (r.Outcome)
{
case TestOutcome.Failed:
Write(ConsoleColor.Red, "FAIL ");
Console.WriteLine($"{r.Test.FullName} ({Ms(r.Duration)})");
Console.WriteLine(" " + r.Message);
if (r.StackTrace is not null) Console.WriteLine(Indent(r.StackTrace));
if (r.Log.Length > 0) Console.WriteLine(Indent(r.Log));
break;
case TestOutcome.Skipped when verbose:
Write(ConsoleColor.Yellow, "SKIP ");
Console.WriteLine($"{r.Test.FullName} — {r.Message}");
break;
case TestOutcome.Passed when verbose:
Write(ConsoleColor.Green, "PASS ");
Console.WriteLine($"{r.Test.FullName} ({Ms(r.Duration)})");
if (r.Log.Length > 0) Console.WriteLine(Indent(r.Log));
break;
}
}
}
public void Summary(string suite, int passed, int failed, int skipped, TimeSpan elapsed, IReadOnlyList<TestResult> results)
{
lock (_gate)
{
Console.WriteLine();
var slowest = results.Where(r => r.Outcome == TestOutcome.Passed).OrderByDescending(r => r.Duration).Take(3).ToList();
if (verbose && slowest.Count > 0)
{
Console.WriteLine("Slowest:");
foreach (var r in slowest) Console.WriteLine($" {Ms(r.Duration),9} {r.Test.FullName}");
}
Write(failed > 0 ? ConsoleColor.Red : ConsoleColor.Green, failed > 0 ? "FAILED" : "PASSED");
Console.WriteLine($" {suite}: {passed} passed, {failed} failed, {skipped} skipped in {elapsed.TotalSeconds.ToString("F1", CultureInfo.InvariantCulture)} s");
}
}
public void Dispose() => Console.Out.Flush();
private static string Ms(TimeSpan t) => t.TotalMilliseconds.ToString("F0", CultureInfo.InvariantCulture) + " ms";
private static string Indent(string s) => string.Join(Environment.NewLine, s.TrimEnd().Split('\n').Select(l => " " + l.TrimEnd('\r')));
private static void Write(ConsoleColor color, string text)
{
var redirected = Console.IsOutputRedirected;
if (!redirected) Console.ForegroundColor = color;
Console.Write(text);
if (!redirected) Console.ResetColor();
}
}