Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Allow Task-returning test methods without async keyword #510

Merged
merged 3 commits into from
Nov 7, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ internal static bool HasCorrectTimeout(this MethodInfo method)
/// <returns>True if the method has a void/task return type..</returns>
internal static bool IsVoidOrTaskReturnType(this MethodInfo method)
{
return method.GetAsyncTypeName() == null ? ReflectHelper.MatchReturnType(method, typeof(void))
: ReflectHelper.MatchReturnType(method, typeof(Task));
return ReflectHelper.MatchReturnType(method, typeof(Task))
|| (ReflectHelper.MatchReturnType(method, typeof(void)) && method.GetAsyncTypeName() == null);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void IsValidTestMethodShouldReturnFalseForMethodsWithNonVoidReturnType()
{
this.SetupTestMethod();
var methodInfo = typeof(DummyTestClass).GetMethod(
"MethodWithTaskReturnType",
"MethodWithIntReturnType",
BindingFlags.Instance | BindingFlags.Public);

Assert.IsFalse(this.testMethodValidator.IsValidTestMethod(methodInfo, typeof(DummyTestClass), this.warnings));
Expand All @@ -151,6 +151,17 @@ public void IsValidTestMethodShouldReturnTrueForAsyncMethodsWithTaskReturnType()
Assert.IsTrue(this.testMethodValidator.IsValidTestMethod(methodInfo, this.type, this.warnings));
}

[TestMethod]
public void IsValidTestMethodShouldReturnTrueForNonAsyncMethodsWithTaskReturnType()
{
this.SetupTestMethod();
var methodInfo = typeof(DummyTestClass).GetMethod(
"MethodWithTaskReturnType",
BindingFlags.Instance | BindingFlags.Public);

Assert.IsTrue(this.testMethodValidator.IsValidTestMethod(methodInfo, this.type, this.warnings));
}

[TestMethod]
public void IsValidTestMethodShouldReturnTrueForMethodsWithVoidReturnType()
{
Expand Down Expand Up @@ -201,6 +212,11 @@ public Task MethodWithTaskReturnType()
return Task.Delay(TimeSpan.Zero);
}

public int MethodWithIntReturnType()
{
return 0;
}

public void MethodWithVoidReturnType()
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ public void HasCorrectClassOrAssemblyInitializeSignatureShouldReturnTrueForAsync
Assert.IsTrue(methodInfo.HasCorrectClassOrAssemblyInitializeSignature());
}

[TestMethod]
public void HasCorrectClassOrAssemblyInitializeSignatureShouldReturnTrueForTestMethodsWithoutAsync()
{
var methodInfo = typeof(DummyTestClass).GetMethod("PublicStaticNonAsyncTaskMethodWithTC");
Assert.IsTrue(methodInfo.HasCorrectClassOrAssemblyInitializeSignature());
}

[TestMethod]
public void HasCorrectClassOrAssemblyInitializeSignatureShouldReturnFalseForAsyncTestMethodsWithNonTaskReturnTypes()
{
Expand Down Expand Up @@ -127,6 +134,13 @@ public void HasCorrectClassOrAssemblyCleanupSignatureShouldReturnTrueForAsyncTes
Assert.IsTrue(methodInfo.HasCorrectClassOrAssemblyCleanupSignature());
}

[TestMethod]
public void HasCorrectClassOrAssemblyCleanupSignatureShouldReturnTrueForTestMethodsWithoutAsync()
{
var methodInfo = typeof(DummyTestClass).GetMethod("PublicStaticNonAsyncTaskMethod");
Assert.IsTrue(methodInfo.HasCorrectClassOrAssemblyCleanupSignature());
}

[TestMethod]
public void HasCorrectClassOrAssemblyCleanupSignatureShouldReturnFalseForAsyncTestMethodsWithNonTaskReturnTypes()
{
Expand Down Expand Up @@ -180,6 +194,13 @@ public void HasCorrectTestInitializeOrCleanupSignatureShouldReturnTrueForAsyncTe
Assert.IsTrue(methodInfo.HasCorrectTestInitializeOrCleanupSignature());
}

[TestMethod]
public void HasCorrectTestInitializeOrCleanupSignatureShouldReturnTrueForTestMethodsWithoutAsync()
{
var methodInfo = typeof(DummyTestClass).GetMethod("PublicNonAsyncTaskMethod");
Assert.IsTrue(methodInfo.HasCorrectTestInitializeOrCleanupSignature());
}

[TestMethod]
public void HasCorrectTestInitializeOrCleanupSignatureShouldReturnFalseForAsyncTestMethodsWithNonTaskReturnTypes()
{
Expand Down Expand Up @@ -247,6 +268,13 @@ public void HasCorrectTestMethodSignatureShouldReturnTrueForAsyncTestMethods()
Assert.IsTrue(methodInfo.HasCorrectTestMethodSignature(false));
}

[TestMethod]
public void HasCorrectTestMethodSignatureShouldReturnTrueForTaskTestMethodsWithoutAsync()
{
var methodInfo = typeof(DummyTestClass).GetMethod("PublicNonAsyncTaskMethod");
Assert.IsTrue(methodInfo.HasCorrectTestMethodSignature(false));
}

[TestMethod]
public void HasCorrectTestMethodSignatureShouldReturnFalseForAsyncTestMethodsWithNonTaskReturnTypes()
{
Expand Down Expand Up @@ -297,6 +325,13 @@ public void IsVoidOrTaskReturnTypeShouldReturnTrueForAsyncTaskMethods()
Assert.IsTrue(methodInfo.IsVoidOrTaskReturnType());
}

[TestMethod]
public void IsVoidOrTaskReturnTypeShouldReturnTrueForTaskMethodsWithoutAsync()
{
var methodInfo = typeof(DummyTestClass).GetMethod("PublicNonAsyncTaskMethod");
Assert.IsTrue(methodInfo.IsVoidOrTaskReturnType());
}

[TestMethod]
public void IsVoidOrTaskReturnTypeShouldReturnFalseForNonVoidMethods()
{
Expand Down Expand Up @@ -453,6 +488,11 @@ public static async Task PublicStaticAsyncTaskMethodWithTC(UTFExtension.TestCont
await Task.FromResult(true);
}

public static Task PublicStaticNonAsyncTaskMethodWithTC(UTFExtension.TestContext tc)
{
return Task.FromResult(true);
}

public static async void PublicStaticAsyncVoidMethodWithTC(UTFExtension.TestContext tc)
{
await Task.FromResult(true);
Expand All @@ -473,6 +513,11 @@ public static async void PublicStaticAsyncVoidMethod()
await Task.FromResult(true);
}

public static Task PublicStaticNonAsyncTaskMethod()
{
return Task.FromResult(true);
}

public void PublicMethod()
{
}
Expand Down Expand Up @@ -502,6 +547,11 @@ public async void PublicAsyncVoidMethod()
await Task.FromResult(true);
}

public Task PublicNonAsyncTaskMethod()
{
return Task.FromResult(true);
}

[UTF.Timeout(-11)]
public void PublicMethodWithInvalidTimeout()
{
Expand Down