diff --git a/src/nunit.analyzers.tests/UseAssertMultiple/UseAssertMultipleAnalyzerTests.cs b/src/nunit.analyzers.tests/UseAssertMultiple/UseAssertMultipleAnalyzerTests.cs index 58af5940..5d8deeb6 100644 --- a/src/nunit.analyzers.tests/UseAssertMultiple/UseAssertMultipleAnalyzerTests.cs +++ b/src/nunit.analyzers.tests/UseAssertMultiple/UseAssertMultipleAnalyzerTests.cs @@ -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); + } } } diff --git a/src/nunit.analyzers/UseAssertMultiple/UseAssertMultipleAnalyzer.cs b/src/nunit.analyzers/UseAssertMultiple/UseAssertMultipleAnalyzer.cs index 12d16416..697f9d3e 100644 --- a/src/nunit.analyzers/UseAssertMultiple/UseAssertMultipleAnalyzer.cs +++ b/src/nunit.analyzers/UseAssertMultiple/UseAssertMultipleAnalyzer.cs @@ -82,7 +82,13 @@ protected override void AnalyzeAssertInvocation(OperationAnalysisContext context var previousArguments = new HashSet(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; @@ -139,7 +145,16 @@ private static bool IsIndependentAssert(HashSet 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 mergable inside an Assert.Multiple. + return false; + } + + string currentArgument = argumentOperation.Syntax.ToString(); // Check if test is independent return IsIndependent(previousArguments, currentArgument);