/
githubmirror
/
roslyn
Обзор
Документация
Войти
/
githubmirror
/
roslyn
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Features/CSharpTest/ConvertProgram/ConvertToProgramMainRefactoringTests.cs
185 строк
6 KB
Rikki Gibson
File-based apps: avoid running a few irrelevant editor features (#84575)
22 июл 2026, 04:42
Не верифицирован
22 июл 2026, 04:42
6e8d159
Код
Авторство
О чём код?
// 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.Threading.Tasks; using Microsoft.CodeAnalysis.CodeStyle; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.CodeStyle; using Microsoft.CodeAnalysis.CSharp.ConvertProgram; using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions; using Microsoft.CodeAnalysis.Test.Utilities; using Microsoft.CodeAnalysis.Testing; using Xunit; namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.ConvertProgram; using VerifyCS = CSharpCodeRefactoringVerifier<ConvertToProgramMainCodeRefactoringProvider>; [UseExportProvider] public sealed class ConvertToProgramMainRefactoringTests { [Fact] public Task TestNotOnFileWithNoGlobalStatements() => new VerifyCS.Test { TestCode = """ $$ class C { } """, LanguageVersion = LanguageVersion.CSharp10, TestState = { OutputKind = OutputKind.ConsoleApplication }, ExpectedDiagnostics = { // error CS5001: Program does not contain a static 'Main' method suitable for an entry point DiagnosticResult.CompilerError("CS5001"), } }.RunAsync(); [Fact] public Task TestNotOnEmptyFile() => new VerifyCS.Test { TestCode = """ $$ """, LanguageVersion = LanguageVersion.CSharp10, TestState = { OutputKind = OutputKind.ConsoleApplication }, ExpectedDiagnostics = { // error CS5001: Program does not contain a static 'Main' method suitable for an entry point DiagnosticResult.CompilerError("CS5001"), } }.RunAsync(); [Fact] public Task TestConvertToProgramMainWithDefaultTopLevelStatementPreference() => new VerifyCS.Test { TestCode = """ $$System.Console.WriteLine(0); """, FixedCode = """ internal class Program { private static void Main(string[] args) { System.Console.WriteLine(0); } } """, LanguageVersion = LanguageVersion.CSharp10, TestState = { OutputKind = OutputKind.ConsoleApplication }, }.RunAsync(); [Fact] public Task TestNotOfferedForFileBasedProgram() => new VerifyCS.Test { TestCode = """ $$System.Console.WriteLine(0); """, LanguageVersion = LanguageVersion.CSharp10, TestState = { OutputKind = OutputKind.ConsoleApplication }, SolutionTransforms = { static (solution, projectId) => { var project = solution.GetProject(projectId)!; var parseOptions = (CSharpParseOptions)project.ParseOptions!; return solution.WithProjectParseOptions(projectId, parseOptions.WithFeatures([.. parseOptions.Features, new("FileBasedProgram", "true")])); }, }, }.RunAsync(); [Fact] public Task TestNotOfferedInLibrary() => new VerifyCS.Test { TestCode = """ $${|CS8805:System.Console.WriteLine(0);|} """, LanguageVersion = LanguageVersion.CSharp10, }.RunAsync(); [Fact] public Task TestConvertToProgramMainWithTopLevelStatementPreferenceSuggestion() => new VerifyCS.Test { TestCode = """ $$System.Console.WriteLine(0); """, FixedCode = """ internal class Program { private static void Main(string[] args) { System.Console.WriteLine(0); } } """, LanguageVersion = LanguageVersion.CSharp10, TestState = { OutputKind = OutputKind.ConsoleApplication }, Options = { { CSharpCodeStyleOptions.PreferTopLevelStatements, true, NotificationOption2.Suggestion }, } }.RunAsync(); [Fact] public Task TestNoConvertToProgramMainWithProgramMainPreferenceSuggestion() => new VerifyCS.Test { TestCode = """ $$System.Console.WriteLine(0); """, LanguageVersion = LanguageVersion.CSharp10, TestState = { OutputKind = OutputKind.ConsoleApplication }, Options = { { CSharpCodeStyleOptions.PreferTopLevelStatements, false, NotificationOption2.Suggestion }, } }.RunAsync(); [Fact] public Task TestNoConvertToProgramMainWithProgramMainPreferenceSilent() => new VerifyCS.Test { TestCode = """ $$System.Console.WriteLine(0); """, LanguageVersion = LanguageVersion.CSharp10, TestState = { OutputKind = OutputKind.ConsoleApplication }, Options = { { CSharpCodeStyleOptions.PreferTopLevelStatements, false, NotificationOption2.Silent }, } }.RunAsync(); [Fact] public Task TestConvertToProgramMainWithProgramMainPreferenceSuppress() => new VerifyCS.Test { TestCode = """ $$System.Console.WriteLine(0); """, FixedCode = """ internal class Program { private static void Main(string[] args) { System.Console.WriteLine(0); } } """, LanguageVersion = LanguageVersion.CSharp10, TestState = { OutputKind = OutputKind.ConsoleApplication }, Options = { { CSharpCodeStyleOptions.PreferTopLevelStatements, false, NotificationOption2.None }, } }.RunAsync(); }