From 1a4c989b0045b43317fa8b7a75b432bb5a7b3968 Mon Sep 17 00:00:00 2001 From: Dennis Doomen Date: Mon, 5 Sep 2016 20:38:07 +0200 Subject: [PATCH] Added support for async Givens --- .../Core/Chill.Shared/Chill.Shared.projitems | 1 + Source/Core/Chill.Shared/GivenSubject.cs | 19 ------------ Source/Core/Chill.Shared/GivenWhenThen.cs | 29 +++++++++++++++++-- Source/Core/Chill.Shared/TaskExtensions.cs | 24 +++++++++++++++ Source/Core/Chill.Tests.Shared/AsyncSpecs.cs | 28 +++++++++++++++++- 5 files changed, 78 insertions(+), 23 deletions(-) create mode 100644 Source/Core/Chill.Shared/TaskExtensions.cs diff --git a/Source/Core/Chill.Shared/Chill.Shared.projitems b/Source/Core/Chill.Shared/Chill.Shared.projitems index 5b3a78a..4687c20 100644 --- a/Source/Core/Chill.Shared/Chill.Shared.projitems +++ b/Source/Core/Chill.Shared/Chill.Shared.projitems @@ -27,6 +27,7 @@ + diff --git a/Source/Core/Chill.Shared/GivenSubject.cs b/Source/Core/Chill.Shared/GivenSubject.cs index 7ba124e..6e4376f 100644 --- a/Source/Core/Chill.Shared/GivenSubject.cs +++ b/Source/Core/Chill.Shared/GivenSubject.cs @@ -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(); - } - } - } } \ No newline at end of file diff --git a/Source/Core/Chill.Shared/GivenWhenThen.cs b/Source/Core/Chill.Shared/GivenWhenThen.cs index e65ebe7..fc6ce3e 100644 --- a/Source/Core/Chill.Shared/GivenWhenThen.cs +++ b/Source/Core/Chill.Shared/GivenWhenThen.cs @@ -75,7 +75,19 @@ protected void When(Func> 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 + } + + /// + /// Records an asynchronous precondition. + /// + public void Given(Func givenActionAsync) + { +#if NET45 + Given(() => Task.Run(givenActionAsync).Wait()); +#else + Given(() => Task.Factory.StartNew(() => givenActionAsync().Wait()).Wait()); #endif } @@ -88,7 +100,6 @@ public void Given(Action a) EnsureContainer(); a(); } - } /// @@ -144,13 +155,25 @@ public void When(Func 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); + } + + /// + /// Records an asynchronous precondition. + /// + public void Given(Func givenActionAsync) + { +#if NET45 + Given(() => Task.Run(givenActionAsync).Wait()); +#else + Given(() => Task.Factory.StartNew(() => givenActionAsync().Wait()).Wait()); +#endif } /// diff --git a/Source/Core/Chill.Shared/TaskExtensions.cs b/Source/Core/Chill.Shared/TaskExtensions.cs new file mode 100644 index 0000000..0ea019a --- /dev/null +++ b/Source/Core/Chill.Shared/TaskExtensions.cs @@ -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(); + } + } + } +} \ No newline at end of file diff --git a/Source/Core/Chill.Tests.Shared/AsyncSpecs.cs b/Source/Core/Chill.Tests.Shared/AsyncSpecs.cs index 66651ee..822460e 100644 --- a/Source/Core/Chill.Tests.Shared/AsyncSpecs.cs +++ b/Source/Core/Chill.Tests.Shared/AsyncSpecs.cs @@ -4,7 +4,7 @@ using FluentAssertions; using Xunit; -namespace Chill.Net45.Tests +namespace Chill.Tests.Shared { namespace AsyncSpecs { @@ -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 results = new BlockingCollection(); + + 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() {