From b4dde16aa1286d9a9f6a77489a14a4165ee8351c Mon Sep 17 00:00:00 2001 From: Ody Mbegbu Date: Wed, 23 May 2018 15:52:54 +0100 Subject: [PATCH] Added Support for await keyword --- src/Operation/Async.cs | 66 ++++++++++++++++++++++++++++++++++++++++ test/Tests/AsyncTests.cs | 61 +++++++++++++++++++++++++++++++++++++ test/Tests/Tests.csproj | 2 +- 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 src/Operation/Async.cs create mode 100644 test/Tests/AsyncTests.cs diff --git a/src/Operation/Async.cs b/src/Operation/Async.cs new file mode 100644 index 0000000..da1ab81 --- /dev/null +++ b/src/Operation/Async.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; + +namespace System +{ + + public static partial class OperationExtensions + { + public static OperationAwaiter GetAwaiter(this Operation operation) + { + return new OperationAwaiter(operation); + } + + public static OperationAwaiter GetAwaiter(this Operation operation) + { + return new OperationAwaiter(operation); + } + + } + + public class OperationAwaiter : INotifyCompletion + { + private Operation _operation; + + public OperationAwaiter(Operation operation) + { + _operation = operation; + } + public bool IsCompleted => true; + + public void OnCompleted(Action continuation) + { + continuation(); + } + + public void GetResult() + { + _operation.Unwrap(); + } + } + + + public class OperationAwaiter : INotifyCompletion + { + private Operation _operation; + + public OperationAwaiter(Operation operation) + { + _operation = operation; + } + public bool IsCompleted => true; + + public void OnCompleted(Action continuation) + { + continuation(); + } + + public T GetResult() + { + return _operation.Unwrap(); + } + } +} \ No newline at end of file diff --git a/test/Tests/AsyncTests.cs b/test/Tests/AsyncTests.cs new file mode 100644 index 0000000..01d7403 --- /dev/null +++ b/test/Tests/AsyncTests.cs @@ -0,0 +1,61 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace Tests +{ + [TestClass] + public class AsyncTests + { + [TestMethod] + public async Task AwaitSuccessfulOperation() + { + var operation = Operation.Success(10); + + var result = await operation; + + Assert.AreEqual(10, result); + } + + [TestMethod] + public async Task AwaitFailedOperation() + { + var operation = Operation.Fail("An Error Occured"); + + bool failed = false; + try + { + var result = await operation; + } + catch + { + failed = true; + } + + Assert.IsTrue(failed, "Operation Not Throwing Exception"); + } + + [TestMethod] + public void TestErrorCapturedInTask() + { + var result = AsyncOpMethod(); + + Assert.IsTrue(result.IsFaulted); + + async Task AsyncOpMethod() + { + await Operation.Fail("An Error Occured"); + return 1; + } + } + + [TestMethod] + public async Task AwaitNonGenericOperation() + { + var operation = Operation.Success(); + await operation; + } + } +} diff --git a/test/Tests/Tests.csproj b/test/Tests/Tests.csproj index 9efd5b7..565653d 100644 --- a/test/Tests/Tests.csproj +++ b/test/Tests/Tests.csproj @@ -17,7 +17,7 @@ - +