Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent format exceptions when parameters array is empty #1124

Merged
merged 1 commit into from
Jun 9, 2022
Merged
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
4 changes: 2 additions & 2 deletions src/TestFramework/MSTest.Core/Assertions/Assert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2374,7 +2374,7 @@ public static void Inconclusive(string message, params object[] parameters)
string finalMessage = string.Empty;
if (!string.IsNullOrEmpty(message))
{
if (parameters == null)
if (parameters == null || parameters.Length == 0)
{
finalMessage = ReplaceNulls(message);
}
Expand Down Expand Up @@ -2763,7 +2763,7 @@ internal static void HandleFail(string assertionName, string message, params obj
string finalMessage = string.Empty;
if (!string.IsNullOrEmpty(message))
{
if (parameters == null)
if (parameters == null || parameters.Length == 0)
{
finalMessage = ReplaceNulls(message);
}
Expand Down
24 changes: 24 additions & 0 deletions test/UnitTests/MSTest.Core.Unit.Tests/Assertions/AssertTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -608,5 +608,29 @@ public void AreEqualShouldFailWhenNotEqualFloatWithDelta()
}

#endregion

#region HandleFail tests
[TestMethod] // See https://github.com/dotnet/sdk/issues/25373
public void HandleFailDoesNotFailWithFormatExceptionOnEmptyParameters()
{
var ex = ActionUtility.PerformActionAndReturnException(() => TestFrameworkV2.Assert.HandleFail("name", "{"));

Assert.IsNotNull(ex);
Assert.AreEqual(typeof(TestFrameworkV2.AssertFailedException), ex.GetType());
StringAssert.Contains(ex.Message, "name failed. {");
}
#endregion

#region Inconclusive tests
[TestMethod] // See https://github.com/dotnet/sdk/issues/25373
public void InconclusiveDoesNotFailWithFormatExceptionOnEmptyParameters()
{
var ex = ActionUtility.PerformActionAndReturnException(() => TestFrameworkV2.Assert.Inconclusive("{"));

Assert.IsNotNull(ex);
Assert.AreEqual(typeof(TestFrameworkV2.AssertInconclusiveException), ex.GetType());
StringAssert.Contains(ex.Message, "Assert.Inconclusive failed. {");
}
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,13 @@ public void StringAssertEndsWithIgnoreCase()
var ex = ActionUtility.PerformActionAndReturnException(() => TestFrameworkV2.StringAssert.EndsWith(actual, inString, StringComparison.OrdinalIgnoreCase));
Assert.IsNull(ex);
}

[TestMethod] // See https://github.com/dotnet/sdk/issues/25373
public void StringAssertContainsDoesNotThrowFormatException()
{
var ex = ActionUtility.PerformActionAndReturnException(() => TestFrameworkV2.StringAssert.Contains(":-{", "x"));
Assert.IsNotNull(ex);
TestFrameworkV1.StringAssert.Contains(ex.Message, "StringAssert.Contains failed");
}
}
}