Skip to content

Commit 2d029d1

Browse files
committed
Put actual error on top in error message
This way you don't have to scroll to the actual error in case of large JSON documents.
1 parent 25cd3f3 commit 2d029d1

File tree

3 files changed

+51
-41
lines changed

3 files changed

+51
-41
lines changed

Src/FluentAssertions.Json/JTokenAssertions.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,12 @@ public AndConstraint<JTokenAssertions> BeEquivalentTo(JToken expected, string be
8888
{
8989
Difference difference = JTokenDifferentiator.FindFirstDifference(Subject, expected);
9090

91-
var message = $"Expected JSON document {Format(Subject, true).Replace("{", "{{").Replace("}", "}}")}" +
92-
$" to be equivalent to {Format(expected, true).Replace("{", "{{").Replace("}", "}}")}" +
93-
$"{{reason}}, but {difference}.";
91+
var message = $"JSON document {difference}.{Environment.NewLine}" +
92+
$"Expected{Environment.NewLine}" +
93+
$"{Format(Subject, true).Replace("{", "{{").Replace("}", "}}")}{Environment.NewLine}" +
94+
$"to be equivalent to{Environment.NewLine}" +
95+
$"{Format(expected, true).Replace("{", "{{").Replace("}", "}}")}{Environment.NewLine}" +
96+
"{reason}.";
9497

9598
Execute.Assertion
9699
.ForCondition(difference == null)

Src/FluentAssertions.Json/JTokenDifferentiator.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,21 +185,21 @@ public override string ToString()
185185
switch (Kind)
186186
{
187187
case DifferenceKind.ActualIsNull:
188-
return "it is null";
188+
return "is null";
189189
case DifferenceKind.ExpectedIsNull:
190-
return "it is not null";
190+
return "is not null";
191191
case DifferenceKind.OtherType:
192-
return $"the type is different at {Path}";
192+
return $"has a different type at {Path}";
193193
case DifferenceKind.OtherName:
194-
return $"the name is different at {Path}";
194+
return $"has a different name at {Path}";
195195
case DifferenceKind.OtherValue:
196-
return $"the value is different at {Path}";
196+
return $"has a different value at {Path}";
197197
case DifferenceKind.DifferentLength:
198-
return $"the length is different at {Path}";
198+
return $"has a different length at {Path}";
199199
case DifferenceKind.ActualMissesProperty:
200-
return $"it misses property {Path}";
200+
return $"misses property {Path}";
201201
case DifferenceKind.ExpectedMissesProperty:
202-
return $"it has extra property {Path}";
202+
return $"has extra property {Path}";
203203
default:
204204
throw new ArgumentOutOfRangeException();
205205
}

Tests/FluentAssertions.Json.Shared.Specs/JTokenAssertionsSpecs.cs

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -76,67 +76,67 @@ public void When_objects_differ_BeEquivalentTo_should_fail()
7676
Tuple.Create(
7777
(string)null,
7878
"{ id: 2 }",
79-
"it is null")
79+
"is null")
8080
,
8181
Tuple.Create(
8282
"{ id: 1 }",
8383
(string)null,
84-
"it is not null")
84+
"is not null")
8585
,
8686
Tuple.Create(
8787
"{ items: [] }",
8888
"{ items: 2 }",
89-
"the type is different at $.items")
89+
"has a different type at $.items")
9090
,
9191
Tuple.Create(
9292
"{ items: [ \"fork\", \"knife\" , \"spoon\" ]}",
9393
"{ items: [ \"fork\", \"knife\" ]}",
94-
"the length is different at $.items")
94+
"has a different length at $.items")
9595
,
9696
Tuple.Create(
97-
"{ items: [ \"fork\", \"knife\" , \"spoon\" ]}",
9897
"{ items: [ \"fork\", \"knife\" ]}",
99-
"the length is different at $.items")
98+
"{ items: [ \"fork\", \"knife\" , \"spoon\" ]}",
99+
"has a different length at $.items")
100100
,
101101
Tuple.Create(
102102
"{ items: [ \"fork\", \"knife\" , \"spoon\" ]}",
103103
"{ items: [ \"fork\", \"spoon\", \"knife\" ]}",
104-
"the value is different at $.items[1]")
104+
"has a different value at $.items[1]")
105105
,
106106
Tuple.Create(
107107
"{ tree: { } }",
108108
"{ tree: \"oak\" }",
109-
"the type is different at $.tree")
109+
"has a different type at $.tree")
110110
,
111111
Tuple.Create(
112112
"{ tree: { leaves: 10} }",
113113
"{ tree: { branches: 5, leaves: 10 } }",
114-
"it misses property $.tree.branches")
114+
"misses property $.tree.branches")
115115
,
116116
Tuple.Create(
117117
"{ tree: { branches: 5, leaves: 10 } }",
118118
"{ tree: { leaves: 10} }",
119-
"it has extra property $.tree.branches")
119+
"has extra property $.tree.branches")
120120
,
121121
Tuple.Create(
122122
"{ tree: { leaves: 5 } }",
123123
"{ tree: { leaves: 10} }",
124-
"the value is different at $.tree.leaves")
124+
"has a different value at $.tree.leaves")
125125
,
126126
Tuple.Create(
127127
"{ eyes: \"blue\" }",
128128
"{ eyes: [] }",
129-
"the type is different at $.eyes")
129+
"has a different type at $.eyes")
130130
,
131131
Tuple.Create(
132132
"{ eyes: \"blue\" }",
133133
"{ eyes: 2 }",
134-
"the type is different at $.eyes")
134+
"has a different type at $.eyes")
135135
,
136136
Tuple.Create(
137137
"{ id: 1 }",
138138
"{ id: 2 }",
139-
"the value is different at $.id")
139+
"has a different value at $.id")
140140
};
141141

