Skip to content

Commit

Permalink
Added Support for await keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
odytrice committed May 23, 2018
1 parent 19d56e6 commit b4dde16
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 1 deletion.
66 changes: 66 additions & 0 deletions src/Operation/Async.cs
Original file line number Diff line number Diff line change
@@ -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<T> GetAwaiter<T>(this Operation<T> operation)
{
return new OperationAwaiter<T>(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<T> : INotifyCompletion
{
private Operation<T> _operation;

public OperationAwaiter(Operation<T> operation)
{
_operation = operation;
}
public bool IsCompleted => true;

public void OnCompleted(Action continuation)
{
continuation();
}

public T GetResult()
{
return _operation.Unwrap();
}
}
}
61 changes: 61 additions & 0 deletions test/Tests/AsyncTests.cs
Original file line number Diff line number Diff line change
@@ -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<int>("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<int> AsyncOpMethod()
{
await Operation.Fail<int>("An Error Occured");
return 1;
}
}

[TestMethod]
public async Task AwaitNonGenericOperation()
{
var operation = Operation.Success();
await operation;
}
}
}
2 changes: 1 addition & 1 deletion test/Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0-preview-20170106-08" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.2" />
<PackageReference Include="MSTest.TestAdapter" Version="1.1.18" />
<PackageReference Include="MSTest.TestFramework" Version="1.1.18" />
</ItemGroup>
Expand Down

0 comments on commit b4dde16

Please sign in to comment.