/
githubmirror
/
roslyn
Обзор
Документация
Войти
/
githubmirror
/
roslyn
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Analyzers/CSharp/Tests/ImplementAbstractClass/ImplementAbstractClassTests_Razor.cs
148 строк
6 KB
David Wengier
Handle hidden Razor abstract class generation
14 май 2026, 04:04
14 май 2026, 04:04
ba59cc5
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.CSharp.ImplementAbstractClass; using Microsoft.CodeAnalysis.Test.Utilities; using Roslyn.Test.Utilities; using Xunit; namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.ImplementAbstractClass; public sealed partial class ImplementAbstractClassTests { private sealed record RazorGeneratedDocumentData(string HintName, string GeneratedCode); protected override void InitializeWorkspace(TestWorkspace workspace, TestParameters parameters) { if (parameters.fixProviderData is not RazorGeneratedDocumentData generatedDocument) { return; } var project = workspace.CurrentSolution.Projects.Single(); var updatedProject = project.AddAnalyzerReference( new TestGeneratorReference( new Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator( context => context.AddSource(generatedDocument.HintName, generatedDocument.GeneratedCode)))); Assert.True(workspace.TryApplyChanges(updatedProject.Solution)); } private static TestParameters WithRazorGeneratedDocument(string generatedCode, string hintName = "Component.razor.g.cs") => new(fixProviderData: new RazorGeneratedDocumentData(hintName, generatedCode)); private async Task TestImplementingAbstractClassInRazorSourceGeneratedDocumentAsync( string generatedCode, string expectedGeneratedCode) { using var workspace = CreateWorkspaceFromOptions( """ <Workspace> <Project Language="C#" AssemblyName="ClassLibrary1" CommonReferences="true"> <Document> abstract class BaseComponent { public abstract string GetValue(); } class Placeholder { } </Document> </Project> </Workspace> """, WithRazorGeneratedDocument(generatedCode)); var project = workspace.CurrentSolution.Projects.Single(); var sourceGeneratedDocument = Assert.Single(await project.GetSourceGeneratedDocumentsAsync(CancellationToken.None)); Assert.Contains("Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator", sourceGeneratedDocument.FilePath); Assert.StartsWith( "// <auto-generated />", (await sourceGeneratedDocument.GetTextAsync(CancellationToken.None)).ToString()); var requestTree = await sourceGeneratedDocument.GetSyntaxTreeAsync(CancellationToken.None); var compilation = await project.GetCompilationAsync(CancellationToken.None); Assert.NotNull(requestTree); Assert.NotNull(compilation); var diagnostic = Assert.Single( compilation!.GetDiagnostics(CancellationToken.None).Where( diagnostic => diagnostic.Id == "CS0534" && diagnostic.Location.SourceTree == requestTree)); var codeActions = new List<CodeAction>(); var context = new CodeFixContext( sourceGeneratedDocument, diagnostic.Location.SourceSpan, [diagnostic], (action, _) => codeActions.Add(action), CancellationToken.None); var provider = new CSharpImplementAbstractClassCodeFixProvider(); await provider.RegisterCodeFixesAsync(context); var operations = await Assert.Single(codeActions).GetOperationsAsync(CancellationToken.None); var changedSolutions = await ApplyOperationsAndGetSolutionAsync(workspace, operations); var newSourceGeneratedDocument = await changedSolutions.Item2.GetSourceGeneratedDocumentAsync(sourceGeneratedDocument.Id, CancellationToken.None); Assert.NotNull(newSourceGeneratedDocument); AssertEx.EqualOrDiff( expectedGeneratedCode, (await newSourceGeneratedDocument!.GetTextAsync(CancellationToken.None)).ToString()); } [Fact] public Task TestImplementingAbstractClassInRazorSourceGeneratedDocument() => TestImplementingAbstractClassInRazorSourceGeneratedDocumentAsync( """ // <auto-generated /> public partial class Component : BaseComponent { } """, """ // <auto-generated /> public partial class Component : BaseComponent { public override string GetValue() { throw new System.NotImplementedException(); } } """); [Fact] public Task TestImplementingAbstractClassInHiddenRazorSourceGeneratedDocument() => TestImplementingAbstractClassInRazorSourceGeneratedDocumentAsync( """ // <auto-generated /> #line hidden public partial class Component : BaseComponent { } #line default """, """ // <auto-generated /> #line hidden public partial class Component : BaseComponent { public override string GetValue() { throw new System.NotImplementedException(); } } #line default """); }