Skip to content

Commit

Permalink
Added support for async Givens
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisdoomen committed Sep 5, 2016
1 parent 427547c commit 1a4c989
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 23 deletions.
1 change: 1 addition & 0 deletions Source/Core/Chill.Shared/Chill.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<Compile Include="$(MSBuildThisFileDirectory)StateBuilders\IStoreStateBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)StateBuilders\StoreStateBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)StateBuilders\StoreStateBuilderExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TaskExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TestBase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TestFor.cs" />
</ItemGroup>
Expand Down
19 changes: 0 additions & 19 deletions Source/Core/Chill.Shared/GivenSubject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,23 +169,4 @@ public void Given(Action a)
a();
}
}

public static class TaskExtensions
{
public static void WaitAndFlattenExceptions(this Task t)
{
try
{
#if NET45
Task.Run(() => t.Wait()).Wait();
#else
t.Wait();
#endif
}
catch (AggregateException aggregateException)
{
throw aggregateException.Flatten();
}
}
}
}
29 changes: 26 additions & 3 deletions Source/Core/Chill.Shared/GivenWhenThen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,19 @@ protected void When(Func<Task<TResult>> whenFunc, bool? deferedExecution = null)
#if NET45
When(() => Task.Run(whenFunc).Result, deferedExecution);
#else
When(() => Task.Factory.StartNew(whenFunc).Result, deferedExecution);
When(() => Task.Factory.StartNew(() => whenFunc().Result).Result, deferedExecution);
#endif
}

/// <summary>
/// Records an asynchronous precondition.
/// </summary>
public void Given(Func<Task> givenActionAsync)
{
#if NET45
Given(() => Task.Run(givenActionAsync).Wait());
#else
Given(() => Task.Factory.StartNew(() => givenActionAsync().Wait()).Wait());
#endif
}

Expand All @@ -88,7 +100,6 @@ public void Given(Action a)
EnsureContainer();
a();
}

}

/// <summary>
Expand Down Expand Up @@ -144,13 +155,25 @@ public void When(Func<Task> whenActionAsync, bool? deferedExecution = null)
#if NET45
When(() => Task.Run(whenActionAsync).Wait(), deferedExecution);
#else
When(() => whenActionAsync().Wait(), deferedExecution);
When(() => Task.Factory.StartNew(() => whenActionAsync().Wait()).Wait(), deferedExecution);
#endif
}

internal override void TriggerTest(bool expectExceptions)
{
TriggerTest(() => whenAction(), expectExceptions);
}

/// <summary>
/// Records an asynchronous precondition.
/// </summary>
public void Given(Func<Task> givenActionAsync)
{
#if NET45
Given(() => Task.Run(givenActionAsync).Wait());
#else
Given(() => Task.Factory.StartNew(() => givenActionAsync().Wait()).Wait());
#endif
}

/// <summary>
Expand Down
24 changes: 24 additions & 0 deletions Source/Core/Chill.Shared/TaskExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Threading.Tasks;

namespace Chill
{
internal static class TaskExtensions
{
public static void WaitAndFlattenExceptions(this Task t)
{
try
{
#if NET45
Task.Run(() => t.Wait()).Wait();
#else
Task.Factory.StartNew(t.Wait).Wait();
#endif
}
catch (AggregateException aggregateException)
{
throw aggregateException.Flatten();
}
}
}
}
28 changes: 27 additions & 1 deletion Source/Core/Chill.Tests.Shared/AsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using FluentAssertions;
using Xunit;

namespace Chill.Net45.Tests
namespace Chill.Tests.Shared
{
namespace AsyncSpecs
{
Expand All @@ -28,6 +28,32 @@ public When_an_async_act_is_used()
});
}

[Fact]
public void Then_it_should_evaluate_the_sync_code_synchronously()
{
results.Should().HaveCount(1000);
}
}
public class When_an_async_arrange_is_used : GivenWhenThen
{
private BlockingCollection<int> results = new BlockingCollection<int>();

public When_an_async_arrange_is_used()
{
Given(async () =>
{
foreach (int key in Enumerable.Range(0, 1000))
{
#if NET45
await Task.Delay(10);
#else
await TaskEx.Delay(10);
#endif
results.Add(key);
}
});
}

[Fact]
public void Then_it_should_evaluate_the_sync_code_synchronously()
{
Expand Down

0 comments on commit 1a4c989

Please sign in to comment.