Skip to content

Commit

Permalink
Add placeholder escaping in BeEquivalentTo message (#33)
Browse files Browse the repository at this point in the history
* Added placeholder escaping for `difference` in BeEquivalentTo message creation (#32)

* Add expected message to test
  • Loading branch information
alhimik45 authored and dennisdoomen committed Dec 27, 2019
1 parent a270667 commit 9d21fa4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
8 changes: 4 additions & 4 deletions Src/FluentAssertions.Json/JTokenAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ public JTokenAssertions(JToken subject)
{
Difference difference = JTokenDifferentiator.FindFirstDifference(Subject, expected);

var message = $"JSON document {difference}.{Environment.NewLine}" +
var message = $"JSON document {difference?.ToString().Escape(true)}.{Environment.NewLine}" +
$"Expected{Environment.NewLine}" +
$"{Format(Subject, true).Replace("{", "{{").Replace("}", "}}")}{Environment.NewLine}" +
$"{Format(Subject, true).Escape(true)}{Environment.NewLine}" +
$"to be equivalent to{Environment.NewLine}" +
$"{Format(expected, true).Replace("{", "{{").Replace("}", "}}")}{Environment.NewLine}" +
$"{Format(expected, true).Escape(true)}{Environment.NewLine}" +
"{reason}.";

Execute.Assertion
.ForCondition(difference == null)
.BecauseOf(because, becauseArgs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,31 @@ public void When_checking_whether_a_JToken_is_not_equivalent_to_the_string_repre
action.Should().Throw<XunitException>()
.WithMessage("Expected JSON document not to be equivalent*");
}


[Fact]
public void When_properties_contains_curly_braces_BeEquivalentTo_should_not_fail_with_FormatException()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var actual = JToken.Parse(@"{ ""{a1}"": {b: 1 }}");
var expected = JToken.Parse(@"{ ""{a1}"": {b: 2 }}");

var expectedMessage =
"JSON document has a different value at $.{a1}.b." +
"Expected" +
$"{Format(actual, true)}" +
"to be equivalent to" +
$"{Format(expected, true)}.";

//-----------------------------------------------------------------------------------------------------------
// Act & Assert
//-----------------------------------------------------------------------------------------------------------
actual.Should().Invoking(x => x.BeEquivalentTo(expected))
.Should().Throw<XunitException>()
.WithMessage(expectedMessage);
}

#endregion (Not)BeEquivalentTo

#region (Not)HaveValue
Expand Down

0 comments on commit 9d21fa4

Please sign in to comment.