-
Notifications
You must be signed in to change notification settings - Fork 30
Added the option to ignore the order in arrays and collections #80
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
Merged
dennisdoomen
merged 7 commits into
fluentassertions:master
from
maximiliysiss:adding_without_strict_ordering
Oct 20, 2025
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5054499
add WithoutStrictOrdering for IJsonAssertionOptions<T>
maximiliysiss 8e6e591
correct api
maximiliysiss 424b09e
rename prop IsStrictlyOrdered, tests + reorder test's methods
maximiliysiss b45cc22
add paragraph about WithoutStrictOrdering in README.md
maximiliysiss ec7dd18
using TheoryData + adding JTokenComparer instead of compare like ToSt…
maximiliysiss bb9e46b
avoid code duplications
maximiliysiss 18910d5
add JConstructor invariant for comparer
maximiliysiss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| using System.Linq; | ||
| using Newtonsoft.Json.Linq; | ||
|
|
||
| namespace FluentAssertions.Json.Common | ||
| { | ||
| internal static class JTokenExtensions | ||
| { | ||
| /// <summary> | ||
| /// Recursively sorts the properties of JObject instances by name and | ||
| /// the elements of JArray instances by their string representation, | ||
| /// producing a normalized JToken for consistent comparison | ||
| /// </summary> | ||
| public static JToken Normalize(this JToken token) | ||
| { | ||
| return token switch | ||
| { | ||
| JObject obj => new JObject(obj.Properties().OrderBy(p => p.Name).Select(p => new JProperty(p.Name, Normalize(p.Value)))), | ||
| JArray array => new JArray(array.Select(Normalize).OrderBy(x => x.ToString(Newtonsoft.Json.Formatting.None))), | ||
jnyrup marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| _ => token | ||
|
Comment on lines
21
to
23
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⛏️ (nitpick) I would prefer to have this spelled out instead of a switch statement for readability |
||
| }; | ||
| } | ||
| } | ||
| } | ||
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
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
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
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
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
69 changes: 69 additions & 0 deletions
69
Tests/FluentAssertions.Json.Specs/WithoutStrictOrderingSpecs.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using Newtonsoft.Json.Linq; | ||
| using Xunit; | ||
| using Xunit.Sdk; | ||
|
|
||
| namespace FluentAssertions.Json.Specs | ||
| { | ||
| public class WithoutStrictOrderingSpecs | ||
| { | ||
| [Theory] | ||
| [MemberData(nameof(When_ignoring_ordering_BeEquivalentTo_should_succeed_sample_data))] | ||
| public void When_ignoring_ordering_BeEquivalentTo_should_succeed(string subject, string expectation) | ||
| { | ||
| // Arrange | ||
| var subjectJToken = JToken.Parse(subject); | ||
| var expectationJToken = JToken.Parse(expectation); | ||
|
|
||
| // Act | ||
| subjectJToken.Should().BeEquivalentTo(expectationJToken, opt => opt.WithoutStrictOrdering()); | ||
|
|
||
| // Assert | ||
| } | ||
|
|
||
| public static IEnumerable<object[]> When_ignoring_ordering_BeEquivalentTo_should_succeed_sample_data() | ||
jnyrup marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| yield return new object[] { @"{""ids"":[1,2,3]}", @"{""ids"":[3,2,1]}" }; | ||
| yield return new object[] { @"{""ids"":[1,2,3]}", @"{""ids"":[1,2,3]}" }; | ||
| yield return new object[] { @"{""type"":2,""name"":""b""}", @"{""name"":""b"",""type"":2}" }; | ||
| yield return new object[] { @"{""names"":[""a"",""b""]}", @"{""names"":[""b"",""a""]}" }; | ||
| yield return new object[] | ||
| { | ||
| @"{""vals"":[{""type"":1,""name"":""a""},{""name"":""b"",""type"":2}]}", | ||
| @"{""vals"":[{""type"":2,""name"":""b""},{""name"":""a"",""type"":1}]}" | ||
| }; | ||
| yield return new object[] | ||
| { | ||
| @"{""vals"":[{""type"":1,""name"":""a""},{""name"":""b"",""type"":2}]}", | ||
| @"{""vals"":[{""name"":""a"",""type"":1},{""type"":2,""name"":""b""}]}" | ||
| }; | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(When_not_ignoring_ordering_BeEquivalentTo_should_throw_sample_data))] | ||
| public void When_not_ignoring_ordering_BeEquivalentTo_should_throw(string subject, string expectation) | ||
| { | ||
| // Arrange | ||
| var subjectJToken = JToken.Parse(subject); | ||
| var expectationJToken = JToken.Parse(expectation); | ||
|
|
||
| // Act | ||
| var action = new Func<AndConstraint<JTokenAssertions>>(() => subjectJToken.Should().BeEquivalentTo(expectationJToken)); | ||
|
|
||
| // Assert | ||
| action.Should().Throw<XunitException>(); | ||
| } | ||
|
|
||
| public static IEnumerable<object[]> When_not_ignoring_ordering_BeEquivalentTo_should_throw_sample_data() | ||
| { | ||
| yield return new object[] { @"{""ids"":[1,2,3]}", @"{""ids"":[3,2,1]}" }; | ||
| yield return new object[] { @"{""names"":[""a"",""b""]}", @"{""names"":[""b"",""a""]}" }; | ||
| yield return new object[] | ||
| { | ||
| @"{""vals"":[{""type"":1,""name"":""a""},{""name"":""b"",""type"":2}]}", | ||
| @"{""vals"":[{""type"":2,""name"":""b""},{""name"":""a"",""type"":1}]}" | ||
| }; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.