Skip to content

Commit

Permalink
Merge pull request #702 from manfred-brands/issues/693_Assume
Browse files Browse the repository at this point in the history
Support Assume.That(x, Is.Not.Null) to suppress nullable warning
  • Loading branch information
mikkelbu authored Mar 11, 2024
2 parents 47648c5 + 97bd87e commit 59a9926
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public sealed class NUnitFrameworkConstantsTests
(nameof(NUnitFrameworkConstants.NameOfThrowsTargetInvocationException), nameof(Throws.TargetInvocationException)),

(nameof(NUnitFrameworkConstants.NameOfAssert), nameof(Assert)),
(nameof(NUnitFrameworkConstants.NameOfAssume), nameof(Assume)),

(nameof(NUnitFrameworkConstants.NameOfAssertPass), nameof(Assert.Pass)),
(nameof(NUnitFrameworkConstants.NameOfAssertFail), nameof(Assert.Fail)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void Test(string? s)
[TestCase("ClassicAssert.NotNull(s)")]
[TestCase("ClassicAssert.IsNotNull(s)")]
[TestCase("Assert.That(s, Is.Not.Null)")]
[TestCase("Assume.That(s, Is.Not.Null)")]
public void WithLocalValidAssert(string assert)
{
var testCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@$"
Expand All @@ -75,6 +76,7 @@ public void Test(string? s)
[TestCase("ClassicAssert.NotNull(s)")]
[TestCase("ClassicAssert.IsNotNull(s)")]
[TestCase("Assert.That(s, Is.Not.Null)")]
[TestCase("Assume.That(s, Is.Not.Null)")]
public void WithFieldValidAssert(string assert)
{
var testCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@$"
Expand Down Expand Up @@ -391,6 +393,7 @@ await Assert.MultipleAsync(async () =>
[TestCase("Assert.That(nullable.HasValue)")]
[TestCase("Assert.That(nullable.HasValue, Is.True)")]
[TestCase("Assert.That(nullable, Is.Not.Null)")]
[TestCase("Assume.That(nullable, Is.Not.Null)")]
public void NullableWithValidAssert(string assert)
{
var testCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@$"
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 @@ -59,6 +59,7 @@ public static class NUnitFrameworkConstants
public const string NameOfThrowsTargetInvocationException = "TargetInvocationException";

public const string NameOfAssert = "Assert";
public const string NameOfAssume = "Assume";

public const string NameOfAssertPass = "Pass";
public const string NameOfAssertFail = "Fail";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,7 @@ private static bool IsValidatedNotNullByPreviousStatementInSameBlock(string poss
private static bool IsValidatedNotNullByExpression(string possibleNullReference, ExpressionSyntax expression)
{
// Check if this is an Assert for the same symbol
if (AssertHelper.IsAssert(expression, out string member, out ArgumentListSyntax? argumentList) ||
AssertHelper.IsClassicAssert(expression, out member, out argumentList))
if (AssertHelper.IsAssertClassicAssertOrAssume(expression, out string member, out ArgumentListSyntax? argumentList))
{
string firstArgument = argumentList.Arguments.First().Expression.ToString();

Expand Down
15 changes: 10 additions & 5 deletions src/nunit.analyzers/Helpers/AssertHelper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -123,25 +124,29 @@ public static bool IsAssert(ExpressionSyntax? expression,
out string member,
[NotNullWhen(true)] out ArgumentListSyntax? argumentList)
{
return IsAssert(expression, NUnitFrameworkConstants.NameOfAssert, out member, out argumentList);
return IsAssert(expression, x => x is NUnitFrameworkConstants.NameOfAssert, out member, out argumentList);
}

public static bool IsClassicAssert(ExpressionSyntax? expression,
public static bool IsAssertClassicAssertOrAssume(ExpressionSyntax? expression,
out string member,
[NotNullWhen(true)] out ArgumentListSyntax? argumentList)
{
return IsAssert(expression, NUnitFrameworkConstants.NameOfClassicAssert, out member, out argumentList);
return IsAssert(expression,
x => x is NUnitFrameworkConstants.NameOfAssert
or NUnitFrameworkConstants.NameOfClassicAssert
or NUnitFrameworkConstants.NameOfAssume,
out member, out argumentList);
}

private static bool IsAssert(ExpressionSyntax? expression,
string nameOfAssert,
Func<string, bool> isNameOfAssert,
out string member,
[NotNullWhen(true)] out ArgumentListSyntax? argumentList)
{
if (expression is InvocationExpressionSyntax invocationExpression &&
invocationExpression.Expression is MemberAccessExpressionSyntax memberAccessExpression &&
memberAccessExpression.Expression is IdentifierNameSyntax identifierName &&
identifierName.Identifier.Text == nameOfAssert)
isNameOfAssert(identifierName.Identifier.Text))
{
member = memberAccessExpression.Name.Identifier.Text;
argumentList = invocationExpression.ArgumentList;
Expand Down

0 comments on commit 59a9926

Please sign in to comment.