From bf6059458b38342802e5c5458fc6a194af23d680 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20St=C3=BChmer?= Date: Thu, 29 Oct 2026 04:25:15 +0100 Subject: [PATCH] test(analyzer): exercise using insertion in NE0010 code fix The NE0010 code-fix tests named AddsUsing / UsingInsertedAtNamespaceLevel already contained 'using System.Threading;' in their input, so the fix returned early and never ran EnsureSystemThreadingUsing's insertion branches (11 NoCoverage and 13 Survived mutants in the mutation report). Drop the directive from the input of those three tests (the helper now uses the fully qualified CancellationToken) so the using is really inserted at compilation-unit, file-scoped and block-scoped namespace level, before an existing using. Add a block-scoped case where the new using sorts last, covering the indentation carried over from the first existing using. --- ...eCancellationTokenParameterCodeFixTests.cs | 62 ++++++++++++++++--- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/test/NetEvolve.Analyzer.Tests.Unit/Usage/RequireCancellationTokenParameterCodeFixTests.cs b/test/NetEvolve.Analyzer.Tests.Unit/Usage/RequireCancellationTokenParameterCodeFixTests.cs index 4e92941..701168b 100644 --- a/test/NetEvolve.Analyzer.Tests.Unit/Usage/RequireCancellationTokenParameterCodeFixTests.cs +++ b/test/NetEvolve.Analyzer.Tests.Unit/Usage/RequireCancellationTokenParameterCodeFixTests.cs @@ -18,14 +18,13 @@ public Task NoParameters_AppendsTokenAndAddsUsing() => RequireCancellationTokenParameterCodeFixProvider >.VerifyCodeFixAsync( """ - using System.Threading; using System.Threading.Tasks; public sealed class Sample { public Task {|NE0010:Run|}() => HelperAsync(); - private static Task HelperAsync(CancellationToken token = default) => Task.CompletedTask; + private static Task HelperAsync(System.Threading.CancellationToken token = default) => Task.CompletedTask; } """, """ @@ -36,7 +35,7 @@ public sealed class Sample { public Task Run(CancellationToken cancellationToken = default) => HelperAsync(token: cancellationToken); - private static Task HelperAsync(CancellationToken token = default) => Task.CompletedTask; + private static Task HelperAsync(System.Threading.CancellationToken token = default) => Task.CompletedTask; } """ ); @@ -136,14 +135,13 @@ public Task FileScopedNamespace_UsingInsertedAtNamespaceLevel() => """ namespace Sample; - using System.Threading; using System.Threading.Tasks; public sealed class Sample { public Task {|NE0010:Run|}() => HelperAsync(); - private static Task HelperAsync(CancellationToken token = default) => Task.CompletedTask; + private static Task HelperAsync(System.Threading.CancellationToken token = default) => Task.CompletedTask; } """, """ @@ -156,7 +154,7 @@ public sealed class Sample { public Task Run(CancellationToken cancellationToken = default) => HelperAsync(token: cancellationToken); - private static Task HelperAsync(CancellationToken token = default) => Task.CompletedTask; + private static Task HelperAsync(System.Threading.CancellationToken token = default) => Task.CompletedTask; } """ ); @@ -452,14 +450,13 @@ public Task BlockScopedNamespaceWithExistingUsings_UsingInsertedAtNamespaceLevel """ namespace Sample { - using System.Threading; using System.Threading.Tasks; public sealed class Sample { public Task {|NE0010:Run|}() => HelperAsync(); - private static Task HelperAsync(CancellationToken token = default) => Task.CompletedTask; + private static Task HelperAsync(System.Threading.CancellationToken token = default) => Task.CompletedTask; } } """, @@ -473,7 +470,7 @@ public sealed class Sample { public Task Run(CancellationToken cancellationToken = default) => HelperAsync(token: cancellationToken); - private static Task HelperAsync(CancellationToken token = default) => Task.CompletedTask; + private static Task HelperAsync(System.Threading.CancellationToken token = default) => Task.CompletedTask; } } """ @@ -547,6 +544,53 @@ private static System.Threading.Tasks.Task HelperAsync( """ ); + [Test] + public Task BlockScopedNamespaceNewUsingSortsAfterAllExisting_AppendedWithIndentation() => + CSharpCodeFixVerifier< + RequireCancellationTokenParameterAnalyzer, + RequireCancellationTokenParameterCodeFixProvider + >.VerifyCodeFixAsync( + """ + namespace Sample + { + using System.Collections.Generic; + + public sealed class Sample + { + public System.Threading.Tasks.Task {|NE0010:Run|}() + { + _ = new List(); + return HelperAsync(); + } + + private static System.Threading.Tasks.Task HelperAsync( + System.Threading.CancellationToken token = default + ) => System.Threading.Tasks.Task.CompletedTask; + } + } + """, + """ + namespace Sample + { + using System.Collections.Generic; + using System.Threading; + + public sealed class Sample + { + public System.Threading.Tasks.Task Run(CancellationToken cancellationToken = default) + { + _ = new List(); + return HelperAsync(token: cancellationToken); + } + + private static System.Threading.Tasks.Task HelperAsync( + System.Threading.CancellationToken token = default + ) => System.Threading.Tasks.Task.CompletedTask; + } + } + """ + ); + // ---- Call-site propagation edge cases -------------------------------------------------------------- [Test]