Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes a FormatException when a bad string is included in the 'because' clause #764

Merged
merged 1 commit into from
Feb 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 14 additions & 6 deletions Src/FluentAssertions/Execution/AssertionScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,26 @@ public AssertionScope UsingLineBreaks
/// <param name="because">
/// A formatted phrase compatible with <see cref="string.Format(string,object[])"/> explaining why
/// the condition should be satisfied. If the phrase does not start with the word <i>because</i>,
/// it is prepended to the message.
/// it is prepended to the message. If the format of <paramref name="because"/> or
/// <paramref name="becauseArgs"/> is not compatible with <see cref="string.Format(string,object[])"/>,
/// then a warning message is returned instead.
/// </param>
/// <param name="becauseArgs">
/// Zero or more values to use for filling in any <see cref="string.Format(string,object[])"/> compatible placeholders.
/// </param>
/// <exception cref="System.FormatException">
/// Thrown if the format of <paramref name="because"/> or <paramref name="becauseArgs"/> is not
/// compatible with <see cref="string.Format(string,object[])"/>.
/// </exception>
public AssertionScope BecauseOf(string because, params object[] becauseArgs)
{
reason = () => string.Format(because ?? "", becauseArgs ?? new object[0]);
reason = () =>
{
try
{
return string.Format(because ?? "", becauseArgs ?? new object[0]);
}
catch (FormatException formatException)
{
return $"**WARNING** because message '{because}' could not be formatted with string.Format{Environment.NewLine}{formatException.StackTrace}";
}
};
return this;
}

Expand Down
15 changes: 15 additions & 0 deletions Tests/Shared.Specs/AssertionScopeSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,21 @@ public void When_parentheses_are_used_in_the_because_arguments_it_should_render_
.WithMessage("*because can't use these in becauseArgs: { }*");
}

[Fact]
public void When_invalid_format_is_used_in_because_parameter_it_should_render_default_text()
{
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = () => 1.Should().Be(2, "use of {} is considered invalid in because parameter");

//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>()
.WithMessage("*because message 'use of {} is considered invalid in because parameter' could not be formatted with string.Format*");
}

[Fact]
public void When_an_assertion_fails_in_a_scope_with_braces_it_should_use_the_name_as_the_assertion_context()
{
Expand Down