Skip to content

Commit

Permalink
Merge pull request #642 from manfred-brands/issue615_MultipleAsync
Browse files Browse the repository at this point in the history
Add support for Assert.MultipleAsync
  • Loading branch information
mikkelbu authored Nov 17, 2023
2 parents 8fe0d46 + f57940c commit f850667
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public sealed class NUnitFrameworkConstantsTests
(nameof(NUnitFrameworkConstants.NameOfHasMember), nameof(Has.Member)),

(nameof(NUnitFrameworkConstants.NameOfMultiple), nameof(Assert.Multiple)),
#if NUNIT4
(nameof(NUnitFrameworkConstants.NameOfMultipleAsync), nameof(Assert.MultipleAsync)),
#else
(nameof(NUnitFrameworkConstants.NameOfMultipleAsync), "MultipleAsync"),
#endif

(nameof(NUnitFrameworkConstants.NameOfThrows), nameof(Throws)),
(nameof(NUnitFrameworkConstants.NameOfThrowsArgumentException), nameof(Throws.ArgumentException)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,29 @@ public void Test(string? s)
testCode);
}

#if NUNIT4
[Test]
public void InsideAssertMultipleAsync()
{
var testCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@$"
[TestCase("""")]
public async Task Test(string? s)
{{
await Assert.MultipleAsync(async () =>
{{
await Task.Yield();
ClassicAssert.NotNull(s);
Assert.That(↓s.Length, Is.GreaterThan(0));
}});
}}
");

RoslynAssert.NotSuppressed(suppressor,
ExpectedDiagnostic.Create(DereferencePossiblyNullReferenceSuppressor.SuppressionDescriptors["CS8602"]),
testCode);
}
#endif

[TestCase("ClassicAssert.True(nullable.HasValue)")]
[TestCase("ClassicAssert.IsTrue(nullable.HasValue)")]
[TestCase("Assert.That(nullable.HasValue, \"Ensure Value is set\")")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@ public void Test()
RoslynAssert.Valid(this.analyzer, testCode);
}

#if NUNIT4
[Test]
public void AnalyzeWhenMultipleAsyncIsUsed()
{
var testCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public async Task Test()
{
await Assert.MultipleAsync(async () =>
{
Assert.That(await Get1(), Is.Not.Null);
Assert.That(await Get2(), Is.Not.Null);
});
static Task<string?> Get1() => Task.FromResult(default(string));
static Task<string?> Get2() => Task.FromResult(default(string));
}");
RoslynAssert.Valid(this.analyzer, testCode);
}
#endif

[Test]
public void AnalyzeWhenDependent()
{
Expand Down
1 change: 1 addition & 0 deletions src/nunit.analyzers/Constants/NUnitFrameworkConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public static class NUnitFrameworkConstants
public const string NameOfHasMember = "Member";

public const string NameOfMultiple = "Multiple";
public const string NameOfMultipleAsync = "MultipleAsync";

public const string NameOfThrows = "Throws";
public const string NameOfThrowsArgumentException = "ArgumentException";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ private static bool IsValidatedNotNullByExpression(string possibleNullReference,
return true;
}
}
else if (member == NUnitFrameworkConstants.NameOfMultiple)
else if (member is NUnitFrameworkConstants.NameOfMultiple or NUnitFrameworkConstants.NameOfMultipleAsync)
{
// Look up into the actual asserted parameter
if (argumentList.Arguments.FirstOrDefault()?.Expression is AnonymousFunctionExpressionSyntax anonymousFunction)
Expand Down
2 changes: 1 addition & 1 deletion src/nunit.analyzers/Helpers/AssertHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static bool IsInsideAssertMultiple(SyntaxNode node)
while ((possibleAssertMultiple = node.Ancestors().OfType<InvocationExpressionSyntax>().FirstOrDefault()) is not null)
{
// Is the statement inside a Block which is part of an Assert.Multiple.
if (IsAssert(possibleAssertMultiple, NUnitFrameworkConstants.NameOfMultiple))
if (IsAssert(possibleAssertMultiple, NUnitFrameworkConstants.NameOfMultiple, NUnitFrameworkConstants.NameOfMultipleAsync))
{
return true;
}
Expand Down

0 comments on commit f850667

Please sign in to comment.