diff --git a/Src/FluentAssertions/Specialized/AsyncFunctionAssertions.cs b/Src/FluentAssertions/Specialized/AsyncFunctionAssertions.cs index ee7a52530c..6b71fdc0d4 100644 --- a/Src/FluentAssertions/Specialized/AsyncFunctionAssertions.cs +++ b/Src/FluentAssertions/Specialized/AsyncFunctionAssertions.cs @@ -132,21 +132,21 @@ public async Task> ThrowExactlyAsync .BecauseOf(because, becauseArgs) .FailWith("Expected {context} to throw exactly {0}{reason}, but found .", expectedType); - if (!success) + if (success) { - return new ExceptionAssertions(Array.Empty()); - } + Exception exception = await InvokeWithInterceptionAsync(Subject); - Exception exception = await InvokeWithInterceptionAsync(Subject); + Execute.Assertion + .ForCondition(exception is not null) + .BecauseOf(because, becauseArgs) + .FailWith("Expected {0}{reason}, but no exception was thrown.", expectedType); - Execute.Assertion - .ForCondition(exception is not null) - .BecauseOf(because, becauseArgs) - .FailWith("Expected {0}{reason}, but no exception was thrown.", expectedType); + exception.Should().BeOfType(expectedType, because, becauseArgs); - exception.Should().BeOfType(expectedType, because, becauseArgs); + return new ExceptionAssertions(new[] { exception as TException }); + } - return new ExceptionAssertions(new[] { exception as TException }); + return new ExceptionAssertions(Array.Empty()); } /// @@ -168,13 +168,13 @@ public async Task> ThrowAsync(string .BecauseOf(because, becauseArgs) .FailWith("Expected {context} to throw {0}{reason}, but found .", typeof(TException)); - if (!success) + if (success) { - return new ExceptionAssertions(Array.Empty()); + Exception exception = await InvokeWithInterceptionAsync(Subject); + return ThrowInternal(exception, because, becauseArgs); } - Exception exception = await InvokeWithInterceptionAsync(Subject); - return ThrowInternal(exception, because, becauseArgs); + return new ExceptionAssertions(Array.Empty()); } /// @@ -194,18 +194,16 @@ public async Task> NotThrowAsync(string because = "", .BecauseOf(because, becauseArgs) .FailWith("Expected {context} not to throw{reason}, but found ."); - if (!success) - { - return new AndConstraint((TAssertions)this); - } - - try - { - await Subject.Invoke(); - } - catch (Exception exception) + if (success) { - NotThrowInternal(exception, because, becauseArgs); + try + { + await Subject.Invoke(); + } + catch (Exception exception) + { + return NotThrowInternal(exception, because, becauseArgs); + } } return new AndConstraint((TAssertions)this); @@ -229,18 +227,16 @@ public async Task> NotThrowAsync(string b .BecauseOf(because, becauseArgs) .FailWith("Expected {context} not to throw{reason}, but found ."); - if (!success) - { - return new AndConstraint((TAssertions)this); - } - - try - { - await Subject.Invoke(); - } - catch (Exception exception) + if (success) { - NotThrowInternal(exception, because, becauseArgs); + try + { + await Subject.Invoke(); + } + catch (Exception exception) + { + return NotThrowInternal(exception, because, becauseArgs); + } } return new AndConstraint((TAssertions)this); @@ -279,37 +275,37 @@ public Task> NotThrowAfterAsync(TimeSpan waitTime, Ti .BecauseOf(because, becauseArgs) .FailWith("Expected {context} not to throw any exceptions after {0}{reason}, but found .", waitTime); - if (!success) - { - return Task.FromResult(new AndConstraint((TAssertions)this)); - } - - return AssertionTaskAsync(); - - async Task> AssertionTaskAsync() + if (success) { - TimeSpan? invocationEndTime = null; - Exception exception = null; - ITimer timer = Clock.StartTimer(); + return AssertionTaskAsync(); - while (invocationEndTime is null || invocationEndTime < waitTime) + async Task> AssertionTaskAsync() { - exception = await InvokeWithInterceptionAsync(Subject); - if (exception is null) + TimeSpan? invocationEndTime = null; + Exception exception = null; + ITimer timer = Clock.StartTimer(); + + while (invocationEndTime is null || invocationEndTime < waitTime) { - return new AndConstraint((TAssertions)this); + exception = await InvokeWithInterceptionAsync(Subject); + if (exception is null) + { + return new AndConstraint((TAssertions)this); + } + + await Clock.DelayAsync(pollInterval, CancellationToken.None); + invocationEndTime = timer.Elapsed; } - await Clock.DelayAsync(pollInterval, CancellationToken.None); - invocationEndTime = timer.Elapsed; - } - - Execute.Assertion - .BecauseOf(because, becauseArgs) - .FailWith("Did not expect any exceptions after {0}{reason}, but found {1}.", waitTime, exception); + Execute.Assertion + .BecauseOf(because, becauseArgs) + .FailWith("Did not expect any exceptions after {0}{reason}, but found {1}.", waitTime, exception); - return new AndConstraint((TAssertions)this); + return new AndConstraint((TAssertions)this); + } } + + return Task.FromResult(new AndConstraint((TAssertions)this)); } /// diff --git a/Src/FluentAssertions/Specialized/DelegateAssertions.cs b/Src/FluentAssertions/Specialized/DelegateAssertions.cs index b93c06c066..03bd49c569 100644 --- a/Src/FluentAssertions/Specialized/DelegateAssertions.cs +++ b/Src/FluentAssertions/Specialized/DelegateAssertions.cs @@ -43,14 +43,14 @@ public ExceptionAssertions Throw(string because = "", pa .BecauseOf(because, becauseArgs) .FailWith("Expected {context} to throw {0}{reason}, but found .", typeof(TException)); - if (!success) + if (success) { - return new ExceptionAssertions(Array.Empty()); + FailIfSubjectIsAsyncVoid(); + Exception exception = InvokeSubjectWithInterception(); + return ThrowInternal(exception, because, becauseArgs); } - FailIfSubjectIsAsyncVoid(); - Exception exception = InvokeSubjectWithInterception(); - return ThrowInternal(exception, because, becauseArgs); + return new ExceptionAssertions(Array.Empty()); } /// @@ -71,14 +71,14 @@ public AndConstraint NotThrow(string because = "", para .BecauseOf(because, becauseArgs) .FailWith("Expected {context} not to throw {0}{reason}, but found .", typeof(TException)); - if (!success) + if (success) { - return new AndConstraint((TAssertions)this); + FailIfSubjectIsAsyncVoid(); + Exception exception = InvokeSubjectWithInterception(); + return NotThrowInternal(exception, because, becauseArgs); } - FailIfSubjectIsAsyncVoid(); - Exception exception = InvokeSubjectWithInterception(); - return NotThrowInternal(exception, because, becauseArgs); + return new AndConstraint((TAssertions)this); } /// @@ -98,14 +98,14 @@ public AndConstraint NotThrow(string because = "", params object[] .BecauseOf(because, becauseArgs) .FailWith("Expected {context} not to throw{reason}, but found ."); - if (!success) + if (success) { - return new AndConstraint((TAssertions)this); + FailIfSubjectIsAsyncVoid(); + Exception exception = InvokeSubjectWithInterception(); + return NotThrowInternal(exception, because, becauseArgs); } - FailIfSubjectIsAsyncVoid(); - Exception exception = InvokeSubjectWithInterception(); - return NotThrowInternal(exception, because, becauseArgs); + return new AndConstraint((TAssertions)this); } /// @@ -133,24 +133,24 @@ public ExceptionAssertions ThrowExactly(string because = .BecauseOf(because, becauseArgs) .FailWith("Expected {context} to throw exactly {0}{reason}, but found .", typeof(TException)); - if (!success) + if (success) { - return new ExceptionAssertions(Array.Empty()); - } + FailIfSubjectIsAsyncVoid(); + Exception exception = InvokeSubjectWithInterception(); - FailIfSubjectIsAsyncVoid(); - Exception exception = InvokeSubjectWithInterception(); + Type expectedType = typeof(TException); - Type expectedType = typeof(TException); + Execute.Assertion + .ForCondition(exception is not null) + .BecauseOf(because, becauseArgs) + .FailWith("Expected {0}{reason}, but no exception was thrown.", expectedType); - Execute.Assertion - .ForCondition(exception is not null) - .BecauseOf(because, becauseArgs) - .FailWith("Expected {0}{reason}, but no exception was thrown.", expectedType); + exception.Should().BeOfType(expectedType, because, becauseArgs); - exception.Should().BeOfType(expectedType, because, becauseArgs); + return new ExceptionAssertions(new[] { exception as TException }); + } - return new ExceptionAssertions(new[] { exception as TException }); + return new ExceptionAssertions(Array.Empty()); } /// @@ -186,34 +186,32 @@ public AndConstraint NotThrowAfter(TimeSpan waitTime, TimeSpan poll .BecauseOf(because, becauseArgs) .FailWith("Expected {context} not to throw after {0}{reason}, but found .", waitTime); - if (!success) + if (success) { - return new AndConstraint((TAssertions)this); - } - - FailIfSubjectIsAsyncVoid(); + FailIfSubjectIsAsyncVoid(); - TimeSpan? invocationEndTime = null; - Exception exception = null; - ITimer timer = Clock.StartTimer(); + TimeSpan? invocationEndTime = null; + Exception exception = null; + ITimer timer = Clock.StartTimer(); - while (invocationEndTime is null || invocationEndTime < waitTime) - { - exception = InvokeSubjectWithInterception(); - if (exception is null) + while (invocationEndTime is null || invocationEndTime < waitTime) { - break; + exception = InvokeSubjectWithInterception(); + if (exception is null) + { + break; + } + + Clock.Delay(pollInterval); + invocationEndTime = timer.Elapsed; } - Clock.Delay(pollInterval); - invocationEndTime = timer.Elapsed; + Execute.Assertion + .BecauseOf(because, becauseArgs) + .ForCondition(exception is null) + .FailWith("Did not expect any exceptions after {0}{reason}, but found {1}.", waitTime, exception); } - Execute.Assertion - .BecauseOf(because, becauseArgs) - .ForCondition(exception is null) - .FailWith("Did not expect any exceptions after {0}{reason}, but found {1}.", waitTime, exception); - return new AndConstraint((TAssertions)this); } diff --git a/Src/FluentAssertions/Specialized/GenericAsyncFunctionAssertions.cs b/Src/FluentAssertions/Specialized/GenericAsyncFunctionAssertions.cs index 3a46130f8e..624184c4df 100644 --- a/Src/FluentAssertions/Specialized/GenericAsyncFunctionAssertions.cs +++ b/Src/FluentAssertions/Specialized/GenericAsyncFunctionAssertions.cs @@ -39,31 +39,29 @@ public GenericAsyncFunctionAssertions(Func> subject, IExtractExcep .BecauseOf(because, becauseArgs) .FailWith("Expected {context} to complete within {0}{reason}, but found .", timeSpan); - if (!success) - { - // subject is null, nothing to execute - // We need (currently) to return a default result as "Which" because actual result is not available. - return new AndWhichConstraint, TResult>(this, default(TResult)); - } - - (Task task, TimeSpan remainingTime) = InvokeWithTimer(timeSpan); - - success = Execute.Assertion - .ForCondition(remainingTime >= TimeSpan.Zero) - .BecauseOf(because, becauseArgs) - .FailWith("Expected {context:task} to complete within {0}{reason}.", timeSpan); - if (success) { - bool completesWithinTimeout = await CompletesWithinTimeoutAsync(task, remainingTime); - Execute.Assertion - .ForCondition(completesWithinTimeout) + (Task task, TimeSpan remainingTime) = InvokeWithTimer(timeSpan); + + success = Execute.Assertion + .ForCondition(remainingTime >= TimeSpan.Zero) .BecauseOf(because, becauseArgs) .FailWith("Expected {context:task} to complete within {0}{reason}.", timeSpan); + + if (success) + { + bool completesWithinTimeout = await CompletesWithinTimeoutAsync(task, remainingTime); + Execute.Assertion + .ForCondition(completesWithinTimeout) + .BecauseOf(because, becauseArgs) + .FailWith("Expected {context:task} to complete within {0}{reason}.", timeSpan); + } + + TResult result = task.IsCompleted ? task.Result : default; + return new AndWhichConstraint, TResult>(this, result); } - TResult result = task.IsCompleted ? task.Result : default; - return new AndWhichConstraint, TResult>(this, result); + return new AndWhichConstraint, TResult>(this, default(TResult)); } /// @@ -84,21 +82,20 @@ public GenericAsyncFunctionAssertions(Func> subject, IExtractExcep .BecauseOf(because, becauseArgs) .FailWith("Expected {context} not to throw{reason}, but found ."); - if (!success) + if (success) { - return new AndWhichConstraint, TResult>(this, default(TResult)); + try + { + TResult result = await Subject.Invoke(); + return new AndWhichConstraint, TResult>(this, result); + } + catch (Exception exception) + { + _ = NotThrowInternal(exception, because, becauseArgs); + } } - try - { - TResult result = await Subject.Invoke(); - return new AndWhichConstraint, TResult>(this, result); - } - catch (Exception exception) - { - NotThrowInternal(exception, because, becauseArgs); - return new AndWhichConstraint, TResult>(this, default(TResult)); - } + return new AndWhichConstraint, TResult>(this, default(TResult)); } /// @@ -135,40 +132,39 @@ public GenericAsyncFunctionAssertions(Func> subject, IExtractExcep .BecauseOf(because, becauseArgs) .FailWith("Expected {context} not to throw any exceptions after {0}{reason}, but found .", waitTime); - if (!success) - { - var result = new AndWhichConstraint, TResult>(this, default(TResult)); - return Task.FromResult(result); - } - - return AssertionTaskAsync(); - - async Task, TResult>> AssertionTaskAsync() + if (success) { - TimeSpan? invocationEndTime = null; - Exception exception = null; - ITimer timer = Clock.StartTimer(); + return AssertionTaskAsync(); - while (invocationEndTime is null || invocationEndTime < waitTime) + async Task, TResult>> AssertionTaskAsync() { - try - { - TResult result = await Subject.Invoke(); - return new AndWhichConstraint, TResult>(this, result); - } - catch (Exception ex) + TimeSpan? invocationEndTime = null; + Exception exception = null; + ITimer timer = Clock.StartTimer(); + + while (invocationEndTime is null || invocationEndTime < waitTime) { - exception = ex; - await Clock.DelayAsync(pollInterval, CancellationToken.None); - invocationEndTime = timer.Elapsed; + try + { + TResult result = await Subject.Invoke(); + return new AndWhichConstraint, TResult>(this, result); + } + catch (Exception ex) + { + exception = ex; + await Clock.DelayAsync(pollInterval, CancellationToken.None); + invocationEndTime = timer.Elapsed; + } } - } - Execute.Assertion - .BecauseOf(because, becauseArgs) - .FailWith("Did not expect any exceptions after {0}{reason}, but found {1}.", waitTime, exception); + Execute.Assertion + .BecauseOf(because, becauseArgs) + .FailWith("Did not expect any exceptions after {0}{reason}, but found {1}.", waitTime, exception); - return new AndWhichConstraint, TResult>(this, default(TResult)); + return new AndWhichConstraint, TResult>(this, default(TResult)); + } } + + return Task.FromResult(new AndWhichConstraint, TResult>(this, default(TResult))); } } diff --git a/Src/FluentAssertions/Specialized/TaskCompletionSourceAssertions.cs b/Src/FluentAssertions/Specialized/TaskCompletionSourceAssertions.cs index ae6a61ae83..463cb0b754 100644 --- a/Src/FluentAssertions/Specialized/TaskCompletionSourceAssertions.cs +++ b/Src/FluentAssertions/Specialized/TaskCompletionSourceAssertions.cs @@ -124,18 +124,18 @@ public async Task, T>> Comp .BecauseOf(because, becauseArgs) .FailWith("Expected {context} to complete within {0}{reason}, but found .", timeSpan); - if (!success) + if (success) { - return new AndWhichConstraint, T>(this, default(T)); + bool completesWithinTimeout = await CompletesWithinTimeoutAsync(subject.Task, timeSpan); + Execute.Assertion + .ForCondition(completesWithinTimeout) + .BecauseOf(because, becauseArgs) + .FailWith("Expected {context:task} to complete within {0}{reason}.", timeSpan); + T result = subject.Task.IsCompleted ? subject.Task.Result : default; + return new AndWhichConstraint, T>(this, result); } - bool completesWithinTimeout = await CompletesWithinTimeoutAsync(subject.Task, timeSpan); - Execute.Assertion - .ForCondition(completesWithinTimeout) - .BecauseOf(because, becauseArgs) - .FailWith("Expected {context:task} to complete within {0}{reason}.", timeSpan); - T result = subject.Task.IsCompleted ? subject.Task.Result : default; - return new AndWhichConstraint, T>(this, result); + return new AndWhichConstraint, T>(this, default(T)); } ///