Skip to content

Commit 3a11173

Browse files
Add support for Assert.MultipleAsync
This affects: - DereferencePossiblyNullReferenceSuppressor - UseAssertMultipleAnalyzer
1 parent 8fe0d46 commit 3a11173

File tree

6 files changed

+51
-2
lines changed

6 files changed

+51
-2
lines changed

src/nunit.analyzers.tests/Constants/NUnitFrameworkConstantsTests.cs

+5
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public sealed class NUnitFrameworkConstantsTests
6969
(nameof(NUnitFrameworkConstants.NameOfHasMember), nameof(Has.Member)),
7070

7171
(nameof(NUnitFrameworkConstants.NameOfMultiple), nameof(Assert.Multiple)),
72+
#if NUNIT4
73+
(nameof(NUnitFrameworkConstants.NameOfMultipleAsync), nameof(Assert.MultipleAsync)),
74+
#else
75+
(nameof(NUnitFrameworkConstants.NameOfMultipleAsync), nameof("MultipleAsync")),
76+
#endif
7277

7378
(nameof(NUnitFrameworkConstants.NameOfThrows), nameof(Throws)),
7479
(nameof(NUnitFrameworkConstants.NameOfThrowsArgumentException), nameof(Throws.ArgumentException)),

src/nunit.analyzers.tests/DiagnosticSuppressors/DereferencePossiblyNullReferenceSuppressorTests.cs

+23
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,29 @@ public void Test(string? s)
362362
testCode);
363363
}
364364

365+
#if NUNIT4
366+
[Test]
367+
public void InsideAssertMultipleAsync()
368+
{
369+
var testCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@$"
370+
[TestCase("""")]
371+
public async Task Test(string? s)
372+
{{
373+
await Assert.MultipleAsync(async () =>
374+
{{
375+
await Task.Yield();
376+
ClassicAssert.NotNull(s);
377+
Assert.That(↓s.Length, Is.GreaterThan(0));
378+
}});
379+
}}
380+
");
381+
382+
RoslynAssert.NotSuppressed(suppressor,
383+
ExpectedDiagnostic.Create(DereferencePossiblyNullReferenceSuppressor.SuppressionDescriptors["CS8602"]),
384+
testCode);
385+
}
386+
#endif
387+
365388
[TestCase("ClassicAssert.True(nullable.HasValue)")]
366389
[TestCase("ClassicAssert.IsTrue(nullable.HasValue)")]
367390
[TestCase("Assert.That(nullable.HasValue, \"Ensure Value is set\")")]

src/nunit.analyzers.tests/UseAssertMultiple/UseAssertMultipleAnalyzerTests.cs

+20
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@ public void Test()
2727
RoslynAssert.Valid(this.analyzer, testCode);
2828
}
2929

30+
#if NUNIT4
31+
[Test]
32+
public void AnalyzeWhenMultipleAsyncIsUsed()
33+
{
34+
var testCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
35+
public async Task Test()
36+
{
37+
await Assert.MultipleAsync(async () =>
38+
{
39+
Assert.That(await Get1(), Is.Not.Null);
40+
Assert.That(await Get2(), Is.Not.Null);
41+
});
42+
43+
static Task<string?> Get1() => Task.FromResult(default(string));
44+
static Task<string?> Get2() => Task.FromResult(default(string));
45+
}");
46+
RoslynAssert.Valid(this.analyzer, testCode);
47+
}
48+
#endif
49+
3050
[Test]
3151
public void AnalyzeWhenDependent()
3252
{

src/nunit.analyzers/Constants/NUnitFrameworkConstants.cs

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public static class NUnitFrameworkConstants
5050
public const string NameOfHasMember = "Member";
5151

5252
public const string NameOfMultiple = "Multiple";
53+
public const string NameOfMultipleAsync = "MultipleAsync";
5354

5455
public const string NameOfThrows = "Throws";
5556
public const string NameOfThrowsArgumentException = "ArgumentException";

src/nunit.analyzers/DiagnosticSuppressors/DereferencePossiblyNullReferenceSuppressor.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ private static bool IsValidatedNotNullByExpression(string possibleNullReference,
357357
return true;
358358
}
359359
}
360-
else if (member == NUnitFrameworkConstants.NameOfMultiple)
360+
else if (member is NUnitFrameworkConstants.NameOfMultiple or NUnitFrameworkConstants.NameOfMultipleAsync)
361361
{
362362
// Look up into the actual asserted parameter
363363
if (argumentList.Arguments.FirstOrDefault()?.Expression is AnonymousFunctionExpressionSyntax anonymousFunction)

src/nunit.analyzers/Helpers/AssertHelper.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public static bool IsInsideAssertMultiple(SyntaxNode node)
9999
while ((possibleAssertMultiple = node.Ancestors().OfType<InvocationExpressionSyntax>().FirstOrDefault()) is not null)
100100
{
101101
// Is the statement inside a Block which is part of an Assert.Multiple.
102-
if (IsAssert(possibleAssertMultiple, NUnitFrameworkConstants.NameOfMultiple))
102+
if (IsAssert(possibleAssertMultiple, NUnitFrameworkConstants.NameOfMultiple, NUnitFrameworkConstants.NameOfMultipleAsync))
103103
{
104104
return true;
105105
}

0 commit comments

Comments
 (0)