Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita2aa665

Browse files
committed
Fixes#955: Support F# async natively
1 parentdc62122 commita2aa665

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

‎src/xunit.execution/Sdk/Frameworks/Runners/TestInvoker.cs‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
usingSystem;
2+
usingSystem.Linq;
23
usingSystem.Reflection;
34
usingSystem.Security;
45
usingSystem.Threading;
@@ -17,6 +18,8 @@ namespace Xunit.Sdk
1718
publicabstractclassTestInvoker<TTestCase>
1819
whereTTestCase:ITestCase
1920
{
21+
staticMethodInfostartAsTaskOpenGenericMethod;
22+
2023
/// <summary>
2124
/// Initializes a new instance of the <see cref="TestInvoker{TTestCase}"/> class.
2225
/// </summary>
@@ -146,6 +149,36 @@ protected virtual Task BeforeTestMethodInvokedAsync()
146149
protectedvirtualobjectCallTestMethod(objecttestClassInstance)
147150
=>TestMethod.Invoke(testClassInstance,TestMethodArguments);
148151

152+
/// <summary>
153+
/// Given an object, will determine if it is an instance of <see cref="Task"/> (in which case, it is
154+
/// directly returned), or an instance of <see cref="T:Microsoft.FSharp.Control.FSharpAsync`1"/>
155+
/// (in which case it is converted), or neither (in which case <c>null</c> is returned).
156+
/// </summary>
157+
/// <param name="obj">The object to convert</param>
158+
publicstaticTaskGetTaskFromResult(objectobj)
159+
{
160+
if(obj==null)
161+
returnnull;
162+
163+
vartask=objasTask;
164+
if(task!=null)
165+
returntask;
166+
167+
vartype=obj.GetType();
168+
if(type.IsGenericType()&&type.GetGenericTypeDefinition().FullName=="Microsoft.FSharp.Control.FSharpAsync`1")
169+
{
170+
if(startAsTaskOpenGenericMethod==null)
171+
startAsTaskOpenGenericMethod=type.GetAssembly().GetType("Microsoft.FSharp.Control.FSharpAsync")
172+
.GetRuntimeMethods()
173+
.FirstOrDefault(m=>m.Name=="StartAsTask");
174+
175+
returnstartAsTaskOpenGenericMethod.MakeGenericMethod(type.GetGenericArguments()[0])
176+
.Invoke(null,new[]{obj,null,null})asTask;
177+
}
178+
179+
returnnull;
180+
}
181+
149182
/// <summary>
150183
/// Creates the test class (if necessary), and invokes the test method.
151184
/// </summary>
@@ -222,7 +255,7 @@ await Aggregator.RunAsync(
222255
else
223256
{
224257
varresult=CallTestMethod(testClassInstance);
225-
vartask=resultasTask;
258+
vartask=GetTaskFromResult(result);
226259
if(task!=null)
227260
awaittask;
228261
else

‎test/test.xunit.runner.utility/Frameworks/v2/Xunit2Tests.cs‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,36 @@ let TestMethod (x:int) =
232232
);
233233
}
234234
}
235+
236+
[Fact]
237+
publicvoidSupportsAsyncReturningMethods()
238+
{
239+
stringcode=@"
240+
module FSharpTests
241+
242+
open Xunit
243+
244+
[<Fact>]
245+
let AsyncFailing() =
246+
async {
247+
do! Async.Sleep(10)
248+
Assert.True(false)
249+
}
250+
";
251+
252+
using(varassembly=FSharpAcceptanceTestV2Assembly.Create(code))
253+
using(varcontroller=newTestableXunit2(assembly.FileName,null,true))
254+
{
255+
varsink=newSpyMessageSink<ITestAssemblyFinished>();
256+
257+
controller.RunAll(sink,discoveryOptions:TestFrameworkOptions.ForDiscovery(),executionOptions:TestFrameworkOptions.ForExecution());
258+
sink.Finished.WaitOne();
259+
260+
varfailures=sink.Messages.OfType<ITestFailed>();
261+
varfailure=Assert.Single(failures);
262+
Assert.Equal("FSharpTests.AsyncFailing",failure.TestCase.DisplayName);
263+
}
264+
}
235265
}
236266

237267
classTestableXunit2:Xunit2

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp