Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add support for Assert.MultipleAsync #642

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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