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

Do not consider Assert.That with TestDelegate to be a candidate for Assert.Multiple #616

Merged
merged 1 commit into from
Oct 21, 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 @@ -99,5 +99,39 @@ public void Test()
}");
RoslynAssert.Valid(this.analyzer, testCode);
}

[Test]
public void AnalyzeWhenUsingAnonymousLambda()
{
var testCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void Test()
{
object? actualDeserialized = null;

Assert.That(() => actualDeserialized = Calculate(), Throws.Nothing);
Assert.That(actualDeserialized, Is.Not.Null);

static object? Calculate() => new object();
}");

RoslynAssert.Valid(this.analyzer, testCode);
}

[Test]
public void AnalyzeWhenUsingTestDelegate()
{
var testCode = TestUtility.WrapMethodInClassNamespaceAndAddUsings(@"
public void Test()
{
object? actualDeserialized = null;

Assert.That(Calculate, Throws.Nothing);
Assert.That(actualDeserialized, Is.Not.Null);

void Calculate() => actualDeserialized = new object();
}");

RoslynAssert.Valid(this.analyzer, testCode);
}
}
}
19 changes: 17 additions & 2 deletions src/nunit.analyzers/UseAssertMultiple/UseAssertMultipleAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ protected override void AnalyzeAssertInvocation(OperationAnalysisContext context
var previousArguments = new HashSet<string>(StringComparer.Ordinal);

// No need to check argument count as Assert.That needs at least one argument.
var assertArgument = assertOperation.Arguments[0].Syntax.ToString();
IArgumentOperation assertArgumentOperation = assertOperation.Arguments[0];
if (assertArgumentOperation.Value is IDelegateCreationOperation)
{
return;
}

var assertArgument = assertArgumentOperation.Syntax.ToString();

IOperation? statementBefore = null;
int firstAssert = -1;
Expand Down Expand Up @@ -139,7 +145,16 @@ private static bool IsIndependentAssert(HashSet<string> previousArguments, IOper
if (currentAssertOperation is not null)
{
// No need to check argument count as Assert.That needs at least one argument.
string currentArgument = currentAssertOperation.Arguments[0].Syntax.ToString();
IArgumentOperation argumentOperation = currentAssertOperation.Arguments[0];
if (argumentOperation.Value is IDelegateCreationOperation)
{
// Assert.That(() => { SomeCode }, Throws.Nothing);
// TODO: Should we delve into the lambda?
// For now state that it isn't mergeable inside an Assert.Multiple.
return false;
}

string currentArgument = argumentOperation.Syntax.ToString();

// Check if test is independent
return IsIndependent(previousArguments, currentArgument);
Expand Down