{{ message }}
Repository navigation
Fix formatting exception when comparing strings containing braces - #3151
Merged
Merged
Conversation
Co-authored-by: dennisdoomen <572734+dennisdoomen@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix System.FormatException when value contains "{"
Fix FormatException when comparing strings containing braces
Jan 21, 2026
Pull Request Test Coverage Report for Build 21300676482Details
💛 - Coveralls |
Qodana for .NETIt seems all right 👌 No new problems were found according to the checks applied 💡 Qodana analysis was run in the pull request mode: only the changed files were checked Contact Qodana teamContact us at qodana-support@jetbrains.com
|
dennisdoomen
marked this pull request as ready for review
January 23, 2026 20:45
Co-authored-by: dennisdoomen <572734+dennisdoomen@users.noreply.github.com>
dennisdoomen
force-pushed
the
copilot/fix-system-format-exception
branch
from
January 23, 2026 20:47
233a12c to
e773f85
Compare
dennisdoomen
approved these changes
Jan 23, 2026
jnyrup
reviewed
Jan 25, 2026
Member
There was a problem hiding this comment.
If we polyfill StringBuilder.Append in StringBuilderExtensions we can avoid the intermediate string.
sb.Append(CultureInfo.InvariantCulture,
$"""
Full expectation:
{expected.RenderAsIndentedBlock()},
Full subject:
{subject.RenderAsIndentedBlock()}
""");For StringBuilderExtensions
public static StringBuilder AppendLine(this StringBuilder stringBuilder, IFormatProvider provider, FormattableString formattable) =>
stringBuilder.AppendLine(string.Create(provider, formattable));
public static StringBuilder Append(this StringBuilder stringBuilder, IFormatProvider provider, FormattableString formattable) =>
stringBuilder.Append(string.Create(provider, formattable));
Member
There was a problem hiding this comment.
Is that a suggestion? And why is that important? This code is only executed for a failing test and when the full details are requested.
Member
There was a problem hiding this comment.
It's a suggestion and not blocking for approving the PR.
jnyrup
approved these changes
Jan 25, 2026
jnyrup
pushed a commit
to jnyrup/fluentassertions
that referenced
this pull request
Feb 28, 2026
jnyrup
pushed a commit
to jnyrup/fluentassertions
that referenced
this pull request
Mar 7, 2026
jnyrup
pushed a commit
that referenced
this pull request
Mar 8, 2026
This was referenced Mar 16, 2026
This was referenced Oct 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Be()assertion throwsFormatExceptioninstead of test failure when comparing strings containing{or}characters.Root Cause
StringEqualityStrategy.GetMismatchSegmentForLongStringsusedStringBuilder.AppendFormatwith an interpolated string literal containing user values:This causes double-interpretation: string interpolation inserts user values first, then
AppendFormatinterprets the result as a format string. Braces in user strings are mistaken for format placeholders.Changes
string.Create()to build the interpolated string, thenAppend()the result.AsNonFormattable()calls since we no longer useAppendFormatThis ensures user strings are never reinterpreted as format strings.
Original prompt
This section details on the original issue you should resolve
<issue_title>Be() throws System.FormatException when expected value contains "{"</issue_title>
<issue_description>### Description
Hi,
I was writing tests for my code formatter and came across this weird behavior - FluentAssertions use
string.Formatwithout sanitizing the values (but that is not optimal solution IMHO).It is kind of annoying:
This works just fine:
This throws throws System.FormatException.
Thanks
Reproduction Steps
P.S.: The fact that it fails around 79-80 is funny.
Expected behavior
Be should throw only XunitException for string comparisons - strings are either equal or not equal.
IMHO, building final assertion message incrementally using multiple string.Format calls feels too hacky.
Probably, a better approach would be to create some tree-ish type and walking over it to build the final string. But I'm not regularly programming in C# so don't listen to me. :)
Actual behavior
In reproduction tests:
NotOktest fails - the actual string is "{" and then 80+ "x"s and it is compared to an empty string.Oktest succeeds but it has string.Format error in the assertion message - the actual string is "{" and 79 "x"s.Full dotnet test result: