/
githubmirror
/
roslyn
Обзор
Документация
Войти
/
githubmirror
/
roslyn
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Features/DiagnosticsTestUtilities/CodeActions/CSharpCodeFixVerifier`2+Test.cs
120 строк
5 KB
Cyrus Najmabadi
Use file scoped namespaces. (#77854)
27 мар 2025, 09:55
Не верифицирован
27 мар 2025, 09:55
98d41b8
Код
Авторство
О чём код?
// 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; using System.Collections.Immutable; using System.Diagnostics.CodeAnalysis; using System.Net; using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Testing; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Testing; using Xunit; #if !CODE_STYLE using Roslyn.Utilities; #endif namespace Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions; public static partial class CSharpCodeFixVerifier<TAnalyzer, TCodeFix> where TAnalyzer : DiagnosticAnalyzer, new() where TCodeFix : CodeFixProvider, new() { public class Test : CSharpCodeFixTest<TAnalyzer, TCodeFix, DefaultVerifier> { private readonly SharedVerifierState _sharedState; static Test() { // If we have outdated defaults from the host unit test application targeting an older .NET Framework, use more // reasonable TLS protocol version for outgoing connections. #pragma warning disable CA5364 // Do Not Use Deprecated Security Protocols #pragma warning disable CS0618 // Type or member is obsolete #pragma warning disable SYSLIB0014 // 'ServicePointManager' is obsolete if (ServicePointManager.SecurityProtocol == (SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls)) #pragma warning restore CS0618 // Type or member is obsolete #pragma warning restore CA5364 // Do Not Use Deprecated Security Protocols { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; } #pragma warning restore SYSLIB0014 // 'ServicePointManager' is obsolete } public Test() { _sharedState = new SharedVerifierState(this, DefaultFileExt); MarkupOptions = Testing.MarkupOptions.UseFirstDescriptor; } /// <summary> /// Gets or sets the language version to use for the test. The default value is /// <see cref="LanguageVersion.CSharp8"/>. /// </summary> public LanguageVersion LanguageVersion { get; set; } = LanguageVersion.CSharp8; /// <inheritdoc cref="SharedVerifierState.Options"/> internal OptionsCollection Options => _sharedState.Options; [StringSyntax(PredefinedEmbeddedLanguageNames.CSharpTest)] public new string TestCode { set => base.TestCode = value; } [StringSyntax(PredefinedEmbeddedLanguageNames.CSharpTest)] public new string FixedCode { set => base.FixedCode = value; } [StringSyntax(PredefinedEmbeddedLanguageNames.CSharpTest)] public new string BatchFixedCode { set => base.BatchFixedCode = value; } /// <inheritdoc cref="SharedVerifierState.EditorConfig"/> public string? EditorConfig { get => _sharedState.EditorConfig; set => _sharedState.EditorConfig = value; } public Func<ImmutableArray<Diagnostic>, Diagnostic?>? DiagnosticSelector { get; set; } public Action<ImmutableArray<CodeAction>>? CodeActionsVerifier { get; set; } protected override async Task RunImplAsync(CancellationToken cancellationToken = default) { if (DiagnosticSelector is object) { Assert.True(CodeFixTestBehaviors.HasFlag(Testing.CodeFixTestBehaviors.FixOne), $"'{nameof(DiagnosticSelector)}' can only be used with '{nameof(Testing.CodeFixTestBehaviors)}.{nameof(Testing.CodeFixTestBehaviors.FixOne)}'"); } _sharedState.Apply(); await base.RunImplAsync(cancellationToken); } protected override ParseOptions CreateParseOptions() { var parseOptions = (CSharpParseOptions)base.CreateParseOptions(); return parseOptions.WithLanguageVersion(LanguageVersion); } protected override CompilationOptions CreateCompilationOptions() { var compilationOptions = (CSharpCompilationOptions)base.CreateCompilationOptions(); return compilationOptions.WithSpecificDiagnosticOptions(compilationOptions.SpecificDiagnosticOptions.SetItems(CSharpVerifierHelper.NullableWarnings)); } protected override Diagnostic? TrySelectDiagnosticToFix(ImmutableArray<Diagnostic> fixableDiagnostics) { return DiagnosticSelector?.Invoke(fixableDiagnostics) ?? base.TrySelectDiagnosticToFix(fixableDiagnostics); } protected override ImmutableArray<CodeAction> FilterCodeActions(ImmutableArray<CodeAction> actions) { CodeActionsVerifier?.Invoke(actions); return base.FilterCodeActions(actions); } } }