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

Fix substitution of contextual tags #754

Merged
merged 1 commit into from Feb 5, 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
2 changes: 1 addition & 1 deletion Src/FluentAssertions/Execution/MessageBuilder.cs
Expand Up @@ -72,7 +72,7 @@ private string SubstituteIdentifier(string message, string identifier, string fa

private string SubstituteContextualTags(string message, ContextDataItems contextData)
{
string pattern = @"[^\{]\{(?<key>[a-z|A-Z]+)(?:\:(?<default>[a-z|A-Z|\s]+))?\}";
string pattern = @"(?<!\{)\{(?<key>[a-z|A-Z]+)(?:\:(?<default>[a-z|A-Z|\s]+))?\}(?!\})";

return Regex.Replace(message, pattern, match =>
{
Expand Down
47 changes: 47 additions & 0 deletions Tests/Shared.Specs/AssertionScopeSpecs.cs
Expand Up @@ -250,5 +250,52 @@ public void When_message_contains_double_braces_they_should_not_be_replaced_with
act.Should().ThrowExactly<XunitException>()
.WithMessage("*empty*");
}

[Fact]
public void When_message_starts_with_single_braces_they_should_be_replaced_with_context()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var scope = new AssertionScope();
scope.AddReportable("MyKey", "MyValue");

AssertionScope.Current.FailWith("{MyKey}");

//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = scope.Dispose;

//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().ThrowExactly<XunitException>()
.WithMessage("*MyValue*");
}

[Fact]
public void When_message_starts_with_two_single_braces_they_should_be_replaced_with_context()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var scope = new AssertionScope();
scope.AddReportable("SomeKey", "SomeValue");
scope.AddReportable("AnotherKey", "AnotherValue");

AssertionScope.Current.FailWith("{SomeKey}{AnotherKey}");

//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = scope.Dispose;

//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().ThrowExactly<XunitException>()
.WithMessage("*SomeValue*AnotherValue*");
}
}
}