11using System . Diagnostics ;
22using FluentAssertions . Collections ;
3- using FluentAssertions . Common ;
3+ using FluentAssertions . Json . Common ;
44using FluentAssertions . Execution ;
55using FluentAssertions . Formatting ;
66using FluentAssertions . Primitives ;
@@ -34,7 +34,7 @@ public JTokenAssertions(JToken subject)
3434 /// <summary>
3535 /// Returns the type of the subject the assertion applies on.
3636 /// </summary>
37- protected override string Context => nameof ( JToken ) ;
37+ protected override string Identifier => nameof ( JToken ) ;
3838
3939 /// <summary>
4040 /// Asserts that the current <see cref="JToken" /> equals the <paramref name="expected" /> element.
@@ -131,10 +131,10 @@ public AndConstraint<JTokenAssertions> BeEquivalentTo(JToken expected, string be
131131 {
132132 ObjectDiffPatchResult diff = ObjectDiffPatch . GenerateDiff ( Subject , expected ) ;
133133 JToken firstDifferingToken = diff . NewValues ? . First ?? diff . OldValues ? . First ;
134- JTokenFormatter formatter = new JTokenFormatter ( ) ;
135-
136- var message = $ "Expected JSON document { formatter . ToString ( Subject , true ) . Replace ( "{" , "{{" ) . Replace ( "}" , "}}" ) } " +
137- $ " to be equivalent to { formatter . ToString ( expected , true ) . Replace ( "{" , "{{" ) . Replace ( "}" , "}}" ) } " +
134+
135+
136+ var message = $ "Expected JSON document { Format ( Subject , true ) . Replace ( "{" , "{{" ) . Replace ( "}" , "}}" ) } " +
137+ $ " to be equivalent to { Format ( expected , true ) . Replace ( "{" , "{{" ) . Replace ( "}" , "}}" ) } " +
138138 $ "{{reason}}, but differs at { firstDifferingToken ? . ToString ( ) . Replace ( "{" , "{{" ) . Replace ( "}" , "}}" ) } .";
139139
140140 Execute . Assertion
@@ -304,7 +304,7 @@ public AndWhichConstraint<JTokenAssertions, JToken> NotHaveElement(string unexpe
304304 public AndWhichConstraint < JTokenAssertions , JToken > ContainSingleItem ( string because = "" , params object [ ] becauseArgs )
305305 {
306306 var formatter = new JTokenFormatter ( ) ;
307- string formattedDocument = formatter . ToString ( Subject ) . Replace ( "{" , "{{" ) . Replace ( "}" , "}}" ) ;
307+ string formattedDocument = Format ( Subject ) . Replace ( "{" , "{{" ) . Replace ( "}" , "}}" ) ;
308308
309309 using ( new AssertionScope ( "JSON document " + formattedDocument ) )
310310 {
@@ -327,13 +327,22 @@ public AndWhichConstraint<JTokenAssertions, JToken> ContainSingleItem(string bec
327327 public AndConstraint < JTokenAssertions > HaveCount ( int expected , string because = "" , params object [ ] becauseArgs )
328328 {
329329 var formatter = new JTokenFormatter ( ) ;
330- string formattedDocument = formatter . ToString ( Subject ) . Replace ( "{" , "{{" ) . Replace ( "}" , "}}" ) ;
330+ string formattedDocument = Format ( Subject ) . Replace ( "{" , "{{" ) . Replace ( "}" , "}}" ) ;
331331
332332 using ( new AssertionScope ( "JSON document " + formattedDocument ) )
333333 {
334334 EnumerableSubject . HaveCount ( expected , because , becauseArgs ) ;
335335 return new AndConstraint < JTokenAssertions > ( this ) ;
336336 }
337337 }
338+
339+ public string Format ( JToken value , bool useLineBreaks = false )
340+ {
341+ return new JTokenFormatter ( ) . Format ( value , new FormattingContext
342+ {
343+ UseLineBreaks = useLineBreaks
344+ } , null ) ;
345+ }
346+
338347 }
339348}
0 commit comments