Add tests for generic exception formatting

This commit is contained in:
Cédric Luthi 2025-02-05 18:20:12 +01:00
parent a6b96e9297
commit 7e1142df58
4 changed files with 30 additions and 0 deletions

View File

@ -6,6 +6,8 @@ public static class TestExceptions
public static bool GenericMethodThatThrows<T0, T1, TRet>(int? number) => throw new InvalidOperationException("Throwing!");
public static bool MethodThatThrowsGenericException<T>() => throw new GenericException<T>("Throwing!", default);
public static void ThrowWithInnerException()
{
try
@ -42,3 +44,6 @@ public static class TestExceptions
return ("key", new List<T>());
}
}
#pragma warning disable CS9113 // Parameter is unread.
public class GenericException<T>(string message, T value) : Exception(message);

View File

@ -0,0 +1,4 @@
Spectre.Console.Tests.Data.GenericException<Spectre.Console.IAnsiConsole>: Throwing!
at bool Spectre.Console.Tests.Data.TestExceptions.MethodThatThrowsGenericException<T>() in {ProjectDirectory}Data/Exceptions.cs:9
at void Spectre.Console.Tests.Unit.ExceptionTests.<>c.<Should_Write_GenericException>b__8_0() in {ProjectDirectory}Unit/ExceptionTests.cs:134
at Exception Spectre.Console.Tests.Unit.ExceptionTests.GetException(Action action) in {ProjectDirectory}Unit/ExceptionTests.cs:147

View File

@ -0,0 +1,4 @@
GenericException<IAnsiConsole>: Throwing!
at bool Spectre.Console.Tests.Data.TestExceptions.MethodThatThrowsGenericException<T>() in {ProjectDirectory}Data/Exceptions.cs:9
at void Spectre.Console.Tests.Unit.ExceptionTests.<>c.<Should_Write_GenericException>b__8_0() in {ProjectDirectory}Unit/ExceptionTests.cs:134
at Exception Spectre.Console.Tests.Unit.ExceptionTests.GetException(Action action) in {ProjectDirectory}Unit/ExceptionTests.cs:147

View File

@ -123,6 +123,23 @@ public sealed class ExceptionTests
return Verifier.Verify(result);
}
[Theory]
[InlineData(ExceptionFormats.Default)]
[InlineData(ExceptionFormats.ShortenTypes)]
[Expectation("GenericException")]
public Task Should_Write_GenericException(ExceptionFormats exceptionFormats)
{
// Given
var console = new TestConsole { EmitAnsiSequences = true }.Width(1024);
var dex = GetException(() => TestExceptions.MethodThatThrowsGenericException<IAnsiConsole>());
// When
var result = console.WriteNormalizedException(dex, exceptionFormats);
// Then
return Verifier.Verify(result).UseParameters(exceptionFormats);
}
public static Exception GetException(Action action)
{
try