Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Microsoft.Testing.Platform.OutputDevice.Terminal;

internal sealed class ExceptionFlattener
{
internal static FlatException[] Flatten(string? errorMessage, Exception? exception)
internal static (string? ErrorMessage, string? ErrorType, string? StackTrace)[] Flatten(string? errorMessage, Exception? exception)
{
if (errorMessage is null && exception is null)
{
Expand All @@ -15,9 +15,9 @@ internal static FlatException[] Flatten(string? errorMessage, Exception? excepti
string? message = !RoslynString.IsNullOrWhiteSpace(errorMessage) ? errorMessage : exception?.Message;
string? type = exception?.GetType().FullName;
string? stackTrace = exception?.StackTrace;
var flatException = new FlatException(message, type, stackTrace);
(string? ErrorMessage, string? ErrorType, string? StackTrace) flatException = (message, type, stackTrace);

List<FlatException> flatExceptions = [flatException];
List<(string? ErrorMessage, string? ErrorType, string? StackTrace)> flatExceptions = [flatException];

// Add all inner exceptions. This will flatten top level AggregateExceptions,
// and all AggregateExceptions that are directly in AggregateExceptions, but won't expand
Expand All @@ -33,7 +33,7 @@ internal static FlatException[] Flatten(string? errorMessage, Exception? excepti
Exception? currentException = aggregate;
while (currentException is not null)
{
flatExceptions.Add(new FlatException(
flatExceptions.Add((
currentException.Message,
currentException.GetType().FullName,
currentException.StackTrace));
Expand All @@ -45,5 +45,3 @@ internal static FlatException[] Flatten(string? errorMessage, Exception? excepti
return [.. flatExceptions];
}
}

internal sealed record FlatException(string? ErrorMessage, string? ErrorType, string? StackTrace);
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.Testing.Platform.OutputDevice.Terminal;
[UnsupportedOSPlatform("browser")]
internal sealed partial class TerminalTestReporter
{
private static void FormatInnerExceptions(ITerminal terminal, FlatException[] exceptions)
private static void FormatInnerExceptions(ITerminal terminal, (string? ErrorMessage, string? ErrorType, string? StackTrace)[] exceptions)
{
if (exceptions.Length == 0)
{
Expand All @@ -26,7 +26,7 @@ private static void FormatInnerExceptions(ITerminal terminal, FlatException[] ex
}
}

private static void FormatErrorMessage(ITerminal terminal, FlatException[] exceptions, TestOutcome outcome, int index)
private static void FormatErrorMessage(ITerminal terminal, (string? ErrorMessage, string? ErrorType, string? StackTrace)[] exceptions, TestOutcome outcome, int index)
{
string? firstErrorMessage = GetStringFromIndexOrDefault(exceptions, e => e.ErrorMessage, index);
string? firstErrorType = GetStringFromIndexOrDefault(exceptions, e => e.ErrorType, index);
Expand Down Expand Up @@ -55,7 +55,7 @@ private static void FormatErrorMessage(ITerminal terminal, FlatException[] excep
terminal.ResetColor();
}

private static string? GetStringFromIndexOrDefault(FlatException[] exceptions, Func<FlatException, string?> property, int index) =>
private static string? GetStringFromIndexOrDefault((string? ErrorMessage, string? ErrorType, string? StackTrace)[] exceptions, Func<(string? ErrorMessage, string? ErrorType, string? StackTrace), string?> property, int index) =>
exceptions.Length >= index + 1 ? property(exceptions[index]) : null;

private static void FormatExpectedAndActual(ITerminal terminal, string? expected, string? actual)
Expand All @@ -75,7 +75,7 @@ private static void FormatExpectedAndActual(ITerminal terminal, string? expected
terminal.ResetColor();
}

private static void FormatStackTrace(ITerminal terminal, FlatException[] exceptions, int index)
private static void FormatStackTrace(ITerminal terminal, (string? ErrorMessage, string? ErrorType, string? StackTrace)[] exceptions, int index)
{
string? stackTrace = GetStringFromIndexOrDefault(exceptions, e => e.StackTrace, index);
if (RoslynString.IsNullOrWhiteSpace(stackTrace))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal void TestCompleted(
string? standardOutput,
string? errorOutput)
{
FlatException[] flatExceptions = ExceptionFlattener.Flatten(errorMessage, exception);
(string? ErrorMessage, string? ErrorType, string? StackTrace)[] flatExceptions = ExceptionFlattener.Flatten(errorMessage, exception);
TestCompleted(
testNodeUid,
displayName,
Expand All @@ -42,7 +42,7 @@ private void TestCompleted(
TestOutcome outcome,
TimeSpan? duration,
string? informativeMessage,
FlatException[] exceptions,
(string? ErrorMessage, string? ErrorType, string? StackTrace)[] exceptions,
string? expected,
string? actual,
string? standardOutput,
Expand Down Expand Up @@ -108,7 +108,7 @@ private void RenderTestCompleted(
TestOutcome outcome,
TimeSpan? duration,
string? informativeMessage,
FlatException[] flatExceptions,
(string? ErrorMessage, string? ErrorType, string? StackTrace)[] flatExceptions,
string? expected,
string? actual,
string? standardOutput,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public async Task ConsumeAsync(IDataProducer dataProducer, IData value, Cancella
return null;
}

FlatException[] exceptions = ExceptionFlattener.Flatten(errorMessage, exception);
(string? ErrorMessage, string? ErrorType, string? StackTrace)[] exceptions = ExceptionFlattener.Flatten(errorMessage, exception);

var exceptionMessages = new ExceptionMessage[exceptions.Length];
for (int i = 0; i < exceptions.Length; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void ExceptionFlattener_WhenNestedInnerExceptions_ShouldKeepAllMessagesIn
{
var exception = new Exception("outer", new InvalidOperationException("inner-1", new ArgumentException("inner-2")));

FlatException[] flattenedExceptions = ExceptionFlattener.Flatten(null, exception);
(string? ErrorMessage, string? ErrorType, string? StackTrace)[] flattenedExceptions = ExceptionFlattener.Flatten(null, exception);

Assert.HasCount(3, flattenedExceptions);
Assert.AreEqual("outer", flattenedExceptions[0].ErrorMessage);
Expand All @@ -32,7 +32,7 @@ public void ExceptionFlattener_WhenAggregateException_ShouldKeepTopLevelThenFlat
new InvalidOperationException("inner-1"),
new ArgumentException("inner-2"));

FlatException[] flattenedExceptions = ExceptionFlattener.Flatten(null, exception);
(string? ErrorMessage, string? ErrorType, string? StackTrace)[] flattenedExceptions = ExceptionFlattener.Flatten(null, exception);

Assert.HasCount(3, flattenedExceptions);
Assert.AreEqual(typeof(AggregateException).FullName, flattenedExceptions[0].ErrorType);
Expand Down
Loading