forked from fluentassertions/fluentassertions
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sync and chainable
Func<Task<T>>
assertions
This PR enables you to continue asserting on the result of `Func<Task<int>>` when it does not throw. ```c# Func<Task<int>> func = () => Task.FromResult(42); func.Should().NotThrow() .Which.Should().Be(42); (await func.Should().NotThrowAsync()) .Which.Should().Be(42); func.Should().NotThrowAfter(10.Seconds(), 1.Seconds()) .Which.Should().Be(42) (await func.Should().NotThrowAfterAsync(10.Seconds(), 1.Seconds())) .Which.Should().Be(42); ``` The implementations and tests are mostly copied from the non-generic or sync versions. To avoid the redefinitions of `Subject` in each class two changes in generics: * `DelegateAssertions` and `AsyncFunctionAssertions` are now generic in `TAssertions`. * `AsyncFunctionAssertions` is also generic in `TTask : Task` to support both `Task` and `Task<T>` equally. This fixes fluentassertions#990
- Loading branch information
Showing
18 changed files
with
890 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
Src/FluentAssertions/Specialized/FunctionAssertionHelpers.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System; | ||
using FluentAssertions.Common; | ||
using FluentAssertions.Execution; | ||
|
||
namespace FluentAssertions.Specialized | ||
{ | ||
internal class FunctionAssertionHelpers | ||
{ | ||
internal static T NotThrow<T>(Func<T> subject, string because, object[] becauseArgs) | ||
{ | ||
try | ||
{ | ||
return subject(); | ||
} | ||
catch (Exception exception) | ||
{ | ||
Execute.Assertion | ||
.ForCondition(exception is null) | ||
.BecauseOf(because, becauseArgs) | ||
.FailWith("Did not expect any exception{reason}, but found {0}.", exception); | ||
|
||
return default; | ||
} | ||
} | ||
|
||
internal static TResult NotThrowAfter<TResult>(Func<TResult> subject, IClock clock, TimeSpan waitTime, TimeSpan pollInterval, string because, object[] becauseArgs) | ||
{ | ||
if (waitTime < TimeSpan.Zero) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(waitTime), $"The value of {nameof(waitTime)} must be non-negative."); | ||
} | ||
|
||
if (pollInterval < TimeSpan.Zero) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(pollInterval), $"The value of {nameof(pollInterval)} must be non-negative."); | ||
} | ||
|
||
TimeSpan? invocationEndTime = null; | ||
Exception exception = null; | ||
ITimer timer = clock.StartTimer(); | ||
|
||
while (invocationEndTime is null || invocationEndTime < waitTime) | ||
{ | ||
try | ||
{ | ||
return subject(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
exception = ex; | ||
} | ||
|
||
clock.Delay(pollInterval); | ||
invocationEndTime = timer.Elapsed; | ||
} | ||
|
||
Execute.Assertion | ||
.BecauseOf(because, becauseArgs) | ||
.FailWith("Did not expect any exceptions after {0}{reason}, but found {1}.", waitTime, exception); | ||
|
||
return default; | ||
} | ||
} | ||
} |
Oops, something went wrong.