Skip to content

Commit e9e4310

Browse files
committed
Make JTokenDifferentiator an instance class
1 parent 3f466e3 commit e9e4310

File tree

2 files changed

+42
-38
lines changed

2 files changed

+42
-38
lines changed

Src/FluentAssertions.Json/JTokenAssertions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ private AndConstraint<JTokenAssertions> BeEquivalentTo(JToken expected, bool ign
114114
string because = "",
115115
params object[] becauseArgs)
116116
{
117-
Difference difference = JTokenDifferentiator.FindFirstDifference(Subject, expected, ignoreExtraProperties, config);
117+
var differentiator = new JTokenDifferentiator(ignoreExtraProperties, config);
118+
Difference difference = differentiator.FindFirstDifference(Subject, expected);
118119

119120
var expectation = ignoreExtraProperties ? "was expected to contain" : "was expected to be equivalent to";
120121

Src/FluentAssertions.Json/JTokenDifferentiator.cs

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,20 @@
66

77
namespace FluentAssertions.Json
88
{
9-
// REFACTOR: Change to non-static and make parameters fields
10-
internal static class JTokenDifferentiator
9+
internal class JTokenDifferentiator
1110
{
12-
public static Difference FindFirstDifference(JToken actual, JToken expected, bool ignoreExtraProperties, Func<IJsonAssertionOptions<object>, IJsonAssertionOptions<object>> config)
11+
private readonly bool ignoreExtraProperties;
12+
13+
private readonly Func<IJsonAssertionOptions<object>, IJsonAssertionOptions<object>> config;
14+
15+
public JTokenDifferentiator(bool ignoreExtraProperties,
16+
Func<IJsonAssertionOptions<object>, IJsonAssertionOptions<object>> config)
17+
{
18+
this.ignoreExtraProperties = ignoreExtraProperties;
19+
this.config = config;
20+
}
21+
22+
public Difference FindFirstDifference(JToken actual, JToken expected)
1323
{
1424
var path = new JPath();
1525

@@ -28,24 +38,22 @@ public static Difference FindFirstDifference(JToken actual, JToken expected, boo
2838
return new Difference(DifferenceKind.ExpectedIsNull, path);
2939
}
3040

31-
return FindFirstDifference(actual, expected, path, ignoreExtraProperties, config);
41+
return FindFirstDifference(actual, expected, path);
3242
}
3343

34-
private static Difference FindFirstDifference(JToken actual, JToken expected, JPath path, bool ignoreExtraProperties, Func<IJsonAssertionOptions<object>, IJsonAssertionOptions<object>> config)
44+
private Difference FindFirstDifference(JToken actual, JToken expected, JPath path)
3545
{
3646
return actual switch
3747
{
38-
JArray actualArray => FindJArrayDifference(actualArray, expected, path, ignoreExtraProperties, config),
39-
JObject actualObject => FindJObjectDifference(actualObject, expected, path, ignoreExtraProperties, config),
40-
JProperty actualProperty => FindJPropertyDifference(actualProperty, expected, path, ignoreExtraProperties, config),
41-
JValue actualValue => FindValueDifference(actualValue, expected, path, config),
48+
JArray actualArray => FindJArrayDifference(actualArray, expected, path),
49+
JObject actualObject => FindJObjectDifference(actualObject, expected, path),
50+
JProperty actualProperty => FindJPropertyDifference(actualProperty, expected, path),
51+
JValue actualValue => FindValueDifference(actualValue, expected, path),
4252
_ => throw new NotSupportedException(),
4353
};
4454
}
4555

46-
private static Difference FindJArrayDifference(JArray actualArray, JToken expected, JPath path,
47-
bool ignoreExtraProperties,
48-
Func<IJsonAssertionOptions<object>, IJsonAssertionOptions<object>> config)
56+
private Difference FindJArrayDifference(JArray actualArray, JToken expected, JPath path)
4957
{
5058
if (expected is not JArray expectedArray)
5159
{
@@ -54,15 +62,15 @@ private static Difference FindJArrayDifference(JArray actualArray, JToken expect
5462

5563
if (ignoreExtraProperties)
5664
{
57-
return CompareExpectedItems(actualArray, expectedArray, path, config);
65+
return CompareExpectedItems(actualArray, expectedArray, path);
5866
}
5967
else
6068
{
61-
return CompareItems(actualArray, expectedArray, path, config);
69+
return CompareItems(actualArray, expectedArray, path);
6270
}
6371
}
6472

65-
private static Difference CompareExpectedItems(JArray actual, JArray expected, JPath path, Func<IJsonAssertionOptions<object>, IJsonAssertionOptions<object>> config)
73+
private Difference CompareExpectedItems(JArray actual, JArray expected, JPath path)
6674
{
6775
JToken[] actualChildren = actual.Children().ToArray();
6876
JToken[] expectedChildren = expected.Children().ToArray();
@@ -74,7 +82,7 @@ private static Difference CompareExpectedItems(JArray actual, JArray expected, J
7482
bool match = false;
7583
for (int actualIndex = matchingIndex; actualIndex < actualChildren.Length; actualIndex++)
7684
{
77-
var difference = FindFirstDifference(actualChildren[actualIndex], expectedChild, true, config);
85+
var difference = FindFirstDifference(actualChildren[actualIndex], expectedChild);
7886

7987
if (difference == null)
8088
{
@@ -88,7 +96,7 @@ private static Difference CompareExpectedItems(JArray actual, JArray expected, J
8896
{
8997
if (matchingIndex >= actualChildren.Length)
9098
{
91-
if (actualChildren.Any(actualChild => FindFirstDifference(actualChild, expectedChild, true, config) == null))
99+
if (actualChildren.Any(actualChild => FindFirstDifference(actualChild, expectedChild) == null))
92100
{
93101
return new Difference(DifferenceKind.WrongOrder, path.AddIndex(expectedIndex));
94102
}
@@ -97,15 +105,14 @@ private static Difference CompareExpectedItems(JArray actual, JArray expected, J
97105
}
98106

99107
return FindFirstDifference(actualChildren[matchingIndex], expectedChild,
100-
path.AddIndex(expectedIndex), true, config);
108+
path.AddIndex(expectedIndex));
101109
}
102110
}
103111

104112
return null;
105113
}
106114

107-
private static Difference CompareItems(JArray actual, JArray expected, JPath path,
108-
Func<IJsonAssertionOptions<object>, IJsonAssertionOptions<object>> config)
115+
private Difference CompareItems(JArray actual, JArray expected, JPath path)
109116
{
110117
JToken[] actualChildren = actual.Children().ToArray();
111118
JToken[] expectedChildren = expected.Children().ToArray();
@@ -117,8 +124,7 @@ private static Difference CompareItems(JArray actual, JArray expected, JPath pat
117124

118125
for (int i = 0; i < actualChildren.Length; i++)
119126
{
120-
Difference firstDifference = FindFirstDifference(actualChildren[i], expectedChildren[i],
121-
path.AddIndex(i), false, config);
127+
Difference firstDifference = FindFirstDifference(actualChildren[i], expectedChildren[i], path.AddIndex(i));
122128

123129
if (firstDifference != null)
124130
{
@@ -129,20 +135,17 @@ private static Difference CompareItems(JArray actual, JArray expected, JPath pat
129135
return null;
130136
}
131137

132-
private static Difference FindJObjectDifference(JObject actual, JToken expected, JPath path, bool ignoreExtraProperties,
133-
Func<IJsonAssertionOptions<object>, IJsonAssertionOptions<object>> config)
138+
private Difference FindJObjectDifference(JObject actual, JToken expected, JPath path)
134139
{
135140
if (expected is not JObject expectedObject)
136141
{
137142
return new Difference(DifferenceKind.OtherType, path, Describe(actual.Type), Describe(expected.Type));
138143
}
139144

140-
return CompareProperties(actual?.Properties(), expectedObject.Properties(), path, ignoreExtraProperties, config);
145+
return CompareProperties(actual?.Properties(), expectedObject.Properties(), path);
141146
}
142147

143-
private static Difference CompareProperties(IEnumerable<JProperty> actual, IEnumerable<JProperty> expected, JPath path,
144-
bool ignoreExtraProperties,
145-
Func<IJsonAssertionOptions<object>, IJsonAssertionOptions<object>> config)
148+
private Difference CompareProperties(IEnumerable<JProperty> actual, IEnumerable<JProperty> expected, JPath path)
146149
{
147150
var actualDictionary = actual?.ToDictionary(p => p.Name, p => p.Value) ?? new Dictionary<string, JToken>();
148151
var expectedDictionary = expected?.ToDictionary(p => p.Name, p => p.Value) ?? new Dictionary<string, JToken>();
@@ -168,7 +171,7 @@ private static Difference CompareProperties(IEnumerable<JProperty> actual, IEnum
168171
JToken actualValue = actualDictionary[expectedPair.Key];
169172

170173
Difference firstDifference = FindFirstDifference(actualValue, expectedPair.Value,
171-
path.AddProperty(expectedPair.Key), ignoreExtraProperties, config);
174+
path.AddProperty(expectedPair.Key));
172175

173176
if (firstDifference != null)
174177
{
@@ -179,9 +182,7 @@ private static Difference CompareProperties(IEnumerable<JProperty> actual, IEnum
179182
return null;
180183
}
181184

182-
private static Difference FindJPropertyDifference(JProperty actualProperty, JToken expected, JPath path,
183-
bool ignoreExtraProperties,
184-
Func<IJsonAssertionOptions<object>, IJsonAssertionOptions<object>> config)
185+
private Difference FindJPropertyDifference(JProperty actualProperty, JToken expected, JPath path)
185186
{
186187
if (expected is not JProperty expectedProperty)
187188
{
@@ -193,20 +194,20 @@ private static Difference FindJPropertyDifference(JProperty actualProperty, JTok
193194
return new Difference(DifferenceKind.OtherName, path);
194195
}
195196

196-
return FindFirstDifference(actualProperty.Value, expectedProperty.Value, path, ignoreExtraProperties, config);
197+
return FindFirstDifference(actualProperty.Value, expectedProperty.Value, path);
197198
}
198199

199-
private static Difference FindValueDifference(JValue actualValue, JToken expected, JPath path, Func<IJsonAssertionOptions<object>, IJsonAssertionOptions<object>> config)
200+
private Difference FindValueDifference(JValue actualValue, JToken expected, JPath path)
200201
{
201202
if (expected is not JValue expectedValue)
202203
{
203204
return new Difference(DifferenceKind.OtherType, path, Describe(actualValue.Type), Describe(expected.Type));
204205
}
205206

206-
return CompareValues(actualValue, expectedValue, path, config);
207+
return CompareValues(actualValue, expectedValue, path);
207208
}
208209

209-
private static Difference CompareValues(JValue actual, JValue expected, JPath path, Func<IJsonAssertionOptions<object>, IJsonAssertionOptions<object>> config)
210+
private Difference CompareValues(JValue actual, JValue expected, JPath path)
210211
{
211212
if (actual.Type != expected.Type)
212213
{
@@ -216,7 +217,9 @@ private static Difference CompareValues(JValue actual, JValue expected, JPath pa
216217
bool hasMismatches;
217218
using (var scope = new AssertionScope())
218219
{
219-
actual.Value.Should().BeEquivalentTo(expected.Value, options => (JsonAssertionOptions<object>)config.Invoke(new JsonAssertionOptions<object>(options)));
220+
actual.Value.Should().BeEquivalentTo(expected.Value, options =>
221+
(JsonAssertionOptions<object>)config.Invoke(new JsonAssertionOptions<object>(options)));
222+
220223
hasMismatches = scope.Discard().Length > 0;
221224
}
222225

0 commit comments

Comments
 (0)