Skip to content

Commit

Permalink
Merge pull request #4663 from nunit/Issue4659_Naming
Browse files Browse the repository at this point in the history
Add support for Exceptions for Display Name
  • Loading branch information
mikkelbu committed Mar 16, 2024
2 parents f4f266b + 5ddbbc9 commit 7fedfa5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/NUnitFramework/framework/Internal/DisplayName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,33 @@ public static string GetValueString(object? arg, int stringMax)
else if (sb.Equals(sbyte.MinValue))
display = "sbyte.MinValue";
}
else if (arg is Exception e)
{
display = FormatException(e);
}

return display;
}

private static string FormatException(Exception ex)
{
var builder = new StringBuilder();

for (Exception? e = ex; e is not null; e = e.InnerException)
{
builder.Append(e.GetType().Name);
builder.Append(": ");
builder.Append(e.Message);

if (e.InnerException is not null)
{
builder.Append(", ");
}
}

return builder.ToString();
}

/// <summary>
/// Checks if string contains any character that might need escaping.
/// </summary>
Expand Down
19 changes: 19 additions & 0 deletions src/NUnitFramework/tests/Issue4659.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System;

namespace NUnit.Framework.Tests
{
[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
internal sealed class Issue4659
{
[TestCaseSource(nameof(GetSomeExceptionCases))]
public void NoMethod_GivenException_ShouldPassTest(Exception testCase) => Assert.That(testCase, Is.Not.InstanceOf<string>());

private static TestCaseData[] GetSomeExceptionCases() => new[]
{
new TestCaseData(new ArgumentNullException()),
new TestCaseData(new ArgumentNullException("message", new Exception("Some Exception Message"))),
};
}
}

0 comments on commit 7fedfa5

Please sign in to comment.