/
githubmirror
/
roslyn
Обзор
Документация
Войти
/
githubmirror
/
roslyn
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Features/CSharpTest/GenerateVariable/GenerateVariableTests_Razor.cs
268 строк
9 KB
David Wengier
Remove nullable
28 апр 2026, 14:06
28 апр 2026, 14:06
ec543a5
Код
Авторство
О чём код?
// 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.GenerateVariable; using Microsoft.CodeAnalysis.Test.Utilities; using Roslyn.Test.Utilities; using Xunit; namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.GenerateVariable; public sealed partial class GenerateVariableTests { private const int UppercasePropertyIndex = 0; 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 TestGeneratingPropertyInRazorSourceGeneratedDocumentAsync( string workspaceMarkup, string generatedCode, string expectedGeneratedCode, bool requestFromSourceGeneratedDocument) { using var workspace = CreateWorkspaceFromOptions(workspaceMarkup, 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 requestDocument = requestFromSourceGeneratedDocument ? sourceGeneratedDocument : project.Documents.Single(); var requestTree = await requestDocument.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.Severity == DiagnosticSeverity.Error && diagnostic.Location.SourceTree == requestTree)); var codeActions = new List<CodeAction>(); var context = new CodeFixContext( requestDocument, diagnostic.Location.SourceSpan, [diagnostic], (action, _) => codeActions.Add(action), CancellationToken.None); var provider = new CSharpGenerateVariableCodeFixProvider(); await provider.RegisterCodeFixesAsync(context); var flattenedActions = FlattenActions([.. codeActions]); Assert.True(flattenedActions.Length > PropertyIndex); var operations = await flattenedActions[UppercasePropertyIndex].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 TestGeneratingPropertyIntoRazorSourceGeneratedDocumentFromRegularDocument() => TestGeneratingPropertyInRazorSourceGeneratedDocumentAsync( """ <Workspace> <Project Language="C#" AssemblyName="ClassLibrary1" CommonReferences="true"> <Document> public class C { public object M() { return Component.Pr$$op; } } </Document> </Project> </Workspace> """, """ // <auto-generated /> using System; public partial class Component { } """, """ // <auto-generated /> using System; public partial class Component { public static object Prop { get; internal set; } } """, requestFromSourceGeneratedDocument: false); [Fact] public Task TestGeneratingPropertyInRazorSourceGeneratedDocument() => TestGeneratingPropertyInRazorSourceGeneratedDocumentAsync( """ <Workspace> <Project Language="C#" AssemblyName="ClassLibrary1" CommonReferences="true"> <Document> public class Placeholder { } </Document> </Project> </Workspace> """, """ // <auto-generated /> using System; public partial class Component { public object M() { return MissingProp; } } """, """ // <auto-generated /> using System; public partial class Component { public object MissingProp { get; private set; } public object M() { return MissingProp; } } """, requestFromSourceGeneratedDocument: true); [Fact] public Task TestGeneratingPropertyIntoHiddenRazorSourceGeneratedDocumentFromRegularDocument() => TestGeneratingPropertyInRazorSourceGeneratedDocumentAsync( """ <Workspace> <Project Language="C#" AssemblyName="ClassLibrary1" CommonReferences="true"> <Document> public class C { public object M() { return Component.Pr$$op; } } </Document> </Project> </Workspace> """, """ // <auto-generated /> using System; #line hidden public partial class Component { } #line default """, """ // <auto-generated /> using System; #line hidden public partial class Component { public static object Prop { get; internal set; } } #line default """, requestFromSourceGeneratedDocument: false); [Fact] public Task TestGeneratingPropertyInHiddenRazorSourceGeneratedDocument() => TestGeneratingPropertyInRazorSourceGeneratedDocumentAsync( """ <Workspace> <Project Language="C#" AssemblyName="ClassLibrary1" CommonReferences="true"> <Document> public class Placeholder { } </Document> </Project> </Workspace> """, """ // <auto-generated /> using System; #line hidden public partial class Component { public object M() { return MissingProp; } } #line default """, """ // <auto-generated /> using System; #line hidden public partial class Component { public object MissingProp { get; private set; } public object M() { return MissingProp; } } #line default """, requestFromSourceGeneratedDocument: true); }