/
githubmirror
/
aspnetcore
Обзор
Документация
Войти
/
githubmirror
/
aspnetcore
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Components/Analyzers/test/ComponentParametersShouldBePublicCodeFixProviderTest.cs
194 строки
6 KB
Damyan Petev
Fix Blazor [Parameter] public code fix handling of multiple access modifiers (#67365)
25 июн 2026, 19:45
Не верифицирован
25 июн 2026, 19:45
6ad0de2
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.Diagnostics; using TestHelper; namespace Microsoft.AspNetCore.Components.Analyzers.Test; public class ComponentParametersShouldBePublicCodeFixProviderTest : CodeFixVerifier { [Fact] public void IgnoresPrivatePropertiesWithoutParameterAttribute() { var test = @" namespace ConsoleApplication1 { class TypeName { private string MyProperty { get; set; } } }" + ComponentsTestDeclarations.Source; VerifyCSharpDiagnostic(test); } [Fact] public void AddsDiagnosticAndFixForPrivatePropertiesWithParameterAttribute() { var test = @" namespace ConsoleApplication1 { using " + typeof(ParameterAttribute).Namespace + @"; class TypeName { [Parameter] private string BadProperty1 { get; set; } } }" + ComponentsTestDeclarations.Source; VerifyCSharpDiagnostic(test, new DiagnosticResult { Id = DiagnosticDescriptors.ComponentParametersShouldBePublic.Id, Message = "Component parameter 'ConsoleApplication1.TypeName.BadProperty1' should be public.", Severity = DiagnosticSeverity.Error, Locations = new[] { new DiagnosticResultLocation("Test0.cs", 8, 40) } }); VerifyCSharpFix(test, @" namespace ConsoleApplication1 { using " + typeof(ParameterAttribute).Namespace + @"; class TypeName { [Parameter] public string BadProperty1 { get; set; } } }" + ComponentsTestDeclarations.Source); } [Fact] public void AddsDiagnosticAndFixForMultiModifierPropertiesWithParameterAttribute() { var test = @" namespace ConsoleApplication1 { using " + typeof(ParameterAttribute).Namespace + @"; class TypeName { [Parameter] private protected string BadProperty1 { get; set; } [Parameter] protected internal string BadProperty2 { get; set; } } class TypeName2 : TypeName { [Parameter] protected new internal string BadProperty2 { get; set; } } }" + ComponentsTestDeclarations.Source; VerifyCSharpDiagnostic(test, new DiagnosticResult { Id = DiagnosticDescriptors.ComponentParametersShouldBePublic.Id, Message = "Component parameter 'ConsoleApplication1.TypeName.BadProperty1' should be public.", Severity = DiagnosticSeverity.Error, Locations = new[] { new DiagnosticResultLocation("Test0.cs", 8, 50) } }, new DiagnosticResult { Id = DiagnosticDescriptors.ComponentParametersShouldBePublic.Id, Message = "Component parameter 'ConsoleApplication1.TypeName.BadProperty2' should be public.", Severity = DiagnosticSeverity.Error, Locations = new[] { new DiagnosticResultLocation("Test0.cs", 10, 51) } }, new DiagnosticResult { Id = DiagnosticDescriptors.ComponentParametersShouldBePublic.Id, Message = "Component parameter 'ConsoleApplication1.TypeName2.BadProperty2' should be public.", Severity = DiagnosticSeverity.Error, Locations = new[] { new DiagnosticResultLocation("Test0.cs", 14, 55) } }); VerifyCSharpFix(test, @" namespace ConsoleApplication1 { using " + typeof(ParameterAttribute).Namespace + @"; class TypeName { [Parameter] public string BadProperty1 { get; set; } [Parameter] public string BadProperty2 { get; set; } } class TypeName2 : TypeName { [Parameter] public new string BadProperty2 { get; set; } } }" + ComponentsTestDeclarations.Source); } [Fact] public void IgnoresPublicPropertiesWithNonPublicSetterWithParameterAttribute() { var test = @" namespace ConsoleApplication1 { using " + typeof(ParameterAttribute).Namespace + @"; class TypeName { [Parameter] public string MyProperty1 { get; private set; } [Parameter] public object MyProperty2 { get; protected set; } [Parameter] public object MyProperty3 { get; internal set; } } }" + ComponentsTestDeclarations.Source; VerifyCSharpDiagnostic(test, new DiagnosticResult { Id = DiagnosticDescriptors.ComponentParameterSettersShouldBePublic.Id, Message = "Component parameter 'ConsoleApplication1.TypeName.MyProperty1' should have a public setter.", Severity = DiagnosticSeverity.Error, Locations = new[] { new DiagnosticResultLocation("Test0.cs", 8, 39) } }, new DiagnosticResult { Id = DiagnosticDescriptors.ComponentParameterSettersShouldBePublic.Id, Message = "Component parameter 'ConsoleApplication1.TypeName.MyProperty2' should have a public setter.", Severity = DiagnosticSeverity.Error, Locations = new[] { new DiagnosticResultLocation("Test0.cs", 9, 39) } }, new DiagnosticResult { Id = DiagnosticDescriptors.ComponentParameterSettersShouldBePublic.Id, Message = "Component parameter 'ConsoleApplication1.TypeName.MyProperty3' should have a public setter.", Severity = DiagnosticSeverity.Error, Locations = new[] { new DiagnosticResultLocation("Test0.cs", 10, 39) } }); } protected override CodeFixProvider GetCSharpCodeFixProvider() { return new ComponentParametersShouldBePublicCodeFixProvider(); } protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() { return new ComponentParameterAnalyzer(); } }