Skip to content

Commit

Permalink
Add ability in IAwaitableFactory to create result expression
Browse files Browse the repository at this point in the history
  • Loading branch information
stakx committed Jan 1, 2021
1 parent 187902c commit 42521c4
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/Moq/Async/AwaitExpression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors.
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.

using System;
using System.Diagnostics;
using System.Linq.Expressions;

namespace Moq.Async
{
internal sealed class AwaitExpression : Expression
{
private readonly IAwaitableFactory awaitableFactory;
private readonly Expression operand;

public AwaitExpression(Expression operand, IAwaitableFactory awaitableFactory)
{
Debug.Assert(awaitableFactory != null);
Debug.Assert(operand != null);

this.awaitableFactory = awaitableFactory;
this.operand = operand;
}

public override bool CanReduce => false;

public override ExpressionType NodeType => ExpressionType.Extension;

public Expression Operand => this.operand;

public override Type Type => this.awaitableFactory.ResultType;

public override string ToString()
{
return this.awaitableFactory.ResultType == typeof(void) ? $"await {this.operand}"
: $"(await {this.operand})";
}

protected override Expression VisitChildren(ExpressionVisitor visitor) => this;
}
}
6 changes: 6 additions & 0 deletions src/Moq/Async/AwaitableFactory`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;

namespace Moq.Async
{
Expand Down Expand Up @@ -44,6 +45,11 @@ object IAwaitableFactory.CreateFaulted(IEnumerable<Exception> exceptions)
return this.CreateFaulted(exceptions);
}

Expression IAwaitableFactory.CreateResultExpression(Expression awaitableExpression)
{
return new AwaitExpression(awaitableExpression, this);
}

bool IAwaitableFactory.TryGetResult(object awaitable, out object result)
{
Debug.Assert(awaitable is TAwaitable);
Expand Down
3 changes: 3 additions & 0 deletions src/Moq/Async/AwaitableFactory`2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;

namespace Moq.Async
{
Expand Down Expand Up @@ -46,6 +47,8 @@ object IAwaitableFactory.CreateFaulted(IEnumerable<Exception> exceptions)

public abstract bool TryGetResult(TAwaitable awaitable, out TResult result);

public abstract Expression CreateResultExpression(Expression awaitableExpression);

bool IAwaitableFactory.TryGetResult(object awaitable, out object result)
{
Debug.Assert(awaitable is TAwaitable);
Expand Down
3 changes: 3 additions & 0 deletions src/Moq/Async/IAwaitableFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace Moq.Async
{
Expand All @@ -16,6 +17,8 @@ internal interface IAwaitableFactory

object CreateFaulted(IEnumerable<Exception> exceptions);

Expression CreateResultExpression(Expression awaitableExpression);

bool TryGetResult(object awaitable, out object result);
}
}
1 change: 1 addition & 0 deletions src/Moq/Async/TaskFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading.Tasks;

Expand Down
8 changes: 8 additions & 0 deletions src/Moq/Async/TaskFactory`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Moq.Async
Expand All @@ -28,6 +29,13 @@ public override Task<TResult> CreateFaulted(IEnumerable<Exception> exceptions)
return tcs.Task;
}

public override Expression CreateResultExpression(Expression awaitableExpression)
{
return Expression.MakeMemberAccess(
awaitableExpression,
typeof(Task<TResult>).GetProperty(nameof(Task<TResult>.Result)));
}

public override bool TryGetResult(Task<TResult> task, out TResult result)
{
if (task.Status == TaskStatus.RanToCompletion)
Expand Down
8 changes: 8 additions & 0 deletions src/Moq/Async/ValueTaskFactory`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Moq.Async
Expand All @@ -28,6 +29,13 @@ public override ValueTask<TResult> CreateFaulted(IEnumerable<Exception> exceptio
return new ValueTask<TResult>(tcs.Task);
}

public override Expression CreateResultExpression(Expression awaitableExpression)
{
return Expression.MakeMemberAccess(
awaitableExpression,
typeof(ValueTask<TResult>).GetProperty(nameof(ValueTask<TResult>.Result)));
}

public override bool TryGetResult(ValueTask<TResult> valueTask, out TResult result)
{
if (valueTask.IsCompletedSuccessfully)
Expand Down

0 comments on commit 42521c4

Please sign in to comment.