-
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 all 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,93 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using Newtonsoft.Json.Linq; | ||
|
|
||
| namespace FluentAssertions.Json.Common | ||
| { | ||
| internal static class JTokenExtensions | ||
| { | ||
| private static readonly JTokenComparer Comparer = new(); | ||
|
|
||
| /// <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, Comparer)), | ||
| _ => token | ||
| }; | ||
| } | ||
|
|
||
| private sealed class JTokenComparer : IComparer<JToken> | ||
| { | ||
| public int Compare(JToken x, JToken y) | ||
| { | ||
| if (ReferenceEquals(x, y)) | ||
| return 0; | ||
|
|
||
| if (x is null) | ||
| return -1; | ||
|
|
||
| if (y is null) | ||
| return 1; | ||
|
|
||
| var typeComparison = x.Type.CompareTo(y.Type); | ||
| if (typeComparison != 0) | ||
| return typeComparison; | ||
|
|
||
| return x switch | ||
| { | ||
| JArray a => Compare(a, (JArray)y), | ||
| JObject o => Compare(o, (JObject)y), | ||
| JProperty p => Compare(p, (JProperty)y), | ||
| JValue v => Compare(v, (JValue)y), | ||
| JConstructor c => Compare(c, (JConstructor)y), | ||
| _ => string.Compare(x.ToString(), y.ToString(), StringComparison.Ordinal) | ||
| }; | ||
| } | ||
|
|
||
| private static int Compare(JValue x, JValue y) => Comparer<object>.Default.Compare(x.Value, y.Value); | ||
|
|
||
| private static int Compare(JConstructor x, JConstructor y) | ||
| { | ||
| var nameComparison = string.Compare(x.Name, y.Name, StringComparison.Ordinal); | ||
| return nameComparison != 0 ? nameComparison : Compare(x, (JContainer)y); | ||
| } | ||
|
|
||
| private static int Compare(JContainer x, JContainer y) | ||
| { | ||
| var countComparison = x.Count.CompareTo(y.Count); | ||
| if (countComparison != 0) | ||
| return countComparison; | ||
|
|
||
| return x | ||
| .Select((t, i) => Comparer.Compare(t, y[i])) | ||
| .FirstOrDefault(itemComparison => itemComparison != 0); | ||
| } | ||
|
|
||
| private static int Compare(JObject x, JObject y) | ||
| { | ||
| var countComparison = x.Count.CompareTo(y.Count); | ||
| if (countComparison != 0) | ||
| return countComparison; | ||
|
|
||
| return x.Properties() | ||
| .OrderBy(p => p.Name) | ||
| .Zip(y.Properties().OrderBy(p => p.Name), (px, py) => Compare(px, py)) | ||
| .FirstOrDefault(itemComparison => itemComparison != 0); | ||
| } | ||
|
|
||
| private static int Compare(JProperty x, JProperty y) | ||
| { | ||
| var nameComparison = string.Compare(x.Name, y.Name, StringComparison.Ordinal); | ||
| return nameComparison != 0 ? nameComparison : Comparer.Compare(x.Value, y.Value); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
158 changes: 158 additions & 0 deletions
158
Tests/FluentAssertions.Json.Specs/JTokenComparerSpecs.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,158 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using Newtonsoft.Json.Linq; | ||
| using Xunit; | ||
|
|
||
| namespace FluentAssertions.Json.Specs | ||
| { | ||
| public class JTokenComparerSpecs | ||
| { | ||
| private static readonly IComparer<JToken> Comparer = | ||
| Type.GetType("FluentAssertions.Json.Common.JTokenExtensions, FluentAssertions.Json")! | ||
| .GetField("Comparer", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic)! | ||
| .GetValue(null) as IComparer<JToken>; | ||
|
|
||
| [Fact] | ||
| public void Should_return_zero_for_same_reference() | ||
| { | ||
| // Arrange | ||
| var token = JToken.Parse(@"{""a"":1}"); | ||
|
|
||
| // Act & Assert | ||
| Comparer.Compare(token, token).Should().Be(0); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Should_handle_nulls() | ||
| { | ||
| // Arrange | ||
| var token = JToken.Parse("1"); | ||
|
|
||
| // Act & Assert | ||
| Comparer.Compare(null, token).Should().Be(-1); | ||
| Comparer.Compare(token, null).Should().Be(1); | ||
| Comparer.Compare(null, null).Should().Be(0); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Should_compare_different_types() | ||
| { | ||
| // Arrange | ||
| var obj = JToken.Parse(@"{""a"":1}"); | ||
| var arr = JToken.Parse("[1]"); | ||
|
|
||
| // Act & Assert | ||
| Comparer.Compare(obj, arr).Should().NotBe(0); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Should_compare_jvalues() | ||
| { | ||
| // Arrange | ||
| var v1 = new JValue(1); | ||
| var v2 = new JValue(2); | ||
|
|
||
| // Act & Assert | ||
| Comparer.Compare(v1, v2).Should().Be(-1); | ||
| Comparer.Compare(v2, v1).Should().Be(1); | ||
| Comparer.Compare(v1, new JValue(1)).Should().Be(0); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Should_compare_jarrays_by_count_and_elements() | ||
| { | ||
| // Arrange | ||
| var arr1 = JArray.Parse("[1,2]"); | ||
| var arr2 = JArray.Parse("[1,2,3]"); | ||
| var arr3 = JArray.Parse("[1,3]"); | ||
| var arr4 = JArray.Parse("[1,2,3]"); | ||
|
|
||
| // Act & Assert | ||
| Comparer.Compare(arr1, arr2).Should().Be(-1); | ||
| Comparer.Compare(arr1, arr3).Should().Be(-1); | ||
| Comparer.Compare(arr3, arr1).Should().Be(1); | ||
| Comparer.Compare(arr2, arr4).Should().Be(0); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Should_compare_jobjects_by_count_and_properties() | ||
| { | ||
| // Arrange | ||
| var obj1 = JObject.Parse(@"{""a"":1}"); | ||
| var obj2 = JObject.Parse(@"{""a"":1,""b"":2}"); | ||
| var obj3 = JObject.Parse(@"{""a"":2}"); | ||
| var obj4 = JObject.Parse(@"{""a"":1,""b"":2}"); | ||
| var obj5 = JObject.Parse(@"{""b"":2}"); | ||
|
|
||
| // Act & Assert | ||
| Comparer.Compare(obj1, obj2).Should().Be(-1); | ||
| Comparer.Compare(obj1, obj3).Should().Be(-1); | ||
| Comparer.Compare(obj3, obj1).Should().Be(1); | ||
| Comparer.Compare(obj2, obj4).Should().Be(0); | ||
| Comparer.Compare(obj1, obj5).Should().Be(-1); | ||
| Comparer.Compare(obj5, obj1).Should().Be(1); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Should_compare_jproperties_by_name_and_value() | ||
| { | ||
| // Arrange | ||
| var prop1 = new JProperty("a", 1); | ||
| var prop2 = new JProperty("b", 1); | ||
| var prop3 = new JProperty("a", 2); | ||
| var prop4 = new JProperty("a", 1); | ||
|
|
||
| // Act & Assert | ||
| Comparer.Compare(prop1, prop2).Should().Be(-1); | ||
| Comparer.Compare(prop1, prop3).Should().Be(-1); | ||
| Comparer.Compare(prop3, prop1).Should().Be(1); | ||
| Comparer.Compare(prop4, prop1).Should().Be(0); | ||
| Comparer.Compare(prop2, prop3).Should().Be(1); | ||
| Comparer.Compare(prop3, prop2).Should().Be(-1); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Should_compare_jconstructors_by_name() | ||
| { | ||
| // Arrange | ||
| var ctor1 = new JConstructor("foo", new JValue(1)); | ||
| var ctor2 = new JConstructor("bar", new JValue(1)); | ||
|
|
||
| // Act & Assert | ||
| Comparer.Compare(ctor1, ctor2).Should().BeGreaterThan(0); // "foo" > "bar" | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Should_compare_jconstructors_by_argument_count() | ||
| { | ||
| // Arrange | ||
| var ctor1 = new JConstructor("foo", new JValue(1)); | ||
| var ctor2 = new JConstructor("foo", new JValue(1), new JValue(2)); | ||
|
|
||
| // Act & Assert | ||
| Comparer.Compare(ctor1, ctor2).Should().Be(-1); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Should_compare_jconstructors_by_argument_values() | ||
| { | ||
| // Arrange | ||
| var ctor1 = new JConstructor("foo", new JValue(1), new JValue(2)); | ||
| var ctor2 = new JConstructor("foo", new JValue(1), new JValue(3)); | ||
|
|
||
| // Act & Assert | ||
| Comparer.Compare(ctor1, ctor2).Should().Be(-1); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Should_return_zero_for_equal_jconstructors() | ||
| { | ||
| // Arrange | ||
| var ctor1 = new JConstructor("foo", new JValue(1), new JValue(2)); | ||
| var ctor2 = new JConstructor("foo", new JValue(1), new JValue(2)); | ||
|
|
||
| // Act & Assert | ||
| Comparer.Compare(ctor1, ctor2).Should().Be(0); | ||
| } | ||
| } | ||
| } |
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.