Skip to content

Commit

Permalink
Add more WhenLater alternatives for the When with deferred execution (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ronaldkroon authored and dennisdoomen committed Jan 8, 2018
1 parent da02712 commit f27c017
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Source/Core/Chill.Shared/GivenWhenThen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public Func<TResult> WhenAction
}
}

/// <summary>
/// Records the asynchronous action that will trigger the actual test, when later executing the <see cref="WhenAction"/>
/// </summary>
protected void WhenLater(Func<TResult> whenFunc)
{
When(whenFunc, deferredExecution: true);
}

/// <summary>
/// Records the action that will trigger the actual test
/// </summary>
Expand All @@ -65,6 +73,14 @@ internal override void TriggerTest(bool expectExceptions)
TriggerTest(() => result = whenAction(), expectExceptions);
}

/// <summary>
/// Records the asynchronous action that will trigger the actual test, when later executing the <see cref="WhenAction"/>
/// </summary>
protected void WhenLater(Func<Task<TResult>> whenFunc)
{
When(() => whenFunc.ExecuteInDefaultSynchronizationContext(), deferredExecution: true);
}

/// <summary>
/// Records the asynchronous action that will trigger the actual test
/// </summary>
Expand Down Expand Up @@ -116,6 +132,14 @@ public Action WhenAction
}
}

/// <summary>
/// Records the asynchronous action that will trigger the actual test, when later executing the <see cref="WhenAction"/>
/// </summary>
protected void WhenLater(Action whenAction)
{
When(whenAction, deferredExecution: true);
}

/// <summary>
/// Records the action that will trigger the actual test
/// </summary>
Expand All @@ -137,6 +161,14 @@ public void When(Action whenAction, bool? deferredExecution = null)

}

/// <summary>
/// Records the asynchronous action that will trigger the actual test, when later executing the <see cref="WhenAction"/>
/// </summary>
protected void WhenLater(Func<Task> whenActionAsync)
{
When(() => whenActionAsync.ExecuteInDefaultSynchronizationContext(), deferredExecution: true);
}

/// <summary>
/// Records the asynchronous action that will trigger the actual test
/// </summary>
Expand Down
60 changes: 60 additions & 0 deletions Source/Core/Chill.Tests.Shared/AsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,35 @@ public void Then_it_should_evaluate_the_sync_code_synchronously()
}
}

public class When_later_async_act_throws : GivenWhenThen
{
public When_later_async_act_throws()
{
WhenLater(
async () =>
{
#if NET45
await Task.Delay(10);
#else
await TaskEx.Delay(10);
#endif
throw new ApplicationException();
});
}

[Fact]
public void Then_the_exception_should_be_observed()
{
WhenAction.ShouldThrow<ApplicationException>();
}

[Fact]
public void Then_the_exception_should_not_be_wrapped_to_an_aggregate_exception()
{
WhenAction.ShouldNotThrow<AggregateException>();
}
}

public class When_a_deferred_async_act_throws : GivenWhenThen
{
public When_a_deferred_async_act_throws()
Expand Down Expand Up @@ -66,6 +95,37 @@ public void Then_the_exception_should_not_be_wrapped_to_an_aggregate_exception()
}
}

public class When_later_async_act_throws_in_a_test_with_result : GivenWhenThen<object>
{
public When_later_async_act_throws_in_a_test_with_result()
{
WhenLater((Func<Task<object>>) (
async () =>
{
#if NET45
await Task.Delay(10);
#else
await TaskEx.Delay(10);
#endif
throw new ApplicationException();
}));
}

[Fact]
public void Then_the_exception_should_be_observed()
{
Action action = () => WhenAction();
action.ShouldThrow<ApplicationException>();
}

[Fact]
public void Then_the_exception_should_not_be_wrapped_to_an_aggregate_exception()
{
Action action = () => WhenAction();
action.ShouldNotThrow<AggregateException>();
}
}

public class When_a_deferred_async_act_throws_in_a_test_with_result : GivenWhenThen<object>
{
public When_a_deferred_async_act_throws_in_a_test_with_result()
Expand Down
39 changes: 39 additions & 0 deletions Source/Core/Chill.Tests.Shared/StateBuilderSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public void When_setting_items_directly_then_it_should_be_found()
When(() => UseThe(_expectedAClass));
}

[Fact]
public void When_executing_later_then_action_is_not_executed_immediately()
{
WhenLater(() => { throw new Exception(); });
}

[Fact]
public void When_deferring_execution_then_action_is_not_executed_immediately()
{
Expand All @@ -46,6 +52,14 @@ public void When_setting_multiple_items_then_they_should_be_set()
All<AClass>().Should().BeEquivalentTo(new[] {new AClass("1"), new AClass("2"),});
}

[Fact]
public void When_exception_is_thrown_in_async_method_in_later_execution_expected_exception_is_filled()
{
Func<Task> whenActionASync = () => Task.Factory.StartNew(() => { throw new AbandonedMutexException(); });
WhenLater(whenActionASync);
CaughtException.Should().BeOfType<AbandonedMutexException>();
}

[Fact]
public void When_exception_is_thrown_in_async_method_in_deferred_execution_expected_exception_is_filled()
{
Expand All @@ -54,6 +68,13 @@ public void When_exception_is_thrown_in_async_method_in_deferred_execution_expec
CaughtException.Should().BeOfType<AbandonedMutexException>();
}

[Fact]
public void When_exception_is_thrown_in_later_execution_expected_exception_is_filled()
{
Action whenActionASync = () => { throw new AbandonedMutexException(); };
WhenLater(whenActionASync);
CaughtException.Should().BeOfType<AbandonedMutexException>();
}
[Fact]
public void When_exception_is_thrown_in_deferred_execution_expected_exception_is_filled()
{
Expand All @@ -69,6 +90,24 @@ public void When_deferring_execution_then_whenaction_can_be_used_to_test_for_exc
WhenAction.ShouldThrow<AbandonedMutexException>();
}

[Fact]
public void When_executing_later_then_whenaction_can_be_used_to_test_for_exceptions()
{
WhenLater(() => { throw new AbandonedMutexException(); });
WhenAction.ShouldThrow<AbandonedMutexException>();
}

[Fact]
public void When_later_no_exception_would_be_thrown_but_caught_exception_is_used_it_should_throw()
{
WhenLater(() => { });
Action a = () =>
{
Exception e = CaughtException;
};
a.ShouldThrow<InvalidOperationException>().WithMessage("Expected exception but no exception was thrown");
}

[Fact]
public void When_no_exception_is_thrown_but_expected_exception_is_used_then_caughtexceptoin_throws()
{
Expand Down

0 comments on commit f27c017

Please sign in to comment.