Skip to content

Commit

Permalink
Added support to write test using async / await
Browse files Browse the repository at this point in the history
  • Loading branch information
marwijn committed Dec 6, 2011
1 parent 5a97871 commit f80a702
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Raven.Tests.Silverlight.UnitTestProvider.Example/Tests.cs
Expand Up @@ -41,6 +41,17 @@ public IEnumerable<Task> NewWay()

Assert.AreEqual(42, another.Result);
}

[TestMethod]
[Asynchronous]
public async Task AnotherWay()
{
await SomeTestTask.DoSomethingAsync();
int result = await SomeTestTask.DoSomethingAsync();
await Delay(100);

Assert.AreEqual(42, result);
}
}

public static class SomeTestTask
Expand Down
11 changes: 11 additions & 0 deletions Raven.Tests.Silverlight.UnitTestProvider/AsynchronousTaskTest.cs
Expand Up @@ -21,6 +21,17 @@ public void ExecuteTest(MethodInfo test)
ExecuteTestStep(enumerator);
}

public void ExecuteTaskTest(MethodInfo test)
{
var task = (Task)test.Invoke(this, new object[] { });
EnqueueConditional(() => task.IsCompleted || task.IsFaulted);
EnqueueCallback(() =>
{
if (task.IsFaulted) throw task.Exception.InnerException;
});
EnqueueTestComplete();
}

private void ExecuteTestStep(IEnumerator<Task> enumerator)
{
bool moveNextSucceeded = enumerator.MoveNext();
Expand Down
6 changes: 6 additions & 0 deletions Raven.Tests.Silverlight.UnitTestProvider/TestMethod.cs
Expand Up @@ -171,6 +171,12 @@ public virtual IEnumerable<Attribute> GetDynamicAttributes()

public virtual void Invoke(object instance)
{
if (methodInfo.ReturnType == typeof(Task) && typeof(AsynchronousTaskTest).IsAssignableFrom(instance.GetType()))
{
var executor = instance.GetType().GetMethod("ExecuteTaskTest");
executor.Invoke(instance, new[] { methodInfo });
}
else
if (ReturnTypeForAsyncTaskTest.IsAssignableFrom(methodInfo.ReturnType) && typeof(AsynchronousTaskTest).IsAssignableFrom(instance.GetType()))
{
var executor = instance.GetType().GetMethod("ExecuteTest");
Expand Down

0 comments on commit f80a702

Please sign in to comment.