Skip to content

Commit 42521c4

Browse files
committed
Add ability in IAwaitableFactory to create result expression
1 parent 187902c commit 42521c4

File tree

7 files changed

+69
-0
lines changed

7 files changed

+69
-0
lines changed

src/Moq/Async/AwaitExpression.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors.
2+
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.
3+
4+
using System;
5+
using System.Diagnostics;
6+
using System.Linq.Expressions;
7+
8+
namespace Moq.Async
9+
{
10+
internal sealed class AwaitExpression : Expression
11+
{
12+
private readonly IAwaitableFactory awaitableFactory;
13+
private readonly Expression operand;
14+
15+
public AwaitExpression(Expression operand, IAwaitableFactory awaitableFactory)
16+
{
17+
Debug.Assert(awaitableFactory != null);
18+
Debug.Assert(operand != null);
19+
20+
this.awaitableFactory = awaitableFactory;
21+
this.operand = operand;
22+
}
23+
24+
public override bool CanReduce => false;
25+
26+
public override ExpressionType NodeType => ExpressionType.Extension;
27+
28+
public Expression Operand => this.operand;
29+
30+
public override Type Type => this.awaitableFactory.ResultType;
31+
32+
public override string ToString()
33+
{
34+
return this.awaitableFactory.ResultType == typeof(void) ? $"await {this.operand}"
35+
: $"(await {this.operand})";
36+
}
37+
38+
protected override Expression VisitChildren(ExpressionVisitor visitor) => this;
39+
}
40+
}

src/Moq/Async/AwaitableFactory`1.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Diagnostics;
77
using System.Linq;
8+
using System.Linq.Expressions;
89

910
namespace Moq.Async
1011
{
@@ -44,6 +45,11 @@ object IAwaitableFactory.CreateFaulted(IEnumerable<Exception> exceptions)
4445
return this.CreateFaulted(exceptions);
4546
}
4647

48+
Expression IAwaitableFactory.CreateResultExpression(Expression awaitableExpression)
49+
{
50+
return new AwaitExpression(awaitableExpression, this);
51+
}
52+
4753
bool IAwaitableFactory.TryGetResult(object awaitable, out object result)
4854
{
4955
Debug.Assert(awaitable is TAwaitable);

src/Moq/Async/AwaitableFactory`2.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Diagnostics;
77
using System.Linq;
8+
using System.Linq.Expressions;
89

910
namespace Moq.Async
1011
{
@@ -46,6 +47,8 @@ object IAwaitableFactory.CreateFaulted(IEnumerable<Exception> exceptions)
4647

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

50+
public abstract Expression CreateResultExpression(Expression awaitableExpression);
51+
4952
bool IAwaitableFactory.TryGetResult(object awaitable, out object result)
5053
{
5154
Debug.Assert(awaitable is TAwaitable);

src/Moq/Async/IAwaitableFactory.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Linq.Expressions;
67

78
namespace Moq.Async
89
{
@@ -16,6 +17,8 @@ internal interface IAwaitableFactory
1617

1718
object CreateFaulted(IEnumerable<Exception> exceptions);
1819

20+
Expression CreateResultExpression(Expression awaitableExpression);
21+
1922
bool TryGetResult(object awaitable, out object result);
2023
}
2124
}

src/Moq/Async/TaskFactory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Linq.Expressions;
67
using System.Reflection;
78
using System.Threading.Tasks;
89

src/Moq/Async/TaskFactory`1.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Linq.Expressions;
67
using System.Threading.Tasks;
78

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

32+
public override Expression CreateResultExpression(Expression awaitableExpression)
33+
{
34+
return Expression.MakeMemberAccess(
35+
awaitableExpression,
36+
typeof(Task<TResult>).GetProperty(nameof(Task<TResult>.Result)));
37+
}
38+
3139
public override bool TryGetResult(Task<TResult> task, out TResult result)
3240
{
3341
if (task.Status == TaskStatus.RanToCompletion)

src/Moq/Async/ValueTaskFactory`1.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Linq.Expressions;
67
using System.Threading.Tasks;
78

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

32+
public override Expression CreateResultExpression(Expression awaitableExpression)
33+
{
34+
return Expression.MakeMemberAccess(
35+
awaitableExpression,
36+
typeof(ValueTask<TResult>).GetProperty(nameof(ValueTask<TResult>.Result)));
37+
}
38+
3139
public override bool TryGetResult(ValueTask<TResult> valueTask, out TResult result)
3240
{
3341
if (valueTask.IsCompletedSuccessfully)

0 commit comments

Comments
 (0)