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

Escape brackets in execution time failures #1994

Merged
merged 1 commit into from Sep 13, 2022
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
11 changes: 6 additions & 5 deletions Src/FluentAssertions/Specialized/ExecutionTimeAssertions.cs
@@ -1,5 +1,6 @@
using System;
using System.ComponentModel;
using FluentAssertions.Common;
using FluentAssertions.Execution;

namespace FluentAssertions.Specialized;
Expand Down Expand Up @@ -77,7 +78,7 @@ public AndConstraint<ExecutionTimeAssertions> BeLessThanOrEqualTo(TimeSpan maxDu
.ForCondition(Condition(elapsed))
.BecauseOf(because, becauseArgs)
.FailWith("Execution of " +
execution.ActionDescription + " should be less than or equal to {0}{reason}, but it required " +
execution.ActionDescription.EscapePlaceholders() + " should be less than or equal to {0}{reason}, but it required " +
(isRunning ? "more than " : "exactly ") + "{1}.",
maxDuration,
elapsed);
Expand Down Expand Up @@ -110,7 +111,7 @@ public AndConstraint<ExecutionTimeAssertions> BeLessThan(TimeSpan maxDuration, s
.ForCondition(Condition(execution.ElapsedTime))
.BecauseOf(because, becauseArgs)
.FailWith("Execution of " +
execution.ActionDescription + " should be less than {0}{reason}, but it required " +
execution.ActionDescription.EscapePlaceholders() + " should be less than {0}{reason}, but it required " +
(isRunning ? "more than " : "exactly ") + "{1}.",
maxDuration,
elapsed);
Expand Down Expand Up @@ -140,7 +141,7 @@ public AndConstraint<ExecutionTimeAssertions> BeGreaterThanOrEqualTo(TimeSpan mi
.ForCondition(Condition(elapsed))
.BecauseOf(because, becauseArgs)
.FailWith("Execution of " +
execution.ActionDescription + " should be greater than or equal to {0}{reason}, but it required " +
execution.ActionDescription.EscapePlaceholders() + " should be greater than or equal to {0}{reason}, but it required " +
(isRunning ? "more than " : "exactly ") + "{1}.",
minDuration,
elapsed);
Expand Down Expand Up @@ -173,7 +174,7 @@ public AndConstraint<ExecutionTimeAssertions> BeGreaterThan(TimeSpan minDuration
.ForCondition(Condition(elapsed))
.BecauseOf(because, becauseArgs)
.FailWith("Execution of " +
execution.ActionDescription + " should be greater than {0}{reason}, but it required " +
execution.ActionDescription.EscapePlaceholders() + " should be greater than {0}{reason}, but it required " +
(isRunning ? "more than " : "exactly ") + "{1}.",
minDuration,
elapsed);
Expand Down Expand Up @@ -218,7 +219,7 @@ public AndConstraint<ExecutionTimeAssertions> BeCloseTo(TimeSpan expectedDuratio
Execute.Assertion
.ForCondition(MinCondition(elapsed) && MaxCondition(elapsed))
.BecauseOf(because, becauseArgs)
.FailWith("Execution of " + execution.ActionDescription +
.FailWith("Execution of " + execution.ActionDescription.EscapePlaceholders() +
" should be within {0} from {1}{reason}, but it required " +
(isRunning ? "more than " : "exactly ") + "{2}.",
precision,
Expand Down
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions.Extensions;
Expand Down Expand Up @@ -85,6 +86,20 @@ public void When_action_runs_indefinitely_it_should_be_stopped_and_throw_if_ther
act.Should().Throw<XunitException>().WithMessage(
"*action should be less than or equal to 100ms, but it required more than*");
}

[Fact]
public void Actions_with_brackets_fail_with_correctly_formatted_message()
{
// Arrange
var subject = new List<object>();

// Act
Action act = () => subject.ExecutionTimeOf(s => s.AddRange(new object[] { })).Should().BeLessOrEqualTo(1.Nanoseconds());

// Assert
act.Should().ThrowExactly<XunitException>()
.Which.Message.Should().Contain("{}").And.NotContain("{0}");
}
}

public class BeLessThan
Expand Down Expand Up @@ -189,6 +204,20 @@ public void When_action_runs_indefinitely_it_should_be_stopped_and_throw_if_ther
act.Should().Throw<XunitException>().WithMessage(
"*action should be less than 100ms, but it required more than*");
}

[Fact]
public void Actions_with_brackets_fail_with_correctly_formatted_message()
{
// Arrange
var subject = new List<object>();

// Act
Action act = () => subject.ExecutionTimeOf(s => s.AddRange(new object[] { })).Should().BeLessThan(1.Nanoseconds());

// Assert
act.Should().ThrowExactly<XunitException>()
.Which.Message.Should().Contain("{}").And.NotContain("{0}");
}
}

public class BeGreaterThanOrEqualTo
Expand Down Expand Up @@ -265,6 +294,20 @@ public void When_action_runs_indefinitely_it_should_be_stopped_and_not_throw_if_
// Assert
act.Should().NotThrow<XunitException>();
}

[Fact]
public void Actions_with_brackets_fail_with_correctly_formatted_message()
{
// Arrange
var subject = new List<object>();

// Act
Action act = () => subject.ExecutionTimeOf(s => s.AddRange(new object[] { })).Should().BeGreaterThanOrEqualTo(1.Days());

// Assert
act.Should().ThrowExactly<XunitException>()
.Which.Message.Should().Contain("{}").And.NotContain("{0}");
}
}

public class BeGreaterThan
Expand Down Expand Up @@ -341,6 +384,20 @@ public void When_action_runs_indefinitely_it_should_be_stopped_and_not_throw_if_
// Assert
act.Should().NotThrow<XunitException>();
}

