From 1cb3e79e17ccf1ba2b5156ceabfab572aeb034f8 Mon Sep 17 00:00:00 2001 From: Alex Sobolev Date: Mon, 15 Sep 2025 17:21:44 +0400 Subject: [PATCH] Add CSharpAnalyzerTestExtensions --- .../CSharpAnalyzerTestExtensions.cs | 43 +++++++++++++++++++ .../test/Verifiers/CSharpAnalyzerVerifier.cs | 19 +++----- 2 files changed, 48 insertions(+), 14 deletions(-) create mode 100644 src/Framework/AspNetCoreAnalyzers/test/Extensions/CSharpAnalyzerTestExtensions.cs diff --git a/src/Framework/AspNetCoreAnalyzers/test/Extensions/CSharpAnalyzerTestExtensions.cs b/src/Framework/AspNetCoreAnalyzers/test/Extensions/CSharpAnalyzerTestExtensions.cs new file mode 100644 index 000000000000..a341d4758b9d --- /dev/null +++ b/src/Framework/AspNetCoreAnalyzers/test/Extensions/CSharpAnalyzerTestExtensions.cs @@ -0,0 +1,43 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Diagnostics.CodeAnalysis; +using Microsoft.AspNetCore.Analyzers.Verifiers; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Testing; +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Testing; + +namespace Microsoft.AspNetCore.Analyzers; + +public static class CSharpAnalyzerTestExtensions +{ + extension(CSharpAnalyzerTest) + where TAnalyzer : DiagnosticAnalyzer, new() + where TVerifier : IVerifier, new() + { + public static CSharpAnalyzerTest Create([StringSyntax("C#-test")] string source, params ReadOnlySpan expectedDiagnostics) + { + var test = new CSharpAnalyzerTest + { + TestCode = source.ReplaceLineEndings(), + // We need to set the output type to an exe to properly + // support top-level programs in the tests. Otherwise, + // the test infra will assume we are trying to build a library. + TestState = { OutputKind = OutputKind.ConsoleApplication }, + ReferenceAssemblies = CSharpAnalyzerVerifier.GetReferenceAssemblies(), + }; + + test.ExpectedDiagnostics.AddRange(expectedDiagnostics); + return test; + } + } + + public static CSharpAnalyzerTest WithSource(this CSharpAnalyzerTest test, [StringSyntax("C#-test")] string source) + where TAnalyzer : DiagnosticAnalyzer, new() + where TVerifier : IVerifier, new() + { + test.TestState.Sources.Add(source); + return test; + } +} diff --git a/src/Framework/AspNetCoreAnalyzers/test/Verifiers/CSharpAnalyzerVerifier.cs b/src/Framework/AspNetCoreAnalyzers/test/Verifiers/CSharpAnalyzerVerifier.cs index cfbc023573b3..316d6d0ef37a 100644 --- a/src/Framework/AspNetCoreAnalyzers/test/Verifiers/CSharpAnalyzerVerifier.cs +++ b/src/Framework/AspNetCoreAnalyzers/test/Verifiers/CSharpAnalyzerVerifier.cs @@ -2,15 +2,16 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Collections.Immutable; +using System.Diagnostics.CodeAnalysis; using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.InternalTesting; +using Microsoft.AspNetCore.Mvc; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Testing; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Testing; using Microsoft.Extensions.DependencyInjection; -using Microsoft.AspNetCore.Hosting; namespace Microsoft.AspNetCore.Analyzers.Verifiers; @@ -30,19 +31,9 @@ public static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor) => CSharpAnalyzerVerifier.Diagnostic(descriptor); /// - public static async Task VerifyAnalyzerAsync(string source, params DiagnosticResult[] expected) + public static async Task VerifyAnalyzerAsync([StringSyntax("C#-test")] string source, params DiagnosticResult[] expected) { - var test = new CSharpAnalyzerTest - { - TestCode = source.ReplaceLineEndings(), - // We need to set the output type to an exe to properly - // support top-level programs in the tests. Otherwise, - // the test infra will assume we are trying to build a library. - TestState = { OutputKind = OutputKind.ConsoleApplication }, - ReferenceAssemblies = GetReferenceAssemblies(), - }; - - test.ExpectedDiagnostics.AddRange(expected); + var test = CSharpAnalyzerTest.Create(source, expected); await test.RunAsync(CancellationToken.None); }