Skip to content

Commit

Permalink
Rework on async function assertions (#1079)
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisdoomen committed Jun 7, 2019
1 parent e67e913 commit 9852f6a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
Expand Up @@ -33,7 +33,7 @@ public GenericAsyncFunctionAssertions(Func<Task<TResult>> subject, IExtractExcep
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see cref="because" />.
/// </param>
public AndWhichConstraint<GenericAsyncFunctionAssertions<TResult>, Task<TResult>> CompleteWithin(
public AndWhichConstraint<GenericAsyncFunctionAssertions<TResult>, TResult> CompleteWithin(
TimeSpan timeSpan, string because = "", params object[] becauseArgs)
{
Task<TResult> task = subject();
Expand All @@ -44,7 +44,7 @@ public GenericAsyncFunctionAssertions(Func<Task<TResult>> subject, IExtractExcep
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:task} to complete within {0}{reason}.", timeSpan);

return new AndWhichConstraint<GenericAsyncFunctionAssertions<TResult>, Task<TResult>>(this, task);
return new AndWhichConstraint<GenericAsyncFunctionAssertions<TResult>, TResult>(this, task.Result);
}

/// <summary>
Expand All @@ -58,7 +58,7 @@ public GenericAsyncFunctionAssertions(Func<Task<TResult>> subject, IExtractExcep
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see cref="because" />.
/// </param>
public async Task<AndWhichConstraint<GenericAsyncFunctionAssertions<TResult>, Task<TResult>>> CompleteWithinAsync(
public async Task<AndWhichConstraint<GenericAsyncFunctionAssertions<TResult>, TResult>> CompleteWithinAsync(
TimeSpan timeSpan, string because = "", params object[] becauseArgs)
{
using (var timeoutCancellationTokenSource = new CancellationTokenSource())
Expand All @@ -79,7 +79,7 @@ public GenericAsyncFunctionAssertions(Func<Task<TResult>> subject, IExtractExcep
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:task} to complete within {0}{reason}.", timeSpan);

return new AndWhichConstraint<GenericAsyncFunctionAssertions<TResult>, Task<TResult>>(this, task);
return new AndWhichConstraint<GenericAsyncFunctionAssertions<TResult>, TResult>(this, task.Result);
}
}
}
Expand Down
Expand Up @@ -32,7 +32,7 @@ public class NonGenericAsyncFunctionAssertions : AsyncFunctionAssertions
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see cref="because" />.
/// </param>
public AndWhichConstraint<AsyncFunctionAssertions, Task> CompleteWithin(
public AndConstraint<AsyncFunctionAssertions> CompleteWithin(
TimeSpan timeSpan, string because = "", params object[] becauseArgs)
{
Task task = Subject();
Expand All @@ -43,7 +43,7 @@ public class NonGenericAsyncFunctionAssertions : AsyncFunctionAssertions
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:task} to complete within {0}{reason}.", timeSpan);

return new AndWhichConstraint<AsyncFunctionAssertions, Task>(this, task);
return new AndConstraint<AsyncFunctionAssertions>(this);
}

/// <summary>
Expand All @@ -57,28 +57,29 @@ public class NonGenericAsyncFunctionAssertions : AsyncFunctionAssertions
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see cref="because" />.
/// </param>
public async Task<AndWhichConstraint<AsyncFunctionAssertions, Task>> CompleteWithinAsync(
public async Task<AndConstraint<AsyncFunctionAssertions>> CompleteWithinAsync(
TimeSpan timeSpan, string because = "", params object[] becauseArgs)
{
var timeoutCancellationTokenSource = new CancellationTokenSource();
using (var timeoutCancellationTokenSource = new CancellationTokenSource())
{
Task task = Subject();

Task task = Subject();
Task completedTask =
await Task.WhenAny(task, clock.DelayAsync(timeSpan, timeoutCancellationTokenSource.Token));

Task completedTask =
await Task.WhenAny(task, clock.DelayAsync(timeSpan, timeoutCancellationTokenSource.Token));
if (completedTask == task)
{
timeoutCancellationTokenSource.Cancel();
await completedTask;
}

if (completedTask == task)
{
timeoutCancellationTokenSource.Cancel();
await completedTask;
}
Execute.Assertion
.ForCondition(completedTask == task)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:task} to complete within {0}{reason}.", timeSpan);

Execute.Assertion
.ForCondition(completedTask == task)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:task} to complete within {0}{reason}.", timeSpan);

return new AndWhichConstraint<AsyncFunctionAssertions, Task>(this, task);
return new AndConstraint<AsyncFunctionAssertions>(this);
}
}
}
}
4 changes: 2 additions & 2 deletions Tests/Shared.Specs/TaskOfTAssertionSpecs.cs
Expand Up @@ -25,7 +25,7 @@ public void When_task_completes_fast_it_should_succeed()
Func<Task<int>> func = () => taskFactory.Task;
func.Should(timer).CompleteWithin(100.Milliseconds())
.Which.Result.Should().Be(42);
.Which.Should().Be(42);
};

taskFactory.SetResult(42);
Expand Down Expand Up @@ -81,7 +81,7 @@ public void When_task_completes_fast_async_it_should_succeed()
Func<Task<int>> func = () => taskFactory.Task;
(await func.Should(timer).CompleteWithinAsync(100.Milliseconds()))
.Which.Result.Should().Be(42);
.Which.Should().Be(42);
};

taskFactory.SetResult(42);
Expand Down

0 comments on commit 9852f6a

Please sign in to comment.