142142
foreach (var testCase in testCases)
@@ -149,9 +149,11 @@ public void When_objects_differ_BeEquivalentTo_should_fail()
149149
var expected = (expectedJson != null) ? JToken.Parse(expectedJson) : null;
150150

151151
var expectedMessage =
152-
$"Expected JSON document {Format(actual, true)} " +
153-
$"to be equivalent to {Format(expected, true)}, " +
154-
"but " + expectedDifference + ".";
152+
$"JSON document {expectedDifference}." +
153+
$"Expected" +
154+
$"{Format(actual, true)}" +
155+
$"to be equivalent to" +
156+
$"{Format(expected, true)}.";
155157

156158
//-----------------------------------------------------------------------------------------------------------
157159
// Act & Assert
@@ -173,29 +175,32 @@ public void When_properties_differ_BeEquivalentTo_should_fail()
173175
Tuple.Create<JToken, JToken, string>(
174176
new JProperty("eyes", "blue"),
175177
new JArray(),
176-
"the type is different at $")
178+
"has a different type at $")
177179
,
178180
Tuple.Create<JToken, JToken, string>(
179181
new JProperty("eyes", "blue"),
180182
new JProperty("hair", "black"),
181-
"the name is different at $")
183+
"has a different name at $")
182184
,
183185
};
184186

185187
foreach (var testCase in testCases)
186188
{
187-
var a = testCase.Item1;
188-
var b = testCase.Item2;
189-
189+
var actual = testCase.Item1;
190+
var expected = testCase.Item2;
191+
var expectedDifference = testCase.Item3;
192+
190193
var expectedMessage =
191-
$"Expected JSON document {Format(a, true)} " +
192-
$"to be equivalent to {Format(b, true)}, " +
193-
"but " + testCase.Item3 + ".";
194+
$"JSON document {expectedDifference}." +
195+
$"Expected" +
196+
$"{Format(actual, true)}" +
197+
$"to be equivalent to" +
198+
$"{Format(expected, true)}.";
194199

195200
//-----------------------------------------------------------------------------------------------------------
196201
// Act & Assert
197202
//-----------------------------------------------------------------------------------------------------------
198-
a.Should().Invoking(x => x.BeEquivalentTo(b))
203+
actual.Should().Invoking(x => x.BeEquivalentTo(expected))
199204
.Should().Throw<XunitException>()
200205
.WithMessage(expectedMessage);
201206
}
@@ -360,10 +365,12 @@ public void When_specifying_a_reason_why_object_should_be_equivalent_it_should_u
360365
var expected = JToken.Parse("{ child: { expected: 'bar' } }");
361366

362367
var expectedMessage =
363-
$"Expected JSON document {Format(subject, true)} " +
364-
$"to be equivalent to {Format(expected, true)} " +
365-
"because we want to test the failure message, " +
366-
"but it misses property $.child.expected.";
368+
$"JSON document misses property $.child.expected." +
369+
$"Expected" +
370+
$"{Format(subject, true)}" +
371+
$"to be equivalent to" +
372+
$"{Format(expected, true)} " +
373+
"because we want to test the failure message.";
367374

368375
//-----------------------------------------------------------------------------------------------------------
369376
// Act & Assert

0 commit comments

Comments
 (0)