Skip to content

Commit 18910d5

Browse files
committed
add JConstructor invariant for comparer
1 parent bb9e46b commit 18910d5

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

Src/FluentAssertions.Json/Common/JTokenExtensions.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,20 @@ public int Compare(JToken x, JToken y)
4747
JObject o => Compare(o, (JObject)y),
4848
JProperty p => Compare(p, (JProperty)y),
4949
JValue v => Compare(v, (JValue)y),
50+
JConstructor c => Compare(c, (JConstructor)y),
5051
_ => string.Compare(x.ToString(), y.ToString(), StringComparison.Ordinal)
5152
};
5253
}
5354

5455
private static int Compare(JValue x, JValue y) => Comparer<object>.Default.Compare(x.Value, y.Value);
5556

56-
private static int Compare(JArray x, JArray y)
57+
private static int Compare(JConstructor x, JConstructor y)
58+
{
59+
var nameComparison = string.Compare(x.Name, y.Name, StringComparison.Ordinal);
60+
return nameComparison != 0 ? nameComparison : Compare(x, (JContainer)y);
61+
}
62+
63+
private static int Compare(JContainer x, JContainer y)
5764
{
5865
var countComparison = x.Count.CompareTo(y.Count);
5966
if (countComparison != 0)

Tests/FluentAssertions.Json.Specs/JTokenComparerSpecs.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,49 @@ public void Should_compare_jproperties_by_name_and_value()
110110
Comparer.Compare(prop2, prop3).Should().Be(1);
111111
Comparer.Compare(prop3, prop2).Should().Be(-1);
112112
}
113+
114+
[Fact]
115+
public void Should_compare_jconstructors_by_name()
116+
{
117+
// Arrange
118+
var ctor1 = new JConstructor("foo", new JValue(1));
119+
var ctor2 = new JConstructor("bar", new JValue(1));
120+
121+
// Act & Assert
122+
Comparer.Compare(ctor1, ctor2).Should().BeGreaterThan(0); // "foo" > "bar"
123+
}
124+
125+
[Fact]
126+
public void Should_compare_jconstructors_by_argument_count()
127+
{
128+
// Arrange
129+
var ctor1 = new JConstructor("foo", new JValue(1));
130+
var ctor2 = new JConstructor("foo", new JValue(1), new JValue(2));
131+
132+
// Act & Assert
133+
Comparer.Compare(ctor1, ctor2).Should().Be(-1);
134+
}
135+
136+
[Fact]
137+
public void Should_compare_jconstructors_by_argument_values()
138+
{
139+
// Arrange
140+
var ctor1 = new JConstructor("foo", new JValue(1), new JValue(2));
141+
var ctor2 = new JConstructor("foo", new JValue(1), new JValue(3));
142+
143+
// Act & Assert
144+
Comparer.Compare(ctor1, ctor2).Should().Be(-1);
145+
}
146+
147+
[Fact]
148+
public void Should_return_zero_for_equal_jconstructors()
149+
{
150+
// Arrange
151+
var ctor1 = new JConstructor("foo", new JValue(1), new JValue(2));
152+
var ctor2 = new JConstructor("foo", new JValue(1), new JValue(2));
153+
154+
// Act & Assert
155+
Comparer.Compare(ctor1, ctor2).Should().Be(0);
156+
}
113157
}
114158
}

0 commit comments

Comments
 (0)