[Fact]
public void Actions_with_brackets_fail_with_correctly_formatted_message()
{
// Arrange
var subject = new List<object>();

// Act
Action act = () => subject.ExecutionTimeOf(s => s.AddRange(new object[] { })).Should().BeGreaterThan(1.Days());

// Assert
act.Should().ThrowExactly<XunitException>()
.Which.Message.Should().Contain("{}").And.NotContain("{0}");
}
}

public class BeCloseTo
Expand Down Expand Up @@ -437,6 +494,21 @@ public void When_action_runs_indefinitely_it_should_be_stopped_and_throw_if_ther
act.Should().Throw<XunitException>().WithMessage(
"*action should be within 50ms from 100ms, but it required*");
}

[Fact]
public void Actions_with_brackets_fail_with_correctly_formatted_message()
{
// Arrange
var subject = new List<object>();

// Act
Action act = () => subject.ExecutionTimeOf(s => s.AddRange(new object[] { }))
.Should().BeCloseTo(1.Days(), 50.Milliseconds());

// Assert
act.Should().ThrowExactly<XunitException>()
.Which.Message.Should().Contain("{}").And.NotContain("{0}");
}
}

public class ExecutingTime
Expand Down
1 change: 1 addition & 0 deletions docs/_pages/releases.md
Expand Up @@ -19,6 +19,7 @@ sidebar:
* Fixed `For`/`Exclude` not excluding properties in objects in a collection - [#1953](https://github.com/fluentassertions/fluentassertions/pull/1953)
* Changed `MatchEquivalentOf` to use `CultureInfo.InvariantCulture` instead of `CultureInfo.CurrentCulture` - [#1985](https://github.com/fluentassertions/fluentassertions/pull/1985).
* Fixes `BeEquivalentTo` not taking into account any `record` equivalency settings coming from the `AssertionOptions` - [#1984](https://github.com/fluentassertions/fluentassertions/pull/1984)
* Fixed `ExecutionTimeOf` formatting failing when the expression includes {} - [#1994](https://github.com/fluentassertions/fluentassertions/pull/1994)

## 6.7.0

Expand Down