107 lines
3.8 KiB
C#
107 lines
3.8 KiB
C#
// Copyright (c) .NET Foundation. All rights reserved.
|
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
|
|
using Microsoft.CodeAnalysis;
|
|
using Microsoft.CodeAnalysis.Diagnostics;
|
|
using TestHelper;
|
|
using Xunit;
|
|
|
|
namespace Microsoft.AspNetCore.Components.Analyzers
|
|
{
|
|
public class ComponentParametersShouldBePublicTest : DiagnosticVerifier
|
|
{
|
|
[Fact]
|
|
public void IgnoresPublicProperties()
|
|
{
|
|
var test = $@"
|
|
namespace ConsoleApplication1
|
|
{{
|
|
using {typeof(ParameterAttribute).Namespace};
|
|
class TypeName
|
|
{{
|
|
[Parameter] public string MyProperty {{ get; set; }}
|
|
}}
|
|
}}" + ComponentsTestDeclarations.Source;
|
|
|
|
VerifyCSharpDiagnostic(test);
|
|
}
|
|
|
|
[Fact]
|
|
public void IgnoresPrivateNonParameterProperties()
|
|
{
|
|
var test = $@"
|
|
namespace ConsoleApplication1
|
|
{{
|
|
using {typeof(ParameterAttribute).Namespace};
|
|
class TypeName
|
|
{{
|
|
private string MyProperty {{ get; set; }}
|
|
}}
|
|
}}" + ComponentsTestDeclarations.Source;
|
|
|
|
VerifyCSharpDiagnostic(test);
|
|
}
|
|
|
|
[Fact]
|
|
public void ErrorsForNonPublicParameters()
|
|
{
|
|
var test = $@"
|
|
namespace ConsoleApplication1
|
|
{{
|
|
using {typeof(ParameterAttribute).Namespace};
|
|
class TypeName
|
|
{{
|
|
[Parameter] string MyProperty1 {{ get; set; }}
|
|
[Parameter] private string MyProperty2 {{ get; set; }}
|
|
[Parameter] protected string MyProperty3 {{ get; set; }}
|
|
[Parameter] internal string MyProperty4 {{ get; set; }}
|
|
}}
|
|
}}" + ComponentsTestDeclarations.Source;
|
|
|
|
VerifyCSharpDiagnostic(test,
|
|
new DiagnosticResult
|
|
{
|
|
Id = DiagnosticDescriptors.ComponentParametersShouldBePublic.Id,
|
|
Message = "Component parameter 'ConsoleApplication1.TypeName.MyProperty1' should be public.",
|
|
Severity = DiagnosticSeverity.Error,
|
|
Locations = new[]
|
|
{
|
|
new DiagnosticResultLocation("Test0.cs", 7, 32)
|
|
}
|
|
},
|
|
new DiagnosticResult
|
|
{
|
|
Id = DiagnosticDescriptors.ComponentParametersShouldBePublic.Id,
|
|
Message = "Component parameter 'ConsoleApplication1.TypeName.MyProperty2' should be public.",
|
|
Severity = DiagnosticSeverity.Error,
|
|
Locations = new[]
|
|
{
|
|
new DiagnosticResultLocation("Test0.cs", 8, 40)
|
|
}
|
|
},
|
|
new DiagnosticResult
|
|
{
|
|
Id = DiagnosticDescriptors.ComponentParametersShouldBePublic.Id,
|
|
Message = "Component parameter 'ConsoleApplication1.TypeName.MyProperty3' should be public.",
|
|
Severity = DiagnosticSeverity.Error,
|
|
Locations = new[]
|
|
{
|
|
new DiagnosticResultLocation("Test0.cs", 9, 42)
|
|
}
|
|
},
|
|
new DiagnosticResult
|
|
{
|
|
Id = DiagnosticDescriptors.ComponentParametersShouldBePublic.Id,
|
|
Message = "Component parameter 'ConsoleApplication1.TypeName.MyProperty4' should be public.",
|
|
Severity = DiagnosticSeverity.Error,
|
|
Locations = new[]
|
|
{
|
|
new DiagnosticResultLocation("Test0.cs", 10, 41)
|
|
}
|
|
});
|
|
}
|
|
|
|
protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() => new ComponentParameterAnalyzer();
|
|
}
|
|
}
|