Skip to content

Commit

Permalink
nunit#258: Deal with untyped Tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
manfred-brands committed Jul 13, 2020
1 parent aab0b1f commit 15fb7d2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -809,5 +809,26 @@ public void NoDiagnosticWhenCombinedConstraintAndUseAllOperator()

AnalyzerAssert.Valid(analyzer, testCode);
}

[Test]
public void NoDiagnosticWhenComparingTask()
{
var testCode = TestUtility.WrapInAsyncTestMethod(@"
Task wait = Task.CompletedTask;
Assert.That(await Task.WhenAny(wait).ConfigureAwait(false), Is.EqualTo(wait));");

AnalyzerAssert.Valid(analyzer, testCode);
}

[Test]
public void NoDiagnosticWhenComparingTask2()
{
var testCode = TestUtility.WrapInTestMethod(@"
Task task1 = Task.CompletedTask;
Task task2 = Task.CompletedTask;
Assert.That(task1, Is.SameAs(task2));");

AnalyzerAssert.Valid(analyzer, testCode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,16 @@ public void TestMethod()

AnalyzerAssert.Valid(analyzer, testCode);
}

[TestCase("Is.Null")]
[TestCase("Is.Not.Null")]
public void ValidWhenActualIsTask(string constraint)
{
var testCode = TestUtility.WrapInTestMethod($@"
var task = Task.CompletedTask;
Assert.That(task, {constraint});");

AnalyzerAssert.Valid(analyzer, testCode);
}
}
}
9 changes: 9 additions & 0 deletions src/nunit.analyzers.tests/TestUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,14 @@ public void TestMethod()
{{{code}
}}", additionalUsings);
}

internal static string WrapInAsyncTestMethod(string code, string additionalUsings = null)
{
return WrapMethodInClassNamespaceAndAddUsings($@"
[Test]
public async Task TestMethod()
{{{code}
}}", additionalUsings);
}
}
}
2 changes: 1 addition & 1 deletion src/nunit.analyzers/Helpers/AssertHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static ITypeSymbol UnwrapActualType(ITypeSymbol actualType)
if (actualType is INamedTypeSymbol namedType && namedType.DelegateInvokeMethod != null)
actualType = namedType.DelegateInvokeMethod.ReturnType;

if (actualType.IsAwaitable(out var awaitReturnType))
if (actualType.IsAwaitable(out var awaitReturnType) && awaitReturnType.SpecialType != SpecialType.System_Void)
actualType = awaitReturnType;

return actualType;
Expand Down

0 comments on commit 15fb7d2

Please sign in to comment.