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

Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert("{","x") throws System.FormatException #25373

Closed
nonZero opened this issue May 11, 2022 · 5 comments · Fixed by microsoft/testfx#1124 or microsoft/testfx#1126

Comments

@nonZero
Copy link

nonZero commented May 11, 2022

Describe the bug

A clear and concise description of what the bug is.
Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert("{","x") throws System.FormatException instead of AssertFailedException.

To Reproduce

StringAssert.Contains(":-{","x");

See: https://github.com/nonZero/StringAssert_Demo

Exceptions (if any)

Test method threw exception System.FormatException, but exception Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException was expected. Exc...

Test method threw exception System.FormatException, but exception Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException was expected. Exception message: System.FormatException: Input string was not in a correct format.
   at System.Text.ValueStringBuilder.ThrowFormatError()
   at System.Text.ValueStringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args)
   at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args)
   at System.String.Format(IFormatProvider provider, String format, Object[] args)
   at StringAssert_Demo.UnitTest1.Test_StringAssert_DoesNotContain_WithCurly() in .../StringAssert_Demo/StringAssertUnitTest.cs:line 26

Further technical details

.NET SDK (reflecting any global.json):
 Version:   6.0.102
 Commit:    49861cb924

Runtime Environment:
 OS Name:     arch
 OS Version:  
 OS Platform: Linux
 RID:         arch-x64
 Base Path:   /usr/share/dotnet/sdk/6.0.102/

Host (useful for support):
  Version: 6.0.2
  Commit:  839cdfb0ec

.NET SDKs installed:
  6.0.102 [/usr/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 5.0.16 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 5.0.16 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.2 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-dotnet test untriaged Request triage from a team member labels May 11, 2022
@nonZero
Copy link
Author

nonZero commented May 11, 2022

Original stack overflow question: https://stackoverflow.com/questions/72179163/

@nohwnd
Copy link
Member

nohwnd commented May 13, 2022

@Haplois Please have a look, this seems like formatting issue where we don't escape the groups in provided string.

@KalleOlaviNiemitalo
Copy link
Contributor

Bug in https://github.com/microsoft/testfx/blob/23793c5d6e7ebcd08d455674927bbf3031697e74/src/TestFramework/MSTest.Core/Assertions/StringAssert.cs#L201-L210, and similar bugs in other methods that call Assert.HandleFail. I didn't find a bug already filed for this in the testfx repo.

@KalleOlaviNiemitalo
Copy link
Contributor

That is an incomplete fix; the parameters array might not be empty. Consider StringAssert.Contains("{", "x", "message {0}", "param");.

@Evangelink
Copy link
Member

Nice catch @KalleOlaviNiemitalo! I will do a follow-up fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants