-
-
Notifications
You must be signed in to change notification settings - Fork 544
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
BeEquivalentTo incorrectly compare JsonElements? #2374
Comments
My snippet in #1212 only compares the Here's some code I hacked together in five minutes. [Fact]
public void Test()
{
var foo = JsonDocument.Parse("""{"test":"something"}""").RootElement;
var bar = JsonDocument.Parse("""{"test":"something else"}""").RootElement;
foo.Should().BeEquivalentTo(bar, opt => opt.Using(new JsonObjectEquivalencyStep()));
}
public class JsonObjectEquivalencyStep : IEquivalencyStep
{
public EquivalencyResult Handle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
{
if (comparands.Subject is not JsonElement subject || comparands.Expectation is not JsonElement expectation)
{
return EquivalencyResult.ContinueWithNext;
}
if (subject.ValueKind is JsonValueKind.Object && expectation.ValueKind is JsonValueKind.Object)
{
var newComparands = new Comparands(subject.EnumerateObject(), expectation.EnumerateObject(), typeof(IEnumerable<JsonProperty>));
nestedValidator.RecursivelyAssertEquality(newComparands, context);
return EquivalencyResult.AssertionCompleted;
}
if (subject.ValueKind is JsonValueKind.Array && expectation.ValueKind is JsonValueKind.Array)
{
var newComparands = new Comparands(subject.EnumerateArray(), expectation.EnumerateArray(), typeof(IEnumerable<JsonElement>));
nestedValidator.RecursivelyAssertEquality(newComparands, context);
return EquivalencyResult.AssertionCompleted;
}
return EquivalencyResult.ContinueWithNext;
}
} Also the failure message is also not as pretty as it could be, as it uses some internal index over the json property name
|
This should also be covered by #2205 |
Sorry for late response and thanks for your replies @jnyrup and @dennisdoomen. Wanted to make sure if I was missing something obvious to make it work out of the box. But then I know it would require some custom comparison implementation to get it working. Cheers! |
@jnyrup ? |
Description
I am trying to compare if two json strings are equivalent (don't care about whitespaces or order). Using suggestion from previous issue: #1212 yields incorrect results.
Reproduction Steps
Expected behavior
Would expect this to fail, "something" != "something else".
Actual behavior
Test passes.
Regression?
No response
Known Workarounds
No response
Configuration
.NET 7.0
xunit 2.5.0
FluentAssertions 6.12.0
Other information
No response
Are you willing to help with a pull-request?
No
The text was updated successfully, but these errors were encountered: