-
Notifications
You must be signed in to change notification settings - Fork 731
Description
Description
When using a record struct in the generic TKey of a Dictionary, when an error of fluent assertion is generated, the .ToString() method is called which generates the following output:
MyStruct{ MyProperty1 = "MyProperty1", MyProperty2 = "MyProeprty2", ... }
One can see that using a ToString() on a record generates the characters { and } in the returned string.
The returned string is then used in turn in the string.Format() method but will crash because it considers { and } as parameter injection.
I mean, a message like this : MyStruct{ MyProperty1 = "MyProperty1", MyProperty2 = "MyProeprty2", ... } is considered as MyStruct{ paramForFormatMethod }.
So fluent assertion tries to inject a param in the generated string from the record struct.
(the example will help clarify)
Reproduction Steps
public readonly record struct MyRecordStruct
{
public string MyProperty { get; init; }
}
[Fact]
public void myTest()
{
var dict = new Dictionary<MyRecordStruct, MyRecordStruct>();
var dict2 = new Dictionary<MyRecordStruct, MyRecordStruct>();
var key = new MyRecordStruct() { MyProperty = "MyKey" };
dict.Add(key, new MyRecordStruct());
dict2.Add(key, new MyRecordStruct() { MyProperty = ""});
dict2.Should().BeEquivalentTo(dict);
}Expected behavior
Expected property dict2[MyRecordStruct { MyProperty = MyKey }].MyProperty to be null, but found "".
Actual behavior
It generates the following error message :
**WARNING** failure message 'Expected property dict2[MyRecordStruct { MyProperty = MyKey }].MyProperty to be {0}, but found {1}.' could not be formatted with string.Format
at System.Text.ValueStringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ReadOnlySpan`1 args)
at System.String.FormatHelper(IFormatProvider provider, String format, ReadOnlySpan`1 args)
at System.String.Format(IFormatProvider provider, String format, Object[] args)
at FluentAssertions.Execution.MessageBuilder.FormatArgumentPlaceholders(String failureMessage, Object[] failureArgs)
Regression?
No response
Known Workarounds
No response
Configuration
- FluentAssertions Version="6.12.0"
- .NET8