Skip to content

Commit 9d21fa4

Browse files
alhimik45dennisdoomen
authored andcommitted
Add placeholder escaping in BeEquivalentTo message (#33)
* Added placeholder escaping for `difference` in BeEquivalentTo message creation (#32) * Add expected message to test
1 parent a270667 commit 9d21fa4

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

Src/FluentAssertions.Json/JTokenAssertions.cs

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

91-
var message = $"JSON document {difference}.{Environment.NewLine}" +
91+
var message = $"JSON document {difference?.ToString().Escape(true)}.{Environment.NewLine}" +
9292
$"Expected{Environment.NewLine}" +
93-
$"{Format(Subject, true).Replace("{", "{{").Replace("}", "}}")}{Environment.NewLine}" +
93+
$"{Format(Subject, true).Escape(true)}{Environment.NewLine}" +
9494
$"to be equivalent to{Environment.NewLine}" +
95-
$"{Format(expected, true).Replace("{", "{{").Replace("}", "}}")}{Environment.NewLine}" +
95+
$"{Format(expected, true).Escape(true)}{Environment.NewLine}" +
9696
"{reason}.";
97-
97+
9898
Execute.Assertion
9999
.ForCondition(difference == null)
100100
.BecauseOf(because, becauseArgs)

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,31 @@ public void When_checking_whether_a_JToken_is_not_equivalent_to_the_string_repre
432432
action.Should().Throw<XunitException>()
433433
.WithMessage("Expected JSON document not to be equivalent*");
434434
}
435-
435+
436+
[Fact]
437+
public void When_properties_contains_curly_braces_BeEquivalentTo_should_not_fail_with_FormatException()
438+
{
439+
//-----------------------------------------------------------------------------------------------------------
440+
// Arrange
441+
//-----------------------------------------------------------------------------------------------------------
442+
var actual = JToken.Parse(@"{ ""{a1}"": {b: 1 }}");
443+
var expected = JToken.Parse(@"{ ""{a1}"": {b: 2 }}");
444+
445+
var expectedMessage =
446+
"JSON document has a different value at $.{a1}.b." +
447+
"Expected" +
448+
$"{Format(actual, true)}" +
449+
"to be equivalent to" +
450+
$"{Format(expected, true)}.";
451+
452+
//-----------------------------------------------------------------------------------------------------------
453+
// Act & Assert
454+
//-----------------------------------------------------------------------------------------------------------
455+
actual.Should().Invoking(x => x.BeEquivalentTo(expected))
456+
.Should().Throw<XunitException>()
457+
.WithMessage(expectedMessage);
458+
}
459+
436460
#endregion (Not)BeEquivalentTo
437461

438462
#region (Not)HaveValue

0 commit comments

Comments
 (0)