From 029b894eb5c441d2f36f4b85779141fae556b427 Mon Sep 17 00:00:00 2001 From: Brad Wilson Date: Fri, 10 Jan 2025 11:02:32 -0800 Subject: [PATCH] Ensure xUnit1051 triggers in Assert.Collection/CollectionAsync --- .../Analyzers/X1000/UseCancellationTokenTests.cs | 1 + src/xunit.analyzers/Utility/CodeAnalysisExtensions.cs | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/xunit.analyzers.tests/Analyzers/X1000/UseCancellationTokenTests.cs b/src/xunit.analyzers.tests/Analyzers/X1000/UseCancellationTokenTests.cs index 821de550..854cc43a 100644 --- a/src/xunit.analyzers.tests/Analyzers/X1000/UseCancellationTokenTests.cs +++ b/src/xunit.analyzers.tests/Analyzers/X1000/UseCancellationTokenTests.cs @@ -175,6 +175,7 @@ public async Task InsideAssertionLambda_Triggers() class TestClass { [Fact] public async ValueTask TestMethod() { + await Assert.CollectionAsync(Array.Empty(), x => [|Task.Delay(x)|], x => [|Task.Delay(x)|]); await Assert.ThrowsAsync(() => [|Task.Delay(1)|]); await Record.ExceptionAsync(() => [|Task.Delay(1)|]); } diff --git a/src/xunit.analyzers/Utility/CodeAnalysisExtensions.cs b/src/xunit.analyzers/Utility/CodeAnalysisExtensions.cs index 8f7d9c68..1b5137f6 100644 --- a/src/xunit.analyzers/Utility/CodeAnalysisExtensions.cs +++ b/src/xunit.analyzers/Utility/CodeAnalysisExtensions.cs @@ -92,10 +92,13 @@ public static (bool isInTestMethod, IOperation? lambdaOwner) IsInTestMethod( { lambdaOwner = parent; - if (parent.Parent is IDelegateCreationOperation && - parent.Parent.Parent is IArgumentOperation && - parent.Parent.Parent.Parent is IInvocationOperation invocationOperation) - lambdaOwner = invocationOperation; + if (parent.Parent is IDelegateCreationOperation) + for (var target = parent.Parent.Parent; target is not null; target = target.Parent) + if (target is IArgumentOperation && target.Parent is IInvocationOperation invocationOperation) + { + lambdaOwner = invocationOperation; + break; + } } continue;