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

Recognize Fully Qualified NUnit.Framework.Assert.That #740

Merged
merged 1 commit into from
May 19, 2024
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 @@ -1032,5 +1032,27 @@ public HasString(string? inner)
ExpectedDiagnostic.Create(DereferencePossiblyNullReferenceSuppressor.SuppressionDescriptors["CS8603"]),
testCode);
}

[Test]
public void TestIssue739FullyQualified()
{
var testCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
[Test]
public void T()
{
object? x = null;
NUnit.Framework.Assert.That(x, Is.Not.Null);
M(x);
}

void M(object o)
{
}
");

RoslynAssert.Suppressed(suppressor,
ExpectedDiagnostic.Create(DereferencePossiblyNullReferenceSuppressor.SuppressionDescriptors["CS8604"]),
testCode);
}
}
}
37 changes: 25 additions & 12 deletions src/nunit.analyzers/Helpers/AssertHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,33 @@ private static bool IsAssert(ExpressionSyntax? expression,
[NotNullWhen(true)] out ArgumentListSyntax? argumentList)
{
if (expression is InvocationExpressionSyntax invocationExpression &&
invocationExpression.Expression is MemberAccessExpressionSyntax memberAccessExpression &&
memberAccessExpression.Expression is IdentifierNameSyntax identifierName &&
isNameOfAssert(identifierName.Identifier.Text))
invocationExpression.Expression is MemberAccessExpressionSyntax memberAccessExpression)
{
member = memberAccessExpression.Name.Identifier.Text;
argumentList = invocationExpression.ArgumentList;
return true;
}
else
{
member = string.Empty;
argumentList = null;
return false;
SimpleNameSyntax? className = GetClassName(memberAccessExpression);

if (className is not null &&
isNameOfAssert(className.Identifier.Text))
{
member = memberAccessExpression.Name.Identifier.Text;
argumentList = invocationExpression.ArgumentList;
return true;
}
}

member = string.Empty;
argumentList = null;
return false;
}

private static SimpleNameSyntax? GetClassName(MemberAccessExpressionSyntax memberAccessExpression)
{
if (memberAccessExpression.Expression is MemberAccessExpressionSyntax nestedMemberAccessExpression)
return nestedMemberAccessExpression.Name;

if (memberAccessExpression.Expression is IdentifierNameSyntax identifierName)
return identifierName;

return null;
}
}
}